@numero-uno-jpn/rstats 1.18.0 → 1.20.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.
- package/CHANGELOG.md +52 -0
- package/README.ja.md +7 -7
- package/README.md +7 -7
- package/dist/causal.js +2 -2
- package/dist/chunks/chunk-33ODAPMR.js +2 -0
- package/dist/chunks/chunk-3ST6RZXG.js +2 -0
- package/dist/chunks/{chunk-JF4PXFJE.js → chunk-4EQN74JN.js} +2 -2
- package/dist/chunks/chunk-5RRFO4NU.js +2 -0
- package/dist/chunks/{chunk-3JFRDXN7.js → chunk-64DDDGJU.js} +2 -2
- package/dist/chunks/chunk-66TEOHPF.js +2 -0
- package/dist/chunks/chunk-6ISDROPG.js +2 -0
- package/dist/chunks/{chunk-EC77KAAM.js → chunk-6LAKI2QG.js} +2 -2
- package/dist/chunks/{chunk-5CTWYFMB.js → chunk-6QCH4LAL.js} +2 -2
- package/dist/chunks/{chunk-H6Z7JVSD.js → chunk-6RA73LXX.js} +2 -2
- package/dist/chunks/{chunk-KJMLQHCY.js → chunk-7C2ECGT6.js} +1 -1
- package/dist/chunks/{chunk-SCFFHEDZ.js → chunk-7KPFNJBE.js} +2 -2
- package/dist/chunks/chunk-ARPXG2PG.js +2 -0
- package/dist/chunks/chunk-AWXW4WSX.js +2 -0
- package/dist/chunks/{chunk-XQCHC3SH.js → chunk-B5GSH3AK.js} +1 -1
- package/dist/chunks/{chunk-2JB7F646.js → chunk-BLDLJHUG.js} +2 -2
- package/dist/chunks/{chunk-IKMW63BE.js → chunk-BU4I544S.js} +2 -2
- package/dist/chunks/{chunk-LCIX76FR.js → chunk-CAUNJ7S6.js} +2 -2
- package/dist/chunks/chunk-DFGW4QWU.js +2 -0
- package/dist/chunks/{chunk-WYVUCQO3.js → chunk-DPCCSFCG.js} +1 -1
- package/dist/chunks/chunk-EXS5QD7V.js +2 -0
- package/dist/chunks/chunk-FOCHNNP3.js +2 -0
- package/dist/chunks/{chunk-MXZ64QME.js → chunk-HEPGGURG.js} +2 -2
- package/dist/chunks/chunk-INNFAVT3.js +2 -0
- package/dist/chunks/chunk-JEELIABM.js +2 -0
- package/dist/chunks/chunk-KNF2GPJV.js +2 -0
- package/dist/chunks/chunk-L4ZS7MAX.js +2 -0
- package/dist/chunks/{chunk-RXYZQ3UX.js → chunk-LZAB4M7P.js} +2 -2
- package/dist/chunks/chunk-LZTDYQRS.js +3 -0
- package/dist/chunks/{chunk-N7BUE54L.js → chunk-MTSU2QHB.js} +2 -2
- package/dist/chunks/{chunk-GIK34P23.js → chunk-MXRN2G7O.js} +2 -2
- package/dist/chunks/chunk-OFDD4GTZ.js +2 -0
- package/dist/chunks/chunk-OOHFFLMY.js +2 -0
- package/dist/chunks/chunk-OOO5AAM7.js +2 -0
- package/dist/chunks/chunk-PJJJR3H6.js +2 -0
- package/dist/chunks/chunk-PVRNAOO7.js +2 -0
- package/dist/chunks/chunk-Q2JLU5YD.js +2 -0
- package/dist/chunks/{chunk-Z33Q545U.js → chunk-QCQBK5AC.js} +2 -2
- package/dist/chunks/chunk-RXK4KSKH.js +2 -0
- package/dist/chunks/chunk-UC3YYJHM.js +4 -0
- package/dist/chunks/chunk-UPACHZ4F.js +2 -0
- package/dist/chunks/{chunk-VATAUY7N.js → chunk-VCXPGCA4.js} +2 -2
- package/dist/chunks/chunk-VFL4GGYT.js +2 -0
- package/dist/chunks/chunk-VL72MQOQ.js +2 -0
- package/dist/chunks/chunk-VSV5QGXX.js +2 -0
- package/dist/chunks/{chunk-CTZUYKVL.js → chunk-WMEF5XWO.js} +2 -2
- package/dist/chunks/chunk-X6JS6ZZB.js +2 -0
- package/dist/chunks/chunk-YEDVOX5V.js +2 -0
- package/dist/chunks/chunk-ZCTB4N5J.js +3 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions/normal.d.ts +4 -0
- package/dist/distributions/normal.js +4 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/tests.d.ts +6 -0
- package/dist/entries/tests.js +6 -0
- package/dist/entries/timeseries.d.ts +11 -0
- package/dist/entries/timeseries.js +11 -0
- package/dist/htest/timeROC.d.ts +564 -0
- package/dist/htest/timeROC.js +1106 -0
- package/dist/linalg.js +2 -2
- package/dist/meta/metabin.d.ts +32 -0
- package/dist/meta/metabin.js +137 -0
- package/dist/meta/metaextra.d.ts +153 -0
- package/dist/meta/metaextra.js +290 -0
- package/dist/meta/metagen.d.ts +124 -0
- package/dist/meta/metagen.js +263 -0
- package/dist/meta/metaother.d.ts +87 -0
- package/dist/meta/metaother.js +342 -0
- package/dist/meta/netmeta.d.ts +256 -0
- package/dist/meta/netmeta.js +547 -0
- package/dist/mixed.js +3 -4
- package/dist/models/glmFamily.js +4 -1
- package/dist/models/metaAnalysis.d.ts +4 -0
- package/dist/models/metaAnalysis.js +100 -18
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/regression.js +2 -2
- package/dist/rstats.js +20 -18
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/dist/timeseries/acf.js +5 -2
- package/dist/timeseries/arima.d.ts +154 -0
- package/dist/timeseries/arima.js +1007 -0
- package/dist/timeseries/autoArima.d.ts +70 -0
- package/dist/timeseries/autoArima.js +191 -0
- package/dist/timeseries/ets.d.ts +158 -0
- package/dist/timeseries/ets.js +883 -0
- package/dist/timeseries/forecastArima.d.ts +136 -0
- package/dist/timeseries/forecastArima.js +432 -0
- package/dist/timeseries/forecastDispatch.d.ts +83 -0
- package/dist/timeseries/forecastDispatch.js +130 -0
- package/dist/timeseries/forecastSimple.d.ts +177 -0
- package/dist/timeseries/forecastSimple.js +371 -0
- package/dist/timeseries/forecastUtils.d.ts +260 -0
- package/dist/timeseries/forecastUtils.js +619 -0
- package/dist/timeseries/holtWinters.d.ts +65 -0
- package/dist/timeseries/holtWinters.js +193 -0
- package/dist/timeseries/input.d.ts +8 -0
- package/dist/timeseries/input.js +27 -0
- package/dist/timeseries/ocsbCritical.d.ts +2 -0
- package/dist/timeseries/ocsbCritical.js +6 -0
- package/dist/timeseries/scale.d.ts +16 -0
- package/dist/timeseries/scale.js +48 -0
- package/dist/timeseries/statsTs.d.ts +128 -0
- package/dist/timeseries/statsTs.js +456 -0
- package/dist/timeseries/stl.d.ts +131 -0
- package/dist/timeseries/stl.js +489 -0
- package/dist/timeseries/tseries.d.ts +110 -0
- package/dist/timeseries/tseries.js +369 -0
- package/dist/timeseries.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-53WQDB3C.js +0 -2
- package/dist/chunks/chunk-6I23C2YJ.js +0 -2
- package/dist/chunks/chunk-FLFNMSHI.js +0 -3
- package/dist/chunks/chunk-GEHMND3C.js +0 -2
- package/dist/chunks/chunk-HG4LJBNV.js +0 -2
- package/dist/chunks/chunk-HGAEVPVS.js +0 -2
- package/dist/chunks/chunk-HMLYEFQN.js +0 -2
- package/dist/chunks/chunk-HRRCC2BD.js +0 -2
- package/dist/chunks/chunk-IBHMM2SL.js +0 -2
- package/dist/chunks/chunk-ICDW3I2F.js +0 -2
- package/dist/chunks/chunk-IOJVFYHW.js +0 -2
- package/dist/chunks/chunk-IW4S634O.js +0 -2
- package/dist/chunks/chunk-JPWYGTUN.js +0 -2
- package/dist/chunks/chunk-KVJBCT3D.js +0 -2
- package/dist/chunks/chunk-LY5XGFPU.js +0 -2
- package/dist/chunks/chunk-MKZH65DK.js +0 -2
- package/dist/chunks/chunk-MVPHISOV.js +0 -2
- package/dist/chunks/chunk-NDZVVNBB.js +0 -2
- package/dist/chunks/chunk-NOTJ45G3.js +0 -4
- package/dist/chunks/chunk-QUD3374N.js +0 -2
- package/dist/chunks/chunk-V6FMGGUV.js +0 -2
- package/dist/chunks/chunk-W5M3R4NH.js +0 -2
- package/dist/chunks/chunk-XUMVTGMM.js +0 -2
- package/dist/chunks/chunk-YXMHWS5R.js +0 -2
package/dist/tests.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
/*! rStats.js 1.
|
|
2
|
-
import{a as en,b as Vn,c as Zn,d as dr,e as ws,f as va}from"./chunks/chunk-5CTWYFMB.js";import{a as xe,b as qe,c as bn,d as it,e as No,f as bh,g as vh,h as wa,i as ba,j as Nh,k as il,l as vs,m as Jn,n as Ns,o as fl,p as wr,q as ml,r as Mh,s as Eh,t as Ma,u as Ea,v as pl,w as dl,x as gl,y as xa,z as kh}from"./chunks/chunk-HMLYEFQN.js";import{a as gn,c as on,h as $r,i as jc,m as Kc,n as zr,o as Wc,s as go,t as is,u as cs,v as ls}from"./chunks/chunk-6I23C2YJ.js";import{b as Qc}from"./chunks/chunk-EC77KAAM.js";import{a as Ra,e as Ta}from"./chunks/chunk-NDZVVNBB.js";import{a as ms,b as Qn,c as vo,d as el,e as tl,f as us,g as Ge,h as an,i as wn,j as It,k as ol,l as yh,n as Ms,o as Mo}from"./chunks/chunk-MKZH65DK.js";import{a as Es}from"./chunks/chunk-VATAUY7N.js";import{a as as,b as Yc}from"./chunks/chunk-HGAEVPVS.js";import"./chunks/chunk-SCFFHEDZ.js";import"./chunks/chunk-QUD3374N.js";import{a as zc,d as ss}from"./chunks/chunk-ICDW3I2F.js";import{b as qh,c as Ih,d as _h}from"./chunks/chunk-HRRCC2BD.js";import{f as ll,g as br,h as er,i as ys,k as yo,l as ul,m as ya,n as xh,o as Rh,q as Th,r as Sh,s as Ah}from"./chunks/chunk-IOJVFYHW.js";import{b as Mt,c as Cn}from"./chunks/chunk-LCIX76FR.js";import{b as Eo}from"./chunks/chunk-FLFNMSHI.js";import{a as nl,c as rl,e as pr}from"./chunks/chunk-Z33Q545U.js";import{b as ga}from"./chunks/chunk-WYVUCQO3.js";import{a as at,b as hs,c as ps}from"./chunks/chunk-CTZUYKVL.js";import{c as gs,d as Na}from"./chunks/chunk-NOTJ45G3.js";import{a as gr,b as Kr}from"./chunks/chunk-RXYZQ3UX.js";import"./chunks/chunk-H6Z7JVSD.js";import{d as rt}from"./chunks/chunk-JPWYGTUN.js";import{a as jt,d as al,e as ds,k as bs,o as un,p as hl}from"./chunks/chunk-IBHMM2SL.js";import{a as Xn,b as Jc,g as jr}from"./chunks/chunk-GIK34P23.js";import{d as Zo,e as Jo,g as es,h as Pr,m as fn,n as On,s as da,t as vt,u as xt,w as dn,x as rn}from"./chunks/chunk-IW4S634O.js";import{A as xn,B as Vr,E as Yn,i as gt,l as Qo,u as Pc,x as wt,y as ln}from"./chunks/chunk-LY5XGFPU.js";import{a as cl}from"./chunks/chunk-IKMW63BE.js";import{a as Rn,c as fs,f as sl}from"./chunks/chunk-MXZ64QME.js";import{b as Bc,c as Uc,d as Gc,e as Hc,f as Xc,i as wo,l as wh,n as H,o as je,p as mt}from"./chunks/chunk-N7BUE54L.js";import{a as bo,b as Pn,c as Zc,d as Ot,e as Rt}from"./chunks/chunk-XQCHC3SH.js";import{a as At,b as Hn,c as sn}from"./chunks/chunk-KJMLQHCY.js";import"./chunks/chunk-YXMHWS5R.js";import{h as _c,i as Lc,k as Cc,u as Oc,v as Fc,z as Dc}from"./chunks/chunk-53WQDB3C.js";import{f as ha,r as Xt,s as ie,t as oe}from"./chunks/chunk-GEHMND3C.js";import{A as Dt,B as os,C as Re,e as Fn,f as mn,g as Dn,h as $,i as et,j as Bn,k as st,l as ot,m as Un,n as Vc,p as ts,q as Et,r as ns,s as $c,t as rs,u as pa,v as Ie,w as bt,x as pn,y as Ct,z as Gn}from"./chunks/chunk-HG4LJBNV.js";import{b as $t,c as Ht,d as ue,e as qc,f as Ce,g as _e,h as zt,i as lt,j as ft,k as Ic,l as V,m as Ft}from"./chunks/chunk-MVPHISOV.js";var wl=["alternative","mu","paired","varEqual","confLevel"];function vn(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,wl,"tTest"),V(n,wl,"tTest"),typeof e=="string")return Lh(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.varEqual??!1,a=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(an(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[k,L]=Gn(Array.from(e),Array.from(r));({d:l,k:m}=ns(k,L))}else{let k=Re(r===void 0?[Ct(Array.from(e))]:[Ct(Array.from(e)),Ct(Array.from(r))]);m=k.k,l=k.groups[0],f=k.groups[1]}let u=$(s,-m),h,p,d,g,w,N,v,E=l.length,b=f!==void 0&&c;if(E<1||!b&&E<2)throw new Error("not enough 'x' observations");let M=st(l),x=E>1?ot(l):NaN;if(f===void 0){if(p=E-1,d=Math.sqrt(x/E),d===0||d<10*Number.EPSILON*Math.abs(M))throw new Error("data are essentially constant");h=(M-u)/d,N=i?"Paired t-test":"One Sample t-test",g=M,w=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let k=f.length;if(k<1||!c&&k<2)throw new Error("not enough 'y' observations");if(c&&E+k<3)throw new Error("not enough observations");let L=st(f),_=k>1?ot(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,g=[M,L],w=["mean of x","mean of y"],v="difference in means",c){p=E+k-2;let I=0;E>1&&(I+=(E-1)*x),k>1&&(I+=(k-1)*_),I/=p,d=Math.sqrt(I*(1/E+1/k))}else{let I=x/E,F=_/k;d=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(F)**2),p=rs([I,F],[E-1,k-1])}if(d===0||d<10*Number.EPSILON*Math.max(Math.abs(M),Math.abs(L)))throw new Error("data are essentially constant");h=(M-L-u)/d}let y=typeof g=="number"?g:g[0]-g[1],R=(k,L)=>{let _=u+(h+L*k)*d;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*d))<=16*(Math.abs(y)+k*d)?_:y+L*k*d},T,S;if(o==="less")T=vt(h,p,!0,!1),S=[-1/0,R(xt(a,p,!0,!1),1)];else if(o==="greater")T=vt(h,p,!1,!1),S=[R(xt(a,p,!0,!1),-1),1/0];else{T=2*vt(-Math.abs(h),p,!0,!1);let k=1-a,L=k/2<1e-8?xt(k/2,p,!1,!1):xt(1-k/2,p,!0,!1);S=[R(L,-1),R(L,1)]}return S=[$(S[0],m),$(S[1],m)],g=typeof g=="number"?$(g,m):g.map(k=>$(k,m)),d=$(d,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:S,confLevel:a,estimate:g,estimateNames:w,nullValue:s,nullValueName:v,stdErr:d,stdError:d,warnings:[]}}function Lh(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=ms(e,t,!0);if(r.kind==="one")return vn(r.x,n);if(r.kind==="paired")return vn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...vn(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${s}`],nullValueName:`difference in means between group ${o} and group ${s}`}}var Ch=["correct","p","rescaleP"],Oh=["confLevel"],Fh=["alternative","rowScores","colScores"];function Qt(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function Aa(e){let t=e.map(r=>{if(typeof r!="object"||r===null||typeof r.length!="number")throw new TypeError("'x' must be an array of numbers or an array of rows");return Array.from(r)}),n=t[0].length;if(t.some(r=>r.length!==n))throw new TypeError("all rows of the matrix must have the same length");if(t.flat().some(r=>typeof r!="number"))throw new TypeError("the entries of 'x' must be numbers");return t}var ka=e=>e.length>0&&typeof e[0]!="number",qa=(e,t)=>Number.isNaN(e)?NaN:gt(e,t/2,2,!1,!1);function bl(e,t){if(!(e>=0&&t>=0&&e<1/0&&t<1/0))return NaN;let n=Math.sqrt(e),r=Math.sqrt(e+1),o=n*r+t,s=(e-t)*(e+t);if(Number.isFinite(s)){let c=o===0?0:(s+e)/o;return(2*(e-t)+2*c)/(n+r+Math.sqrt(4*t+1))}let i=(e-t)*((e+t)/o)+e/o;return(.5*(e-t)+.5*i)/((n+r+2*Math.sqrt(t+.25))/4)}function xo(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>bl(o,t[r][s]))):e.map((n,r)=>bl(n,t[r]))}var Dh=["none","williams","yates"],Nl="Log likelihood ratio (G-test)";function Ph(e,t={}){V(t,Ch,"gTest");let n=xe(t.correct,Dh);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(ka(e)){let o=Aa(e);return vl(o.flat()),o.length===1?H("gTest",Sa(o[0],n,t)):o[0].length===1?H("gTest",Sa(o.map(s=>s[0]),n,t)):H("gTest",Vh(o,n))}let r=Array.from(e);if(r.some(o=>typeof o!="number"))throw new TypeError("the entries of 'x' must be numbers");return vl(r),H("gTest",Sa(r,n,t))}function vl(e){if(e.some(t=>!(t>=0&&t<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");if(!e.some(t=>t>0))throw new RangeError("at least one entry of 'x' must be positive")}function yl(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Ml(e,t){if(e.some(r=>r<0))return NaN;let n=[0,0];for(let r=0;r<e.length;r++){let o=e[r],s=t[r];if(o>0&&s[0]===0)return 1/0;n=ue(n,Lc(o,s))}return n[0]+n[1]}function Vh(e,t){let n=e.length,r=e[0].length;if(t==="yates"&&(n!==2||r!==2))throw new RangeError("Yates' correction requires a 2 x 2 matrix");let o=Qt(e.flat()),s=yl(o[0],n*r),i=s===0?e:e.map(v=>v.map(E=>$(E,-s))),c=s===0?o:Qt(i.flat()),a=i.map(v=>Qt(v)),l=i[0].map((v,E)=>Qt(i.map(b=>b[E]))),f=a.map(v=>l.map(E=>_e(v,lt(E,c)))),m=i;if(t==="yates"){let[[v,E],[b,M]]=i,x=v*M-E*b>0?-.5:.5,y=$(.5,-s),R=x<0?-y:y;m=[[v+R,E-R],[b-R,M+R]]}let u=Ml(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(a.some(M=>M[0]===0)||l.some(M=>M[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],E=a.reduce((M,x)=>M+1/(x[0]+x[1]),0),b=l.reduce((M,x)=>M+1/(x[0]+x[1]),0);p=1+$((v*E-1)*(v*b-1)/(6*v*(n-1)*(r-1)),-s)}let d=$(2*u,s)/p,g=(n-1)*(r-1),w=f.map(v=>v.map(E=>$(E[0]+E[1],s)));return{method:`${Nl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:d,statisticName:"G",parameter:g,parameterName:"X-squared df",pValue:qa(d,g),observed:s===0?m:m.map(v=>v.map(E=>$(E,s))),expected:w,ftResiduals:xo(e,w),warnings:h}}function Sa(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(v=>typeof v!="number"||Number.isNaN(v)))throw new RangeError("'p' must not contain missing values");if(s.some(v=>v<0))throw new RangeError("probabilities must be non-negative");if(s.some(v=>v===1/0))throw new RangeError("probabilities must be finite");let i=Qt(s);if(o){if(!(i[0]>0&&i[0]<1/0))throw new RangeError("the probabilities to rescale must have a positive finite sum")}else if(Math.abs(i[0]-1+i[1])>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let c=Qt(e),a=yl(c[0],r),l=a===0?e:e.map(v=>$(v,-a)),f=a===0?c:Qt(l),m=s.map(v=>o?lt(zt(f,v),i):zt(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=$(.5,-a),E=l[0]-u[0];E>$(.25,-a)?h=[l[0]-v,l[1]+v]:-E>$(.25,-a)&&(h=[l[0]+v,l[1]-v])}let p=Ml(h,m);if(!o&&!Number.isNaN(p)){let v=f[0]+f[1];p-=v*(i[0]-1+i[1])}let d=Number.isNaN(p)?["NaNs produced"]:[],g=t==="williams"?1+(r+1)/(6*$(f[0]+f[1],a)):1,w=$(2*p,a)/g,N=u.map(v=>$(v,a));return{method:`${Nl} goodness of fit test`,statistic:w,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:qa(w,r-1),observed:a===0?h:h.map(v=>$(v,a)),expected:N,ftResiduals:xo(e,N),warnings:d}}function $h(e){if(typeof e!="object"||e===null||!ka(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Aa(e),n=t.length;if(n<2||t[0].length!==n)throw new RangeError("'x' must be square with at least two rows and columns");if(t.flat().some(r=>!(r>=0&&r<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");return t}function zh(e){let t=$h(e),n=Qt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(b=>b.map(M=>$(M,-r))),s=o.length,i=o.map(b=>Qt(b)),c=o.map((b,M)=>Qt(o.map(x=>x[M]))),a=o.map((b,M)=>o[M].some((x,y)=>y!==M&&x>0)||o.some((x,y)=>y!==M&&x[M]>0)).flatMap((b,M)=>b?[M]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((b,M)=>M),m=b=>f[b]===b?b:f[b]=m(f[b]);for(let b=0;b<l;b++)for(let M=b+1;M<l;M++)o[a[b]][a[M]]+o[a[M]][a[b]]>0&&(f[m(b)]=m(M));if(a.some((b,M)=>m(M)!==m(0)))throw new RangeError("the categories fall into groups without counts between them: the covariance matrix is singular");let u=l-1,h=a.slice(0,u).map(b=>{let M=ue(i[b],[-c[b][0],-c[b][1]]);return M[0]+M[1]}),p=a.slice(0,u).map(b=>a.slice(0,u).map(M=>{if(b!==M)return-(o[b][M]+o[M][b]);let x=0;for(let y=0;y<s;y++)y!==b&&(x+=o[b][y]+o[y][b]);return x})),d=p.map(b=>b.map(()=>0)),g=[];for(let b=0;b<u;b++){let M=p[b][b];for(let x=0;x<b;x++)M-=d[b][x]*d[b][x]*g[x];g.push(M);for(let x=b+1;x<u;x++){let y=p[x][b];for(let R=0;R<b;R++)y-=d[x][R]*d[b][R]*g[R];d[x][b]=y/M}}let w=0,N=[];for(let b=0;b<u;b++){let M=h[b];for(let x=0;x<b;x++)M-=d[b][x]*N[x];N.push(M),w+=M*M/g[b]}let v=Qt(a.flatMap(b=>a.map(M=>t[b][M]))),E=l-1;return w=$(w,r),H("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:w,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:qa(w,E),N:v[0]+v[1],warnings:[]})}function jh(e,t={}){V(t,Oh,"goodmanKruskalGamma");let n=t.confLevel??.95;if(!(typeof n=="number"&&n>0&&n<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let r=El(e),o=Qt(r.flat())[0],s=0;(o>2**250||o>0&&o<2**-250)&&(s=2*Math.round((Math.log2(o)-250)/2));let i=s===0?r:r.map(b=>b.map(M=>$(M,-s))),{pc:c,pd:a}=Kh(i),l=[0,0],f=[0,0];i.forEach((b,M)=>b.forEach((x,y)=>{l=ue(l,zt(c[M][y],x)),f=ue(f,zt(a[M][y],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=ue(l,f),u=lt(Ce(l,f),m)[0],h=_e(m,m),p=i.map((b,M)=>b.map((x,y)=>2*lt(Ce(_e(f,c[M][y]),_e(l,a[M][y])),h)[0])),d=Math.max(...p.flat().map(Math.abs)),g=d>0&&d<1/0?Math.floor(Math.log2(d)):0,w=0;i.forEach((b,M)=>b.forEach((x,y)=>{let R=$(p[M][y],-g);w+=x*R*R}));let N=$(Math.sqrt(w),g-s/2),v=(1-n)/2,E=v<1e-8?oe(v,{lowerTail:!1}):oe(1-v);return H("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function El(e){if(typeof e!="object"||e===null||!ka(e))throw new TypeError("'table' must be an array of rows");let t=Aa(e);if(t[0].length===0)throw new RangeError("'table' must have at least one column");if(t.flat().some(n=>!(n>=0&&n<1/0)))throw new RangeError("all entries of 'table' must be nonnegative and finite");return t}function Kh(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let d=[],g=r;for(let w=0;w<n;w++)d.push(g),g=ue(g,[p[w],0]);return d}),s=e.map(p=>{let d=new Array(n),g=r;for(let w=n-1;w>=0;w--)d[w]=g,g=ue(g,[p[w],0]);return d}),i=p=>{let d=[],g=new Array(n).fill(r);for(let w=0;w<t;w++)d.push(g),g=g.map((N,v)=>ue(N,p[w][v]));return d},c=p=>{let d=new Array(t),g=new Array(n).fill(r);for(let w=t-1;w>=0;w--)d[w]=g,g=g.map((N,v)=>ue(N,p[w][v]));return d},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,d)=>p.map((g,w)=>ue(g,l[d][w]))),h=f.map((p,d)=>p.map((g,w)=>ue(g,m[d][w])));return{pc:u,pd:h}}function Wh(e,t={}){V(t,Fh,"lblTest");let n=Ge(t.alternative),r=El(e),o=r.length,s=r[0].length,i=(R,T,S)=>{if(R===void 0)return Array.from({length:T},(L,_)=>_+1);let k=Array.from(R);if(k.length!==T)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(k.some(L=>typeof L!="number"||!Number.isFinite(L)))throw new RangeError(`'${S}' must be finite numbers`);return k},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Qt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(R=>R.map(T=>$(T,-f))),u=m.map(R=>Qt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Qt(m.map(S=>S[T]))).map(R=>R[0]+R[1]),p=u.reduce((R,T)=>R+T,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let d=u.reduce((R,T,S)=>R+T*c[S],0)/p,g=h.reduce((R,T,S)=>R+T*a[S],0)/p,w=c.map(R=>R-d),N=a.map(R=>R-g),v=u.reduce((R,T,S)=>R+T*w[S]*w[S],0),E=h.reduce((R,T,S)=>R+T*N[S]*N[S],0),b=0;if(m.forEach((R,T)=>R.forEach((S,k)=>b+=S*w[T]*N[k])),!(v>0&&E>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let M=b/Math.sqrt(v)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*M:$(Math.sqrt(p-$(1,-f))*M,f/2),y=n==="less"?ie(x):n==="greater"?ie(x,{lowerTail:!1}):2*ie(-Math.abs(x));return H("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:y,warnings:[]})}var xl=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function hn(e){let t=[0,0],n=0;for(let r of e)t=ue(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Yh(e,t,n){return at(t)&&V(t,xl,"chisqTest"),V(n,xl,"chisqTest"),Array.isArray(t)?H("chisqTest",Bh(e,t,n??{})):H("chisqTest",Uh(e,t??{}))}var Sl="Chi-squared approximation may be incorrect";function Bh(e,t,n){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let r=u=>u==null;for(let u of[...e,...t])if(!r(u)&&typeof u!="string")throw new TypeError("factor values must be strings (or null for missing values)");let o=[],s=[];e.forEach((u,h)=>{let p=t[h];!r(u)&&!r(p)&&(o.push(u),s.push(p))});let i=[...new Set(o)].sort(Rn),c=[...new Set(s)].sort(Rn);if(i.length<2||c.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let a=new Map(i.map((u,h)=>[u,h])),l=new Map(c.map((u,h)=>[u,h])),f=i.map(()=>c.map(()=>0));return o.forEach((u,h)=>f[a.get(u)][l.get(s[h])]++),{..._l(f,n),rowLevels:i,colLevels:c}}function Uh(e,t){if(e.length>0&&typeof e[0]!="number"){let r=e.map(s=>Array.from(s)),o=r[0].length;if(r.some(s=>s.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?Ia(r[0],t):o===1?Ia(r.map(s=>s[0]),t):_l(r,t)}return Ia(Array.from(e),t)}function Al(e){let t=e.B??2e3;if(!(Number.isInteger(t)&&t>=1&&t<2**31))throw new RangeError("'B' must be a positive integer");return t}function kl(e,t,n){let r=(1-64*Number.EPSILON)*t,o=0;for(let s=0;s<e.length;s++)e[s]>=r&&o++;return(1+o)/(Math.trunc(n)+1)}function ql(e,t){return Number.isNaN(e)?NaN:gt(e,t/2,2,!1,!1)}function Il(e){if(!e.some(t=>t<5)&&e.some(Number.isNaN))throw new RangeError("missing value where TRUE/FALSE needed (the expected counts contain NaN)")}function Ia(e,t){if(t.nMinus1??!1)throw new RangeError("'nMinus1' requires a contingency table with at least two rows and columns");let n=e.length;if(e.some(d=>!(d>=0&&d<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=hn(e);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=t.p===void 0?e.map(()=>1/n):Array.from(t.p);if(o.length!==n)throw new TypeError("'x' and 'p' must have the same number of elements");if(o.some(Number.isNaN))throw new RangeError("p contains missing values");if(o.some(d=>d<0))throw new RangeError("probabilities must be non-negative");if(o.some(d=>d===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let d=hn(o),g=d<1/0?0:Math.ceil(Math.log2(n)),w=g===0?o:o.map(v=>$(v,-g)),N=g===0?d:hn(w);o=w.map(v=>v/N)}else if(Math.abs(hn(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r<1/0?0:2*Math.ceil(Math.log2(n)/2),i=s===0?e:e.map(d=>$(d,-s)),c=s===0?r:hn(i),a=o.map(d=>c*d),l=s===0?a:a.map(d=>$(d,s));Il(l);let f=o.map((d,g)=>a[g]*(1-d)),m=$(hn(i.map((d,g)=>Dt(d-a[g],a[g]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let d=Al(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=bo(d),h=NaN,p=kl(Gh(r,o,l,d),m,d)}else p=ql(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((d,g)=>$((d-a[g])/Math.sqrt(a[g]),s/2)),stdres:i.map((d,g)=>$((d-a[g])/Math.sqrt(f[g]),s/2)),ftResiduals:xo(e,l),warnings:Number.isFinite(h)&&l.some(d=>d<5)?[Sl]:[]}}function Gh(e,t,n,r){let o=t.length;if(r*e>2**27)throw new RangeError("the simulation needs B * sum(x) <= 2^27 (134217728) draws");let s=Qc(o,t);if(r*e===0)return new Float64Array(0);let i=new Float64Array(r),c=new Float64Array(o);for(let a=0;a<r;a++){c.fill(0);for(let f=0;f<e;f++)c[s()]++;let l=0;for(let f=0;f<o;f++)l+=(c[f]-n[f])**2/n[f];i[a]=l}return i}function Hh(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>is)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=cs(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){ls(e,t,c,a);let m=0;for(let u=0;u<s;u++)for(let h=0;h<o;h++){let p=n[h][u];m+=(a[h+u*o]-p)**2/p}l[f]=m}return l}function _l(e,t){let n=t.nMinus1??!1;if(typeof n!="boolean")throw new TypeError("'nMinus1' must be a boolean");if(n&&t.correct===!0)throw new RangeError("'nMinus1' uses no continuity correction: leave 'correct' out or set it to false");if(n&&(t.simulatePValue??!1))throw new RangeError("'nMinus1' cannot be combined with 'simulatePValue'");let r=!n&&(t.correct??!0),o=e.length,s=e[0].length,i=e.flat();if(i.some(v=>!(v>=0&&v<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=hn(i);if(!(c>0))throw new RangeError("at least one entry of 'x' must be positive");if(n&&!(c>1))throw new RangeError("'nMinus1' needs a table with a total above 1");let a=e.map(v=>hn(v)),l=e[0].map((v,E)=>hn(e.map(b=>b[E]))),f=a.map(v=>l.map(E=>os(v,E,c)));Il(f.flat());let m=e.map((v,E)=>v.map((b,M)=>b-f[E][M])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),d,g=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),w=!g&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!g&&N.push("cannot compute simulated p-value with zero marginals"),g){if(e.some(b=>b.some(M=>!Number.isInteger(M))))throw new RangeError("the simulation needs integer counts");let v=Al(t);u+=bo(v),h=hn(m.flatMap((b,M)=>b.map((x,y)=>Dt(x,f[M][y])))),p=NaN;let E=Hh(a.map(b=>Math.trunc(b)),l.map(b=>Math.trunc(b)),f,v);d=kl(E,h,v)}else if(w>0)u+=" with Yates' continuity correction",h=hn(m.flatMap((v,E)=>v.map((b,M)=>Dt(Math.abs(b)-w,f[E][M]))));else if(h=hn(m.flatMap((v,E)=>v.map((b,M)=>Dt(b,f[E][M])))),n){u="Pearson's 'N-1' Chi-squared test";let v=h*(c-1);h=Number.isFinite(v)?v/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:d??ql(h,p),observed:e,expected:f,residuals:m.map((v,E)=>v.map((b,M)=>b/Math.sqrt(f[E][M]))),stdres:Xh(e,c,a,l,f,m),ftResiduals:xo(e,f),warnings:Number.isFinite(p)&&f.some(v=>v.some(E=>E<5))?[...N,Sl]:N}}function Xh(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,d)=>p.map((g,w)=>g/Math.sqrt(o[d][w]*(1-n[d]/t)*(1-r[w]/t))));let i=e.map(p=>Rl(p)),c=e[0].map((p,d)=>Rl(e.map(g=>g[d]))),a=Zh(i),l=Tl(i),f=Tl(c),m=l.map(p=>lt(p,a)),u=c.map(p=>lt(p,a)),h=f.map(p=>lt(p,a));return e.map((p,d)=>p.map((g,w)=>{let N=_e(i[d],u[w]),v=Ce([g,0],N),E=v[0]+v[1],b=_e(_e(N,m[d]),h[w]),M=b[0]+b[1];if(M>=Qh||E===0)return E/Math.sqrt(M);let x=N[0]+N[1],y=m[d][0]+m[d][1],R=h[w][0]+h[w][1];return E/Math.sqrt(x)/Math.sqrt(y)/Math.sqrt(R)}))}var Qh=2**-1022;function Rl(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function Zh(e){let t=[0,0];for(let n of e)t=ue(t,n);return t}function Tl(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=ue(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(ue(o,n[s+1])),o=ue(o,e[s]);return r}var Jh=Math.pow(Number.EPSILON,.25);function Kt(e,t,n,r={}){let o={tol:r.tol??Jh,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),pr(e,t,n,o).root}function tr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let c=r<s?Kt(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Kt(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var ep=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function So(e,t={}){V(t,ep,"fisherTest");let n=e.length,r=n===0?0:e[0].length;if(e.some(f=>f.length!==r))throw new TypeError("all rows of the matrix must have the same length");if(n<2||r<2)throw new TypeError("'x' must have at least 2 rows and columns");let o=e.map(f=>Array.from(f));if(o.flat().some(f=>Number.isNaN(f)||f<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(f=>f===1/0))throw new RangeError("'x' has entries too large to be integer");let s=o.map(f=>f.map(Fc));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=tp(o,s);c!==null&&i.push(`'x' has been rounded to integer: ${c}`);let a="Fisher's Exact Test for Count Data";if(t.midp!==void 0&&typeof t.midp!="boolean")throw new TypeError("'midp' must be true or false");if((t.midp??!1)&&!(n===2&&r===2))throw new RangeError("the mid-p version ('midp') needs a 2 x 2 table");if(n===2&&r===2)return H("fisherTest",{...sp(s,t,a),warnings:i});let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let f=t.B??2e3;if(!(Number.isInteger(f)&&f>=1&&f<2**31))throw new RangeError("'B' must be a positive integer");return H("fisherTest",{method:a+bo(f),alternative:l,pValue:np(s,f),warnings:i})}return H("fisherTest",{method:a,alternative:l,pValue:ap(s),warnings:i})}function tp(e,t){let n=Math.sqrt(Number.EPSILON),r=[],o=[];for(let l=0;l<e[0].length;l++)for(let f=0;f<e.length;f++)e[f][l]!==t[f][l]&&(r.push(e[f][l]),o.push(t[f][l]));let s=r.length;if(s===0)return null;let i=0;for(let l of r)i+=Math.abs(l)/s;let c="relative";Number.isFinite(i)&&i>n||(i=1,c="absolute");let a=0;for(let l=0;l<s;l++)a+=Math.abs(r[l]-o[l])/(s*i);return Number.isNaN(a)||a>n?`Mean ${c} difference: ${Zc(a)}`:null}function np(e,t){let n=e.filter(g=>g.some(w=>w>0)),r=e[0].map((g,w)=>w).filter(g=>e.some(w=>w[g]>0));if(n.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(r.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let o=n.map(g=>r.map(w=>g[w])),s=o.map(g=>g.reduce((w,N)=>w+N,0)),i=o[0].map((g,w)=>o.reduce((N,v)=>N+v[w],0)),c=s.length,a=i.length,l=[0,0];for(let g of o.flat())l=ue(l,[-ss(g),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((g,w)=>g+w,0);if(m>is)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=cs(m),h=new Int32Array(c*a),p=0,d=Math.trunc(t);for(let g=0;g<d;g++){ls(s,i,u,h);let w=0;for(let N=0;N<h.length;N++)w-=u[h[N]];w<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(d+1)))}var Ll=1+1e-7,rp=16,op=2**24;function Ro(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=ue(r,[e[o],0]));return r[0]}var Wr=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let i=this.lo;i<=this.hi;i++){let c=i-this.lo;c%rp===0||!(s>0&&s<1/0)?(o=gn(i,t,n,r,!0),s=1,this.logCentral[c]=o):this.logCentral[c]=o+Math.log(s),s*=(t-i)*(r-i)/((i+1)*(n-r+i+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),s=-1/0;for(let c=0;c<n;c++){let a=this.logCentral[c]+(this.lo+c)*r;o[c]=a,a>s&&(s=a)}for(let c=0;c<n;c++){let a=o[c]-s;o[c]=a<-746?0:Math.exp(a)}let i=Ro(o,0,n-1);for(let c=0;c<n;c++)o[c]!==0&&(o[c]=o[c]/i);return o}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)n[o]!==0&&(r=ue(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?on(t-1,this.m,this.n,this.k,!1,!1):on(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return(r?t<=i:t>=i)?1:0}let o=this.density(n),s=t-this.lo;return r?Ro(o,s,o.length-1):Ro(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*gn(t,this.m,this.n,this.k,!1);return(r?on(t,this.m,this.n,this.k,!1,!1):on(t-1,this.m,this.n,this.k,!0,!1))+i}if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return t===i?.5:(r?t<i:t>i)?1:0}let o=this.density(n),s=t-this.lo;return(r?Ro(o,s+1,o.length-1):Ro(o,0,s-1))+.5*o[s]}};function La(e,t,n){let r=e.density(n),o=r[t-e.lo]*Ll,s=[0,0];for(let i of r)i<=o&&(s=ue(s,[i,0]));return s[0]}function Cl(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=d=>gn(d,t,n,r,!0),c=i(e)+Math.log(Ll),a=Math.min(s,Math.max(o,Math.floor((r+1)*(t+1)/(t+n+2))));for(;a<s&&i(a+1)>i(a);)a++;for(;a>o&&i(a-1)>i(a);)a--;let l=(d,g)=>{let w=d,N=g+1;for(;N-w>1;){let v=w+Math.floor((N-w)/2);i(v)<=c?w=v:N=v}return w},f=(d,g)=>{let w=d-1,N=g+1;for(;N-w>1;){let v=w+Math.floor((N-w)/2);i(v)<=c?N=v:w=v}return N},m,u;e<=a?(m=l(e,a),u=f(a+1,s)):(u=f(a,e),m=a-1<o||i(o)>c?o-1:l(o,a-1));let h=m<o?0:on(m,t,n,r,!0,!1),p=u>s?0:on(u-1,t,n,r,!1,!1);return h+p}function To(e,t){return tr(e,t)}function sp(e,t,n){let r=Ge(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[i,c],[a,l]]=e,f=i,m=Math.min(i,l)+Math.min(c,a)+1;if(m>op)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Wr(i+a,c+l,i+c),{lo:h,hi:p}=u,d=t.midp??!1,g;d?(r==="less"?g=u.midTail(f,o,!1):r==="greater"?g=u.midTail(f,o,!0):g=Math.min(1,2*Math.min(u.midTail(f,o,!1),u.midTail(f,o,!0))),n=r==="two.sided"?"Central Fisher's Exact Test (mid-p version)":"One-sided Fisher's Exact Test (mid-p version)"):r==="less"?g=u.tail(f,o,!1):r==="greater"?g=u.tail(f,o,!0):o===0?g=f===h?1:0:o===1/0?g=f===p?1:0:g=La(u,f,o);let w;if(f===h)w=0;else if(f===p)w=1/0;else{let M=u.mean(1);w=M===f?1:To(x=>u.mean(x)-f,M>f)}let N={method:n,alternative:r,pValue:g,estimate:w,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=M=>{if(f===p)return 1/0;if(d){if(f===h&&M>=.5)return 0;let y=u.midTail(f,1,!1);return y===M?1:To(R=>u.midTail(f,R,!1)-M,y<M)}let x=u.tail(f,1,!1);return x===M?1:To(y=>u.tail(f,y,!1)-M,x<M)},E=M=>{if(f===h)return 0;if(d){if(f===p&&M>=.5)return 1/0;let y=u.midTail(f,1,!0);return y===M?1:To(R=>u.midTail(f,R,!0)-M,y>M)}let x=u.tail(f,1,!0);return x===M?1:To(y=>u.tail(f,y,!0)-M,x>M)},b;if(r==="less")b=[0,v(1-s)];else if(r==="greater")b=[E(1-s),1/0];else{let M=(1-s)/2;b=[E(M),v(M)]}return{...N,confInt:b,confLevel:s}}var xs=[[0,0]];function Tn(e){for(let t=xs.length;t<=e;t++)xs.push(ue(xs[t-1],Ic([t,0])));return xs[e]}function Rs(e,t,n,r){let o=e+n,s=o-e,i=e-(o-s)+(n-s)+t+r,c=o+i;return[c,i-(c-o)]}var _a=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let a=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=a,this.lo*=a,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),s=this.hi+o,i=s-this.hi,c=this.hi-(s-i)+(o-i)+this.lo;this.hi=s+c,this.lo=c-(this.hi-s)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function ap(e){let t=e.filter(v=>v.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,E)=>t.some(b=>b[E]>0));if(t=t.map(v=>v.filter((E,b)=>n[b])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,E)=>t.map(b=>b[E])));let r=t.length,o=t.map(v=>v.reduce((E,b)=>E+b,0)),s=t[0].map((v,E)=>t.reduce((b,M)=>b+M[E],0)).sort((v,E)=>E-v),i=s.length,c=o.reduce((v,E)=>v+E,0),a=qc(Tn(c));for(let v of o)a=ue(a,Tn(v));for(let v of s)a=ue(a,Tn(v));let l=[0,0];for(let v of t.flat())l=Ce(l,Tn(v));let f=ue(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),d=(v,E)=>{let b=E.join(","),M=p[v].get(b);if(M!==void 0)return M;let x=ue(a,Tn(E.reduce((L,_)=>L+_,0)));for(let L of E)x=Ce(x,Tn(L));for(let L=v;L<i;L++)x=Ce(x,Tn(s[L]));let y={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(b,y),v===i-1){let L=[0,0];for(let _ of E)L=Ce(L,Tn(_));return y.max=y.min=L[0],y.edges.push({to:y,hi:L[0],lo:L[1],mult:1}),y}let R=new Map,T=new Array(r+1).fill(0);for(let L=r-1;L>=0;L--)T[L]=T[L+1]+E[L];let S=new Array(r).fill(0),k=(L,_,I)=>{if(L===r-1){S[L]=_;let D=Ce(I,Tn(_)),A=E.map((B,J)=>B-S[J]).sort((B,J)=>B-J),O=d(v+1,A),q=`${A.join(",")}|${D[0]}`,P=R.get(q);P===void 0?R.set(q,{to:O,hi:D[0],lo:D[1],mult:1}):P.mult++;return}let F=Math.max(0,_-T[L+1]),C=Math.min(E[L],_);for(let D=F;D<=C;D++)S[L]=D,k(L+1,_-D,Ce(I,Tn(D)))};if(k(0,s[v],[0,0]),y.edges=[...R.values()],y.max=At(y.edges.map(L=>L.hi+L.to.max)),y.min=sn(y.edges.map(L=>L.hi+L.to.min)),v===i-2){let L=y.edges.map(O=>{let q=O.to.edges[0];return{v:ue([O.hi,O.lo],[q.hi,q.lo]),mult:O.mult}});L.sort((O,q)=>O.v[0]-q.v[0]||O.v[1]-q.v[1]);let _=L.length,I=L[_-1].v[0],F=new Float64Array(_),C=new Float64Array(_),D=new Float64Array(_),A=[0,0];L.forEach(({v:O,mult:q},P)=>{F[P]=O[0],C[P]=O[1];let B=Ce(O,[I,0]);A=ue(A,[q*Math.exp(B[0])*(1+B[1]),0]),D[P]=A[0]}),y.dist={hi:F,lo:C,cum:D,vmax:I,kv:ue(a,[I,0])}}return y},g=d(0,[...o].sort((v,E)=>v-E)),w=new _a,N=(v,E,b,M)=>{if(E+v.max<=u-m){let[k,L]=Rs(v.KZ[0],v.KZ[1],E,b);return w.add(k,L,M),!0}if(E+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[y,R]=Rs(u,h,-E,-b),T=0,S=x.hi.length;for(;T<S;){let k=T+S>>>1,L=x.hi[k];L<y||L===y&&x.lo[k]<=R?T=k+1:S=k}if(T>0){let[k,L]=Rs(x.kv[0],x.kv[1],E,b);w.add(k,L,M*x.cum[T-1])}return!0};N(g,0,0,1)||(g.pastHi.push(0),g.pastLo.push(0),g.count.push(1));for(let v=0;v<i-2;v++)for(let E of p[v].values()){let{pastHi:b,pastLo:M,count:x}=E;if(x.length!==0){for(let{to:y,hi:R,lo:T,mult:S}of E.edges)for(let k=0;k<x.length;k++){let[L,_]=Rs(b[k],M[k],R,T),I=x[k]*S;if(N(y,L,_,I))continue;let F=y.index.get(L);F===void 0?(y.index.set(L,y.count.length),y.pastHi.push(L),y.pastLo.push(_),y.count.push(I)):y.count[F]+=I}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,w.value())}function Ts(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function Oa(e){return[...e].sort((t,n)=>t-n).join(",")}function Ca(e,t,n){let r=n.length,o=new Float64Array(r),s=new Float64Array(r),i=0;for(let c=0;c<r;c++)o[c]=i+=n[c];i=0;for(let c=r-1;c>=0;c--)s[c]=i+=n[c];return{f:e,offset:t,lower:o,upper:s}}function Fa(e){let[,t]=Ts(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function Da(e){let[t,n]=Ts(e),r=0;for(let i of n)r+=i;let o=new Float64Array(r+1);o[0]=1;let s=0;for(let i of n){s+=i;for(let c=s;c>=i;c--)o[c]=.5*(o[c]+o[c-i]);for(let c=i-1;c>=0;c--)o[c]=.5*o[c]}return Ca(t,0,o)}function Pa(e,t){let[,n]=Ts(e),r=n.length,o=Math.min(t,r-t),s=Ol(n),i=0;for(let c=1;c<=r;c++)for(let a=Math.max(1,o-(r-c));a<=Math.min(c,o);a++)i+=s[c]-s[c-a]-s[a]+1;return{steps:i,size:s[r]-s[r-o]-s[o]+1}}function Ol(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function Va(e,t){let[n,r]=Ts(e),o=r.length,s=Math.min(t,o-t),i=Ol(r),c=[];for(let h=0;h<=s;h++)c.push(new Float64Array(i[o]-i[o-h]-i[h]+1));c[0][0]=1;for(let h=1;h<=o;h++){let p=r[h-1];for(let d=Math.min(h,s);d>=Math.max(1,s-(o-h));d--){let g=c[d],w=c[d-1],N=d/h,v=(h-d)/h,E=i[d],b=i[h]-i[h-d],M=i[d-1]+p,x=Math.max(E,M);for(let y=b;y>=x;y--)g[y-E]=v*g[y-E]+N*w[y-M];for(let y=x-1;y>=E;y--)g[y-E]=v*g[y-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return Ca(n,i[s]/n-l,a);let f=a.length,m=new Float64Array(f);for(let h=0;h<f;h++)m[h]=a[f-1-h];let u=i[o]-i[o-s];return Ca(n,(i[o]-u)/n-l,m)}function nr(e,t){let n=Math.ceil(e.f*(t-e.offset)+e.f*1e-8)-1;return n<0?0:n>=e.lower.length?1:Math.min(1,e.lower[n])}function vr(e,t){let n=nr(e,t);if(Math.abs(n-1)>=1/64)return 1-n;let r=Math.ceil(e.f*(t-e.offset)+e.f*1e-8);return r>=e.upper.length?0:Math.min(1,e.upper[Math.max(0,r)])}var Fl=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Ao(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,Fl,"wilcoxTest"),V(n,Fl,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let d=ms(e,t,!0);return d.kind==="one"?Ao(d.x,n):Ao(d.x,d.y,d.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=cp(n.correct??!0),l=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(l&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(i&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[d,g]=Gn(Array.from(e),Array.from(r));u=Ct(d.map((w,N)=>w-g[N]))}else u=Ct(Array.from(e)),r!==void 0&&(h=Ct(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let p={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new mt};return H("wilcoxTest",h===void 0?hp(u,c,i,p):yp(u,h,c,p))}function cp(e){if(typeof e=="boolean")return e?0:-1;if(!(Number.isInteger(e)&&e>=0&&e<=3))throw new RangeError("'correct' must be an integer between 0 and 3");return e}function Ss(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var Vl=10*Number.EPSILON;function $l(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?ie(e):r(ie(e)-n*Xt(e)),s=()=>n===0?ie(e,{lowerTail:!1}):r(ie(e,{lowerTail:!1})+n*Xt(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let c=s();return c>=1/64?2*(1-i):2*c}function lp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function fp(e,t,n){let r=t*(t+1)*(2*t+1),o=-(3*t*t+3*t-1)/(10*r),s=4*(3*t**4+6*t**3-3*t+1)/(35*r*r);return zl(e,o,s,n)}function mp(e,t,n,r){let o=t+n,s=t*n,i=-(o*(o+1)-s)/(20*s*(o+1)),c=(2*(t**4+n**4)+4*s*(t*t+n*n)+6*s*s+4*(t**3+n**3)+(7*s+o-1)*o)/(210*s*s*(o+1)**2);return zl(e,i,c,r)}function zl(e,t,n,r){let[o,s,i]=lp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function ja(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:st([e[n-1],e[n]])}var Ka=(e,t)=>e-t,$a=5e9,up=40;function za(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function As(e,t){if(e.size>16777216||e.steps>$a)throw za(t);return e.steps}function jl(e){let t=e.length,n=bt(e.map(Math.abs)),r=0;for(let i=0;i<t;i++)e[i]>0&&(r+=n[i]);let o=0;for(let i of pn(n))o+=i*i*i-i;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,s]}function hp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(b=>b-s);i.some((b,M)=>!Number.isFinite(b)&&Number.isFinite(e[M]))&&(i=e.map(b=>b/2-s/2));let a=i.some(b=>b===0),l=t??i.length<50,f=(b,M,x)=>({method:b,alternative:o,statistic:M,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let b=bt(i.map(Math.abs)),M=pn(b).length>0,x=0;for(let L=0;L<i.length;L++)i[L]>0&&(x+=b[L]);let y="Wilcoxon signed rank exact test";if(!M&&!a){let L=i.length,_=L*(L+1)/2,I;if(o==="less")I=$r(x,L,!0,!1);else if(o==="greater")I=$r(x-1,L,!1,!1);else{let C=x>_/2?$r(x-1,L,!1,!1):$r(x,L,!0,!1);I=Math.min(1,2*C)}let F=f(y,x,I);return r.confInt?{...F,...pp(e,r)}:F}let R=b.filter((L,_)=>i[_]!==0);As(Fa(R),"distribution");let T=Da(R),S;if(o==="less")S=nr(T,x);else if(o==="greater")S=vr(T,x-.25);else{let L=0;for(let _ of R)L+=_/2;S=Math.min(1,2*(x>L?vr(T,x-.25):nr(T,x)))}let k=f(y,x,S);return r.confInt?{...k,...gp(e,r)}:k}let m=a?i.filter(b=>b!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,d]=jl(m),w=pn(bt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Ss(p,o,w>=0))/d,v=$l(N,o,w>=1?fp(N,u,w):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...E,...wp(e,r)}:E}function Wa(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Dl(e,t,n){let r={confInt:Wa(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Kl(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Wl(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=Hn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(Ka)}function Yl(e,t,n,r,o){let{alternative:s,confLevel:i}=r,c=e.length,a=s==="two.sided"?(1-i)/2:1-i,l=n(a);t(l)<=a+Vl&&l++;let f=ja(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:Wa(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function pp(e,t){let n=e.length;return Yl(Wl(e),r=>$r(r,n,!0,!1),r=>jc(r,n,!0,!1),t,"(pseudo)median")}function Bl(e){let t=e.length,n=Float64Array.from(e).sort(),r=new Array(t);for(let s=0;s<t;s++){let i=e[s],c=0,a=t;for(;c<a;){let f=c+a>>>1;n[f]<i?c=f+1:a=f}let l=c;for(a=t;l<a;){let f=l+a>>>1;n[f]<=i?l=f+1:a=f}r[s]=(c+1+l)/2}let o="";for(let s=0;s<t;){let i=s;for(;i+1<t&&n[i+1]===n[s];)i++;i>s&&(o+=`${s}:${i-s+1},`),s=i+1}return{ranks:r,key:o}}function Ul(e,t,n,r,o,s,i,c,a,l){let{alternative:f,confLevel:m}=a,u=e.length,p=(f==="two.sided"?(1-m)/2:1-m)+Vl,d=f!=="less",g=f!=="greater",w=I=>Hn(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+oe(Math.min(p,1))*i.sd+.1*i.sd+3))),v=8,E=I=>I===0?e[0]-1:w(I),b=I=>I===0?e[u-1]+1:w(u-I),M=[new Map,new Map];{let I=new Map,F=0,C=up*c;for(let[D,A]of[[0,E],[1,b]])if(!(D===0?!d:!g))for(let O=0;O<=N;O++){if(F+=C,F>$a)throw za("interval");let q;try{q=t(A(O))}catch{break}if(M[D].set(O,[q.stat,q.key]),I.has(q.key)){I.delete(q.key),I.set(q.key,!0);continue}if(F+=r(q.scores),F>$a)throw za("interval");I.set(q.key,!0),I.size>v&&I.delete(I.keys().next().value)}}let x=new Map,y=(I,F)=>{let C=I===0?E(F):b(F),D=M[I].get(F),A;if(D===void 0){let B=t(C);D=[B.stat,B.key],A=B.scores}let[O,q]=D,P=x.get(q);return P!==void 0?x.delete(q):(P=n(A??t(C).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(q,P),[O,P]},R=I=>{let[F,C]=y(0,I);return o(C,F)},T=I=>{let[F,C]=y(1,I);return s(C,F)},S=-1/0,k=0;if(d){let I=R(0);if(!(I>p)){let F=I,C=1;for(;C<=u-1;C++){let D=R(C);if(D>p)break;F=D}S=e[C-1],k=F}}let L=1/0,_=0;if(g){let I=T(0);if(!(I>p)){let F=I,C=u-1;for(;C>=1;C--){let D=T(u-C);if(D>p)break;F=D}L=e[C],_=F}}return{confInt:[S,L],confLevel:1-(k+_),estimate:ja(e),estimateNames:l}}function dp(e,t){let n=e.map(r=>r-t);if(n.some(r=>Number.isNaN(r)))throw new Error("cannot compute the exact confidence interval: the shifted data have undefined values (infinite observations)");return Number.isFinite(t)&&n.some((r,o)=>!Number.isFinite(r)&&Number.isFinite(e[o]))&&(n=e.map(r=>r/2-t/2)),n}function gp(e,t){let n=e.length,r=s=>{let i=dp(e,s),{ranks:c,key:a}=Bl(i.map(Math.abs)),l=0;for(let f=0;f<n;f++)i[f]>0&&(l+=c[f]);return{stat:l,scores:c,key:a}},o=n*(n+1)/2;return Ul(Wl(e),r,Da,s=>As(Fa(s),"interval"),(s,i)=>vr(s,i-.25),nr,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function wp(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return Dl(n,NaN);let c=sn(e),a=At(e);if(!Number.isFinite(c)||!Number.isFinite(a)||i===1||c===a)return Number.isFinite(c)&&c===a&&t.warnings.add("cannot compute confidence interval when all observations are zero or tied"),Dl(n,Hn(c,a),"midrange");let l=(v,E)=>{let[,b,M]=jl(e.map(x=>x-v).filter(x=>x!==0));return(b-Ss(b,n,E))/M},f=l(c,s),m=l(a,s),u=v=>-oe(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>oe(v/2)):!(f<u(v/2))&&!(m>oe(v/2)),p=1-r,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;d&&t.warnings.add("requested conf.level not achievable"),Kl(o);let g=v=>Kt(E=>l(E,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),w;if(p>=1){let v=ja([...e].sort(Ka));w=Wa(n,v,v)}else n==="greater"?w=[g(u(p)),1/0]:n==="less"?w=[-1/0,g(oe(p))]:w=[g(u(p/2)),g(oe(p/2))];let N=Kt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:w,confLevel:d?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Gl(e,t){let n=e.length,r=n-t,o=0;for(let a=0;a<t;a++)o+=e[a];let s=o-t*(t+1)/2,i=0;for(let a of pn(e))i+=a*a*a-a;let c=Math.sqrt(t*r/12*(n+1-i/(n*(n-1))));return[s,s-t*r/2,c,i>0]}function bp(e,t){let[n,r,o]=Gl(bt([...e,...t]),e.length);return[n,r,o]}function vp(e,t){let n=e.length,r=n+t.length,o=(l,f)=>{let m=l===0||!Number.isFinite(l)||!Number.isFinite(f)?f:f/2;return l<m?-1:l>m?1:0},s=(l,f)=>l<n&&f<n?e[l]<e[f]?-1:e[l]>e[f]?1:0:l>=n&&f>=n?t[l-n]<t[f-n]?-1:t[l-n]>t[f-n]?1:0:l<n?o(e[l],t[f-n]):-o(e[f],t[l-n]),i=Array.from({length:r},(l,f)=>f);i.sort((l,f)=>s(l,f)||l-f);let c=new Array(r),a=0;for(;a<r;){let l=a;for(;l+1<r&&s(i[l+1],i[a])===0;)l++;let f=(a+l+2)/2;for(let m=a;m<=l;m++)c[i[m]]=f;a=l+1}return c}function Np(e,t,n){return Hl(e,t,n).ranks}function Hl(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((a,l)=>[a,l]).filter(([a])=>!Number.isNaN(a)),s=bt([...o.map(([a])=>a),...t]),i=new Array(e.length+t.length);o.forEach(([,a],l)=>i[a]=s[l]),t.forEach((a,l)=>i[e.length+l]=s[o.length+l]);let c=o.length+t.length;return r.forEach((a,l)=>{Number.isNaN(a)&&(i[l]=++c)}),{ranks:i,key:Oa(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=vp(e.map(s=>s/2-n/2),t);return{ranks:o,key:Oa(o)}}return Bl([...r,...t])}function yp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=Np(e,t,s),[f,m,u,h]=Gl(l,i),p,d;if(a)if(d="Wilcoxon rank sum exact test",!h)p=Pl(()=>o==="less"?zr(f,i,c,!0,!1):o==="greater"?zr(f-1,i,c,!1,!1):Math.min(2*zr(f,i,c,!0,!1),2*zr(f-1,i,c,!1,!1),1));else{As(Pa(l,i),"distribution");let N=Va(l,i);o==="less"?p=nr(N,f):o==="greater"?p=vr(N,f-.25):p=Math.min(2*nr(N,f),2*vr(N,f-.25),1)}else{d=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(b=>b===l[0]),E=(m-Ss(m,o,N>=0))/u;p=v?1:$l(E,o,N>=1?mp(E,i,c,N):0)}let g={method:d,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return g;let w=a?h?Ep(e,t,r):Pl(()=>Mp(e,t,r)):xp(e,t,r);return{...g,...w}}function Pl(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Xl(e,t,n){let r=[];for(let o of e)for(let s of t){let i=o-s;n&&Number.isNaN(i)||r.push(i)}return r.sort(Ka)}function Mp(e,t,n){let r=e.length,o=t.length;return Yl(Xl(e,t,!1),s=>zr(s,r,o,!0,!1),s=>Wc(s,r,o,!0,!1),n,"difference in location")}function Ep(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=Hl(e,t,i),l=0;for(let f=0;f<r;f++)l+=c[f];return{stat:l-r*(r+1)/2,scores:c,key:a}};return Ul(Xl(e,t,!0),s,i=>Va(i,r),i=>As(Pa(i,r),"interval"),(i,c)=>vr(i,c-.25),nr,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function xp(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=sn(e)-At(t),a=At(e)-sn(t);if(!Number.isFinite(c)||!Number.isFinite(a))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(h,p)=>{let[,d,g]=bp(e.map(w=>w-h),t);return(d-Ss(d,r,p))/g},f=(h,p)=>{let d=l(c,p)-h,g=l(a,p)-h;if(Number.isNaN(d)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return d<=0?c:g>=0?a:(Kl(s),Kt(w=>l(w,p)-h,c,a,{tol:s,fLower:d,fUpper:g}))},m=1-o,u;return r==="greater"?u=[f(-oe(m),i),1/0]:r==="less"?u=[-1/0,f(oe(m),i)]:u=[f(-oe(m/2),i),f(oe(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Rp=["method","n"],Ql=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Tp(e){if(Ql.includes(e))return e;let t=Ql.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Yr(e,t={}){V(t,Rp,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Tp(n.method??"holm"),o=Array.from(e),s=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||s.push(m);let i=s.length,c=n.n??i;if(!(c>=i))throw new RangeError("n must be at least the number of non-NaN p-values");if(c===1/0)throw new RangeError("n must be finite");if(c<=1||i===0)return o;let a=s.map(m=>o[m]),l=Jl(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function ks(e,t){let n=e.map((r,o)=>o);return n.sort((r,o)=>(t?e[o]-e[r]:e[r]-e[o])||r-o),n}function Jl(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"holm":{let s=ks(e,!1),i=-1/0;return s.forEach((c,a)=>{i=Math.max(i,(n-a)*e[c]),o[c]=Math.min(1,i)}),o}case"hochberg":{let s=ks(e,!0),i=1/0;return s.forEach((c,a)=>{let l=r-a;i=Math.min(i,(n-l+1)*e[c]),o[c]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=Fn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)mn(m,1/h,u);s=Dn(m)}else{let h=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*h)+1/(120*h*h)}}let i=s*n,c=Number.isFinite(i)?1:1024,a=Number.isFinite(i)?i:s*(n/1024),l=ks(e,!0),f=1/0;return l.forEach((m,u)=>{let h=r-u;f=Math.min(f,a/h*e[m]*c),o[m]=Math.min(1,f)}),o}case"hommel":return n===2?Jl(e,"hochberg",n):Sp(e,n)}}var Zl=2**16;function Sp(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Zl)throw new RangeError(`n must be at most ${Zl} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=ks(r,!1),s=o.map(a=>r[a]),i=s.slice();for(let a=t;a>=2;a--){let l=1/0;for(let f=1;f<=a;f++)l=Math.min(l,a*s[t-a+f-1]/f);for(let f=t-a;f<t;f++)i[f]=Math.max(i[f],l);for(let f=0;f<t-a;f++)i[f]=Math.max(i[f],Math.min(a*s[f],l))}let c=new Array(n);return o.forEach((a,l)=>{a<n&&(c[a]=i[l])}),c}var ef=["p","alternative","confLevel"],Ap=1+1e-7;function Ya(e,t,n){let r=e-1;return e===0||e-r===1&&$t(t,-r)[1]===0?On(r,t,n,!1,!1):On(e,t,n,!1,!1)+fn(e,t,n,!1)}function kp(e,t,n){if(n===0)return e===0?1:0;if(n===1)return e===t?1:0;let r=t*n;if(e===r)return 1;let o=fn(e,t,n,!1)*Ap,s=a=>fn(a,t,n,!1)<=o;if(e<r){let a=Zo(s,Math.ceil(r),t),l=a>t?0:Ya(a,t,n);return Math.min(1,On(e,t,n,!0,!1)+l)}let i=Jo(s,0,Math.floor(r)),c=i<0?0:On(i,t,n,!0,!1);return Math.min(1,Ya(e,t,n)+c)}function qp(e,t,n={}){V(t,ef,"binomTest"),V(n,ef,"binomTest");let r,o;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(n=t),!wn(e[0])||!wn(e[1]))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1]),o===0||o===1/0)throw new RangeError("'n' must be a positive integer >= 'x'")}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!wn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(wn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return Ba(r,o,n)}function Ba(e,t,n={}){let r=n.p??.5;if(!(r>=0&&r<=1))throw new RangeError("'p' must be a single number between 0 and 1");let o=Ge(n.alternative),s=n.confLevel??.95;It(s);let i;o==="less"?i=On(e,t,r,!0,!1):o==="greater"?i=Ya(e,t,r):i=kp(e,t,r);let c=f=>e===0?0:Yn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Yn(f,e+1,t-e,!1,!1):Yn(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,a(1-s)];else if(o==="greater")l=[c(1-s),1];else{let f=(1-s)/2;l=[c(f),a(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:i,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var Ip=["T","r","alternative","confLevel"],_p=1+1e-7;function Lp(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=es(e,t,!1)*_p,r=i=>es(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=Zo(r,Math.max(e+1,o),1/0);return Math.min(1,Pr(e,t,!0,!1)+Ua(i,t))}let s=Jo(r,0,Math.min(e-1,o));return Math.min(1,Pr(s,t,!0,!1)+Ua(e,t))}var ko=2**-1022;function Ua(e,t){return e-(e-1)===1?Pr(e-1,t,!1,!1):Pr(e,t,!1,!1)+es(e,t,!1)}function Nr(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function Cp(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=ko&&(s!==0||e===0||t===0))return s;let i=e*t,c=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(i)&&Number.isFinite(c))return i/c;let[a,l]=Nr(e),[f,m]=Nr(t),[u,h]=Nr(n),[p,d]=Nr(r),g=f*p,w=h-m-d,N=w>=0?u/(u+$(g,-w)):u/($(u,w)+g);return $(a*f*N,l+m+Math.min(w,0))}function Op(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=ko&&(o===0||o>=ko)?i:e===0?0:e/t*(r/n)}function Fp(e,t={}){V(t,Ip,"poissonTest");let n=typeof e=="number"?[e]:Array.from(e);if(n.length<1||n.length>2)throw new RangeError("incorrect length of 'x'");if(!n.every(wn))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,i=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(w=>Number.isFinite(w)&&w>0))throw new RangeError("'T' must be positive");let c=t.r??1;if(!(Number.isFinite(c)&&c>=0))throw new RangeError("'r' must be a single positive number");let a=Ge(t.alternative),l=t.confLevel??.95;if(o===2?It(l):an(l),o===2){let[w,N]=r,[v,E]=i,b=w+N;if(b===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let M=c*v,x=M+E,y=Number.isFinite(x)&&x>=ko&&(M===0||M>=ko)?M/x:1/(1+E/v/c),R=Ba(Math.round(w),Math.round(w)+Math.round(N),{p:y,alternative:a,confLevel:l}),T=E/v,S=k=>{if(k===0)return 0;let L=k/(1-k);if(Number.isFinite(T)||L===1/0)return L*T;let[_,I]=Nr(L),[F,C]=Nr(v),[D,A]=Nr(E);return $(_*D/F,I+A-C)};return{method:"Comparison of Poisson rates",alternative:a,statistic:w,statisticName:"count1",parameter:Cp(b,c,v,E),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:Op(w,N,v,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Pr(f,u,!0,!1):a==="greater"?h=Ua(f,u):h=Lp(f,u);let p=w=>f===0?0:Qo(w,f,1,!0,!1),d=w=>w<1e-8?Qo(w,f+1,1,!1,!1):Qo(1-w,f+1,1,!0,!1),g;if(a==="less")g=[0,d(1-l)];else if(a==="greater")g=[p(1-l),1/0];else{let w=(1-l)/2;g=[p(w),d(w)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[g[0]/m,g[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var tf=["p","alternative","confLevel","correct"],Dp="Chi-squared approximation may be incorrect";function Ga(e,t,n={}){at(t)&&V(t,tf,"propTest"),V(n,tf,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(k=>k[0]),o=S.map(k=>k[0]+k[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let i=r.length,c=[];for(let S=0;S<i;S++)!Number.isNaN(r[S])&&!Number.isNaN(o[S])&&c.push(S);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(S=>r[S]),o=c.map(S=>o[S]),o.some(S=>!(S>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(S=>S===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(S=>!(S>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((S,k)=>S>o[k]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let S=typeof s.p=="number"?[s.p]:Array.from(s.p);if(S.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(k=>S[k]),l.some(k=>!(k>0&&k<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;a===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;It(m);let u=Ge(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,k)=>S/o[k]),p=a===2?h[0]-h[1]:0,d=a===2?1/o[0]+1/o[1]:0,g=(s.correct??!0)&&a<=2?.5:0,w=!1;if(g>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=g&&([g,w]=[S,!0])}else if(g>0&&a===2&&!f){let S=Math.abs(p)/d;S<=g&&([g,w]=[S,!0])}let N=Pp(r,o),v=o.some((S,k)=>{let L=l?.[k]??N;return S*L<5||S*(1-L)<5}),E=0;if(!w)for(let S=0;S<a;S++){let k=l?.[S]??N,L=o[S]*k,_=o[S]*(1-k);E+=Dt(Math.abs(r[S]-L)-g,L),E+=Dt(Math.abs(o[S]-r[S]-_)-g,_)}let b=l!==void 0?a:a-1,M;if(u==="two.sided")M=gt(E,b/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:p);M=ie(S*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,k=u==="two.sided"&&S<1e-8?oe(S,{lowerTail:!1}):oe(u==="two.sided"?(1+m)/2:m);if(a===1){let L=o[0],_=k*k,I=(O,q)=>(O+_/(2*L)+q*k*Math.sqrt(O*(1-O)/L+_/(4*L*L)))/(1+_/L),F=h[0]+g/L,C=h[0]-g/L,D=Math.min(1,Math.max(0,F>=1?1:I(F,1))),A=Math.min(1,Math.max(0,C<=0?0:I(C,-1)));x=u==="two.sided"?[A,D]:u==="greater"?[A,1]:[0,D]}else{let L=Math.sqrt(h.reduce((C,D,A)=>C+D*(1-D)/o[A],0)),_=k*L+g*d,I=Math.max(-1,p-_),F=Math.min(1,p+_);x=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[-1,F]}}let y=a===1?"p":c.map(S=>`prop ${S+1}`),T={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${g!==0?"with":"without"} continuity correction`,alternative:u,statistic:E,statisticName:"X-squared",parameter:b,parameterName:"df",pValue:M,estimate:a===1?h[0]:h,estimateNames:y,warnings:v?[Dp]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=y),x!==void 0&&(T.confInt=x,T.confLevel=m),H("propTest",T)}function Pp(e,t){let n=t.reduce((o,s)=>o+s,0);if(n<1/0)return e.reduce((o,s)=>o+s,0)/n;let r=Math.ceil(Math.log2(t.length));return e.reduce((o,s)=>o+$(s,-r),0)/t.reduce((o,s)=>o+$(s,-r),0)}var Vp=["method","confLevel","sides"],$p=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],zp=["two.sided","left","right"];function qo(e,t){let n=xe(t,zp),r=e??.95;if(!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let o=n==="two.sided"?r:1-2*(1-r);if(!(o>=0))throw new RangeError("a one-sided 'confLevel' must be at least 0.5");let s=n!=="two.sided"&&1-r<1e-8?2*(1-r):1-o;return{...Ha(s,o),sides:n}}function Ha(e,t=1-e){let n=e/2<1e-8,r=n?oe(e/2,{lowerTail:!1}):oe(1-e/2),o=n?Cn(e,1,{lowerTail:!1}):Cn(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Br(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!wn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function $n(e,t,n,r){let o=Br(e,n),s=Br(t,r);if(s===0)throw new RangeError(`'${r}' must be positive`);if(s>2**53)throw new RangeError(`'${r}' must be at most 2^53`);if(o>s)throw new RangeError(`'${n}' must not be greater than '${r}'`);return[o,s]}function Is(e,t,n){let r=e/t,o=1-r,s=n*n,i=(e+s/2)/(t+s),c=n*Math.sqrt(t)/(t+s)*Math.sqrt(r*o+s/(4*t)),a=i+c,l=i-c;return e===0?l=0:l<i/2&&(l=r*(e/(t+s))/a),[l,e===t?1:a]}function qs(e,t,n,r){return r?e/2<1e-8?Yn(e/2,t,n,!1,!1):Yn(1-e/2,t,n,!0,!1):Yn(e/2,t,n,!0,!1)}function nf(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*fn(e,t,m,!1)+On(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*fn(e,t,m,!1)+On(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:Kt(d=>i*s(d),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=i*s(0);return l<=0?1:Math.exp(Kt(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function Xa(e,t,n={}){V(n,Vp,"binomCI");let r=xe(n.method,$p),o=qo(n.confLevel,n.sides),[s,i]=$n(e,t,"x","n"),{alpha:c,kappa:a}=o,l=s/i,f=1-l,m,u;switch(r){case"wald":{let h=a*Math.sqrt(l*f/i);[m,u]=[l-h,l+h];break}case"wilson":[m,u]=Is(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),d=2*s+h-1,g=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:d-g<d/2?(2*s-1)/(2*i)*(2*s-1)/(d+g):(d-g)/p,u=l===1?1:(2*s+h+1+a*Math.sqrt(h+2-1/i+4*l*(i*f-1)))/p;break}case"agresti-coull":{let h=a*a,p=i+h,d=(s+h/2)/p,g=a*Math.sqrt(d*(1-d)/p);[m,u]=[d-g,d+g];break}case"jeffreys":m=s===0?0:qs(c,s+.5,i-s+.5,!1),u=s===i?1:qs(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:qs(c,s,i-s+1,!1),u=s===i?1:qs(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):nf(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):nf(s,i,c,!0);break}return m=Math.max(0,m),u=Math.min(1,u),o.sides==="left"?u=1:o.sides==="right"&&(m=0),H("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Io(e){return e===1/0?1:e/(1+e)}function _o(e,t){return e===0?0:t>0?e*e/t:1/0}function jp(e,t,n,r){return e/t+n/r>1}function Kp(e,t,n,r){return Ls(e,t,n,r,0)}function Ja(e,t,n,r,o){if(jp(e,t,n,r)){let[p,d,g,w]=Ja(t-e,t,r-n,r,-o);return[d,p,w,g]}let s=o<=0,[i,c,a,l]=s?[e,t,n,r]:[n,r,e,t],f=Math.abs(o),m=1-f,u=0;if(m>0){let p=w=>{let N=0;return i>0&&(N+=i/w),i<c&&(N-=(c-i)/(1-w)),a>0&&(N+=a/(w+f)),a<l&&(N-=(l-a)/(m-w)),N},d=w=>{let N=0;return i>0&&(N-=i/(w*w)),i<c&&(N-=(c-i)/((1-w)*(1-w))),a>0&&(N-=a/((w+f)*(w+f))),a<l&&(N-=(l-a)/((m-w)*(m-w))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let w=0,N=m,v=Wp(e,t,n,r,o);u=s?v:v-o,u>w&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let b=p(u);if(b===0)break;b>0?w=u:N=u;let M=u-b/d(u);if(M>w&&M<N||(M=w===0?N/2:N>2*w?Math.sqrt(w*N):w+(N-w)/2),!(M>w&&M<N))break;let x=Math.abs(M-u);if(u=M,x<=2**-53*u)break}}}let h=[u,1-u,Math.min(1,u+f),Math.max(0,m-u)];return s?h:[h[2],h[3],h[0],h[1]]}function Wp(e,t,n,r,o){let s=e/t,i=n/r,c=r/t,a=1+c,l=-(1+c+s+c*i+o*(c+2)),f=o*o+o*(2*s+c+1)+s+c*i,m=-s*o*(1+o),u=l/(3*a),h=u*u*u-l*f/(6*a*a)+m/(2*a),p=Math.sign(h)*Math.sqrt(Math.max(0,u*u-f/(3*a)));if(p===0)return-u;let d=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(d)-u}function ei(e,t,n,r,o,s){if(s==="wald")return e/t*((t-e)/t)/t+n/r*((r-n)/r)/r;let[i,c,a,l]=Ja(e,t,n,r,o),f=i*c/t+a*l/r;if(s==="mee")return f;let m=t+r;return f*m/(m-1)}function Yp(e,t,n,r,o,s,i){let c=Ce(Ce(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=Ja(e,t,n,r,o),u=ue(ft(Ht(a,l),t),ft(Ht(f,m),r));return s==="mn"&&(u=ft(zt(u,t+r),t+r-1)),Ce(_e(c,c),zt(u,i))[0]}function Ls(e,t,n,r,o){return Ce(Ce(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function Bp(e,t,n,r,o,s){return _o(Ls(e,t,n,r,o),ei(e,t,n,r,o,s))}var _s=new Float64Array(1),Qa=new BigInt64Array(_s.buffer);function rf(e){return _s[0]=Math.abs(e),e<0?-Qa[0]:Qa[0]}function rr(e){return Qa[0]=e<0n?-e:e,e<0n?-_s[0]:_s[0]}function Za(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=rf(i?r:n),a=rf(t),l=s,f=a,m=s,u=1n;for(;;){if(f=i?a+u:a-u,(i?f>=c:f<=c)&&(f=c),m=e(rr(f)),m===0)return rr(f);if(m<0!=l<0||f===c)break;if(a=f,l=m,u*=2n,u>1n<<40n)return rr(a)}if(m<0==l<0||Number.isNaN(m))return rr(a);for(;f-a>1n||a-f>1n;){let h=(a+f)/2n,p=e(rr(h));if(p===0)return rr(h);p<0==l<0?(a=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?rr(f):rr(a)}function sf(e,t,n,r,o,s){let i=Kp(e,t,n,r);if(o===0)return[i,i];let c=Io(o),a=p=>(p===i?0:Io(Bp(e,t,n,r,p,s)))-c,l=(p,d,g,w)=>{if(g===0)return p;if(w===0)return d;let N=Kt(a,p,d,{tol:1e-300,fLower:g,fUpper:w});return s==="wald"?N:Za(v=>Yp(e,t,n,r,v,s,o),N,p,d,g<0)},f=a(-1),m=a(1),u=i<=-1||f<=0?-1:l(-1,i,f,-c),h=i>=1||m<=0?1:l(i,1,-c,m);return[u,h]}function of(e,t,n){let r=t,o=e(t);if(o>=0)return Math.exp(t);let s=1;for(;;){let i=t+n*s;if(Math.abs(i)>745)return n<0?0:1/0;let c=e(i);if(c>=0){if(c===0)return Math.exp(i);let a=n>0?Kt(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Kt(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Up(e,t,n,r,o){let s=t+r,i=o*(t+n)+e+r,c=o*(e+n),a=Math.sqrt(Math.max(0,i*i-4*s*c)),l=Math.min(1,o,c===0?0:2*c/(i+a));return[l,Math.min(1,l/o)]}function Gp(e,t,n,r,o){let[s,i]=Up(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,i,1-i];let c=s<=.5&&i<=.5?0:o*(1-i)<=1-s?2:3,a=c===0?i:c===2?1-i:1-s,l=M=>c===0?[M,0]:c===2?$t(1,-M):ft($t(1,-M),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?ft($t(o,-1),o)[0]:0:c===3&&o<1?$t(1,-o)[0]:0,h=M=>{let x=l(M),y=zt(x,o);return[y[0],Ce([1,0],y)[0],x[0],Ce([1,0],x)[0]]},p=e+n,d=c<2?1:-1,g=M=>{let[,x,y,R]=h(M),T=0,S=0;return p>0&&(T+=y>0?p/y:1/0,S-=p/(y*y)),e<t&&(T-=x>0?o*(t-e)/x:1/0,S-=o*(t-e)/x*(o/x)),n<r&&(T-=R>0?(r-n)/R:1/0,S-=(r-n)/(R*R)),[d*T,S*f]},w;if(!(m>u))w=u;else if(!(g(u)[0]>0))w=u;else if(!(g(m)[0]<0))w=m;else{let M=u,x=m;w=a,w>M&&w<x||(w=M+(x-M)/2);for(let y=0;y<2e3;y++){let[R,T]=g(w);if(R===0)break;R>0?M=w:x=w;let S=w-R/T;if(S>M&&S<x||(S=M<=0?x/2:x>2*M?Math.sqrt(M*x):M+(x-M)/2),!(S>M&&S<x))break;let k=Math.abs(S-w);if(w=S,k<=2**-53*w)break}}let[N,v,E,b]=h(w);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,b))]}function Hp(e,t,n,r,o){let[s,i,c,a]=Gp(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return _o(l(e,t,s,i),t*s*i)+_o(l(n,r,c,a),r*c*a)}function af(e,t,n,r,o){let s=e/t/(n/r);return lf(s,o,i=>Hp(e,t,n,r,i))}function Xp(e,t,n,r,o){let s=e+n,i=r*(o-1),c=t*o+r-s*(o-1),a=s===0?0:Math.min(1,2*s/(c+Math.sqrt(Math.max(0,c*c+4*i*s))));return[o===1/0?a>0?1:0:Math.min(1,a*o/(1+a*(o-1))),a]}function Qp(e,t,n,r,o){let[s,i]=Xp(e,t,n,r,o),c=e-t*s;return _o(c,t*s*(1-s))+_o(c,r*i*(1-i))}function cf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return lf(s,o,i=>Qp(e,t,n,r,i))}function lf(e,t,n){if(t===0)return[e,e];let r=Io(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Za(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?Za(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Io(n(u))-r,f=l(1),m=f===0?1:tr(l,e===0?f>0:f<0);return e===0?[0,i(m)]:[s(m),1/0]}let c=Math.log(e),a=l=>(l===c?0:Io(n(Math.exp(l))))-r;return[s(of(a,c,-1)),i(of(a,c,1))]}var Zp=["method","confLevel","sides"],Jp=["method","confLevel","sides"],ed=["method","confLevel"],td=["ac","wald","score","mn","mee"];function ti(e,t,n,r,o,s){let{kappa:i}=s,c=e/t,a=n/r,l=c-a,f,m;switch(o){case"wald":{let u=i*Math.sqrt(c*(1-c)/t+a*(1-a)/r);[f,m]=[Math.max(-1,l-u),Math.min(1,l+u)];break}case"ac":{let u=(e+1)/(t+2),h=(n+1)/(r+2),p=u-h,d=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-d),Math.min(1,p+d)];break}case"score":{let[u,h]=Is(e,t,i),[p,d]=Is(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+d*(1-d)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=sf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function nd(e,t,n,r,o={}){V(o,Zp,"binomDiffCI");let s=xe(o.method,td),i=qo(o.confLevel,o.sides),[c,a]=$n(e,t,"x1","n1"),[l,f]=$n(n,r,"x2","n2"),m=c/a-l/f,[u,h]=ti(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),H("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var rd=["katz.log","koopman"];function Cs(e,t,n,r,o){let s=e/t/(n/r),i=o*Math.sqrt(1/e-1/t+1/n-1/r);return[s*Math.exp(-i),s*Math.exp(i)]}function od(e,t,n,r,o={}){V(o,Jp,"binomRatioCI");let s=xe(o.method,rd),i=qo(o.confLevel,o.sides),[c,a]=$n(e,t,"x1","n1"),[l,f]=$n(n,r,"x2","n2"),m=i.kappa,u=c===0?0:c/a/(l/f),h,p;return c===0&&l===0?[h,p]=[0,1/0]:s==="katz.log"?c===0?[h,p]=[0,Cs(.5,a,l,f,m)[1]]:l===0?[h,p]=[Cs(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=Cs(a-.5,a,f-.5,f,m):[h,p]=Cs(c,a,l,f,m):[h,p]=af(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),H("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var sd=["midp","fisher","wald","score"],ad={midp:"median-unbiased estimate & mid-p exact CI",fisher:"Conditional MLE & exact CI from 'fisher.test'",wald:"Unconditional MLE & normal approximation (Wald) CI",score:"Unconditional MLE & score CI"},id=2**23,cd=2**20;function ff(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=ue(r,[e[o],0]));return r[0]}function ld(e,t={}){V(t,ed,"oddsRatio");let n=xe(t.method,sd);if(e.length!==2||e.some(S=>S.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=qo(r,void 0),c=e.map((S,k)=>Array.from(S,(L,_)=>Br(L,`table[${k}][${_}]`))),[[a,l],[f,m]]=c,u=a+l+f+m;if(u===0)throw new RangeError("all entries of the table are zero");if(!(u<=2**53))throw new RangeError("the total of the table must be at most 2^53");let h=Math.max(0,a-m),p=a+Math.min(l,f),d=p-h+1;if(n==="midp"||n==="fisher"){if(d>id)throw new RangeError(`the exact distribution of the 2 x 2 table has ${d} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(a,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let g,w,N;try{g=on(a-1,a+f,l+m,a+l,!0,!1)+.5*gn(a,a+f,l+m,a+l,!1),w=on(a,a+f,l+m,a+l,!1,!1)+.5*gn(a,a+f,l+m,a+l,!1),N=d<=cd?La(new Wr(a+f,l+m,a+l),a,1):Cl(a,a+f,l+m,a+l)}catch(S){throw S instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${S.message}`):S}let v=2*Math.min(g,w),E=[a+l,f+m],b=[a+f,l+m],M=0,x=!1;for(let S=0;S<2;S++)for(let k=0;k<2;k++){let L=E[k]*b[S]/u;L<5&&(x=!0);let _=Math.abs(c[k][S]-L);M+=_*_/L}let y=gt(M,.5,2,!1,!1),R,T;switch(n){case"wald":{R=a*m/(l*f);let S=s*Math.sqrt(1/a+1/l+1/f+1/m);T=[Math.exp(Math.log(R)-S),Math.exp(Math.log(R)+S)];break}case"fisher":{let S=je(()=>So(c,{confLevel:r}));R=S.estimate,T=S.confInt;break}case"score":{R=a*m/(l*f),T=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:cf(a,a+l,f,f+m,i);break}case"midp":{let S=new Wr(a+f,l+m,a+l),k=F=>{if(F===1)return[g,w];let C=S.density(F),D=a-h,A=.5*C[D];return[ff(C,0,D-1)+A,ff(C,D+1,C.length-1)+A]},L=(F,C)=>{let D=F(1);return D===0?1:tr(F,C?D>0:D<0)};R=a===h?0:a===p?1/0:L(F=>k(F)[0]-.5,!1);let _=a===h?0:L(F=>k(F)[1]-o/2,!0),I=a===p?1/0:L(F=>k(F)[0]-o/2,!1);T=[_,I];break}}return H("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:ad[n],pValue:{midp:v,fisher:N,chiSquare:y},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var fd=["epsilon","paired","varEqual","confLevel","alpha"],md=["margin","method","alpha"];function mf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function ud(e,t,n={}){V(n,fd,"tost");let r=n.epsilon??1;if(!(typeof r=="number"&&r>0&&r<1/0))throw new RangeError("'epsilon' must be a positive number");let o,s;if(n.alpha!==void 0)mf(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,It(o),!(o>.5))throw new RangeError("'confLevel' must be greater than 0.5");s=1-o}if(t===void 0)throw new TypeError("'y' is required");let i=n.paired??!1,c=n.varEqual??!1,a=je(()=>vn(e,t,{paired:i,varEqual:c})),l=a.stdErr,f=a.parameter,m=typeof a.estimate=="number"?a.estimate:a.estimate[0]-a.estimate[1],u=vt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?xt(s,f,!1,!1):xt(o,f,!0,!1),p=[m-h*l,m+h*l];return H("tost",{method:a.method.replace("t-test","TOST"),parameter:f,parameterName:"df",pValue:u,result:p[0]>-r&&p[1]<r?"rejected":"not rejected",confInt:p,confLevel:o,estimate:a.estimate,estimateNames:i?"mean of the differences":a.estimateNames,epsilon:r,seDiff:l,warnings:[]})}var hd=["mn","mee","wald"];function pd(e,t,n,r,o={}){V(o,md,"binomDiffEquivalenceTest");let s=xe(o.method,hd),i=o.margin;if(i===void 0)throw new TypeError("'margin' is required");if(!(typeof i=="number"&&i>0&&i<1))throw new RangeError("'margin' must be a single number in (0, 1)");let c=o.alpha??.05;mf(c,"alpha");let[a,l]=$n(e,t,"x1","n1"),[f,m]=$n(n,r,"x2","n2"),u=a/l-f/m,h=E=>Ls(a,l,f,m,E)/Math.sqrt(ei(a,l,f,m,E,s)),p=h(-i),d=h(i),g=[ie(p,{lowerTail:!1}),ie(d)],w=Math.max(g[0],g[1]),N=1-2*c,v=ti(a,l,f,m,s,Ha(2*c,N));return H("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,d],statisticName:"z",pValues:g,pValue:w,result:g[0]<c&&g[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var Wt=new WeakMap;function Fs(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Re(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Ds(e,t){return e.map(n=>{if(n.rss>=dd&&n.rss<1/0)return $(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let i=n.weights?.[s]??1;if(i===0)return;let c=$(o,-t);r+=i*c*c}),r})}var dd=2**-1022;function ri(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,$(n,-t)]])}function Ps(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function yr(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+$(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=Ps(e),s=r(o)),!($(t,-o)>=1e-24*s)}function uf(e){let t=Fs([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let i=e.assign[n[s]],c=o.get(i);c===void 0&&(c={term:i,df:0,ss:0},o.set(i,c),r.push(c)),c.df++,c.ss+=$(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Ds([e],t)[0],k:t}}var hf=(e,t,n)=>wt(e,t,n,!1,!1),oi="essentially perfect fit for \u20181\u2019: summary may be unreliable";function si(e,t,n){if(!(n>0))return!1;let r=e.length,o=0;for(let a of e)o+=a;o/=r;let s=0;for(let a of e)s+=(a-o)**2;let i=r>1?s/(r-1):0,c=t/n;return Number.isFinite(c)&&c<(o*o+i)*1e-30}var ai=new WeakMap;function gd(e){let t=uf(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let i=e.weights?.[s]??1;i!==0&&(r+=i*$(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function ni(e){let t=gd(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let d=ri(e,o);yr(d,c)&&(c=0,i=i.map(g=>yr(d,g.ss)?{...g,ss:0}:g))}let a=c/r,l=d=>$(d,2*o),f=i.map(({df:d,ss:g})=>{let w=g/d/a;return[d,l(g),l(g/d),w,hf(w,d,r)]});f.push([r,l(c),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],h={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},p=i.map(({df:d,ss:g})=>[d,g]);return p.push([r,c]),Wt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function wd(e){let t=e.map(f=>f.dfResidual),n=Fs(e),r=Ds(e,n),o=f=>$(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let i=r[s]/t[s],c=0;e[s].fittedValues.forEach((f,m)=>{let u=e[s].weights?.[m]??1;u!==0&&(c+=u*$(f,-n)**2)});let a=r[s]<1e-10*c,l=e.map((f,m)=>{if(m===0)return[t[0],o(r[0]),NaN,NaN,NaN,NaN];let u=t[m-1]-t[m],h=r[m-1]-r[m],[p,d]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],g=N=>N.filter((v,E)=>(p.weights?.[E]??1)!==0),w=u===0||!gs(g(p.x),g(d.x))?NaN:h/u/i;return w<0&&(w=NaN),[t[m],o(r[m]),u,o(h),w,Number.isNaN(w)?NaN:hf(w,Math.abs(u),t[s])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((f,m)=>String(m+1)),table:l,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function Os(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function ii(e,...t){let n=[e,...t];for(let c of n)if(typeof c!="object"||c===null||!Array.isArray(c.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1){let c=ni(e),a={...c,warnings:Os(e.warnings,c.warnings)},l=Wt.get(c);return l!==void 0&&Wt.set(a,l),H("anova",a,e)}if(n.some(c=>c.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=(c,a)=>c===a||c!==void 0&&a!==void 0&&c.length===a.length&&c.every((l,f)=>Object.is(l,a[f]));if(n.some(c=>!r(c.rows,e.rows)))throw new Error("models were not all fitted to the same rows of the data");let o=n.filter(c=>c.response===e.response);if(o.some(c=>!r(c.y,e.y)))throw new Error("models were not all fitted to the same response values");if(o.some(c=>!r(c.weights,e.weights)))throw new Error("models were not all fitted with the same weights");let s=[];if(o.length<n.length){let c=[...new Set(n.filter(a=>a.response!==e.response).map(a=>a.response))];s.push(`models with response ${c.map(a=>`\u2018${a}\u2019`).join(", ")} removed because response differs from model 1`)}let i=o.length===1?ni(e):wd(o);return i.warnings=Os(...n.map(c=>c.warnings),s,i.warnings),H("anova",i,...n)}function ci(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Os(i.warnings,ai.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Wt.get(i);return Array.isArray(l)&&Wt.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),H("aovSummary",a,e)}if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=e,n=ni(t),r=uf(t),o=si(t.fittedValues.map(i=>$(i,-r.k)),r.rss,r.rdf),s=Os(t.warnings,n.warnings,o?[oi]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Wt.get(n);return c!==void 0&&Wt.set(i,c),H("aovSummary",i,e)}return H("aovSummary",{colNames:n.colNames.slice(0,3),rowNames:n.rowNames.slice(0,-1),table:n.table.slice(0,-1).map(i=>i.slice(0,3)),warnings:s},e)}var bd=["pooledSd","mu","paired","ci","alternative","adjust"],vd=["pooledSd","mu","paired","ci","alternative"],Nd=["generalized","partial","ci","alternative"],yd=["partial","ci","alternative"],Md=["partial","ci","alternative"];function mi(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var Ed="Missing values detected. NAs dropped.",xd=`'y' is numeric but has only 2 unique values.
|
|
3
|
-
If this is a grouping variable, convert it to a factor.`,wf={tol:0,maxiter:5e3};function Mr(e){return e===1/0?1:Math.exp(Dc(.5,(e-1)/2)-.5*Math.log(e/2))}function pf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return pr(o=>dn(e,t,{ncp:o})-n,e-r,e+r,{...wf,extendInt:"downX"}).root}function bf(e,t,n,r){let o=n.pooledSd??!0,s=n.mu??0,i=n.paired??!1,c=n.ci===void 0?.95:n.ci;mi(c);let a=xe(n.alternative,["two.sided","less","greater"]),l=Array.from(e),f=t===void 0?void 0:Array.from(t),m=[];f!==void 0&&new Set(Ct(f)).size===2&&m.push(xd),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(Ed);let u=S=>S===1/0||S===-1/0;if(l.some(u)||(f?.some(u)??!1))throw new RangeError("the observations must be finite (NaN is dropped as missing)");let h,p,d,g,w,N;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:N}=Re([Ct(l)]));else{let[L,_]=Gn(l,f);({d:S,k:N}=ns(L,_))}let k=S.length;if(k<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");h=st(S),p=Math.sqrt(ot(S)),d=p/Math.sqrt(k),g=1/k,w=k-1}else{let S=Et([Ct(l),Ct(f)]);N=S.k;let[k,L]=S.n;if(k<2||L<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=S.mean[0]-S.mean[1];let _=Math.sqrt(S.variance[0]),I=Math.sqrt(S.variance[1]);o?(p=Math.sqrt(((k-1)*_**2+(L-1)*I**2)/(k+L-2)),d=p*Math.sqrt(1/k+1/L),g=1/k+1/L,w=k+L-2):(p=Math.sqrt((_**2+I**2)/2),g=2*(L*_**2+k*I**2)/(k*L*(_**2+I**2)),d=Math.sqrt(Math.sqrt(_**2/k)**2+Math.sqrt(I**2/L)**2),w=rs([_**2/k,I**2/L],[k-1,L-1]))}let v=r?Mr(w):1,E=$(s,-N),b=(h-E)/p*v,M=r?"Hedges_g":"Cohens_d";if(c===null)return{name:M,value:b,warnings:m};let x=a==="two.sided"?c:2*c-1,y=(h-E)/d,R=pf(y,w,(1+x)/2)*Math.sqrt(g)*v,T=pf(y,w,(1-x)/2)*Math.sqrt(g)*v;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:M,value:b,ci:c,ciLow:R,ciHigh:T,warnings:m}}function Rd(e,t,n={}){return V(n,bd,"cohensD"),H("cohensD",bf(e,t,n,n.adjust??!1))}function Td(e,t,n={}){return V(n,vd,"hedgesG"),H("hedgesG",bf(e,t,n,!0))}var Sd=e=>Array.isArray(e.effects),li=e=>e.multistratum===!0,vf=e=>Array.isArray(e.strata),Nf=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},fi=e=>({test:t=>t.split(":").includes(e)});function Ad(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let s=o.stratum.replace(/^Error: /,""),i=o.colNames.indexOf("Df"),c=o.colNames.indexOf("Sum Sq"),a=o.rowNames.map((f,m)=>({name:f,df:o.table[m][i],ss:o.table[m][c]})),l=a.find(f=>f.name==="Residuals");if(l===void 0)throw new Error(`the error stratum '${s}' has no residual degrees of freedom`);return{name:s,terms:a.filter(f=>f!==l),sse:l.ss,dfe:l.df}});if(t.length===0)throw new TypeError("the fit has no error strata");let n=t[0].name.split(":")[0],r=t.find(o=>o.name===n);if(r===void 0)throw new Error(`there is no error stratum '${n}'`);return{strata:t,subject:r}}function kd(e){let t=e.univariate,n=t.colNames.indexOf("Sum Sq"),r=[],o=(a,l,f)=>{let m=r.find(u=>u.name===a);return m===void 0&&r.push(m={name:a,terms:[],sse:l,dfe:f}),m},s=new Map;if(t.colNames.includes("Error SS")){let a=t.colNames.indexOf("num Df"),l=t.colNames.indexOf("Error SS"),f=t.colNames.indexOf("den Df");t.rowNames.forEach((m,u)=>{let h=t.table[u],p=e.within.filter(w=>fi(w).test(m)),d=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[a],ss:h[n]};d.terms.push(g),s.set(m,g)})}else{let a=t.colNames.indexOf("Df"),l=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[l][n],t.table[l][a]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][a],ss:t.table[u][n]};f.terms.push(h),s.set(m,h)})}let i=r.find(a=>a.name==="Subject");if(i===void 0)throw new Error("the result has no subject stratum");let c=e.anova.rowNames.map(a=>{let l=s.get(a);if(l===void 0)throw new Error(`there is no univariate test of '${a}'`);return l});return{strata:r,subject:i,order:c}}function yf(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>fi(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>fi(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function df(e,t,n,r){let o=i=>xn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:pr(o,0,Math.max(10,e*t*4),{...wf,extendInt:"downX"}).root:0}function Mf(e,t,n,r,o){let s=o==="two.sided"?r:2*r-1,i=Math.max(0,e),c=i/t/((1-i)/n),a=df(c,t,n,(1+s)/2),l=df(c,t,n,(1-s)/2);c<=Vr((1-s)/2,t,n)&&(l=0),c<=Vr((1+s)/2,t,n)&&(a=0);let f=a===1/0?1:a/(a+n),m=l===1/0?1:l/(l+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}function qd(e){let t=Re([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=$(n.ss,-t)}var gf="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function ui(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(gf);if(li(e)||vf(e)||Nf(e))return Id(e,t,n);let r=Sd(e)?je(()=>ii(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(gf);let o=Wt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;mi(i);let c=xe(n.alternative??"greater",["two.sided","less","greater"]),a=s.colNames.indexOf("Df"),l=s.colNames.indexOf("Sum Sq");if(a<0||l<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let f=s.rowNames.map((R,T)=>({name:R,df:s.table[T][a],ss:s.table[T][l]})).filter(R=>R.name!=="(Intercept)");qd(f);let m=f.find(R=>R.name==="Residuals");if(m===void 0)throw new TypeError("the table has no row 'Residuals'");if(!(m.df>0))throw new Error("the model has no residual degrees of freedom: the effect sizes are not defined");let u=f.filter(R=>R!==m),h=m.df,p=m.ss,d=p/h,g=f.reduce((R,T)=>R+T.ss,0),w=f.reduce((R,T)=>R+T.df,0)+1,N=t==="Eta2"?n.generalized??!1:!1,E=u.length===1?"plain":N!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:b,ssObserved:M}=yf(N,u),x=R=>{let{ss:T,df:S}=R;if(t==="Eta2")return E==="partial"?T/(T+p):E==="plain"?T/g:T/(T+p+M-(b(R.name)?T:0));if(t==="Omega2"){let L=E==="partial"?(T-S*d)/(T+(w-S)*d):(T-S*d)/(g+d);return Math.max(0,L)}let k=E==="partial"?(T-S*d)/(T+p):(T-S*d)/g;return Math.max(0,k)},y=`${t}${E==="plain"?"":`_${E}`}`;return{colNames:i===null?[y]:[y,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>i===null?[x(R)]:Mf(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function Id(e,t,n){let r=n.ci===void 0?.95:n.ci;mi(r);let o=xe(n.alternative??"greater",["two.sided","less","greater"]),s=Nf(e),i,c,a;if(s){let b=Wt.get(e),M=kd(b!==void 0&&!Array.isArray(b)?{...e,univariate:b}:e);({strata:i,subject:c}=M),a=M.order.map(x=>({term:x,stratum:M.strata.find(y=>y.terms.includes(x))}))}else{let b=li(e)?je(()=>ci(e)):e,M=Wt.get(b);({strata:i,subject:c}=Ad(Array.isArray(M)?{...b,strata:M.map((x,y)=>({...b.strata[y],...x}))}:b)),a=i.flatMap(x=>x.terms.map(y=>({term:y,stratum:x})))}let l=Re([[...i.flatMap(b=>b.terms.map(M=>M.ss)),...i.map(b=>b.sse)]]).k;for(let b of i){b.sse=$(b.sse,-l);for(let M of b.terms)M.ss=$(M.ss,-l)}let f=i.flatMap(b=>b.terms),m=i.reduce((b,M)=>b+M.sse,0),u=f.reduce((b,M)=>b+M.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,d=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:w}=yf(p,f),N=(b,M)=>{let{ss:x,df:y}=b,R=M.sse/M.dfe;if(t==="Eta2")return d==="partial"?x/(x+M.sse):d==="plain"?x/u:x/(x+m+w-(g(b.name)?x:0));if(t==="Omega2"){let S=M===c?0:1,k=d==="partial"?(x-y*R)/(x+S*M.sse+c.sse+h):(x-y*R)/(u+h);return Math.max(0,k)}let T=d==="partial"?(x-y*R)/(x+M.sse):(x-y*R)/u;return Math.max(0,T)},v=`${t}${d==="plain"?"":`_${d}`}`,E={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(b=>b.term.name),table:a.map(({term:b,stratum:M})=>{let x=N(b,M);return r===null?[x]:Mf(x,b.df,M.dfe,r,o)}),warnings:[]};return s||(E.groups=a.map(b=>b.stratum.name),E.warnings=[...li(e)||vf(e)?e.warnings:[]]),E}function _d(e,t={}){return V(t,Nd,"etaSquared"),H("etaSquared",ui(e,"Eta2",t),e)}function Ld(e,t={}){return V(t,yd,"omegaSquared"),H("omegaSquared",ui(e,"Omega2",t),e)}function Cd(e,t={}){return V(t,Md,"epsilonSquared"),H("epsilonSquared",ui(e,"Epsilon2",t),e)}var Od=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),Fd=["m1","sd1","n1","m2","sd2","n2","r12"],Dd=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),Pd="Equivalence interval does not include zero.",Vd="Warning: setting bound type to SMD produces biased results!",$d="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function Ef(e,t,n,r){if(e===1/0||e===-1/0)return e;let o=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return pr(s=>dn(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function zd(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(y,R,T)=>(1-(T-2)/(T*y*y))*R*R,i,c,a,l,f,m,u,h,p,d,g,w,N=t.biasCorrection;if(e.design==="two"){let{n1:y,n2:R,s1:T,s2:S}=e,k=T*T,L=S*S,_=2*y*R/(y+R);if(t.glass!==void 0){let I=t.glass==="glass1",[F,C,D,A]=I?[T,y,S,R]:[S,R,T,y];c=F,a=C-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Mr(a):1,f=l*o/c,p=1/R+1/y;let O=D/F;m=Math.sqrt(O*O/(A-1)+1/(C-1)+f*f/(2*(C-1))),u=o/(F*Math.sqrt(1/C+O*O/A)),h=y+R-2,d=Math.sqrt(2/_)}else if(t.varEqual)c=Math.sqrt(((y-1)*k+(R-1)*L)/(y+R-2)),a=y+R-2,i=N?"Hedges's g":"Cohen's d",l=N?Mr(a):1,f=l*o/c,p=1/y+1/R,m=Math.sqrt(1/y+1/R+s(l,f,a)),u=o/(c*Math.sqrt(p)),h=a,d=Math.sqrt(2*(R*k+y*L)/(y*R*(k+L)));else{c=Math.sqrt((k+L)/2),a=(y-1)*(R-1)*(k+L)**2/((R-1)*k*k+(y-1)*L*L),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Mr(a):1,f=l*o/c,p=2*(R*k+y*L)/(y*R*(k+L));let I=c**4;m=Math.sqrt(f*f*(k*k/(y-1)+L*L/(R-1))/(8*I)+(k/(y-1)+L/(R-1))/(c*c)),u=o/Math.sqrt(k/y+L/R),h=a,d=Math.sqrt(2/_)}g=a,w=(I,F)=>Math.sqrt(g/(g-2)*(2/_)*(1+I*I*_/2)-I*I/(F*F))}else if(e.design==="paired"){let{n:y,s1:R,s2:T,sdif:S,oneMinusR:k}=e,L=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?R:T:c=t.rmCorrection?S/Math.sqrt(2*k):S,i=t.glass!==void 0?N?"Glass's delta(g)":"Glass's delta(d)":N?`Hedges's g${L}`:`Cohen's d${L}`,a=y-1,g=2*y-2,l=N?Mr(r==="goulet"?g:a):1,f=l*o/c,p=1/y,u=o*Math.sqrt(y)/c,h=a,d=Math.sqrt(2*k/y),w=(_,I)=>{let F=r==="goulet"?g:a,C=Math.sqrt(F/(F-2)*(2*k/y)*(1+_*_*y/(2*k))-_*_/(I*I));return t.rmCorrection?C:C*Math.sqrt(2*k)},t.glass!==void 0){let _=S/c;m=Math.sqrt(_*_/(y-1)+f*f/(2*(y-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/y+s(l,f,a)):m=w(f,l)}else{let{n:y,s:R}=e;c=R,a=y-1,g=a,i=N?"Hedges's g":"Cohen's d",l=N?Mr(a):1,f=l*o/c,p=1/y,m=Math.sqrt(1/y+f*f/(2*y)),u=o*Math.sqrt(y)/R,h=a,d=1/Math.sqrt(y),w=(T,S)=>Math.sqrt(a/(a-2)*(1/y)*(1+T*T*y)-T*T/(S*S))}let v,E,b,M=a;if(r==="nct"){b=m,M=h;let y=Ef(u,h,n,!0),R=Ef(u,h,n,!1);v=y*Math.sqrt(p)*l,E=R*Math.sqrt(p)*l}else if(r==="goulet"){b=w(f,l),M=g;let y=f/d;v=rn(n,g,{ncp:y})*d,E=rn(n,g,{ncp:y,lowerTail:!1})*d}else{b=m;let y=r==="t"?xt(n,a,!1,!1):oe(n,{lowerTail:!1});v=f-y*b,E=f+y*b}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:b,confInt:x?[-E,-v]:[v,E],df:M,J:l}}function xf(e,t){if(!(Array.isArray(e)||ArrayBuffer.isView(e)))throw new TypeError(`'${t}' must be an array of numbers`);let n=e;for(let r=0;r<n.length;r++){let o=n[r];if(typeof o!="number")throw new TypeError(`'${t}' must be numeric`);if(o===1/0||o===-1/0)throw new RangeError(`'${t}' must not contain infinite values`)}}function Rf(e){let{eqb:t}=e,n;if(t!==void 0){let r=typeof t=="number"?[t]:Array.from(t);if(!r.every(o=>typeof o=="number"))throw new TypeError("'eqb' must be a number or an array of 1 or 2 numbers");if(r.length===1)n=[-Math.abs(r[0]),Math.abs(r[0])];else if(r.length===2)n=[Math.min(r[0],r[1]),Math.max(r[0],r[1])];else throw new RangeError("'eqb' must be a number or an array of 1 or 2 numbers")}else{if(e.lowEqbound===void 0||e.highEqbound===void 0)throw new TypeError("the equivalence bounds are required: 'eqb', or 'lowEqbound' and 'highEqbound'");n=[e.lowEqbound,e.highEqbound]}if(!n.every(r=>typeof r=="number"&&Number.isFinite(r)))throw new RangeError("the equivalence bounds must be finite numbers");if(n[0]>n[1])throw new RangeError("'lowEqbound' must not be greater than 'highEqbound'");return n}function Tf(e){let t=xe(e.hypothesis,["EQU","MET"]),n=xe(e.eqboundType,["raw","SMD"])==="SMD",r=xe(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:xe(e.glass,["glass1","glass2"]),s=e.alpha??.05;if(!(typeof s=="number"&&s>0&&s<.5))throw new RangeError("'alpha' must be a single number in (0, 0.5)");let i=e.mu??0;if(!(typeof i=="number"&&Number.isFinite(i)))throw new RangeError("'mu' must be a finite number");let c=(a,l,f)=>{if(a!==void 0&&typeof a!="boolean")throw new TypeError(`'${l}' must be true or false`);return a??f};return{hypothesis:t,smdBounds:n,smdCi:r,glass:o,alpha:s,mu:i,paired:c(e.paired,"paired",!1),varEqual:c(e.varEqual,"varEqual",!1),biasCorrection:c(e.biasCorrection,"biasCorrection",!0),rmCorrection:c(e.rmCorrection,"rmCorrection",!1)}}function Sf(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,p=zd(c,l),d=$(p.denom,a),g=l.smdBounds?[f[0]*d,f[1]*d]:f,w=D=>{if(D===0)return[0,n];let A=Math.floor(Math.log2(Math.abs(D))),O=et(D,-A);return l.smdBounds?[O*p.denom,A+a]:[O,A]},N=l.smdBounds?f:[f[0]/d,f[1]/d];l.smdBounds&&m.push(Vd),g[0]<=0&&g[1]>=0||m.push(Pd);let v=D=>$(D/r,n-o),E=D=>{let[A,O]=w(D),q=Math.max(n,O);return et((et(t,n-q)-et(A,O-q))/r,q-o)},b=v(t),M=E(f[0]),x=E(f[1]),y=$(r,o),R=h==="EQU",T={t:b,se:y,df:s,pValue:2*vt(-Math.abs(b),s,!0,!1)},S={t:M,se:y,df:s,pValue:vt(M,s,!R,!1)},k={t:x,se:y,df:s,pValue:vt(x,s,R,!1)},L=R?Math.max(S.pValue,k.pValue):Math.min(S.pValue,k.pValue);!R&&g[0]<=0&&g[1]>=0&&L<=T.pValue&&m.push($d);let _=xt(u,s,!1,!1),I=Math.max(n,o),[F,C]=[$(t,n-I),$(r,o-I)];return H(e,{tTest:T,lower:S,upper:k,pValue:L,bounds:{raw:g,smd:N},raw:{estimate:$(t,n),se:y,confInt:[$(F-_*C,I),$(F+_*C,I)]},smd:{label:p.label,estimate:p.estimate,se:p.se,confInt:p.confInt,df:p.df,J:p.J},confLevel:1-2*u,method:i,hypothesis:h,warnings:m})}function jd(e,t,n={}){V(n,Od,"tTOST");let r=Tf(n),o=Rf(n),{paired:s,varEqual:i,mu:c}=r,a=t===void 0;if(s&&a)throw new TypeError("'y' is required when paired is true");xf(e,"x"),a||xf(t,"y");let l=je(()=>a?vn(e,{mu:c}):vn(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,p;if(a){let d=Re([Ct(Array.from(e))]);u=d.k;let g=d.groups[0],w=ot(g);h=st(g)-$(c,-u),p=Math.sqrt(w/g.length),m={design:"one",n:g.length,delta:h,s:Math.sqrt(w)}}else if(s){let[d,g]=Gn(Array.from(e),Array.from(t)),w=Re([d,g]);u=w.k;let[N,v]=w.groups,E=N.map((T,S)=>T-v[S]),b=Math.sqrt(ot(N)),M=Math.sqrt(ot(v)),x=ot(E),y=Math.sqrt(x),R=(y-(b-M))*(y+(b-M))/(2*b*M);h=st(E)-$(c,-u),p=Math.sqrt(x/E.length),m={design:"paired",n:E.length,delta:st(N)-st(v)-$(c,-u),s1:b,s2:M,sdif:y,oneMinusR:R}}else{let d=Re([Ct(Array.from(e)),Ct(Array.from(t))]);u=d.k;let[g,w]=d.groups;if(g.length<2||w.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,v,E,b]=[g.length,w.length,ot(g),ot(w)];h=st(g)-st(w)-$(c,-u),p=Math.sqrt(i?((N-1)*E+(v-1)*b)/(N+v-2)*(1/N+1/v):Math.sqrt(E/N)**2+Math.sqrt(b/v)**2),m={design:"two",n1:N,n2:v,delta:h,s1:Math.sqrt(E),s2:Math.sqrt(b)}}return Sf("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function Kd(e,t={}){V(e,Fd,"tsumTOST"),V(t,Dd,"tsumTOST");let n=Tf(t),r=Rf(t),{m1:o,sd1:s,n1:i,m2:c,sd2:a,n2:l,r12:f}=e,m=(I,F)=>{if(typeof I!="number"||!Number.isFinite(I))throw new RangeError(`'${F}' must be a finite number`);return I},u=(I,F)=>{let C=m(I,F);if(!(Number.isInteger(C)&&C>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return C},h=(I,F)=>{let C=m(I,F);if(!(C>0))throw new RangeError(`'${F}' must be positive`);return C};m(o,"m1"),h(s,"sd1"),u(i,"n1");let p=c===void 0&&a===void 0&&l===void 0;if(n.paired&&p)throw new TypeError("'m2', 'sd2' and 'n2' are required when paired is true");p||(m(c,"m2"),h(a,"sd2"),u(l,"n2"));let d=[],{groups:g,k:w}=Re([[o,...p?[]:[c],n.mu]]),{groups:N,k:v}=Re([[s,...p?[]:[a]]]),[E,b=NaN]=p?[g[0][0]]:[g[0][0],g[0][1]],M=g[0][g[0].length-1],[x,y=NaN]=N[0],R=I=>$(I,w-v),T,S,k,L,_;if(p)T=E-M,S=x/Math.sqrt(i),k=i-1,L="One Sample t-test",_={design:"one",n:i,delta:R(T),s:x};else if(n.paired){let I=m(f,"r12");if(!(I>-1&&I<1))throw new RangeError("'r12' must be in (-1, 1)");let F=i;i!==l&&(d.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let C=Math.sqrt(x*x+y*y-2*I*x*y);if(!(C>0))throw new RangeError("the standard deviation of the differences is 0");T=E-b-M,S=C/Math.sqrt(F),k=F-1,L="Paired t-test",_={design:"paired",n:F,delta:R(T),s1:x,s2:y,sdif:C,oneMinusR:1-I}}else{let I=l,F=x*x,C=y*y;if(T=E-b-M,n.varEqual){let D=((i-1)*F+(I-1)*C)/(i+I-2);S=Math.sqrt(D*(1/i+1/I)),k=i+I-2,L=" Two Sample t-test"}else{let D=F/i,A=C/I;S=Math.sqrt(D+A),k=(D+A)**2/(D*D/(i-1)+A*A/(I-1)),L="Welch Two Sample t-test"}_={design:"two",n1:i,n2:I,delta:R(T),s1:x,s2:y}}return Sf("tsumTOST",T,w,S,v,k,L,_,v,n,r,d)}var Wd=["alternative","tsmethod"],Yd=["square","central"],Bd=1e3,Ud=32,Gd=.9,Af=1+1e-7,Hd=2**24,Xd=34e7;function Qd(e,t={}){if(V(t,Wd,"boschlooTest"),e.length!==2||e.some(M=>M.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ge(t.alternative),r=xe(t.tsmethod,Yd),o=e.map((M,x)=>Array.from(M,(y,R)=>Br(y,`table[${x}][${R}]`))),[[s,i],[c,a]]=o,l=s+i,f=c+a;if(l===0||f===0)throw new RangeError("each row (group) of 'table' must have at least one observation");if((l+1)*(f+1)>Hd)throw new RangeError(`Boschloo's test of these group sizes has ${(l+1)*(f+1)} possible tables, more than 2^24`);let m=l+f,u=Math.max(Bd,Math.ceil(Ud*Math.sqrt(m))),h=M=>Math.min(M+1,40*Math.sqrt(M));if(u*(h(l)+h(f)+4)>Xd)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,d;n!=="two.sided"?d=n:r==="square"?d="square":d=p>0?"greater":"less";let g=s+c,w;d==="less"?w=on(s,l,f,g,!0,!1):d==="greater"?w=on(s-1,l,f,g,!1,!1):w=je(()=>So(o)).pValue;let N=Zd(l,f,s,c,d),{value:v,at:E}=Jd(N,l,f,u),b=n==="two.sided"&&r==="central"?Math.min(1,2*v):v;return H("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:w,statisticName:"test statistic",pValue:b,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:E,warnings:[]})}function Zd(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let w=2;w<=s;w++)i[w]=ss(w);let c=t+1,a=new Float64Array((e+1)*c),l=Math.min(e,t)+1,f=new Float64Array(l),m=new Float64Array(l),u=new Float64Array(l);for(let w=0;w<=s;w++){let N=Math.max(0,w-t),E=Math.min(w,e)-N+1,b=-1/0;for(let y=0;y<E;y++){let R=N+y,T=-i[R]-i[e-R]-i[w-R]-i[t-w+R];f[y]=T,T>b&&(b=T)}let M=0;for(let y=0;y<E;y++)f[y]=Math.exp(f[y]-b),M+=f[y];for(let y=0;y<E;y++)f[y]=f[y]/M;let x=0;for(let y=0;y<E;y++)m[y]=x+=f[y];x=0;for(let y=E-1;y>=0;y--)u[y]=x+=f[y];if(o==="less")for(let y=0;y<E;y++)a[(N+y)*c+w-N-y]=m[y];else if(o==="greater")for(let y=0;y<E;y++)a[(N+y)*c+w-N-y]=u[y];else{let y=0;for(let R=1;R<E;R++)f[R]>f[y]&&(y=R);for(let R=0;R<E;R++){let T=f[R]*Af,S=-1,k=y;for(;k-S>1;){let F=S+k>>1;f[F]<=T?S=F:k=F}let L=y,_=E;for(;_-L>1;){let F=L+_>>1;f[F]<=T?_=F:L=F}let I=f[y]<=T?1:(S>=0?m[S]:0)+(_<E?u[_]:0);a[(N+R)*c+w-N-R]=Math.min(1,I)}}}let h=a[n*c+r]*Af,p=new Int32Array(e+2),d=[],g=[];for(let w=0;w<=e;w++){p[w]=d.length;let N=0;for(;N<=t;){if(a[w*c+N]<=h){let v=N;for(;N+1<=t&&a[w*c+N+1]<=h;)N++;d.push(v),g.push(N)}N++}}return p[e+1]=d.length,{starts:p,from:Int32Array.from(d),to:Int32Array.from(g)}}function kf(e,t,n){if(t===0||t===1){let h=t===0?0:e;return n[h]=1,[h,h]}let r=Math.min(e,Math.floor((e+1)*t));n[r]=fn(r,e,t,!1);let o=$t(1,-t),s=lt([t,0],o),i=lt(o,[t,0]),c=s[0],a=i[0],l=r;for(let h=r+1;h<=e;h++){let p=n[h-1]*((e-h+1)/h*c);if(p===0)break;n[h]=p,l=h}let f=r;for(let h=r-1;h>=0;h--){let p=n[h+1]*((h+1)/(e-h)*a);if(p===0)break;n[h]=p,f=h}let m=s[1]/c,u=i[1]/a;for(let h=r+1;h<=l;h++)n[h]=n[h]*(1+(h-r)*m);for(let h=f;h<r;h++)n[h]=n[h]*(1+(r-h)*u);return[f,l]}function Jd(e,t,n,r){let{starts:o,from:s,to:i}=e,c=new Float64Array(t+1),a=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=v=>{let[E,b]=kf(t,v,c),[M,x]=kf(n,v,a),y=0;for(let T=M;T<=x;T++)l[T]=y+=a[T];y=0;for(let T=x;T>=M;T--)f[T]=y+=a[T];let R=0;for(let T=E;T<=b;T++){let S=c[T];if(S===0)continue;let k=0;for(let L=o[T];L<o[T+1];L++){let _=s[L],I=i[L];if(!(I<M||_>x))if(_===0)k+=l[Math.min(I,x)];else if(I===n)k+=f[Math.max(_,M)];else for(let F=Math.max(_,M);F<=Math.min(I,x);F++)k+=a[F]}R+=S*k}return Math.min(1,R)},u=Math.PI/2,h=v=>v<=0?0:v>=u?1:Math.sin(v)**2,p=new Float64Array(r+2),d=0;for(let v=0;v<=r+1;v++)p[v]=m(h(u*v/(r+1))),p[v]>p[d]&&(d=v);let g=p[d],w=h(u*d/(r+1));if(g>=1)return{value:1,at:w};let N=Gd*g;for(let v=0;v<=r+1;v++){let E=p[v];if(E<N||v>0&&!(E>p[v-1])||v<=r&&!(E>=p[v+1]))continue;let b=u*Math.max(0,v-1)/(r+1),M=u*Math.min(r+1,v+1)/(r+1),y=nl(S=>-m(h(S)),b,M,1e-14).x,R=m(h(y)),T=1e-4/Math.sqrt(2*(t+n));for(let S=0;S<2&&(y-T>b&&y+T<M);S++,T*=.01){let k=m(h(y-T)),L=m(h(y+T)),_=k-2*R+L;if(!(_<0))break;let I=T*(k-L)/(2*_);if(!(Math.abs(I)<T))break;let F=m(h(y+I));F>R*(1+2**-50)&&(y+=I,R=F)}R>g&&(g=R,w=h(y))}return{value:g,at:w}}var qf=["correct"];function e0(e,t,n={}){at(t)&&V(t,qf,"mcnemarTest"),V(n,qf,"mcnemarTest");let r;if(e.length>0&&Array.isArray(e[0])){t!==void 0&&!Array.isArray(t)&&(n=t),r=e.map(f=>Array.from(f));let l=r.length;if(l<2||r.some(f=>f.length!==l))throw new RangeError("'x' must be square with at least two rows and columns");if(r.some(f=>f.some(m=>!Number.isFinite(m)||m<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}else{if(!Array.isArray(t))throw new TypeError("'y' is required when 'x' is not a table");r=t0(e,t)}let o=n.correct??!0,s=r.length,i,c=o&&s===2&&r[0][1]!==r[1][0];if(c){let l=r[0][1],f=r[1][0];i=If(Math.max(0,Math.abs(l-f)-1),l,f)}else{i=0;let l=0;for(let f=0;f<s;f++)for(let m=f+1;m<s;m++){let u=r[f][m],h=r[m][f];u+h!==0&&(i+=If(u-h,u,h),l++)}if(l===0)throw new RangeError("the table has no discordant pairs")}let a=s*(s-1)/2;return{method:c?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:i,statisticName:"McNemar's chi-squared",parameter:a,parameterName:"df",pValue:gt(i,a/2,2,!1,!1),warnings:[]}}function If(e,t,n){let r=t+n;return r<1/0?Dt(e,r):Dt($(e,-520),$(t,-1040)+$(n,-1040))}function t0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=en(e),o=en(t);for(let m=0;m<e.length;m++){let u=r[m],h=o[m];typeof u=="number"&&Number.isNaN(u)||typeof h=="number"&&Number.isNaN(h)||n.push([u,h])}let s=m=>Array.from(new Set(m)).sort(Vn),i=s(n.map(m=>m[0])),c=s(n.map(m=>m[1]));if(i.length<2||i.length!==c.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let a=new Map(i.map((m,u)=>[m,u])),l=new Map(c.map((m,u)=>[m,u])),f=i.map(()=>new Array(i.length).fill(0));for(let[m,u]of n)f[a.get(m)][l.get(u)]++;return f}var n0=["ratio","alternative","confLevel"],hi=2**-1022;function _f(e,t,n={}){if(V(n,n0,"varTest"),typeof e=="string"){let v=Qn(e,t);return _f(v.x,v.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=n.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let i=Ge(n.alternative),c=n.confLevel??.95;It(c);let a=r.length-1,l=o.length-1;if(a<1)throw new Error("not enough 'x' observations");if(l<1)throw new Error("not enough 'y' observations");let[f,m]=Et([r,o]).variance,u=0;if(!(f>=hi&&m>=hi)){let v=Et([r]),E=Et([o]);f=v.variance[0],m=E.variance[0],u=2*(v.k-E.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:et(f/m,u),p=h/s,d=wt(p,a,l,!0,!1),g=wt(p,a,l,!1,!1),w,N;if(i==="less")w=d,N=[0,h/ln(1-c,a,l,!0,!1)];else if(i==="greater")w=g,N=[h/ln(c,a,l,!0,!1),1/0];else{w=Math.min(1,2*Math.min(d,g));let v=(1-c)/2,E=v<1e-8?ln(v,a,l,!1,!1):ln(1-v,a,l,!0,!1);N=[h/E,h/ln(v,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:p,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:w,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function Lf(e,t){if(typeof e=="string"){let p=vo(e,t);return Lf(el(p.values,p.group))}let n=e,r=n.length;if(r<2)throw new RangeError("'x' must be a list with at least 2 elements");let o=n.map(p=>Array.from(p).filter(Number.isFinite));if(o.some(p=>p.length<2))throw new RangeError("there must be at least 2 observations in each group");let s=o.map(p=>p.length-1),i=Et(o).variance,c=0,a=0,l=0,f=0;for(let p=0;p<r;p++)c+=s[p],a+=s[p]*i[p],l+=s[p]*Math.log(i[p]),f+=1/s[p];a/=c;let m=c*Math.log(a)-l;if(i.some(p=>p<hi)){let p=o.map(E=>{let b=Et([E]);return{w:b.variance[0],e:b.k}}),d=Math.max(...p.map(E=>E.e)),g=p.map(E=>Math.log(E.w)+2*(E.e-d)*Math.LN2),w=g.map((E,b)=>Math.log(s[b])+E),N=Math.max(...w),v=N+Math.log(w.reduce((E,b)=>E+Math.exp(b-N),0))-Math.log(c);m=c*v-g.reduce((E,b,M)=>E+s[M]*b,0)}let u=m/(1+(f-1/c)/(3*(r-1))),h=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:u,statisticName:"Bartlett's K-squared",parameter:h,parameterName:"df",pValue:gt(u,h/2,2,!1,!1),warnings:[]}}var r0=["alternative"],pi=["alternative"];function di(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:gt(n,r/2,2,!1,!1),warnings:[]}}function Cf(e){let t=0;for(let n of pn(e))t+=n*n*n-n;return t}function Vs(e,t){if(typeof e=="string"){let l=vo(e,t);return Vs(l.values,l.group.codes)}let n=dr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),s=o.length,i=bt(o),c=0,a=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=i[a+m];a+=l.length,c+=f*f/l.length}return c=(12*c/(s*(s+1))-3*(s+1))/(1-Cf(o)/(s*s*s-s)),di("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function gi(e,t){if(typeof e=="string")return gi(us(e,t));let n=va(e),r=n.length,o=e[0]?.length??0;if(r<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=new Array(o).fill(0),i=0;for(let l of n){let f=bt(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=Cf(l)}let c=0;for(let l of s)c+=(l-r*(o+1)/2)**2;let a=12*c/(r*o*(o+1)-i/(o-1));return di("Friedman rank sum test","Friedman chi-squared",a,o-1)}function o0(e,t={},n,r={}){typeof e!="string"&&at(t)&&V(t,pi,"pageTest"),at(n)&&V(n,pi,"pageTest"),V(r,pi,"pageTest");let o,s;if(typeof e=="string")o=us(e,t),s=n??{};else if(Array.isArray(n)){let d=Array.from(e),g=t,w=n;if(d.length!==g.length||d.length!==w.length)throw new RangeError("'x', 'groups' and 'blocks' must have the same length");if(!d.every(Number.isFinite))throw new RangeError("missing or non-finite values are not allowed");let N=en(g),v=en(w);if(N.some(y=>typeof y=="number"&&Number.isNaN(y))||v.some(y=>typeof y=="number"&&Number.isNaN(y)))throw new RangeError("missing treatment or block labels are not allowed");let E=Array.from(new Set(N)).sort(Vn),b=Array.from(new Set(v)).sort(Vn);if(E.length<2||b.length<1)throw new RangeError("at least two treatments and one block are required");let M=new Map(E.map((y,R)=>[y,R])),x=new Map(b.map((y,R)=>[y,R]));o=b.map(()=>new Array(E.length).fill(NaN));for(let y=0;y<d.length;y++){let R=x.get(v[y]),T=M.get(N[y]);if(!Number.isNaN(o[R][T]))throw new RangeError("not an unreplicated complete block design");o[R][T]=d[y]}if(o.some(y=>y.some(R=>Number.isNaN(R))))throw new RangeError("not an unreplicated complete block design");s=r}else o=e.map(d=>Array.from(d)),s=t;let i=o.length,c=o[0]?.length??0;if(i<1||c<2||o.some(d=>d.length!==c))throw new RangeError("'y' must be a non-empty matrix with at least two columns");if(o.some(d=>d.some(g=>!Number.isFinite(g))))throw new RangeError("missing or non-finite values are not allowed in a block");let a=Ge(s.alternative),l=new Array(c).fill(0);for(let d of o){let g=bt(d);for(let w=0;w<c;w++)l[w]+=g[w]}let f=0;for(let d=0;d<c;d++)f+=(d+1)*l[d];let m=i*c*(c+1)**2/4,u=i*c**2*(c-1)*(c+1)**2/144,h=(f-m-.5)/Math.sqrt(u),p=a==="less"?ie(h):a==="greater"?ie(h,{lowerTail:!1}):2*ie(-Math.abs(h));return{method:"Page's ordered aligned rank sum test",alternative:a,statistic:h,statisticName:"z",pValue:p,estimate:f,warnings:[]}}function s0(e){if(e.some(t=>t.some(n=>!Number.isNaN(n)&&n!==0&&n!==1)))throw new RangeError("the responses must be 0 or 1 (or NaN)");return{...gi(e),method:"Cochran's Q test",statisticName:"Q"}}function Of(e,t){if(typeof e=="string")return Of(us(e,t));let n=va(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(d=>At(d)-sn(d));s.some(d=>d===1/0)&&n.every(d=>d.every(Number.isFinite))&&(s=Re(n).groups.map(d=>At(d)-sn(d)));let i=bt(s),c=new Array(o).fill(0),a=0;n.forEach((d,g)=>{let w=bt(d);for(let N=0;N<o;N++){let v=i[g]*(w[N]-(o+1)/2);a+=v*v,c[N]+=v}});let l=0;for(let d of c)l+=d*d;l/=r;let f=o-1,m=(r-1)*(o-1),u,h,p=[f,m];if(a===l){u=NaN,p=[NaN,NaN];let d=0;for(let g=2;g<=o;g++)d+=Math.log(g);h=Math.exp((1-r)*d)}else u=(r-1)*l/(a-l),h=wt(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:p,parameterName:["num df","denom df"],pValue:h,warnings:[]}}function Ff(e,t){if(typeof e=="string"){let h=vo(e,t);return Ff(h.values,h.group.codes)}let n=dr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=h=>h.flatMap(p=>{let d=ws(p);return p.map(g=>Math.abs(g-d))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(h=>h===1/0)&&n.every(h=>h.every(Number.isFinite))&&(s=o(Re(n).groups));let i=s.length,a=bt(s).map(h=>oe((1+h/(i+1))/2)),l=0;for(let h of a)l+=h;let f=l/i,m=0,u=0;for(let h of n){let p=0;for(let d=0;d<h.length;d++)p+=a[u+d];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=ot(a),di("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function Df(e,t,n={}){if(V(n,r0,"moodTest"),typeof e=="string"){let E=Qn(e,t);return Df(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ge(n.alternative),i=r.length,c=o.length,a=i+c;if(a<3)throw new RangeError("not enough observations");if(i===0||c===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:a},(E,b)=>b).sort((E,b)=>l[E]-l[b]),m=new Array(a),u=0;for(;u<a;){let E=u;for(;E+1<a&&l[f[E+1]]===l[f[u]];)E++;let b=0;for(let M=u;M<=E;M++)b+=(M+1-(a+1)/2)**2;b/=E-u+1;for(let M=u;M<=E;M++)m[f[M]]=b;u=E+1}let h=0;for(let E=0;E<i;E++)h+=m[E];let p=0;for(let E of m)p+=E;p/=a;let d=0;for(let E of m)d+=(E-p)**2;let g=i*p,w=i*c/(a*(a-1))*d,N=(h-g)/Math.sqrt(w),v=s==="less"?ie(N):s==="greater"?ie(N,{lowerTail:!1}):2*ie(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:v,warnings:[]}}function Er(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var a0=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],i0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],c0=[.544,-.39978,.025054,-6714e-7],l0=[1.3822,-.77857,.062767,-.0020322],f0=[-1.5861,-.31082,-.083751,.0038915],m0=[-.4803,-.082676,.0030302],u0=[-2.273,.459];function h0(e){if(!(e>=3)||e!==Math.floor(e))throw new RangeError("n must be an integer of at least 3");let t=new Float64Array(e);if(e===3)return t[0]=-Math.SQRT1_2,t[2]=Math.SQRT1_2,t;let n=Math.floor(e/2),r=new Float64Array(n),o=0;for(let m=0;m<n;m++)r[m]=-oe((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),i=1/Math.sqrt(e),c=r[0]/s+Er(a0,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Er(i0,i);a[1]=m,l=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*c*c-2*m*m))}else l=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*c*c));for(let m=l;m<n;m++)a[m]=r[m]/f;for(let m=0;m<n;m++)t[e-1-m]=a[m],t[m]=-a[m];return t}function Pf(e){let t=Re([e]).groups[0],n=t.length,r=h0(n),o=t[n-1]-t[0],s=0;for(let f of t)s+=f/o;s/=n;let i=0,c=0,a=0;for(let f=0;f<n;f++){let m=t[f]/o-s;i+=r[f]*r[f],c+=m*m,a+=r[f]*m}let l=Math.sqrt(i*c);return(l-a)*(l+a)/(i*c)}var p0=1.90985931710274,d0=1.0471975511966;function Vf(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=p0*(Math.asin(Math.sqrt(1-e))-d0);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Er(u0,t);if(n>=i)return 1e-99;let c=Er(c0,t),a=Math.exp(Er(l0,t));return ie(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Er(f0,r),s=Math.exp(Er(m0,r));return ie(n,{mean:o,sd:s,lowerTail:!1})}var Kf=Math.log(2*Math.PI),$f=2e-14,zf=500;function g0(e){return Math.exp(-.5*e*e-.5*Kf)}function w0(e){return ie(e,{logP:!0})}function Wf(e,t=-1/0,n=!1){let r=t===-1/0?ga(e,0,2,0,$f,zf):ga(e,t,1,0,$f,zf),o=r.ier===1||r.ier===3||r.ier===5;return n&&o&&!(r.abserr<=1e-6*Math.abs(r.result))?NaN:r.result}function jf(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Wf(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=w0(l);continue}let f=(r[a]*i-e)/o[a],m=ie(f)+ie(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?ie(l)-ie(f):ie(f,{lowerTail:!1})-ie(l,{lowerTail:!1})))}return g0(i)*-Math.expm1(c)})}function xr(e,t,n,r){if(Number.isNaN(e)||Number.isNaN(n))return NaN;if(r&&(e=Math.abs(e)),e===1/0)return 0;if(e===-1/0||r&&e===0)return 1;if(n===1/0)return Math.min(1,jf(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Kf-_c(o);return Math.min(1,Wf(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*jf(e*Math.exp(c),t,r)},-1/0,!0))}var b0=["pAdjustMethod"],v0=["pAdjustMethod","alternative"],N0=["alternative"],y0=["pAdjustMethod","alternative"],M0=["method","altp"],E0=["alternative"],x0=["welch"],Yf="Ties are present. z-quantiles were corrected for ties.";function Sn(e,t){let{labels:n,groups:r}=Zn(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(i=>i.length);return{labels:n,groups:r,n:s,N:s.reduce((i,c)=>i+c,0),k:o}}function wi(e){let t=e.groups.flat(),n=bt(t),r=[],o=0;for(let i of e.n){let c=0;for(let a=0;a<i;a++)c+=n[o+a];o+=i,r.push(c/i)}let s=0;for(let i of pn(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function Rr(e,t){let n=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));for(let r=0;r<e-1;r++)for(let o=r+1;o<e;o++)n[o-1][r]=t(o,r);return n}function R0(e,t){let n=e.length+1,r=[];for(let c=0;c<n-1;c++)for(let a=c+1;a<n;a++)r.push(e[a-1][c]);let o=Yr(r,t),s=0,i=e.map(c=>c.slice());for(let c=0;c<n-1;c++)for(let a=c+1;a<n;a++)i[a-1][c]=o[s++];return i}function Lo(e,t,n,r,o,s=[]){return{method:t,statistic:n,pValue:r,rowNames:e.labels.slice(1),colNames:e.labels.slice(0,-1),pAdjustMethod:o,warnings:s}}function bi(e,t,n,r,o,s=[]){return{method:t,statistic:n.map(i=>[i]),pValue:r.map(i=>[i]),rowNames:e.labels.slice(1),colNames:e.labels.slice(0,1),pAdjustMethod:o,warnings:s}}var $s=e=>ie(e,{lowerTail:!1}),vi=(e,t,n)=>as(Math.abs(e),t,n,{lowerTail:!1});function Ni(e){let t=Et(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Co(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Co(e,t){if(e===0)throw new RangeError("no residual degrees of freedom");if(t===0)throw new RangeError("the pooled within-group variance is 0")}function T0(e,t,n={}){V(n,b0,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=Sn(e,t),{R:s,C:i}=wi(o),c=o.N*(o.N+1)/12-i,a=Rr(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=R0(a.map(f=>f.map(m=>2*$s(m))),r);return H("kwAllPairsDunnTest",Lo(o,"Dunn's all-pairs test",a,l,r,i!==0?[Yf]:[]))}function S0(e,t,n={}){V(n,v0,"kwManyOneDunnTest");let r=Ge(n.alternative),o=n.pAdjustMethod??"single-step",s=Sn(e,t),{R:i,C:c}=wi(s),a=s.N*(s.N+1)/12-c,l=s.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(i[h]-i[0])/Math.sqrt(a*(1/s.n[0]+1/s.n[h]))),m=l.map(h=>s.n[h]/(s.n[h]+s.n[0])),u=Bf(f,m,r,o);return H("kwManyOneDunnTest",bi(s,"Dunn's many-to-one test",f,u,o,c!==0?[Yf]:[]))}function Bf(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?xr(o,t,1/0,!0):xr(n==="greater"?o:-o,t,1/0,!1)):Yr(e.map(o=>n==="two.sided"?2*$s(Math.abs(o)):$s(n==="greater"?o:-o)),r)}function A0(e,t,n={}){V(n,N0,"dunnettTest");let r=Ge(n.alternative),o=Sn(e,t),{m:s,s2:i,df:c}=Ni(o),a=Math.sqrt(i),l=o.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(s[h]-s[0])/(a*Math.sqrt(1/o.n[h]+1/o.n[0]))),m=l.map(h=>o.n[h]/(o.n[h]+o.n[0])),u=f.map(h=>r==="two.sided"?Math.min(1,2*xr(Math.abs(h),m,c,!1)):xr(r==="greater"?h:-h,m,c,!1));return H("dunnettTest",bi(o,`Dunnett's-test for multiple
|
|
4
|
-
comparisons with one control`,f,u,"single-step"))}function
|
|
1
|
+
/*! rStats.js 1.20.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{a as rn,b as Qn,c as hr,d as Ir,e as Bs,f as Qa}from"./chunks/chunk-6QCH4LAL.js";import{a as Xo,c as ri}from"./chunks/chunk-OFDD4GTZ.js";import{a as Te,b as qe,c as Rn,d as ft,e as Uo,f as Sp,g as Ap,h as Ga,i as Ha,j as kp,k as Za,l as Ja,m as sf,n as af,o as cf,p as ei,q as Dp}from"./chunks/chunk-5RRFO4NU.js";import{a as Zl,b as Gs,c as pr,d as Hs,e as tf,f as Xn,g as nf,i as Ip,j as _p}from"./chunks/chunk-VSV5QGXX.js";import{a as En,c as cn,h as no,i as Il,m as _l,n as ro,o as Cl,s as lr,t as Fs,u as Ps,v as Ds}from"./chunks/chunk-6ISDROPG.js";import{a as Wo}from"./chunks/chunk-64DDDGJU.js";import{b as zl}from"./chunks/chunk-6LAKI2QG.js";import{a as ti}from"./chunks/chunk-L4ZS7MAX.js";import{a as ni}from"./chunks/chunk-33ODAPMR.js";import{a as $s,b as ur,c as Bo,d as Wl,e as jl,f as zs,g as Ge,h as fn,i as Tn,j as Ct,k as Hl,l as qp,n as Xs,o as Ho}from"./chunks/chunk-RXK4KSKH.js";import{a as io}from"./chunks/chunk-VCXPGCA4.js";import{a as Os,b as Ll}from"./chunks/chunk-JEELIABM.js";import"./chunks/chunk-7KPFNJBE.js";import"./chunks/chunk-3ST6RZXG.js";import{a as ql,d as Ls}from"./chunks/chunk-YEDVOX5V.js";import{F as lf}from"./chunks/chunk-ZCTB4N5J.js";import{b as Vp,c as $p,d as zp}from"./chunks/chunk-66TEOHPF.js";import{a as Cr,b as xn,c as Qs,e as Go,f as rf,g as ao,h as Cp,i as Lp,k as Op,l as Fp,m as Pp}from"./chunks/chunk-OOHFFLMY.js";import{f as ef}from"./chunks/chunk-AWXW4WSX.js";import{b as Ze,c as jn}from"./chunks/chunk-CAUNJ7S6.js";import{b as Qo}from"./chunks/chunk-LZTDYQRS.js";import{a as Bl,b as Ul,c as Gl,e as qr}from"./chunks/chunk-QCQBK5AC.js";import{b as Ua}from"./chunks/chunk-DPCCSFCG.js";import{a as lt,b as Ks,c as Ys}from"./chunks/chunk-WMEF5XWO.js";import{a as Xa}from"./chunks/chunk-UC3YYJHM.js";import{c as js}from"./chunks/chunk-PVRNAOO7.js";import{a as _r,b as so}from"./chunks/chunk-LZAB4M7P.js";import"./chunks/chunk-6RA73LXX.js";import{d as ot}from"./chunks/chunk-OOO5AAM7.js";import{a as Yt,d as Xl,e as Ws,k as Us,o as wn,p as of}from"./chunks/chunk-DFGW4QWU.js";import{a as mr,b as Yl,g as oo}from"./chunks/chunk-MXRN2G7O.js";import{d as Ss,e as As,g as ks,h as eo,m as dn,n as Bn}from"./chunks/chunk-UPACHZ4F.js";import{a as Ba,b as at,c as it,e as yn,f as an}from"./chunks/chunk-VFL4GGYT.js";import{A as Cn,B as to,E as sr,i as vt,l as xs,u as Sl,x as Nt,y as pn}from"./chunks/chunk-INNFAVT3.js";import{a as Jl}from"./chunks/chunk-BU4I544S.js";import{a as Ln,c as Vs,f as Ql}from"./chunks/chunk-HEPGGURG.js";import{b as Ol,c as Fl,d as Pl,e as Dl,f as Vl,i as Yo,j as $l,l as xp,n as J,o as Ke,p as pt}from"./chunks/chunk-MTSU2QHB.js";import{a as jo,b as Hn,c as Kl,d as Pt,e as xt}from"./chunks/chunk-B5GSH3AK.js";import{a as kt,b as fr,c as ln}from"./chunks/chunk-7C2ECGT6.js";import"./chunks/chunk-VL72MQOQ.js";import{A as Ko,C as Ml,D as yl,F as El,P as Tl,Q as Rl,U as xl,e as Ya,q as Rs,r as Wa,s as Zt,t as oe,u as re}from"./chunks/chunk-PJJJR3H6.js";import{A as Al,C as qs,D as Rt,E as Is,F as kl,G as _s,H as ja,I as Ie,J as Mt,K as Mn,L as Ft,M as cr,N as Dt,O as Cs,P as Re,b as zt,c as Xt,d as he,e as vl,f as Le,g as _e,h as Kt,i as ut,j as ht,k as Nl,l as z,m as _t,r as Un,s as gn,t as Gn,u as W,v as tt,w as ar,x as ct,y as st,z as ir}from"./chunks/chunk-ARPXG2PG.js";var ff=["alternative","mu","paired","varEqual","confLevel"];function Sn(e,t,n={}){if(typeof e!="string"&<(t)&&z(t,ff,"tTest"),z(n,ff,"tTest"),typeof e=="string")return Kp(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(n.alternative),s=n.mu??0,i=n.paired??!1,a=n.varEqual??!1,c=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(fn(c),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[S,I]=cr(Array.from(e),Array.from(r));({d:l,k:m}=Is(S,I))}else{let S=Re(r===void 0?[Ft(Array.from(e))]:[Ft(Array.from(e)),Ft(Array.from(r))]);m=S.k,l=S.groups[0],f=S.groups[1]}let u=W(s,-m),h,p,d,g,w,N,b,E=l.length,v=f!==void 0&&a;if(E<1||!v&&E<2)throw new Error("not enough 'x' observations");let y=ct(l),T=E>1?st(l):NaN;if(f===void 0){if(p=E-1,d=Math.sqrt(T/E),d===0||d<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/d,N=i?"Paired t-test":"One Sample t-test",g=y,w=i?"mean difference":"mean of x",b=i?"mean difference":"mean"}else{let S=f.length;if(S<1||!a&&S<2)throw new Error("not enough 'y' observations");if(a&&E+S<3)throw new Error("not enough observations");let I=ct(f),C=S>1?st(f):NaN;if(N=`${a?"":"Welch"} Two Sample t-test`,g=[y,I],w=["mean of x","mean of y"],b="difference in means",a){p=E+S-2;let _=0;E>1&&(_+=(E-1)*T),S>1&&(_+=(S-1)*C),_/=p,d=Math.sqrt(_*(1/E+1/S))}else{let _=T/E,F=C/S;d=Math.sqrt(Math.sqrt(_)**2+Math.sqrt(F)**2),p=_s([_,F],[E-1,S-1])}if(d===0||d<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(I)))throw new Error("data are essentially constant");h=(y-I-u)/d}let M=typeof g=="number"?g:g[0]-g[1],R=(S,I)=>{let C=u+(h+I*S)*d;return Number.isFinite(C)&&Math.max(Math.abs(u),Math.abs(h*d))<=16*(Math.abs(M)+S*d)?C:M+I*S*d},x,A;if(o==="less")x=at(h,p,!0,!1),A=[-1/0,R(it(c,p,!0,!1),1)];else if(o==="greater")x=at(h,p,!1,!1),A=[R(it(c,p,!0,!1),-1),1/0];else{x=2*at(-Math.abs(h),p,!0,!1);let S=1-c,I=S/2<1e-8?it(S/2,p,!1,!1):it(1-S/2,p,!0,!1);A=[R(I,-1),R(I,1)]}return A=[W(A[0],m),W(A[1],m)],g=typeof g=="number"?W(g,m):g.map(S=>W(S,m)),d=W(d,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:x,confInt:A,confLevel:c,estimate:g,estimateNames:w,nullValue:s,nullValueName:b,stdErr:d,stdError:d,warnings:[]}}function Kp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=$s(e,t,!0);if(r.kind==="one")return Sn(r.x,n);if(r.kind==="paired")return Sn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...Sn(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 Yp=["correct","p","rescaleP"],Wp=["confLevel"],jp=["alternative","rowScores","colScores"];function Jt(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function si(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 ai=e=>e.length>0&&typeof e[0]!="number",ii=(e,t)=>Number.isNaN(e)?NaN:vt(e,t/2,2,!1,!1);function mf(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 a=o===0?0:(s+e)/o;return(2*(e-t)+2*a)/(n+r+Math.sqrt(4*t+1))}let i=(e-t)*((e+t)/o)+e/o;return(.5*(e-t)+.5*i)/((n+r+2*Math.sqrt(t+.25))/4)}function Zo(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>mf(o,t[r][s]))):e.map((n,r)=>mf(n,t[r]))}var Bp=["none","williams","yates"],hf="Log likelihood ratio (G-test)";function Up(e,t={}){z(t,Yp,"gTest");let n=Te(t.correct,Bp);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(ai(e)){let o=si(e);return uf(o.flat()),o.length===1?J("gTest",oi(o[0],n,t)):o[0].length===1?J("gTest",oi(o.map(s=>s[0]),n,t)):J("gTest",Gp(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 uf(r),J("gTest",oi(r,n,t))}function uf(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 pf(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function df(e,t){if(e.some(r=>r<0))return NaN;let n=[0,0];for(let r=0;r<e.length;r++){let o=e[r],s=t[r];if(o>0&&s[0]===0)return 1/0;n=he(n,yl(o,s))}return n[0]+n[1]}function Gp(e,t){let n=e.length,r=e[0].length;if(t==="yates"&&(n!==2||r!==2))throw new RangeError("Yates' correction requires a 2 x 2 matrix");let o=Jt(e.flat()),s=pf(o[0],n*r),i=s===0?e:e.map(b=>b.map(E=>W(E,-s))),a=s===0?o:Jt(i.flat()),c=i.map(b=>Jt(b)),l=i[0].map((b,E)=>Jt(i.map(v=>v[E]))),f=c.map(b=>l.map(E=>_e(b,ut(E,a)))),m=i;if(t==="yates"){let[[b,E],[v,y]]=i,T=b*y-E*v>0?-.5:.5,M=W(.5,-s),R=T<0?-M:M;m=[[b+R,E-R],[v-R,y+R]]}let u=df(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(c.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=a[0]+a[1],E=c.reduce((y,T)=>y+1/(T[0]+T[1]),0),v=l.reduce((y,T)=>y+1/(T[0]+T[1]),0);p=1+W((b*E-1)*(b*v-1)/(6*b*(n-1)*(r-1)),-s)}let d=W(2*u,s)/p,g=(n-1)*(r-1),w=f.map(b=>b.map(E=>W(E[0]+E[1],s)));return{method:`${hf} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:d,statisticName:"G",parameter:g,parameterName:"X-squared df",pValue:ii(d,g),observed:s===0?m:m.map(b=>b.map(E=>W(E,s))),expected:w,ftResiduals:Zo(e,w),warnings:h}}function oi(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(b=>typeof b!="number"||Number.isNaN(b)))throw new RangeError("'p' must not contain missing values");if(s.some(b=>b<0))throw new RangeError("probabilities must be non-negative");if(s.some(b=>b===1/0))throw new RangeError("probabilities must be finite");let i=Jt(s);if(o){if(!(i[0]>0&&i[0]<1/0))throw new RangeError("the probabilities to rescale must have a positive finite sum")}else if(Math.abs(i[0]-1+i[1])>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let a=Jt(e),c=pf(a[0],r),l=c===0?e:e.map(b=>W(b,-c)),f=c===0?a:Jt(l),m=s.map(b=>o?ut(Kt(f,b),i):Kt(f,b)),u=m.map(b=>b[0]+b[1]),h=l;if(t==="yates"){let b=W(.5,-c),E=l[0]-u[0];E>W(.25,-c)?h=[l[0]-b,l[1]+b]:-E>W(.25,-c)&&(h=[l[0]+b,l[1]-b])}let p=df(h,m);if(!o&&!Number.isNaN(p)){let b=f[0]+f[1];p-=b*(i[0]-1+i[1])}let d=Number.isNaN(p)?["NaNs produced"]:[],g=t==="williams"?1+(r+1)/(6*W(f[0]+f[1],c)):1,w=W(2*p,c)/g,N=u.map(b=>W(b,c));return{method:`${hf} goodness of fit test`,statistic:w,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:ii(w,r-1),observed:c===0?h:h.map(b=>W(b,c)),expected:N,ftResiduals:Zo(e,N),warnings:d}}function Hp(e){if(typeof e!="object"||e===null||!ai(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=si(e),n=t.length;if(n<2||t[0].length!==n)throw new RangeError("'x' must be square with at least two rows and columns");if(t.flat().some(r=>!(r>=0&&r<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");return t}function Qp(e){let t=Hp(e),n=Jt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(v=>v.map(y=>W(y,-r))),s=o.length,i=o.map(v=>Jt(v)),a=o.map((v,y)=>Jt(o.map(T=>T[y]))),c=o.map((v,y)=>o[y].some((T,M)=>M!==y&&T>0)||o.some((T,M)=>M!==y&&T[y]>0)).flatMap((v,y)=>v?[y]:[]),l=c.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=c.map((v,y)=>y),m=v=>f[v]===v?v:f[v]=m(f[v]);for(let v=0;v<l;v++)for(let y=v+1;y<l;y++)o[c[v]][c[y]]+o[c[y]][c[v]]>0&&(f[m(v)]=m(y));if(c.some((v,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=c.slice(0,u).map(v=>{let y=he(i[v],[-a[v][0],-a[v][1]]);return y[0]+y[1]}),p=c.slice(0,u).map(v=>c.slice(0,u).map(y=>{if(v!==y)return-(o[v][y]+o[y][v]);let T=0;for(let M=0;M<s;M++)M!==v&&(T+=o[v][M]+o[M][v]);return T})),d=p.map(v=>v.map(()=>0)),g=[];for(let v=0;v<u;v++){let y=p[v][v];for(let T=0;T<v;T++)y-=d[v][T]*d[v][T]*g[T];g.push(y);for(let T=v+1;T<u;T++){let M=p[T][v];for(let R=0;R<v;R++)M-=d[T][R]*d[v][R]*g[R];d[T][v]=M/y}}let w=0,N=[];for(let v=0;v<u;v++){let y=h[v];for(let T=0;T<v;T++)y-=d[v][T]*N[T];N.push(y),w+=y*y/g[v]}let b=Jt(c.flatMap(v=>c.map(y=>t[v][y]))),E=l-1;return w=W(w,r),J("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:w,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:ii(w,E),N:b[0]+b[1],warnings:[]})}function Xp(e,t={}){z(t,Wp,"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=gf(e),o=Jt(r.flat())[0],s=0;(o>2**250||o>0&&o<2**-250)&&(s=2*Math.round((Math.log2(o)-250)/2));let i=s===0?r:r.map(v=>v.map(y=>W(y,-s))),{pc:a,pd:c}=Zp(i),l=[0,0],f=[0,0];i.forEach((v,y)=>v.forEach((T,M)=>{l=he(l,Kt(a[y][M],T)),f=he(f,Kt(c[y][M],T))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=he(l,f),u=ut(Le(l,f),m)[0],h=_e(m,m),p=i.map((v,y)=>v.map((T,M)=>2*ut(Le(_e(f,a[y][M]),_e(l,c[y][M])),h)[0])),d=Math.max(...p.flat().map(Math.abs)),g=d>0&&d<1/0?Math.floor(Math.log2(d)):0,w=0;i.forEach((v,y)=>v.forEach((T,M)=>{let R=W(p[y][M],-g);w+=T*R*R}));let N=W(Math.sqrt(w),g-s/2),b=(1-n)/2,E=b<1e-8?re(b,{lowerTail:!1}):re(1-b);return J("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function gf(e){if(typeof e!="object"||e===null||!ai(e))throw new TypeError("'table' must be an array of rows");let t=si(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 Zp(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let d=[],g=r;for(let w=0;w<n;w++)d.push(g),g=he(g,[p[w],0]);return d}),s=e.map(p=>{let d=new Array(n),g=r;for(let w=n-1;w>=0;w--)d[w]=g,g=he(g,[p[w],0]);return d}),i=p=>{let d=[],g=new Array(n).fill(r);for(let w=0;w<t;w++)d.push(g),g=g.map((N,b)=>he(N,p[w][b]));return d},a=p=>{let d=new Array(t),g=new Array(n).fill(r);for(let w=t-1;w>=0;w--)d[w]=g,g=g.map((N,b)=>he(N,p[w][b]));return d},c=i(o),l=a(s),f=i(s),m=a(o),u=c.map((p,d)=>p.map((g,w)=>he(g,l[d][w]))),h=f.map((p,d)=>p.map((g,w)=>he(g,m[d][w])));return{pc:u,pd:h}}function Jp(e,t={}){z(t,jp,"lblTest");let n=Ge(t.alternative),r=gf(e),o=r.length,s=r[0].length,i=(R,x,A)=>{if(R===void 0)return Array.from({length:x},(I,C)=>C+1);let S=Array.from(R);if(S.length!==x)throw new RangeError(`'${A}' must have one score for each ${A==="rowScores"?"row":"column"}`);if(S.some(I=>typeof I!="number"||!Number.isFinite(I)))throw new RangeError(`'${A}' must be finite numbers`);return S},a=i(t.rowScores,o,"rowScores"),c=i(t.colScores,s,"colScores"),f=Jt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(R=>R.map(x=>W(x,-f))),u=m.map(R=>Jt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,x)=>Jt(m.map(A=>A[x]))).map(R=>R[0]+R[1]),p=u.reduce((R,x)=>R+x,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let d=u.reduce((R,x,A)=>R+x*a[A],0)/p,g=h.reduce((R,x,A)=>R+x*c[A],0)/p,w=a.map(R=>R-d),N=c.map(R=>R-g),b=u.reduce((R,x,A)=>R+x*w[A]*w[A],0),E=h.reduce((R,x,A)=>R+x*N[A]*N[A],0),v=0;if(m.forEach((R,x)=>R.forEach((A,S)=>v+=A*w[x]*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 y=v/Math.sqrt(b)/Math.sqrt(E),T=f===0?Math.sqrt(p-1)*y:W(Math.sqrt(p-W(1,-f))*y,f/2),M=n==="less"?oe(T):n==="greater"?oe(T,{lowerTail:!1}):2*oe(-Math.abs(T));return J("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:T,statisticName:"Z",pValue:M,warnings:[]})}var wf=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function bn(e){let t=[0,0],n=0;for(let r of e)t=he(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function ed(e,t,n){return lt(t)&&z(t,wf,"chisqTest"),z(n,wf,"chisqTest"),Array.isArray(t)?J("chisqTest",td(e,t,n??{})):J("chisqTest",nd(e,t??{}))}var Nf="Chi-squared approximation may be incorrect";function td(e,t,n){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let r=u=>u==null;for(let u of[...e,...t])if(!r(u)&&typeof u!="string")throw new TypeError("factor values must be strings (or null for missing values)");let o=[],s=[];e.forEach((u,h)=>{let p=t[h];!r(u)&&!r(p)&&(o.push(u),s.push(p))});let i=[...new Set(o)].sort(Ln),a=[...new Set(s)].sort(Ln);if(i.length<2||a.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let c=new Map(i.map((u,h)=>[u,h])),l=new Map(a.map((u,h)=>[u,h])),f=i.map(()=>a.map(()=>0));return o.forEach((u,h)=>f[c.get(u)][l.get(s[h])]++),{...Rf(f,n),rowLevels:i,colLevels:a}}function nd(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?ci(r[0],t):o===1?ci(r.map(s=>s[0]),t):Rf(r,t)}return ci(Array.from(e),t)}function Mf(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 yf(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 Ef(e,t){return Number.isNaN(e)?NaN:vt(e,t/2,2,!1,!1)}function Tf(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 ci(e,t){if(t.nMinus1??!1)throw new RangeError("'nMinus1' requires a contingency table with at least two rows and columns");let n=e.length;if(e.some(d=>!(d>=0&&d<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=bn(e);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=t.p===void 0?e.map(()=>1/n):Array.from(t.p);if(o.length!==n)throw new TypeError("'x' and 'p' must have the same number of elements");if(o.some(Number.isNaN))throw new RangeError("p contains missing values");if(o.some(d=>d<0))throw new RangeError("probabilities must be non-negative");if(o.some(d=>d===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let d=bn(o),g=d<1/0?0:Math.ceil(Math.log2(n)),w=g===0?o:o.map(b=>W(b,-g)),N=g===0?d:bn(w);o=w.map(b=>b/N)}else if(Math.abs(bn(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r<1/0?0:2*Math.ceil(Math.log2(n)/2),i=s===0?e:e.map(d=>W(d,-s)),a=s===0?r:bn(i),c=o.map(d=>a*d),l=s===0?c:c.map(d=>W(d,s));Tf(l);let f=o.map((d,g)=>c[g]*(1-d)),m=W(bn(i.map((d,g)=>Dt(d-c[g],c[g]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let d=Mf(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=jo(d),h=NaN,p=yf(rd(r,o,l,d),m,d)}else p=Ef(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((d,g)=>W((d-c[g])/Math.sqrt(c[g]),s/2)),stdres:i.map((d,g)=>W((d-c[g])/Math.sqrt(f[g]),s/2)),ftResiduals:Zo(e,l),warnings:Number.isFinite(h)&&l.some(d=>d<5)?[Nf]:[]}}function rd(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=zl(o,t);if(r*e===0)return new Float64Array(0);let i=new Float64Array(r),a=new Float64Array(o);for(let c=0;c<r;c++){a.fill(0);for(let f=0;f<e;f++)a[s()]++;let l=0;for(let f=0;f<o;f++)l+=(a[f]-n[f])**2/n[f];i[c]=l}return i}function od(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>Fs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let a=Ps(i),c=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){Ds(e,t,a,c);let m=0;for(let u=0;u<s;u++)for(let h=0;h<o;h++){let p=n[h][u];m+=(c[h+u*o]-p)**2/p}l[f]=m}return l}function Rf(e,t){let n=t.nMinus1??!1;if(typeof n!="boolean")throw new TypeError("'nMinus1' must be a boolean");if(n&&t.correct===!0)throw new RangeError("'nMinus1' uses no continuity correction: leave 'correct' out or set it to false");if(n&&(t.simulatePValue??!1))throw new RangeError("'nMinus1' cannot be combined with 'simulatePValue'");let r=!n&&(t.correct??!0),o=e.length,s=e[0].length,i=e.flat();if(i.some(b=>!(b>=0&&b<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let a=bn(i);if(!(a>0))throw new RangeError("at least one entry of 'x' must be positive");if(n&&!(a>1))throw new RangeError("'nMinus1' needs a table with a total above 1");let c=e.map(b=>bn(b)),l=e[0].map((b,E)=>bn(e.map(v=>v[E]))),f=c.map(b=>l.map(E=>Cs(b,E,a)));Tf(f.flat());let m=e.map((b,E)=>b.map((v,y)=>v-f[E][y])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),d,g=(t.simulatePValue??!1)&&c.every(b=>b>0)&&l.every(b=>b>0),w=!g&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!g&&N.push("cannot compute simulated p-value with zero marginals"),g){if(e.some(v=>v.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let b=Mf(t);u+=jo(b),h=bn(m.flatMap((v,y)=>v.map((T,M)=>Dt(T,f[y][M])))),p=NaN;let E=od(c.map(v=>Math.trunc(v)),l.map(v=>Math.trunc(v)),f,b);d=yf(E,h,b)}else if(w>0)u+=" with Yates' continuity correction",h=bn(m.flatMap((b,E)=>b.map((v,y)=>Dt(Math.abs(v)-w,f[E][y]))));else if(h=bn(m.flatMap((b,E)=>b.map((v,y)=>Dt(v,f[E][y])))),n){u="Pearson's 'N-1' Chi-squared test";let b=h*(a-1);h=Number.isFinite(b)?b/a:h*((a-1)/a)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:d??Ef(h,p),observed:e,expected:f,residuals:m.map((b,E)=>b.map((v,y)=>v/Math.sqrt(f[E][y]))),stdres:sd(e,a,c,l,f,m),ftResiduals:Zo(e,f),warnings:Number.isFinite(p)&&f.some(b=>b.some(E=>E<5))?[...N,Nf]:N}}function sd(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,d)=>p.map((g,w)=>g/Math.sqrt(o[d][w]*(1-n[d]/t)*(1-r[w]/t))));let i=e.map(p=>bf(p)),a=e[0].map((p,d)=>bf(e.map(g=>g[d]))),c=id(i),l=vf(i),f=vf(a),m=l.map(p=>ut(p,c)),u=a.map(p=>ut(p,c)),h=f.map(p=>ut(p,c));return e.map((p,d)=>p.map((g,w)=>{let N=_e(i[d],u[w]),b=Le([g,0],N),E=b[0]+b[1],v=_e(_e(N,m[d]),h[w]),y=v[0]+v[1];if(y>=ad||E===0)return E/Math.sqrt(y);let T=N[0]+N[1],M=m[d][0]+m[d][1],R=h[w][0]+h[w][1];return E/Math.sqrt(T)/Math.sqrt(M)/Math.sqrt(R)}))}var ad=2**-1022;function bf(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function id(e){let t=[0,0];for(let n of e)t=he(t,n);return t}function vf(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=he(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(he(o,n[s+1])),o=he(o,e[s]);return r}var cd=Math.pow(Number.EPSILON,.25);function Wt(e,t,n,r={}){let o={tol:r.tol??cd,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),qr(e,t,n,o).root}function dr(e,t){let n=c=>e(Math.exp(c)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let a=r<s?Wt(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Wt(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(a)}var ld=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function ts(e,t={}){z(t,ld,"fisherTest");let n=e.length,r=n===0?0:e[0].length;if(e.some(f=>f.length!==r))throw new TypeError("all rows of the matrix must have the same length");if(n<2||r<2)throw new TypeError("'x' must have at least 2 rows and columns");let o=e.map(f=>Array.from(f));if(o.flat().some(f=>Number.isNaN(f)||f<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(f=>f===1/0))throw new RangeError("'x' has entries too large to be integer");let s=o.map(f=>f.map(Rl));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],a=fd(o,s);a!==null&&i.push(`'x' has been rounded to integer: ${a}`);let c="Fisher's Exact Test for Count Data";if(t.midp!==void 0&&typeof t.midp!="boolean")throw new TypeError("'midp' must be true or false");if((t.midp??!1)&&!(n===2&&r===2))throw new RangeError("the mid-p version ('midp') needs a 2 x 2 table");if(n===2&&r===2)return J("fisherTest",{...pd(s,t,c),warnings:i});let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let f=t.B??2e3;if(!(Number.isInteger(f)&&f>=1&&f<2**31))throw new RangeError("'B' must be a positive integer");return J("fisherTest",{method:c+jo(f),alternative:l,pValue:md(s,f),warnings:i})}return J("fisherTest",{method:c,alternative:l,pValue:dd(s),warnings:i})}function fd(e,t){let n=Math.sqrt(Number.EPSILON),r=[],o=[];for(let l=0;l<e[0].length;l++)for(let f=0;f<e.length;f++)e[f][l]!==t[f][l]&&(r.push(e[f][l]),o.push(t[f][l]));let s=r.length;if(s===0)return null;let i=0;for(let l of r)i+=Math.abs(l)/s;let a="relative";Number.isFinite(i)&&i>n||(i=1,a="absolute");let c=0;for(let l=0;l<s;l++)c+=Math.abs(r[l]-o[l])/(s*i);return Number.isNaN(c)||c>n?`Mean ${a} difference: ${Kl(c)}`:null}function md(e,t){let n=e.filter(g=>g.some(w=>w>0)),r=e[0].map((g,w)=>w).filter(g=>e.some(w=>w[g]>0));if(n.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(r.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let o=n.map(g=>r.map(w=>g[w])),s=o.map(g=>g.reduce((w,N)=>w+N,0)),i=o[0].map((g,w)=>o.reduce((N,b)=>N+b[w],0)),a=s.length,c=i.length,l=[0,0];for(let g of o.flat())l=he(l,[-Ls(g),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((g,w)=>g+w,0);if(m>Fs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Ps(m),h=new Int32Array(a*c),p=0,d=Math.trunc(t);for(let g=0;g<d;g++){Ds(s,i,u,h);let w=0;for(let N=0;N<h.length;N++)w-=u[h[N]];w<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(d+1)))}var xf=1+1e-7,ud=16,hd=2**24;function Jo(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=he(r,[e[o],0]));return r[0]}var co=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let i=this.lo;i<=this.hi;i++){let a=i-this.lo;a%ud===0||!(s>0&&s<1/0)?(o=En(i,t,n,r,!0),s=1,this.logCentral[a]=o):this.logCentral[a]=o+Math.log(s),s*=(t-i)*(r-i)/((i+1)*(n-r+i+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),s=-1/0;for(let a=0;a<n;a++){let c=this.logCentral[a]+(this.lo+a)*r;o[a]=c,c>s&&(s=c)}for(let a=0;a<n;a++){let c=o[a]-s;o[a]=c<-746?0:Math.exp(c)}let i=Jo(o,0,n-1);for(let a=0;a<n;a++)o[a]!==0&&(o[a]=o[a]/i);return o}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)n[o]!==0&&(r=he(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?cn(t-1,this.m,this.n,this.k,!1,!1):cn(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return(r?t<=i:t>=i)?1:0}let o=this.density(n),s=t-this.lo;return r?Jo(o,s,o.length-1):Jo(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*En(t,this.m,this.n,this.k,!1);return(r?cn(t,this.m,this.n,this.k,!1,!1):cn(t-1,this.m,this.n,this.k,!0,!1))+i}if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return t===i?.5:(r?t<i:t>i)?1:0}let o=this.density(n),s=t-this.lo;return(r?Jo(o,s+1,o.length-1):Jo(o,0,s-1))+.5*o[s]}};function fi(e,t,n){let r=e.density(n),o=r[t-e.lo]*xf,s=[0,0];for(let i of r)i<=o&&(s=he(s,[i,0]));return s[0]}function Sf(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=d=>En(d,t,n,r,!0),a=i(e)+Math.log(xf),c=Math.min(s,Math.max(o,Math.floor((r+1)*(t+1)/(t+n+2))));for(;c<s&&i(c+1)>i(c);)c++;for(;c>o&&i(c-1)>i(c);)c--;let l=(d,g)=>{let w=d,N=g+1;for(;N-w>1;){let b=w+Math.floor((N-w)/2);i(b)<=a?w=b:N=b}return w},f=(d,g)=>{let w=d-1,N=g+1;for(;N-w>1;){let b=w+Math.floor((N-w)/2);i(b)<=a?N=b:w=b}return N},m,u;e<=c?(m=l(e,c),u=f(c+1,s)):(u=f(c,e),m=c-1<o||i(o)>a?o-1:l(o,c-1));let h=m<o?0:cn(m,t,n,r,!0,!1),p=u>s?0:cn(u-1,t,n,r,!1,!1);return h+p}function es(e,t){return dr(e,t)}function pd(e,t,n){let r=Ge(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[i,a],[c,l]]=e,f=i,m=Math.min(i,l)+Math.min(a,c)+1;if(m>hd)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new co(i+c,a+l,i+a),{lo:h,hi:p}=u,d=t.midp??!1,g;d?(r==="less"?g=u.midTail(f,o,!1):r==="greater"?g=u.midTail(f,o,!0):g=Math.min(1,2*Math.min(u.midTail(f,o,!1),u.midTail(f,o,!0))),n=r==="two.sided"?"Central Fisher's Exact Test (mid-p version)":"One-sided Fisher's Exact Test (mid-p version)"):r==="less"?g=u.tail(f,o,!1):r==="greater"?g=u.tail(f,o,!0):o===0?g=f===h?1:0:o===1/0?g=f===p?1:0:g=fi(u,f,o);let w;if(f===h)w=0;else if(f===p)w=1/0;else{let y=u.mean(1);w=y===f?1:es(T=>u.mean(T)-f,y>f)}let N={method:n,alternative:r,pValue:g,estimate:w,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let b=y=>{if(f===p)return 1/0;if(d){if(f===h&&y>=.5)return 0;let M=u.midTail(f,1,!1);return M===y?1:es(R=>u.midTail(f,R,!1)-y,M<y)}let T=u.tail(f,1,!1);return T===y?1:es(M=>u.tail(f,M,!1)-y,T<y)},E=y=>{if(f===h)return 0;if(d){if(f===p&&y>=.5)return 1/0;let M=u.midTail(f,1,!0);return M===y?1:es(R=>u.midTail(f,R,!0)-y,M>y)}let T=u.tail(f,1,!0);return T===y?1:es(M=>u.tail(f,M,!0)-y,T>y)},v;if(r==="less")v=[0,b(1-s)];else if(r==="greater")v=[E(1-s),1/0];else{let y=(1-s)/2;v=[E(y),b(y)]}return{...N,confInt:v,confLevel:s}}var Zs=[[0,0]];function On(e){for(let t=Zs.length;t<=e;t++)Zs.push(he(Zs[t-1],Nl([t,0])));return Zs[e]}function Js(e,t,n,r){let o=e+n,s=o-e,i=e-(o-s)+(n-s)+t+r,a=o+i;return[a,i-(a-o)]}var li=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let c=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=c,this.lo*=c,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),s=this.hi+o,i=s-this.hi,a=this.hi-(s-i)+(o-i)+this.lo;this.hi=s+a,this.lo=a-(this.hi-s)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function dd(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(v=>v[E]>0));if(t=t.map(b=>b.filter((E,v)=>n[v])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((b,E)=>t.map(v=>v[E])));let r=t.length,o=t.map(b=>b.reduce((E,v)=>E+v,0)),s=t[0].map((b,E)=>t.reduce((v,y)=>v+y[E],0)).sort((b,E)=>E-b),i=s.length,a=o.reduce((b,E)=>b+E,0),c=vl(On(a));for(let b of o)c=he(c,On(b));for(let b of s)c=he(c,On(b));let l=[0,0];for(let b of t.flat())l=Le(l,On(b));let f=he(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),d=(b,E)=>{let v=E.join(","),y=p[b].get(v);if(y!==void 0)return y;let T=he(c,On(E.reduce((I,C)=>I+C,0)));for(let I of E)T=Le(T,On(I));for(let I=b;I<i;I++)T=Le(T,On(s[I]));let M={R:E,KZ:T,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(v,M),b===i-1){let I=[0,0];for(let C of E)I=Le(I,On(C));return M.max=M.min=I[0],M.edges.push({to:M,hi:I[0],lo:I[1],mult:1}),M}let R=new Map,x=new Array(r+1).fill(0);for(let I=r-1;I>=0;I--)x[I]=x[I+1]+E[I];let A=new Array(r).fill(0),S=(I,C,_)=>{if(I===r-1){A[I]=C;let D=Le(_,On(C)),k=E.map((V,P)=>V-A[P]).sort((V,P)=>V-P),L=d(b+1,k),q=`${k.join(",")}|${D[0]}`,$=R.get(q);$===void 0?R.set(q,{to:L,hi:D[0],lo:D[1],mult:1}):$.mult++;return}let F=Math.max(0,C-x[I+1]),O=Math.min(E[I],C);for(let D=F;D<=O;D++)A[I]=D,S(I+1,C-D,Le(_,On(D)))};if(S(0,s[b],[0,0]),M.edges=[...R.values()],M.max=kt(M.edges.map(I=>I.hi+I.to.max)),M.min=ln(M.edges.map(I=>I.hi+I.to.min)),b===i-2){let I=M.edges.map(L=>{let q=L.to.edges[0];return{v:he([L.hi,L.lo],[q.hi,q.lo]),mult:L.mult}});I.sort((L,q)=>L.v[0]-q.v[0]||L.v[1]-q.v[1]);let C=I.length,_=I[C-1].v[0],F=new Float64Array(C),O=new Float64Array(C),D=new Float64Array(C),k=[0,0];I.forEach(({v:L,mult:q},$)=>{F[$]=L[0],O[$]=L[1];let V=Le(L,[_,0]);k=he(k,[q*Math.exp(V[0])*(1+V[1]),0]),D[$]=k[0]}),M.dist={hi:F,lo:O,cum:D,vmax:_,kv:he(c,[_,0])}}return M},g=d(0,[...o].sort((b,E)=>b-E)),w=new li,N=(b,E,v,y)=>{if(E+b.max<=u-m){let[S,I]=Js(b.KZ[0],b.KZ[1],E,v);return w.add(S,I,y),!0}if(E+b.min>u+m)return!0;let T=b.dist;if(T===void 0)return!1;let[M,R]=Js(u,h,-E,-v),x=0,A=T.hi.length;for(;x<A;){let S=x+A>>>1,I=T.hi[S];I<M||I===M&&T.lo[S]<=R?x=S+1:A=S}if(x>0){let[S,I]=Js(T.kv[0],T.kv[1],E,v);w.add(S,I,y*T.cum[x-1])}return!0};N(g,0,0,1)||(g.pastHi.push(0),g.pastLo.push(0),g.count.push(1));for(let b=0;b<i-2;b++)for(let E of p[b].values()){let{pastHi:v,pastLo:y,count:T}=E;if(T.length!==0){for(let{to:M,hi:R,lo:x,mult:A}of E.edges)for(let S=0;S<T.length;S++){let[I,C]=Js(v[S],y[S],R,x),_=T[S]*A;if(N(M,I,C,_))continue;let F=M.index.get(I);F===void 0?(M.index.set(I,M.count.length),M.pastHi.push(I),M.pastLo.push(C),M.count.push(_)):M.count[F]+=_}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,w.value())}function ea(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function ui(e){return[...e].sort((t,n)=>t-n).join(",")}function mi(e,t,n){let r=n.length,o=new Float64Array(r),s=new Float64Array(r),i=0;for(let a=0;a<r;a++)o[a]=i+=n[a];i=0;for(let a=r-1;a>=0;a--)s[a]=i+=n[a];return{f:e,offset:t,lower:o,upper:s}}function hi(e){let[,t]=ea(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function pi(e){let[t,n]=ea(e),r=0;for(let i of n)r+=i;let o=new Float64Array(r+1);o[0]=1;let s=0;for(let i of n){s+=i;for(let a=s;a>=i;a--)o[a]=.5*(o[a]+o[a-i]);for(let a=i-1;a>=0;a--)o[a]=.5*o[a]}return mi(t,0,o)}function di(e,t){let[,n]=ea(e),r=n.length,o=Math.min(t,r-t),s=Af(n),i=0;for(let a=1;a<=r;a++)for(let c=Math.max(1,o-(r-a));c<=Math.min(a,o);c++)i+=s[a]-s[a-c]-s[c]+1;return{steps:i,size:s[r]-s[r-o]-s[o]+1}}function Af(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function gi(e,t){let[n,r]=ea(e),o=r.length,s=Math.min(t,o-t),i=Af(r),a=[];for(let h=0;h<=s;h++)a.push(new Float64Array(i[o]-i[o-h]-i[h]+1));a[0][0]=1;for(let h=1;h<=o;h++){let p=r[h-1];for(let d=Math.min(h,s);d>=Math.max(1,s-(o-h));d--){let g=a[d],w=a[d-1],N=d/h,b=(h-d)/h,E=i[d],v=i[h]-i[h-d],y=i[d-1]+p,T=Math.max(E,y);for(let M=v;M>=T;M--)g[M-E]=b*g[M-E]+N*w[M-y];for(let M=T-1;M>=E;M--)g[M-E]=b*g[M-E]}}let c=a[s],l=t*(t+1)/2;if(s===t)return mi(n,i[s]/n-l,c);let f=c.length,m=new Float64Array(f);for(let h=0;h<f;h++)m[h]=c[f-1-h];let u=i[o]-i[o-s];return mi(n,(i[o]-u)/n-l,m)}function gr(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 Lr(e,t){let n=gr(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 kf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function ns(e,t,n={}){if(typeof e!="string"&<(t)&&z(t,kf,"wilcoxTest"),z(n,kf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let d=$s(e,t,!0);return d.kind==="one"?ns(d.x,n):ns(d.x,d.y,d.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(n.alternative),s=n.mu??0,i=n.paired??!1,a=n.exact??null,c=wd(n.correct??!0),l=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(l&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(i&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[d,g]=cr(Array.from(e),Array.from(r));u=Ft(d.map((w,N)=>w-g[N]))}else u=Ft(Array.from(e)),r!==void 0&&(h=Ft(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:c,confInt:l,confLevel:f,tolRoot:m,warnings:new pt};return J("wilcoxTest",h===void 0?yd(u,a,i,p):qd(u,h,a,p))}function wd(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 ta(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var _f=10*Number.EPSILON;function Cf(e,t,n=0){let r=c=>Math.min(1,Math.max(c,0)),o=()=>n===0?oe(e):r(oe(e)-n*Zt(e)),s=()=>n===0?oe(e,{lowerTail:!1}):r(oe(e,{lowerTail:!1})+n*Zt(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let a=s();return a>=1/64?2*(1-i):2*a}function bd(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function vd(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 Lf(e,o,s,n)}function Nd(e,t,n,r){let o=t+n,s=t*n,i=-(o*(o+1)-s)/(20*s*(o+1)),a=(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 Lf(e,i,a,r)}function Lf(e,t,n,r){let[o,s,i]=bd(e),a=t*o;return r>=2&&(a+=n*s),r>=3&&(a+=t*t/2*i),a}function vi(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:ct([e[n-1],e[n]])}var Ni=(e,t)=>e-t,wi=5e9,Md=40;function bi(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function na(e,t){if(e.size>16777216||e.steps>wi)throw bi(t);return e.steps}function Of(e){let t=e.length,n=Mt(e.map(Math.abs)),r=0;for(let i=0;i<t;i++)e[i]>0&&(r+=n[i]);let o=0;for(let i of Mn(n))o+=i*i*i-i;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,s]}function yd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(v=>v-s);i.some((v,y)=>!Number.isFinite(v)&&Number.isFinite(e[y]))&&(i=e.map(v=>v/2-s/2));let c=i.some(v=>v===0),l=t??i.length<50,f=(v,y,T)=>({method:v,alternative:o,statistic:y,statisticName:"V",pValue:T,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let v=Mt(i.map(Math.abs)),y=Mn(v).length>0,T=0;for(let I=0;I<i.length;I++)i[I]>0&&(T+=v[I]);let M="Wilcoxon signed rank exact test";if(!y&&!c){let I=i.length,C=I*(I+1)/2,_;if(o==="less")_=no(T,I,!0,!1);else if(o==="greater")_=no(T-1,I,!1,!1);else{let O=T>C/2?no(T-1,I,!1,!1):no(T,I,!0,!1);_=Math.min(1,2*O)}let F=f(M,T,_);return r.confInt?{...F,...Ed(e,r)}:F}let R=v.filter((I,C)=>i[C]!==0);na(hi(R),"distribution");let x=pi(R),A;if(o==="less")A=gr(x,T);else if(o==="greater")A=Lr(x,T-.25);else{let I=0;for(let C of R)I+=C/2;A=Math.min(1,2*(T>I?Lr(x,T-.25):gr(x,T)))}let S=f(M,T,A);return r.confInt?{...S,...Rd(e,r)}:S}let m=c?i.filter(v=>v!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,d]=Of(m),w=Mn(Mt(m.map(Math.abs))).length>0||c?Math.min(r.correct,0):r.correct,N=(p-ta(p,o,w>=0))/d,b=Cf(N,o,w>=1?vd(N,u,w):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,b);return r.confInt?{...E,...xd(e,r)}:E}function Mi(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function qf(e,t,n){let r={confInt:Mi(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Ff(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Pf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=fr(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(Ni)}function Df(e,t,n,r,o){let{alternative:s,confLevel:i}=r,a=e.length,c=s==="two.sided"?(1-i)/2:1-i,l=n(c);t(l)<=c+_f&&l++;let f=vi(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:Mi(s,e[l-1],e[a-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function Ed(e,t){let n=e.length;return Df(Pf(e),r=>no(r,n,!0,!1),r=>Il(r,n,!0,!1),t,"(pseudo)median")}function Vf(e){let t=e.length,n=Float64Array.from(e).sort(),r=new Array(t);for(let s=0;s<t;s++){let i=e[s],a=0,c=t;for(;a<c;){let f=a+c>>>1;n[f]<i?a=f+1:c=f}let l=a;for(c=t;l<c;){let f=l+c>>>1;n[f]<=i?l=f+1:c=f}r[s]=(a+1+l)/2}let o="";for(let s=0;s<t;){let i=s;for(;i+1<t&&n[i+1]===n[s];)i++;i>s&&(o+=`${s}:${i-s+1},`),s=i+1}return{ranks:r,key:o}}function $f(e,t,n,r,o,s,i,a,c,l){let{alternative:f,confLevel:m}=c,u=e.length,p=(f==="two.sided"?(1-m)/2:1-m)+_f,d=f!=="less",g=f!=="greater",w=_=>fr(e[_-1],e[_]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+re(Math.min(p,1))*i.sd+.1*i.sd+3))),b=8,E=_=>_===0?e[0]-1:w(_),v=_=>_===0?e[u-1]+1:w(u-_),y=[new Map,new Map];{let _=new Map,F=0,O=Md*a;for(let[D,k]of[[0,E],[1,v]])if(!(D===0?!d:!g))for(let L=0;L<=N;L++){if(F+=O,F>wi)throw bi("interval");let q;try{q=t(k(L))}catch{break}if(y[D].set(L,[q.stat,q.key]),_.has(q.key)){_.delete(q.key),_.set(q.key,!0);continue}if(F+=r(q.scores),F>wi)throw bi("interval");_.set(q.key,!0),_.size>b&&_.delete(_.keys().next().value)}}let T=new Map,M=(_,F)=>{let O=_===0?E(F):v(F),D=y[_].get(F),k;if(D===void 0){let V=t(O);D=[V.stat,V.key],k=V.scores}let[L,q]=D,$=T.get(q);return $!==void 0?T.delete(q):($=n(k??t(O).scores),T.size>=b&&T.delete(T.keys().next().value)),T.set(q,$),[L,$]},R=_=>{let[F,O]=M(0,_);return o(O,F)},x=_=>{let[F,O]=M(1,_);return s(O,F)},A=-1/0,S=0;if(d){let _=R(0);if(!(_>p)){let F=_,O=1;for(;O<=u-1;O++){let D=R(O);if(D>p)break;F=D}A=e[O-1],S=F}}let I=1/0,C=0;if(g){let _=x(0);if(!(_>p)){let F=_,O=u-1;for(;O>=1;O--){let D=x(u-O);if(D>p)break;F=D}I=e[O],C=F}}return{confInt:[A,I],confLevel:1-(S+C),estimate:vi(e),estimateNames:l}}function Td(e,t){let n=e.map(r=>r-t);if(n.some(r=>Number.isNaN(r)))throw new Error("cannot compute the exact confidence interval: the shifted data have undefined values (infinite observations)");return Number.isFinite(t)&&n.some((r,o)=>!Number.isFinite(r)&&Number.isFinite(e[o]))&&(n=e.map(r=>r/2-t/2)),n}function Rd(e,t){let n=e.length,r=s=>{let i=Td(e,s),{ranks:a,key:c}=Vf(i.map(Math.abs)),l=0;for(let f=0;f<n;f++)i[f]>0&&(l+=a[f]);return{stat:l,scores:a,key:c}},o=n*(n+1)/2;return $f(Pf(e),r,pi,s=>na(hi(s),"interval"),(s,i)=>Lr(s,i-.25),gr,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function xd(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return qf(n,NaN);let a=ln(e),c=kt(e);if(!Number.isFinite(a)||!Number.isFinite(c)||i===1||a===c)return Number.isFinite(a)&&a===c&&t.warnings.add("cannot compute confidence interval when all observations are zero or tied"),qf(n,fr(a,c),"midrange");let l=(b,E)=>{let[,v,y]=Of(e.map(T=>T-b).filter(T=>T!==0));return(v-ta(v,n,E))/y},f=l(a,s),m=l(c,s),u=b=>-re(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>re(b/2)):!(f<u(b/2))&&!(m>re(b/2)),p=1-r,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;d&&t.warnings.add("requested conf.level not achievable"),Ff(o);let g=b=>Wt(E=>l(E,s)-b,a,c,{tol:o,fLower:f-b,fUpper:m-b}),w;if(p>=1){let b=vi([...e].sort(Ni));w=Mi(n,b,b)}else n==="greater"?w=[g(u(p)),1/0]:n==="less"?w=[-1/0,g(re(p))]:w=[g(u(p/2)),g(re(p/2))];let N=Wt(b=>l(b,!1),a,c,{tol:o,fLower:l(a,!1),fUpper:l(c,!1)});return{confInt:w,confLevel:d?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function zf(e,t){let n=e.length,r=n-t,o=0;for(let c=0;c<t;c++)o+=e[c];let s=o-t*(t+1)/2,i=0;for(let c of Mn(e))i+=c*c*c-c;let a=Math.sqrt(t*r/12*(n+1-i/(n*(n-1))));return[s,s-t*r/2,a,i>0]}function Sd(e,t){let[n,r,o]=zf(Mt([...e,...t]),e.length);return[n,r,o]}function Ad(e,t){let n=e.length,r=n+t.length,o=(l,f)=>{let m=l===0||!Number.isFinite(l)||!Number.isFinite(f)?f:f/2;return l<m?-1:l>m?1:0},s=(l,f)=>l<n&&f<n?e[l]<e[f]?-1:e[l]>e[f]?1:0:l>=n&&f>=n?t[l-n]<t[f-n]?-1:t[l-n]>t[f-n]?1:0:l<n?o(e[l],t[f-n]):-o(e[f],t[l-n]),i=Array.from({length:r},(l,f)=>f);i.sort((l,f)=>s(l,f)||l-f);let a=new Array(r),c=0;for(;c<r;){let l=c;for(;l+1<r&&s(i[l+1],i[c])===0;)l++;let f=(c+l+2)/2;for(let m=c;m<=l;m++)a[i[m]]=f;c=l+1}return a}function kd(e,t,n){return Kf(e,t,n).ranks}function Kf(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((c,l)=>[c,l]).filter(([c])=>!Number.isNaN(c)),s=Mt([...o.map(([c])=>c),...t]),i=new Array(e.length+t.length);o.forEach(([,c],l)=>i[c]=s[l]),t.forEach((c,l)=>i[e.length+l]=s[o.length+l]);let a=o.length+t.length;return r.forEach((c,l)=>{Number.isNaN(c)&&(i[l]=++a)}),{ranks:i,key:ui(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=Ad(e.map(s=>s/2-n/2),t);return{ranks:o,key:ui(o)}}return Vf([...r,...t])}function qd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,a=t.length,c=n??(i<50&&a<50),l=kd(e,t,s),[f,m,u,h]=zf(l,i),p,d;if(c)if(d="Wilcoxon rank sum exact test",!h)p=If(()=>o==="less"?ro(f,i,a,!0,!1):o==="greater"?ro(f-1,i,a,!1,!1):Math.min(2*ro(f,i,a,!0,!1),2*ro(f-1,i,a,!1,!1),1));else{na(di(l,i),"distribution");let N=gi(l,i);o==="less"?p=gr(N,f):o==="greater"?p=Lr(N,f-.25):p=Math.min(2*gr(N,f),2*Lr(N,f-.25),1)}else{d=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,b=l.every(v=>v===l[0]),E=(m-ta(m,o,N>=0))/u;p=b?1:Cf(E,o,N>=1?Nd(E,i,a,N):0)}let g={method:d,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return g;let w=c?h?_d(e,t,r):If(()=>Id(e,t,r)):Cd(e,t,r);return{...g,...w}}function If(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Yf(e,t,n){let r=[];for(let o of e)for(let s of t){let i=o-s;n&&Number.isNaN(i)||r.push(i)}return r.sort(Ni)}function Id(e,t,n){let r=e.length,o=t.length;return Df(Yf(e,t,!1),s=>ro(s,r,o,!0,!1),s=>Cl(s,r,o,!0,!1),n,"difference in location")}function _d(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:a,key:c}=Kf(e,t,i),l=0;for(let f=0;f<r;f++)l+=a[f];return{stat:l-r*(r+1)/2,scores:a,key:c}};return $f(Yf(e,t,!0),s,i=>gi(i,r),i=>na(di(i,r),"interval"),(i,a)=>Lr(i,a-.25),gr,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function Cd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,a=ln(e)-kt(t),c=kt(e)-ln(t);if(!Number.isFinite(a)||!Number.isFinite(c))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(h,p)=>{let[,d,g]=Sd(e.map(w=>w-h),t);return(d-ta(d,r,p))/g},f=(h,p)=>{let d=l(a,p)-h,g=l(c,p)-h;if(Number.isNaN(d)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return d<=0?a:g>=0?c:(Ff(s),Wt(w=>l(w,p)-h,a,c,{tol:s,fLower:d,fUpper:g}))},m=1-o,u;return r==="greater"?u=[f(-re(m),i),1/0]:r==="less"?u=[-1/0,f(re(m),i)]:u=[f(-re(m/2),i),f(re(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Ld=["method","n"],Wf=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Od(e){if(Wf.includes(e))return e;let t=Wf.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function lo(e,t={}){z(t,Ld,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Od(n.method??"holm"),o=Array.from(e),s=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||s.push(m);let i=s.length,a=n.n??i;if(!(a>=i))throw new RangeError("n must be at least the number of non-NaN p-values");if(a===1/0)throw new RangeError("n must be finite");if(a<=1||i===0)return o;let c=s.map(m=>o[m]),l=Bf(c,r,a),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function ra(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 Bf(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=ra(e,!1),i=-1/0;return s.forEach((a,c)=>{i=Math.max(i,(n-c)*e[a]),o[a]=Math.min(1,i)}),o}case"hochberg":{let s=ra(e,!0),i=1/0;return s.forEach((a,c)=>{let l=r-c;i=Math.min(i,(n-l+1)*e[a]),o[a]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=Un(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)gn(m,1/h,u);s=Gn(m)}else{let h=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*h)+1/(120*h*h)}}let i=s*n,a=Number.isFinite(i)?1:1024,c=Number.isFinite(i)?i:s*(n/1024),l=ra(e,!0),f=1/0;return l.forEach((m,u)=>{let h=r-u;f=Math.min(f,c/h*e[m]*a),o[m]=Math.min(1,f)}),o}case"hommel":return n===2?Bf(e,"hochberg",n):Fd(e,n)}}var jf=2**16;function Fd(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>jf)throw new RangeError(`n must be at most ${jf} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=ra(r,!1),s=o.map(c=>r[c]),i=s.slice();for(let c=t;c>=2;c--){let l=1/0;for(let f=1;f<=c;f++)l=Math.min(l,c*s[t-c+f-1]/f);for(let f=t-c;f<t;f++)i[f]=Math.max(i[f],l);for(let f=0;f<t-c;f++)i[f]=Math.max(i[f],Math.min(c*s[f],l))}let a=new Array(n);return o.forEach((c,l)=>{c<n&&(a[c]=i[l])}),a}var Uf=["p","alternative","confLevel"],Pd=1+1e-7;function yi(e,t,n){let r=e-1;return e===0||e-r===1&&zt(t,-r)[1]===0?Bn(r,t,n,!1,!1):Bn(e,t,n,!1,!1)+dn(e,t,n,!1)}function Dd(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=dn(e,t,n,!1)*Pd,s=c=>dn(c,t,n,!1)<=o;if(e<r){let c=Ss(s,Math.ceil(r),t),l=c>t?0:yi(c,t,n);return Math.min(1,Bn(e,t,n,!0,!1)+l)}let i=As(s,0,Math.floor(r)),a=i<0?0:Bn(i,t,n,!0,!1);return Math.min(1,yi(e,t,n)+a)}function Vd(e,t,n={}){z(t,Uf,"binomTest"),z(n,Uf,"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 Ei(r,o,n)}function Ei(e,t,n={}){let r=n.p??.5;if(!(r>=0&&r<=1))throw new RangeError("'p' must be a single number between 0 and 1");let o=Ge(n.alternative),s=n.confLevel??.95;Ct(s);let i;o==="less"?i=Bn(e,t,r,!0,!1):o==="greater"?i=yi(e,t,r):i=Dd(e,t,r);let a=f=>e===0?0:sr(f,e,t-e+1,!0,!1),c=f=>e===t?1:f<1e-8?sr(f,e+1,t-e,!1,!1):sr(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,c(1-s)];else if(o==="greater")l=[a(1-s),1];else{let f=(1-s)/2;l=[a(f),c(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:i,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var $d=["T","r","alternative","confLevel"],zd=1+1e-7;function Kd(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=ks(e,t,!1)*zd,r=i=>ks(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=Ss(r,Math.max(e+1,o),1/0);return Math.min(1,eo(e,t,!0,!1)+Ti(i,t))}let s=As(r,0,Math.min(e-1,o));return Math.min(1,eo(s,t,!0,!1)+Ti(e,t))}var rs=2**-1022;function Ti(e,t){return e-(e-1)===1?eo(e-1,t,!1,!1):eo(e,t,!1,!1)+ks(e,t,!1)}function Or(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function Yd(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=rs&&(s!==0||e===0||t===0))return s;let i=e*t,a=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(i)&&Number.isFinite(a))return i/a;let[c,l]=Or(e),[f,m]=Or(t),[u,h]=Or(n),[p,d]=Or(r),g=f*p,w=h-m-d,N=w>=0?u/(u+W(g,-w)):u/(W(u,w)+g);return W(c*f*N,l+m+Math.min(w,0))}function Wd(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=rs&&(o===0||o>=rs)?i:e===0?0:e/t*(r/n)}function jd(e,t={}){z(t,$d,"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,i=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(w=>Number.isFinite(w)&&w>0))throw new RangeError("'T' must be positive");let a=t.r??1;if(!(Number.isFinite(a)&&a>=0))throw new RangeError("'r' must be a single positive number");let c=Ge(t.alternative),l=t.confLevel??.95;if(o===2?Ct(l):fn(l),o===2){let[w,N]=r,[b,E]=i,v=w+N;if(v===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=a*b,T=y+E,M=Number.isFinite(T)&&T>=rs&&(y===0||y>=rs)?y/T:1/(1+E/b/a),R=Ei(Math.round(w),Math.round(w)+Math.round(N),{p:M,alternative:c,confLevel:l}),x=E/b,A=S=>{if(S===0)return 0;let I=S/(1-S);if(Number.isFinite(x)||I===1/0)return I*x;let[C,_]=Or(I),[F,O]=Or(b),[D,k]=Or(E);return W(C*D/F,_+k-O)};return{method:"Comparison of Poisson rates",alternative:c,statistic:w,statisticName:"count1",parameter:Yd(v,a,b,E),parameterName:"expected count1",pValue:R.pValue,confInt:[A(R.confInt[0]),A(R.confInt[1])],confLevel:l,estimate:Wd(w,N,b,E),estimateNames:"rate ratio",nullValue:a,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=a*m,h;c==="less"?h=eo(f,u,!0,!1):c==="greater"?h=Ti(f,u):h=Kd(f,u);let p=w=>f===0?0:xs(w,f,1,!0,!1),d=w=>w<1e-8?xs(w,f+1,1,!1,!1):xs(1-w,f+1,1,!0,!1),g;if(c==="less")g=[0,d(1-l)];else if(c==="greater")g=[p(1-l),1/0];else{let w=(1-l)/2;g=[p(w),d(w)]}return{method:"Exact Poisson test",alternative:c,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[g[0]/m,g[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:a,nullValueName:"event rate",warnings:[]}}var Gf=["p","alternative","confLevel","correct"],Bd="Chi-squared approximation may be incorrect";function Ri(e,t,n={}){lt(t)&&z(t,Gf,"propTest"),z(n,Gf,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let A=e;if(A.some(S=>S.length!==2))throw new RangeError("'x' must have 2 columns");r=A.map(S=>S[0]),o=A.map(S=>S[0]+S[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let i=r.length,a=[];for(let A=0;A<i;A++)!Number.isNaN(r[A])&&!Number.isNaN(o[A])&&a.push(A);let c=a.length;if(c<1)throw new RangeError("not enough data");if(r=a.map(A=>r[A]),o=a.map(A=>o[A]),o.some(A=>!(A>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(A=>A===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(A=>!(A>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((A,S)=>A>o[S]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let A=typeof s.p=="number"?[s.p]:Array.from(s.p);if(A.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=a.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;c===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;Ct(m);let u=Ge(s.alternative);(c>2||c===2&&f)&&(u="two.sided");let h=r.map((A,S)=>A/o[S]),p=c===2?h[0]-h[1]:0,d=c===2?1/o[0]+1/o[1]:0,g=(s.correct??!0)&&c<=2?.5:0,w=!1;if(g>0&&c===1){let A=Math.abs(r[0]-o[0]*l[0]);A<=g&&([g,w]=[A,!0])}else if(g>0&&c===2&&!f){let A=Math.abs(p)/d;A<=g&&([g,w]=[A,!0])}let N=Ud(r,o),b=o.some((A,S)=>{let I=l?.[S]??N;return A*I<5||A*(1-I)<5}),E=0;if(!w)for(let A=0;A<c;A++){let S=l?.[A]??N,I=o[A]*S,C=o[A]*(1-S);E+=Dt(Math.abs(r[A]-I)-g,I),E+=Dt(Math.abs(o[A]-r[A]-C)-g,C)}let v=l!==void 0?c:c-1,y;if(u==="two.sided")y=vt(E,v/2,2,!1,!1);else{let A=Math.sign(c===1?h[0]-l[0]:p);y=oe(A*Math.sqrt(E),{lowerTail:u==="less"})}let T;if(c===1||c===2&&!f){let A=(1-m)/2,S=u==="two.sided"&&A<1e-8?re(A,{lowerTail:!1}):re(u==="two.sided"?(1+m)/2:m);if(c===1){let I=o[0],C=S*S,_=(L,q)=>(L+C/(2*I)+q*S*Math.sqrt(L*(1-L)/I+C/(4*I*I)))/(1+C/I),F=h[0]+g/I,O=h[0]-g/I,D=Math.min(1,Math.max(0,F>=1?1:_(F,1))),k=Math.min(1,Math.max(0,O<=0?0:_(O,-1)));T=u==="two.sided"?[k,D]:u==="greater"?[k,1]:[0,D]}else{let I=Math.sqrt(h.reduce((O,D,k)=>O+D*(1-D)/o[k],0)),C=S*I+g*d,_=Math.max(-1,p-C),F=Math.min(1,p+C);T=u==="two.sided"?[_,F]:u==="greater"?[_,1]:[-1,F]}}let M=c===1?"p":a.map(A=>`prop ${A+1}`),x={method:`${c===1?"1-sample proportions test":`${c}-sample test for ${f?"given proportions":"equality of proportions"}`} ${g!==0?"with":"without"} continuity correction`,alternative:u,statistic:E,statisticName:"X-squared",parameter:v,parameterName:"df",pValue:y,estimate:c===1?h[0]:h,estimateNames:M,warnings:b?[Bd]:[]};return l!==void 0&&(x.nullValue=c===1?l[0]:l,x.nullValueName=M),T!==void 0&&(x.confInt=T,x.confLevel=m),J("propTest",x)}function Ud(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+W(s,-r),0)/t.reduce((o,s)=>o+W(s,-r),0)}var Gd=["method","confLevel","sides"],Hd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],Qd=["two.sided","left","right"];function os(e,t){let n=Te(t,Qd),r=e??.95;if(!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let o=n==="two.sided"?r:1-2*(1-r);if(!(o>=0))throw new RangeError("a one-sided 'confLevel' must be at least 0.5");let s=n!=="two.sided"&&1-r<1e-8?2*(1-r):1-o;return{...xi(s,o),sides:n}}function xi(e,t=1-e){let n=e/2<1e-8,r=n?re(e/2,{lowerTail:!1}):re(1-e/2),o=n?jn(e,1,{lowerTail:!1}):jn(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function fo(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 Zn(e,t,n,r){let o=fo(e,n),s=fo(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 sa(e,t,n){let r=e/t,o=1-r,s=n*n,i=(e+s/2)/(t+s),a=n*Math.sqrt(t)/(t+s)*Math.sqrt(r*o+s/(4*t)),c=i+a,l=i-a;return e===0?l=0:l<i/2&&(l=r*(e/(t+s))/c),[l,e===t?1:c]}function oa(e,t,n,r){return r?e/2<1e-8?sr(e/2,t,n,!1,!1):sr(1-e/2,t,n,!0,!1):sr(e/2,t,n,!0,!1)}function Hf(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*dn(e,t,m,!1)+Bn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*dn(e,t,m,!1)+Bn(e,t,m,!1,!1)-o},i=r?-1:1,a=Math.log(e/t),c=i*s(a);if(c===0)return e/t;if(c>0){let f=a,m=c,u=1;for(;;){let h=a-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:Wt(d=>i*s(d),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=i*s(0);return l<=0?1:Math.exp(Wt(f=>i*s(f),a,0,{tol:2**-60,fLower:c,fUpper:l}))}function Si(e,t,n={}){z(n,Gd,"binomCI");let r=Te(n.method,Hd),o=os(n.confLevel,n.sides),[s,i]=Zn(e,t,"x","n"),{alpha:a,kappa:c}=o,l=s/i,f=1-l,m,u;switch(r){case"wald":{let h=c*Math.sqrt(l*f/i);[m,u]=[l-h,l+h];break}case"wilson":[m,u]=sa(s,i,c);break;case"wilsoncc":{let h=c*c,p=2*(i+h),d=2*s+h-1,g=c*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:d-g<d/2?(2*s-1)/(2*i)*(2*s-1)/(d+g):(d-g)/p,u=l===1?1:(2*s+h+1+c*Math.sqrt(h+2-1/i+4*l*(i*f-1)))/p;break}case"agresti-coull":{let h=c*c,p=i+h,d=(s+h/2)/p,g=c*Math.sqrt(d*(1-d)/p);[m,u]=[d-g,d+g];break}case"jeffreys":m=s===0?0:oa(a,s+.5,i-s+.5,!1),u=s===i?1:oa(a,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:oa(a,s,i-s+1,!1),u=s===i?1:oa(a,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(a)/i):Hf(s,i,a,!1),u=s===i?1:s===0?-Math.expm1(Math.log(a)/i):Hf(s,i,a,!0);break}return m=Math.max(0,m),u=Math.min(1,u),o.sides==="left"?u=1:o.sides==="right"&&(m=0),J("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function ss(e){return e===1/0?1:e/(1+e)}function as(e,t){return e===0?0:t>0?e*e/t:1/0}function Xd(e,t,n,r){return e/t+n/r>1}function Zd(e,t,n,r){return ia(e,t,n,r,0)}function qi(e,t,n,r,o){if(Xd(e,t,n,r)){let[p,d,g,w]=qi(t-e,t,r-n,r,-o);return[d,p,w,g]}let s=o<=0,[i,a,c,l]=s?[e,t,n,r]:[n,r,e,t],f=Math.abs(o),m=1-f,u=0;if(m>0){let p=w=>{let N=0;return i>0&&(N+=i/w),i<a&&(N-=(a-i)/(1-w)),c>0&&(N+=c/(w+f)),c<l&&(N-=(l-c)/(m-w)),N},d=w=>{let N=0;return i>0&&(N-=i/(w*w)),i<a&&(N-=(a-i)/((1-w)*(1-w))),c>0&&(N-=c/((w+f)*(w+f))),c<l&&(N-=(l-c)/((m-w)*(m-w))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let w=0,N=m,b=Jd(e,t,n,r,o);u=s?b:b-o,u>w&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let v=p(u);if(v===0)break;v>0?w=u:N=u;let y=u-v/d(u);if(y>w&&y<N||(y=w===0?N/2:N>2*w?Math.sqrt(w*N):w+(N-w)/2),!(y>w&&y<N))break;let T=Math.abs(y-u);if(u=y,T<=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 Jd(e,t,n,r,o){let s=e/t,i=n/r,a=r/t,c=1+a,l=-(1+a+s+a*i+o*(a+2)),f=o*o+o*(2*s+a+1)+s+a*i,m=-s*o*(1+o),u=l/(3*c),h=u*u*u-l*f/(6*c*c)+m/(2*c),p=Math.sign(h)*Math.sqrt(Math.max(0,u*u-f/(3*c)));if(p===0)return-u;let d=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(d)-u}function Ii(e,t,n,r,o,s){if(s==="wald")return e/t*((t-e)/t)/t+n/r*((r-n)/r)/r;let[i,a,c,l]=qi(e,t,n,r,o),f=i*a/t+c*l/r;if(s==="mee")return f;let m=t+r;return f*m/(m-1)}function e0(e,t,n,r,o,s,i){let a=Le(Le(ht([e,0],t),ht([n,0],r)),[o,0]),[c,l,f,m]=qi(e,t,n,r,o),u=he(ht(Xt(c,l),t),ht(Xt(f,m),r));return s==="mn"&&(u=ht(Kt(u,t+r),t+r-1)),Le(_e(a,a),Kt(u,i))[0]}function ia(e,t,n,r,o){return Le(Le(ht([e,0],t),ht([n,0],r)),[o,0])[0]}function t0(e,t,n,r,o,s){return as(ia(e,t,n,r,o),Ii(e,t,n,r,o,s))}var aa=new Float64Array(1),Ai=new BigInt64Array(aa.buffer);function Qf(e){return aa[0]=Math.abs(e),e<0?-Ai[0]:Ai[0]}function wr(e){return Ai[0]=e<0n?-e:e,e<0n?-aa[0]:aa[0]}function ki(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,a=Qf(i?r:n),c=Qf(t),l=s,f=c,m=s,u=1n;for(;;){if(f=i?c+u:c-u,(i?f>=a:f<=a)&&(f=a),m=e(wr(f)),m===0)return wr(f);if(m<0!=l<0||f===a)break;if(c=f,l=m,u*=2n,u>1n<<40n)return wr(c)}if(m<0==l<0||Number.isNaN(m))return wr(c);for(;f-c>1n||c-f>1n;){let h=(c+f)/2n,p=e(wr(h));if(p===0)return wr(h);p<0==l<0?(c=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?wr(f):wr(c)}function Zf(e,t,n,r,o,s){let i=Zd(e,t,n,r);if(o===0)return[i,i];let a=ss(o),c=p=>(p===i?0:ss(t0(e,t,n,r,p,s)))-a,l=(p,d,g,w)=>{if(g===0)return p;if(w===0)return d;let N=Wt(c,p,d,{tol:1e-300,fLower:g,fUpper:w});return s==="wald"?N:ki(b=>e0(e,t,n,r,b,s,o),N,p,d,g<0)},f=c(-1),m=c(1),u=i<=-1||f<=0?-1:l(-1,i,f,-a),h=i>=1||m<=0?1:l(i,1,-a,m);return[u,h]}function Xf(e,t,n){let r=t,o=e(t);if(o>=0)return Math.exp(t);let s=1;for(;;){let i=t+n*s;if(Math.abs(i)>745)return n<0?0:1/0;let a=e(i);if(a>=0){if(a===0)return Math.exp(i);let c=n>0?Wt(e,r,i,{tol:2**-60,fLower:o,fUpper:a}):Wt(e,i,r,{tol:2**-60,fLower:a,fUpper:o});return Math.exp(c)}r=i,o=a,s*=2}}function n0(e,t,n,r,o){let s=t+r,i=o*(t+n)+e+r,a=o*(e+n),c=Math.sqrt(Math.max(0,i*i-4*s*a)),l=Math.min(1,o,a===0?0:2*a/(i+c));return[l,Math.min(1,l/o)]}function r0(e,t,n,r,o){let[s,i]=n0(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,i,1-i];let a=s<=.5&&i<=.5?0:o*(1-i)<=1-s?2:3,c=a===0?i:a===2?1-i:1-s,l=y=>a===0?[y,0]:a===2?zt(1,-y):ht(zt(1,-y),o),f=a===3?1/o:1,m=a===0?Math.min(1,1/o):1,u=a===2?o>1?ht(zt(o,-1),o)[0]:0:a===3&&o<1?zt(1,-o)[0]:0,h=y=>{let T=l(y),M=Kt(T,o);return[M[0],Le([1,0],M)[0],T[0],Le([1,0],T)[0]]},p=e+n,d=a<2?1:-1,g=y=>{let[,T,M,R]=h(y),x=0,A=0;return p>0&&(x+=M>0?p/M:1/0,A-=p/(M*M)),e<t&&(x-=T>0?o*(t-e)/T:1/0,A-=o*(t-e)/T*(o/T)),n<r&&(x-=R>0?(r-n)/R:1/0,A-=(r-n)/(R*R)),[d*x,A*f]},w;if(!(m>u))w=u;else if(!(g(u)[0]>0))w=u;else if(!(g(m)[0]<0))w=m;else{let y=u,T=m;w=c,w>y&&w<T||(w=y+(T-y)/2);for(let M=0;M<2e3;M++){let[R,x]=g(w);if(R===0)break;R>0?y=w:T=w;let A=w-R/x;if(A>y&&A<T||(A=y<=0?T/2:T>2*y?Math.sqrt(y*T):y+(T-y)/2),!(A>y&&A<T))break;let S=Math.abs(A-w);if(w=A,S<=2**-53*w)break}}let[N,b,E,v]=h(w);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,v))]}function o0(e,t,n,r,o){let[s,i,a,c]=r0(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return as(l(e,t,s,i),t*s*i)+as(l(n,r,a,c),r*a*c)}function Jf(e,t,n,r,o){let s=e/t/(n/r);return tm(s,o,i=>o0(e,t,n,r,i))}function s0(e,t,n,r,o){let s=e+n,i=r*(o-1),a=t*o+r-s*(o-1),c=s===0?0:Math.min(1,2*s/(a+Math.sqrt(Math.max(0,a*a+4*i*s))));return[o===1/0?c>0?1:0:Math.min(1,c*o/(1+c*(o-1))),c]}function a0(e,t,n,r,o){let[s,i]=s0(e,t,n,r,o),a=e-t*s;return as(a,t*s*(1-s))+as(a,r*i*(1-i))}function em(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return tm(s,o,i=>a0(e,t,n,r,i))}function tm(e,t,n){if(t===0)return[e,e];let r=ss(t),o=l=>n(l)-t,s=l=>l>0&&l<e?ki(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?ki(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>ss(n(u))-r,f=l(1),m=f===0?1:dr(l,e===0?f>0:f<0);return e===0?[0,i(m)]:[s(m),1/0]}let a=Math.log(e),c=l=>(l===a?0:ss(n(Math.exp(l))))-r;return[s(Xf(c,a,-1)),i(Xf(c,a,1))]}var i0=["method","confLevel","sides"],c0=["method","confLevel","sides"],l0=["method","confLevel"],f0=["ac","wald","score","mn","mee"];function _i(e,t,n,r,o,s){let{kappa:i}=s,a=e/t,c=n/r,l=a-c,f,m;switch(o){case"wald":{let u=i*Math.sqrt(a*(1-a)/t+c*(1-c)/r);[f,m]=[Math.max(-1,l-u),Math.min(1,l+u)];break}case"ac":{let u=(e+1)/(t+2),h=(n+1)/(r+2),p=u-h,d=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-d),Math.min(1,p+d)];break}case"score":{let[u,h]=sa(e,t,i),[p,d]=sa(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+d*(1-d)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=Zf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function m0(e,t,n,r,o={}){z(o,i0,"binomDiffCI");let s=Te(o.method,f0),i=os(o.confLevel,o.sides),[a,c]=Zn(e,t,"x1","n1"),[l,f]=Zn(n,r,"x2","n2"),m=a/c-l/f,[u,h]=_i(a,c,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),J("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var u0=["katz.log","koopman"];function ca(e,t,n,r,o){let s=e/t/(n/r),i=o*Math.sqrt(1/e-1/t+1/n-1/r);return[s*Math.exp(-i),s*Math.exp(i)]}function h0(e,t,n,r,o={}){z(o,c0,"binomRatioCI");let s=Te(o.method,u0),i=os(o.confLevel,o.sides),[a,c]=Zn(e,t,"x1","n1"),[l,f]=Zn(n,r,"x2","n2"),m=i.kappa,u=a===0?0:a/c/(l/f),h,p;return a===0&&l===0?[h,p]=[0,1/0]:s==="katz.log"?a===0?[h,p]=[0,ca(.5,c,l,f,m)[1]]:l===0?[h,p]=[ca(a,c,.5,f,m)[0],1/0]:a===c&&l===f?[h,p]=ca(c-.5,c,f-.5,f,m):[h,p]=ca(a,c,l,f,m):[h,p]=Jf(a,c,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),J("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var p0=["midp","fisher","wald","score"],d0={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"},g0=2**23,w0=2**20;function nm(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=he(r,[e[o],0]));return r[0]}function b0(e,t={}){z(t,l0,"oddsRatio");let n=Te(t.method,p0);if(e.length!==2||e.some(A=>A.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=os(r,void 0),a=e.map((A,S)=>Array.from(A,(I,C)=>fo(I,`table[${S}][${C}]`))),[[c,l],[f,m]]=a,u=c+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,c-m),p=c+Math.min(l,f),d=p-h+1;if(n==="midp"||n==="fisher"){if(d>g0)throw new RangeError(`the exact distribution of the 2 x 2 table has ${d} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(c,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let g,w,N;try{g=cn(c-1,c+f,l+m,c+l,!0,!1)+.5*En(c,c+f,l+m,c+l,!1),w=cn(c,c+f,l+m,c+l,!1,!1)+.5*En(c,c+f,l+m,c+l,!1),N=d<=w0?fi(new co(c+f,l+m,c+l),c,1):Sf(c,c+f,l+m,c+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(g,w),E=[c+l,f+m],v=[c+f,l+m],y=0,T=!1;for(let A=0;A<2;A++)for(let S=0;S<2;S++){let I=E[S]*v[A]/u;I<5&&(T=!0);let C=Math.abs(a[S][A]-I);y+=C*C/I}let M=vt(y,.5,2,!1,!1),R,x;switch(n){case"wald":{R=c*m/(l*f);let A=s*Math.sqrt(1/c+1/l+1/f+1/m);x=[Math.exp(Math.log(R)-A),Math.exp(Math.log(R)+A)];break}case"fisher":{let A=Ke(()=>ts(a,{confLevel:r}));R=A.estimate,x=A.confInt;break}case"score":{R=c*m/(l*f),x=(c===0||m===0)&&(l===0||f===0)?[0,1/0]:em(c,c+l,f,f+m,i);break}case"midp":{let A=new co(c+f,l+m,c+l),S=F=>{if(F===1)return[g,w];let O=A.density(F),D=c-h,k=.5*O[D];return[nm(O,0,D-1)+k,nm(O,D+1,O.length-1)+k]},I=(F,O)=>{let D=F(1);return D===0?1:dr(F,O?D>0:D<0)};R=c===h?0:c===p?1/0:I(F=>S(F)[0]-.5,!1);let C=c===h?0:I(F=>S(F)[1]-o/2,!0),_=c===p?1/0:I(F=>S(F)[0]-o/2,!1);x=[C,_];break}}return J("oddsRatio",{estimate:R,confInt:x,confLevel:r,method:d0[n],pValue:{midp:b,fisher:N,chiSquare:M},warnings:T?["Chi-squared approximation may be incorrect"]:[]})}var v0=["epsilon","paired","varEqual","confLevel","alpha"],N0=["margin","method","alpha"];function rm(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function M0(e,t,n={}){z(n,v0,"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)rm(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 i=n.paired??!1,a=n.varEqual??!1,c=Ke(()=>Sn(e,t,{paired:i,varEqual:a})),l=c.stdErr,f=c.parameter,m=typeof c.estimate=="number"?c.estimate:c.estimate[0]-c.estimate[1],u=at((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?it(s,f,!1,!1):it(o,f,!0,!1),p=[m-h*l,m+h*l];return J("tost",{method:c.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:c.estimate,estimateNames:i?"mean of the differences":c.estimateNames,epsilon:r,seDiff:l,warnings:[]})}var y0=["mn","mee","wald"];function E0(e,t,n,r,o={}){z(o,N0,"binomDiffEquivalenceTest");let s=Te(o.method,y0),i=o.margin;if(i===void 0)throw new TypeError("'margin' is required");if(!(typeof i=="number"&&i>0&&i<1))throw new RangeError("'margin' must be a single number in (0, 1)");let a=o.alpha??.05;rm(a,"alpha");let[c,l]=Zn(e,t,"x1","n1"),[f,m]=Zn(n,r,"x2","n2"),u=c/l-f/m,h=E=>ia(c,l,f,m,E)/Math.sqrt(Ii(c,l,f,m,E,s)),p=h(-i),d=h(i),g=[oe(p,{lowerTail:!1}),oe(d)],w=Math.max(g[0],g[1]),N=1-2*a,b=_i(c,l,f,m,s,xi(2*a,N));return J("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:a,statistic:[p,d],statisticName:"z",pValues:g,pValue:w,result:g[0]<a&&g[1]<a?"rejected":"not rejected",confInt:b,confLevel:N,warnings:[]})}var jt=new WeakMap;function fa(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Re(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function ma(e,t){return e.map(n=>{if(n.rss>=T0&&n.rss<1/0)return W(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let i=n.weights?.[s]??1;if(i===0)return;let a=W(o,-t);r+=i*a*a}),r})}var T0=2**-1022;function Li(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,W(n,-t)]])}function ua(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function Fr(e,t,n=0){let r=i=>e.reduce((a,[c,l])=>a+W(c,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=ua(e),s=r(o)),!(W(t,-o)>=1e-24*s)}function om(e){let t=fa([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let i=e.assign[n[s]],a=o.get(i);a===void 0&&(a={term:i,df:0,ss:0},o.set(i,a),r.push(a)),a.df++,a.ss+=W(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:ma([e],t)[0],k:t}}var sm=(e,t,n)=>Nt(e,t,n,!1,!1),Oi="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Fi(e,t,n){if(!(n>0))return!1;let r=e.length,o=0;for(let c of e)o+=c;o/=r;let s=0;for(let c of e)s+=(c-o)**2;let i=r>1?s/(r-1):0,a=t/n;return Number.isFinite(a)&&a<(o*o+i)*1e-30}var Pi=new WeakMap;function R0(e){let t=om(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let i=e.weights?.[s]??1;i!==0&&(r+=i*W(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 Ci(e){let t=R0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:a}=t;if(r>0){let d=Li(e,o);Fr(d,a)&&(a=0,i=i.map(g=>Fr(d,g.ss)?{...g,ss:0}:g))}let c=a/r,l=d=>W(d,2*o),f=i.map(({df:d,ss:g})=>{let w=g/d/c;return[d,l(g),l(g/d),w,sm(w,d,r)]});f.push([r,l(a),r===0?NaN:l(c),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],h={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},p=i.map(({df:d,ss:g})=>[d,g]);return p.push([r,a]),jt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function x0(e){let t=e.map(f=>f.dfResidual),n=fa(e),r=ma(e,n),o=f=>W(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let i=r[s]/t[s],a=0;e[s].fittedValues.forEach((f,m)=>{let u=e[s].weights?.[m]??1;u!==0&&(a+=u*W(f,-n)**2)});let c=r[s]<1e-10*a,l=e.map((f,m)=>{if(m===0)return[t[0],o(r[0]),NaN,NaN,NaN,NaN];let u=t[m-1]-t[m],h=r[m-1]-r[m],[p,d]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],g=N=>N.filter((b,E)=>(p.weights?.[E]??1)!==0),w=u===0||!js(g(p.x),g(d.x))?NaN:h/u/i;return w<0&&(w=NaN),[t[m],o(r[m]),u,o(h),w,Number.isNaN(w)?NaN:sm(w,Math.abs(u),t[s])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((f,m)=>String(m+1)),table:l,warnings:c?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function la(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function Di(e,...t){let n=[e,...t];for(let a of n)if(typeof a!="object"||a===null||!Array.isArray(a.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1){let a=Ci(e),c={...a,warnings:la(e.warnings,a.warnings)},l=jt.get(a);return l!==void 0&&jt.set(c,l),J("anova",c,e)}if(n.some(a=>a.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=(a,c)=>a===c||a!==void 0&&c!==void 0&&a.length===c.length&&a.every((l,f)=>Object.is(l,c[f]));if(n.some(a=>!r(a.rows,e.rows)))throw new Error("models were not all fitted to the same rows of the data");let o=n.filter(a=>a.response===e.response);if(o.some(a=>!r(a.y,e.y)))throw new Error("models were not all fitted to the same response values");if(o.some(a=>!r(a.weights,e.weights)))throw new Error("models were not all fitted with the same weights");let s=[];if(o.length<n.length){let a=[...new Set(n.filter(c=>c.response!==e.response).map(c=>c.response))];s.push(`models with response ${a.map(c=>`\u2018${c}\u2019`).join(", ")} removed because response differs from model 1`)}let i=o.length===1?Ci(e):x0(o);return i.warnings=la(...n.map(a=>a.warnings),s,i.warnings),J("anova",i,...n)}function Vi(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,a=la(i.warnings,Pi.get(i)),c={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:a},l=jt.get(i);return Array.isArray(l)&&jt.set(c,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),J("aovSummary",c,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=Ci(t),r=om(t),o=Fi(t.fittedValues.map(i=>W(i,-r.k)),r.rss,r.rdf),s=la(t.warnings,n.warnings,o?[Oi]:[]);if(t.dfResidual>0){let i={...n,warnings:s},a=jt.get(n);return a!==void 0&&jt.set(i,a),J("aovSummary",i,e)}return J("aovSummary",{colNames:n.colNames.slice(0,3),rowNames:n.rowNames.slice(0,-1),table:n.table.slice(0,-1).map(i=>i.slice(0,3)),warnings:s},e)}var S0=["pooledSd","mu","paired","ci","alternative","adjust"],A0=["pooledSd","mu","paired","ci","alternative"],k0=["generalized","partial","ci","alternative"],q0=["partial","ci","alternative"],I0=["partial","ci","alternative"];function Ki(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var _0="Missing values detected. NAs dropped.",C0=`'y' is numeric but has only 2 unique values.
|
|
3
|
+
If this is a grouping variable, convert it to a factor.`,lm={tol:0,maxiter:5e3};function Pr(e){return e===1/0?1:Math.exp(xl(.5,(e-1)/2)-.5*Math.log(e/2))}function am(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return qr(o=>yn(e,t,{ncp:o})-n,e-r,e+r,{...lm,extendInt:"downX"}).root}function fm(e,t,n,r){let o=n.pooledSd??!0,s=n.mu??0,i=n.paired??!1,a=n.ci===void 0?.95:n.ci;Ki(a);let c=Te(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(Ft(f)).size===2&&m.push(C0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(_0);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,d,g,w,N;if(f===void 0||i){let A;if(f===void 0)({groups:[A],k:N}=Re([Ft(l)]));else{let[I,C]=cr(l,f);({d:A,k:N}=Is(I,C))}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=ct(A),p=Math.sqrt(st(A)),d=p/Math.sqrt(S),g=1/S,w=S-1}else{let A=Rt([Ft(l),Ft(f)]);N=A.k;let[S,I]=A.n;if(S<2||I<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=A.mean[0]-A.mean[1];let C=Math.sqrt(A.variance[0]),_=Math.sqrt(A.variance[1]);o?(p=Math.sqrt(((S-1)*C**2+(I-1)*_**2)/(S+I-2)),d=p*Math.sqrt(1/S+1/I),g=1/S+1/I,w=S+I-2):(p=Math.sqrt((C**2+_**2)/2),g=2*(I*C**2+S*_**2)/(S*I*(C**2+_**2)),d=Math.sqrt(Math.sqrt(C**2/S)**2+Math.sqrt(_**2/I)**2),w=_s([C**2/S,_**2/I],[S-1,I-1]))}let b=r?Pr(w):1,E=W(s,-N),v=(h-E)/p*b,y=r?"Hedges_g":"Cohens_d";if(a===null)return{name:y,value:v,warnings:m};let T=c==="two.sided"?a:2*a-1,M=(h-E)/d,R=am(M,w,(1+T)/2)*Math.sqrt(g)*b,x=am(M,w,(1-T)/2)*Math.sqrt(g)*b;return c==="less"&&(R=-1/0),c==="greater"&&(x=1/0),{name:y,value:v,ci:a,ciLow:R,ciHigh:x,warnings:m}}function L0(e,t,n={}){return z(n,S0,"cohensD"),J("cohensD",fm(e,t,n,n.adjust??!1))}function O0(e,t,n={}){return z(n,A0,"hedgesG"),J("hedgesG",fm(e,t,n,!0))}var F0=e=>Array.isArray(e.effects),$i=e=>e.multistratum===!0,mm=e=>Array.isArray(e.strata),um=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},zi=e=>({test:t=>t.split(":").includes(e)});function P0(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let s=o.stratum.replace(/^Error: /,""),i=o.colNames.indexOf("Df"),a=o.colNames.indexOf("Sum Sq"),c=o.rowNames.map((f,m)=>({name:f,df:o.table[m][i],ss:o.table[m][a]})),l=c.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:c.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 D0(e){let t=e.univariate,n=t.colNames.indexOf("Sum Sq"),r=[],o=(c,l,f)=>{let m=r.find(u=>u.name===c);return m===void 0&&r.push(m={name:c,terms:[],sse:l,dfe:f}),m},s=new Map;if(t.colNames.includes("Error SS")){let c=t.colNames.indexOf("num Df"),l=t.colNames.indexOf("Error SS"),f=t.colNames.indexOf("den Df");t.rowNames.forEach((m,u)=>{let h=t.table[u],p=e.within.filter(w=>zi(w).test(m)),d=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[c],ss:h[n]};d.terms.push(g),s.set(m,g)})}else{let c=t.colNames.indexOf("Df"),l=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[l][n],t.table[l][c]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][c],ss:t.table[u][n]};f.terms.push(h),s.set(m,h)})}let i=r.find(c=>c.name==="Subject");if(i===void 0)throw new Error("the result has no subject stratum");let a=e.anova.rowNames.map(c=>{let l=s.get(c);if(l===void 0)throw new Error(`there is no univariate test of '${c}'`);return l});return{strata:r,subject:i,order:a}}function hm(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>zi(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>zi(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function im(e,t,n,r){let o=i=>Cn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:qr(o,0,Math.max(10,e*t*4),{...lm,extendInt:"downX"}).root:0}function pm(e,t,n,r,o){let s=o==="two.sided"?r:2*r-1,i=Math.max(0,e),a=i/t/((1-i)/n),c=im(a,t,n,(1+s)/2),l=im(a,t,n,(1-s)/2);a<=to((1-s)/2,t,n)&&(l=0),a<=to((1+s)/2,t,n)&&(c=0);let f=c===1/0?1:c/(c+n),m=l===1/0?1:l/(l+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}function V0(e){let t=Re([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=W(n.ss,-t)}var cm="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Yi(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(cm);if($i(e)||mm(e)||um(e))return $0(e,t,n);let r=F0(e)?Ke(()=>Di(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(cm);let o=jt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;Ki(i);let a=Te(n.alternative??"greater",["two.sided","less","greater"]),c=s.colNames.indexOf("Df"),l=s.colNames.indexOf("Sum Sq");if(c<0||l<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let f=s.rowNames.map((R,x)=>({name:R,df:s.table[x][c],ss:s.table[x][l]})).filter(R=>R.name!=="(Intercept)");V0(f);let m=f.find(R=>R.name==="Residuals");if(m===void 0)throw new TypeError("the table has no row 'Residuals'");if(!(m.df>0))throw new Error("the model has no residual degrees of freedom: the effect sizes are not defined");let u=f.filter(R=>R!==m),h=m.df,p=m.ss,d=p/h,g=f.reduce((R,x)=>R+x.ss,0),w=f.reduce((R,x)=>R+x.df,0)+1,N=t==="Eta2"?n.generalized??!1:!1,E=u.length===1?"plain":N!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:v,ssObserved:y}=hm(N,u),T=R=>{let{ss:x,df:A}=R;if(t==="Eta2")return E==="partial"?x/(x+p):E==="plain"?x/g:x/(x+p+y-(v(R.name)?x:0));if(t==="Omega2"){let I=E==="partial"?(x-A*d)/(x+(w-A)*d):(x-A*d)/(g+d);return Math.max(0,I)}let S=E==="partial"?(x-A*d)/(x+p):(x-A*d)/g;return Math.max(0,S)},M=`${t}${E==="plain"?"":`_${E}`}`;return{colNames:i===null?[M]:[M,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>i===null?[T(R)]:pm(T(R),R.df,h,i,a)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function $0(e,t,n){let r=n.ci===void 0?.95:n.ci;Ki(r);let o=Te(n.alternative??"greater",["two.sided","less","greater"]),s=um(e),i,a,c;if(s){let v=jt.get(e),y=D0(v!==void 0&&!Array.isArray(v)?{...e,univariate:v}:e);({strata:i,subject:a}=y),c=y.order.map(T=>({term:T,stratum:y.strata.find(M=>M.terms.includes(T))}))}else{let v=$i(e)?Ke(()=>Vi(e)):e,y=jt.get(v);({strata:i,subject:a}=P0(Array.isArray(y)?{...v,strata:y.map((T,M)=>({...v.strata[M],...T}))}:v)),c=i.flatMap(T=>T.terms.map(M=>({term:M,stratum:T})))}let l=Re([[...i.flatMap(v=>v.terms.map(y=>y.ss)),...i.map(v=>v.sse)]]).k;for(let v of i){v.sse=W(v.sse,-l);for(let y of v.terms)y.ss=W(y.ss,-l)}let f=i.flatMap(v=>v.terms),m=i.reduce((v,y)=>v+y.sse,0),u=f.reduce((v,y)=>v+y.ss,0)+m,h=a.sse/a.dfe,p=t==="Eta2"?n.generalized??!1:!1,d=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:w}=hm(p,f),N=(v,y)=>{let{ss:T,df:M}=v,R=y.sse/y.dfe;if(t==="Eta2")return d==="partial"?T/(T+y.sse):d==="plain"?T/u:T/(T+m+w-(g(v.name)?T:0));if(t==="Omega2"){let A=y===a?0:1,S=d==="partial"?(T-M*R)/(T+A*y.sse+a.sse+h):(T-M*R)/(u+h);return Math.max(0,S)}let x=d==="partial"?(T-M*R)/(T+y.sse):(T-M*R)/u;return Math.max(0,x)},b=`${t}${d==="plain"?"":`_${d}`}`,E={colNames:r===null?[b]:[b,"CI","CI_low","CI_high"],rowNames:c.map(v=>v.term.name),table:c.map(({term:v,stratum:y})=>{let T=N(v,y);return r===null?[T]:pm(T,v.df,y.dfe,r,o)}),warnings:[]};return s||(E.groups=c.map(v=>v.stratum.name),E.warnings=[...$i(e)||mm(e)?e.warnings:[]]),E}function z0(e,t={}){return z(t,k0,"etaSquared"),J("etaSquared",Yi(e,"Eta2",t),e)}function K0(e,t={}){return z(t,q0,"omegaSquared"),J("omegaSquared",Yi(e,"Omega2",t),e)}function Y0(e,t={}){return z(t,I0,"epsilonSquared"),J("epsilonSquared",Yi(e,"Epsilon2",t),e)}var W0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),j0=["m1","sd1","n1","m2","sd2","n2","r12"],B0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),U0="Equivalence interval does not include zero.",G0="Warning: setting bound type to SMD produces biased results!",H0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function dm(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 qr(s=>yn(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function Q0(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(M,R,x)=>(1-(x-2)/(x*M*M))*R*R,i,a,c,l,f,m,u,h,p,d,g,w,N=t.biasCorrection;if(e.design==="two"){let{n1:M,n2:R,s1:x,s2:A}=e,S=x*x,I=A*A,C=2*M*R/(M+R);if(t.glass!==void 0){let _=t.glass==="glass1",[F,O,D,k]=_?[x,M,A,R]:[A,R,x,M];a=F,c=O-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Pr(c):1,f=l*o/a,p=1/R+1/M;let L=D/F;m=Math.sqrt(L*L/(k-1)+1/(O-1)+f*f/(2*(O-1))),u=o/(F*Math.sqrt(1/O+L*L/k)),h=M+R-2,d=Math.sqrt(2/C)}else if(t.varEqual)a=Math.sqrt(((M-1)*S+(R-1)*I)/(M+R-2)),c=M+R-2,i=N?"Hedges's g":"Cohen's d",l=N?Pr(c):1,f=l*o/a,p=1/M+1/R,m=Math.sqrt(1/M+1/R+s(l,f,c)),u=o/(a*Math.sqrt(p)),h=c,d=Math.sqrt(2*(R*S+M*I)/(M*R*(S+I)));else{a=Math.sqrt((S+I)/2),c=(M-1)*(R-1)*(S+I)**2/((R-1)*S*S+(M-1)*I*I),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Pr(c):1,f=l*o/a,p=2*(R*S+M*I)/(M*R*(S+I));let _=a**4;m=Math.sqrt(f*f*(S*S/(M-1)+I*I/(R-1))/(8*_)+(S/(M-1)+I/(R-1))/(a*a)),u=o/Math.sqrt(S/M+I/R),h=c,d=Math.sqrt(2/C)}g=c,w=(_,F)=>Math.sqrt(g/(g-2)*(2/C)*(1+_*_*C/2)-_*_/(F*F))}else if(e.design==="paired"){let{n:M,s1:R,s2:x,sdif:A,oneMinusR:S}=e,I=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?a=t.glass==="glass1"?R:x:a=t.rmCorrection?A/Math.sqrt(2*S):A,i=t.glass!==void 0?N?"Glass's delta(g)":"Glass's delta(d)":N?`Hedges's g${I}`:`Cohen's d${I}`,c=M-1,g=2*M-2,l=N?Pr(r==="goulet"?g:c):1,f=l*o/a,p=1/M,u=o*Math.sqrt(M)/a,h=c,d=Math.sqrt(2*S/M),w=(C,_)=>{let F=r==="goulet"?g:c,O=Math.sqrt(F/(F-2)*(2*S/M)*(1+C*C*M/(2*S))-C*C/(_*_));return t.rmCorrection?O:O*Math.sqrt(2*S)},t.glass!==void 0){let C=A/a;m=Math.sqrt(C*C/(M-1)+f*f/(2*(M-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/M+s(l,f,c)):m=w(f,l)}else{let{n:M,s:R}=e;a=R,c=M-1,g=c,i=N?"Hedges's g":"Cohen's d",l=N?Pr(c):1,f=l*o/a,p=1/M,m=Math.sqrt(1/M+f*f/(2*M)),u=o*Math.sqrt(M)/R,h=c,d=1/Math.sqrt(M),w=(x,A)=>Math.sqrt(c/(c-2)*(1/M)*(1+x*x*M)-x*x/(A*A))}let b,E,v,y=c;if(r==="nct"){v=m,y=h;let M=dm(u,h,n,!0),R=dm(u,h,n,!1);b=M*Math.sqrt(p)*l,E=R*Math.sqrt(p)*l}else if(r==="goulet"){v=w(f,l),y=g;let M=f/d;b=an(n,g,{ncp:M})*d,E=an(n,g,{ncp:M,lowerTail:!1})*d}else{v=m;let M=r==="t"?it(n,c,!1,!1):re(n,{lowerTail:!1});b=f-M*v,E=f+M*v}let T=e.delta<0;return{label:i,denom:a,estimate:T?-f:f,se:v,confInt:T?[-E,-b]:[b,E],df:y,J:l}}function gm(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 wm(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 bm(e){let t=Te(e.hypothesis,["EQU","MET"]),n=Te(e.eqboundType,["raw","SMD"])==="SMD",r=Te(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:Te(e.glass,["glass1","glass2"]),s=e.alpha??.05;if(!(typeof s=="number"&&s>0&&s<.5))throw new RangeError("'alpha' must be a single number in (0, 0.5)");let i=e.mu??0;if(!(typeof i=="number"&&Number.isFinite(i)))throw new RangeError("'mu' must be a finite number");let a=(c,l,f)=>{if(c!==void 0&&typeof c!="boolean")throw new TypeError(`'${l}' must be true or false`);return c??f};return{hypothesis:t,smdBounds:n,smdCi:r,glass:o,alpha:s,mu:i,paired:a(e.paired,"paired",!1),varEqual:a(e.varEqual,"varEqual",!1),biasCorrection:a(e.biasCorrection,"biasCorrection",!0),rmCorrection:a(e.rmCorrection,"rmCorrection",!1)}}function vm(e,t,n,r,o,s,i,a,c,l,f,m){let{alpha:u,hypothesis:h}=l,p=Q0(a,l),d=W(p.denom,c),g=l.smdBounds?[f[0]*d,f[1]*d]:f,w=D=>{if(D===0)return[0,n];let k=Math.floor(Math.log2(Math.abs(D))),L=tt(D,-k);return l.smdBounds?[L*p.denom,k+c]:[L,k]},N=l.smdBounds?f:[f[0]/d,f[1]/d];l.smdBounds&&m.push(G0),g[0]<=0&&g[1]>=0||m.push(U0);let b=D=>W(D/r,n-o),E=D=>{let[k,L]=w(D),q=Math.max(n,L);return tt((tt(t,n-q)-tt(k,L-q))/r,q-o)},v=b(t),y=E(f[0]),T=E(f[1]),M=W(r,o),R=h==="EQU",x={t:v,se:M,df:s,pValue:2*at(-Math.abs(v),s,!0,!1)},A={t:y,se:M,df:s,pValue:at(y,s,!R,!1)},S={t:T,se:M,df:s,pValue:at(T,s,R,!1)},I=R?Math.max(A.pValue,S.pValue):Math.min(A.pValue,S.pValue);!R&&g[0]<=0&&g[1]>=0&&I<=x.pValue&&m.push(H0);let C=it(u,s,!1,!1),_=Math.max(n,o),[F,O]=[W(t,n-_),W(r,o-_)];return J(e,{tTest:x,lower:A,upper:S,pValue:I,bounds:{raw:g,smd:N},raw:{estimate:W(t,n),se:M,confInt:[W(F-C*O,_),W(F+C*O,_)]},smd:{label:p.label,estimate:p.estimate,se:p.se,confInt:p.confInt,df:p.df,J:p.J},confLevel:1-2*u,method:i,hypothesis:h,warnings:m})}function X0(e,t,n={}){z(n,W0,"tTOST");let r=bm(n),o=wm(n),{paired:s,varEqual:i,mu:a}=r,c=t===void 0;if(s&&c)throw new TypeError("'y' is required when paired is true");gm(e,"x"),c||gm(t,"y");let l=Ke(()=>c?Sn(e,{mu:a}):Sn(e,t,{paired:s,varEqual:i,mu:a})),f=l.parameter,m,u,h,p;if(c){let d=Re([Ft(Array.from(e))]);u=d.k;let g=d.groups[0],w=st(g);h=ct(g)-W(a,-u),p=Math.sqrt(w/g.length),m={design:"one",n:g.length,delta:h,s:Math.sqrt(w)}}else if(s){let[d,g]=cr(Array.from(e),Array.from(t)),w=Re([d,g]);u=w.k;let[N,b]=w.groups,E=N.map((x,A)=>x-b[A]),v=Math.sqrt(st(N)),y=Math.sqrt(st(b)),T=st(E),M=Math.sqrt(T),R=(M-(v-y))*(M+(v-y))/(2*v*y);h=ct(E)-W(a,-u),p=Math.sqrt(T/E.length),m={design:"paired",n:E.length,delta:ct(N)-ct(b)-W(a,-u),s1:v,s2:y,sdif:M,oneMinusR:R}}else{let d=Re([Ft(Array.from(e)),Ft(Array.from(t))]);u=d.k;let[g,w]=d.groups;if(g.length<2||w.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,b,E,v]=[g.length,w.length,st(g),st(w)];h=ct(g)-ct(w)-W(a,-u),p=Math.sqrt(i?((N-1)*E+(b-1)*v)/(N+b-2)*(1/N+1/b):Math.sqrt(E/N)**2+Math.sqrt(v/b)**2),m={design:"two",n1:N,n2:b,delta:h,s1:Math.sqrt(E),s2:Math.sqrt(v)}}return vm("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function Z0(e,t={}){z(e,j0,"tsumTOST"),z(t,B0,"tsumTOST");let n=bm(t),r=wm(t),{m1:o,sd1:s,n1:i,m2:a,sd2:c,n2:l,r12:f}=e,m=(_,F)=>{if(typeof _!="number"||!Number.isFinite(_))throw new RangeError(`'${F}' must be a finite number`);return _},u=(_,F)=>{let O=m(_,F);if(!(Number.isInteger(O)&&O>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return O},h=(_,F)=>{let O=m(_,F);if(!(O>0))throw new RangeError(`'${F}' must be positive`);return O};m(o,"m1"),h(s,"sd1"),u(i,"n1");let p=a===void 0&&c===void 0&&l===void 0;if(n.paired&&p)throw new TypeError("'m2', 'sd2' and 'n2' are required when paired is true");p||(m(a,"m2"),h(c,"sd2"),u(l,"n2"));let d=[],{groups:g,k:w}=Re([[o,...p?[]:[a],n.mu]]),{groups:N,k:b}=Re([[s,...p?[]:[c]]]),[E,v=NaN]=p?[g[0][0]]:[g[0][0],g[0][1]],y=g[0][g[0].length-1],[T,M=NaN]=N[0],R=_=>W(_,w-b),x,A,S,I,C;if(p)x=E-y,A=T/Math.sqrt(i),S=i-1,I="One Sample t-test",C={design:"one",n:i,delta:R(x),s:T};else if(n.paired){let _=m(f,"r12");if(!(_>-1&&_<1))throw new RangeError("'r12' must be in (-1, 1)");let F=i;i!==l&&(d.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let O=Math.sqrt(T*T+M*M-2*_*T*M);if(!(O>0))throw new RangeError("the standard deviation of the differences is 0");x=E-v-y,A=O/Math.sqrt(F),S=F-1,I="Paired t-test",C={design:"paired",n:F,delta:R(x),s1:T,s2:M,sdif:O,oneMinusR:1-_}}else{let _=l,F=T*T,O=M*M;if(x=E-v-y,n.varEqual){let D=((i-1)*F+(_-1)*O)/(i+_-2);A=Math.sqrt(D*(1/i+1/_)),S=i+_-2,I=" Two Sample t-test"}else{let D=F/i,k=O/_;A=Math.sqrt(D+k),S=(D+k)**2/(D*D/(i-1)+k*k/(_-1)),I="Welch Two Sample t-test"}C={design:"two",n1:i,n2:_,delta:R(x),s1:T,s2:M}}return vm("tsumTOST",x,w,A,b,S,I,C,b,n,r,d)}var J0=["alternative","tsmethod"],eg=["square","central"],tg=1e3,ng=32,rg=.9,Nm=1+1e-7,og=2**24,sg=34e7;function ag(e,t={}){if(z(t,J0,"boschlooTest"),e.length!==2||e.some(y=>y.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ge(t.alternative),r=Te(t.tsmethod,eg),o=e.map((y,T)=>Array.from(y,(M,R)=>fo(M,`table[${T}][${R}]`))),[[s,i],[a,c]]=o,l=s+i,f=a+c;if(l===0||f===0)throw new RangeError("each row (group) of 'table' must have at least one observation");if((l+1)*(f+1)>og)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(tg,Math.ceil(ng*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>sg)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-a/f,d;n!=="two.sided"?d=n:r==="square"?d="square":d=p>0?"greater":"less";let g=s+a,w;d==="less"?w=cn(s,l,f,g,!0,!1):d==="greater"?w=cn(s-1,l,f,g,!1,!1):w=Ke(()=>ts(o)).pValue;let N=ig(l,f,s,a,d),{value:b,at:E}=cg(N,l,f,u),v=n==="two.sided"&&r==="central"?Math.min(1,2*b):b;return J("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:w,statisticName:"test statistic",pValue:v,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:E,warnings:[]})}function ig(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let w=2;w<=s;w++)i[w]=Ls(w);let a=t+1,c=new Float64Array((e+1)*a),l=Math.min(e,t)+1,f=new Float64Array(l),m=new Float64Array(l),u=new Float64Array(l);for(let w=0;w<=s;w++){let N=Math.max(0,w-t),E=Math.min(w,e)-N+1,v=-1/0;for(let M=0;M<E;M++){let R=N+M,x=-i[R]-i[e-R]-i[w-R]-i[t-w+R];f[M]=x,x>v&&(v=x)}let y=0;for(let M=0;M<E;M++)f[M]=Math.exp(f[M]-v),y+=f[M];for(let M=0;M<E;M++)f[M]=f[M]/y;let T=0;for(let M=0;M<E;M++)m[M]=T+=f[M];T=0;for(let M=E-1;M>=0;M--)u[M]=T+=f[M];if(o==="less")for(let M=0;M<E;M++)c[(N+M)*a+w-N-M]=m[M];else if(o==="greater")for(let M=0;M<E;M++)c[(N+M)*a+w-N-M]=u[M];else{let M=0;for(let R=1;R<E;R++)f[R]>f[M]&&(M=R);for(let R=0;R<E;R++){let x=f[R]*Nm,A=-1,S=M;for(;S-A>1;){let F=A+S>>1;f[F]<=x?A=F:S=F}let I=M,C=E;for(;C-I>1;){let F=I+C>>1;f[F]<=x?C=F:I=F}let _=f[M]<=x?1:(A>=0?m[A]:0)+(C<E?u[C]:0);c[(N+R)*a+w-N-R]=Math.min(1,_)}}}let h=c[n*a+r]*Nm,p=new Int32Array(e+2),d=[],g=[];for(let w=0;w<=e;w++){p[w]=d.length;let N=0;for(;N<=t;){if(c[w*a+N]<=h){let b=N;for(;N+1<=t&&c[w*a+N+1]<=h;)N++;d.push(b),g.push(N)}N++}}return p[e+1]=d.length,{starts:p,from:Int32Array.from(d),to:Int32Array.from(g)}}function Mm(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]=dn(r,e,t,!1);let o=zt(1,-t),s=ut([t,0],o),i=ut(o,[t,0]),a=s[0],c=i[0],l=r;for(let h=r+1;h<=e;h++){let p=n[h-1]*((e-h+1)/h*a);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)*c);if(p===0)break;n[h]=p,f=h}let m=s[1]/a,u=i[1]/c;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 cg(e,t,n,r){let{starts:o,from:s,to:i}=e,a=new Float64Array(t+1),c=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=b=>{let[E,v]=Mm(t,b,a),[y,T]=Mm(n,b,c),M=0;for(let x=y;x<=T;x++)l[x]=M+=c[x];M=0;for(let x=T;x>=y;x--)f[x]=M+=c[x];let R=0;for(let x=E;x<=v;x++){let A=a[x];if(A===0)continue;let S=0;for(let I=o[x];I<o[x+1];I++){let C=s[I],_=i[I];if(!(_<y||C>T))if(C===0)S+=l[Math.min(_,T)];else if(_===n)S+=f[Math.max(C,y)];else for(let F=Math.max(C,y);F<=Math.min(_,T);F++)S+=c[F]}R+=A*S}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),d=0;for(let b=0;b<=r+1;b++)p[b]=m(h(u*b/(r+1))),p[b]>p[d]&&(d=b);let g=p[d],w=h(u*d/(r+1));if(g>=1)return{value:1,at:w};let N=rg*g;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 v=u*Math.max(0,b-1)/(r+1),y=u*Math.min(r+1,b+1)/(r+1),M=Bl(A=>-m(h(A)),v,y,1e-14).x,R=m(h(M)),x=1e-4/Math.sqrt(2*(t+n));for(let A=0;A<2&&(M-x>v&&M+x<y);A++,x*=.01){let S=m(h(M-x)),I=m(h(M+x)),C=S-2*R+I;if(!(C<0))break;let _=x*(S-I)/(2*C);if(!(Math.abs(_)<x))break;let F=m(h(M+_));F>R*(1+2**-50)&&(M+=_,R=F)}R>g&&(g=R,w=h(M))}return{value:g,at:w}}var ym=["correct"];function lg(e,t,n={}){lt(t)&&z(t,ym,"mcnemarTest"),z(n,ym,"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=fg(e,t)}let o=n.correct??!0,s=r.length,i,a=o&&s===2&&r[0][1]!==r[1][0];if(a){let l=r[0][1],f=r[1][0];i=Em(Math.max(0,Math.abs(l-f)-1),l,f)}else{i=0;let l=0;for(let f=0;f<s;f++)for(let m=f+1;m<s;m++){let u=r[f][m],h=r[m][f];u+h!==0&&(i+=Em(u-h,u,h),l++)}if(l===0)throw new RangeError("the table has no discordant pairs")}let c=s*(s-1)/2;return{method:a?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:i,statisticName:"McNemar's chi-squared",parameter:c,parameterName:"df",pValue:vt(i,c/2,2,!1,!1),warnings:[]}}function Em(e,t,n){let r=t+n;return r<1/0?Dt(e,r):Dt(W(e,-520),W(t,-1040)+W(n,-1040))}function fg(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=rn(e),o=rn(t);for(let m=0;m<e.length;m++){let u=r[m],h=o[m];typeof u=="number"&&Number.isNaN(u)||typeof h=="number"&&Number.isNaN(h)||n.push([u,h])}let s=m=>Array.from(new Set(m)).sort(Qn),i=s(n.map(m=>m[0])),a=s(n.map(m=>m[1]));if(i.length<2||i.length!==a.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let c=new Map(i.map((m,u)=>[m,u])),l=new Map(a.map((m,u)=>[m,u])),f=i.map(()=>new Array(i.length).fill(0));for(let[m,u]of n)f[c.get(m)][l.get(u)]++;return f}var mg=["ratio","alternative","confLevel"],Wi=2**-1022;function Tm(e,t,n={}){if(z(n,mg,"varTest"),typeof e=="string"){let b=ur(e,t);return Tm(b.x,b.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=n.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let i=Ge(n.alternative),a=n.confLevel??.95;Ct(a);let c=r.length-1,l=o.length-1;if(c<1)throw new Error("not enough 'x' observations");if(l<1)throw new Error("not enough 'y' observations");let[f,m]=Rt([r,o]).variance,u=0;if(!(f>=Wi&&m>=Wi)){let b=Rt([r]),E=Rt([o]);f=b.variance[0],m=E.variance[0],u=2*(b.k-E.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:tt(f/m,u),p=h/s,d=Nt(p,c,l,!0,!1),g=Nt(p,c,l,!1,!1),w,N;if(i==="less")w=d,N=[0,h/pn(1-a,c,l,!0,!1)];else if(i==="greater")w=g,N=[h/pn(a,c,l,!0,!1),1/0];else{w=Math.min(1,2*Math.min(d,g));let b=(1-a)/2,E=b<1e-8?pn(b,c,l,!1,!1):pn(1-b,c,l,!0,!1);N=[h/E,h/pn(b,c,l,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:p,statisticName:"F",parameter:[c,l],parameterName:["num df","denom df"],pValue:w,confInt:N,confLevel:a,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function Rm(e,t){if(typeof e=="string"){let p=Bo(e,t);return Rm(Wl(p.values,p.group))}let n=e,r=n.length;if(r<2)throw new RangeError("'x' must be a list with at least 2 elements");let o=n.map(p=>Array.from(p).filter(Number.isFinite));if(o.some(p=>p.length<2))throw new RangeError("there must be at least 2 observations in each group");let s=o.map(p=>p.length-1),i=Rt(o).variance,a=0,c=0,l=0,f=0;for(let p=0;p<r;p++)a+=s[p],c+=s[p]*i[p],l+=s[p]*Math.log(i[p]),f+=1/s[p];c/=a;let m=a*Math.log(c)-l;if(i.some(p=>p<Wi)){let p=o.map(E=>{let v=Rt([E]);return{w:v.variance[0],e:v.k}}),d=Math.max(...p.map(E=>E.e)),g=p.map(E=>Math.log(E.w)+2*(E.e-d)*Math.LN2),w=g.map((E,v)=>Math.log(s[v])+E),N=Math.max(...w),b=N+Math.log(w.reduce((E,v)=>E+Math.exp(v-N),0))-Math.log(a);m=a*b-g.reduce((E,v,y)=>E+s[y]*v,0)}let u=m/(1+(f-1/a)/(3*(r-1))),h=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:u,statisticName:"Bartlett's K-squared",parameter:h,parameterName:"df",pValue:vt(u,h/2,2,!1,!1),warnings:[]}}var ug=["alternative"],ji=["alternative"];function Bi(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:vt(n,r/2,2,!1,!1),warnings:[]}}function xm(e){let t=0;for(let n of Mn(e))t+=n*n*n-n;return t}function ha(e,t){if(typeof e=="string"){let l=Bo(e,t);return ha(l.values,l.group.codes)}let n=Ir(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),s=o.length,i=Mt(o),a=0,c=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=i[c+m];c+=l.length,a+=f*f/l.length}return a=(12*a/(s*(s+1))-3*(s+1))/(1-xm(o)/(s*s*s-s)),Bi("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",a,r-1)}function Ui(e,t){if(typeof e=="string")return Ui(zs(e,t));let n=Qa(e),r=n.length,o=e[0]?.length??0;if(r<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=new Array(o).fill(0),i=0;for(let l of n){let f=Mt(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=xm(l)}let a=0;for(let l of s)a+=(l-r*(o+1)/2)**2;let c=12*a/(r*o*(o+1)-i/(o-1));return Bi("Friedman rank sum test","Friedman chi-squared",c,o-1)}function hg(e,t={},n,r={}){typeof e!="string"&<(t)&&z(t,ji,"pageTest"),lt(n)&&z(n,ji,"pageTest"),z(r,ji,"pageTest");let o,s;if(typeof e=="string")o=zs(e,t),s=n??{};else if(Array.isArray(n)){let d=Array.from(e),g=t,w=n;if(d.length!==g.length||d.length!==w.length)throw new RangeError("'x', 'groups' and 'blocks' must have the same length");if(!d.every(Number.isFinite))throw new RangeError("missing or non-finite values are not allowed");let N=rn(g),b=rn(w);if(N.some(M=>typeof M=="number"&&Number.isNaN(M))||b.some(M=>typeof M=="number"&&Number.isNaN(M)))throw new RangeError("missing treatment or block labels are not allowed");let E=Array.from(new Set(N)).sort(Qn),v=Array.from(new Set(b)).sort(Qn);if(E.length<2||v.length<1)throw new RangeError("at least two treatments and one block are required");let y=new Map(E.map((M,R)=>[M,R])),T=new Map(v.map((M,R)=>[M,R]));o=v.map(()=>new Array(E.length).fill(NaN));for(let M=0;M<d.length;M++){let R=T.get(b[M]),x=y.get(N[M]);if(!Number.isNaN(o[R][x]))throw new RangeError("not an unreplicated complete block design");o[R][x]=d[M]}if(o.some(M=>M.some(R=>Number.isNaN(R))))throw new RangeError("not an unreplicated complete block design");s=r}else o=e.map(d=>Array.from(d)),s=t;let i=o.length,a=o[0]?.length??0;if(i<1||a<2||o.some(d=>d.length!==a))throw new RangeError("'y' must be a non-empty matrix with at least two columns");if(o.some(d=>d.some(g=>!Number.isFinite(g))))throw new RangeError("missing or non-finite values are not allowed in a block");let c=Ge(s.alternative),l=new Array(a).fill(0);for(let d of o){let g=Mt(d);for(let w=0;w<a;w++)l[w]+=g[w]}let f=0;for(let d=0;d<a;d++)f+=(d+1)*l[d];let m=i*a*(a+1)**2/4,u=i*a**2*(a-1)*(a+1)**2/144,h=(f-m-.5)/Math.sqrt(u),p=c==="less"?oe(h):c==="greater"?oe(h,{lowerTail:!1}):2*oe(-Math.abs(h));return{method:"Page's ordered aligned rank sum test",alternative:c,statistic:h,statisticName:"z",pValue:p,estimate:f,warnings:[]}}function pg(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{...Ui(e),method:"Cochran's Q test",statisticName:"Q"}}function Sm(e,t){if(typeof e=="string")return Sm(zs(e,t));let n=Qa(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(d=>kt(d)-ln(d));s.some(d=>d===1/0)&&n.every(d=>d.every(Number.isFinite))&&(s=Re(n).groups.map(d=>kt(d)-ln(d)));let i=Mt(s),a=new Array(o).fill(0),c=0;n.forEach((d,g)=>{let w=Mt(d);for(let N=0;N<o;N++){let b=i[g]*(w[N]-(o+1)/2);c+=b*b,a[N]+=b}});let l=0;for(let d of a)l+=d*d;l/=r;let f=o-1,m=(r-1)*(o-1),u,h,p=[f,m];if(c===l){u=NaN,p=[NaN,NaN];let d=0;for(let g=2;g<=o;g++)d+=Math.log(g);h=Math.exp((1-r)*d)}else u=(r-1)*l/(c-l),h=Nt(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 Am(e,t){if(typeof e=="string"){let h=Bo(e,t);return Am(h.values,h.group.codes)}let n=Ir(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=h=>h.flatMap(p=>{let d=Bs(p);return p.map(g=>Math.abs(g-d))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(h=>h===1/0)&&n.every(h=>h.every(Number.isFinite))&&(s=o(Re(n).groups));let i=s.length,c=Mt(s).map(h=>re((1+h/(i+1))/2)),l=0;for(let h of c)l+=h;let f=l/i,m=0,u=0;for(let h of n){let p=0;for(let d=0;d<h.length;d++)p+=c[u+d];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=st(c),Bi("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function km(e,t,n={}){if(z(n,ug,"moodTest"),typeof e=="string"){let E=ur(e,t);return km(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ge(n.alternative),i=r.length,a=o.length,c=i+a;if(c<3)throw new RangeError("not enough observations");if(i===0||a===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:c},(E,v)=>v).sort((E,v)=>l[E]-l[v]),m=new Array(c),u=0;for(;u<c;){let E=u;for(;E+1<c&&l[f[E+1]]===l[f[u]];)E++;let v=0;for(let y=u;y<=E;y++)v+=(y+1-(c+1)/2)**2;v/=E-u+1;for(let y=u;y<=E;y++)m[f[y]]=v;u=E+1}let h=0;for(let E=0;E<i;E++)h+=m[E];let p=0;for(let E of m)p+=E;p/=c;let d=0;for(let E of m)d+=(E-p)**2;let g=i*p,w=i*a/(c*(c-1))*d,N=(h-g)/Math.sqrt(w),b=s==="less"?oe(N):s==="greater"?oe(N,{lowerTail:!1}):2*oe(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:b,warnings:[]}}function Dr(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var dg=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],gg=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],wg=[.544,-.39978,.025054,-6714e-7],bg=[1.3822,-.77857,.062767,-.0020322],vg=[-1.5861,-.31082,-.083751,.0038915],Ng=[-.4803,-.082676,.0030302],Mg=[-2.273,.459];function yg(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]=-re((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),i=1/Math.sqrt(e),a=r[0]/s+Dr(dg,i),c=new Float64Array(n);c[0]=a;let l,f;if(e>5){let m=r[1]/s+Dr(gg,i);c[1]=m,l=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*a*a-2*m*m))}else l=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*a*a));for(let m=l;m<n;m++)c[m]=r[m]/f;for(let m=0;m<n;m++)t[e-1-m]=c[m],t[m]=-c[m];return t}function qm(e){let t=Re([e]).groups[0],n=t.length,r=yg(n),o=t[n-1]-t[0],s=0;for(let f of t)s+=f/o;s/=n;let i=0,a=0,c=0;for(let f=0;f<n;f++){let m=t[f]/o-s;i+=r[f]*r[f],a+=m*m,c+=r[f]*m}let l=Math.sqrt(i*a);return(l-c)*(l+c)/(i*a)}var Eg=1.90985931710274,Tg=1.0471975511966;function Im(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=Eg*(Math.asin(Math.sqrt(1-e))-Tg);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Dr(Mg,t);if(n>=i)return 1e-99;let a=Dr(wg,t),c=Math.exp(Dr(bg,t));return oe(-Math.log(i-n),{mean:a,sd:c,lowerTail:!1})}let r=Math.log(t),o=Dr(vg,r),s=Math.exp(Dr(Ng,r));return oe(n,{mean:o,sd:s,lowerTail:!1})}var Om=Math.log(2*Math.PI),_m=2e-14,Cm=500;function Rg(e){return Math.exp(-.5*e*e-.5*Om)}function xg(e){return oe(e,{logP:!0})}function Fm(e,t=-1/0,n=!1){let r=t===-1/0?Ua(e,0,2,0,_m,Cm):Ua(e,t,1,0,_m,Cm),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 Lm(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Fm(i=>{let a=0;for(let c=0;c<r.length;c++){let l=(r[c]*i+e)/o[c];if(!n){a+=xg(l);continue}let f=(r[c]*i-e)/o[c],m=oe(f)+oe(l,{lowerTail:!1});m<.5?a+=Math.log1p(-m):a+=Math.log(Math.max(0,l+f<0?oe(l)-oe(f):oe(f,{lowerTail:!1})-oe(l,{lowerTail:!1})))}return Rg(i)*-Math.expm1(a)})}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,Lm(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Om-Ml(o);return Math.min(1,Fm(a=>{let c=s-o*(Math.expm1(2*a)-2*a);return c<-745?0:Math.exp(c)*Lm(e*Math.exp(a),t,r)},-1/0,!0))}var Sg=["pAdjustMethod"],Ag=["pAdjustMethod","alternative"],kg=["alternative"],qg=["pAdjustMethod","alternative"],Ig=["method","altp"],_g=["alternative"],Cg=["welch"],Pm="Ties are present. z-quantiles were corrected for ties.";function Fn(e,t){let{labels:n,groups:r}=hr(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(i=>i.length);return{labels:n,groups:r,n:s,N:s.reduce((i,a)=>i+a,0),k:o}}function Gi(e){let t=e.groups.flat(),n=Mt(t),r=[],o=0;for(let i of e.n){let a=0;for(let c=0;c<i;c++)a+=n[o+c];o+=i,r.push(a/i)}let s=0;for(let i of Mn(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function $r(e,t){let n=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));for(let r=0;r<e-1;r++)for(let o=r+1;o<e;o++)n[o-1][r]=t(o,r);return n}function Lg(e,t){let n=e.length+1,r=[];for(let a=0;a<n-1;a++)for(let c=a+1;c<n;c++)r.push(e[c-1][a]);let o=lo(r,t),s=0,i=e.map(a=>a.slice());for(let a=0;a<n-1;a++)for(let c=a+1;c<n;c++)i[c-1][a]=o[s++];return i}function is(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 Hi(e,t,n,r,o,s=[]){return{method:t,statistic:n.map(i=>[i]),pValue:r.map(i=>[i]),rowNames:e.labels.slice(1),colNames:e.labels.slice(0,1),pAdjustMethod:o,warnings:s}}var pa=e=>oe(e,{lowerTail:!1}),Qi=(e,t,n)=>Os(Math.abs(e),t,n,{lowerTail:!1});function Xi(e){let t=Rt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return cs(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function cs(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 Og(e,t,n={}){z(n,Sg,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=Fn(e,t),{R:s,C:i}=Gi(o),a=o.N*(o.N+1)/12-i,c=$r(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(a*(1/o.n[f]+1/o.n[m]))),l=Lg(c.map(f=>f.map(m=>2*pa(m))),r);return J("kwAllPairsDunnTest",is(o,"Dunn's all-pairs test",c,l,r,i!==0?[Pm]:[]))}function Fg(e,t,n={}){z(n,Ag,"kwManyOneDunnTest");let r=Ge(n.alternative),o=n.pAdjustMethod??"single-step",s=Fn(e,t),{R:i,C:a}=Gi(s),c=s.N*(s.N+1)/12-a,l=s.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(i[h]-i[0])/Math.sqrt(c*(1/s.n[0]+1/s.n[h]))),m=l.map(h=>s.n[h]/(s.n[h]+s.n[0])),u=Dm(f,m,r,o);return J("kwManyOneDunnTest",Hi(s,"Dunn's many-to-one test",f,u,o,a!==0?[Pm]:[]))}function Dm(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)):lo(e.map(o=>n==="two.sided"?2*pa(Math.abs(o)):pa(n==="greater"?o:-o)),r)}function Pg(e,t,n={}){z(n,kg,"dunnettTest");let r=Ge(n.alternative),o=Fn(e,t),{m:s,s2:i,df:a}=Xi(o),c=Math.sqrt(i),l=o.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(s[h]-s[0])/(c*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,a,!1)):Vr(r==="greater"?h:-h,m,a,!1));return J("dunnettTest",Hi(o,`Dunnett's-test for multiple
|
|
4
|
+
comparisons with one control`,f,u,"single-step"))}function Dg(e,t,n={}){z(n,qg,"manyOneUTest");let r=Ge(n.alternative),o=n.pAdjustMethod??"single-step",s=Fn(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
|
|
5
5
|
|
|
6
|
-
k = 3 groups are required.`);let i=s.n[0],
|
|
7
|
-
for multiple comparisons with one control`,a,f,o))}function q0(e,t){let n=Sn(e,t),r=Rr(n.k,(s,i)=>{let c=n.n[s],a=n.n[i],l=[...n.groups[s],...n.groups[i]],f=bt(l),m=0;for(let w=0;w<c;w++)m+=f[w];let u=m-c*(c+1)/2,h=Math.min(u,c*a-u),p=c+a,d=0;for(let w of pn(l))d+=w*w*w-w;let g=c*a/(p*(p-1))*((p*p*p-p)/12-d/12);return Math.SQRT2*(h-c*a/2)/Math.sqrt(g)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:vi(i,n.k,1/0)));return H("dscfAllPairsTest",Lo(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function I0(e,t,n={}){V(n,x0,"tamhaneT2Test");let r=(()=>{if(t!==void 0)return Sn(e,t);let f=Zn(e),m=f.groups.length;if(m<2)throw new RangeError("there must be at least two groups");let u=f.groups.map(h=>h.length);return{labels:f.labels,groups:f.groups,n:u,N:u.reduce((h,p)=>h+p,0),k:m}})(),o=Et(r.groups),s=Et(o.groups),i=n.welch??!0,c=r.k*(r.k-1)/2,a=Rr(r.k,(f,m)=>{let u=s.variance[f]/r.n[f]+s.variance[m]/r.n[m];return(s.mean[f]-s.mean[m])/Math.sqrt(u)}),l=Rr(r.k,(f,m)=>{let u=a[f-1][m];if(Number.isNaN(u))return NaN;let h=i?pa([s.variance[f],s.variance[m]],[r.n[f],r.n[m]]):r.n[f]+r.n[m]-2,p=2*dn(Math.abs(u),h,{lowerTail:!1});return p>=1?1:-Math.expm1(c*Math.log1p(-p))});return H("tamhaneT2Test",Lo(r,i?"Tamhane's T2-test for unequal variances":"Tamhane's T2'-test for unequal variances",a,l,i?"T2 (Sidak)":"T2' (Sidak)"))}function _0(e,t){let n=Sn(e,t),r=Et(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),i=Rr(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),c=Rr(n.k,(a,l)=>{let f=pa([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return vi(i[a-1][l],n.k,f)});return H("gamesHowellTest",Lo(n,"Games-Howell test",i,c,"none"))}function L0(e,t){let n=Sn(e,t),{m:r,s2:o,df:s}=Ni(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let i=Rr(n.k,(a,l)=>(r[a]-r[l])/Math.sqrt(o*(1/n.n[a]+1/n.n[l])/2)),c=i.map(a=>a.map(l=>Number.isNaN(l)?NaN:vi(l,n.k,s)));return H("tukeyTest",Lo(n,"Tukey's test",i,c,"single-step"))}function C0(e,t,n={}){V(n,M0,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=Sn(e,t),i=Vs(e,t).statistic,{R:c,C:a}=wi(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let p=1;p<s.k;p++)for(let d=0;d<p;d++)f.push((c[d]-c[p])/Math.sqrt(l*(1/s.n[d]+1/s.n[p]))),m.push(`${s.labels[d]} - ${s.labels[p]}`);let u=f.map(p=>(o?2:1)*$s(Math.abs(p))),h=O0(u,r);return{chi2:i,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function O0(e,t){let n=e.length,r=(s,i)=>Math.min(1,-Math.expm1(i*Math.log1p(-s))),o=s=>{let i=e.map((a,l)=>l).sort((a,l)=>(s?e[l]-e[a]:e[a]-e[l])||a-l),c=new Array(n);return i.forEach((a,l)=>c[a]=l>0&&e[a]===e[i[l-1]]?c[i[l-1]]:l+1),c};switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"sidak":return e.map(s=>r(s,n));case"holm":{let s=o(!1);return e.map((i,c)=>Math.min(1,(n+1-s[c])*i))}case"hs":{let s=o(!1);return e.map((i,c)=>r(i,n+1-s[c]))}case"hochberg":{let s=o(!0);return e.map((i,c)=>Math.min(1,s[c]*i))}case"bh":{let s=o(!0);return e.map((i,c)=>Math.min(1,n/(n+1-s[c])*i))}case"by":{let s=o(!0),i=0;for(let c=1;c<=n;c++)i+=1/c;return e.map((c,a)=>Math.min(1,n/(n+1-s[a])*i*c))}default:throw new RangeError(`invalid method "${String(t)}"`)}}function F0(e,t,n={}){V(n,E0,"glhtDunnett");let r=Ge(n.alternative),o=Sn(e,t),{m:s,s2:i,df:c,k:a}=Ni(o),l=Math.sqrt(i),f=o.n.slice(1).map((N,v)=>v+1),m=f.map(N=>s[N]-s[0]),u=f.map(N=>l*Math.sqrt(1/o.n[N]+1/o.n[0])),h=m.map((N,v)=>N/u[v]),p=m.map(N=>$(N,a)),d=u.map(N=>$(N,a)),g=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),w=h.map(N=>r==="two.sided"?xr(N,g,c,!0):xr(r==="greater"?N:-N,g,c,!1));return{names:f.map(N=>`${o.labels[N]} - ${o.labels[0]}`),estimate:p,se:d,t:h,p:w,stdError:d,tValue:h,pValue:w,df:c,warnings:[]}}var Uf=["varEqual"],D0=2**-1022;function Hf(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,Uf,"onewayTest"),V(n,Uf,"onewayTest"),typeof e=="string"){let g=tl(e,t);return Hf(g.values,g.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=dr(e,r),s=Et(o),i=s.groups,c=n.varEqual??!1,a=i.length;if(a<2)throw new RangeError("not enough groups");let l=i.map(g=>g.length);if(l.some(g=>g<2))throw new RangeError("not enough observations");let f=s.mean,m=s.variance,u=l.reduce((g,w)=>g+w,0),h,p,d;if(c){let g=s.grandMean,w=0,N=0;for(let v=0;v<a;v++)w+=l[v]*(f[v]-g)**2,N+=(l[v]-1)*m[v];p=u-a,Co(p,s.ssWithin),h=w/(a-1)/(N/p)}else if(m.some(g=>!(g>=D0))&&(d=V0(o))!==void 0){let g=d.map((y,R)=>Gf(l[R]/y.variance,-2*y.k)),w=Math.max(...g.map(y=>y.e)),N=g.map(y=>et(y.m,y.e-w)),v=N.reduce((y,R)=>y+R,0),E=0;for(let y=0;y<a;y++){let R=N.reduce((T,S,k)=>k===y?T:T+S,0);E+=(R/v)**2/(l[y]-1)}E/=a*a-1;let b=[];for(let y=0;y<a;y++)for(let R=y+1;R<a;R++){let[T,S]=d[y].k>=d[R].k?[d[y],d[R]]:[d[R],d[y]],k=T.mean-et(S.mean,S.k-T.k);if(k===0)continue;let L=Gf(k,T.k);b.push({m:g[y].m*g[R].m*L.m*L.m/v,e:g[y].e+g[R].e+2*L.e-w})}let M=Math.max(...b.map(y=>y.e)),x=b.reduce((y,R)=>y+et(R.m,R.e-M),0);h=(b.length===0?0:et(x,M))/((a-1)*(1+2*(a-2)*E)),p=1/(3*E)}else if(m.some(g=>g===0)){let g=m.flatMap((E,b)=>E===0?[b]:[]);if(g.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let w=g[0],N=0,v=0;for(let E=0;E<a;E++)E!==w&&(N+=1/(l[E]-1),v+=l[E]/m[E]*(f[E]-f[w])**2);N/=a*a-1,h=v/((a-1)*(1+2*(a-2)*N)),p=1/(3*N)}else{let g=l.map((b,M)=>b/m[M]),w=g.reduce((b,M)=>b+M,0),N=0,v=0;for(let b=0;b<a;b++)N+=(1-g[b]/w)**2/(l[b]-1),v+=g[b]*f[b];N/=a*a-1,v/=w;let E=0;for(let b=0;b<a;b++)E+=g[b]*(f[b]-v)**2;h=E/((a-1)*(1+2*(a-2)*N)),p=1/(3*N)}return{method:c?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:h,statisticName:"F",parameter:[a-1,p],parameterName:["num df","denom df"],pValue:wt(h,a-1,p,!1,!1),warnings:[]}}function P0(e){let t=Array.from(e).filter(o=>!Number.isNaN(o)).sort((o,s)=>o-s),n=t.length;if(n<3||n>5e3)throw new RangeError("sample size must be between 3 and 5000");if(!Number.isFinite(t[0])||!Number.isFinite(t[n-1]))throw new RangeError("'x' must be finite");if(t[n-1]===t[0])throw new RangeError("all 'x' values are identical");let r=Pf(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:Vf(r,n),warnings:[]}}function V0(e){let t=[];for(let n of e){let r=Et([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function Gf(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=et(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function yi(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=ue(r,[e[o],0]);return r[0]}function $0([[e,t],[n,r]]){let o=e+n,s=t+r,i=e+t,c=Math.max(0,i-s),a=Math.min(i,o),l=new Float64Array(a-c+1);for(let f=c;f<=a;f++)l[f-c]=gn(f,o,s,i,!1);return{lo:c,d:l}}function z0(e,t){let n=new Float64Array(e.length+t.length-1);for(let r=0;r<e.length;r++){let o=e[r];if(o!==0)for(let s=0;s<t.length;s++)n[r+s]+=o*t[s]}return n}var zs=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=$0(o);n+=s.lo,r=z0(r,s.d)}this.lo=n,this.hi=n+r.length-1,this.logNull=r.map(Math.log)}density(t){let n=this.logNull.length,r=new Float64Array(n);if(t===0||t===1/0)return r[t===0?0:n-1]=1,r;let o=Math.log(t),s=-1/0;for(let c=0;c<n;c++){let a=this.logNull[c]+(this.lo+c)*o;r[c]=a,a>s&&(s=a)}for(let c=0;c<n;c++)r[c]=Math.exp(r[c]-s);let i=yi(r,0,n);for(let c=0;c<n;c++)r[c]=r[c]/i;return r}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)r=ue(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(t,n,r){let o=this.density(n),s=t-this.lo;return r?yi(o,s,o.length):yi(o,0,s+1)}};function Mi(e,t){return tr(e,t)}function Xf(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Mi(r=>e.mean(r)-t,n>t)}function Ei(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Mi(o=>n-e.cdf(t,o,!1),r<n)}function xi(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Mi(o=>e.cdf(t,o,!0)-n,r>n)}var j0=["alternative","correct","exact","confLevel"];function K0(e,t={}){V(t,j0,"mantelhaenTest");let n=e.length;if(n<1)throw new RangeError("'x' must have at least one stratum");let r=e[0].length,o=e[0][0]?.length??0;if(r<2||o<2||n<2)throw new RangeError("each dimension in table must be >= 2");for(let f of e){if(f.length!==r||f.some(m=>m.length!==o))throw new RangeError("all strata must have the same dimensions");if(f.some(m=>m.some(u=>!Number.isFinite(u)||u<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}let s=e.map(Ri);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=Ge(t.alternative),c=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(It(l),r===2&&o===2){if(a&&e.some(m=>m.some(u=>u.some(h=>!Number.isInteger(h)))))throw new RangeError("the exact test needs integer counts");if(a&&s.reduce((m,u)=>m+u,0)>Number.MAX_SAFE_INTEGER)throw new RangeError("the exact test needs counts with a total below 2^53: use exact: false");let f=e.map(m=>[[m[0][0],m[0][1]],[m[1][0],m[1][1]]]);return a?B0(f,i,l):W0(f,s,i,c,l)}return U0(e,s,r,o)}function Ri(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Jf(e){let t=0,n=0;for(let o of e)for(let s of o)for(let i of s)t=Math.max(t,i),n++;let r=Math.floor(Math.log2(t))+1+Math.ceil(Math.log2(8*n))-1020;return r<=0?0:r+r%2}function em(e,t){return e.map(n=>n.map(r=>r.map(o=>$(o,-t))))}function Qf(e,t,n){let r=0,o=0,s=0,i=0,c=0,a=0,l=0,f=0;return e.forEach(([[m,u],[h,p]],d)=>{let g=t[d],w=m+u,N=h+p,v=m+h,E=u+p;r+=m;let b=w*v/g;o+=Number.isFinite(b)?b:w/g*v;let M=w*N*v*E/(g*g*(g-n));s+=Number.isFinite(M)?M:w/g*(N/g)*(v/(g-n))*E;let x=(m+p)/g,y=(u+h)/g,R=m*p/g,T=u*h/g,S=Number.isFinite(R)?R:m/g*p,k=Number.isFinite(T)?T:u/g*h;i+=S,c+=k,a+=x*S,l+=x*k+y*S,f+=y*k}),{sa:r,se:o,sv:s,sR:i,sS:c,sPR:a,sPSQR:l,sQS:f}}function W0(e,t,n,r,o){let s=Qf(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=Jf(e);let x=em(e,i);s=Qf(x,x.map(Ri),$(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,d=c-a,g=$(.5,-i),w=r&&Math.abs(d)>=g?g:0,N=$(Dt(Math.abs(d)-w,l),i),v=n==="two.sided"?gt(N,.5,2,!1,!1):ie(Math.sign(d)*Math.sqrt(N),{lowerTail:n==="less"}),E=f/m,b=$(Math.sqrt(u/(2*f*f)+h/(2*f*m)+p/(2*m*m)),-i/2),M;if(n==="less")M=[0,E*Math.exp(oe(o)*b)];else if(n==="greater")M=[E*Math.exp(oe(o,{lowerTail:!1})*b),1/0];else{let x=(1-o)/2,y=x<1e-8?oe(x,{lowerTail:!1}):oe((1+o)/2);M=[E*Math.exp(-y*b),E*Math.exp(y*b)]}return{method:`Mantel-Haenszel chi-squared test ${w!==0?"with":"without"} continuity correction`,alternative:n,statistic:N,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:v,confInt:M,confLevel:o,estimate:E,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var Y0=2**24;function B0(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>Y0)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new zs(e),s=e.reduce((a,l)=>a+l[0][0],0),i;if(t==="two.sided"){let a=o.density(1),l=a[s-o.lo]*(1+1e-7),f=[0,0];for(let m of a)m<=l&&(f=ue(f,[m,0]));i=Math.min(1,f[0])}else i=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,Ei(o,s,1-n)];else if(t==="greater")c=[xi(o,s,1-n),1/0];else{let a=(1-n)/2;c=[xi(o,s,a),Ei(o,s,a)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:s,statisticName:"S",pValue:i,confInt:c,confLevel:n,estimate:Xf(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function U0(e,t,n,r){let{diff:o,V:s}=Zf(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=Jf(e);let f=em(e,i);({diff:o,V:s}=Zf(f,f.map(Ri),n,r,$(1,-i)))}let c=o.length,a=rt(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*a[f];return l=$(l,i),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:gt(l,c/2,2,!1,!1),warnings:[]}}var G0=2**-1022;function Zf(e,t,n,r,o){let s=(n-1)*(r-1),i=new Array(s).fill(0),c=Array.from({length:s},()=>new Array(s).fill(0));return e.forEach((a,l)=>{let f=t[l],m=a.map(p=>p.reduce((d,g)=>d+g,0)),u=Array.from({length:r},(p,d)=>a.reduce((g,w)=>g+w[d],0)),h=1/(f*f*(f-o));for(let p=0;p<r-1;p++)for(let d=0;d<n-1;d++){let g=d+(n-1)*p,w=m[d]*u[p]/f;i[g]+=a[d][p]-(Number.isFinite(w)?w:os(m[d],u[p],f));for(let N=0;N<r-1;N++)for(let v=0;v<n-1;v++){let E=(d===v?f*m[d]:0)-m[d]*m[v],b=(p===N?f*u[p]:0)-u[p]*u[N],M=h*E*b;c[g][v+(n-1)*N]+=Number.isFinite(M)&&h>=G0?M:m[d]*((d===v?1:0)-m[v]/f)*(((p===N?1:0)-u[N]/f)*(u[p]/(f-o)))}}}),{diff:i,V:c}}var tm=new Map,H0=2**24,X0=2**32;function rm(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>H0||n*r>X0)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function Q0(e,t){let n=`${e},${t}`,r=tm.get(n);if(r!==void 0)return r;let o=e+t,s=[];for(let u=1;u<=o;u++)s.push(Math.min(u,o+1-u));let i=s.reduce((u,h)=>u+h,0),c=Array.from({length:e+1},()=>new Float64Array(i+1));c[0][0]=1;for(let u=0;u<o;u++){let h=o-u,p=s[u],d=Array.from({length:e+1},()=>new Float64Array(i+1));for(let g=0;g<=e;g++){let w=c[g],N=e-g;if(N>h)continue;let v=N/h;for(let E=0;E<=i;E++){let b=w[E];b!==0&&(N>0&&(d[g+1][E+p]+=b*v),N<h&&(d[g][E]+=b*(1-v)))}}c=d}let a=c[e],l=0;for(;l<a.length&&a[l]===0;)l++;let f=a.length-1;for(;f>l&&a[f]===0;)f--;let m={min:l,probs:a.slice(l,f+1)};return tm.set(n,m),m}function Ti(e,t,n,r=!0){if(Number.isNaN(e)||!(t>=1)||!(n>=1)||t!==Math.floor(t)||n!==Math.floor(n))return NaN;let{min:o,probs:s}=Q0(t,n),i=Oc(e)-o,c=0;if(r)for(let a=0;a<=Math.min(i,s.length-1);a++)c+=s[a];else for(let a=Math.max(i+1,0);a<s.length;a++)c+=s[a];return Math.min(c,1)}var nm=new Map;function Z0(e,t){let n=`${e},${t}`,r=nm.get(n);if(r!==void 0)return r;let o=e+t,s=0;for(let m=1;m<=o;m++)s+=Math.min(m,o+1-m);let i=Array.from({length:e+1},()=>new Float64Array(s+1));i[0][0]=1;for(let m=0;m<o;m++){let u=Math.min(m+1,o-m),h=Array.from({length:e+1},()=>new Float64Array(s+1));for(let p=0;p<=e;p++){if(e-p>o-m)continue;let d=i[p];for(let g=0;g<=s;g++){let w=d[g];w!==0&&(p<e&&(h[p+1][g+u]+=w),e-p<o-m&&(h[p][g]+=w))}}i=h}let c=i[e],a=0;for(;c[a]===0;)a++;let l=c.length-1;for(;c[l]===0;)l--;let f={min:a,counts:c.slice(a,l+1)};return nm.set(n,f),f}function js(e,t,n){let{min:r,counts:o}=Z0(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=zc(t+n,t),i=0;for(let c=0;c<o.length;c++)if(i+=o[c]/s,i>=e)return r+c;return r+o.length-1}var J0=["alternative","exact","confInt","confLevel"];function im(e,t,n={}){if(V(n,J0,"ansariTest"),typeof e=="string"){let b=Qn(e,t);return im(b.x,b.y,n)}let r=Array.from(e).filter(b=>!Number.isNaN(b)),o=Array.from(t).filter(b=>!Number.isNaN(b)),s=r.length,i=o.length;if(s<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let c=Ge(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&It(l);let f=s+i,m=bt(r.concat(o)),u=m.map(b=>Math.min(b,f-b+1)),h=0;for(let b=0;b<s;b++)h+=u[b];let p=new Set(m).size<f,d=n.exact??(s<50&&i<50&&!p),g=n.exact??(s<50&&i<50),w=new mt,N,v;if(d&&!p){rm(s,i);let b=x=>Ti(x-1,s,i,!1),M=x=>Ti(x,s,i,!0);if(c==="two.sided"){let x=Math.floor((s+1)**2/4)+Math.floor(s*i/2)/2;N=Math.min(1,2*(h>x?b(h):M(h)))}else c==="less"?N=b(h):N=M(h);a&&(v=rg(r,o,c,l,w))}else{let b=cm(h,u,p,s,i);N=c==="two.sided"?2*Math.min(ie(b),ie(b,{lowerTail:!1})):c==="less"?ie(b,{lowerTail:!1}):ie(b),a&&(v=og(r,o,c,l,g?new mt:w)),g&&p&&(w.add("cannot compute exact p-value with ties"),a&&w.add("cannot compute exact confidence interval with ties"))}let E={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:N,warnings:w.list};return v!==void 0&&(E.confInt=v.confInt,E.confLevel=l,E.estimate=v.estimate,E.estimateNames="ratio of scales"),E.nullValue=1,E.nullValueName="ratio of scales",H("ansariTest",E)}function cm(e,t,n,r,o){let s=r+o;if(!n){let l=s%2===0,f=l?e-r*(s+2)/4:e-r*(s+1)**2/(4*s),m=Math.sqrt(l?r*o*(s+2)*(s-2)/(48*(s-1)):r*o*(s+1)*(3+s*s)/(48*s*s));return f/m}let i=0;for(let l of t)i+=l;let c=i/s,a=0;for(let l of t)a+=(l-c)**2;return(e-r*c)/Math.sqrt(r*o*(a/(s-1))/s)}function lm(e,t,n){let r=e.length,o=r+t.length,s=e.map(f=>f/n),c=n>0&&n<1/0&&s.some((f,m)=>Number.isFinite(e[m])&&(!Number.isFinite(f)||e[m]!==0&&Math.abs(f)<eg))?tg(e.map(f=>om(f,n)).concat(t.map(f=>om(f,1)))):ng(s.concat(t)),a=c.map(f=>Math.min(f,o-f+1)),l=0;for(let f=0;f<r;f++)l+=a[f];return{ab:l,a,ties:new Set(c).size<o}}var eg=2**-1022;function om(e,t){if(e===0)return[0,0,0];let n=e>0?1:-1;if(!Number.isFinite(e))return[n,1/0,1];let[r,o]=sm(Math.abs(e)),[s,i]=sm(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function sm(e){let t=Math.floor(Math.log2(e)),n=$(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function am(e,t){if(e[0]!==t[0])return e[0]-t[0];let n=e[1]!==t[1]?e[1]-t[1]:e[2]-t[2];return e[0]*n}function tg(e){let t=e.map((r,o)=>o).sort((r,o)=>am(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&am(e[t[r]],e[t[o]])===0;)o++;for(let s=r;s<o;s++)n[t[s]]=(r+1+o)/2;r=o}return n}function ng(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return bt(e);let n=bt(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function Ks(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function rg(e,t,n,r,o){let s=e.length,i=t.length,c=[];for(let w of e)for(let N of t)w/N>=0&&c.push(w/N);c.sort((w,N)=>w-N);let a=c.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:Ks(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((w,N)=>lm(e,t,w+(N<a-1?(c[N+1]-w)/2:w*1.01)).ab),f=(w,N,v)=>{let E=1/0;for(let M of l)(N?M-w>0:M-w>=0)&&M-w<E&&(E=M-w);let b=v?-1/0:1/0;return l.forEach((M,x)=>{M-w===E&&(b=v?Math.max(b,c[x]):Math.min(b,c[x]))}),b},m=w=>{let N=js(w/2,s,i),v=js(1-w/2,s,i);if(!l.some(M=>M-N>=0)||!l.some(M=>M-v>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=f(N,!1,!1),b=f(v,!0,!0);return E>b?[f(v,!1,!1),f(N,!0,!0)]:[E,b]},u=Ks(n,1-r,m),h=js(.5,s,i),p=-1/0,d=1/0;l.forEach((w,N)=>{w-h<=0?p=Math.max(p,c[N]):d=Math.min(d,c[N])});let g=p===-1/0||d===1/0?NaN:(d+p)/2;return g===1/0&&(g=d/2+p/2),{confInt:u,estimate:g}}function og(e,t,n,r,o){let s=e.length,i=t.length,c=(w,N)=>{let{ab:v,a:E,ties:b}=lm(e,t,w);return cm(v,E,b,s,i)-N},a=[],l=(w,N)=>{w.length===0||N.length===0||a.push(sn(w)/At(N),At(w)/sn(N))};if(l(e.filter(w=>w>0),t.filter(w=>w>0)),l(e.filter(w=>w<=0),t.filter(w=>w<0)),a.some(w=>!Number.isFinite(w))){o.add("cannot compute asymptotic confidence set or estimator");return}if(a.length===0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),{confInt:Ks(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=sn(a),m=At(a),u=w=>Kt(N=>c(N,w),f,m,{tol:1e-4}),h=w=>{let N=oe(w/2),v=oe(w/2,{lowerTail:!1});if(c(f,N)>0||c(m,v)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=u(N),b=u(v);return E<=b?[E,b]:[b,E]},p=Ks(n,1-r,h),d=c(f,0)>0||c(m,0)<0;d&&o.add("cannot compute estimate, returning NA");let g=d?NaN:u(0);return{confInt:p,estimate:g}}var sg=["alternative","confLevel"];function ag(e,t,n={}){V(n,sg,"brunnerMunzelTest");let r=Array.from(e).filter(y=>!Number.isNaN(y)),o=Array.from(t).filter(y=>!Number.isNaN(y)),s=r.length,i=o.length;if(s<2)throw new RangeError("not enough 'x' observations");if(i<2)throw new RangeError("not enough 'y' observations");let c=Ge(n.alternative),a=n.confLevel??.95;an(a);let l=bt(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=y=>y.reduce((R,T)=>R+T,0)/y.length,h=u(f),p=u(m),d=(y,R,T)=>{let S=(y.length+1)/2,k=0;for(let L=0;L<y.length;L++)k+=(y[L]-R[L]-T+S)**2;return k/(y.length-1)},g=d(f,bt(r),h),w=d(m,bt(o),p),N=(p-(i+1)/2)/s,v=s*g+i*w,E=s*i*(p-h)/(s+i)/Math.sqrt(v),b=v**2/((s*g)**2/(s-1)+(i*w)**2/(i-1)),M;c==="greater"?M=vt(E,b,!0,!1):c==="less"?M=vt(E,b,!1,!1):M=2*vt(-Math.abs(E),b,!0,!1);let x=xt((1-a)/2,b,!1,!1)*Math.sqrt(g/(s*i**2)+w/(i*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:E,statisticName:"Brunner-Munzel Test Statistic",parameter:b,parameterName:"df",pValue:M,confInt:[N-x,N+x],confLevel:a,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var ig=[1.2337005501361697,7831619385924639e-32];function cg(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let d=f[h*t+p];if(d!==0)for(let g=0;g<t;g++)u[h*t+g]+=d*m[p*t+g]}return u},o=f=>{let m=0;for(let p of f)m=Math.max(m,Math.abs(p));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let p=0;p<f.length;p++)f[p]*=h;return u},s,i=0,c=e.slice(),a=0,l=n;for(;l>0;)l&1&&(s=s===void 0?c.slice():r(s,c),i+=a,i+=o(s)),l>>=1,l>0&&(c=r(c,c),a*=2,a+=o(c));return[s,i]}var lg=2**33;function mm(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e)||t<=1/(2*e)||t>=1||e*t<1)return;let n=2*Math.floor(e*t)+1,r=e.toString(2),o=Math.max(1,r.length-1+(r.split("1").length-2));if(n**3*o>lg)throw new RangeError(`the exact distribution of D for n = ${e} needs powers of a ${n} x ${n} matrix, too much work: use exact: false`)}function um(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=1/(2*e))return 0;if(t>=1)return 1;if(e*t<1){let[f,m]=Ht(e,t),u=2*f-1+2*m,h=1,p=0;for(let d=1;d<=e;d++)h*=d/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,s=new Float64Array(r*r);for(let f=0;f<r;f++)for(let m=0;m<r;m++)s[f*r+m]=f-m+1>=0?1:0;for(let f=0;f<r;f++)s[f*r]-=Math.pow(o,f+1),s[(r-1)*r+f]-=Math.pow(o,r-f);2*o-1>0&&(s[(r-1)*r]+=Math.pow(2*o-1,r));for(let f=0;f<r;f++)for(let m=0;m<r;m++){let u=f-m+1;if(u>0){let h=1;for(let p=2;p<=u;p++)h*=p;s[f*r+m]/=h}}let[i,c]=cg(s,r,e),a=i[(n-1)*r+n-1],l=c;for(let f=1;f<=e;f++)a=a*f/e,Math.abs(a)<2**-500&&(a*=2**500,l-=500);for(;l>1e3;)a*=2**1e3,l-=1e3;for(;l<-1e3;)a*=2**-1e3,l+=1e3;return a*2**l}function Si(e,t=!0){if(Number.isNaN(e))return NaN;if(e<=0)return t?0:1;if(e===1/0)return t?1:0;if(e<1){let o=lt(ig,Ht(e,e)),s=o[0]+o[1],i=Math.exp(-o[0])*(1-o[1]),c=0;for(let l=2;l<100;l++){let f=Math.exp(-(2*l-1)*(2*l-1)*s);if(c+=f,f<=1e-17*(c+i))break}if(i+c===0)return t?0:1;let a=Math.sqrt(2*Math.PI)/e*(i+c);return t?a:1-a}let n=0;for(let o=1;o<100;o++){let s=Math.exp(-2*o*o*e*e);if(n+=o%2===1?s:-s,s<=1e-17*Math.abs(n))break}let r=2*n;return t?1-r:r}function Ai(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=0)return 1;if(t>1)return 0;let[n,r]=Ht(e,t),o=Math.floor(e-n-r),s=0;for(let i=0;i<=o;i++){let c=ue($t(e-i,-n),[-r,0]);if(c[0]<=0)break;let a=ue($t(i,n),[r,0]),l=lt(a,[e,0]),f=lt(c,[e,0]);s+=Cc(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var fm=2**28;function hm(e,t){let[n,r]=Ht(e,t),o=Math.floor(n);o===n&&r<0&&(o-=1);let s=[];for(let h=0;h<=e;h++){let p=(h-n-r)/e;p>0&&p<1&&s.push({t:p,up:!0,i:h});let d=(h+n+r)/e;d>0&&d<1&&s.push({t:d,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>fm)return NaN;s.sort((h,p)=>h.t-p.t);let i=o+1,c=new Float64Array(e+1),a=new Float64Array(e+1);c[0]=1;let l=0,f=0,m=0,u=0;for(let h of s){let p=(h.t-f)/(1-f);if(p>0){let d=p/(1-p);a.fill(0);for(let g=l;g<i;g++){let w=c[g];if(w===0)continue;let N=e-g,v=Math.min(N,Math.floor((N+1)*p)),E=i-g,b=fn(v,N,p,!1);for(let M=v;M<=N&&b!==0;M++)M>v&&(b=b*(N-M+1)*d/M),M>=E?m+=w*b:a[g+M]+=w*b,u++;b=v>0?fn(v-1,N,p,!1):0;for(let M=v-1;M>=0&&b!==0;M--)M<v-1&&(b=b*(M+1)/((N-M)*d)),M>=E?m+=w*b:a[g+M]+=w*b,u++}if(u>2*fm)return NaN;[c,a]=[a,c]}if(f=h.t,h.up)i=h.i+1;else{for(let d=l;d<=h.i;d++)m+=c[d],c[d]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function pm(e,t,n,r={}){let o=r.alternative??"two.sided";if(Number.isNaN(e)||!(t>=1)||!(n>=1)||t!==Math.floor(t)||n!==Math.floor(n))return NaN;if(e<=0)return 1;if(e>1)return 0;let s=t+n,i;if(r.z!==void 0){if(r.z.length!==s)throw new RangeError("z must have length m + n");let u=[...r.z].sort((h,p)=>h-p);i=new Array(s+1).fill(!1);for(let h=1;h<s;h++)i[h]=u[h]!==u[h-1]}let c=e*t*n*(1-1e-10),a=(u,h)=>{let p=u*n-h*t;return(o==="two.sided"?Math.abs(p):o==="greater"?p:-p)>=c},l=new Float64Array(n+1),f=new Float64Array(n+1),m=0;for(let u=0;u<=t;u++){for(let h=0;h<=n;h++){if(u===0&&h===0){f[0]=1;continue}let p=u+h,d=(u>0?u/p*l[h]:0)+(h>0?h/p*f[h-1]:0);(i===void 0?p<s:i[p])&&a(u,h)?(d>0&&(m+=d*gn(u,t,n,p,!1)),f[h]=0):f[h]=d}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var fg=["alternative","exact"];function dm(e,t){return e==="two.sided"?"two-sided":`the CDF of x lies ${e==="less"?"below":"above"} ${t?"the null hypothesis":"that of y"}`}function gm(e,t,n={}){if(V(n,fg,"ksTest"),typeof e=="string"){let N=Qn(e,t);return gm(N.x,N.y,n)}let r=Array.from(e).filter(N=>!Number.isNaN(N)).sort((N,v)=>N-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=Ge(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,v=-1/0;for(let y=0;y<o;y++){let R=t(r[y]);if(!(R>=0&&R<=1))throw new RangeError(`the distribution function returned ${R} at ${r[y]}, not a value in [0, 1]`);N=Math.max(N,(y+1)/o-R),v=Math.max(v,R-y/o)}let E=s==="two.sided"?Math.max(N,v):s==="greater"?N:v,b=new Set(r).size<o,M=n.exact??(o<100&&!b),x;if(M&&s==="two.sided"&&E<.5&&mm(o,E),s==="two.sided")if(!M)x=Si(Math.sqrt(o)*E,!1);else{let y=2*Ai(o,E);if(E>=.5)x=Math.min(1,y);else{let R=y<.01?hm(o,E):NaN;x=Number.isNaN(R)?1-um(o,E):R}}else x=M?Ai(o,E):Math.exp(-2*o*E*E);return H("ksTest",{method:`${M?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:dm(s,!0),exact:M,statistic:E,statisticName:i,pValue:Math.min(1,Math.max(0,x)),warnings:b?["ties should not be present for the one-sample Kolmogorov-Smirnov test"]:[]})}let c=Array.from(t).filter(N=>!Number.isNaN(N)),a=c.length;if(a<1)throw new RangeError("not enough 'y' data");let l=r.map(N=>[N,0]).concat(c.map(N=>[N,1]));l.sort((N,v)=>N[0]-v[0]);let f=0,m=0,u=0,h=0;for(let N=0;N<l.length;N++){if(l[N][1]===0?f++:m++,N+1<l.length&&l[N+1][0]===l[N][0])continue;let v=f/o-m/a;u=Math.max(u,v),h=Math.max(h,-v)}let p=s==="two.sided"?Math.max(u,h):s==="greater"?u:h,d=new Set(l.map(N=>N[0])).size<l.length,g=n.exact??o*a<1e4,w;if(g)w=pm(p,o,a,d?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*a/(o+a);w=s==="two.sided"?Si(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return H("ksTest",{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:dm(s,!1),exact:g,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,w)),warnings:d&&!g?["p-value will be approximate in the presence of ties"]:[]})}var mg=["poolSd","paired","pAdjustMethod","alternative"],ug=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],hg=["pAdjustMethod","correct"];function wm(e,t,n){let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=new Map,s=en(t);for(let c=0;c<r.length;c++){let a=s[c];if(typeof a=="number"&&Number.isNaN(a)||n&&Number.isNaN(r[c]))continue;let l=o.get(a);l===void 0&&o.set(a,l=[]),l.push(r[c])}let i=Array.from(o.keys()).sort(Vn);return{levels:i,groups:i.map(c=>o.get(c))}}function ki(e,t,n){let r=[],o=[];for(let c=0;c<e-1;c++)for(let a=c+1;a<e;a++)r.push(t(a,c)),o.push([a-1,c]);let s=Yr(r,n),i=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([c,a],l)=>i[c][a]=s[l]),i}function qi(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(Rt),colNames:t.slice(0,-1).map(Rt),pAdjustMethod:r,warnings:o}}function pg(e,t,n={}){V(n,mg,"pairwiseTTest");let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let s=Ge(n.alternative),i=n.pAdjustMethod??"holm",{levels:c,groups:a}=wm(e,t,!r),l=c.length;if(l<2)throw new RangeError("there must be at least two groups");let f,m;if(o){m="t tests with pooled SD";let u=a.map(w=>w.length),h=Et(a),p=h.mean,d=0;for(let w of u)w>1&&(d+=w-1);Co(d,h.ssWithin);let g=Math.sqrt(h.ssWithin/d);f=(w,N)=>{let v=(p[w]-p[N])/(g*Math.sqrt(1/u[w]+1/u[N]));return s==="less"?vt(v,d,!0,!1):s==="greater"?vt(v,d,!1,!1):2*vt(-Math.abs(v),d,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>je(()=>vn(a[u],a[h],{paired:r,alternative:s})).pValue;return qi(m,c,ki(l,f,i),i)}function dg(e,t,n={}){V(n,ug,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=wm(e,t,!(s.paired??!1)),l=c.length;if(l<2)throw new RangeError("there must be at least two groups");let f="",m=new mt,u=ki(l,(h,p)=>{let d=je(()=>Ao(a[h],a[p],{...s,...o===void 0?{}:{alternative:o}}));f=d.method;for(let g of d.warnings)m.add(g);return d.pValue},i);return H("pairwiseWilcoxTest",qi(f,c,u,i,m.list))}function gg(e,t,n={}){if(V(n,hg,"pairwisePropTest"),e.length!==t.length)throw new RangeError("'x' and 'n' must have the same length");let r=[],o=[];e.forEach((m,u)=>{Number.isNaN(m)||Number.isNaN(t[u])||(r.push(m),o.push(t[u]))});let s=r.length;if(s<2)throw new RangeError("too few groups");let i=n.pAdjustMethod??"holm",c=n.correct??!0,a=new mt,l=ki(s,(m,u)=>{let h=je(()=>Ga([r[m],r[u]],[o[m],o[u]],{correct:c}));for(let p of h.warnings)a.add(p);return h.pValue},i),f=Array.from({length:s},(m,u)=>u+1);return H("pairwisePropTest",qi("Pairwise comparison of proportions",f,l,i,a.list))}var wg=1e-14;function bg(e,t,n,r,o,s,i,c){let a=M=>$(M,2*i),l=t.length,f=n.flatMap((M,x)=>Ns(M)[0]>wg*s[x]?[x]:[]),m=0,u=[],h=t.slice(),p=new Array(l).fill(0);if(f.length>0){let M=jt(Array.from({length:l},(y,R)=>f.map(T=>n[T][R][0])));m=M.rank,u=M.pivot.slice(0,m).map(y=>f[y]);let x=vs(u.map(y=>n[y]),t);h=x.qty,u.forEach((y,R)=>{for(let T=0;T<l;T++)p[T]=p[T]+n[y][T][0]*x.coef[R][0]})}let d=[];u.forEach((M,x)=>{let y=r[M],R=d.find(T=>T.term===y);R===void 0&&d.push(R={term:y,df:0,ss:[0,0]}),R.df++,R.ss=ue(R.ss,_e(h[x],h[x]))});let g=l-m,w=Ns(h,m)[0],N=si(p,w,g),v=d.filter(M=>M.term!==0);g>0&&yr(c,w)&&(w=0,v=v.map(M=>yr(c,M.ss[0])?{...M,ss:[0,0]}:M));let E=v.map(M=>o[M.term-1]),b=M=>{if(g>0){let x=w/g,y=v.map(({df:R,ss:T})=>{let S=T[0]/R/x;return[R,M(T[0]),M(T[0]/R),S,wt(S,R,g,!1,!1)]});return y.push([g,M(w),M(x),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...E,"Residuals"],table:y}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:E,table:v.map(({df:x,ss:y})=>[x,M(y[0]),M(y[0]/x)])}};return[b(a),b(M=>M),N]}function bm(e,t,n,r,o,s){if(s.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let i=n??{type:"num",value:1,text:"1"},c=jr({response:t.response,rhs:i},Object.keys(o)).variables,a=gr(t,o,{...s,dropUnusedLevels:!0,contrastVariables:c},i),l=a.y;if(l===void 0)throw new TypeError("the formula has no response");if(l.some(I=>!Number.isFinite(I)))throw new RangeError("NA/NaN/Inf in 'y'");let f=gr({response:t.response,rhs:r},o,{...s.levels===void 0?{}:{levels:s.levels},allContrasts:"contr.helmert",intercept:a.terms.intercept,extraVariables:a.terms.variables,dropUnusedLevels:!0}),m=l.length,u=[...a.warnings,...f.warnings.filter(I=>!a.warnings.includes(I))],h=f.colNames.length,p=f.colNames.map((I,F)=>Re([f.x.map(C=>C[F])]).groups[0]),d=f.x.map((I,F)=>p.map(C=>C[F])),g=h>0?jt(d):void 0,w=g?.rank??0;w<h&&u.push("Error() model is singular");let N=g?.pivot.slice(0,w)??[],v=I=>I.map(F=>[F,0]),E=a.colNames.map((I,F)=>v(Re([a.x.map(C=>C[F])]).groups[0])),{groups:b,k:M}=Re([l]),{fit:x,qtv:y}=Jn(N.map(I=>v(p[I])),v(b[0]),E),R=b[0].map(I=>[1,I]),T=E.map(I=>Ns(I)[0]),S=[];N.forEach((I,F)=>{let C=f.assign[I],D=C===0?"(Intercept)":f.terms.termLabels[C-1],A=S.find(O=>O.name===D);A===void 0&&S.push(A={name:D,idx:[]}),A.idx.push(F)}),m>w&&S.push({name:"Within",idx:Array.from({length:m-w},(I,F)=>w+F)});let k=S.map(I=>bg(I.name,I.idx.map(F=>x.qty[F]),y.map(F=>I.idx.map(C=>F[C])),a.assign,a.terms.termLabels,T,M,R)),L=k.map(I=>I[0]),_={formula:e,multistratum:!0,strata:L,rows:a.frame.rows,warnings:u};return Wt.set(_,k.map(I=>I[1])),k.some((I,F)=>I[2]&&S[F].name!=="(Intercept)")&&ai.set(_,[oi]),_}var vg=["weights","contrasts","levels"],vm=["confLevel"],Nm=["sigmaX","sigmaY","mu","alternative","confLevel"];function Ng(e,t,n={}){return V(n,vg,"aov"),typeof e=="string"?H("aov",ym(e,t,n)):H("aov",yg(e,t))}function ym(e,t,n){let r=Xn(e,!0),{treatment:o,errors:s}=Jc(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?bm(e,r,o,s[0],t,n):fl(e,gr(r,t,{...n,dropUnusedLevels:!0},o),!0)}function yg(e,t){let{labels:n,groups:r}=Zn(e,t);if(r.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=r.flat(),s=r.flatMap((i,c)=>i.map(()=>n[c]));return ym("y ~ g",{y:o,g:s},{levels:{g:n}})}function Mm(e){let t=e.length;if(t<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let n=e.reduce((u,h)=>u+h.length,0),{ssBetween:r,ssWithin:o,k:s}=Et(e),i=t-1,c=n-t,a=r/i,l=o/c,f=a/l,m=u=>$(u,2*s);return{df:[i,c],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:c>0?wt(f,i,c,!1,!1):NaN}}function Mg(e,t,n={}){at(t)&&V(t,vm,"TukeyHSD"),V(n,vm,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=Zn(e,r),i=s.length;if(i<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let c=n.confLevel??.95;an(c);let l=s.reduce((w,N)=>w+N.length,0)-i;if(l===0)throw new RangeError("no residual degrees of freedom");if(l<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let{ssWithin:f,mean:m,k:u}=Et(s),h=f/l,p=Yc(c,i,l),d=[],g={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:d,pAdjusted:d,warnings:[]};for(let w=0;w<i-1;w++)for(let N=w+1;N<i;N++){let v=m[N]-m[w],E=Math.sqrt(h/2*(1/s[N].length+1/s[w].length));g.names.push(`${o[N]}-${o[w]}`),g.i.push(N+1),g.j.push(w+1),g.diff.push($(v,u)),g.lwr.push($(v-p*E,u)),g.upr.push($(v+p*E,u)),g.pAdj.push(as(Math.abs(v)/E,i,l,{lowerTail:!1}))}return g}function Eg(e,t,n={}){at(t)&&V(t,Nm,"zTest"),V(n,Nm,"zTest");let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:s}=n,i=n.mu??0;if(o===void 0&&r===void 0)throw new RangeError("sigmaX (the known standard deviation of x) is required");if(o===void 0||r!==void 0&&s===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");for(let T of[o,s])if(T!==void 0&&!(T>0&&T<1/0))throw new RangeError("sigmaX and sigmaY must be positive finite numbers");if(Number.isNaN(i))throw new RangeError("mu must be a number");let c=Ge(n.alternative),a=n.confLevel??.95;if(!(a>=0&&a<=1))throw new RangeError("confLevel must be between 0 and 1");let l=Array.from(e).filter(T=>!Number.isNaN(T)),f=r===void 0?void 0:Array.from(r).filter(T=>!Number.isNaN(T));if(l.length<3)throw new RangeError("x needs at least 3 non-missing values");if(f!==void 0&&f.length<3)throw new RangeError("y needs at least 3 non-missing values");let m=Re([l,f??[],[o,s??0]]),u=m.k,[h,p,[d,g]]=m.groups,w=Bn(h)[0],N=f===void 0?NaN:Bn(p)[0],v=f===void 0?d/Math.sqrt(l.length):Math.sqrt(d**2/l.length+g**2/f.length),E=f===void 0?w:w-N,b=(E-$(i,-u))/v,M,x;if(c==="less")M=ie(b),x=[-1/0,E+oe(a)*v];else if(c==="greater")M=ie(b,{lowerTail:!1}),x=[E-oe(a)*v,1/0];else{M=2*ie(-Math.abs(b));let T=(1-a)/2,S=(T<1e-8?oe(T,{lowerTail:!1}):oe(1-(1-a)/2))*v;x=[E-S,E+S]}x=[$(x[0],u),$(x[1],u)];let y=f!==void 0,R=(T,S)=>{if(!(u>0&&T.some((L,_)=>L!==0&&Math.abs(S[_])<2**-1022)))return $(Bn(S)[0],u);let k=Math.max(Re([T]).k,0);return $(Bn(k===0?T:T.map(L=>$(L,-k)))[0],k)};return{method:y?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:b,statisticName:"z",pValue:M,confInt:x,confLevel:a,estimate:y?[R(l,h),R(f,p)]:R(l,h),estimateNames:y?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:y?"difference in means":"mean",warnings:[]}}var xg=["parm","level","test"],Em={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Ii(e,t,n,r,o=1e-11){let s=[];for(let d=0;d<t.length;d++)n[d]>0&&s.push(d);let i=0,c=0;for(let d of s)i+=n[d],c+=n[d]*t[d];if(r&&!(Number.isFinite(i)&&Number.isFinite(c))){let d=0;for(let g of s)d=Math.max(d,n[g]);if(Number.isFinite(d)){let g=d>1?Math.floor(Math.log2(d)):0;i=0,c=0;for(let w of s){let N=$(n[w],-g);i+=N,c+=N*t[w]}}}let a=r?c/i:0,l=0;for(let d of s)l+=n[d]*(t[d]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(d=>Math.sqrt(n[d])),u=jt(s.map((d,g)=>e[d].map(w=>w*m[g])),{tol:o}),h=ds(u,s.map((d,g)=>t[d]*m[g])),p=0;for(let d of h)p+=d*d;if(!Number.isFinite(l)){let d=s.map((w,N)=>m[N]*(t[w]-a)),g=0;for(let w of d)g=Math.max(g,Math.abs(w));for(let w of h)g=Math.max(g,Math.abs(w));if(Number.isFinite(g)){let w=g>1?Math.floor(Math.log2(g)):0,N=0;for(let E of d)N+=$(E,-w)**2;let v=0;for(let E of h)v+=$(E,-w)**2;return $($(N-v,w),w)}}return l-p}function Rg(e,t={}){V(t,xg,"glmConfint");let{fit:n,k:r}=ys(e),o=t.level??.95,s=t.test??"LRT";if(s!=="LRT"&&s!=="Rao")throw new RangeError(`invalid 'test': ${String(s)}`);if(!(o>0&&o<1))throw new RangeError("'level' must be in (0, 1)");let i=n.coefficients.length,c=t.parm??Array.from({length:i},(_,I)=>I);for(let _ of c)if(!Number.isInteger(_)||_<0||_>=i)throw new RangeError(`invalid coefficient index ${_}`);let a=n.model,l=n.coefficients.map((_,I)=>I).filter(_=>!Number.isNaN(n.coefficients[_])),f=new mt,m=br(a,l,f,{...Em,start:a.start===void 0?void 0:l.map(_=>a.start[_])}),u=new Array(i).fill(NaN);if(l.forEach((_,I)=>u[_]=m.fit.coefficients[I]),!m.fit.converged)return{lower:c.map(()=>NaN),upper:c.map(()=>NaN)};let h=!1,p={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},d=je(()=>ya(p)),g=d.dispersion,w=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((_,I)=>w[l[_]]=d.stdError[I]);let v=a.x.map(_=>l.map(I=>_[I])),E=m.fit.eta,b=(_,I)=>{let F=l.filter(A=>A!==_),C=br(a,F,new mt,{...Em,start:void 0,etastart:E,extraOffset:a.x.map(A=>A[_]*I)}).fit;if(!C.converged)return h=!0,NaN;let D=s==="LRT"?C.deviance-m.fit.deviance:Ii(v,C.workingResiduals,C.workingWeights,!1);return Math.sign(I-u[_])*Math.sqrt(Math.max(D/g,0))},M=(1-o)/2,x=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),y=2**-9,R=x<y,T=R?[-x,x]:[oe(M),M<1e-8?oe(M,{lowerTail:!1}):oe((1+o)/2)],S=c.map(()=>NaN),k=c.map(()=>NaN),L=(_,I,F)=>{let C=F===0?-1:1;h=!1;let D=NaN,A=NaN;for(let W=0;W<=30;W++){let te=u[_]+C*2**W*w[_],U=b(_,te);if(Math.abs(U)>=Math.abs(I)){D=te,A=U;break}}if(Number.isNaN(D)||h)return NaN;let O=W=>W===u[_]?-I:b(_,W)-I,[q,P]=F===0?[D,u[_]]:[u[_],D],[B,J]=F===0?[A-I,-I]:[-I,A-I],ce;try{ce=Kt(O,q,P,{tol:1e-14*w[_],maxIter:500,fLower:B,fUpper:J})}catch(W){if(!h)throw W;ce=NaN}return h?NaN:ce};return c.forEach((_,I)=>{if(Number.isNaN(u[_]))return;if(!R){S[I]=L(_,T[0],0),k[I]=L(_,T[1],1);return}let F=[-2*y,-y,0,y,2*y],C=[L(_,-2*y,0),L(_,-y,0),u[_],L(_,y,1),L(_,2*y,1)].map(A=>A-u[_]),D=A=>{let O=0;for(let q=0;q<5;q++){let P=1;for(let B=0;B<5;B++)B!==q&&(P*=(A-F[B])/(F[q]-F[B]));O+=P*C[q]}return u[_]+O};S[I]=D(T[0]),k[I]=D(T[1])}),{lower:S.map(_=>$(_,r)),upper:k.map(_=>$(_,r))}}var Tg=["scope","direction","k","scale","steps"],Sg=["lower","upper"],Ag=["scope","direction","k","scale","steps"],kg=["lower","upper"],qg=["scope","test","k","scale"],Ig=["scope","test","k","scale"],_g=["k","scale"],Yt=e=>"linearPredictors"in e;function Oi(e,t,n){if(typeof e!="object"||e===null||typeof e.formula!="string"||!Array.isArray(e.termLabels))throw new TypeError(`${n} needs a fit of lm or glm with a formula`);if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let r=Yt(e),o=ll(e);if(o===void 0&&((r?e.model.priorGiven.some(f=>f!==1)||e.model.hasOffset:e.weights!==void 0)||r))throw new TypeError(`${n} needs the fit as returned by lm or glm (its options are not known)`);let s=e.formula.indexOf("~"),i=e.formula.slice(0,s).trim(),c=r?e.model.intercept:e.coefNames[0]==="(Intercept)",a=Object.values(t)[0]?.length??0;return{data:t,glm:r,lhs:i,intercept:c,options:o??{},nAll:a}}function _i(e,t){return t.length===0?`${e.lhs} ~ ${e.intercept?"1":"0"}`:`${e.lhs} ~ ${t.join(" + ")}${e.intercept?"":" - 1"}`}function xm(e,t){let n=s=>s instanceof Float64Array?Float64Array.from(t,i=>s[i]):Array.isArray(s)&&s.length===e.nAll?t.map(i=>s[i]):s,r=Object.fromEntries(Object.entries(e.data).map(([s,i])=>[s,n(i)])),o={...e.options};for(let s of["weights","offset"])typeof o[s]=="object"&&o[s]!==null&&(o[s]=n(o[s]));return{data:r,options:o}}function Oo(e,t,n){let{data:r,options:o}=n===void 0?{data:e.data,options:{...e.options}}:xm(e,n),s=_i(e,t),i=je(()=>e.glm?er(s,r,o):wr(s,r,o));if(n!==void 0){let a=(e.glm?i.model.rows:i.rows).map(l=>n[l]);e.glm?i.model.rows=a:i.rows=a}return i}var Rm=e=>Yt(e)?e.model.rows:e.rows,Fi=e=>Rm(e).length,Ws=e=>Yt(e)?e.priorWeights.filter(t=>t>0).length:e.nObs,or=e=>Yt(e)?e.deviance:e.rss,Tm=e=>e.rank;function Di(e){let t=e.rss;if(t>1e-290&&t<1/0)return Math.log(t);let n=e.weights,r=[];if(e.residuals.forEach((s,i)=>{let c=n?.[i]??1;c>0&&s!==0&&r.push(Math.log(Math.abs(s))+.5*Math.log(c))}),r.length===0)return-1/0;let o=Math.max(...r);return 2*o+Math.log(r.reduce((s,i)=>s+Math.exp(2*(i-o)),0))}function Lg(e){return e.rss>1e-290&&e.rss<1/0?Math.log(e.rss):e.yScale!==void 0&&e.rssScaled!==void 0&&e.rssScaled>0?Math.log(e.rssScaled)+2*e.yScale*Math.LN2:Math.log(e.rss)}function Ys(e,t,n){let r=Fi(e),o=r-e.dfResidual;return Yt(e)?[o,e.aic+(t-2)*o]:n>0?[o,e.rss/n-r+t*o]:[o,r*(Di(e)-Math.log(r))+t*o]}function Do(e){return e.termLabels.map((t,n)=>{let r=e.variables.filter((o,s)=>(e.factors[s]?.[n]??0)!==0);return{label:t,vars:r,key:[...r].sort().join("\0")}})}function Fo(e,t,n){if(typeof e!="string")throw new TypeError(`the ${n} scope must be a formula such as "~ x1 + x2"`);let r=e.includes("~")?e:`~ ${e}`,o=Xn(r),s=c=>typeof c=="object"&&c!==null&&(c.type==="dot"||Object.values(c).some(a=>typeof a=="object"&&s(a)));if(s(o.rhs))throw new RangeError(`'.' is not supported in the ${n} scope: list its terms`);let i=jr({response:void 0,rhs:o.rhs},Object.keys(t));return i.termLabels.map((c,a)=>{let l=i.variables.filter((f,m)=>(i.factors[m]?.[a]??0)!==0);return{label:c,vars:l,key:[...l].sort().join("\0")}})}var Sm=(e,t)=>t.vars.length<e.vars.length&&t.vars.every(n=>e.vars.includes(n));function Am(e,t){return e.filter(n=>!t.some(r=>r.key===n.key)&&!e.some(r=>Sm(r,n)))}function km(e,t){let n=r=>e.some(o=>o.key===r.key);return t.filter(r=>!n(r)&&t.every(o=>!Sm(r,o)||n(o)))}function qm(e,t){if(Yt(e))return;let n=e.weights,r=0;e.y.forEach((o,s)=>r+=(n?.[s]??1)*o*o),e.rss<1e-10*r&&t.add("attempting model selection on an essentially perfect fit is nonsense")}function Ur(e,t){for(let n of t.warnings)(Yt(t)||n!=="essentially perfect fit: summary may be unreliable")&&e.add(n)}var Im=e=>e.priorWeights.filter(t=>t>0).length,Pi=e=>e.family==="binomial"||e.family==="poisson";function _m(e,t){return e.family==="gaussian"?Im(e)*Math.log(t/e.deviance):Pi(e)?t-e.deviance:(t-e.deviance)/yo(e)}function Li(e,t,n){let r=Fi(e),o=Tm(e),s=Yt(e)?NaN:Di(e),i=Yt(e)?e.aic+(t-2)*o:n>0?e.rss/n-r+t*o:r*(s-Math.log(r))+t*o;return{label:"<none>",df:NaN,sumOfSq:NaN,deviance:or(e),aic:i,rank:o,dfResidual:e.dfResidual,logDev:s,fit:e}}function Ci(e,t,n,r,o,s,i,c,a=Math.log(n),l){let f=e.fit,m=s?r-e.rank:e.rank-r,u;if(Yt(f))u=e.aic+_m(f,n)+i*(r-e.rank);else{let h=Fi(f);u=c>0?n/c-h+i*r:h*(a-Math.log(h))+i*r}return{label:t,df:m,sumOfSq:s?e.deviance-n:n-e.deviance,deviance:n,aic:u,rank:r,dfResidual:o,logDev:a,fit:l}}function Lm(e,t){Yt(e)&&!Pi(e)&&yo(e,t)}function Cm(e,t,n,r,o){qm(e,o),Lm(e,o);let s=Li(e,n,r),i=Do(e),c=[s];for(let a of t){let l=i.findIndex(m=>m.key===a.key)+1,f=e.assign.flatMap((m,u)=>m===l?[]:[u]);if(Yt(e)){let m=new mt,u=br(e.model,f,m);for(let h of m.list)o.add(h);c.push(Ci(s,a.label,u.fit.deviance,u.fit.rank,Im(e)-u.fit.rank,!1,n,r))}else{let m=ml(e.x.map(u=>f.map(h=>u[h])),e.y,e.weights,e.coefNames[0]==="(Intercept)"&&f[0]===0,!0);c.push(Ci(s,a.label,m.rss,m.result.rank,m.result.dfResidual,!1,n,r,Lg(m)))}}return c}function Om(e,t,n,r,o,s){let i=Rm(t),c=t,a=[...new Set(n.flatMap(m=>m.vars))].filter(m=>!t.variables.includes(m));if(a.length>0){let m=e.options.levels,u=xm(e,i).data,h=fs(u,a,m===void 0?{}:{levels:m}).rows.map(p=>i[p]);h.length<i.length&&(s.add(`using the ${h.length}/${i.length} rows from a combined fit`),i=h,c=Oo(e,t.termLabels,i))}qm(c,s),Lm(c,s);let l=Li(c,r,o);Yt(c)&&c!==t&&(l.aic=Li(t,r,o).aic);let f=[l];for(let m of n){let u=Oo(e,[...t.termLabels,m.label],i);Ur(s,u),f.push(Ci(l,m.label,or(u),Tm(u),u.dfResidual,!0,r,o,Yt(u)?NaN:Di(u),u))}return f}function Fm(e,t,n){let r=e[0],o=r.fit,s=[NaN],i=[NaN];for(let a of e.slice(1)){let l=n?a:r,f=Math.abs(a.deviance-r.deviance),m,u;t==="F"?(m=f/a.df/(l.deviance/l.dfResidual),u=xn(m,a.df,l.dfResidual,{lowerTail:!1})):(m=Yt(o)?Math.abs(_m(o,a.deviance)):f/(l.deviance/l.dfResidual),u=Mt(m,a.df,{lowerTail:!1})),s.push(a.df===0?NaN:m),i.push(a.df===0?NaN:u)}let c=t==="F"?"F value":Yt(o)&&Pi(o)?"LRT":"scaled dev.";return{statistic:s,pValue:i,statisticName:c}}function Dm(e,t,n,r){let o={term:e.map(s=>s.label),df:e.map(s=>s.df),deviance:e.map(s=>s.deviance),aic:e.map(s=>s.aic),criterion:n>0&&!t?"Cp":"AIC",warnings:r.list};return t||(o.sumOfSq=e.map(s=>s.sumOfSq)),o}function zn(e,t,n=!1){if(typeof e!="number"||Number.isNaN(e))throw new TypeError(`${t} must be a number`);if(n?!(e>=0):!Number.isFinite(e))throw new RangeError(`${t} must be ${n?"non-negative":"finite"}`);return e}function Cg(e,t={}){V(t,_g,"extractAIC");let n=zn(t.k??2,"k"),r=zn(t.scale??0,"scale",!0);return Ys(e,n,r)}function Og(e,t,n={}){V(n,qg,"drop1");let r=Oi(e,t,"drop1"),o=zn(n.k??2,"k"),s=r.glm?0:zn(n.scale??0,"scale",!0),i=Do(e),c=n.scope===void 0?Am(i,[]):Fo(n.scope,t,"drop1").map(m=>{let u=i.find(h=>h.key===m.key);if(u===void 0)throw new RangeError("scope is not a subset of term labels");return u}),a=new mt;Ur(a,e);let l=Cm(e,c,o,s,a),f=Dm(l,r.glm,s,a);return n.test!==void 0&&n.test!=="none"&&Object.assign(f,{test:n.test==="F"?"F":"Chisq"},Fm(l,n.test==="F"?"F":"Chisq",!1)),f.warnings=a.list.filter(m=>!e.warnings.includes(m)),H("drop1",f,e)}function Fg(e,t,n={}){V(n,Ig,"add1");let r=Oi(e,t,"add1");if(n.scope===void 0)throw new TypeError("no terms in scope for adding to object");let o=zn(n.k??2,"k"),s=r.glm?0:zn(n.scale??0,"scale",!0),i=km(Do(e),Fo(n.scope,t,"add1")),c=new mt;Ur(c,e);let a=Om(r,e,i,o,s,c),l=Dm(a,r.glm,s,c);return n.test!==void 0&&n.test!=="none"&&Object.assign(l,{test:n.test==="F"?"F":"Chisq"},Fm(a,n.test==="F"?"F":"Chisq",!0)),l.warnings=c.list.filter(f=>!e.warnings.includes(f)),H("add1",l,e)}function Pm(e,t,n,r){let o=Oi(e,t,r),s=n.direction??"both";if(!["both","backward","forward"].includes(s))throw new RangeError(`'direction' should be one of "both", "backward", "forward"`);let i=zn(n.k??2,"k"),c=o.glm?0:zn(n.scale??0,"scale",!0),a=zn(n.steps??1e3,"steps",!0),l=Do(e),f=[],m=l,u=n.scope;if(u!==void 0)if(typeof u=="string")m=Fo(u,t,"upper");else if(typeof u=="object"&&u!==null)u.lower!==void 0&&(f=Fo(u.lower,t,"lower")),u.upper!==void 0&&(m=Fo(u.upper,t,"upper"));else throw new TypeError("scope must be a formula or { lower, upper }");for(let y of l)if(!m.some(R=>R.key===y.key))throw new RangeError(`upper scope has term \u2018${y.label}\u2019 not included in model`);for(let y of f)if(!l.some(R=>R.key===y.key))throw new RangeError(`lower scope has term \u2018${y.label}\u2019 not included in model`);let h=s!=="forward",p=s!=="backward",d=new mt;Ur(d,e);let g=e,[,w]=Ys(g,i,c);if(Number.isNaN(w))throw new RangeError(`AIC is not defined for this model, so '${r==="step"?"step":"stepAIC"}' cannot proceed`);if(w===-1/0)throw new RangeError(`AIC is -infinity for this model, so '${r==="step"?"step":"stepAIC"}' cannot proceed`);let N=Ws(g),v={step:[""],df:[NaN],deviance:[NaN],residDf:[Ws(g)-Ys(g,i,c)[0]],residDeviance:[or(g)],aic:[w]},E=[{formula:_i(o,g.termLabels),termLabels:[...g.termLabels],aic:w}],b=(y,R,T)=>{if(Ws(y)!==N)throw new RangeError("number of rows in use has changed: remove missing values?");let[S,k]=Ys(y,i,c),L=y.dfResidual-g.dfResidual;v.step.push(R),v.df.push(r==="step"?L:Math.abs(L)),v.deviance.push(T?or(y)-or(g):or(g)-or(y)),v.residDf.push(Ws(y)-S),v.residDeviance.push(or(y)),v.aic.push(k),g=y,w=k,E.push({formula:_i(o,g.termLabels),termLabels:[...g.termLabels],aic:w})},M=0;for(;M<a;){let y=Do(g),R=y.filter((O,q)=>{let P=(Yt(g),g.coefNames).map((B,J)=>J).filter(B=>g.assign[B]===q+1);return P.length>0&&P.every(B=>Number.isNaN(g.coefficients[B]))});if(R.length>0){let O=R[R.length-1],q=Oo(o,g.termLabels.filter(P=>P!==O.label));Ur(d,q),b(q,`- ${O.label}`,!0),M++;continue}let T=h?Am(y,f):[],S=p?km(y,m):[],k=[];if(T.length>0&&(k=Cm(g,T,i,c,d).map((O,q)=>q===0?O:{...O,label:`- ${O.label}`})),S.length>0){let O=Om(o,g,S,i,c,d).map((q,P)=>P===0?q:{...q,label:`+ ${q.label}`});k=k.length===0?O.filter((q,P)=>P===0||q.df>0):[...k,...O.slice(1).filter(q=>q.df>0)]}if(k.length===0)break;let L=k.map((O,q)=>q).sort((O,q)=>Dg(k[O].aic,k[q].aic)||O-q),_=L.map(O=>k[O]),I=E[E.length-1];if(I.table={move:_.map(O=>O.label),df:_.map(O=>O.df),deviance:_.map(O=>O.deviance),aic:_.map(O=>O.aic)},o.glm||(I.table.sumOfSq=_.map(O=>O.sumOfSq)),L[0]===0)break;let F=_[0],C=F.label.startsWith("- "),D=F.label.slice(2),A=C?Oo(o,g.termLabels.filter(O=>O!==D)):Oo(o,[...g.termLabels,D]);Ur(d,A),b(A,F.label,C),M++}let x={fit:g,anova:v,criterion:c>0&&!o.glm?"Cp":"AIC",visits:E,warnings:d.list.filter(y=>!e.warnings.includes(y))};return H(r,x,e)}var Dg=(e,t)=>Number.isNaN(e)?Number.isNaN(t)?0:1:Number.isNaN(t)?-1:e-t;function Pg(e,t,n={}){return V(n,Tg,"step"),V(Ft(n,"scope"),Sg,"step","scope"),Pm(e,t,n,"step")}function Vg(e,t,n={}){return V(n,Ag,"stepAIC"),V(Ft(n,"scope"),kg,"stepAIC","scope"),Pm(e,t,n,"stepAIC")}var $g=["weights","family","link","epsilon","maxit"],zg=["family","link","variance"],Vm=["power"];function jg(e,t,n){V(n,$g,"svyglm"),V(Ft(n,"family"),zg,"svyglm","family"),V(Ft(n,"family.link"),Vm,"svyglm","family.link"),V(Ft(n,"link"),Vm,"svyglm","link");let{weights:r,family:o,link:s,epsilon:i,maxit:c}=n;if(r===void 0)throw new RangeError("'weights' must be given");let a=Array.from(r);if(a.length!==t.length)throw new RangeError("'weights' must have the length of 'y'");if(a.some(A=>!(A>0)||!Number.isFinite(A)))throw new RangeError("'weights' must be positive and finite");if(o==="quasi"||typeof o=="object"&&o!==null&&o.family==="quasi")throw new RangeError("the quasi family is not available in svyglm (its variance function is not known here)");let l=a.length,f=a.reduce((A,O)=>A+O,0)/l,m=a.map(A=>A/f),u=je(()=>er(e,t,{weights:m,...o===void 0?{}:{family:o},...s===void 0?{}:{link:s},...i===void 0?{}:{epsilon:i},...c===void 0?{}:{maxit:c}})),h=Array.isArray(e[0])||ArrayBuffer.isView(e[0])?e.map(A=>Array.from(A)):Array.from(e,A=>[A]),p=A=>{let O=0;for(let q=0;q<A.length;q++)Number.isFinite(A[q])&&Math.abs(A[q])>O&&(O=Math.abs(A[q]));return O>0?Math.floor(Math.log2(O)):0},d=(h[0]??[]).map((A,O)=>p(h.map(q=>q[O]))),N=(typeof o=="object"&&o!==null?o.family:o??"gaussian")==="gaussian"&&(s===void 0||s==="identity")&&(typeof o!="object"||o===null||o.link===void 0||o.link==="identity")?p(t):0,v=d.every(A=>A===0)&&N===0?u:je(()=>er(h.map(A=>A.map((O,q)=>$(O,-d[q]))),Array.from(t,A=>$(A,-N)),{weights:m,...o===void 0?{}:{family:o},...s===void 0?{}:{link:s},...i===void 0?{}:{epsilon:i},...c===void 0?{}:{maxit:c}})),E=je(()=>xa(v,{type:"HC0"})).vcov,b=[...u.coefficients.length>d.length?[N]:[],...d.map(A=>N-A)],M=E.map((A,O)=>A.map((q,P)=>$(q*l/(l-1),b[O]+b[P]))),x=u.coefficients.length,y=l-x,R=E.map((A,O)=>$(Math.sqrt(A[O]*l/(l-1)),b[O])),T=v.coefficients.map((A,O)=>A/Math.sqrt(E[O][O]*l/(l-1))),S=T.map(A=>2*dn(-Math.abs(A),y)),k=v.fittedValues,L=v.y,_=u.family==="binomial"||u.family==="quasibinomial"?A=>A*(1-A):u.family==="poisson"||u.family==="quasipoisson"?A=>A:u.family==="Gamma"?A=>A*A:u.family==="inverse.gaussian"?A=>A*A*A:()=>1,I=k.map((A,O)=>(L[O]-A)/Math.sqrt(_(A))),F=I.reduce((A,O,q)=>A+m[q]*O,0)/l,C=0;for(let A=0;A<l;A++)C+=m[A]*(I[A]-F)**2;let D=$(C/l*(l/(l-1)),2*N);return H("svyglm",{names:u.model.columnNames.slice(),coefficients:u.coefficients.slice(),se:R,tValue:T,pValue:S,vcov:M,dfResidual:y,dispersion:D,deviance:u.deviance,nullDeviance:u.nullDeviance,fittedValues:u.fittedValues.slice(),linearPredictors:u.linearPredictors.slice(),iter:u.iter,converged:u.converged,glm:u,warnings:u.warnings.slice()})}function sr(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(s=>Number.isFinite(s)))).sort((s,i)=>s-i);if(n.length<2)return e.slice();let r=st(n.map(Math.abs)),o=[n[0]];for(let s=0;s+1<n.length;s++){let i=n[s+1]-n[s];i<=t||i/r<=t||o.push(n[s+1])}return e.map(s=>{if(!Number.isFinite(s))return s;let i=0,c=o.length-1;for(;i<c;){let a=Math.floor((i+c+1)/2);o[a]<=s?i=a:c=a-1}return o[i]})}var Kg=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),Wg=["eps","iterMax","tolerChol","tolerInf","timefix"],Vi=e=>e==null||typeof e=="number"&&Number.isNaN(e),Yg=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,Bg=2**64,Ug=2**-1022,$m=700;function Bs(e,t,n){let{time:r,event:o,weight:s,x:i,stratumEnd:c,logUnit:a,start:l}=e,f=r.length,m=t.length,u=()=>new Array(m).fill(0),h=()=>Array.from({length:m},u),p=u(),d=h(),g=0,w=0,N=u(),v=h(),E=u(),b=h(),M=0,x=0,y=R=>{let T=Math.exp(x-R);w*=T,M*=T;for(let S=0;S<m;S++){N[S]*=T,E[S]*=T;let k=v[S],L=b[S];for(let _=0;_<=S;_++)k[_]*=T,L[_]*=T}x=R};for(let R=f-1;R>=0;){c[R]&&(x=0,w=0,N=u(),v=h());let T=r[R],S=!1;if(l!==void 0){let I=R;for(;I>0&&r[I-1]===T&&!c[I-1];)I--;for(let F=I;F<=R;F++)o[F]&&(S=!0)}if(l!==void 0&&S){let I=R;for(;I>0&&!c[I-1];)I--;let F=R;for(;!c[F];)F++;let C=A=>{let O=0;for(let q=0;q<m;q++)O+=t[q]*i[A][q];return O},D=-1/0;for(let A=I;A<=F;A++)l[A]<T&&r[A]>=T&&(D=Math.max(D,C(A)));x=Number.isFinite(D)?D:0,w=0,N=u(),v=h();for(let A=I;A<=F;A++){if(!(l[A]<T&&r[A]>=T)||r[A]===T&&o[A])continue;let O=Math.exp(C(A)-x)*s[A];w+=O;for(let q=0;q<m;q++){N[q]+=O*i[A][q];for(let P=0;P<=q;P++)v[q][P]+=O*i[A][q]*i[A][P]}}}let k=0,L=0;M=0;let _=R;do{let I=i[_],F=0;for(let A=0;A<m;A++)F+=t[A]*I[A];let C=s[_];Number.isFinite(F)&&(F-x>$m?y(F):F-x<-$m&&w===0&&M===0&&(x=F));let D=Math.exp(F-x)*C;if(o[_]){k++,L+=C,M+=D,g+=C*F;for(let A=0;A<m;A++){p[A]+=C*I[A],E[A]+=D*I[A];let O=b[A];for(let q=0;q<=A;q++)O[q]+=D*I[A]*I[q]}}else if(l===void 0){w+=D;for(let A=0;A<m;A++){N[A]+=D*I[A];let O=v[A];for(let q=0;q<=A;q++)O[q]+=D*I[A]*I[q]}}_--}while(_>=0&&r[_]===T&&!c[_]);if(k>0){let I=n?k:1,F=n?k:1,C=L/I;for(let D=0;D<I;D++){w+=n?M/F:M,g-=C*(Math.log(w)+x+a);for(let A=0;A<m;A++){N[A]+=n?E[A]/F:E[A];let O=N[A]/w;p[A]-=C*O;let q=v[A],P=b[A],B=d[A];for(let J=0;J<=A;J++){q[J]+=n?P[J]/F:P[J];let ce=q[J]-O*N[J],W=C*ce;B[J]+=W<1/0&&W>-1/0&&(Math.abs(W)>=Ug||ce===0)||!Number.isFinite(C)?W/w:C*(ce/w)}}}E.fill(0);for(let D of b)D.fill(0)}R=_}for(let R=0;R<m;R++)for(let T=0;T<R;T++)d[T][R]=d[R][T];return{loglik:g,score:p,info:d}}function zm(e,t,n){let{time:r,event:o,x:s,stratumEnd:i}=e,c=Re([e.weight]).groups[0],a=r.length,l=t.length,f=Array.from({length:a},()=>new Array(l).fill(0)),m=u=>{let h=0;for(let p=0;p<l;p++)h+=t[p]*s[u][p];return h};for(let u=0;u<a;){let h=u;for(;!i[h];)h++;let p=[];for(let R=u;R<=h;R++)p.push(m(R));let d=[];for(let R=u;R<=h;R++)(R===u||r[R]!==r[R-1])&&d.push(R);let g=R=>R+1<d.length?d[R+1]:h+1,w=new Array(d.length),N=new Array(d.length),v=new Array(d.length),E=-1/0,b=0,M=new Array(l).fill(0);for(let R=d.length-1;R>=0;R--){let T=Number.isFinite(E)?E:0;for(let k=d[R];k<g(R);k++)E=Math.max(E,p[k-u]);let S=Number.isFinite(E)?E:0;if(R<d.length-1&&S!==T){let k=Math.exp(T-S);b*=k;for(let L=0;L<l;L++)M[L]*=k}for(let k=d[R];k<g(R);k++){let L=c[k]*Math.exp(p[k-u]-S);b+=L;for(let _=0;_<l;_++)M[_]+=L*s[k][_]}w[R]=b,N[R]=M.slice(),v[R]=S}let x=0,y=new Array(l).fill(0);for(let R=0;R<d.length;R++){let T=v[R];if(R>0&&T!==v[R-1]){let A=Math.exp(T-v[R-1]);x*=A;for(let O=0;O<l;O++)y[O]*=A}let S=d[R],k=g(R),L=[];for(let A=S;A<k;A++)L.push(Math.exp(p[A-u]-T));let _=0,I=0,F=0,C=new Array(l).fill(0);for(let A=S;A<k;A++){if(!o[A])continue;let O=c[A]*L[A-S];_++,I+=c[A],F+=O;for(let q=0;q<l;q++)C[q]+=O*s[A][q]}for(let A=S;A<k;A++){let O=L[A-S],q=s[A],P=f[A];for(let B=0;B<l;B++)P[B]=-O*(q[B]*x-y[B])}if(_===0)continue;let D=n?_:1;for(let A=0;A<D;A++){let O=A/D,q=w[R]-O*F,P=N[R].map((J,ce)=>(J-O*C[ce])/q),B=I/D/q;for(let J=S;J<k;J++){let ce=L[J-S],W=s[J],te=f[J];if(o[J])for(let U=0;U<l;U++){let ve=W[U]-P[U];te[U]+=ve/D-(1-O)*ce*ve*B}else for(let U=0;U<l;U++)te[U]-=ce*(W[U]-P[U])*B}x+=B;for(let J=0;J<l;J++)y[J]+=B*P[J]}}u=h+1}return f}function $i(e,t,n){let r=e.start,{time:o,event:s,x:i,stratumEnd:c}=e,a=Re([e.weight]).groups[0],l=o.length,f=t.length,m=Array.from({length:l},()=>new Array(f).fill(0)),u=o.map((h,p)=>{let d=0;for(let g=0;g<f;g++)d+=t[g]*i[p][g];return d});for(let h=0;h<l;){let p=h;for(;!c[p];)p++;for(let d=h;d<=p;){let g=o[d],w=d;for(;w<p&&o[w+1]===g;)w++;let N=[],v=[],E=-1/0;for(let b=d;b<=w;b++)s[b]&&v.push(b);if(v.length>0)for(let b=h;b<=p;b++)r[b]<g&&o[b]>=g&&(N.push(b),E=Math.max(E,u[b]));if(v.length>0&&N.length>0){Number.isFinite(E)||(E=0);let b=0,M=new Array(f).fill(0),x=0,y=new Array(f).fill(0),R=0;for(let S of N){let k=Math.exp(u[S]-E),L=a[S]*k;b+=L;for(let _=0;_<f;_++)M[_]+=L*i[S][_];if(s[S]&&o[S]===g){R+=a[S],x+=L;for(let _=0;_<f;_++)y[_]+=L*i[S][_]}}let T=n?v.length:1;for(let S=0;S<T;S++){let k=S/T,L=b-k*x,_=R/T/L,I=M.map((F,C)=>(F-k*y[C])/L);for(let F of N){let C=Math.exp(u[F]-E),D=s[F]&&o[F]===g;for(let A=0;A<f;A++){let O=i[F][A]-I[A];D?m[F][A]+=O/T-(1-k)*C*O*_:m[F][A]-=C*O*_}}}}d=w+1}h=p+1}return m}function Tr(e,t){let n=e.length,r=0,o=!1;for(let a=0;a<n;a++){let l=e[a][a];l>0&&((!o||l>r)&&(r=l),o=!0)}let s=o?r*t:t,i=0,c=!0;for(let a=0;a<n;a++){let l=e[a][a];if(!Number.isFinite(l)||l<s){e[a][a]=0,l<-8*s&&(c=!1);for(let f=a+1;f<n;f++)e[f][a]=0;continue}i++;for(let f=a+1;f<n;f++){let m=e[f][a]/l;e[f][a]=m,e[f][f]-=m*m*l;for(let u=f+1;u<n;u++)e[u][f]-=m*e[u][a]}}return c?i:-i}function Sr(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let s=r[o];for(let i=0;i<o;i++)s-=r[i]*e[o][i];r[o]=s}for(let o=n-1;o>=0;o--){if(e[o][o]===0){r[o]=0;continue}let s=r[o]/e[o][o];for(let i=o+1;i<n;i++)s-=r[i]*e[i][o];r[o]=s}return r}function Gr(e){let t=e.length;for(let n=0;n<t;n++)if(e[n][n]>0){e[n][n]=1/e[n][n];for(let r=n+1;r<t;r++){e[r][n]=-e[r][n];for(let o=0;o<n;o++)e[r][o]+=e[r][n]*e[n][o]}}for(let n=0;n<t;n++){if(e[n][n]===0){for(let r=0;r<t;r++)e[n][r]=0,e[r][n]=0;continue}for(let r=n+1;r<t;r++){let o=e[r][n]*e[r][r];e[n][r]=o;for(let s=n;s<r;s++)e[n][s]+=o*e[r][s]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}var Po=(e,t)=>({statistic:e,df:t,pValue:Mt(e,t,{lowerTail:!1})});function Gg(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=Po(0,0),i=new Array(t).fill(0),c=o(),a=o();return{coefficients:o(),var:Array.from({length:t},()=>new Array(t).fill(0)),loglik:[0,0],score:0,wald:0,iter:0,means:Array.from({length:t},(l,f)=>st(e.map(m=>m[f]))),n:r,nevent:0,linearPredictors:new Array(r).fill(0),se:i,z:c,p:a,stdError:i,zValue:c,pValue:a,lower:o(),upper:o(),logtest:s,sctest:{...s},waldtest:{...s},rsq:{rsq:0,maxrsq:0},usedRobust:!1,warnings:n}}function Hr(e,t,n,r={}){return V(r,Kg,"coxph"),V(Ft(r,"control"),Wg,"coxph","control"),H("coxph",Hg(e,t,n,r).result)}function Hg(e,t,n,r={},o=!1){let{ties:s="efron",weights:i,strata:c,init:a,control:l={},nocenter:f=[-1,0,1],confLevel:m=.95,robust:u,cluster:h,start:p,id:d}=r,{eps:g=1e-9,iterMax:w=20,tolerChol:N=2**-39}=l,v=l.tolerInf??Math.sqrt(g);if(l.timefix!==void 0&&typeof l.timefix!="boolean")throw new TypeError("'timefix' must be a boolean");let E=l.timefix??!0;if(s!=="efron"&&s!=="breslow")throw new RangeError(`invalid 'ties': ${String(s)}`);if(!(g>0&&g<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(N>0&&N<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(v>0&&v<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(w)&&w>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(m>0&&m<1))throw new RangeError("'confLevel' must be between 0 and 1");let b=e.length;if(t.length!==b||n.length!==b)throw new RangeError("'time', 'status' and 'x' must have the same length");if(i!==void 0&&i.length!==b)throw new RangeError("'weights' must have the same length as 'time'");if(c!==void 0&&c.length!==b)throw new RangeError("'strata' must have the same length as 'time'");if(h!==void 0&&h.length!==b)throw new RangeError("'cluster' must have the same length as 'time'");if(p!==void 0&&p.length!==b)throw new RangeError("'start' must have the same length as 'time'");if(d!==void 0&&d.length!==b)throw new RangeError("'id' must have the same length as 'time'");if(u!==void 0&&typeof u!="boolean")throw new TypeError("'robust' must be true or false");let M=b===0?0:n[0].length;if(M<1)throw new RangeError("'x' must have at least one column");if(n.some(Y=>Y.length!==M))throw new RangeError("the rows of 'x' must have the same length");if(a!==void 0&&a.length!==M)throw new RangeError("'init' must have one value per column of 'x'");if(a?.some(Y=>!Number.isFinite(Y)))throw new RangeError("'init' must contain finite values");let x=new mt,y=[];for(let Y=0;Y<b;Y++){if(Number.isNaN(e[Y])||Number.isNaN(t[Y])||n[Y].some(ye=>Number.isNaN(ye))||p!==void 0&&Number.isNaN(p[Y])||i!==void 0&&Number.isNaN(i[Y])||c!==void 0&&Vi(c[Y])||h!==void 0&&Vi(h[Y])||d!==void 0&&Vi(d[Y]))continue;if(p!==void 0&&!(p[Y]<e[Y]))throw new RangeError("stop time must be > start time");let re=t[Y];if(re!==0&&re!==1){if(p===void 0)throw new RangeError("'status' must be 0 (censored) or 1 (event)");x.add("Invalid status value, converted to NA");continue}if(e[Y]===-1/0||e[Y]===1/0&&re===1)throw new RangeError("event times must be finite");y.push(Y)}let R=y.length;if(R===0)throw new RangeError(p===void 0?"no observations without missing values":"No (non-missing) observations");if(y.some(Y=>n[Y].some(re=>!Number.isFinite(re))))throw new RangeError("'x' contains infinite values");let T=y.map(Y=>i===void 0?1:i[Y]);if(T.some(Y=>!(Y>0)))throw new RangeError("Invalid weights, must be >0");if(T.some(Y=>Y===1/0))throw new RangeError("'weights' must be finite");let S=d===void 0?void 0:Ot(d),k=new Map;if(S!==void 0)for(let Y of y){if(t[Y]!==1)continue;let re=S[Y];k.set(re,(k.get(re)??0)+1)}let L=p!==void 0&&[...k.values()].some(Y=>Y>1),_=u??(h!==void 0||T.some(Y=>!Number.isInteger(Y))||L);if(p!==void 0&&u===!0&&h===void 0&&d===void 0)throw new RangeError("one of cluster or id is needed");h!==void 0&&!_&&x.add("cluster specified with robust=FALSE, cluster ignored");let I=0;for(let Y of y)I+=t[Y];let F=y.map(Y=>e[Y]),C=p===void 0?void 0:y.map(Y=>p[Y]),D,A;if(C===void 0)D=E?sr(F):F;else if(!E)D=F,A=C;else{let Y=sr([...C,...F]);if(A=Y.slice(0,R),D=Y.slice(R),A.some((re,ye)=>re>=D[ye]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(I===0){let Y=Gg(y.map(re=>n[re]),M,x.list);return S!==void 0&&(Y.nId=new Set(y.map(re=>S[re])).size),{result:Y}}let O=new Array(R).fill(0),q,P=[];if(c!==void 0){let Y=Ot(c);P=Array.from(new Set(y.map(ye=>Y[ye]))).sort(Yg);let re=new Map;P.forEach((ye,Ye)=>re.set(ye,Ye)),q=y.map(ye=>Y[ye]),O=y.map(ye=>re.get(Y[ye]))}let B=Array.from({length:R},(Y,re)=>re).sort((Y,re)=>O[Y]-O[re]||D[Y]-D[re]||Y-re),J=B.map(Y=>T[Y]),{groups:ce,k:W}=At(J)>=Bg?Re([J]):{groups:[J],k:0},te=ce[0],U=0;for(let Y of T)U+=W===0?Y:$(Y,-W);let ve=B.map(Y=>n[y[Y]].slice()),X=new Array(M).fill(0),K=new Array(M).fill(1),Z=new Array(M).fill(0),z=new Set(f);for(let Y=0;Y<M;Y++){if(ve.every(ze=>z.has(ze[Y])))continue;let re=ve.map(ze=>ze[Y]),ye=ze=>{let Vt=0;for(let Ln=0;Ln<R;Ln++)Vt+=(p===void 0?te[Ln]:1)*ze[Ln];let _n=p===void 0?U:R,ur=Vt/_n,Fr=0,ma=ze.map((Ln,Xo)=>(Fr+=(p===void 0?te[Xo]:1)*Math.abs(Ln-ur),Ln-ur)),Dr=Fr===0?1:_n/Fr;return{m:ur,s:Dr,d:ma,ok:Number.isFinite(ur)&&Dr>=2**-1022&&Dr<1/0}},Ye=ye(re);if(!Ye.ok){let{groups:ze,k:Vt}=Re([re]);Ye=ye(ze[0]),Z[Y]=Vt}for(let ze=0;ze<R;ze++)ve[ze][Y]=Ye.d[ze]*Ye.s;X[Y]=$(Ye.m,Z[Y]),K[Y]=Ye.s}let G={time:B.map(Y=>D[Y]),...A===void 0?{}:{start:B.map(Y=>A[Y])},event:B.map(Y=>t[y[Y]]===1),weight:te,x:ve,stratumEnd:B.map((Y,re)=>re===R-1||O[B[re+1]]!==O[Y]),logUnit:W*Math.LN2},j=s==="efron",Q=a===void 0?new Array(M).fill(0):a.map((Y,re)=>$(Y/K[re],Z[re])),ne=Bs(G,Q,j),be=[ne.loglik,ne.loglik],Ne=ne.info.map(Y=>Y.slice()),me=Tr(Ne,N),le=Sr(Ne,ne.score),Ee=0;for(let Y=0;Y<M;Y++)Ee+=ne.score[Y]*le[Y];let he=0,Le,we=!1,Me=w===0;if(w===0||!Number.isFinite(ne.loglik))Le=Gr(Ne);else{let Y=ne.loglik,re=Q.map((Ye,ze)=>Ye+le[ze]),ye=0;for(Me=!1,he=1;he<=w;he++){ne=Bs(G,re,j),Ne=ne.info.map(ze=>ze.slice()),me=Tr(Ne,N);let Ye=Number.isFinite(ne.loglik)&&ne.score.every(Number.isFinite)&&ne.info.every(ze=>ze.every(Number.isFinite));if(Ye&&(ne.loglik===Y||Math.abs(1-Y/ne.loglik)<=g)){Me=!0,be[1]=ne.loglik,Q=re;break}!Ye||ne.loglik<Y?(ye++,re=re.map((ze,Vt)=>(ze+ye*Q[Vt])/(ye+1))):(ye=0,Y=ne.loglik,be[1]=Y,le=Sr(Ne,ne.score),Q=re,re=re.map((ze,Vt)=>ze+le[Vt]))}Me?Le=Gr(Ne):(he=p===void 0?w+1:w,we=!0,p!==void 0?(ne=Bs(G,Q,j),be[1]=ne.loglik,Ne=ne.info.map(Ye=>Ye.slice()),me=Tr(Ne,N),Le=Gr(Ne)):w>1?(ne=Bs(G,Q,j),be[1]=ne.loglik,Le=ne.info.map((Ye,ze)=>Ye.map((Vt,_n)=>ze===_n?Ye[ze]>0?1/Ye[ze]:0:-0))):Le=Gr(Ne))}let Se=K.map(Y=>Math.floor(Math.log2(Y))),Ke=K.map((Y,re)=>$(Y,-Se[re])),Pe=Se.map((Y,re)=>Y-Z[re]),Ze=Q.map((Y,re)=>Y*Ke[re]),Be=ne.score.map((Y,re)=>Y/Ke[re]),Qe=Le.map((Y,re)=>Y.map((ye,Ye)=>ye*Ke[re]*Ke[Ye])),fe=Ze.map((Y,re)=>$(Y,Pe[re])),ee=Qe.map((Y,re)=>Y.map((ye,Ye)=>$(ye,Pe[re]+Pe[Ye]-W))),ge=fe.slice();if(w>0&&Math.abs(me)<M)for(let Y=0;Y<M;Y++)Le[Y][Y]===0&&(ge[Y]=NaN);let de=p===void 0?fe:fe.map((Y,re)=>Number.isNaN(ge[re])?0:Y),ae=0;for(let Y=0;Y<M;Y++)ae+=de[Y]*X[Y];let Je=y.map(Y=>{let re=0;for(let ye=0;ye<M;ye++)re+=n[Y][ye]*de[ye];return re-ae});if(w>1){let Y=Qe.map((re,ye)=>{let Ye=0;for(let ze=0;ze<M;ze++)Ye+=Be[ze]*Qe[ze][ye];return Math.abs(Ye)});if(we)(p===void 0||w>1)&&x.add("Ran out of iterations and did not converge"),p===void 0&&(At(Je)>500||Y.some(re=>!Number.isFinite(re)))&&x.add("one or more coefficients may be infinite");else if(p===void 0){let re=[];for(let ye=0;ye<M;ye++){let Ye=Y[ye];(!Number.isFinite(Be[ye])||$(Ye,Pe[ye])>g&&Ye>v*Math.abs(Ze[ye]))&&re.push(ye+1)}re.length>0&&x.add("Loglik converged before variable "+re.join(",")+" ; coefficient may be infinite. ")}}let Oe=ge.map((Y,re)=>Number.isNaN(Y)?-1:re).filter(Y=>Y>=0),tt=Oe.length,ke,$e=0,He=0,se;if(_&&tt>0){let Y=B.map((ct,Ue)=>Ue),re=R,ye=h??(p===void 0?void 0:d);if(ye!==void 0){let ct=Ot(ye),Ue=new Map;Y=B.map(dt=>{let Nt=ct[y[dt]],yt=Ue.get(Nt);return yt===void 0&&(yt=Ue.size,Ue.set(Nt,yt)),yt}),re=Ue.size}if(re<2)throw new RangeError(p===void 0?"at least two clusters are needed for the robust variance":"(subscript) logical subscript too long");let Ye=ct=>ct.map(Ue=>Ue.map((dt,Nt)=>dt/Ke[Nt])),ze=Ye(p===void 0?zm(G,Q,j):$i(G,Q,j)),Vt=ze.map(ct=>Qe.map((Ue,dt)=>{let Nt=0;for(let yt=0;yt<M;yt++)Nt+=ct[yt]*Qe[yt][dt];return Nt})),_n;if(W===0)_n=Vt.map((ct,Ue)=>ct.map(dt=>te[Ue]*dt));else{let ct=dt=>dt===0||!Number.isFinite(dt)?0:Math.floor(Math.log2(Math.abs(dt))),Ue=-1/0;Vt.forEach((dt,Nt)=>dt.forEach(yt=>yt!==0&&Number.isFinite(yt)&&(Ue=Math.max(Ue,ct(te[Nt])+ct(yt))))),$e=Ue===-1/0?0:-Ue,_n=Vt.map((dt,Nt)=>{let yt=ct(te[Nt]);return dt.map(hr=>{let kc=ct(hr);return $($(te[Nt],-yt)*$(hr,-kc),yt+kc+$e)})})}let ur=ct=>{let Ue=Array.from({length:re},()=>new Array(M).fill(0));return ct.forEach((dt,Nt)=>{let yt=Ue[Y[Nt]];for(let hr=0;hr<M;hr++)yt[hr]+=dt[hr]}),Ue},Fr=ct=>{let Ue=Array.from({length:M},()=>new Array(M).fill(0));for(let dt of ct)for(let Nt=0;Nt<M;Nt++)for(let yt=0;yt<M;yt++)Ue[Nt][yt]+=dt[Nt]*dt[yt];return Ue};ke=Fr(ur(_n));let ma=Re([J]).groups[0],Dr=a===void 0?new Array(M).fill(0):a.map((ct,Ue)=>$(ct/K[Ue],Z[Ue])),Ln=p===void 0?zm(G,Dr,j):$i(G,Dr,j),Xo=ur(Ye(Ln).map((ct,Ue)=>ct.map(dt=>ma[Ue]*dt))),po=new Array(M).fill(0);for(let ct of Xo)for(let Ue=0;Ue<M;Ue++)po[Ue]+=ct[Ue];let ua=Fr(Xo);if(M===1)He=po[0]*po[0]/ua[0][0];else{Tr(ua,N);let ct=Sr(ua,po);for(let Ue=0;Ue<M;Ue++)He+=po[Ue]*ct[Ue]}let Sc=new Array(R),Ac=new Array(R);B.forEach((ct,Ue)=>{Sc[ct]=ze[Ue].map((dt,Nt)=>$(dt,-Pe[Nt])),Ac[ct]=_n[Ue].map((dt,Nt)=>$(dt,Pe[Nt]-$e))}),se={scoreResiduals:Sc,dfbeta:Ac}}let Te=ke!==void 0,pe=ke??Qe,Ae=ke===void 0?-W:-2*$e,We=Oe.map(Y=>Ze[Y]-(a===void 0?0:$(a[Y],-Pe[Y]))),Fe=0;if(tt===1)Fe=We[0]*We[0]/pe[Oe[0]][Oe[0]];else if(tt>1){let Y=Oe.map(ye=>Oe.map(Ye=>pe[ye][Ye]));Tr(Y,N);let re=Sr(Y,We);for(let ye=0;ye<tt;ye++)Fe+=We[ye]*re[ye]}Fe=$(Fe,-Ae);let nt=(1-m)/2,ut=nt<1e-8?oe(nt,{lowerTail:!1}):oe((1+m)/2),_t=(Y,re)=>{let ye=Math.floor(re/2),Ye=Y.map((ze,Vt)=>Math.sqrt($(ze[Vt],re-2*ye)));return{unit:Ye,h:ye,se:Ye.map((ze,Vt)=>$(ze,Pe[Vt]+ye))}},Bt=_t(Qe,-W).se,{unit:Wn,h:Ve,se:De}=_t(pe,Ae),Tt=ge.map((Y,re)=>$((Number.isNaN(Y)?Y:Ze[re])/Wn[re],-Ve)),pt=Tt.map(Y=>Mt(Y*Y,1,{lowerTail:!1})),En=ge.map((Y,re)=>Math.exp(Y-ut*De[re])),Pt=ge.map((Y,re)=>Math.exp(Y+ut*De[re])),qt=$(-2*(be[0]-be[1]),W),ht=[$(be[0],W),$(be[1],W)],Ut=$(Ee,W),nn=ke?.map((Y,re)=>Y.map((ye,Ye)=>$(ye,Pe[re]+Pe[Ye]-2*$e))),Zt={coefficients:ge,var:nn??ee,loglik:ht,score:Ut,wald:Fe,iter:he,means:X,n:R,nevent:I,...S===void 0?{}:{nId:new Set(y.map(Y=>S[Y])).size},linearPredictors:Je,se:Bt,z:Tt,p:pt,stdError:Bt,zValue:Tt,pValue:pt,lower:En,upper:Pt,logtest:Po(qt,tt),sctest:Po(Ut,tt),waldtest:Po(Fe,tt),rsq:{rsq:1-Math.exp(-qt/R),maxrsq:1-Math.exp(2*ht[0]/R)},usedRobust:Te,...Te?{naiveVar:ee,robustSe:De,rscore:He,robscore:Po(He,tt)}:{},warnings:x.list},Lt=ne.score.map((Y,re)=>$(Y/K[re],Z[re]+W));Object.defineProperty(Zt,"coxZphData",{enumerable:!1,value:{time:D.slice(),...A===void 0?{}:{start:A.slice()},status:y.map(Y=>t[Y]===1),weights:T.slice(),...q===void 0?{}:{strata:q.slice()},x:y.map(Y=>n[Y].slice()),sortedToInput:B.slice(),sortedTime:G.time.slice(),...G.start===void 0?{}:{sortedStart:G.start.slice()},sortedEvent:G.event.slice(),sortedWeight:G.weight.slice(),sortedX:G.x.map(Y=>Y.slice()),stratumIndex:B.map(Y=>O[Y]),stratumEnd:G.stratumEnd.slice(),betaScaled:Q.slice(),inverseCovariateScale:Ke.map((Y,re)=>$(1/Y,-Pe[re])),weightExp:W,ties:s}});let Jt=se?.scoreResiduals;if(p!==void 0&&o&&Jt===void 0){let Y=$i(G,Q,j);Jt=new Array(R),B.forEach((re,ye)=>{Jt[re]=Y[ye].map((Ye,ze)=>$(Ye/Ke[ze],-Pe[ze]))})}return{result:Zt,...se,...Jt===void 0?{}:{scoreResiduals:Jt},first:Lt,converged:Me}}var Xg=["rho"],Qg=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),zi=e=>e==null||typeof e=="number"&&Number.isNaN(e);function Zg(e,t,n){if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n!==void 0&&n.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");let r=[];for(let a=0;a<e.length;a++){if(Number.isNaN(e[a])||Number.isNaN(t[a])||n!==void 0&&zi(n[a]))continue;let l=t[a];if(l!==0&&l!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[a]===-1/0||e[a]===1/0&&l===1)throw new RangeError("event times must be finite");r.push(a)}if(r.length===0)throw new RangeError("no observations without missing values");let o=[],s=new Map;n!==void 0&&(n=Ot(n)),n!==void 0&&(o=Array.from(new Set(r.map(a=>n[a]))).sort((a,l)=>typeof a=="number"&&typeof l=="number"?a-l:String(a)<String(l)?-1:String(a)>String(l)?1:0),o.forEach((a,l)=>s.set(a,l)));let i=sr(r.map(a=>e[a])),c=r.map((a,l)=>({time:i[l],event:t[a]===1,group:n===void 0?0:s.get(n[a])}));return c.sort((a,l)=>a.time-l.time),{obs:c,labels:o.map(a=>`group=${Rt(a)}`)}}function Jg(e,t,n,r={}){V(r,Xg,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=Zg(e,t,n),c=i.length;if(c<2)throw new RangeError("there is only 1 group");let a=new Array(c).fill(0);for(let v of s)a[v.group]++;let l=new Array(c).fill(0),f=new Array(c).fill(0),m=Array.from({length:c},()=>new Array(c).fill(0)),u=a.slice(),h=s.length,p=1;for(let v=0;v<s.length;){let E=s[v].time,b=v,M=new Array(c).fill(0),x=new Array(c).fill(0),y=0;for(;b<s.length&&s[b].time===E;){let R=s[b];x[R.group]++,R.event&&(M[R.group]++,y++),b++}if(y>0){let R=o===0?1:p**o;for(let T=0;T<c;T++)l[T]+=R*M[T],f[T]+=R*y*u[T]/h;if(h>1){let T=R*R*y*(h-y)/(h*(h-1));for(let S=0;S<c;S++)for(let k=0;k<c;k++)m[S][k]+=T*u[S]*((S===k?1:0)-u[k]/h)}p*=1-y/h}for(let R=0;R<c;R++)u[R]-=x[R];h-=b-v,v=b}let d=f.map((v,E)=>v>0?E:-1).filter(v=>v>=0),g=0,w=Math.max(d.length-1,0);if(d.length>=2){let v=d.slice(1),E=v.map(M=>l[M]-f[M]),b=v.map(M=>v.map(x=>m[M][x]));try{let M=rt(b,E);g=E.reduce((x,y,R)=>x+y*M[R],0)}catch{({chisq:g,df:w}=ew(b,E))}}let N=w===0?NaN:Mt(g,w,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:g,df:w,pvalue:N,pValue:N,warnings:[]}}function ew(e,t){let n=un(e,{symmetric:!0}),r=n.values,o=r.reduce((c,a)=>Math.max(c,a),0),s=0,i=0;return r.forEach((c,a)=>{if(!(o>0&&c>1e-10*o))return;let l=t.reduce((f,m,u)=>f+m*n.vectors[u][a],0);s+=l*l/c,i++}),{chisq:s,df:i}}function Ar(e,t){let n=Math.sqrt(Number.EPSILON),r=t.findIndex(s=>s<.5+n);if(r<0)return NaN;let o=t[r];if(Math.abs(o-.5)<n){for(let s=r+1;s<t.length;s++)if(t[s]<o)return Hn(e[r],e[s])}return e[r]}function ar(e,t,n={}){V(n,Qg,"survfit");let{group:r,start:o,id:s,weights:i,ctype:c=1,stype:a=1,rmean:l="common",confType:f="log",confInt:m=.95}=n;if(typeof f!="string")throw new TypeError("confType must be a string");let u=["log","log-log","plain","none","logit","arcsin"],h=typeof f=="string"?u.filter(q=>q.startsWith(f)):[],p=u.includes(f)?f:h.length===1?h[0]:f;if(typeof c!="number")throw new TypeError("ctype must be a number");if(typeof a!="number")throw new TypeError("stype must be a number");if(c!==1&&c!==2)throw new RangeError("ctype must be 1 or 2");if(a!==1&&a!==2)throw new RangeError("stype must be 1 or 2");if(!u.some(q=>q===p))throw new RangeError("'arg' should be one of \u201Clog\u201D, \u201Clog-log\u201D, \u201Cplain\u201D, \u201Cnone\u201D, \u201Clogit\u201D, \u201Carcsin\u201D");if(typeof m!="number")throw new TypeError("confInt must be a number");if(!(m>0&&m<1))throw new RangeError("confidence intervals must be between 0 and 1");if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(r!==void 0&&r.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(o!==void 0&&o.length!==e.length)throw new RangeError("'start' must have the same length as 'time'");if(s!==void 0&&s.length!==e.length)throw new RangeError("'id' must have the same length as 'time'");if(s!==void 0&&o===void 0)throw new RangeError("'id' requires 'start'");if(i!==void 0&&!Array.isArray(i))throw new TypeError("'weights' must be an array");if(i!==void 0&&i.length!==e.length)throw new RangeError("'weights' must have the same length as 'time'");let d=n.robust??(i!==void 0&&Array.from(i).some(q=>q!=null&&Number.isFinite(q)&&!Number.isInteger(q)));if(typeof d!="boolean")throw new TypeError("'robust' must be a boolean");if(d&&a===2&&i!==void 0&&Array.from(i).some(q=>q!=null&&Number.isFinite(q)&&!Number.isInteger(q)))throw new RangeError("non-integer weights with stype 2 and the robust variance are not supported");if(typeof l!="number"&&typeof l!="string")throw new TypeError("rmean must be a number or string");if(typeof l=="number"&&!Number.isFinite(l))throw new RangeError("'rmean' must be finite");if(typeof l!="number"&&l!=="none"&&l!=="common"&&l!=="individual")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Ccommon\u201D, \u201Cindividual\u201D");let g=[];for(let q=0;q<e.length;q++){let P=i?.[q];if(i&&P!=null&&typeof P!="number")throw new TypeError("weights must contain numbers");if(i&&(P==null||Number.isNaN(P)))continue;let B=P??1;if(!Number.isFinite(B))throw new RangeError("weights must be finite");if(B<0)throw new RangeError("weights must be non-negative");if(e[q]!=null&&typeof e[q]!="number")throw new TypeError("'time' must contain numbers");if(t[q]!=null&&typeof t[q]!="number")throw new TypeError("'status' must contain numbers");if(o!==void 0&&o[q]!=null&&typeof o[q]!="number")throw new TypeError("'start' must contain numbers");if(!(e[q]==null||t[q]==null||Number.isNaN(e[q])||Number.isNaN(t[q])||o!==void 0&&(o[q]==null||Number.isNaN(o[q]))||r!==void 0&&zi(r[q])||s!==void 0&&zi(s[q]))){if(o!==void 0&&!(o[q]<e[q]))throw new RangeError("stop time must be > start time");if(t[q]!==0&&t[q]!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[q]===-1/0||e[q]===1/0&&t[q]===1)throw new RangeError("event times must be finite");g.push(q)}}if(g.length===0)throw new RangeError("no observations without missing values");let w=r===void 0?[]:Ot(r),N=s===void 0?[]:Array.from(s),v=r===void 0?["all"]:Array.from(new Set(g.map(q=>w[q]))).sort((q,P)=>typeof q=="number"&&typeof P=="number"?q-P:String(q)<String(P)?-1:String(q)>String(P)?1:0).map(q=>`group=${Rt(q)}`),E=new Map;r!==void 0&&Array.from(new Set(g.map(q=>w[q]))).sort((q,P)=>typeof q=="number"&&typeof P=="number"?q-P:String(q)<String(P)?-1:String(q)>String(P)?1:0).forEach((q,P)=>E.set(q,P));let b=new Map;s!==void 0&&Array.from(new Set(g.map(q=>N[q]))).forEach((q,P)=>b.set(q,P));let M=g.flatMap(q=>o===void 0?[e[q]]:[o[q],e[q]]),x=sr(M),y=new Map,R=new Map;g.forEach((q,P)=>{if(o===void 0)R.set(q,x[P]);else if(y.set(q,x[2*P]),R.set(q,x[2*P+1]),!(y.get(q)<R.get(q)))throw new Error("aeqSurv exception, an interval has effective length 0")});let T=g.map(q=>({time:R.get(q),start:o===void 0?-1/0:y.get(q),event:t[q]===1,group:r===void 0?0:E.get(w[q]),id:s===void 0?q:b.get(N[q]),weight:i?.[q]??1})).sort((q,P)=>q.time-P.time),S=s===void 0?void 0:new Map;if(S)for(let q of T){let P=S.get(q.id)??[];P.push(q),S.set(q.id,P)}let k=v,L=T.map(q=>q.time),_=Math.min(...L),I=Math.min(0,_);if(typeof l=="number"&&l<=I)throw new RangeError("'rmean' must be greater than the integration origin");let F=(1-m)/2,C=F<1e-8?oe(F,{lowerTail:!1}):oe(1-F),D=[],A={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:D,stdError:D,lower:[],upper:[],cumhaz:[],stdChaz:[],logse:!d||c===2,n:[],records:[],nMax:[],nStart:[],events:[],rmean:[],seRmean:[],median:[],medianRows:k,table:{colNames:[],rowNames:k,table:[]},warnings:[]};i!==void 0&&(A.counts={nRisk:[],nEvent:[],nCensor:[]});let O=[];for(let q=0;q<k.length;q++){let P=T.filter(fe=>fe.group===q);if(!P.some(fe=>fe.weight>0))throw new Error("a group has no positive weight");let B=A.time.length,J=P.length,ce=Math.max(...P.map(fe=>fe.time)),W=0,te=0,U=1,ve=0,X=0,K=0;if(o===void 0){let fe=0;for(let ee of P)fe+=ee.weight;W=fe,te=P.length}let Z=[],z=[],G=[],j=s===void 0?void 0:new Map;if(j)for(let fe of P){let ee=j.get(fe.id)??[];ee.push(fe),j.set(fe.id,ee)}for(let fe=0;fe<P.length;){let ee=P[fe].time,ge=fe,de=0,ae=0,Je=0,Oe=te;for(;ge<P.length&&P[ge].time===ee;)ge++;let tt=P.slice(fe,ge);if(o!==void 0){let Ve=P.filter(De=>De.start<ee&&De.time>=ee);W=Ve.reduce((De,Tt)=>De+Tt.weight,0),te=Ve.length,Oe=te}let ke=tt.filter(Ve=>Ve.event),$e=tt.filter(Ve=>!Ve.event&&(S===void 0||!(S.get(Ve.id)??[]).some(De=>De.start===ee&&De.time>ee)));for(let Ve of ke)de+=Ve.weight,ae++;for(let Ve of $e)Je+=Ve.weight;let He=ke.length,se=$e.length;if(s!==void 0&&ke.length===0&&$e.length===0){fe=ge;continue}let Te=0,pe=0;if(de>0){if(c===1)Te=de/W,pe=de/(W*W);else for(let De=0;De<ae;De++){let Tt=de/ae,pt=W-De*Tt;Te+=Tt/pt,pe+=Tt/(pt*pt)}ve+=Te,K+=pe;let Ve=ae===Oe&&Oe>0?1:de/W;a===1?(U*=1-Ve,X+=Ve<1?de/W/(W-de):1/0):U=Math.exp(-ve),G.push({t:ee,d:de,n:W,e:ae,S:U})}let Ae=Math.sqrt(a===1?X:K),We=0,Fe=0;if(d){let Ve=0,De=0,Tt=j===void 0?P:Array.from(j.values(),pt=>pt[0]);for(let pt of Tt){let En=j?.get(pt.id)??[pt],Pt=0,qt=0;for(let ht of G){if(ht.t>ee)break;let Ut=En.some(Lt=>Lt.start<ht.t&&Lt.time>=ht.t),nn=En.some(Lt=>Lt.time===ht.t&&Lt.event),Zt=ht.d/ht.n;if(c===1)qt+=(Number(nn)-Number(Ut)*Zt)/ht.n;else{let Lt=0,Jt=0;for(let Y=0;Y<ht.e;Y++){let re=ht.d/ht.e,ye=ht.n-Y*re;Lt+=(ye+Y*re)/(ht.e*ye*ye),Jt-=re/(ye*ye)}qt+=Lt*Number(nn)+Jt*Number(Ut)}a===1&&Zt<1&&(Pt-=U*(Number(nn)-Number(Ut)*Zt)/((1-Zt)*ht.n))}Ve+=(pt.weight*Pt)**2,De+=(pt.weight*qt)**2}We=Math.sqrt(Ve),Fe=Math.sqrt(De)}let nt=d?c===2?a===1?We:Fe:a===1?U===0?NaN:We/U:Fe:Ae,ut=d?c===2?U*(a===1?We:Fe):a===1?We:U*Fe:U*Ae,_t=NaN,Bt=NaN;if(p!=="none")if(p==="log")_t=U===0?ut===0?0:NaN:U*Math.exp(-C*nt),Bt=U===0?ut===0?0:NaN:Math.min(1,U*Math.exp(C*nt));else if(p==="log-log")if(U===0||U===1)_t=ut===0?U:NaN,Bt=_t;else{let Ve=Math.log(-Math.log(U));_t=Math.exp(-Math.exp(Ve-C*nt/Math.log(U))),Bt=Math.exp(-Math.exp(Ve+C*nt/Math.log(U)))}else if(p==="plain")U===0&&ut===1/0?(_t=NaN,Bt=NaN):(_t=Math.max(0,U-C*ut),Bt=Math.min(1,U+C*ut));else if(p==="logit")if(U===0||U===1)_t=ut===0?U:NaN,Bt=_t;else{let Ve=Math.log(U/(1-U)),De=C*ut/(U*(1-U));_t=1-1/(1+Math.exp(Ve-De)),Bt=1-1/(1+Math.exp(Ve+De))}else if(U>0&&U<1){let Ve=Math.asin(Math.sqrt(U)),De=C*ut/(2*Math.sqrt(U*(1-U)));_t=Math.sin(Math.max(0,Ve-De))**2,Bt=Math.sin(Math.min(Math.PI/2,Ve+De))**2}else _t=NaN,Bt=NaN;let Wn=W;A.strata.push(k[q]),A.time.push(ee),A.nRisk.push(Wn),A.nEvent.push(de),A.nCensor.push(Je),A.surv.push(U),A.cumhaz.push(ve),A.stdChaz.push(d?Fe:Math.sqrt(K)),A.stdErr.push(d?c===2?U*(a===1?We:Fe):a===1?We:U*Fe:U*Ae),A.lower.push(_t),A.upper.push(Bt),A.counts&&(A.counts.nRisk.push(Oe),A.counts.nEvent.push(He),A.counts.nCensor.push(se)),o===void 0&&(W-=de+Je,te-=ge-fe),fe=ge}let Q=A.time.slice(B),ne=A.surv.slice(B),be=A.lower.slice(B),Ne=A.upper.slice(B);A.median.push([Ar(Q,ne),p==="none"?NaN:Ar(Q,be),p==="none"?NaN:Ar(Q,Ne)]),A.n.push(J),A.records.push(J);let me=A.nRisk.slice(B);A.nMax.push(Math.max(...me)),A.nStart.push(me[0]??0),A.events.push(P.reduce((fe,ee)=>fe+(ee.event?ee.weight:0),0));let le=l==="none"?NaN:l==="common"?Math.max(...L):l==="individual"?ce:l;if(l!=="none"&&!Number.isFinite(le))throw new RangeError("'rmean' must be finite");let Ee=P.filter(fe=>Number.isFinite(fe.time)).map(fe=>Math.abs(fe.time)),he=Number.isFinite(le)?Math.max(Math.abs(le),Math.abs(I),...Ee):Math.max(Math.abs(I),...Ee),Le=he===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(he)))),we=he===0?1:2**Le,Me=Number.isFinite(le)&&!Number.isFinite(le-I),Se=fe=>{let ee=1;for(let ge of G)if(ge.t<=fe)ee=ge.S;else break;return ee},Ke=fe=>{let ee=1;for(let ge of G)if(ge.t/we<=fe)ee=ge.S;else break;return ee},Pe=Fn();if(Me){let fe=Array.from(new Set([I/we,...G.map(ee=>ee.t/we).filter(ee=>ee>I/we&&ee<le/we),le/we])).sort((ee,ge)=>ee-ge);for(let ee=0;ee<fe.length-1;ee++){let ge=fe[ee],de=fe[ee+1];mn(Pe,Ke(ge)*(de-ge),fe.length+1)}}else{let fe=Array.from(new Set([I,...G.map(ee=>ee.t).filter(ee=>ee>I&&ee<le),le])).sort((ee,ge)=>ee-ge);for(let ee=0;ee<fe.length-1;ee++){let ge=fe[ee],de=fe[ee+1];mn(Pe,Se(ge)*(de-ge),fe.length+1)}}let Ze=Dn(Pe)*(Me?we:1),Be=Fn();if(Number.isFinite(le))for(let fe of G){if(fe.t>le||fe.n<=fe.d)continue;let ee=Fn();if(Me){let de=Array.from(new Set([fe.t/we,...G.map(ae=>ae.t/we).filter(ae=>ae>fe.t/we&&ae<le/we),le/we])).sort((ae,Je)=>ae-Je);for(let ae=0;ae<de.length-1;ae++)mn(ee,Ke(de[ae])*(de[ae+1]-de[ae]),de.length+1)}else{let de=Array.from(new Set([fe.t,...G.map(ae=>ae.t).filter(ae=>ae>fe.t&&ae<le),le])).sort((ae,Je)=>ae-Je);for(let ae=0;ae<de.length-1;ae++)mn(ee,Se(de[ae])*((de[ae+1]-de[ae])/we),de.length+1)}let ge=Dn(ee);mn(Be,ge*ge*fe.d/(fe.n*(fe.n-fe.d)),G.length+1)}let Qe=Dn(Be);A.rmean.push(l==="none"?NaN:Ze),A.seRmean.push(l==="none"?NaN:Math.sqrt(Qe)*we),O.push(l==="none"?p==="none"?[J,A.nMax[q],A.nStart[q],A.events[q],A.median[q][0]]:[J,A.nMax[q],A.nStart[q],A.events[q],A.median[q][0],A.median[q][1],A.median[q][2]]:p==="none"?[J,A.nMax[q],A.nStart[q],A.events[q],Ze,Math.sqrt(Qe)*we,A.median[q][0]]:[J,A.nMax[q],A.nStart[q],A.events[q],Ze,Math.sqrt(Qe)*we,A.median[q][0],A.median[q][1],A.median[q][2]])}return A.table={colNames:[...l==="none"?["records","n.max","n.start","events","median"]:["records","n.max","n.start","events","rmean","se(rmean)","median"],...p==="none"?[]:[`${m}LCL`,`${m}UCL`]],rowNames:k,table:O},H("survfit",A)}var tw=new Set(["newdata","confType","confInt","times","ties","weights","init","control","nocenter","confLevel","robust"]),nw=["eps","iterMax","tolerChol","tolerInf","timefix"];function rw(e,t,n,r={}){V(r,tw,"survfitCoxph"),V(Ft(r,"control"),nw,"survfitCoxph","control");let{newdata:o,confType:s="log",confInt:i=.95,times:c,...a}=r;if(!["log","log-log","plain","none"].includes(s))throw new RangeError(`invalid 'confType': ${String(s)}`);if(!(i>0&&i<1))throw new RangeError("'confInt' must be between 0 and 1");let l=je(()=>Hr(e,t,n,a)),{ties:f="efron",weights:m}=a,u=(n[0]??[]).map((U,ve)=>ow(n.map(X=>X[ve]))),h=n.map(U=>U.map((ve,X)=>u[X](ve))),p=je(()=>Hr(e,t,h,a)),d=p.coefficients.length,g=p.coefficients.map(U=>Number.isFinite(U)?U:0),w=p.var.map(U=>U.map(ve=>Number.isFinite(ve)?ve:0)),N=[];for(let U=0;U<e.length;U++)Number.isNaN(e[U])||Number.isNaN(t[U])||n[U].some(ve=>Number.isNaN(ve))||m!==void 0&&Number.isNaN(m[U])||N.push(U);let v=a.control?.timefix===!1?e.slice():sr(e);N.sort((U,ve)=>v[U]-v[ve]);let E=U=>m===void 0?1:m[U],b=U=>U.map((ve,X)=>ve-p.means[X]),M=new Map;for(let U of N)M.set(U,Math.exp(b(h[U]).reduce((ve,X,K)=>ve+X*g[K],0)));let x=[];for(let U of N)(x.length===0||x[x.length-1]!==v[U])&&x.push(v[U]);let y=x.length,R=new Array(y).fill(0),T=new Array(y).fill(0),S=Array.from({length:y},()=>new Array(d).fill(0)),k=new Array(y).fill(0),L=new Array(y).fill(0),_=new Array(y).fill(0),I=0,F=0,C=new Array(d).fill(0),D=N.length-1;for(let U=y-1;U>=0;U--){let ve=x[U],X=0,K=0,Z=0,z=new Array(d).fill(0);for(;D>=0&&v[N[D]]===ve;D--){let j=N[D],Q=E(j),ne=M.get(j),be=b(h[j]);I+=Q*ne,F+=Q;for(let Ne=0;Ne<d;Ne++)C[Ne]+=Q*ne*be[Ne];if(t[j]===1){X++,K+=Q,Z+=Q*ne;for(let Ne=0;Ne<d;Ne++)z[Ne]+=Q*ne*be[Ne]}else _[U]+=Q}if(k[U]=F,L[U]=K,X===0)continue;let G=f==="efron"?X:1;for(let j=0;j<G;j++){let Q=f==="efron"?j/X:0,ne=I-Q*Z,Ne=K/G/ne;R[U]+=Ne,T[U]+=Ne/ne;for(let me=0;me<d;me++)S[U][me]+=Ne*((C[me]-Q*z[me])/ne)}}if(o?.some(U=>U.length!==d))throw new RangeError("each row of 'newdata' must have one value per column of 'x'");let A=(o??[l.means]).map(U=>U.map((ve,X)=>u[X](ve))),O=i<1-1e-8?oe(1-(1-i)/2):oe((1-i)/2,{lowerTail:!1}),q=[],P=[],B=[],J=[],ce=[];for(let U of A){let ve=b(U),X=Math.exp(ve.reduce((Ne,me,le)=>Ne+me*g[le],0)),K=0,Z=0,z=new Array(d).fill(0),G=[],j=[],Q=[],ne=[],be=[];for(let Ne=0;Ne<y;Ne++){K+=R[Ne],Z+=T[Ne];for(let Me=0;Me<d;Me++)z[Me]+=ve[Me]*R[Ne]-S[Ne][Me];let me=0;for(let Me=0;Me<d;Me++)for(let Se=0;Se<d;Se++)me+=z[Me]*w[Me][Se]*z[Se];let le=X*K,Ee=Math.exp(-le),he=X*Math.sqrt(Z+me);be.push(le),G.push(Ee),j.push(he);let[Le,we]=sw(Ee,he,O,s);Q.push(Le),ne.push(we)}q.push(G),P.push(j),B.push(Q),J.push(ne),ce.push(be)}let W=q.map((U,ve)=>[Ar(x,U),Ar(x,B[ve]),Ar(x,J[ve])]),te={fit:l,time:x,nRisk:k,nEvent:L,nCensor:_,surv:q,stdErr:P,lower:B,upper:J,cumhaz:ce,median:W,records:N.length,nStart:k[0]??0,events:L.reduce((U,ve)=>U+ve,0),warnings:l.warnings.slice()};return c!==void 0&&(te.at=aw(te,c)),H("survfitCoxph",te)}function ow(e){let t=s=>{let i=0;for(let c of s)Number.isFinite(c)&&Math.abs(c)>i&&(i=Math.abs(c));return i>0?Math.floor(Math.log2(i)):0},n=t(e),r=$(e.find(s=>Number.isFinite(s))??0,-n),o=t(e.map(s=>$(s,-n)-r));return s=>$($(s,-n)-r,-o)}function sw(e,t,n,r){if(r==="none"||!Number.isFinite(t))return[NaN,NaN];if(r==="plain")return[Math.max(0,e-n*t*e),Math.min(1,e+n*t*e)];if(e===0)return[NaN,NaN];if(r==="log")return[Math.exp(Math.log(e)-n*t),Math.min(1,Math.exp(Math.log(e)+n*t))];if(e===1)return[NaN,NaN];let o=Math.log(-Math.log(e)),s=t/Math.log(e);return[Math.exp(-Math.exp(o-n*s)),Math.exp(-Math.exp(o+n*s))]}function aw(e,t){let n=[...t].sort((s,i)=>s-i),r={time:[],surv:e.surv.map(()=>[]),stdErr:e.surv.map(()=>[]),lower:e.surv.map(()=>[]),upper:e.surv.map(()=>[]),nRisk:[],nEvent:[]},o=0;for(let s of n){let i=-1;for(;i+1<e.time.length&&e.time[i+1]<=s;)i++;if(s>e.time[e.time.length-1])break;r.time.push(s);let c=e.time.findIndex(l=>l>=s);r.nRisk.push(c<0?0:e.nRisk[c]);let a=0;for(let l=o;l<=i;l++)a+=e.nEvent[l];r.nEvent.push(a),o=i+1,e.surv.forEach((l,f)=>{r.surv[f].push(i<0?1:e.surv[f][i]),r.stdErr[f].push(i<0?0:e.stdErr[f][i]),r.lower[f].push(i<0?1:e.lower[f][i]),r.upper[f].push(i<0?1:e.upper[f][i])})}return r}var iw=["vcov","df"],cw=["parm","level","vcov","df"];function jm(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function Km(e){if(!Array.isArray(e)||e.some(n=>!Array.isArray(n)&&!ArrayBuffer.isView(n)))throw new TypeError("'vcov' must be a matrix (an array of rows)");let t=e.map(n=>Array.from(n,Number));if(t.some(n=>n.length!==t.length))throw new RangeError("'vcov' must be a square matrix");return t}var Wm=e=>new Set(e).size===e.length;function Ym(e,t){pl(e);let n=e.coefficients,r=dl(e),o=n.length,s=n.map(m=>Number.isNaN(m)),i,c,a=[],l;if(t===void 0){let m=gl(e),u=new Map(m.names.map((p,d)=>[p,d])),h=r.map((p,d)=>s[d]?void 0:u.get(p));i=h.map(p=>h.map(d=>p===void 0||d===void 0?NaN:m.vcov[p][d])),c=r,l=h.map(p=>p===void 0?NaN:Ma(m.vcov,p,m.vcov[p][p])??Math.sqrt(m.vcov[p][p]))}else{let m=typeof t=="function"?je(()=>t(e)):t,u=jm(m)?m.vcov:m;if(jm(m)){if(i=Km(m.vcov),c=m.names.slice(),c.length!==i.length)throw new RangeError("'vcov' must have one name per row");a=[...m.warnings??[]]}else i=Km(m);l=i.map((h,p)=>{let d=h[p],g=typeof u=="object"&&u!==null?Ma(u,p,d):void 0;if(g!==void 0&&!Number.isNaN(g))return g;if(!Number.isFinite(d))throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is not a finite number`);if(d<0)throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is negative`);return Math.sqrt(d)})}if(c!==void 0&&Wm(c)&&Wm(r)){let m=new Map(c.map((h,p)=>[h,p])),u=r.flatMap((h,p)=>m.has(h)?[p]:[]);return{est:u.map(h=>n[h]),se:u.map(h=>l[m.get(r[h])]),names:u.map(h=>r[h]),warnings:a}}let f;if(i.length===o)f=n.map((m,u)=>u);else if(i.length===e.rank)f=n.flatMap((m,u)=>s[u]?[]:[u]);else{let m=e.rank===o?`${o} x ${o}`:`${o} x ${o} or ${e.rank} x ${e.rank}`;throw new RangeError(`'vcov' must be ${m}, one row per coefficient`)}return{est:f.map(m=>n[m]),se:l,names:f.map(m=>r[m]),warnings:a}}function Bm(e,t){if(t===void 0)return Ea(e)?1/0:e.dfResidual;if(typeof t!="number")throw new TypeError("'df' must be a number");if(Number.isNaN(t))throw new RangeError("'df' must not be NaN");return t}var Um=e=>Number.isFinite(e)&&e>0;function lw(e){return Ea(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function fw(e,t={}){V(t,iw,"coeftest");let{est:n,se:r,names:o,warnings:s}=Ym(e,t.vcov),i=Bm(e,t.df),c=Um(i),a=n.map((f,m)=>f/r[m]),l=a.map(f=>Number.isNaN(f)?NaN:c?2*vt(-Math.abs(f),i,!0,!1):2*ie(-Math.abs(f)));return H("coeftest",{estimate:n,stdError:r,statistic:a,pValue:l,names:o,statisticName:c?"t value":"z value",df:i,method:c?"t test of coefficients":"z test of coefficients",nobs:lw(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function mw(e){let t=[100*e,100*(1-e)],n=t.map(o=>{if(o===0||!Number.isFinite(o))return 0;let s=Math.floor(Math.log10(Math.abs(o))),i=o.toFixed(Math.max(0,2-s));return(i.includes(".")?i.replace(/0+$/,"").split(".")[1]:"").length}),r=Math.max(...n);return[`${t[0].toFixed(r)} %`,`${t[1].toFixed(r)} %`]}function uw(e,t={}){V(t,cw,"coefci");let n=t.level??.95;if(typeof n!="number")throw new TypeError("'level' must be a number");if(!(n>=0&&n<=1))throw new RangeError("'level' must be between 0 and 1");let r=Ym(e,t.vcov),o=Bm(e,t.df),s=r.est.map((m,u)=>u),i=t.parm;if(i!==void 0){let m=Array.isArray(i)?i:[i];if(m.every(u=>typeof u=="string")){let u=new Set(m);s=s.filter(h=>u.has(r.names[h]))}else if(m.every(u=>typeof u=="number"))s=m.map(u=>{if(!Number.isInteger(u)||u<0)throw new RangeError("'parm' must hold 0-based indices or names");if(u>=r.est.length)throw new RangeError("subscript out of bounds");return u});else throw new TypeError("'parm' must hold 0-based indices or names")}let c=(1-n)/2,[a,l]=Um(o)?[xt(c,o,!0,!1),c<1e-8?xt(c,o,!1,!1):xt(1-c,o,!0,!1)]:[oe(c),c<1e-8?oe(c,{lowerTail:!1}):oe(1-c)],f=(m,u,h)=>m+h*u;return H("coefci",{lower:s.map(m=>f(r.est[m],r.se[m],a)),upper:s.map(m=>f(r.est[m],r.se[m],l)),names:s.map(m=>r.names[m]),colNames:mw(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var hw=["test","dispersion"],pw=(e,t)=>gt(e,t/2,2,!1,!1);function dw(e,t,n){let{fit:r,w:o}=br(e,t,n,{start:void 0});return{dfRes:o.filter(i=>i>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(i=>t.map(c=>i[c])),intercept:e.intercept}}var ji=(e,t)=>Ii(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function gw(e,t={}){V(t,hw,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(ys),o=r.every(u=>u.k===r[0].k)?r[0].k:0,s=o===0?n:r.map(u=>u.fit),i=t.dispersion===void 0?void 0:ul(t.dispersion,o),c=i?.s??0,a=ww(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return H("glmAnova",a,...n);let l=new Set(["Deviance","Resid. Dev","Rao","Cp"]),f=a.colNames.map(u=>l.has(u)),m=a.table.map(u=>u.map((h,p)=>f[p]?$(h,2*o):h));if(c!==0&&a.colNames.includes("Cp")){let u=a.colNames.indexOf("Cp"),h=a.colNames.indexOf("Resid. Dev"),p=a.colNames.indexOf("Resid. Df"),d=s[0].model.x.length;for(let g of m)g[u]=g[h]+2*(d-g[p])*t.dispersion}return H("glmAnova",{...a,table:m},...n)}function ww(e,t,n,r=0){let o=new mt,s=e[0],i=Na(s.model.familySpec);if(e.some(y=>{let R=Na(y.model.familySpec);return R.family!==i.family||R.name!==i.name||R.varianceName!==i.varianceName}))throw new RangeError("models were not all fitted with the same family and link");let c=i.fixedDispersion,a=n.test===void 0?n.dispersion!==void 0||c?"Chisq":"F":n.test;if(a!==!1&&!["Rao","LRT","Chisq","F","Cp"].includes(a))throw new RangeError(`invalid 'test': ${String(a)}`);let l=[],f,m,u=[],h=new Set,p;if(t){let y=e[0].model.x.length;if(e.some(T=>T.model.x.length!==y))throw new RangeError("models were not all fitted to the same size of dataset");let R=(T,S)=>T.length===S.length&&T.every((k,L)=>JSON.stringify(k)===JSON.stringify(S[L]));if(e.some(T=>!R(T.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(T=>!R(T.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((T,S)=>l.push({name:String(S+1),dfRes:T.dfResidual,dev:T.deviance})),f=l.map((T,S)=>S===0?NaN:l[S-1].dfRes-T.dfRes),m=l.map((T,S)=>S===0?NaN:l[S-1].dev-T.dev),e.forEach((T,S)=>{if(S===0||f[S]===0)return;let[k,L]=f[S]>0?[e[S-1],T]:[T,e[S-1]],_=C=>C.filter((D,A)=>k.priorWeights[A]>0&&L.priorWeights[A]>0),I=k.model.offset.map((C,D)=>C-L.model.offset[D]),F=I.some((C,D)=>C!==0&&k.priorWeights[D]>0)?k.model.x.map((C,D)=>[...C,I[D]]):k.model.x;gs(_(F),_(L.model.x))||h.add(S)}),a==="Rao"){let T=e.map(S=>({dfRes:S.dfResidual,dev:S.deviance,fit:Gm(S),x:S.model.x,intercept:S.model.intercept}));u=l.map((S,k)=>k===0?NaN:f[k]<0?-ji(T[k],T[k-1]):ji(T[k-1],T[k]))}p=e.reduce((T,S)=>S.dfResidual<T.dfResidual?S:T)}else{let y=s,R=y.model,T=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,S=R.intercept?[0]:[],k=_=>[...S,...Array.from({length:_},(I,F)=>F+S.length)],L=[];for(let _=0;_<=T;_++)_===T?L.push({dfRes:y.dfResidual,dev:y.deviance,fit:Gm(y),x:R.x,intercept:R.intercept}):_>0||a==="Rao"?L.push(dw(R,k(_),o)):L.push(void 0);l.push({name:"NULL",dfRes:y.dfNull,dev:y.nullDeviance});for(let _=1;_<=T;_++)l.push({name:`x${_}`,dfRes:L[_].dfRes,dev:L[_].dev});f=l.map((_,I)=>I===0?NaN:l[I-1].dfRes-_.dfRes),m=l.map((_,I)=>I===0?NaN:Math.max(l[I-1].dev-_.dev,0)),a==="Rao"&&(u=l.map((_,I)=>I===0?NaN:ji(L[I-1],L[I]))),p=y}e.forEach((y,R)=>{y.converged||o.add(`model ${R+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let d=new mt,g=n.dispersion??yo(p,d);d.list.forEach(y=>o.add(y));let w=c?1/0:p.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=l.map((y,R)=>t?[y.dfRes,y.dev,f[R],m[R]]:[f[R],m[R],y.dfRes,y.dev]),E=[...N],b=y=>l.map((R,T)=>{if(T===0||f[T]===0||h.has(T))return NaN;let S=et(y[T]/g*Math.sign(f[T]),-2*r);return S<0||Number.isNaN(S)?NaN:pw(S,Math.abs(f[T]))}),M=[];if(a==="Chisq"||a==="LRT")E.push("Pr(>Chi)"),M=[b(m)];else if(a==="Rao")E.push("Rao","Pr(>Chi)"),M=[u.map((y,R)=>h.has(R)?NaN:y),b(u)];else if(a==="F"){if(c&&o.add(`using F test with a '${i.family}' family is inappropriate`),w!==0){let y=l.map((R,T)=>{let S=et(m[T]/f[T]/g,-2*r);return T===0||f[T]===0||h.has(T)||S<0?NaN:S});E.push("F","Pr(>F)"),M=[y,y.map((R,T)=>Number.isNaN(R)?NaN:wt(R,Math.abs(f[T]),w,!1,!1))]}}else if(a==="Cp"){let y=p.model.x.length;E.push("Cp"),M=[l.map(R=>R.dev+2*(y-R.dfRes)*g)]}let x=v.map((y,R)=>[...y,...M.map(T=>T[R])]);return{colNames:E,rowNames:l.map(y=>y.name),table:x,warnings:o.list}}function Gm(e){return{coefficients:e.coefficients,eta:e.linearPredictors,mu:e.fittedValues,deviance:e.deviance,iter:e.iter,converged:e.converged,boundary:e.boundary,rank:e.rank,qr:e.qr,used:e.weights.map(t=>t>0),workingWeights:e.weights,workingResiduals:e.residuals}}var bw=["type"];function Hm(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let i=e.y.flatMap((p,d)=>e.weights===void 0||e.weights[d]!==0?[d]:[]),c=i.map(p=>e.weights===void 0?1:Math.sqrt(e.weights[p])),a=jt(i.map((p,d)=>s.map(g=>e.x[p][g]*c[d]))),l=il(e.weights===void 0?void 0:i.map(p=>$(e.weights[p],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=vs(f.map(p=>i.map((d,g)=>zt(l[g],e.x[d][s[p]]))),i.map((p,d)=>zt(l[d],$(e.y[p],-r)))),u=[0,0],h=0;return f.forEach((p,d)=>{p>=t.length&&(u=ue(u,_e(m[d],m[d])),h++)}),{ss:u[0],df:h}}function Ki(e,t={}){if(V(t,bw,"Anova"),typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("Anova() needs a fit from lm() with a formula");let n=t.type??2,r;if(n===2||n==="II")r=2;else if(n===3||n==="III")r=3;else throw new RangeError(`invalid type (${String(n)}): use 2 or 3`);let o=e.dfResidual,s=Fs([e]),i=Ds([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=ri(e,s),a=M=>{let x=c.map(([T,S])=>[$(T,-M),S]),y=x.reduce((T,[S])=>T+S,0),R=x.reduce((T,[S,k])=>T+S*k,0)/y;return Number.isFinite(y)&&Number.isFinite(R)?x.reduce((T,[S,k])=>T+S*(k-R)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=Ps(c),f=a(l)),yr(c,i,l)||i===0)throw new Error("residual sum of squares is 0 (within rounding error)");let m=[...e.warnings],u=e.termLabels.length;r===2&&u===0&&e.intercept&&(m.push("the model contains only an intercept: Type III test substituted"),r=3);let h=M=>e.assign.flatMap((x,y)=>M.has(x)?[y]:[]),p=[...new Set(e.assign)],d=[],g=[],w=M=>{d.length=0;let x=[];if(r===3){if(e.coefficients.some(y=>Number.isNaN(y)))throw new Error("there are aliased coefficients in the model");for(let y of p){let R=h(new Set([y])),T=h(new Set(p.filter(S=>S!==y)));d.push(y===0?"(Intercept)":e.termLabels[y-1]),x.push({ss:Hm(e,T,R,s,M).ss,df:R.length})}}else{let y=R=>e.factors.flatMap((T,S)=>T[R-1]>0?[S]:[]);for(let R of p){if(R===0)continue;let T=y(R),S=p.filter(_=>_!==0&&_!==R&&T.every(I=>e.factors[I][_-1]>0)),k=h(new Set(p.filter(_=>_!==R&&!S.includes(_)))),L=Hm(e,k,h(new Set([R])),s,M);d.push(e.termLabels[R-1]),x.push(L)}}return x};g=w(l),l===0&&e.weights!==void 0&&g.some(M=>!Number.isFinite(M.ss))&&(l=Ps(c),g=w(l));let N=M=>$(M,2*s+l),v=$(i,-l),E=v/o,b=g.map(({ss:M,df:x})=>{let y=M/x/E;return[N(M),x,y,wt(y,x,o,!1,!1)]});return b.push([N(v),o,NaN,NaN]),H("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...d,"Residuals"],table:b,warnings:m},e)}var Us=[0,0];var Wi=e=>e.map(t=>t.map(n=>n[0]));function Xm(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function kr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Us);return r.forEach((s,i)=>{let c=t[i];for(let a=0;a<n;a++)o[a]=ue(o[a],_e(s,c[a]))}),o})}function qr(e){let t=Us;return e.forEach((n,r)=>t=ue(t,n[r])),t}function Qm(e){let t=e.length,n=e.map(i=>i.slice()),r=Array.from({length:t},(i,c)=>c),o=1,s=!1;for(let i=0;i<t;i++){let c=i;for(let a=i+1;a<t;a++)Math.abs(n[a][i][0])>Math.abs(n[c][i][0])&&(c=a);if(n[c][i][0]===0){s=!0;continue}c!==i&&([n[i],n[c]]=[n[c],n[i]],[r[i],r[c]]=[r[c],r[i]],o=-o);for(let a=i+1;a<t;a++){let l=lt(n[a][i],n[i][i]);n[a][i]=l;for(let f=i+1;f<t;f++)n[a][f]=Ce(n[a][f],_e(l,n[i][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Zm(e){let{a:t,sign:n,singular:r}=Qm(e);if(r)return Us;let o=[n,0];return t.forEach((s,i)=>o=_e(o,s[i])),o}function Jm(e){let t=e.length,{a:n,perm:r,singular:o}=Qm(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(Us));for(let i=0;i<t;i++){let c=r.map(a=>[a===i?1:0,0]);for(let a=0;a<t;a++)for(let l=0;l<a;l++)c[a]=Ce(c[a],_e(n[a][l],c[l]));for(let a=t-1;a>=0;a--){for(let l=a+1;l<t;l++)c[a]=Ce(c[a],_e(n[a][l],c[l]));c[a]=lt(c[a],n[a][a])}for(let a=0;a<t;a++)s[a][i]=c[a]}return s}var vw=["group","idata","M","X"],eu=(e,t)=>gt(e,t/2,2,!1,!1);function Yi(e){let t=e.length,n=ft(qr(e),t);return Zm(e.map(r=>r.map(o=>lt(o,n))))[0]}function Bi(e,t,n,r){let o=1-(2*t*t+t+2)/(6*t*n),s=(t+2)*(t-1)*(t-2)*(2*t**3+6*t*t+3*r+2)/(288*(n*t*o)**2),i=-n*o*Math.log(e),c=t*(t+1)/2-1,a=eu(i,c),l=eu(i,c+4);return a+s*(l-a)}var Nw=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function yw(e,t,n){let r=e.length,o=w=>w.length===0?0:jt(Array.from({length:r},(N,v)=>w.map(E=>E[v]))).rank,s=n.length===0?void 0:jt(Array.from({length:r},(w,N)=>n.map(v=>v[N]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(w=>n[w]),c=i.length,a=[...i,...t],l=jt(Array.from({length:r},(w,N)=>a.map(v=>v[N])));if(l.rank>o(t))throw new Error("X does not define a subspace of M");let f=l.pivot.slice(0,l.rank).map(w=>a[w]),m=w=>w.map(N=>[N,0]),u=new Array(r).fill([0,0]),h=Jn(f.map(m),u,e.map((w,N)=>e.map(v=>v[N]))).qtv,p=Array.from({length:r},(w,N)=>h.map(v=>v[N])),d=Jn(f.map(m),u,p).qtv,g=Array.from({length:l.rank-c},(w,N)=>c+N);return g.map(w=>g.map(N=>d[w][N]))}function Mw(e,t={}){V(t,vw,"mauchlyTest");let n=e.map(b=>Array.from(b)),r=n[0]?.length??0;if(r===0||n.some(b=>b.length!==r))throw new TypeError("y must be a matrix with at least one column");let o=t.group;if(o!==void 0&&o.length!==n.length)throw new RangeError("'group' must have one label per row of y");let s=b=>b==null||typeof b=="number"&&Number.isNaN(b),i=n.map((b,M)=>!b.some(s)&&(o===void 0||!s(o[M])));if(n.some((b,M)=>i[M]&&b.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=Re(n.filter((b,M)=>i[M])).groups,a=c.length,l=o?.filter((b,M)=>i[M]).map(Rt)??c.map(()=>""),f=[...new Set(l)].sort(Rn),m=c.map(b=>b.map(()=>[0,0]));for(let b of f){let M=l.flatMap((x,y)=>x===b?[y]:[]);for(let x=0;x<r;x++){let y=[0,0];for(let T of M)y=ue(y,[c[T][x],0]);let R=ft(y,M.length);for(let T of M)m[T][x]=Ce([c[T][x],0],R)}}let u=a-f.length;if(u<=0)throw new RangeError(`no residual degrees of freedom: ${a} complete row(s) for ${f.length} group(s)`);let h=Array.from({length:r},(b,M)=>Array.from({length:r},(x,y)=>{let R=[0,0];for(let T of m)R=ue(R,_e(T[M],T[y]));return R})),p=t.idata??{index:Array.from({length:r},(b,M)=>M+1)},d=b=>{let M=je(()=>Kr(b,p));if(M.x.length!==r)throw new RangeError("idata must have one row per column of y");return Nw(M.x,M.colNames.length)},g=t.M===void 0?Array.from({length:r},(b,M)=>Array.from({length:r},(x,y)=>y===M?1:0)):d(t.M),w=d(t.X??"~0"),N=yw(h,g,w),v=N.length;if(v===0)throw new RangeError("X spans M: the test has no dimensions");if(v===1)throw new RangeError("one tested dimension: sphericity holds trivially and the test has no degrees of freedom");if(u<v)throw new RangeError(`too few residual degrees of freedom for the test: ${u} for ${v} dimension(s)`);let E=Yi(N);return{statistic:E,statisticName:"W",pValue:Bi(E,v,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var Ew=["n","d","type","sigLevel","power","alternative"],xw=["n1","n2","d","sigLevel","power","alternative"],Rw=["h","n","sigLevel","power","alternative"],Tw=["h","n1","n2","sigLevel","power","alternative"],Sw=["h","n","sigLevel","power","alternative"],Aw=["d","n","sigLevel","power","alternative"],kw=["n","r","sigLevel","power","alternative"],qw=["w","N","df","sigLevel","power"],Iw=["k","n","f","sigLevel","power"],_w=["u","v","f2","sigLevel","power"],Lw=["size"],Cw={p:[.2,.5,.8],t:[.2,.5,.8],r:[.1,.3,.5],anov:[.1,.25,.4],chisq:[.1,.3,.5],f2:[.02,.15,.35]};function nu(e="p",...t){V(ps(t),Lw,"cohenES");let n=hs(["size"],t).size??"small",r=xe(e,["p","t","r","anov","chisq","f2"]),o=xe(n,["small","medium","large"]);return{test:r,size:o,effectSize:Cw[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function Ow(e,t){if(!Gs(e)||!Gs(t))throw new RangeError("p1 and p2 must be probabilities in [0, 1]");return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function Fw(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(Gs))throw new RangeError("'P0' and 'P1' must be probabilities in [0, 1]");let n=0;for(let r=0;r<e.length;r++)(e[r]!==0||t[r]!==0)&&(n+=Dt(t[r]-e[r],e[r]));return Math.sqrt(n)}function Dw(e){if(e.length===0||e[0].length===0||e.some(s=>s.length!==e[0].length))throw new RangeError("'P' must be a non-empty matrix (rows of the same length)");if(!e.every(s=>s.every(Gs)))throw new RangeError("'P' must contain probabilities in [0, 1]");let t=e.map(s=>s.reduce((i,c)=>i+c,0)),n=e[0]?.length??0,r=Array.from({length:n},(s,i)=>e.reduce((c,a)=>c+a[i],0)),o=0;for(let s=0;s<n;s++)for(let i=0;i<e.length;i++){let c=t[i]*r[s];if(!(c===0&&e[i][s]===0))if(c>=Pw)o+=Dt(e[i][s]-c,c);else{let a=Math.sqrt(t[i]),l=Math.sqrt(r[s]),f=e[i][s]/a/l-a*l;o+=f*f}}return Math.sqrt(o)}var Pw=2**-1022,Gs=e=>e>=0&&e<=1,Vw="\u2018sig.level\u2019 must be numeric in [0, 1]",$w="\u2018power\u2019 must be numeric in [0, 1]";function ir(e,t){let n=r=>!qe(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(Vw);if(n(t))throw new RangeError($w)}function cr(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?nu(t,e).effectSize:e}var Vo=["two.sided","less","greater"];function Hs(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function ru(e,t,n,r){if(r==="less")return No(rn(n,e),e,t,!0);if(r==="greater")return No(rn(n,e,{lowerTail:!1}),e,t,!1);let o=rn(n/2,e,{lowerTail:!1});return No(o,e,t,!1)+No(-o,e,t,!0)}var tu=e=>e<1e-8?oe(e,{lowerTail:!1}):oe(1-e);function ou(e,t,n){return n==="greater"?ie(tu(t)-e,{lowerTail:!1}):n==="less"?ie(oe(t)-e):ie(tu(t/2)-e,{lowerTail:!1})+ie(oe(t/2)-e)}function zw(e){let t=1,n=0;for(let r of e){if(r===0)return 0;let o=Math.floor(Math.log2(Math.abs(r)));t*=$(r,-o),n+=o}return $(t,n)}function jw(e={}){V(e,Ew,"pwrTTest");let{n:t,power:n}=e,r=cr(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),ir(o,n),!qe(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=xe(e.type,["two.sample","one.sample","paired"]),i=xe(e.alternative,Vo),c=s==="two.sample"?2:1;i==="two.sided"&&!qe(r)&&(r=Math.abs(r));let a=(m,u,h)=>ru((m-1)*c,Math.sqrt(m/c)*u,h,i);if(qe(n))n=a(t,r,o);else{let m=n;qe(t)?t=it(u=>a(u,r,o)-m,2+1e-10,1e9):qe(r)?r=it(u=>a(t,u,o)-m,...Hs(i,[1e-7,10])):o=it(u=>a(t,r,u)-m,1e-10,1-1e-10)}let l=s==="paired"?"n is number of *pairs*":s==="two.sample"?"n is number in *each* group":void 0,f={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[s]+" t test power calculation";return{n:t,d:r,sigLevel:o,power:n,alternative:i,...l===void 0?{}:{note:l},method:f,warnings:[]}}function Kw(e={}){V(e,xw,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=cr(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),ir(s,r),!qe(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!qe(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=xe(e.alternative,Vo);i==="two.sided"&&!qe(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>ru(a+l-2,f/Math.sqrt(1/a+1/l),m,i);if(qe(r))r=c(t,n,o,s);else{let a=r;qe(t)?t=it(l=>c(l,n,o,s)-a,2+1e-10,1e9):qe(n)?n=it(l=>c(t,l,o,s)-a,2+1e-10,1e9):qe(o)?o=it(l=>c(t,n,l,s)-a,...Hs(i,[1e-7,10])):s=it(l=>c(t,n,o,l)-a,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:s,power:r,alternative:i,method:"t test power calculation",warnings:[]}}function Ui(e,t,n,r,o,s){let{n:i,power:c}=t,a=cr(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(bn([a,i,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),ir(l,c),!qe(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=xe(t.alternative,Vo);f==="two.sided"&&!qe(a)&&(a=Math.abs(a));let m=(u,h,p)=>ou(u*r(h),p,f);if(qe(c))c=m(a,i,l);else{let u=c;qe(a)?a=it(h=>m(h,i,l)-u,...Hs(f,[s,10])):qe(i)?i=it(h=>m(a,h,l)-u,o,1e9):l=it(h=>m(a,i,h)-u,1e-10,1-1e-10)}return{es:a,n:i,sigLevel:l,power:c,alternative:f}}function Ww(e={}){V(e,Rw,"pwr2pTest");let t=Ui(e.h,e,"h",n=>Math.sqrt(n/2),2+1e-10,1e-10);return{h:t.es,n:t.n,sigLevel:t.sigLevel,power:t.power,alternative:t.alternative,method:"Difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"same sample sizes",warnings:[]}}function Yw(e={}){V(e,Sw,"pwrPTest");let t=Ui(e.h,e,"h",n=>Math.sqrt(n),2+1e-10,1e-10);return{h:t.es,n:t.n,sigLevel:t.sigLevel,power:t.power,alternative:t.alternative,method:"proportion power calculation for binomial distribution (arcsine transformation)",warnings:[]}}function Bw(e={}){V(e,Aw,"pwrNormTest");let t=Ui(e.d,e,"d",n=>Math.sqrt(n),1+1e-10,1e-10);return{d:t.es,n:t.n,sigLevel:t.sigLevel,power:t.power,alternative:t.alternative,method:"Mean power calculation for normal distribution with known variance",warnings:[]}}function Uw(e={}){V(e,Tw,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=cr(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),ir(s,r),!qe(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!qe(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=xe(e.alternative,Vo);i==="two.sided"&&!qe(o)&&(o=Math.abs(o));let c=(l,f)=>{let m=l*f;if(Number.isFinite(m)||!Number.isFinite(l)||!Number.isFinite(f))return m/(l+f);let u=Math.min(l,f);return u/(1+u/Math.max(l,f))},a=(l,f,m,u)=>ou(l*Math.sqrt(c(f,m)),u,i);if(qe(r))r=a(o,t,n,s);else{let l=r;qe(o)?o=it(f=>a(f,t,n,s)-l,...Hs(i,[1e-10,10])):qe(t)?t=it(f=>a(o,f,n,s)-l,2+1e-10,1e9):qe(n)?n=it(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=it(f=>a(o,t,n,f)-l,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:s,power:r,alternative:i,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes",warnings:[]}}function Gw(e={}){V(e,kw,"pwrRTest");let{n:t,power:n}=e,r=cr(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),ir(o,n),!qe(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!qe(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=xe(e.alternative,Vo);s==="two.sided"&&!qe(r)&&(r=Math.abs(r));let i=(a,l,f,m)=>{let u=rn(m?f/2:f,a-2,{lowerTail:!1}),h=u**2+a-2,p=h===1/0&&Number.isFinite(u)?1/Math.sqrt(1+(a-2)/u/u):Math.sqrt(u**2/h),d=Math.atanh(l)+l/(2*(a-1)),g=Math.atanh(p),w=ie((d-g)*Math.sqrt(a-3));return m?w+ie((-d-g)*Math.sqrt(a-3)):w},c=(a,l,f)=>s==="two.sided"?i(a,l,f,!0):i(a,s==="less"?-l:l,f,!1);if(qe(n))n=c(t,r,o),s==="less"&&(r=-r);else{let a=n;if(qe(t))t=it(l=>c(l,r,o)-a,4+1e-10,1e9);else if(qe(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=it(m=>c(t,m,o)-a,l,f)}else o=it(l=>c(t,r,l)-a,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:s,method:"approximate correlation power calculation (arctangh transformation)",warnings:[]}}function Hw(e){V(e,qw,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=cr(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([o,t,n,s],"exactly one of w, N, power, and sig.level must be NULL"),typeof r!="number")throw new TypeError('argument "df" is missing, with no default');if(!(r>0&&r<1/0))throw new RangeError("'df' must be a positive finite number");if(ir(s,n),!qe(o)&&!(o>=0))throw new RangeError("w must be positive");if(!qe(t)&&!(t>=1))throw new RangeError("number of observations must be at least 1");let i=(c,a,l)=>{let f=a*c**2;return f===1/0?1:Mt(Cn(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(qe(n))n=i(o,t,s);else{let c=n;qe(o)?o=it(a=>i(a,t,s)-c,1e-10,1e9):qe(t)?t=it(a=>i(o,a,s)-c,1+1e-10,1e9):s=it(a=>i(o,t,a)-c,1e-10,1-1e-10)}return{w:o,N:t,df:r,sigLevel:s,power:n,method:"Chi squared power calculation",note:"N is the number of observations",warnings:[]}}function Xw(e={}){V(e,Iw,"pwrAnovaTest");let{k:t,n,power:r}=e,o=cr(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),ir(s,r),!qe(o)&&!(o>=0))throw new RangeError("f must be positive");if(!qe(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!qe(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");let i=(c,a,l,f)=>{let m=c-1,u=(a-1)*c,h=c*a*l**2;return!Number.isFinite(h)&&Number.isFinite(c)&&Number.isFinite(a)&&Number.isFinite(l)&&(h=zw([c,a,l,l])),h===1/0?1:wa(m,u)?ba(m,u,h,f):xn(Vr(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(qe(r))r=i(t,n,o,s);else{let c=r;qe(t)?t=it(a=>i(a,n,o,s)-c,2+1e-10,100):qe(n)?n=it(a=>i(t,a,o,s)-c,2+1e-10,1e9):qe(o)?o=it(a=>i(t,n,a,s)-c,1e-7,1e7):s=it(a=>i(t,n,o,a)-c,1e-10,1-1e-10)}return{k:t,n,f:o,sigLevel:s,power:r,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}function Qw(e={}){V(e,_w,"pwrF2Test");let{u:t,v:n,power:r}=e,o=cr(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),ir(s,r),!qe(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!qe(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!qe(n)&&!(n>=1))throw new RangeError("degree of freedom v for denominator must be at least 1");let i=(c,a,l,f)=>{let m=l*(c+a+1);return!Number.isFinite(m)&&Number.isFinite(c)&&Number.isFinite(a)&&Number.isFinite(l)&&(m=2*(l*(c/2+a/2+.5))),m===1/0?1:wa(c,a)?ba(c,a,m,f):xn(Vr(f,c,a,{lowerTail:!1}),c,a,{ncp:m,lowerTail:!1})};if(qe(r))r=i(t,n,o,s);else{let c=r;qe(t)?t=it(a=>i(a,n,o,s)-c,1+1e-10,100):qe(n)?n=it(a=>i(t,a,o,s)-c,1+1e-10,1e9):qe(o)?o=it(a=>i(t,n,a,s)-c,1e-7,1e7):s=it(a=>i(t,n,o,a)-c,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:s,power:r,method:"Multiple regression power calculation",warnings:[]}}var Zw=["id","dv","between","within","type","correction","es","observed"],Xs=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Jw=1e-12,su=1e-24;function au(e,t){let n=Object.prototype.hasOwnProperty.call(e,t)?e[t]:void 0;if(n===void 0)throw new RangeError(`object '${t}' not found`);return Array.from(n)}var $o=e=>typeof e=="boolean"?e?"TRUE":"FALSE":Rt(e);function e1(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map($o):[...new Set(t.map($o))].sort(Rn)}var iu=(e,t)=>t.split(":").includes(e);function cu(e,t,n,r,o){let s=l=>r.some(f=>iu(f,l));for(let l of r)if(!e.some(f=>iu(l,f.name)))throw new RangeError(`Observed variable not in data: ${l}`);let i=[...t.values()].reduce((l,f)=>l+f,0),c=e.reduce((l,f)=>s(f.name)?l+f.ss:l,0),a=["pes","ges"].filter(l=>n.includes(l));return{colNames:["num Df","den Df","MSE","F",...a,"Pr(>F)"],rowNames:e.map(l=>l.name),table:e.map(l=>{let f=a.map(m=>m==="pes"?l.ss/(l.ss+l.errSS):l.ss/(l.ss+i+c-(s(l.name)?l.ss:0)));return[l.numDf,l.denDf,o(l.errSS/l.denDf),l.f,...f,l.p]})}}function t1(e,t){V(t,Zw,"aovEz");let n=Xs(t.between),r=Xs(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[z,G]of[["between",n],["within",r]]){let j=G.filter((Q,ne)=>G.indexOf(Q)!==ne);if(j.length>0)throw new RangeError(`duplicated factor names in '${z}': ${[...new Set(j)].join(", ")}`)}let o=n.filter(z=>r.includes(z));if(o.length>0)throw new RangeError(`factors in both 'between' and 'within': ${o.join(", ")}`);let s=t.type??3;if(s!==2&&s!==3)throw new RangeError(`invalid type (${String(s)}): use 2 or 3`);let i=t.correction??"GG";if(!["GG","HF","none"].includes(i))throw new RangeError(`invalid 'correction': ${String(i)}`);let c=Xs(t.es??"ges");for(let z of c)if(z!=="ges"&&z!=="pes")throw new RangeError(`invalid 'es': ${String(z)}`);let a=Xs(t.observed),l=[],f=z=>z==null||typeof z=="number"&&Number.isNaN(z),m=au(e,t.dv),u=[t.id,...n,...r].map(z=>au(e,z));for(let z of u)if(z.length!==m.length)throw new RangeError("the columns must have the same length");let h=m.map((z,G)=>u.every(j=>!f(j[G]))),p=z=>z.filter((G,j)=>h[j]),d=Ot(p(u[0])).map($o),g=p(m).map(z=>{if(f(z))return NaN;if(typeof z!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(z))throw new RangeError(`the response '${t.dv}' must be finite`);return z}),w=n.map((z,G)=>Ot(p(u[1+G]))),N=r.map((z,G)=>Ot(p(u[1+n.length+G]))),v=d.length;for(let z of[g,...w,...N])if(z.length!==v)throw new RangeError("the columns must have the same length");let E=w.map(z=>e1(z)),b=w.map(z=>z.map($o)),M=N.map(z=>z.map($o)),x=M.map(z=>[...new Set(z)]);E.forEach((z,G)=>{if(z.length<2)throw new Error(`Factor "${n[G]}" consists of one level only. Remove factor from model?`)}),x.forEach((z,G)=>{if(z.length<2)throw new Error(`Factor "${r[G]}" consists of one level only. Remove factor from model?`)});let y=[...new Set(d)],R=new Map,T=new Set;if(d.forEach((z,G)=>{let j=JSON.stringify(b.map(ne=>ne[G])),Q=R.get(z);Q===void 0?R.set(z,j):Q!==j&&T.add(z)}),T.size>0)throw new Error(`Following ids are in more than one between subjects condition:
|
|
8
|
-
${
|
|
9
|
-
${G}`)}let J=y.filter(z=>!C.includes(z)),ce=new Map;d.forEach((z,G)=>ce.has(z)||ce.set(z,G));let W=J.map(z=>{let G=B.get(z);return G.s.map((j,Q)=>ft(j,G.n[Q]))}),te={};n.forEach((z,G)=>te[z]=J.map(j=>b[G][ce.get(j)]));let U=Object.fromEntries(n.map(z=>[z,"contr.sum"])),ve=Object.fromEntries(n.map((z,G)=>[z,E[G]])),X="Rank deficient model matrix; insufficient data to estimate full model.";if(P===1)return H("aovEz",K());return H("aovEz",Z());function K(){let z="y";for(;n.includes(z);)z+="_";let G={...te,[z]:W.map(le=>le[0][0])},j=je(()=>wr(`${z} ~ ${n.length>0?n.join(" * "):"1"}`,G,{contrasts:U,levels:ve}));if(j.rank<j.coefNames.length)throw new Error(X);if(j.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${W.length} subject(s) for ${j.rank} parameter(s)`);let Q=je(()=>Ki(j,{type:s}));l.push(...Q.warnings.filter(le=>!l.includes(le)));let ne=j.dfResidual,be=Q.rowNames.flatMap((le,Ee)=>{if(le==="(Intercept)"||le==="Residuals")return[];let[he,Le,we,Me]=Q.table[Ee];return[{name:le,ss:he,numDf:Le,errSS:j.rss,denDf:ne,f:we,p:Me}]}),Ne=Q.colNames.indexOf("Sum Sq"),me={anova:cu(be,new Map([[0,j.rss]]),c,a,q),univariate:{colNames:Q.colNames,rowNames:Q.rowNames,table:Q.table.map(le=>le.map((Ee,he)=>he===Ne?q(Ee):Ee))},within:r,warnings:l};return Wt.set(me,{colNames:Q.colNames,rowNames:Q.rowNames,table:Q.table}),me}function Z(){let z=J.length,G=W.reduce((ke,$e)=>$e.reduce((He,se)=>He+se[0]*se[0],ke),0),j=n.length>0?je(()=>Kr(`~ ${n.join(" * ")}`,te,{contrasts:U,levels:ve})):{colNames:["(Intercept)"],assign:[0],x:J.map(()=>[1]),termLabels:[],factors:[]},Q=j.colNames.length;if(z===0||jt(j.x).rank<Q)throw new Error(X);let ne=z-Q;if(ne<=0)throw new RangeError(`no residual degrees of freedom: ${z} subject(s) for ${Q} between-subject parameter(s)`);let be=[0,...j.termLabels.map((ke,$e)=>$e+1)],Ne=["(Intercept)",...j.termLabels],me=ke=>j.assign.flatMap(($e,He)=>ke.includes($e)?[He]:[]),le=ke=>j.factors.flatMap(($e,He)=>$e[ke-1]>0?[He]:[]),Ee=ke=>j.x.map($e=>[$e[ke],0]),he=new Array(z).fill([0,0]),Le=(ke,$e,He)=>{let{qtv:se}=Jn([...ke,...$e].map(Ee),he,He);return se.map(Te=>Te.slice(ke.length,ke.length+$e.length))},we=ke=>{let{qtv:$e}=Jn(j.colNames.map((He,se)=>Ee(se)),he,ke);return $e.map(He=>He.slice(Q))},Me=Object.fromEntries(r.map((ke,$e)=>[ke,S.map(He=>He[$e])])),Se=je(()=>Kr(`~ ${r.join(" * ")}`,Me,{contrasts:Object.fromEntries(r.map(ke=>[ke,"contr.sum"])),levels:Object.fromEntries(r.map((ke,$e)=>[ke,x[$e]]))})),Ke=[0,...Se.termLabels.map((ke,$e)=>$e+1)],Pe=["(Intercept)",...Se.termLabels],Ze=ke=>ke.map($e=>ke.map(He=>{let se=[0,0];return $e.forEach((Te,pe)=>se=ue(se,_e(Te,He[pe]))),se})),Be=[],Qe=[],fe=[],ee=[],ge=[],de=[],ae=[],Je=new Map,Oe=!1;for(let ke of Ke){let $e=Se.assign.flatMap((Ve,De)=>Ve===ke?[De]:[]),He=$e.length,se=$e.map(Ve=>W.map(De=>De.reduce((Tt,pt,En)=>ue(Tt,zt(pt,Se.x[En][Ve])),[0,0]))),Te=$e.map(Ve=>$e.map(De=>[Se.x.reduce((Tt,pt)=>Tt+pt[Ve]*pt[De],0),0])),pe=Jm(Te),Ae=Ze(we(se)),We=kr(Ae,pe),Fe=qr(We)[0],nt=NaN,ut=NaN,_t=NaN,Bt=NaN,Wn=!1;if(He>=2){let Ve=Pe[ke],De=cl(Wi(pe)).map(Ut=>Ut.map(nn=>[nn,0])),Tt=Wi(kr(kr(De,Ae),Xm(De))),pt=un(Tt,{symmetric:!0,onlyValues:!0}).values,En=pt.reduce((Ut,nn)=>Math.max(Ut,nn),0),Pt=Math.max(En*Jw,G*su),qt=qr(We),ht=qr(kr(We,We));En>G*su&&ht[0]>0&&Number.isFinite(ht[0])?(Wn=!0,nt=lt(_e(qt,qt),zt(ht,He))[0],ne-He*nt>0?ut=((ne+1)*He*nt-2)/(He*(ne-He*nt)):l.push(`The Huynh-Feldt epsilon of ${Ve} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${Ve} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),Wn&&pt.every(Ut=>Ut>Pt)?(_t=Yi(kr(pe,Ae)),Bt=Bi(_t,He,ne,P)):Wn&&l.push(`The error SSP matrix of ${Ve} is singular: no Mauchly test (the epsilons are still defined)`)}for(let Ve=0;Ve<be.length;Ve++){let De=be[Ve],Tt;if(s===3)Tt=Le(me(be.filter(Lt=>Lt!==De)),me([De]),se);else if(De===0)Tt=Le([],me([0]),se);else{let Lt=le(De),Jt=be.filter(Y=>Y!==0&&Y!==De&&Lt.every(re=>j.factors[re][Y-1]>0));Tt=Le(me(be.filter(Y=>Y!==De&&!Jt.includes(Y))),me([De]),se)}let pt=qr(kr(Ze(Tt),pe))[0],Pt=(De===0?1:me([De]).length)*He,qt=ne*He,ht=pt/Pt/(Fe/qt),Ut=wt(ht,Pt,qt,!1,!1),nn=ke===0?Ne[Ve]:De===0?Pe[ke]:`${Ne[Ve]}:${Pe[ke]}`;Be.push([q(pt),Pt,q(Fe),qt,ht,Ut]),Qe.push([pt,Pt,Fe,qt,ht,Ut]),fe.push(nn),Je.set(ke,Fe);let Zt={name:nn,ss:pt,numDf:Pt,errSS:Fe,denDf:qt,f:ht,p:Ut};if(Wn){let Lt=Math.min(1,ut),Jt=wt(ht,nt*Pt,nt*qt,!1,!1),Y=wt(ht,Lt*Pt,Lt*qt,!1,!1);ee.push([nt,Jt,ut,Y]),ge.push([_t,Bt]),de.push(nn),i==="GG"?Zt={...Zt,numDf:nt*Pt,denDf:nt*qt,p:Jt}:i==="HF"&&(ut>1&&(Oe=!0),Zt=Number.isNaN(ut)?{...Zt,numDf:nt*Pt,denDf:nt*qt,p:Jt}:{...Zt,numDf:Lt*Pt,denDf:Lt*qt,p:Y})}ke===0&&De===0||ae.push(Zt)}}Oe&&l.push("HF eps > 1 treated as 1");let tt={anova:cu(ae,Je,c,a,q),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:fe,table:Be},within:r,warnings:l};return ee.length>0&&(tt.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:de,table:ee},tt.sphericity={colNames:["Test statistic","p-value"],rowNames:[...de],table:ge}),Wt.set(tt,{colNames:tt.univariate.colNames,rowNames:fe,table:Qe}),tt}}var n1=["tau","covariates","covariateNames","confLevel"];function zo(e){return 2*ie(-Math.abs(e))}function mu(e,t,n){return[e-n*t,e+n*t]}function lu(e,t,n){return{estimate:e,se:t,confInt:mu(e,t,n)}}function r1(e,t,n,r){return{estimate:e,confInt:mu(e,t,n),pValue:zo(r)}}function fu(e,t){let n=ar(e.map(l=>l.time),e.map(l=>l.status),{rmean:t}),r=n.rmean[0],o=n.seRmean[0],s=0,i=1,c=Fn();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];mn(c,(1-i)*(f-s),n.time.length+1),i=n.surv[l],s=f}mn(c,(1-i)*(t-s),n.time.length+1);let a=Dn(c);return{rmst:{estimate:r,se:o},rmtl:{estimate:a,se:o}}}function Gi(e,t,n,r){if(n==="difference"){let l=e.estimate-t.estimate,f=Math.hypot(e.se,t.se);return r1(l,f,r,l/f)}let o=e.estimate,s=t.estimate;if(o===0||s===0)return{estimate:o===0?s===0?NaN:0:1/0,confInt:[NaN,NaN],pValue:NaN};if(!(o>0&&s>0))return{estimate:NaN,confInt:[NaN,NaN],pValue:NaN};let i=o/s,c=Math.log(i),a=Math.hypot(e.se/o,t.se/s);return{estimate:i,confInt:[Math.exp(c-r*a),Math.exp(c+r*a)],pValue:zo(c/a)}}function o1(e){let t=new Array(e.length).fill(0);for(let n of[0,1]){let r=e.flatMap((s,i)=>s.arm===n?[i]:[]),o=ar(r.map(s=>e[s].time),r.map(s=>e[s].status===0?1:0),{rmean:"none"});for(let s of r){let i=e[s];if(i.status===0)continue;let c=1;for(let a=0;a<o.time.length&&o.time[a]<=i.time;a++)c=o.surv[a];if(!(c>0))throw new Error("the censoring survival probability is zero");t[s]=1/c}}return t}function Hi(e,t,n,r,o,s){let i=e.map(_=>[_.arm,..._.x]),c=i[0].map((_,I)=>{let F=i.reduce((D,A)=>Math.max(D,Math.abs(A[I])),0);if(F===0)return 1;let C=Math.floor(Math.log2(F));return C>500||C<-500?2**C:1}),a=i.map(_=>_.map((I,F)=>I/c[F])),l=e.map((_,I)=>[1,...a[I]]),f=[1,...c],m=o1(e),u=r?er(a,n,{family:"poisson",weights:m}):wr(a,n,{weights:m});if(r&&!u.converged)throw new Error("the adjusted RMST Poisson model did not converge");let p=u.coefficients.map((_,I)=>_/f[I]),d=u.fittedValues;if(p.some(_=>!Number.isFinite(_))||d.some(_=>!Number.isFinite(_)))throw new Error("the adjusted RMST model is singular or has non-finite fitted values");let g=l.map((_,I)=>_.map(F=>m[I]*F*(n[I]-d[I]))),w=g.map(_=>_.slice());for(let _ of[0,1]){let I=e.flatMap((C,D)=>C.arm===_?[D]:[]),F=ar(I.map(C=>e[C].time),I.map(C=>e[C].status===0?1:0),{rmean:"none"});for(let C=0;C<F.time.length;C++){let D=F.time[C],A=F.nEvent[C];if(!(D<t)||A===0)continue;let O=F.nRisk[C];if(!(O>0))continue;let q=new Array(l[0].length).fill(0);for(let B=0;B<q.length;B++){let J=Fn();for(let ce of I)e[ce].time>=D&&mn(J,g[ce][B],I.length);q[B]=Dn(J)}let P=A/O;for(let B of I){let J=e[B],ce=J.time===D&&J.status===0,W=J.time>=D;for(let te=0;te<q.length;te++)w[B][te]+=q[te]/O*(Number(ce)-(W?P:0))}}}let N=l.map((_,I)=>_.map(F=>F*Math.sqrt(r?d[I]:1))),v=Ra(N),E=Ra(w),b=rt(v),x=Ta(Ta(b,E),b).map((_,I)=>_.map((F,C)=>F/f[I]/f[C])),y=x.map((_,I)=>Math.sqrt(Math.max(0,_[I]))),R=p.map((_,I)=>_/y[I]),T=R.map(zo),S=p.map((_,I)=>_-s*y[I]),k=p.map((_,I)=>_+s*y[I]),L=r?[S.map(Math.exp),k.map(Math.exp)]:[S,k];return{coef:p,cov:x,table:{names:["intercept","arm",...o],estimate:p,se:y,z:R,pValue:T,confInt:L}}}function s1(e,t,n,r={}){if(V(r,n1,"rmst2"),e.length!==t.length||e.length!==n.length)throw new RangeError("'time', 'status' and 'arm' must have the same length");if(r.tau!==void 0&&typeof r.tau!="number")throw new TypeError("'tau' must be a number");if(r.confLevel!==void 0&&typeof r.confLevel!="number")throw new TypeError("'confLevel' must be a number");if(r.covariateNames!==void 0&&!Array.isArray(r.covariateNames))throw new TypeError("'covariateNames' must be an array of strings");let o=r.confLevel??.95;if(!(o>0&&o<1))throw new RangeError("'confLevel' must be between 0 and 1");let s=e.reduce((W,te)=>Number.isFinite(te)?Math.max(W,Math.abs(te)):W,0),i=s>2**500?2**Math.floor(Math.log2(s)):1,a=e.every(W=>!Number.isFinite(W)||W===0||W/i!==0)&&(r.tau===void 0||r.tau===0||r.tau/i!==0)?i:1,l=W=>W/a,f=Array.from(t),m=new Set;for(let W=0;W<f.length;W++){let te=n[W]===!0?1:n[W]===!1?0:n[W];(te===0||te===1)&&f[W]===2&&m.add(te)}let u=r.covariates?.[0],h=r.covariates!==void 0&&typeof u=="number";if(r.covariates!==void 0&&r.covariates.length>0&&!h&&!Array.isArray(u))throw new TypeError("'covariates' must be a numeric vector or a matrix of numeric rows");let p=r.covariates===void 0?void 0:h?r.covariates.map(W=>[W]):r.covariates.map(W=>Array.from(W));if(p&&p.length!==e.length)throw new RangeError("'covariates' must have one row per observation");if(p&&p.some(W=>W.length!==p[0].length))throw new RangeError("'covariates' must have the same number of columns in every row");if(p&&p.some(W=>W.some(te=>typeof te!="number")))throw new TypeError("'covariates' must contain numeric values");if(p&&p.some(W=>W.some(te=>!Number.isFinite(te)&&!Number.isNaN(te))))throw new RangeError("'covariates' must be finite");let d=[],g=[],w=[],N=[];for(let W=0;W<e.length;W++){if(e[W]==null||Number.isNaN(e[W])||f[W]==null||Number.isNaN(f[W])||n[W]==null||Number.isNaN(n[W]))throw new Error("missing value where TRUE/FALSE needed");if(typeof e[W]!="number")throw new TypeError("'time' must contain numbers");if(!Number.isFinite(e[W]))throw new RangeError("'time' must be finite");if(typeof n[W]!="number"&&typeof n[W]!="boolean")throw new TypeError("'arm' must contain numbers or booleans");let te=n[W]===!0?1:n[W]===!1?0:n[W];if(te!==0&&te!==1){w.push(l(e[W]));continue}if(typeof f[W]!="number")throw new TypeError("'status' must contain numbers");let U=f[W],ve=U===1?1:0,X=p?p[W]:[];if(X.some(Z=>!Number.isFinite(Z)))continue;g.push({time:l(e[W]),status:ve,arm:te,x:X,index:W});let K=U;if(m.has(te))if(U===2)K=1;else if(U===1)K=0;else{N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}else if(K!==0&&K!==1){N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}d.push({time:l(e[W]),status:K,arm:te,x:X,index:W})}if(g.some(W=>W.time<0))throw new Error("negative follow-up times are not supported");if(!d.some(W=>W.arm===0)||!d.some(W=>W.arm===1))throw new Error("both arms 0 and 1 are required");let v=[0,1].map(W=>Math.max(...g.filter(te=>te.arm===W).map(te=>te.time))),E=[0,1].map(W=>{let te=v[W];return g.some(U=>U.arm===W&&U.time===te&&U.status===0)}),b=E.some(Boolean)?Math.min(...v.filter((W,te)=>E[te])):Math.max(...v),M=r.tau===void 0,x=r.tau===void 0?b:l(r.tau),y=r.tau===void 0?x*a:r.tau;if(w.some(W=>W<=x))throw new RangeError("'arm' must contain only 0 and 1 through tau");if(!(x>0))throw new RangeError("'tau' must be greater than zero");if(x>b)throw new RangeError(`'tau' must be at most ${b*a}: the minimum of the largest times allowed by both arms`);if(x<Math.min(...g.map(W=>W.time)))throw new RangeError("'tau' must not be below the first observed time");let R=(1-o)/2,T=R<1e-8?oe(R,{lowerTail:!1}):oe(1-R),S=d.filter(W=>W.arm===1),k=d.filter(W=>W.arm===0),L=fu(S,x),_=fu(k,x),I=W=>({estimate:W.estimate*a,se:W.se*a}),F={rmst:I(L.rmst),rmtl:I(L.rmtl)},C={rmst:I(_.rmst),rmtl:I(_.rmtl)},D=lu(F.rmst.estimate,F.rmst.se,T),A=lu(C.rmst.estimate,C.rmst.se,T),O={estimate:F.rmtl.estimate,se:F.rmtl.se,confInt:[y-D.confInt[1],y-D.confInt[0]]},q={estimate:C.rmtl.estimate,se:C.rmtl.se,confInt:[y-A.confInt[1],y-A.confInt[0]]},P=Gi(F.rmst,C.rmst,"difference",T),B=Gi(F.rmst,C.rmst,"ratio",T),J=Gi(F.rmtl,C.rmtl,"rmtlRatio",T),ce={tau:y,tauDefault:M,confLevel:o,arm1:{rmst:D,rmtl:O},arm0:{rmst:A,rmtl:q},difference:P,ratio:B,rmtlRatio:J,warnings:N};if(p){let W=r.covariateNames===void 0?h?["covariates"]:Array.from({length:p[0].length},()=>""):Array.from(r.covariateNames);if(W.some(me=>typeof me!="string"))throw new TypeError("'covariateNames' must contain strings");if(W.length!==p[0].length)throw new RangeError("'covariateNames' must have one name per covariate column");let te=d.map(me=>({...me,time:Math.min(me.time,x),status:me.time>=x?1:me.status})),U=te.map(me=>me.time),ve=te.map(me=>x-me.time),X=Hi(te,x,U,!1,W,T),K=!(F.rmst.estimate>0&&C.rmst.estimate>0),Z=!(F.rmtl.estimate>0&&C.rmtl.estimate>0),z=K?void 0:Hi(te,x,U,!0,W,T),G=Z?void 0:Hi(te,x,ve,!0,W,T),j=(me,le)=>{let Ee=me.coef[1],he=Math.sqrt(Math.max(0,me.cov[1][1]));return le?{estimate:Math.exp(Ee),confInt:[Math.exp(Ee-T*he),Math.exp(Ee+T*he)],pValue:zo(Ee/he)}:{estimate:Ee*a,confInt:[(Ee-T*he)*a,(Ee+T*he)*a],pValue:zo(Ee/he)}},Q=me=>({...me,estimate:me.estimate.map(le=>le*a),se:me.se.map(le=>le*a),confInt:[me.confInt[0].map(le=>le*a),me.confInt[1].map(le=>le*a)]}),ne=me=>({...me,estimate:me.estimate.map((le,Ee)=>Ee===0?le+Math.log(a):le),confInt:[me.confInt[0].map((le,Ee)=>Ee===0?le+Math.log(a):le),me.confInt[1].map((le,Ee)=>Ee===0?le+Math.log(a):le)]}),be={names:["intercept","arm",...W],estimate:Array(X.coef.length).fill(NaN),se:Array(X.coef.length).fill(NaN),z:Array(X.coef.length).fill(NaN),pValue:Array(X.coef.length).fill(NaN),confInt:[Array(X.coef.length).fill(NaN),Array(X.coef.length).fill(NaN)]},Ne={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};ce.adjusted={difference:j(X,!1),ratio:K?Ne:j(z,!0),rmtlRatio:Z?Ne:j(G,!0)},ce.coefficients={difference:Q(X.table),ratio:z?ne(z.table):be,rmtlRatio:G?ne(G.table):be},delete ce.arm1,delete ce.arm0}return H("rmst2",ce)}var a1=["method","confLevel","control"],i1=["eps","iterMax"];function Xi(e,t){let n=t.length,r=0,o=new Array(n).fill(0),s=Array.from({length:n},()=>new Array(n).fill(0));for(let{x:i,cases:c}of e){let a=i.length;if(c===0||c===a)continue;let l=i.map(g=>g.reduce((w,N,v)=>w+N*t[v],0)),f=Math.max(...l),m=new Array(c+1).fill(0),u=new Array(c+1).fill(0),h=Array.from({length:c+1},()=>new Array(n).fill(0)),p=Array.from({length:c+1},()=>Array.from({length:n},()=>new Array(n).fill(0)));m[0]=1;for(let g=0;g<a;g++){let w=i[g],N=Math.exp(l[g]-f);for(let v=Math.min(c,g+1);v>=1;v--){let E=m[v]===0?u[v-1]:Math.max(u[v],u[v-1]),b=m[v]===0?0:2**(u[v]-E),M=N*2**(u[v-1]-E),x=m[v-1],y=h[v-1],R=p[v-1],T=h[v],S=p[v];m[v]=m[v]*b+M*x;for(let k=0;k<n;k++){let L=T[k];T[k]=L*b+M*(w[k]*x+y[k]);for(let _=0;_<n;_++)S[k][_]=S[k][_]*b+M*(w[k]*w[_]*x+w[k]*y[_]+w[_]*y[k]+R[k][_])}if(u[v]=E,m[v]>2**256||m[v]>0&&m[v]<2**-256){let k=Math.floor(Math.log2(m[v])),L=2**-k;m[v]=m[v]*L;for(let _=0;_<n;_++){T[_]=T[_]*L;for(let I=0;I<n;I++)S[_][I]=S[_][I]*L}u[v]=E+k}}}let d=0;for(let g=0;g<c;g++)d+=l[g]-f;r+=d-(Math.log(m[c])+u[c]*Math.LN2);for(let g=0;g<n;g++){let w=0;for(let v=0;v<c;v++)w+=i[v][g];let N=h[c][g]/m[c];o[g]+=w-N;for(let v=0;v<n;v++)s[g][v]+=p[c][g][v]/m[c]-N*(h[c][v]/m[c])}}return{loglik:r,score:o,info:s}}function Qi(e,t){return{statistic:e,df:t,pValue:Mt(e,t,{lowerTail:!1})}}function c1(e,t,n,r={}){V(r,a1,"clogit"),V(Ft(r,"control"),i1,"clogit","control");let{method:o="exact",confLevel:s=.95,control:i={}}=r,{eps:c=1e-9,iterMax:a=20}=i,l=e.length;if(t.length!==l||n.length!==l)throw new RangeError("'y', 'x' and 'strata' must have the same length");if(o!=="exact"&&o!=="approximate"&&o!=="breslow")throw new RangeError(`invalid 'method': ${String(o)}`);e.forEach((O,q)=>{if(O!==0&&O!==1)throw new RangeError(`'y' must be 0 or 1 (element ${q+1})`)});let f=t[0]?.length??0;if(f===0||t.some(O=>O.length!==f||O.some(q=>!Number.isFinite(q))))throw new RangeError("'x' must have the same positive number of finite covariates in every row");let m=oe((1+s)/2),u=(O,q,P,B,J,ce,W,te)=>{let U=q.map((X,K)=>Math.sqrt(X[K])),ve=O.map((X,K)=>X/U[K]);return{coefficients:O,var:q,loglik:P,score:B,wald:J,iter:ce,n:l,nevent:e.reduce((X,K)=>X+K,0),method:W,se:U,z:ve,p:ve.map(X=>Mt(X*X,1,{lowerTail:!1})),lower:O.map((X,K)=>Math.exp(X-m*U[K])),upper:O.map((X,K)=>Math.exp(X+m*U[K])),logtest:Qi(2*(P[1]-P[0]),f),sctest:Qi(B,f),waldtest:Qi(J,f),warnings:te}};if(o!=="exact"){let O=Hr(new Array(l).fill(1),e,t,{ties:"breslow",strata:n,confLevel:s,control:{eps:c,iterMax:a}});return u(O.coefficients,O.var,O.loglik,O.score,O.wald,O.iter,"breslow",O.warnings)}let h=Array.from({length:f},(O,q)=>t.reduce((P,B)=>P+B[q],0)/l),p=new Map;for(let O=0;O<l;O++){let q=p.get(n[O]);q===void 0&&(q={cases:[],controls:[]},p.set(n[O],q)),(e[O]===1?q.cases:q.controls).push(t[O].map((P,B)=>P-h[B]))}let d=[...p.values()].map(O=>({x:[...O.cases,...O.controls],cases:O.cases.length})),g=2**-39,w=O=>{let q=O.map(P=>P.slice());return{f:q,rank:Tr(q,g)}},N=new Array(f).fill(0),v=Xi(d,N),E=[v.loglik,v.loglik],{f:b}=w(v.info),M=Sr(b,v.score),x=v.score.reduce((O,q,P)=>O+q*M[P],0),y=[],R=[N],T=v.loglik,S=N.map((O,q)=>O+M[q]),k=0,L=!1,_=0;for(_=1;_<=a;_++){v=Xi(d,S);let O=Number.isFinite(v.loglik)&&v.score.every(Number.isFinite);if(O&&Math.abs(1-T/v.loglik)<=c){L=!0,N=S;break}!O||v.loglik<T?(k++,S=S.map((q,P)=>(q+k*N[P])/(k+1)),R.push(N)):(k=0,T=v.loglik,{f:b}=w(v.info),M=Sr(b,v.score),N=S,R.push(N),S=S.map((q,P)=>q+M[P]))}L||(_=a,N=R[Math.max(1,a-1)]??N,y.push("Ran out of iterations and did not converge")),v=Xi(d,N),E[1]=v.loglik;let I=w(v.info),F=Gr(I.f),C=N.map((O,q)=>I.rank<f&&F[q][q]===0?NaN:O),D=C.map((O,q)=>Number.isNaN(O)?-1:q).filter(O=>O>=0),A=0;for(let O of D)for(let q of D)A+=N[O]*v.info[O][q]*N[q];return u(C,F,E,x,A,_,"exact",y)}var uu=["transform","terms","collapseTerms","global"],jn=e=>new Array(e).fill(0),lr=e=>Array.from({length:e},()=>jn(e));function l1(e){let t=Array.from({length:e.length},(r,o)=>o).sort((r,o)=>e[r]-e[o]||r-o),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&e[t[o]]===e[t[r]];)o++;let s=(r+1+o)/2;for(let i=r;i<o;i++)n[t[i]]=s;r=o}return n}function hu(e,t){let n=0,r=e.length;for(;n<r;){let o=Math.floor((n+r)/2);e[o]<t?n=o+1:r=o}return n}function f1(e){let t=e.time.length,n=new Array(t),r=new Map;for(let a=0;a<t;a++){let l=e.time[a],f=r.get(l)??{rows:[],deaths:0};f.rows.push(a),e.status[a]&&f.deaths++,r.set(l,f)}let o=[...r.keys()].sort((a,l)=>a-l),s=e.time.slice().sort((a,l)=>a-l),i=(e.start??new Array(t).fill(-1/0)).slice().sort((a,l)=>a-l),c=1;for(let a of o){let l=r.get(a);for(let m of l.rows)n[m]=1-c;let f=hu(i,a)-hu(s,a);l.deaths>0&&(c*=1-l.deaths/f)}return n}function m1(e,t,n,r){let o;if(typeof t=="function"){let s=t(e.time.slice());if(!Array.isArray(s)||s.length!==e.time.length)throw new RangeError("transform function must return one value per observation");o=Array.from(s)}else if(t==="km")o=f1(e);else if(t==="rank")o=l1(e.time);else if(t==="identity")o=e.time.slice();else if(t==="log")e.time.some(s=>s<0)&&r.push("NaNs produced"),o=e.time.map(Math.log);else throw new RangeError("Unrecognized transform");for(let s of n)if(!Number.isFinite(o[s]))throw new RangeError("transform must be finite at every event time");return o}function u1(e,t={}){V(t,uu,"coxZph"),V(t,uu,"coxZph");let n=t.transform??"km",r=t.global??!0,o=t.collapseTerms??!0;if(typeof r!="boolean")throw new TypeError("'global' must be a boolean");if(typeof o!="boolean")throw new TypeError("'collapseTerms' must be a boolean");if(t.terms!==void 0&&t.terms!==!1&&!Array.isArray(t.terms))throw new TypeError("'terms' must be an array of column groups or false");if(e.nevent===0)throw new Error("the proportional-hazards test requires events");let s=e.coxZphData;if(s===void 0)throw new TypeError("coxZph requires a CoxphResult returned by coxph");let i=e.coefficients.map((K,Z)=>Number.isNaN(K)?-1:Z).filter(K=>K>=0),c=i.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let a=[];for(let K=0;K<s.sortedTime.length;){let Z=K;for(;!s.stratumEnd[Z];)Z++;for(let z=K;z<=Z;){let G=s.sortedTime[z],j=z;for(;j<Z&&s.sortedTime[j+1]===G;)j++;let Q=[];for(let ne=z;ne<=j;ne++)s.sortedEvent[ne]&&Q.push(ne);s.start!==void 0&&Q.reverse();for(let ne of Q)a.push(s.sortedToInput[ne]);z=j+1}K=Z+1}if(a.length===0)throw new Error("the proportional-hazards test requires events");let l=a.map(K=>s.time[K]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=m1(s,n,a,f),u=m[a[0]];if(a.every(K=>m[K]===u))throw new Error("the transform is constant at the event times");let h=a.reduce((K,Z)=>Math.max(K,Math.abs(m[Z])),0),p=h===0?0:Math.floor(Math.log2(h)),d=m.map(K=>Number.isFinite(K)?$(K,-p):K),g=a.reduce((K,Z)=>K+d[Z],0)/a.length,w=d.map(K=>K-g);if(a.every(K=>w[K]===0))throw new Error("the transform is constant at the event times");let N=i.map(K=>s.betaScaled[K]),v=lr(c),E=lr(c),b=lr(c),M=jn(c),x=[];for(let K=0;K<s.sortedTime.length;){let Z=K;for(;!s.stratumEnd[Z];)Z++;for(let z=K;z<=Z;){let G=s.sortedTime[z],j=z;for(;j<Z&&s.sortedTime[j+1]===G;)j++;let Q=[];for(let ne=z;ne<=j;ne++)s.sortedEvent[ne]&&Q.push(ne);if(Q.length>0){let ne=[],be=-1/0;for(let ee=K;ee<=Z;ee++){if(!((s.sortedStart===void 0||s.sortedStart[ee]<G)&&s.sortedTime[ee]>=G))continue;ne.push(ee);let de=0;for(let ae=0;ae<c;ae++)de+=N[ae]*s.sortedX[ee][i[ae]];be=Math.max(be,de)}if(ne.length===0||!Number.isFinite(be))throw new Error("empty Cox proportional-hazards risk set");let Ne=jn(c),me=lr(c),le=jn(c),Ee=lr(c),he=0,Le=0;for(let ee of ne){let ge=0;for(let ae=0;ae<c;ae++)ge+=N[ae]*s.sortedX[ee][i[ae]];let de=s.sortedWeight[ee]*Math.exp(ge-be);he+=de;for(let ae=0;ae<c;ae++){let Je=s.sortedX[ee][i[ae]];Ne[ae]+=de*Je;for(let Oe=0;Oe<c;Oe++)me[ae][Oe]+=de*Je*s.sortedX[ee][i[Oe]]}if(s.sortedEvent[ee]&&s.sortedTime[ee]===G){Le+=de;for(let ae=0;ae<c;ae++){let Je=s.sortedX[ee][i[ae]];le[ae]+=de*Je;for(let Oe=0;Oe<c;Oe++)Ee[ae][Oe]+=de*Je*s.sortedX[ee][i[Oe]]}}}let we=0,Me=jn(c);for(let ee of Q){let ge=s.sortedWeight[ee];we+=ge;for(let de=0;de<c;de++)Me[de]+=ge*s.sortedX[ee][i[de]]}let Se=Q.length,Ke=s.ties==="efron"&&Se>1?Se:1,Pe=jn(c),Ze=lr(c);for(let ee=0;ee<Ke;ee++){let ge=ee/Ke,de=he-ge*Le,ae=Ne.map((Oe,tt)=>(Oe-ge*le[tt])/de);for(let Oe=0;Oe<c;Oe++)Pe[Oe]+=ae[Oe]/Ke;let Je=we/Ke;for(let Oe=0;Oe<c;Oe++)for(let tt=0;tt<c;tt++)Ze[Oe][tt]+=Je*((me[Oe][tt]-ge*Ee[Oe][tt])/de-ae[Oe]*ae[tt])}let Be=Me.map((ee,ge)=>ee-we*Pe[ge]),Qe=s.sortedToInput[Q[0]],fe=w[Qe];for(let ee=0;ee<c;ee++){M[ee]+=fe*Be[ee];for(let ge=0;ge<c;ge++)v[ee][ge]+=Ze[ee][ge],E[ee][ge]+=fe*Ze[ee][ge],b[ee][ge]+=fe*fe*Ze[ee][ge]}s.start!==void 0&&Q.reverse();for(let ee of Q){let ge=s.sortedToInput[ee],de=i.map(ae=>s.sortedX[ee][ae]);for(let ae=0;ae<c;ae++)de[ae]-=Pe[ae];x.push({input:ge,time:G,stratum:s.stratumIndex[ee],g:m[ge],residual:de})}}z=j+1}K=Z+1}let y;try{y=rt(v,E)}catch{throw new Error("the proportional-hazards information is singular")}let R=lr(c);for(let K=0;K<c;K++)for(let Z=0;Z<c;Z++){let z=0;for(let G=0;G<c;G++)z+=E[G][K]*y[G][Z];R[K][Z]=b[K][Z]-z}for(let K=0;K<c;K++)for(let Z=0;Z<K;Z++){let z=(R[K][Z]+R[Z][K])/2;R[K][Z]=z,R[Z][K]=z}let T=t.terms===!1||t.terms===void 0?i.map(K=>[K]):t.terms,S=new Set,k=[],L=[];if(T.forEach((K,Z)=>{if(!Array.isArray(K)||K.length===0)throw new RangeError("each terms group must be a non-empty array");let z=[];for(let G of K){if(!Number.isInteger(G)||G<0||G>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(S.has(G))throw new RangeError("terms groups must not overlap");S.add(G),i.includes(G)&&z.push(G)}z.length>0&&(k.push(z),L.push(Z))}),i.forEach(K=>{S.has(K)||(k.push([K]),L.push(T.length))}),k.length===0)throw new Error("the proportional-hazards test has no estimable terms");let _=[];k.forEach((K,Z)=>{if(o&&K.length>1)_.push({col:-1,group:Z,collapsed:!0});else for(let z of K)_.push({col:z,group:Z,collapsed:!1})});let I=_.length,F=Array.from({length:c},()=>jn(I)),C=[],D=[];_.forEach((K,Z)=>{let z=k[K.group],G=L[K.group];if(K.collapsed){for(let j of z){let Q=i.indexOf(j);F[Q][Z]=N[Q]}C.push(1),D.push(`g${G+1}`)}else{let j=i.indexOf(K.col);F[j][Z]=s.inverseCovariateScale[K.col],C.push(e.coefficients[K.col]),D.push(z.length>1?`g${G+1}x${K.col+1}`:`x${K.col+1}`)}});let A=Array.from({length:I},(K,Z)=>{let z=0;for(let G=0;G<c;G++)z=Math.max(z,Math.abs(F[G][Z]));if(!(z>0&&Number.isFinite(z)))throw new Error("the proportional-hazards residual variance is singular");return Math.floor(Math.log2(z))}),O=F.map(K=>K.map((Z,z)=>$(Z,-A[z]))),q=Array.from({length:c},()=>jn(I));for(let K=0;K<c;K++)for(let Z=0;Z<I;Z++)for(let z=0;z<c;z++)q[K][Z]+=v[K][z]*O[z][Z];let P=lr(I);for(let K=0;K<I;K++)for(let Z=0;Z<I;Z++)for(let z=0;z<c;z++)P[K][Z]+=O[z][K]*q[z][Z];for(let K=0;K<I;K++)for(let Z=0;Z<K;Z++){let z=(P[K][Z]+P[Z][K])/2;P[K][Z]=z,P[Z][K]=z}let B;try{B=rt(P)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let K=0;K<I;K++)for(let Z=0;Z<K;Z++){let z=(B[K][Z]+B[Z][K])/2;B[K][Z]=z,B[Z][K]=z}let J=B.map((K,Z)=>K.map((z,G)=>a.length*et(z,-A[Z]-A[G]-s.weightExp))),ce=x.map(K=>{let Z=jn(I);for(let z=0;z<I;z++)for(let G=0;G<c;G++)Z[z]+=O[G][z]*K.residual[G];return C.map((z,G)=>{let j=0;for(let Q=0;Q<I;Q++)j+=Z[Q]*B[Q][G];return z+a.length*et(j,-A[G]-s.weightExp)})}),W=[],te=[],U=[],ve=_.map(K=>K.collapsed?k[K.group].map(Z=>i.indexOf(Z)):[i.indexOf(K.col)]);for(let K of ve){let Z=K.map(Q=>K.map(ne=>R[Q][ne])),z=K.map(Q=>M[Q]),G;try{G=rt(Z,z)}catch{throw new Error("the proportional-hazards term information is singular")}let j=$(Math.max(0,z.reduce((Q,ne,be)=>Q+ne*G[be],0)),s.weightExp);W.push(j),te.push(K.length),U.push(Mt(j,K.length,{lowerTail:!1}))}if(r){let K;try{K=rt(R,M)}catch{throw new Error("the global proportional-hazards information is singular")}let Z=$(Math.max(0,M.reduce((z,G,j)=>z+G*K[j],0)),s.weightExp);W.push(Z),te.push(c),U.push(Mt(Z,c,{lowerTail:!1})),D.push("GLOBAL")}let X={rowNames:D,chisq:W,df:te,p:U,pValue:U,x:x.map(K=>K.g),time:x.map(K=>K.time),...s.strata===void 0?{}:{strata:x.map(K=>Rt(s.strata[K.input]))},y:ce,var:J,transform:n,warnings:f};return H("coxZph",X,e)}var h1=["center"],p1=2**-1022;function d1(e,t,n={}){V(n,h1,"leveneTest");let r=xe(n.center??"median",["median","mean"]),{groups:o}=Zn(e,t);if(o.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let s=l=>{let f=r==="median"?ws(l):st(l);return l.map(m=>Math.abs(m-f))},i=Re(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<p1)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let h=Re([u]);return{d:s(h.groups[0]),k:h.k}}),f=Math.max(...l.map(u=>u.d.some(h=>h>0)?Math.floor(Math.log2(Math.max(...u.d)))+u.k:-1/0)),m=Number.isFinite(f)?f:0;i=l.map(u=>u.d.map(h=>$(h,u.k-m)))}let a=Mm(dr(i));return{colNames:["Df","F value","Pr(>F)"],rowNames:["group",""],table:[[a.df[0],a.fValue,a.pValue],[a.df[1],NaN,NaN]],warnings:[]}}var g1=["score"],w1=["alternative","exact","variant","levels"];function b1(e,t,...n){V(ps(n),g1,"propTrendTest");let{score:r}=hs(["score"],n),o=Array.from(e),s=Array.from(t),i=o.length,c=r===void 0?o.map((N,v)=>v+1):Array.from(r);if(s.length!==i||c.length!==i)throw new RangeError("'x', 'n' and 'score' must have the same length");if(s.some(N=>!(N>0&&N<1/0)))throw new RangeError("elements of 'n' must be positive and finite");if(o.some((N,v)=>!(N>=0&&N<=s[v])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(N=>N===0)||o.every((N,v)=>N===s[v]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let a=s.reduce((N,v)=>N+v,0),l=o.reduce((N,v)=>N+v,0)/a,f=s.map(N=>N/(l*(1-l))),m=o.map((N,v)=>N/s[v]),u=c.flatMap((N,v)=>Number.isNaN(N)?[]:[v]);if(u.length===0)throw new RangeError("no non-missing scores");if(u.some(N=>!Number.isFinite(c[N])))throw new RangeError("non-missing scores must be finite");if(u.every(N=>c[N]===c[u[0]]))throw new RangeError("at least 2 distinct non-missing scores are needed");let h=c,{sw:p,sxy:d,sxx:g}=Zi(f,m,h,u);if(!(g>=2**-1022&&g<1/0&&Math.abs(d)<1/0)){let N=Re([u.map(v=>c[v])]);N.k!==0&&(h=c.slice(),u.forEach((v,E)=>h[v]=N.groups[0][E]),{sw:p,sxy:d,sxx:g}=Zi(f,m,h,u))}let w=Dt(d,g);if(!(a<1/0)||l>0&&l<1&&!(p<1/0&&Math.abs(d)<1/0&&g<1/0)){let{groups:[N,v],k:E}=Re([o,s]),b=N.reduce((x,y)=>x+y,0)/v.reduce((x,y)=>x+y,0),M=Zi(v,m,h,u);w=$(M.sxy/b*(M.sxy/M.sxx/(1-b)),E)}return{method:"Chi-squared Test for Trend in Proportions",statistic:w,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:gt(w,.5,2,!1,!1),warnings:[]}}function Zi(e,t,n,r){let o=r.reduce((l,f)=>l+e[f],0),s=r.reduce((l,f)=>l+e[f]*n[f],0)/o,i=r.reduce((l,f)=>l+e[f]*t[f],0)/o,c=0,a=0;for(let l of r){let f=n[l]-s;c+=e[l]*f*(t[l]-i),a+=e[l]*f*f}return{sw:o,sxy:c,sxx:a}}var v1=2**24,N1=2**32,y1=`Sample size > 100 or data with ties
|
|
10
|
-
p-value based on normal approximation. Specify nperm for permutation p-value`,
|
|
11
|
-
p-value based on normal approximation. Specify nperm for permutation p-value.`;function E1(e,t,n,r=!1){let o=Array.from(e);if(o.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let s=f=>typeof f=="number"&&(r?!Number.isFinite(f):Number.isNaN(f)),i=o.flatMap((f,m)=>s(f)||s(t[m])?[]:[m]);n===void 0&&(t=Ot(t));let c;if(n!==void 0)c=n.map(Rt);else{let f=[...new Set(i.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(Rt):[...new Set(f.map(Rt))].sort(Rn)}let a=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of i){let m=l.get(Rt(t[f]));if(m===void 0)throw new RangeError(`the group '${Rt(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function x1(e,t){let n=new Map;for(let l of t)n.set(l,(n.get(l)??0)+1);let r=[...n.values()],o=t.length,s=(l,f)=>l.reduce((m,u)=>m+f(u),0),i=(o*(o-1)*(2*o+5)-s(e,l=>l*(l-1)*(2*l+5))-s(r,l=>l*(l-1)*(2*l+5)))/72,c=o<3?0:s(e,l=>l*(l-1)*(l-2))*s(r,l=>l*(l-1)*(l-2))/(36*o*(o-1)*(o-2)),a=s(e,l=>l*(l-1))*s(r,l=>l*(l-1))/(8*o*(o-1));return i+c+a}function R1(e,t,n={}){V(n,w1,"jonckheereTest");let r=xe(n.alternative,["two.sided","increasing","decreasing"]),o=n.variant??"DescTools";if(o!=="DescTools"&&o!=="clinfun")throw new RangeError(`invalid variant "${String(o)}"`);if(o==="clinfun"&&n.exact!==void 0)throw new TypeError("clinfun's jonckheere.test has no option 'exact'");let s=E1(e,t,n.levels,o==="clinfun");if(s.filter(N=>N.length>0).length<2)throw new RangeError(o==="clinfun"?"only one group has non-missing data":"all observations are in the same group");let i=s.flat(),c=i.length,a=new Set(i).size<c,l=0,f=[],m=c;s.forEach((N,v)=>{m-=N.length,f.push([N.length,m]);for(let E=v+1;E<s.length;E++)for(let b of N)for(let M of s[E])l+=b<M?1:b===M?.5:0});let u=[],h=!a&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&a&&(h=!1,u.push(M1))):h||u.push(y1);let p,d;if(h){let N=1,v=0;for(let[b,M]of f)b===0||M===0||(v+=N*(b*M+1),N+=b*M);if(N>v1||v>N1)throw new RangeError(`the exact distribution of JT has ${N} values and needs ${v} steps, too many: use exact: false`);let E=[1];for(let[b,M]of f){if(b===0||M===0)continue;let x=Array.from({length:b*M+1},(R,T)=>Kc(T,b,M,!1)),y=new Array(E.length+x.length-1).fill(0);E.forEach((R,T)=>x.forEach((S,k)=>y[T+k]+=R*S)),E=y}p=0,d=0,E.forEach((b,M)=>{M<=l&&(p+=b),M>=l&&(d+=b)})}else{let N=f.reduce((b,[M,x])=>b+M*x,0)/2,v=a?x1(s.map(b=>b.length),i):f.reduce((b,[M,x])=>b+M*x*(M+x+1)/12,0),E=(l-N)/Math.sqrt(v);p=ie(E),d=1-p,d<1e-8&&(d=ie(E,{lowerTail:!1}))}let w=r==="increasing"?d:r==="decreasing"?p:Math.min(1,2*Math.min(d,p,o==="clinfun"?.5:1));return H("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:w,warnings:u})}var T1=["nClasses","adjust"];function jo(e){return Ct(Array.from(e)).sort((t,n)=>t-n)}function pu(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function Qs(e){pu(e);let t=ts(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function S1(e){let t=jo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Qs(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(ie(r[a-1],{logP:!0})+ie(r[n-a],{lowerTail:!1,logP:!0}));let s=-n-o/n,i=(1+.75/n+2.25/n**2)*s,c;return i<.2?c=1-Math.exp(-13.436+101.14*i-223.73*i**2):i<.34?c=1-Math.exp(-8.318+42.796*i-59.938*i**2):i<.6?c=Math.exp(.9177-4.279*i-1.38*i**2):i<10?c=Math.exp(1.2937-5.709*i+.0186*i**2):c=37e-25,{method:"Anderson-Darling normality test",statistic:s,statisticName:"A",pValue:c,warnings:[]}}function A1(e){let t=jo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Qs(t),o=1/(12*n);r.forEach((a,l)=>{o+=(ie(a)-(2*(l+1)-1)/(2*n))**2});let s=(1+.5/n)*o,i=[],c;return s<.0275?c=1-Math.exp(-13.953+775.5*s-12542.61*s**2):s<.051?c=1-Math.exp(-5.903+179.546*s-1515.29*s**2):s<.092?c=Math.exp(.886-31.62*s+10.897*s**2):s<1.1?c=Math.exp(1.111-34.242*s+12.832*s**2):(c=737e-12,i.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),H("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function k1(e){let t=jo(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=Qs(t).map(f=>ie(f)),o=-1/0,s=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),s=Math.max(s,f-m/n)});let i=Math.max(o,s),c=n<=100?i:i*(n/100)**.49,a=n<=100?n:100,l=Math.exp(-7.01256*c**2*(a+2.78019)+2.99587*c*Math.sqrt(a+2.78019)-.122119+.974598/Math.sqrt(a)+1.67997/a);if(l>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*i;f<=.302?l=1:f<=.5?l=2.76773-19.828315*f+80.709644*f**2-138.55152*f**3+81.218052*f**4:f<=.9?l=-4.901232+40.662806*f-97.490286*f**2+94.029866*f**3-32.355711*f**4:f<=1.31?l=6.198765-19.558097*f+23.186922*f**2-12.234627*f**3+2.423045*f**4:l=0}return{method:"Lilliefors (Kolmogorov-Smirnov) normality test",statistic:i,statisticName:"D",pValue:l,warnings:[]}}function q1(e){let t=jo(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(pu(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>oe((u+1-3/8)/(n+1/4))),o=Re([t]).groups[0],s=Math.min(Vc(o,r)**2/(ot(o)*ot(r)),1),i=Math.log(n),c=Math.log(i),a=-1.2725+1.0521*(c-i),l=1.0308-.26758*(c+2/i),f=ie((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function I1(e,t={}){V(t,T1,"pearsonTest");let n=jo(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),s=t.adjust??!0;if(!(Number.isInteger(o)&&o-1-(s?2:0)>0))throw new RangeError("'nClasses' must be an integer leaving positive degrees of freedom");if(o>2147483647)throw new RangeError("attempt to make a table with >= 2^31 elements");let i=[];for(let f of Qs(n)){let m=Math.min(o,Math.floor(1+o*ie(f)));m>=1&&i.push(m)}i.sort((f,m)=>f-m);let c=r/o,a=0;for(let f=1,m=0;f<=o;f++){let u=0;for(;m<i.length&&i[m]===f;)u++,m++;a=a+(u-c)**2/c}let l=o-1-(s?2:0);return{method:"Pearson chi-square normality test",statistic:a,statisticName:"P",pValue:gt(a,l/2,2,!1,!1),warnings:[]}}var _1=["weight"],L1=["exact"],C1=["model","type","unit","r0","confLevel"];function Ko(e,t){let n=e[0]?.length??0;if(e.some(r=>r.length!==n))throw new TypeError(`'${t}' must be a matrix (rows of equal length)`);return Array.from({length:n},(r,o)=>e.map(s=>s[o]))}function Zs(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`);if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError(`'${t}' contains infinite values`)}var gu=e=>2*ie(-Math.abs(e));function O1(e,t={}){V(t,_1,"kappa2");let n=xe(t.weight,["unweighted","equal","squared"]);if(e.some(x=>x.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(x=>!x.some(Number.isNaN)),o=r.length;if(o===0)throw new RangeError("no complete rating pairs");if(o<2)throw new RangeError("at least 2 complete rating pairs are needed");let s=r.map(x=>Pn(x[0])),i=r.map(x=>Pn(x[1])),c=[...new Set([...s,...i])].sort((x,y)=>x<y?-1:x>y?1:0),a=c.length,l=new Map(c.map((x,y)=>[x,y])),f=Array.from({length:a},()=>new Array(a).fill(0));for(let x=0;x<o;x++)f[l.get(s[x])][l.get(i[x])]++;let m=x=>n==="unweighted"?x===0?1:0:n==="equal"?1-x/(a-1):1-x*x/((a-1)*(a-1)),u=Array.from({length:a},(x,y)=>Array.from({length:a},(R,T)=>m(Math.abs(y-T)))),h=f.map(x=>Ie(x)/o),p=Array.from({length:a},(x,y)=>Ie(f.map(R=>R[y]))/o),d=x=>{let y=[];for(let R=0;R<a;R++)for(let T=0;T<a;T++)y.push(x(R,T));return Ie(y)},g=d((x,y)=>f[x][y]*u[x][y])/o,w=d((x,y)=>h[x]*p[y]*u[x][y]),N=(g-w)/(1-w),v=Array.from({length:a},(x,y)=>Ie(p.map((R,T)=>R*u[y][T]))),E=Array.from({length:a},(x,y)=>Ie(h.map((R,T)=>R*u[T][y]))),b=(d((x,y)=>h[x]*p[y]*(u[x][y]-(v[x]+E[y]))**2)-w*w)/(o*(1-w)**2),M=N/Math.sqrt(b);return{value:N,statistic:M,pValue:gu(M),warnings:[]}}function F1(e,t={}){V(t,L1,"kappamFleiss");let n=t.exact??!1,r=Ko(e,"ratings");Zs(e,"ratings");let o=e.length,s=r.length;if(o<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let i=e.flat(),c=Ot(i),a=[...new Set(c)].sort((b,M)=>b-M),l=new Map(a.map((b,M)=>[b,M])),f=new Map(i.map((b,M)=>[b,l.get(c[M])])),m=a.length,u=e.map(b=>{let M=new Array(m).fill(0);for(let x of b)M[f.get(x)]++;return M}),h=Ie(u.map(b=>(Ie(b.map(M=>M*M))-s)/(s*(s-1))))/o,p=Array.from({length:m},(b,M)=>Ie(u.map(x=>x[M]))/(o*s)),d=Ie(p.map(b=>b*b));if(n){let b=r.map(x=>{let y=new Array(m).fill(0);for(let R of x)y[f.get(R)]++;return y.map(R=>R/o)}),M=Array.from({length:m},(x,y)=>ot(b.map(R=>R[y]))*(s-1)/s);d-=Ie(M)/(s-1)}let g=(h-d)/(1-d);if(n)return{value:g,warnings:[]};let w=Ie(p.map(b=>b*(1-b))),N=Ie(p.map(b=>b*(1-b)*(1-b-b))),v=2/(w*w*o*s*(s-1))*(w*w-N),E=g/Math.sqrt(v);return{value:g,statistic:E,pValue:gu(E),warnings:[]}}function D1(e,t={}){V(t,C1,"icc");let n=xe(t.model,["oneway","twoway"]),r=xe(t.type,["consistency","agreement"]),o=xe(t.unit,["single","average"]),s=t.r0??0,i=t.confLevel??.95;It(i),Ko(e,"ratings"),Zs(e,"ratings");let c=Re(e).groups,a=Ko(c,"ratings"),l=c.length,f=a.length;if(l<2||f<2)throw new RangeError("at least 2 subjects and 2 raters are needed");if(!(s>=-1&&s<1))throw new RangeError("'r0' must be in [-1, 1)");let m=1-i,u=ot(c.map(O=>st(O)))*f,h=ot(a.map(O=>st(O)))*l,p=st(c.map(O=>ot(O))),d=ot(c.flat())*(l*f-1),g=d-u*(l-1)-h*(f-1),w=g>2**-20*d?g/((l-1)*(f-1)):z1(c)/((l-1)*(f-1)),N=(O,q,P)=>wt(O,q,P,!1,!1),v=(O,q)=>m/2<1e-8?ln(m/2,O,q,!1,!1):ln(1-m/2,O,q,!0,!1),E=l-1;if(n==="oneway"||r==="consistency"){let O=n==="oneway"?p:w,q=n==="oneway"?l*(f-1):(l-1)*(f-1),P=u/O,B=P/v(E,q),J=P*v(q,E),[ce,W,te]=o==="single"?[(u-O)/(u+(f-1)*O),P*(1-s)/(1+(f-1)*s),U=>(U-1)/(U+f-1)]:[(u-O)/u,P*(1-s),U=>1-1/U];return{value:ce,Fvalue:W,fValue:W,df1:E,df2:q,pValue:N(W,E,q),lbound:te(B),ubound:te(J),warnings:[]}}let b=o==="single"?f:1,M=O=>[b*O/(l*(1-O)),1+b*O*(l-1)/(l*(1-O))],x=(O,q)=>$c([O*h,q*w],[f-1,(l-1)*(f-1)]),y=o==="single"?(u-w)/(u+(f-1)*w+f/l*(h-w)):(u-w)/(u+(h-w)/l),[R,T]=M(s),S=u/(R*h+T*w),k=x(R,T),[L,_]=[f*y/(l*(1-y)),1+f*y*(l-1)/(l*(1-y))],I=x(L,_),F=v(E,I),C=v(I,E),[D,A]=o==="single"?[l*(u-F*w)/(F*(f*h+(f*l-f-l)*w)+l*u),l*(C*u-w)/(f*h+(f*l-f-l)*w+l*C*u)]:[l*(u-F*w)/(F*(h-w)+l*u),l*(C*u-w)/(h-w+l*C*u)];return{value:y,Fvalue:S,fValue:S,df1:E,df2:k,pValue:N(S,E,k),lbound:D,ubound:A,warnings:[]}}function du(e,t){let n=t.length,r=Ie(t.flatMap(s=>t.map(i=>e[s][i])));return(1-Ie(t.map(s=>e[s][s]))/r)*n/(n-1)}function P1(e){let t=Ko(e,"x").length;if(Zs(e,"x"),t<2)throw new RangeError("'x' needs at least two items");if(e.length<2)throw new RangeError("'x' needs at least two subjects");let n=Ms(Re(e).groups),r=Mo(e),o=Array.from({length:t},(c,a)=>a),s=Ie(r.flat()),i;try{let c=rt(r);i=o.map(a=>1-1/c[a][a])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let a=Re(e).groups;i=o.map(l=>{let f=a.map(h=>h[l]),m=ds(jt(a.map(h=>[1,...h.filter((p,d)=>d!==l)])),f),u=st(f);return 1-Ie(m.map(h=>h*h))/Ie(f.map(h=>(h-u)**2))})}return H("cronbachAlpha",{rawAlpha:du(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-Ie(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>du(n,o.filter(a=>a!==c))),warnings:V1(e,r)})}function V1(e,t){let n=[],r=t.map((o,s)=>s).filter(o=>e.some(s=>s[o]!==e[0][o]));if(r.length<t.length&&n.push("Item = had no variance and was deleted but still is counted in the score"),r.length>=2){let{vectors:o}=un(r.map(c=>r.map(a=>t[c][a])),{symmetric:!0}),s=o.map(c=>c[0]),i=Ie(s)<0?-1:1;s.some(c=>i*c<0)&&n.push(`Some items were negatively correlated with the first principal component and probably
|
|
6
|
+
k = 3 groups are required.`);let i=s.n[0],a=s.n.slice(1).map((m,u)=>u+1),c=a.map(m=>{let u=s.n[m],h=[...s.groups[0],...s.groups[m]],p=Mt(h),d=0;for(let T=0;T<i;T++)d+=p[T];let g=i+u,w=g*(g+1)/2-d,N=i*u+i*(i+1)/2-d,b=i*u+u*(u+1)/2-w,E=0;for(let T of Mn(h))E+=T*T*T-T;let v=i*u/(g*(g-1))*((g*g*g-g)/12-E/12),y=(Math.min(N,b)-i*u/2)/Math.sqrt(v);return d<w?-y:y}),l=a.map(m=>s.n[m]/(s.n[m]+i)),f=Dm(c,l,r,o);return J("manyOneUTest",Hi(s,`Wilcoxon, Mann, Whittney U-test
|
|
7
|
+
for multiple comparisons with one control`,c,f,o))}function Vg(e,t){let n=Fn(e,t),r=$r(n.k,(s,i)=>{let a=n.n[s],c=n.n[i],l=[...n.groups[s],...n.groups[i]],f=Mt(l),m=0;for(let w=0;w<a;w++)m+=f[w];let u=m-a*(a+1)/2,h=Math.min(u,a*c-u),p=a+c,d=0;for(let w of Mn(l))d+=w*w*w-w;let g=a*c/(p*(p-1))*((p*p*p-p)/12-d/12);return Math.SQRT2*(h-a*c/2)/Math.sqrt(g)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:Qi(i,n.k,1/0)));return J("dscfAllPairsTest",is(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function $g(e,t,n={}){z(n,Cg,"tamhaneT2Test");let r=(()=>{if(t!==void 0)return Fn(e,t);let f=hr(e),m=f.groups.length;if(m<2)throw new RangeError("there must be at least two groups");let u=f.groups.map(h=>h.length);return{labels:f.labels,groups:f.groups,n:u,N:u.reduce((h,p)=>h+p,0),k:m}})(),o=Rt(r.groups),s=Rt(o.groups),i=n.welch??!0,a=r.k*(r.k-1)/2,c=$r(r.k,(f,m)=>{let u=s.variance[f]/r.n[f]+s.variance[m]/r.n[m];return(s.mean[f]-s.mean[m])/Math.sqrt(u)}),l=$r(r.k,(f,m)=>{let u=c[f-1][m];if(Number.isNaN(u))return NaN;let h=i?ja([s.variance[f],s.variance[m]],[r.n[f],r.n[m]]):r.n[f]+r.n[m]-2,p=2*yn(Math.abs(u),h,{lowerTail:!1});return p>=1?1:-Math.expm1(a*Math.log1p(-p))});return J("tamhaneT2Test",is(r,i?"Tamhane's T2-test for unequal variances":"Tamhane's T2'-test for unequal variances",c,l,i?"T2 (Sidak)":"T2' (Sidak)"))}function zg(e,t){let n=Fn(e,t),r=Rt(n.groups),o=r.mean,s=r.variance.map((c,l)=>c/n.n[l]),i=$r(n.k,(c,l)=>(o[c]-o[l])/Math.sqrt(s[c]+s[l])*Math.SQRT2),a=$r(n.k,(c,l)=>{let f=ja([r.variance[c],r.variance[l]],[n.n[c],n.n[l]]);return Qi(i[c-1][l],n.k,f)});return J("gamesHowellTest",is(n,"Games-Howell test",i,a,"none"))}function Kg(e,t){let n=Fn(e,t),{m:r,s2:o,df:s}=Xi(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let i=$r(n.k,(c,l)=>(r[c]-r[l])/Math.sqrt(o*(1/n.n[c]+1/n.n[l])/2)),a=i.map(c=>c.map(l=>Number.isNaN(l)?NaN:Qi(l,n.k,s)));return J("tukeyTest",is(n,"Tukey's test",i,a,"single-step"))}function Yg(e,t,n={}){z(n,Ig,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=Fn(e,t),i=ha(e,t).statistic,{R:a,C:c}=Gi(s),l=s.N*(s.N+1)/12-c,f=[],m=[];for(let p=1;p<s.k;p++)for(let d=0;d<p;d++)f.push((a[d]-a[p])/Math.sqrt(l*(1/s.n[d]+1/s.n[p]))),m.push(`${s.labels[d]} - ${s.labels[p]}`);let u=f.map(p=>(o?2:1)*pa(Math.abs(p))),h=Wg(u,r);return{chi2:i,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function Wg(e,t){let n=e.length,r=(s,i)=>Math.min(1,-Math.expm1(i*Math.log1p(-s))),o=s=>{let i=e.map((c,l)=>l).sort((c,l)=>(s?e[l]-e[c]:e[c]-e[l])||c-l),a=new Array(n);return i.forEach((c,l)=>a[c]=l>0&&e[c]===e[i[l-1]]?a[i[l-1]]:l+1),a};switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"sidak":return e.map(s=>r(s,n));case"holm":{let s=o(!1);return e.map((i,a)=>Math.min(1,(n+1-s[a])*i))}case"hs":{let s=o(!1);return e.map((i,a)=>r(i,n+1-s[a]))}case"hochberg":{let s=o(!0);return e.map((i,a)=>Math.min(1,s[a]*i))}case"bh":{let s=o(!0);return e.map((i,a)=>Math.min(1,n/(n+1-s[a])*i))}case"by":{let s=o(!0),i=0;for(let a=1;a<=n;a++)i+=1/a;return e.map((a,c)=>Math.min(1,n/(n+1-s[c])*i*a))}default:throw new RangeError(`invalid method "${String(t)}"`)}}function jg(e,t,n={}){z(n,_g,"glhtDunnett");let r=Ge(n.alternative),o=Fn(e,t),{m:s,s2:i,df:a,k:c}=Xi(o),l=Math.sqrt(i),f=o.n.slice(1).map((N,b)=>b+1),m=f.map(N=>s[N]-s[0]),u=f.map(N=>l*Math.sqrt(1/o.n[N]+1/o.n[0])),h=m.map((N,b)=>N/u[b]),p=m.map(N=>W(N,c)),d=u.map(N=>W(N,c)),g=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),w=h.map(N=>r==="two.sided"?Vr(N,g,a,!0):Vr(r==="greater"?N:-N,g,a,!1));return{names:f.map(N=>`${o.labels[N]} - ${o.labels[0]}`),estimate:p,se:d,t:h,p:w,stdError:d,tValue:h,pValue:w,df:a,warnings:[]}}var Vm=["varEqual"],Bg=2**-1022;function zm(e,t,n={}){if(typeof e!="string"&<(t)&&z(t,Vm,"onewayTest"),z(n,Vm,"onewayTest"),typeof e=="string"){let g=jl(e,t);return zm(g.values,g.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=Ir(e,r),s=Rt(o),i=s.groups,a=n.varEqual??!1,c=i.length;if(c<2)throw new RangeError("not enough groups");let l=i.map(g=>g.length);if(l.some(g=>g<2))throw new RangeError("not enough observations");let f=s.mean,m=s.variance,u=l.reduce((g,w)=>g+w,0),h,p,d;if(a){let g=s.grandMean,w=0,N=0;for(let b=0;b<c;b++)w+=l[b]*(f[b]-g)**2,N+=(l[b]-1)*m[b];p=u-c,cs(p,s.ssWithin),h=w/(c-1)/(N/p)}else if(m.some(g=>!(g>=Bg))&&(d=Gg(o))!==void 0){let g=d.map((M,R)=>$m(l[R]/M.variance,-2*M.k)),w=Math.max(...g.map(M=>M.e)),N=g.map(M=>tt(M.m,M.e-w)),b=N.reduce((M,R)=>M+R,0),E=0;for(let M=0;M<c;M++){let R=N.reduce((x,A,S)=>S===M?x:x+A,0);E+=(R/b)**2/(l[M]-1)}E/=c*c-1;let v=[];for(let M=0;M<c;M++)for(let R=M+1;R<c;R++){let[x,A]=d[M].k>=d[R].k?[d[M],d[R]]:[d[R],d[M]],S=x.mean-tt(A.mean,A.k-x.k);if(S===0)continue;let I=$m(S,x.k);v.push({m:g[M].m*g[R].m*I.m*I.m/b,e:g[M].e+g[R].e+2*I.e-w})}let y=Math.max(...v.map(M=>M.e)),T=v.reduce((M,R)=>M+tt(R.m,R.e-y),0);h=(v.length===0?0:tt(T,y))/((c-1)*(1+2*(c-2)*E)),p=1/(3*E)}else if(m.some(g=>g===0)){let g=m.flatMap((E,v)=>E===0?[v]:[]);if(g.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let w=g[0],N=0,b=0;for(let E=0;E<c;E++)E!==w&&(N+=1/(l[E]-1),b+=l[E]/m[E]*(f[E]-f[w])**2);N/=c*c-1,h=b/((c-1)*(1+2*(c-2)*N)),p=1/(3*N)}else{let g=l.map((v,y)=>v/m[y]),w=g.reduce((v,y)=>v+y,0),N=0,b=0;for(let v=0;v<c;v++)N+=(1-g[v]/w)**2/(l[v]-1),b+=g[v]*f[v];N/=c*c-1,b/=w;let E=0;for(let v=0;v<c;v++)E+=g[v]*(f[v]-b)**2;h=E/((c-1)*(1+2*(c-2)*N)),p=1/(3*N)}return{method:a?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:h,statisticName:"F",parameter:[c-1,p],parameterName:["num df","denom df"],pValue:Nt(h,c-1,p,!1,!1),warnings:[]}}function Ug(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=qm(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:Im(r,n),warnings:[]}}function Gg(e){let t=[];for(let n of e){let r=Rt([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function $m(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=tt(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function Zi(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=he(r,[e[o],0]);return r[0]}function Hg([[e,t],[n,r]]){let o=e+n,s=t+r,i=e+t,a=Math.max(0,i-s),c=Math.min(i,o),l=new Float64Array(c-a+1);for(let f=a;f<=c;f++)l[f-a]=En(f,o,s,i,!1);return{lo:a,d:l}}function Qg(e,t){let n=new Float64Array(e.length+t.length-1);for(let r=0;r<e.length;r++){let o=e[r];if(o!==0)for(let s=0;s<t.length;s++)n[r+s]+=o*t[s]}return n}var da=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=Hg(o);n+=s.lo,r=Qg(r,s.d)}this.lo=n,this.hi=n+r.length-1,this.logNull=r.map(Math.log)}density(t){let n=this.logNull.length,r=new Float64Array(n);if(t===0||t===1/0)return r[t===0?0:n-1]=1,r;let o=Math.log(t),s=-1/0;for(let a=0;a<n;a++){let c=this.logNull[a]+(this.lo+a)*o;r[a]=c,c>s&&(s=c)}for(let a=0;a<n;a++)r[a]=Math.exp(r[a]-s);let i=Zi(r,0,n);for(let a=0;a<n;a++)r[a]=r[a]/i;return r}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)r=he(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(t,n,r){let o=this.density(n),s=t-this.lo;return r?Zi(o,s,o.length):Zi(o,0,s+1)}};function Ji(e,t){return dr(e,t)}function Km(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Ji(r=>e.mean(r)-t,n>t)}function ec(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Ji(o=>n-e.cdf(t,o,!1),r<n)}function tc(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Ji(o=>e.cdf(t,o,!0)-n,r>n)}var Xg=["alternative","correct","exact","confLevel"];function Zg(e,t={}){z(t,Xg,"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(nc);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=Ge(t.alternative),a=t.correct??!0,c=t.exact??!1,l=t.confLevel??.95;if(Ct(l),r===2&&o===2){if(c&&e.some(m=>m.some(u=>u.some(h=>!Number.isInteger(h)))))throw new RangeError("the exact test needs integer counts");if(c&&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 c?tw(f,i,l):Jg(f,s,i,a,l)}return nw(e,s,r,o)}function nc(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function jm(e){let t=0,n=0;for(let o of e)for(let s of o)for(let i of s)t=Math.max(t,i),n++;let r=Math.floor(Math.log2(t))+1+Math.ceil(Math.log2(8*n))-1020;return r<=0?0:r+r%2}function Bm(e,t){return e.map(n=>n.map(r=>r.map(o=>W(o,-t))))}function Ym(e,t,n){let r=0,o=0,s=0,i=0,a=0,c=0,l=0,f=0;return e.forEach(([[m,u],[h,p]],d)=>{let g=t[d],w=m+u,N=h+p,b=m+h,E=u+p;r+=m;let v=w*b/g;o+=Number.isFinite(v)?v:w/g*b;let y=w*N*b*E/(g*g*(g-n));s+=Number.isFinite(y)?y:w/g*(N/g)*(b/(g-n))*E;let T=(m+p)/g,M=(u+h)/g,R=m*p/g,x=u*h/g,A=Number.isFinite(R)?R:m/g*p,S=Number.isFinite(x)?x:u/g*h;i+=A,a+=S,c+=T*A,l+=T*S+M*A,f+=M*S}),{sa:r,se:o,sv:s,sR:i,sS:a,sPR:c,sPSQR:l,sQS:f}}function Jg(e,t,n,r,o){let s=Ym(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=jm(e);let T=Bm(e,i);s=Ym(T,T.map(nc),W(1,-i))}let{sa:a,se:c,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,d=a-c,g=W(.5,-i),w=r&&Math.abs(d)>=g?g:0,N=W(Dt(Math.abs(d)-w,l),i),b=n==="two.sided"?vt(N,.5,2,!1,!1):oe(Math.sign(d)*Math.sqrt(N),{lowerTail:n==="less"}),E=f/m,v=W(Math.sqrt(u/(2*f*f)+h/(2*f*m)+p/(2*m*m)),-i/2),y;if(n==="less")y=[0,E*Math.exp(re(o)*v)];else if(n==="greater")y=[E*Math.exp(re(o,{lowerTail:!1})*v),1/0];else{let T=(1-o)/2,M=T<1e-8?re(T,{lowerTail:!1}):re((1+o)/2);y=[E*Math.exp(-M*v),E*Math.exp(M*v)]}return{method:`Mantel-Haenszel chi-squared test ${w!==0?"with":"without"} continuity correction`,alternative:n,statistic:N,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:b,confInt:y,confLevel:o,estimate:E,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var ew=2**24;function tw(e,t,n){let r=e.reduce((c,[[l,f],[m,u]])=>c+Math.min(l,u)+Math.min(f,m),1);if(r>ew)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new da(e),s=e.reduce((c,l)=>c+l[0][0],0),i;if(t==="two.sided"){let c=o.density(1),l=c[s-o.lo]*(1+1e-7),f=[0,0];for(let m of c)m<=l&&(f=he(f,[m,0]));i=Math.min(1,f[0])}else i=o.cdf(s,1,t==="greater");let a;if(t==="less")a=[0,ec(o,s,1-n)];else if(t==="greater")a=[tc(o,s,1-n),1/0];else{let c=(1-n)/2;a=[tc(o,s,c),ec(o,s,c)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:s,statisticName:"S",pValue:i,confInt:a,confLevel:n,estimate:Km(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function nw(e,t,n,r){let{diff:o,V:s}=Wm(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=jm(e);let f=Bm(e,i);({diff:o,V:s}=Wm(f,f.map(nc),n,r,W(1,-i)))}let a=o.length,c=ot(s,o),l=0;for(let f=0;f<a;f++)l+=o[f]*c[f];return l=W(l,i),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:a,parameterName:"df",pValue:vt(l,a/2,2,!1,!1),warnings:[]}}var rw=2**-1022;function Wm(e,t,n,r,o){let s=(n-1)*(r-1),i=new Array(s).fill(0),a=Array.from({length:s},()=>new Array(s).fill(0));return e.forEach((c,l)=>{let f=t[l],m=c.map(p=>p.reduce((d,g)=>d+g,0)),u=Array.from({length:r},(p,d)=>c.reduce((g,w)=>g+w[d],0)),h=1/(f*f*(f-o));for(let p=0;p<r-1;p++)for(let d=0;d<n-1;d++){let g=d+(n-1)*p,w=m[d]*u[p]/f;i[g]+=c[d][p]-(Number.isFinite(w)?w:Cs(m[d],u[p],f));for(let N=0;N<r-1;N++)for(let b=0;b<n-1;b++){let E=(d===b?f*m[d]:0)-m[d]*m[b],v=(p===N?f*u[p]:0)-u[p]*u[N],y=h*E*v;a[g][b+(n-1)*N]+=Number.isFinite(y)&&h>=rw?y:m[d]*((d===b?1:0)-m[b]/f)*(((p===N?1:0)-u[N]/f)*(u[p]/(f-o)))}}}),{diff:i,V:a}}var Um=new Map,ow=2**24,sw=2**32;function Hm(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>ow||n*r>sw)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function aw(e,t){let n=`${e},${t}`,r=Um.get(n);if(r!==void 0)return r;let o=e+t,s=[];for(let u=1;u<=o;u++)s.push(Math.min(u,o+1-u));let i=s.reduce((u,h)=>u+h,0),a=Array.from({length:e+1},()=>new Float64Array(i+1));a[0][0]=1;for(let u=0;u<o;u++){let h=o-u,p=s[u],d=Array.from({length:e+1},()=>new Float64Array(i+1));for(let g=0;g<=e;g++){let w=a[g],N=e-g;if(N>h)continue;let b=N/h;for(let E=0;E<=i;E++){let v=w[E];v!==0&&(N>0&&(d[g+1][E+p]+=v*b),N<h&&(d[g][E]+=v*(1-b)))}}a=d}let c=a[e],l=0;for(;l<c.length&&c[l]===0;)l++;let f=c.length-1;for(;f>l&&c[f]===0;)f--;let m={min:l,probs:c.slice(l,f+1)};return Um.set(n,m),m}function rc(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}=aw(t,n),i=Tl(e)-o,a=0;if(r)for(let c=0;c<=Math.min(i,s.length-1);c++)a+=s[c];else for(let c=Math.max(i+1,0);c<s.length;c++)a+=s[c];return Math.min(a,1)}var Gm=new Map;function iw(e,t){let n=`${e},${t}`,r=Gm.get(n);if(r!==void 0)return r;let o=e+t,s=0;for(let m=1;m<=o;m++)s+=Math.min(m,o+1-m);let i=Array.from({length:e+1},()=>new Float64Array(s+1));i[0][0]=1;for(let m=0;m<o;m++){let u=Math.min(m+1,o-m),h=Array.from({length:e+1},()=>new Float64Array(s+1));for(let p=0;p<=e;p++){if(e-p>o-m)continue;let d=i[p];for(let g=0;g<=s;g++){let w=d[g];w!==0&&(p<e&&(h[p+1][g+u]+=w),e-p<o-m&&(h[p][g]+=w))}}i=h}let a=i[e],c=0;for(;a[c]===0;)c++;let l=a.length-1;for(;a[l]===0;)l--;let f={min:c,counts:a.slice(c,l+1)};return Gm.set(n,f),f}function ga(e,t,n){let{min:r,counts:o}=iw(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=ql(t+n,t),i=0;for(let a=0;a<o.length;a++)if(i+=o[a]/s,i>=e)return r+a;return r+o.length-1}var cw=["alternative","exact","confInt","confLevel"];function Jm(e,t,n={}){if(z(n,cw,"ansariTest"),typeof e=="string"){let v=ur(e,t);return Jm(v.x,v.y,n)}let r=Array.from(e).filter(v=>!Number.isNaN(v)),o=Array.from(t).filter(v=>!Number.isNaN(v)),s=r.length,i=o.length;if(s<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let a=Ge(n.alternative),c=n.confInt??!1,l=n.confLevel??.95;c&&Ct(l);let f=s+i,m=Mt(r.concat(o)),u=m.map(v=>Math.min(v,f-v+1)),h=0;for(let v=0;v<s;v++)h+=u[v];let p=new Set(m).size<f,d=n.exact??(s<50&&i<50&&!p),g=n.exact??(s<50&&i<50),w=new pt,N,b;if(d&&!p){Hm(s,i);let v=T=>rc(T-1,s,i,!1),y=T=>rc(T,s,i,!0);if(a==="two.sided"){let T=Math.floor((s+1)**2/4)+Math.floor(s*i/2)/2;N=Math.min(1,2*(h>T?v(h):y(h)))}else a==="less"?N=v(h):N=y(h);c&&(b=uw(r,o,a,l,w))}else{let v=eu(h,u,p,s,i);N=a==="two.sided"?2*Math.min(oe(v),oe(v,{lowerTail:!1})):a==="less"?oe(v,{lowerTail:!1}):oe(v),c&&(b=hw(r,o,a,l,g?new pt:w)),g&&p&&(w.add("cannot compute exact p-value with ties"),c&&w.add("cannot compute exact confidence interval with ties"))}let E={method:"Ansari-Bradley test",alternative:a,statistic:h,statisticName:"AB",pValue:N,warnings:w.list};return b!==void 0&&(E.confInt=b.confInt,E.confLevel=l,E.estimate=b.estimate,E.estimateNames="ratio of scales"),E.nullValue=1,E.nullValueName="ratio of scales",J("ansariTest",E)}function eu(e,t,n,r,o){let s=r+o;if(!n){let l=s%2===0,f=l?e-r*(s+2)/4:e-r*(s+1)**2/(4*s),m=Math.sqrt(l?r*o*(s+2)*(s-2)/(48*(s-1)):r*o*(s+1)*(3+s*s)/(48*s*s));return f/m}let i=0;for(let l of t)i+=l;let a=i/s,c=0;for(let l of t)c+=(l-a)**2;return(e-r*a)/Math.sqrt(r*o*(c/(s-1))/s)}function tu(e,t,n){let r=e.length,o=r+t.length,s=e.map(f=>f/n),a=n>0&&n<1/0&&s.some((f,m)=>Number.isFinite(e[m])&&(!Number.isFinite(f)||e[m]!==0&&Math.abs(f)<lw))?fw(e.map(f=>Qm(f,n)).concat(t.map(f=>Qm(f,1)))):mw(s.concat(t)),c=a.map(f=>Math.min(f,o-f+1)),l=0;for(let f=0;f<r;f++)l+=c[f];return{ab:l,a:c,ties:new Set(a).size<o}}var lw=2**-1022;function Qm(e,t){if(e===0)return[0,0,0];let n=e>0?1:-1;if(!Number.isFinite(e))return[n,1/0,1];let[r,o]=Xm(Math.abs(e)),[s,i]=Xm(t),a=o/i,c=r-s;return a<1&&(a*=2,c-=1),[n,c,a]}function Xm(e){let t=Math.floor(Math.log2(e)),n=W(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Zm(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 fw(e){let t=e.map((r,o)=>o).sort((r,o)=>Zm(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Zm(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 mw(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return Mt(e);let n=Mt(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function wa(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function uw(e,t,n,r,o){let s=e.length,i=t.length,a=[];for(let w of e)for(let N of t)w/N>=0&&a.push(w/N);a.sort((w,N)=>w-N);let c=a.length;if(c===0)return o.add("cannot compute confidence set, returning NA"),{confInt:wa(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=a.map((w,N)=>tu(e,t,w+(N<c-1?(a[N+1]-w)/2:w*1.01)).ab),f=(w,N,b)=>{let E=1/0;for(let y of l)(N?y-w>0:y-w>=0)&&y-w<E&&(E=y-w);let v=b?-1/0:1/0;return l.forEach((y,T)=>{y-w===E&&(v=b?Math.max(v,a[T]):Math.min(v,a[T]))}),v},m=w=>{let N=ga(w/2,s,i),b=ga(1-w/2,s,i);if(!l.some(y=>y-N>=0)||!l.some(y=>y-b>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=f(N,!1,!1),v=f(b,!0,!0);return E>v?[f(b,!1,!1),f(N,!0,!0)]:[E,v]},u=wa(n,1-r,m),h=ga(.5,s,i),p=-1/0,d=1/0;l.forEach((w,N)=>{w-h<=0?p=Math.max(p,a[N]):d=Math.min(d,a[N])});let g=p===-1/0||d===1/0?NaN:(d+p)/2;return g===1/0&&(g=d/2+p/2),{confInt:u,estimate:g}}function hw(e,t,n,r,o){let s=e.length,i=t.length,a=(w,N)=>{let{ab:b,a:E,ties:v}=tu(e,t,w);return eu(b,E,v,s,i)-N},c=[],l=(w,N)=>{w.length===0||N.length===0||c.push(ln(w)/kt(N),kt(w)/ln(N))};if(l(e.filter(w=>w>0),t.filter(w=>w>0)),l(e.filter(w=>w<=0),t.filter(w=>w<0)),c.some(w=>!Number.isFinite(w))){o.add("cannot compute asymptotic confidence set or estimator");return}if(c.length===0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),{confInt:wa(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=ln(c),m=kt(c),u=w=>Wt(N=>a(N,w),f,m,{tol:1e-4}),h=w=>{let N=re(w/2),b=re(w/2,{lowerTail:!1});if(a(f,N)>0||a(m,b)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=u(N),v=u(b);return E<=v?[E,v]:[v,E]},p=wa(n,1-r,h),d=a(f,0)>0||a(m,0)<0;d&&o.add("cannot compute estimate, returning NA");let g=d?NaN:u(0);return{confInt:p,estimate:g}}var pw=["alternative","confLevel"];function dw(e,t,n={}){z(n,pw,"brunnerMunzelTest");let r=Array.from(e).filter(M=>!Number.isNaN(M)),o=Array.from(t).filter(M=>!Number.isNaN(M)),s=r.length,i=o.length;if(s<2)throw new RangeError("not enough 'x' observations");if(i<2)throw new RangeError("not enough 'y' observations");let a=Ge(n.alternative),c=n.confLevel??.95;fn(c);let l=Mt(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=M=>M.reduce((R,x)=>R+x,0)/M.length,h=u(f),p=u(m),d=(M,R,x)=>{let A=(M.length+1)/2,S=0;for(let I=0;I<M.length;I++)S+=(M[I]-R[I]-x+A)**2;return S/(M.length-1)},g=d(f,Mt(r),h),w=d(m,Mt(o),p),N=(p-(i+1)/2)/s,b=s*g+i*w,E=s*i*(p-h)/(s+i)/Math.sqrt(b),v=b**2/((s*g)**2/(s-1)+(i*w)**2/(i-1)),y;a==="greater"?y=at(E,v,!0,!1):a==="less"?y=at(E,v,!1,!1):y=2*at(-Math.abs(E),v,!0,!1);let T=it((1-c)/2,v,!1,!1)*Math.sqrt(g/(s*i**2)+w/(i*s**2));return{method:"Brunner-Munzel Test",alternative:a,statistic:E,statisticName:"Brunner-Munzel Test Statistic",parameter:v,parameterName:"df",pValue:y,confInt:[N-T,N+T],confLevel:c,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var gw=[1.2337005501361697,7831619385924639e-32];function ww(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let d=f[h*t+p];if(d!==0)for(let g=0;g<t;g++)u[h*t+g]+=d*m[p*t+g]}return u},o=f=>{let m=0;for(let p of f)m=Math.max(m,Math.abs(p));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let p=0;p<f.length;p++)f[p]*=h;return u},s,i=0,a=e.slice(),c=0,l=n;for(;l>0;)l&1&&(s=s===void 0?a.slice():r(s,a),i+=c,i+=o(s)),l>>=1,l>0&&(a=r(a,a),c*=2,c+=o(a));return[s,i]}var bw=2**33;function ru(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>bw)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 ou(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=1/(2*e))return 0;if(t>=1)return 1;if(e*t<1){let[f,m]=Xt(e,t),u=2*f-1+2*m,h=1,p=0;for(let d=1;d<=e;d++)h*=d/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,s=new Float64Array(r*r);for(let f=0;f<r;f++)for(let m=0;m<r;m++)s[f*r+m]=f-m+1>=0?1:0;for(let f=0;f<r;f++)s[f*r]-=Math.pow(o,f+1),s[(r-1)*r+f]-=Math.pow(o,r-f);2*o-1>0&&(s[(r-1)*r]+=Math.pow(2*o-1,r));for(let f=0;f<r;f++)for(let m=0;m<r;m++){let u=f-m+1;if(u>0){let h=1;for(let p=2;p<=u;p++)h*=p;s[f*r+m]/=h}}let[i,a]=ww(s,r,e),c=i[(n-1)*r+n-1],l=a;for(let f=1;f<=e;f++)c=c*f/e,Math.abs(c)<2**-500&&(c*=2**500,l-=500);for(;l>1e3;)c*=2**1e3,l-=1e3;for(;l<-1e3;)c*=2**-1e3,l+=1e3;return c*2**l}function oc(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=ut(gw,Xt(e,e)),s=o[0]+o[1],i=Math.exp(-o[0])*(1-o[1]),a=0;for(let l=2;l<100;l++){let f=Math.exp(-(2*l-1)*(2*l-1)*s);if(a+=f,f<=1e-17*(a+i))break}if(i+a===0)return t?0:1;let c=Math.sqrt(2*Math.PI)/e*(i+a);return t?c:1-c}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 sc(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=0)return 1;if(t>1)return 0;let[n,r]=Xt(e,t),o=Math.floor(e-n-r),s=0;for(let i=0;i<=o;i++){let a=he(zt(e-i,-n),[-r,0]);if(a[0]<=0)break;let c=he(zt(i,n),[r,0]),l=ut(c,[e,0]),f=ut(a,[e,0]);s+=El(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var nu=2**28;function su(e,t){let[n,r]=Xt(e,t),o=Math.floor(n);o===n&&r<0&&(o-=1);let s=[];for(let h=0;h<=e;h++){let p=(h-n-r)/e;p>0&&p<1&&s.push({t:p,up:!0,i:h});let d=(h+n+r)/e;d>0&&d<1&&s.push({t:d,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>nu)return NaN;s.sort((h,p)=>h.t-p.t);let i=o+1,a=new Float64Array(e+1),c=new Float64Array(e+1);a[0]=1;let l=0,f=0,m=0,u=0;for(let h of s){let p=(h.t-f)/(1-f);if(p>0){let d=p/(1-p);c.fill(0);for(let g=l;g<i;g++){let w=a[g];if(w===0)continue;let N=e-g,b=Math.min(N,Math.floor((N+1)*p)),E=i-g,v=dn(b,N,p,!1);for(let y=b;y<=N&&v!==0;y++)y>b&&(v=v*(N-y+1)*d/y),y>=E?m+=w*v:c[g+y]+=w*v,u++;v=b>0?dn(b-1,N,p,!1):0;for(let y=b-1;y>=0&&v!==0;y--)y<b-1&&(v=v*(y+1)/((N-y)*d)),y>=E?m+=w*v:c[g+y]+=w*v,u++}if(u>2*nu)return NaN;[a,c]=[c,a]}if(f=h.t,h.up)i=h.i+1;else{for(let d=l;d<=h.i;d++)m+=a[d],a[d]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function au(e,t,n,r={}){let o=r.alternative??"two.sided";if(Number.isNaN(e)||!(t>=1)||!(n>=1)||t!==Math.floor(t)||n!==Math.floor(n))return NaN;if(e<=0)return 1;if(e>1)return 0;let s=t+n,i;if(r.z!==void 0){if(r.z.length!==s)throw new RangeError("z must have length m + n");let u=[...r.z].sort((h,p)=>h-p);i=new Array(s+1).fill(!1);for(let h=1;h<s;h++)i[h]=u[h]!==u[h-1]}let a=e*t*n*(1-1e-10),c=(u,h)=>{let p=u*n-h*t;return(o==="two.sided"?Math.abs(p):o==="greater"?p:-p)>=a},l=new Float64Array(n+1),f=new Float64Array(n+1),m=0;for(let u=0;u<=t;u++){for(let h=0;h<=n;h++){if(u===0&&h===0){f[0]=1;continue}let p=u+h,d=(u>0?u/p*l[h]:0)+(h>0?h/p*f[h-1]:0);(i===void 0?p<s:i[p])&&c(u,h)?(d>0&&(m+=d*En(u,t,n,p,!1)),f[h]=0):f[h]=d}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var vw=["alternative","exact"];function iu(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 cu(e,t,n={}){if(z(n,vw,"ksTest"),typeof e=="string"){let N=ur(e,t);return cu(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=Ge(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,b=-1/0;for(let M=0;M<o;M++){let R=t(r[M]);if(!(R>=0&&R<=1))throw new RangeError(`the distribution function returned ${R} at ${r[M]}, not a value in [0, 1]`);N=Math.max(N,(M+1)/o-R),b=Math.max(b,R-M/o)}let E=s==="two.sided"?Math.max(N,b):s==="greater"?N:b,v=new Set(r).size<o,y=n.exact??(o<100&&!v),T;if(y&&s==="two.sided"&&E<.5&&ru(o,E),s==="two.sided")if(!y)T=oc(Math.sqrt(o)*E,!1);else{let M=2*sc(o,E);if(E>=.5)T=Math.min(1,M);else{let R=M<.01?su(o,E):NaN;T=Number.isNaN(R)?1-ou(o,E):R}}else T=y?sc(o,E):Math.exp(-2*o*E*E);return J("ksTest",{method:`${y?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:iu(s,!0),exact:y,statistic:E,statisticName:i,pValue:Math.min(1,Math.max(0,T)),warnings:v?["ties should not be present for the one-sample Kolmogorov-Smirnov test"]:[]})}let a=Array.from(t).filter(N=>!Number.isNaN(N)),c=a.length;if(c<1)throw new RangeError("not enough 'y' data");let l=r.map(N=>[N,0]).concat(a.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/c;u=Math.max(u,b),h=Math.max(h,-b)}let p=s==="two.sided"?Math.max(u,h):s==="greater"?u:h,d=new Set(l.map(N=>N[0])).size<l.length,g=n.exact??o*c<1e4,w;if(g)w=au(p,o,c,d?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*c/(o+c);w=s==="two.sided"?oc(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return J("ksTest",{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:iu(s,!1),exact:g,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,w)),warnings:d&&!g?["p-value will be approximate in the presence of ties"]:[]})}var Nw=["poolSd","paired","pAdjustMethod","alternative"],Mw=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],yw=["pAdjustMethod","correct"];function lu(e,t,n){let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=new Map,s=rn(t);for(let a=0;a<r.length;a++){let c=s[a];if(typeof c=="number"&&Number.isNaN(c)||n&&Number.isNaN(r[a]))continue;let l=o.get(c);l===void 0&&o.set(c,l=[]),l.push(r[a])}let i=Array.from(o.keys()).sort(Qn);return{levels:i,groups:i.map(a=>o.get(a))}}function ac(e,t,n){let r=[],o=[];for(let a=0;a<e-1;a++)for(let c=a+1;c<e;c++)r.push(t(c,a)),o.push([c-1,a]);let s=lo(r,n),i=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([a,c],l)=>i[a][c]=s[l]),i}function ic(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(xt),colNames:t.slice(0,-1).map(xt),pAdjustMethod:r,warnings:o}}function Ew(e,t,n={}){z(n,Nw,"pairwiseTTest");let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let s=Ge(n.alternative),i=n.pAdjustMethod??"holm",{levels:a,groups:c}=lu(e,t,!r),l=a.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=c.map(w=>w.length),h=Rt(c),p=h.mean,d=0;for(let w of u)w>1&&(d+=w-1);cs(d,h.ssWithin);let g=Math.sqrt(h.ssWithin/d);f=(w,N)=>{let b=(p[w]-p[N])/(g*Math.sqrt(1/u[w]+1/u[N]));return s==="less"?at(b,d,!0,!1):s==="greater"?at(b,d,!1,!1):2*at(-Math.abs(b),d,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>Ke(()=>Sn(c[u],c[h],{paired:r,alternative:s})).pValue;return ic(m,a,ac(l,f,i),i)}function Tw(e,t,n={}){z(n,Mw,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:a,groups:c}=lu(e,t,!(s.paired??!1)),l=a.length;if(l<2)throw new RangeError("there must be at least two groups");let f="",m=new pt,u=ac(l,(h,p)=>{let d=Ke(()=>ns(c[h],c[p],{...s,...o===void 0?{}:{alternative:o}}));f=d.method;for(let g of d.warnings)m.add(g);return d.pValue},i);return J("pairwiseWilcoxTest",ic(f,a,u,i,m.list))}function Rw(e,t,n={}){if(z(n,yw,"pairwisePropTest"),e.length!==t.length)throw new RangeError("'x' and 'n' must have the same length");let r=[],o=[];e.forEach((m,u)=>{Number.isNaN(m)||Number.isNaN(t[u])||(r.push(m),o.push(t[u]))});let s=r.length;if(s<2)throw new RangeError("too few groups");let i=n.pAdjustMethod??"holm",a=n.correct??!0,c=new pt,l=ac(s,(m,u)=>{let h=Ke(()=>Ri([r[m],r[u]],[o[m],o[u]],{correct:a}));for(let p of h.warnings)c.add(p);return h.pValue},i),f=Array.from({length:s},(m,u)=>u+1);return J("pairwisePropTest",ic("Pairwise comparison of proportions",f,l,i,c.list))}var xw=1e-14;function Sw(e,t,n,r,o,s,i,a){let c=y=>W(y,2*i),l=t.length,f=n.flatMap((y,T)=>Hs(y)[0]>xw*s[T]?[T]:[]),m=0,u=[],h=t.slice(),p=new Array(l).fill(0);if(f.length>0){let y=Yt(Array.from({length:l},(M,R)=>f.map(x=>n[x][R][0])));m=y.rank,u=y.pivot.slice(0,m).map(M=>f[M]);let T=Gs(u.map(M=>n[M]),t);h=T.qty,u.forEach((M,R)=>{for(let x=0;x<l;x++)p[x]=p[x]+n[M][x][0]*T.coef[R][0]})}let d=[];u.forEach((y,T)=>{let M=r[y],R=d.find(x=>x.term===M);R===void 0&&d.push(R={term:M,df:0,ss:[0,0]}),R.df++,R.ss=he(R.ss,_e(h[T],h[T]))});let g=l-m,w=Hs(h,m)[0],N=Fi(p,w,g),b=d.filter(y=>y.term!==0);g>0&&Fr(a,w)&&(w=0,b=b.map(y=>Fr(a,y.ss[0])?{...y,ss:[0,0]}:y));let E=b.map(y=>o[y.term-1]),v=y=>{if(g>0){let T=w/g,M=b.map(({df:R,ss:x})=>{let A=x[0]/R/T;return[R,y(x[0]),y(x[0]/R),A,Nt(A,R,g,!1,!1)]});return M.push([g,y(w),y(T),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...E,"Residuals"],table:M}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:E,table:b.map(({df:T,ss:M})=>[T,y(M[0]),y(M[0]/T)])}};return[v(c),v(y=>y),N]}function fu(e,t,n,r,o,s){if(s.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let i=n??{type:"num",value:1,text:"1"},a=oo({response:t.response,rhs:i},Object.keys(o)).variables,c=_r(t,o,{...s,dropUnusedLevels:!0,contrastVariables:a},i),l=c.y;if(l===void 0)throw new TypeError("the formula has no response");if(l.some(_=>!Number.isFinite(_)))throw new RangeError("NA/NaN/Inf in 'y'");let f=_r({response:t.response,rhs:r},o,{...s.levels===void 0?{}:{levels:s.levels},allContrasts:"contr.helmert",intercept:c.terms.intercept,extraVariables:c.terms.variables,dropUnusedLevels:!0}),m=l.length,u=[...c.warnings,...f.warnings.filter(_=>!c.warnings.includes(_))],h=f.colNames.length,p=f.colNames.map((_,F)=>Re([f.x.map(O=>O[F])]).groups[0]),d=f.x.map((_,F)=>p.map(O=>O[F])),g=h>0?Yt(d):void 0,w=g?.rank??0;w<h&&u.push("Error() model is singular");let N=g?.pivot.slice(0,w)??[],b=_=>_.map(F=>[F,0]),E=c.colNames.map((_,F)=>b(Re([c.x.map(O=>O[F])]).groups[0])),{groups:v,k:y}=Re([l]),{fit:T,qtv:M}=pr(N.map(_=>b(p[_])),b(v[0]),E),R=v[0].map(_=>[1,_]),x=E.map(_=>Hs(_)[0]),A=[];N.forEach((_,F)=>{let O=f.assign[_],D=O===0?"(Intercept)":f.terms.termLabels[O-1],k=A.find(L=>L.name===D);k===void 0&&A.push(k={name:D,idx:[]}),k.idx.push(F)}),m>w&&A.push({name:"Within",idx:Array.from({length:m-w},(_,F)=>w+F)});let S=A.map(_=>Sw(_.name,_.idx.map(F=>T.qty[F]),M.map(F=>_.idx.map(O=>F[O])),c.assign,c.terms.termLabels,x,y,R)),I=S.map(_=>_[0]),C={formula:e,multistratum:!0,strata:I,rows:c.frame.rows,warnings:u};return jt.set(C,S.map(_=>_[1])),S.some((_,F)=>_[2]&&A[F].name!=="(Intercept)")&&Pi.set(C,[Oi]),C}var Aw=["weights","contrasts","levels"],mu=["confLevel"],uu=["sigmaX","sigmaY","mu","alternative","confLevel"];function kw(e,t,n={}){return z(n,Aw,"aov"),typeof e=="string"?J("aov",hu(e,t,n)):J("aov",qw(e,t))}function hu(e,t,n){let r=mr(e,!0),{treatment:o,errors:s}=Yl(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?fu(e,r,o,s[0],t,n):tf(e,_r(r,t,{...n,dropUnusedLevels:!0},o),!0)}function qw(e,t){let{labels:n,groups:r}=hr(e,t);if(r.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=r.flat(),s=r.flatMap((i,a)=>i.map(()=>n[a]));return hu("y ~ g",{y:o,g:s},{levels:{g:n}})}function pu(e){let t=e.length;if(t<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let n=e.reduce((u,h)=>u+h.length,0),{ssBetween:r,ssWithin:o,k:s}=Rt(e),i=t-1,a=n-t,c=r/i,l=o/a,f=c/l,m=u=>W(u,2*s);return{df:[i,a],sumSq:[m(r),m(o)],meanSq:[m(c),m(l)],fValue:f,pValue:a>0?Nt(f,i,a,!1,!1):NaN}}function Iw(e,t,n={}){lt(t)&&z(t,mu,"TukeyHSD"),z(n,mu,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=hr(e,r),i=s.length;if(i<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let a=n.confLevel??.95;fn(a);let l=s.reduce((w,N)=>w+N.length,0)-i;if(l===0)throw new RangeError("no residual degrees of freedom");if(l<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let{ssWithin:f,mean:m,k:u}=Rt(s),h=f/l,p=Ll(a,i,l),d=[],g={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:d,pAdjusted:d,warnings:[]};for(let w=0;w<i-1;w++)for(let N=w+1;N<i;N++){let b=m[N]-m[w],E=Math.sqrt(h/2*(1/s[N].length+1/s[w].length));g.names.push(`${o[N]}-${o[w]}`),g.i.push(N+1),g.j.push(w+1),g.diff.push(W(b,u)),g.lwr.push(W(b-p*E,u)),g.upr.push(W(b+p*E,u)),g.pAdj.push(Os(Math.abs(b)/E,i,l,{lowerTail:!1}))}return g}function _w(e,t,n={}){lt(t)&&z(t,uu,"zTest"),z(n,uu,"zTest");let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:s}=n,i=n.mu??0;if(o===void 0&&r===void 0)throw new RangeError("sigmaX (the known standard deviation of x) is required");if(o===void 0||r!==void 0&&s===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");for(let x of[o,s])if(x!==void 0&&!(x>0&&x<1/0))throw new RangeError("sigmaX and sigmaY must be positive finite numbers");if(Number.isNaN(i))throw new RangeError("mu must be a number");let a=Ge(n.alternative),c=n.confLevel??.95;if(!(c>=0&&c<=1))throw new RangeError("confLevel must be between 0 and 1");let l=Array.from(e).filter(x=>!Number.isNaN(x)),f=r===void 0?void 0:Array.from(r).filter(x=>!Number.isNaN(x));if(l.length<3)throw new RangeError("x needs at least 3 non-missing values");if(f!==void 0&&f.length<3)throw new RangeError("y needs at least 3 non-missing values");let m=Re([l,f??[],[o,s??0]]),u=m.k,[h,p,[d,g]]=m.groups,w=ar(h)[0],N=f===void 0?NaN:ar(p)[0],b=f===void 0?d/Math.sqrt(l.length):Math.sqrt(d**2/l.length+g**2/f.length),E=f===void 0?w:w-N,v=(E-W(i,-u))/b,y,T;if(a==="less")y=oe(v),T=[-1/0,E+re(c)*b];else if(a==="greater")y=oe(v,{lowerTail:!1}),T=[E-re(c)*b,1/0];else{y=2*oe(-Math.abs(v));let x=(1-c)/2,A=(x<1e-8?re(x,{lowerTail:!1}):re(1-(1-c)/2))*b;T=[E-A,E+A]}T=[W(T[0],u),W(T[1],u)];let M=f!==void 0,R=(x,A)=>{if(!(u>0&&x.some((I,C)=>I!==0&&Math.abs(A[C])<2**-1022)))return W(ar(A)[0],u);let S=Math.max(Re([x]).k,0);return W(ar(S===0?x:x.map(I=>W(I,-S)))[0],S)};return{method:M?"Two-sample z-Test":"One-sample z-Test",alternative:a,statistic:v,statisticName:"z",pValue:y,confInt:T,confLevel:c,estimate:M?[R(l,h),R(f,p)]:R(l,h),estimateNames:M?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:M?"difference in means":"mean",warnings:[]}}var Cw=["parm","level","test"],du={epsilon:1e-300,maxit:100,untilCoefficients:!0};function cc(e,t,n,r,o=1e-11){let s=[];for(let d=0;d<t.length;d++)n[d]>0&&s.push(d);let i=0,a=0;for(let d of s)i+=n[d],a+=n[d]*t[d];if(r&&!(Number.isFinite(i)&&Number.isFinite(a))){let d=0;for(let g of s)d=Math.max(d,n[g]);if(Number.isFinite(d)){let g=d>1?Math.floor(Math.log2(d)):0;i=0,a=0;for(let w of s){let N=W(n[w],-g);i+=N,a+=N*t[w]}}}let c=r?a/i:0,l=0;for(let d of s)l+=n[d]*(t[d]-c)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(d=>Math.sqrt(n[d])),u=Yt(s.map((d,g)=>e[d].map(w=>w*m[g])),{tol:o}),h=Ws(u,s.map((d,g)=>t[d]*m[g])),p=0;for(let d of h)p+=d*d;if(!Number.isFinite(l)){let d=s.map((w,N)=>m[N]*(t[w]-c)),g=0;for(let w of d)g=Math.max(g,Math.abs(w));for(let w of h)g=Math.max(g,Math.abs(w));if(Number.isFinite(g)){let w=g>1?Math.floor(Math.log2(g)):0,N=0;for(let E of d)N+=W(E,-w)**2;let b=0;for(let E of h)b+=W(E,-w)**2;return W(W(N-b,w),w)}}return l-p}function Lw(e,t={}){z(t,Cw,"glmConfint");let{fit:n,k:r}=Qs(e),o=t.level??.95,s=t.test??"LRT";if(s!=="LRT"&&s!=="Rao")throw new RangeError(`invalid 'test': ${String(s)}`);if(!(o>0&&o<1))throw new RangeError("'level' must be in (0, 1)");let i=n.coefficients.length,a=t.parm??Array.from({length:i},(C,_)=>_);for(let C of a)if(!Number.isInteger(C)||C<0||C>=i)throw new RangeError(`invalid coefficient index ${C}`);let c=n.model,l=n.coefficients.map((C,_)=>_).filter(C=>!Number.isNaN(n.coefficients[C])),f=new pt,m=Cr(c,l,f,{...du,start:c.start===void 0?void 0:l.map(C=>c.start[C])}),u=new Array(i).fill(NaN);if(l.forEach((C,_)=>u[C]=m.fit.coefficients[_]),!m.fit.converged)return{lower:a.map(()=>NaN),upper:a.map(()=>NaN)};let h=!1,p={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},d=Ke(()=>ao(p)),g=d.dispersion,w=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((C,_)=>w[l[C]]=d.stdError[_]);let b=c.x.map(C=>l.map(_=>C[_])),E=m.fit.eta,v=(C,_)=>{let F=l.filter(k=>k!==C),O=Cr(c,F,new pt,{...du,start:void 0,etastart:E,extraOffset:c.x.map(k=>k[C]*_)}).fit;if(!O.converged)return h=!0,NaN;let D=s==="LRT"?O.deviance-m.fit.deviance:cc(b,O.workingResiduals,O.workingWeights,!1);return Math.sign(_-u[C])*Math.sqrt(Math.max(D/g,0))},y=(1-o)/2,T=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),M=2**-9,R=T<M,x=R?[-T,T]:[re(y),y<1e-8?re(y,{lowerTail:!1}):re((1+o)/2)],A=a.map(()=>NaN),S=a.map(()=>NaN),I=(C,_,F)=>{let O=F===0?-1:1;h=!1;let D=NaN,k=NaN;for(let K=0;K<=30;K++){let X=u[C]+O*2**K*w[C],Y=v(C,X);if(Math.abs(Y)>=Math.abs(_)){D=X,k=Y;break}}if(Number.isNaN(D)||h)return NaN;let L=K=>K===u[C]?-_:v(C,K)-_,[q,$]=F===0?[D,u[C]]:[u[C],D],[V,P]=F===0?[k-_,-_]:[-_,k-_],Q;try{Q=Wt(L,q,$,{tol:1e-14*w[C],maxIter:500,fLower:V,fUpper:P})}catch(K){if(!h)throw K;Q=NaN}return h?NaN:Q};return a.forEach((C,_)=>{if(Number.isNaN(u[C]))return;if(!R){A[_]=I(C,x[0],0),S[_]=I(C,x[1],1);return}let F=[-2*M,-M,0,M,2*M],O=[I(C,-2*M,0),I(C,-M,0),u[C],I(C,M,1),I(C,2*M,1)].map(k=>k-u[C]),D=k=>{let L=0;for(let q=0;q<5;q++){let $=1;for(let V=0;V<5;V++)V!==q&&($*=(k-F[V])/(F[q]-F[V]));L+=$*O[q]}return u[C]+L};A[_]=D(x[0]),S[_]=D(x[1])}),{lower:A.map(C=>W(C,r)),upper:S.map(C=>W(C,r))}}var Ow=["scope","direction","k","scale","steps"],Fw=["lower","upper"],Pw=["scope","direction","k","scale","steps"],Dw=["lower","upper"],Vw=["scope","test","k","scale"],$w=["scope","test","k","scale"],zw=["k","scale"],Bt=e=>"linearPredictors"in e;function uc(e,t,n){if(typeof e!="object"||e===null||typeof e.formula!="string"||!Array.isArray(e.termLabels))throw new TypeError(`${n} needs a fit of lm or glm with a formula`);if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let r=Bt(e),o=ef(e);if(o===void 0&&((r?e.model.priorGiven.some(f=>f!==1)||e.model.hasOffset:e.weights!==void 0)||r))throw new TypeError(`${n} needs the fit as returned by lm or glm (its options are not known)`);let s=e.formula.indexOf("~"),i=e.formula.slice(0,s).trim(),a=r?e.model.intercept:e.coefNames[0]==="(Intercept)",c=Object.values(t)[0]?.length??0;return{data:t,glm:r,lhs:i,intercept:a,options:o??{},nAll:c}}function lc(e,t){return t.length===0?`${e.lhs} ~ ${e.intercept?"1":"0"}`:`${e.lhs} ~ ${t.join(" + ")}${e.intercept?"":" - 1"}`}function gu(e,t){let n=s=>s instanceof Float64Array?Float64Array.from(t,i=>s[i]):Array.isArray(s)&&s.length===e.nAll?t.map(i=>s[i]):s,r=Object.fromEntries(Object.entries(e.data).map(([s,i])=>[s,n(i)])),o={...e.options};for(let s of["weights","offset"])typeof o[s]=="object"&&o[s]!==null&&(o[s]=n(o[s]));return{data:r,options:o}}function ls(e,t,n){let{data:r,options:o}=n===void 0?{data:e.data,options:{...e.options}}:gu(e,n),s=lc(e,t),i=Ke(()=>e.glm?xn(s,r,o):Xn(s,r,o));if(n!==void 0){let c=(e.glm?i.model.rows:i.rows).map(l=>n[l]);e.glm?i.model.rows=c:i.rows=c}return i}var wu=e=>Bt(e)?e.model.rows:e.rows,hc=e=>wu(e).length,ba=e=>Bt(e)?e.priorWeights.filter(t=>t>0).length:e.nObs,br=e=>Bt(e)?e.deviance:e.rss,bu=e=>e.rank;function pc(e){let t=e.rss;if(t>1e-290&&t<1/0)return Math.log(t);let n=e.weights,r=[];if(e.residuals.forEach((s,i)=>{let a=n?.[i]??1;a>0&&s!==0&&r.push(Math.log(Math.abs(s))+.5*Math.log(a))}),r.length===0)return-1/0;let o=Math.max(...r);return 2*o+Math.log(r.reduce((s,i)=>s+Math.exp(2*(i-o)),0))}function Kw(e){return e.rss>1e-290&&e.rss<1/0?Math.log(e.rss):e.yScale!==void 0&&e.rssScaled!==void 0&&e.rssScaled>0?Math.log(e.rssScaled)+2*e.yScale*Math.LN2:Math.log(e.rss)}function va(e,t,n){let r=hc(e),o=r-e.dfResidual;return Bt(e)?[o,e.aic+(t-2)*o]:n>0?[o,e.rss/n-r+t*o]:[o,r*(pc(e)-Math.log(r))+t*o]}function ms(e){return e.termLabels.map((t,n)=>{let r=e.variables.filter((o,s)=>(e.factors[s]?.[n]??0)!==0);return{label:t,vars:r,key:[...r].sort().join("\0")}})}function fs(e,t,n){if(typeof e!="string")throw new TypeError(`the ${n} scope must be a formula such as "~ x1 + x2"`);let r=e.includes("~")?e:`~ ${e}`,o=mr(r),s=a=>typeof a=="object"&&a!==null&&(a.type==="dot"||Object.values(a).some(c=>typeof c=="object"&&s(c)));if(s(o.rhs))throw new RangeError(`'.' is not supported in the ${n} scope: list its terms`);let i=oo({response:void 0,rhs:o.rhs},Object.keys(t));return i.termLabels.map((a,c)=>{let l=i.variables.filter((f,m)=>(i.factors[m]?.[c]??0)!==0);return{label:a,vars:l,key:[...l].sort().join("\0")}})}var vu=(e,t)=>t.vars.length<e.vars.length&&t.vars.every(n=>e.vars.includes(n));function Nu(e,t){return e.filter(n=>!t.some(r=>r.key===n.key)&&!e.some(r=>vu(r,n)))}function Mu(e,t){let n=r=>e.some(o=>o.key===r.key);return t.filter(r=>!n(r)&&t.every(o=>!vu(r,o)||n(o)))}function yu(e,t){if(Bt(e))return;let n=e.weights,r=0;e.y.forEach((o,s)=>r+=(n?.[s]??1)*o*o),e.rss<1e-10*r&&t.add("attempting model selection on an essentially perfect fit is nonsense")}function mo(e,t){for(let n of t.warnings)(Bt(t)||n!=="essentially perfect fit: summary may be unreliable")&&e.add(n)}var Eu=e=>e.priorWeights.filter(t=>t>0).length,dc=e=>e.family==="binomial"||e.family==="poisson";function Tu(e,t){return e.family==="gaussian"?Eu(e)*Math.log(t/e.deviance):dc(e)?t-e.deviance:(t-e.deviance)/Go(e)}function fc(e,t,n){let r=hc(e),o=bu(e),s=Bt(e)?NaN:pc(e),i=Bt(e)?e.aic+(t-2)*o:n>0?e.rss/n-r+t*o:r*(s-Math.log(r))+t*o;return{label:"<none>",df:NaN,sumOfSq:NaN,deviance:br(e),aic:i,rank:o,dfResidual:e.dfResidual,logDev:s,fit:e}}function mc(e,t,n,r,o,s,i,a,c=Math.log(n),l){let f=e.fit,m=s?r-e.rank:e.rank-r,u;if(Bt(f))u=e.aic+Tu(f,n)+i*(r-e.rank);else{let h=hc(f);u=a>0?n/a-h+i*r:h*(c-Math.log(h))+i*r}return{label:t,df:m,sumOfSq:s?e.deviance-n:n-e.deviance,deviance:n,aic:u,rank:r,dfResidual:o,logDev:c,fit:l}}function Ru(e,t){Bt(e)&&!dc(e)&&Go(e,t)}function xu(e,t,n,r,o){yu(e,o),Ru(e,o);let s=fc(e,n,r),i=ms(e),a=[s];for(let c of t){let l=i.findIndex(m=>m.key===c.key)+1,f=e.assign.flatMap((m,u)=>m===l?[]:[u]);if(Bt(e)){let m=new pt,u=Cr(e.model,f,m);for(let h of m.list)o.add(h);a.push(mc(s,c.label,u.fit.deviance,u.fit.rank,Eu(e)-u.fit.rank,!1,n,r))}else{let m=nf(e.x.map(u=>f.map(h=>u[h])),e.y,e.weights,e.coefNames[0]==="(Intercept)"&&f[0]===0,!0);a.push(mc(s,c.label,m.rss,m.result.rank,m.result.dfResidual,!1,n,r,Kw(m)))}}return a}function Su(e,t,n,r,o,s){let i=wu(t),a=t,c=[...new Set(n.flatMap(m=>m.vars))].filter(m=>!t.variables.includes(m));if(c.length>0){let m=e.options.levels,u=gu(e,i).data,h=Vs(u,c,m===void 0?{}:{levels:m}).rows.map(p=>i[p]);h.length<i.length&&(s.add(`using the ${h.length}/${i.length} rows from a combined fit`),i=h,a=ls(e,t.termLabels,i))}yu(a,s),Ru(a,s);let l=fc(a,r,o);Bt(a)&&a!==t&&(l.aic=fc(t,r,o).aic);let f=[l];for(let m of n){let u=ls(e,[...t.termLabels,m.label],i);mo(s,u),f.push(mc(l,m.label,br(u),bu(u),u.dfResidual,!0,r,o,Bt(u)?NaN:pc(u),u))}return f}function Au(e,t,n){let r=e[0],o=r.fit,s=[NaN],i=[NaN];for(let c of e.slice(1)){let l=n?c:r,f=Math.abs(c.deviance-r.deviance),m,u;t==="F"?(m=f/c.df/(l.deviance/l.dfResidual),u=Cn(m,c.df,l.dfResidual,{lowerTail:!1})):(m=Bt(o)?Math.abs(Tu(o,c.deviance)):f/(l.deviance/l.dfResidual),u=Ze(m,c.df,{lowerTail:!1})),s.push(c.df===0?NaN:m),i.push(c.df===0?NaN:u)}let a=t==="F"?"F value":Bt(o)&&dc(o)?"LRT":"scaled dev.";return{statistic:s,pValue:i,statisticName:a}}function ku(e,t,n,r){let o={term:e.map(s=>s.label),df:e.map(s=>s.df),deviance:e.map(s=>s.deviance),aic:e.map(s=>s.aic),criterion:n>0&&!t?"Cp":"AIC",warnings:r.list};return t||(o.sumOfSq=e.map(s=>s.sumOfSq)),o}function Jn(e,t,n=!1){if(typeof e!="number"||Number.isNaN(e))throw new TypeError(`${t} must be a number`);if(n?!(e>=0):!Number.isFinite(e))throw new RangeError(`${t} must be ${n?"non-negative":"finite"}`);return e}function Yw(e,t={}){z(t,zw,"extractAIC");let n=Jn(t.k??2,"k"),r=Jn(t.scale??0,"scale",!0);return va(e,n,r)}function Ww(e,t,n={}){z(n,Vw,"drop1");let r=uc(e,t,"drop1"),o=Jn(n.k??2,"k"),s=r.glm?0:Jn(n.scale??0,"scale",!0),i=ms(e),a=n.scope===void 0?Nu(i,[]):fs(n.scope,t,"drop1").map(m=>{let u=i.find(h=>h.key===m.key);if(u===void 0)throw new RangeError("scope is not a subset of term labels");return u}),c=new pt;mo(c,e);let l=xu(e,a,o,s,c),f=ku(l,r.glm,s,c);return n.test!==void 0&&n.test!=="none"&&Object.assign(f,{test:n.test==="F"?"F":"Chisq"},Au(l,n.test==="F"?"F":"Chisq",!1)),f.warnings=c.list.filter(m=>!e.warnings.includes(m)),J("drop1",f,e)}function jw(e,t,n={}){z(n,$w,"add1");let r=uc(e,t,"add1");if(n.scope===void 0)throw new TypeError("no terms in scope for adding to object");let o=Jn(n.k??2,"k"),s=r.glm?0:Jn(n.scale??0,"scale",!0),i=Mu(ms(e),fs(n.scope,t,"add1")),a=new pt;mo(a,e);let c=Su(r,e,i,o,s,a),l=ku(c,r.glm,s,a);return n.test!==void 0&&n.test!=="none"&&Object.assign(l,{test:n.test==="F"?"F":"Chisq"},Au(c,n.test==="F"?"F":"Chisq",!0)),l.warnings=a.list.filter(f=>!e.warnings.includes(f)),J("add1",l,e)}function qu(e,t,n,r){let o=uc(e,t,r),s=n.direction??"both";if(!["both","backward","forward"].includes(s))throw new RangeError(`'direction' should be one of "both", "backward", "forward"`);let i=Jn(n.k??2,"k"),a=o.glm?0:Jn(n.scale??0,"scale",!0),c=Jn(n.steps??1e3,"steps",!0),l=ms(e),f=[],m=l,u=n.scope;if(u!==void 0)if(typeof u=="string")m=fs(u,t,"upper");else if(typeof u=="object"&&u!==null)u.lower!==void 0&&(f=fs(u.lower,t,"lower")),u.upper!==void 0&&(m=fs(u.upper,t,"upper"));else throw new TypeError("scope must be a formula or { lower, upper }");for(let M of l)if(!m.some(R=>R.key===M.key))throw new RangeError(`upper scope has term \u2018${M.label}\u2019 not included in model`);for(let M of f)if(!l.some(R=>R.key===M.key))throw new RangeError(`lower scope has term \u2018${M.label}\u2019 not included in model`);let h=s!=="forward",p=s!=="backward",d=new pt;mo(d,e);let g=e,[,w]=va(g,i,a);if(Number.isNaN(w))throw new RangeError(`AIC is not defined for this model, so '${r==="step"?"step":"stepAIC"}' cannot proceed`);if(w===-1/0)throw new RangeError(`AIC is -infinity for this model, so '${r==="step"?"step":"stepAIC"}' cannot proceed`);let N=ba(g),b={step:[""],df:[NaN],deviance:[NaN],residDf:[ba(g)-va(g,i,a)[0]],residDeviance:[br(g)],aic:[w]},E=[{formula:lc(o,g.termLabels),termLabels:[...g.termLabels],aic:w}],v=(M,R,x)=>{if(ba(M)!==N)throw new RangeError("number of rows in use has changed: remove missing values?");let[A,S]=va(M,i,a),I=M.dfResidual-g.dfResidual;b.step.push(R),b.df.push(r==="step"?I:Math.abs(I)),b.deviance.push(x?br(M)-br(g):br(g)-br(M)),b.residDf.push(ba(M)-A),b.residDeviance.push(br(M)),b.aic.push(S),g=M,w=S,E.push({formula:lc(o,g.termLabels),termLabels:[...g.termLabels],aic:w})},y=0;for(;y<c;){let M=ms(g),R=M.filter((L,q)=>{let $=(Bt(g),g.coefNames).map((V,P)=>P).filter(V=>g.assign[V]===q+1);return $.length>0&&$.every(V=>Number.isNaN(g.coefficients[V]))});if(R.length>0){let L=R[R.length-1],q=ls(o,g.termLabels.filter($=>$!==L.label));mo(d,q),v(q,`- ${L.label}`,!0),y++;continue}let x=h?Nu(M,f):[],A=p?Mu(M,m):[],S=[];if(x.length>0&&(S=xu(g,x,i,a,d).map((L,q)=>q===0?L:{...L,label:`- ${L.label}`})),A.length>0){let L=Su(o,g,A,i,a,d).map((q,$)=>$===0?q:{...q,label:`+ ${q.label}`});S=S.length===0?L.filter((q,$)=>$===0||q.df>0):[...S,...L.slice(1).filter(q=>q.df>0)]}if(S.length===0)break;let I=S.map((L,q)=>q).sort((L,q)=>Bw(S[L].aic,S[q].aic)||L-q),C=I.map(L=>S[L]),_=E[E.length-1];if(_.table={move:C.map(L=>L.label),df:C.map(L=>L.df),deviance:C.map(L=>L.deviance),aic:C.map(L=>L.aic)},o.glm||(_.table.sumOfSq=C.map(L=>L.sumOfSq)),I[0]===0)break;let F=C[0],O=F.label.startsWith("- "),D=F.label.slice(2),k=O?ls(o,g.termLabels.filter(L=>L!==D)):ls(o,[...g.termLabels,D]);mo(d,k),v(k,F.label,O),y++}let T={fit:g,anova:b,criterion:a>0&&!o.glm?"Cp":"AIC",visits:E,warnings:d.list.filter(M=>!e.warnings.includes(M))};return J(r,T,e)}var Bw=(e,t)=>Number.isNaN(e)?Number.isNaN(t)?0:1:Number.isNaN(t)?-1:e-t;function Uw(e,t,n={}){return z(n,Ow,"step"),z(_t(n,"scope"),Fw,"step","scope"),qu(e,t,n,"step")}function Gw(e,t,n={}){return z(n,Pw,"stepAIC"),z(_t(n,"scope"),Dw,"stepAIC","scope"),qu(e,t,n,"stepAIC")}var Hw=["weights","family","link","epsilon","maxit"],Qw=["family","link","variance"],Iu=["power"];function Xw(e,t,n){z(n,Hw,"svyglm"),z(_t(n,"family"),Qw,"svyglm","family"),z(_t(n,"family.link"),Iu,"svyglm","family.link"),z(_t(n,"link"),Iu,"svyglm","link");let{weights:r,family:o,link:s,epsilon:i,maxit:a}=n;if(r===void 0)throw new RangeError("'weights' must be given");let c=Array.from(r);if(c.length!==t.length)throw new RangeError("'weights' must have the length of 'y'");if(c.some(k=>!(k>0)||!Number.isFinite(k)))throw new RangeError("'weights' must be positive and finite");if(o==="quasi"||typeof o=="object"&&o!==null&&o.family==="quasi")throw new RangeError("the quasi family is not available in svyglm (its variance function is not known here)");let l=c.length,f=c.reduce((k,L)=>k+L,0)/l,m=c.map(k=>k/f),u=Ke(()=>xn(e,t,{weights:m,...o===void 0?{}:{family:o},...s===void 0?{}:{link:s},...i===void 0?{}:{epsilon:i},...a===void 0?{}:{maxit:a}})),h=Array.isArray(e[0])||ArrayBuffer.isView(e[0])?e.map(k=>Array.from(k)):Array.from(e,k=>[k]),p=k=>{let L=0;for(let q=0;q<k.length;q++)Number.isFinite(k[q])&&Math.abs(k[q])>L&&(L=Math.abs(k[q]));return L>0?Math.floor(Math.log2(L)):0},d=(h[0]??[]).map((k,L)=>p(h.map(q=>q[L]))),N=(typeof o=="object"&&o!==null?o.family:o??"gaussian")==="gaussian"&&(s===void 0||s==="identity")&&(typeof o!="object"||o===null||o.link===void 0||o.link==="identity")?p(t):0,b=d.every(k=>k===0)&&N===0?u:Ke(()=>xn(h.map(k=>k.map((L,q)=>W(L,-d[q]))),Array.from(t,k=>W(k,-N)),{weights:m,...o===void 0?{}:{family:o},...s===void 0?{}:{link:s},...i===void 0?{}:{epsilon:i},...a===void 0?{}:{maxit:a}})),E=Ke(()=>ei(b,{type:"HC0"})).vcov,v=[...u.coefficients.length>d.length?[N]:[],...d.map(k=>N-k)],y=E.map((k,L)=>k.map((q,$)=>W(q*l/(l-1),v[L]+v[$]))),T=u.coefficients.length,M=l-T,R=E.map((k,L)=>W(Math.sqrt(k[L]*l/(l-1)),v[L])),x=b.coefficients.map((k,L)=>k/Math.sqrt(E[L][L]*l/(l-1))),A=x.map(k=>2*yn(-Math.abs(k),M)),S=b.fittedValues,I=b.y,C=u.family==="binomial"||u.family==="quasibinomial"?k=>k*(1-k):u.family==="poisson"||u.family==="quasipoisson"?k=>k:u.family==="Gamma"?k=>k*k:u.family==="inverse.gaussian"?k=>k*k*k:()=>1,_=S.map((k,L)=>(I[L]-k)/Math.sqrt(C(k))),F=_.reduce((k,L,q)=>k+m[q]*L,0)/l,O=0;for(let k=0;k<l;k++)O+=m[k]*(_[k]-F)**2;let D=W(O/l*(l/(l-1)),2*N);return J("svyglm",{names:u.model.columnNames.slice(),coefficients:u.coefficients.slice(),se:R,tValue:x,pValue:A,vcov:y,dfResidual:M,dispersion:D,deviance:u.deviance,nullDeviance:u.nullDeviance,fittedValues:u.fittedValues.slice(),linearPredictors:u.linearPredictors.slice(),iter:u.iter,converged:u.converged,glm:u,warnings:u.warnings.slice()})}function vr(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(s=>Number.isFinite(s)))).sort((s,i)=>s-i);if(n.length<2)return e.slice();let r=ct(n.map(Math.abs)),o=[n[0]];for(let s=0;s+1<n.length;s++){let i=n[s+1]-n[s];i<=t||i/r<=t||o.push(n[s+1])}return e.map(s=>{if(!Number.isFinite(s))return s;let i=0,a=o.length-1;for(;i<a;){let c=Math.floor((i+a+1)/2);o[c]<=s?i=c:a=c-1}return o[i]})}var Zw=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),Jw=["eps","iterMax","tolerChol","tolerInf","timefix"],gc=e=>e==null||typeof e=="number"&&Number.isNaN(e),e1=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,t1=2**64,n1=2**-1022,_u=700;function Na(e,t,n){let{time:r,event:o,weight:s,x:i,stratumEnd:a,logUnit:c,start:l}=e,f=r.length,m=t.length,u=()=>new Array(m).fill(0),h=()=>Array.from({length:m},u),p=u(),d=h(),g=0,w=0,N=u(),b=h(),E=u(),v=h(),y=0,T=0,M=R=>{let x=Math.exp(T-R);w*=x,y*=x;for(let A=0;A<m;A++){N[A]*=x,E[A]*=x;let S=b[A],I=v[A];for(let C=0;C<=A;C++)S[C]*=x,I[C]*=x}T=R};for(let R=f-1;R>=0;){a[R]&&(T=0,w=0,N=u(),b=h());let x=r[R],A=!1;if(l!==void 0){let _=R;for(;_>0&&r[_-1]===x&&!a[_-1];)_--;for(let F=_;F<=R;F++)o[F]&&(A=!0)}if(l!==void 0&&A){let _=R;for(;_>0&&!a[_-1];)_--;let F=R;for(;!a[F];)F++;let O=k=>{let L=0;for(let q=0;q<m;q++)L+=t[q]*i[k][q];return L},D=-1/0;for(let k=_;k<=F;k++)l[k]<x&&r[k]>=x&&(D=Math.max(D,O(k)));T=Number.isFinite(D)?D:0,w=0,N=u(),b=h();for(let k=_;k<=F;k++){if(!(l[k]<x&&r[k]>=x)||r[k]===x&&o[k])continue;let L=Math.exp(O(k)-T)*s[k];w+=L;for(let q=0;q<m;q++){N[q]+=L*i[k][q];for(let $=0;$<=q;$++)b[q][$]+=L*i[k][q]*i[k][$]}}}let S=0,I=0;y=0;let C=R;do{let _=i[C],F=0;for(let k=0;k<m;k++)F+=t[k]*_[k];let O=s[C];Number.isFinite(F)&&(F-T>_u?M(F):F-T<-_u&&w===0&&y===0&&(T=F));let D=Math.exp(F-T)*O;if(o[C]){S++,I+=O,y+=D,g+=O*F;for(let k=0;k<m;k++){p[k]+=O*_[k],E[k]+=D*_[k];let L=v[k];for(let q=0;q<=k;q++)L[q]+=D*_[k]*_[q]}}else if(l===void 0){w+=D;for(let k=0;k<m;k++){N[k]+=D*_[k];let L=b[k];for(let q=0;q<=k;q++)L[q]+=D*_[k]*_[q]}}C--}while(C>=0&&r[C]===x&&!a[C]);if(S>0){let _=n?S:1,F=n?S:1,O=I/_;for(let D=0;D<_;D++){w+=n?y/F:y,g-=O*(Math.log(w)+T+c);for(let k=0;k<m;k++){N[k]+=n?E[k]/F:E[k];let L=N[k]/w;p[k]-=O*L;let q=b[k],$=v[k],V=d[k];for(let P=0;P<=k;P++){q[P]+=n?$[P]/F:$[P];let Q=q[P]-L*N[P],K=O*Q;V[P]+=K<1/0&&K>-1/0&&(Math.abs(K)>=n1||Q===0)||!Number.isFinite(O)?K/w:O*(Q/w)}}}E.fill(0);for(let D of v)D.fill(0)}R=C}for(let R=0;R<m;R++)for(let x=0;x<R;x++)d[x][R]=d[R][x];return{loglik:g,score:p,info:d}}function Cu(e,t,n){let{time:r,event:o,x:s,stratumEnd:i}=e,a=Re([e.weight]).groups[0],c=r.length,l=t.length,f=Array.from({length:c},()=>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<c;){let h=u;for(;!i[h];)h++;let p=[];for(let R=u;R<=h;R++)p.push(m(R));let d=[];for(let R=u;R<=h;R++)(R===u||r[R]!==r[R-1])&&d.push(R);let g=R=>R+1<d.length?d[R+1]:h+1,w=new Array(d.length),N=new Array(d.length),b=new Array(d.length),E=-1/0,v=0,y=new Array(l).fill(0);for(let R=d.length-1;R>=0;R--){let x=Number.isFinite(E)?E:0;for(let S=d[R];S<g(R);S++)E=Math.max(E,p[S-u]);let A=Number.isFinite(E)?E:0;if(R<d.length-1&&A!==x){let S=Math.exp(x-A);v*=S;for(let I=0;I<l;I++)y[I]*=S}for(let S=d[R];S<g(R);S++){let I=a[S]*Math.exp(p[S-u]-A);v+=I;for(let C=0;C<l;C++)y[C]+=I*s[S][C]}w[R]=v,N[R]=y.slice(),b[R]=A}let T=0,M=new Array(l).fill(0);for(let R=0;R<d.length;R++){let x=b[R];if(R>0&&x!==b[R-1]){let k=Math.exp(x-b[R-1]);T*=k;for(let L=0;L<l;L++)M[L]*=k}let A=d[R],S=g(R),I=[];for(let k=A;k<S;k++)I.push(Math.exp(p[k-u]-x));let C=0,_=0,F=0,O=new Array(l).fill(0);for(let k=A;k<S;k++){if(!o[k])continue;let L=a[k]*I[k-A];C++,_+=a[k],F+=L;for(let q=0;q<l;q++)O[q]+=L*s[k][q]}for(let k=A;k<S;k++){let L=I[k-A],q=s[k],$=f[k];for(let V=0;V<l;V++)$[V]=-L*(q[V]*T-M[V])}if(C===0)continue;let D=n?C:1;for(let k=0;k<D;k++){let L=k/D,q=w[R]-L*F,$=N[R].map((P,Q)=>(P-L*O[Q])/q),V=_/D/q;for(let P=A;P<S;P++){let Q=I[P-A],K=s[P],X=f[P];if(o[P])for(let Y=0;Y<l;Y++){let se=K[Y]-$[Y];X[Y]+=se/D-(1-L)*Q*se*V}else for(let Y=0;Y<l;Y++)X[Y]-=Q*(K[Y]-$[Y])*V}T+=V;for(let P=0;P<l;P++)M[P]+=V*$[P]}}u=h+1}return f}function wc(e,t,n){let r=e.start,{time:o,event:s,x:i,stratumEnd:a}=e,c=Re([e.weight]).groups[0],l=o.length,f=t.length,m=Array.from({length:l},()=>new Array(f).fill(0)),u=o.map((h,p)=>{let d=0;for(let g=0;g<f;g++)d+=t[g]*i[p][g];return d});for(let h=0;h<l;){let p=h;for(;!a[p];)p++;for(let d=h;d<=p;){let g=o[d],w=d;for(;w<p&&o[w+1]===g;)w++;let N=[],b=[],E=-1/0;for(let v=d;v<=w;v++)s[v]&&b.push(v);if(b.length>0)for(let v=h;v<=p;v++)r[v]<g&&o[v]>=g&&(N.push(v),E=Math.max(E,u[v]));if(b.length>0&&N.length>0){Number.isFinite(E)||(E=0);let v=0,y=new Array(f).fill(0),T=0,M=new Array(f).fill(0),R=0;for(let A of N){let S=Math.exp(u[A]-E),I=c[A]*S;v+=I;for(let C=0;C<f;C++)y[C]+=I*i[A][C];if(s[A]&&o[A]===g){R+=c[A],T+=I;for(let C=0;C<f;C++)M[C]+=I*i[A][C]}}let x=n?b.length:1;for(let A=0;A<x;A++){let S=A/x,I=v-S*T,C=R/x/I,_=y.map((F,O)=>(F-S*M[O])/I);for(let F of N){let O=Math.exp(u[F]-E),D=s[F]&&o[F]===g;for(let k=0;k<f;k++){let L=i[F][k]-_[k];D?m[F][k]+=L/x-(1-S)*O*L*C:m[F][k]-=O*L*C}}}}d=w+1}h=p+1}return m}function zr(e,t){let n=e.length,r=0,o=!1;for(let c=0;c<n;c++){let l=e[c][c];l>0&&((!o||l>r)&&(r=l),o=!0)}let s=o?r*t:t,i=0,a=!0;for(let c=0;c<n;c++){let l=e[c][c];if(!Number.isFinite(l)||l<s){e[c][c]=0,l<-8*s&&(a=!1);for(let f=c+1;f<n;f++)e[f][c]=0;continue}i++;for(let f=c+1;f<n;f++){let m=e[f][c]/l;e[f][c]=m,e[f][f]-=m*m*l;for(let u=f+1;u<n;u++)e[u][f]-=m*e[u][c]}}return a?i:-i}function Kr(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let s=r[o];for(let i=0;i<o;i++)s-=r[i]*e[o][i];r[o]=s}for(let o=n-1;o>=0;o--){if(e[o][o]===0){r[o]=0;continue}let s=r[o]/e[o][o];for(let i=o+1;i<n;i++)s-=r[i]*e[i][o];r[o]=s}return r}function uo(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 us=(e,t)=>({statistic:e,df:t,pValue:Ze(e,t,{lowerTail:!1})});function r1(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=us(0,0),i=new Array(t).fill(0),a=o(),c=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)=>ct(e.map(m=>m[f]))),n:r,nevent:0,linearPredictors:new Array(r).fill(0),se:i,z:a,p:c,stdError:i,zValue:a,pValue:c,lower:o(),upper:o(),logtest:s,sctest:{...s},waldtest:{...s},rsq:{rsq:0,maxrsq:0},usedRobust:!1,warnings:n}}function ho(e,t,n,r={}){return z(r,Zw,"coxph"),z(_t(r,"control"),Jw,"coxph","control"),J("coxph",o1(e,t,n,r).result)}function o1(e,t,n,r={},o=!1){let{ties:s="efron",weights:i,strata:a,init:c,control:l={},nocenter:f=[-1,0,1],confLevel:m=.95,robust:u,cluster:h,start:p,id:d}=r,{eps:g=1e-9,iterMax:w=20,tolerChol:N=2**-39}=l,b=l.tolerInf??Math.sqrt(g);if(l.timefix!==void 0&&typeof l.timefix!="boolean")throw new TypeError("'timefix' must be a boolean");let E=l.timefix??!0;if(s!=="efron"&&s!=="breslow")throw new RangeError(`invalid 'ties': ${String(s)}`);if(!(g>0&&g<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(N>0&&N<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(b>0&&b<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(w)&&w>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(m>0&&m<1))throw new RangeError("'confLevel' must be between 0 and 1");let v=e.length;if(t.length!==v||n.length!==v)throw new RangeError("'time', 'status' and 'x' must have the same length");if(i!==void 0&&i.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(h!==void 0&&h.length!==v)throw new RangeError("'cluster' must have the same length as 'time'");if(p!==void 0&&p.length!==v)throw new RangeError("'start' must have the same length as 'time'");if(d!==void 0&&d.length!==v)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=v===0?0:n[0].length;if(y<1)throw new RangeError("'x' must have at least one column");if(n.some(G=>G.length!==y))throw new RangeError("the rows of 'x' must have the same length");if(c!==void 0&&c.length!==y)throw new RangeError("'init' must have one value per column of 'x'");if(c?.some(G=>!Number.isFinite(G)))throw new RangeError("'init' must contain finite values");let T=new pt,M=[];for(let G=0;G<v;G++){if(Number.isNaN(e[G])||Number.isNaN(t[G])||n[G].some(Me=>Number.isNaN(Me))||p!==void 0&&Number.isNaN(p[G])||i!==void 0&&Number.isNaN(i[G])||a!==void 0&&gc(a[G])||h!==void 0&&gc(h[G])||d!==void 0&&gc(d[G]))continue;if(p!==void 0&&!(p[G]<e[G]))throw new RangeError("stop time must be > start time");let ie=t[G];if(ie!==0&&ie!==1){if(p===void 0)throw new RangeError("'status' must be 0 (censored) or 1 (event)");T.add("Invalid status value, converted to NA");continue}if(e[G]===-1/0||e[G]===1/0&&ie===1)throw new RangeError("event times must be finite");M.push(G)}let R=M.length;if(R===0)throw new RangeError(p===void 0?"no observations without missing values":"No (non-missing) observations");if(M.some(G=>n[G].some(ie=>!Number.isFinite(ie))))throw new RangeError("'x' contains infinite values");let x=M.map(G=>i===void 0?1:i[G]);if(x.some(G=>!(G>0)))throw new RangeError("Invalid weights, must be >0");if(x.some(G=>G===1/0))throw new RangeError("'weights' must be finite");let A=d===void 0?void 0:Pt(d),S=new Map;if(A!==void 0)for(let G of M){if(t[G]!==1)continue;let ie=A[G];S.set(ie,(S.get(ie)??0)+1)}let I=p!==void 0&&[...S.values()].some(G=>G>1),C=u??(h!==void 0||x.some(G=>!Number.isInteger(G))||I);if(p!==void 0&&u===!0&&h===void 0&&d===void 0)throw new RangeError("one of cluster or id is needed");h!==void 0&&!C&&T.add("cluster specified with robust=FALSE, cluster ignored");let _=0;for(let G of M)_+=t[G];let F=M.map(G=>e[G]),O=p===void 0?void 0:M.map(G=>p[G]),D,k;if(O===void 0)D=E?vr(F):F;else if(!E)D=F,k=O;else{let G=vr([...O,...F]);if(k=G.slice(0,R),D=G.slice(R),k.some((ie,Me)=>ie>=D[Me]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(_===0){let G=r1(M.map(ie=>n[ie]),y,T.list);return A!==void 0&&(G.nId=new Set(M.map(ie=>A[ie])).size),{result:G}}let L=new Array(R).fill(0),q,$=[];if(a!==void 0){let G=Pt(a);$=Array.from(new Set(M.map(Me=>G[Me]))).sort(e1);let ie=new Map;$.forEach((Me,je)=>ie.set(Me,je)),q=M.map(Me=>G[Me]),L=M.map(Me=>ie.get(G[Me]))}let V=Array.from({length:R},(G,ie)=>ie).sort((G,ie)=>L[G]-L[ie]||D[G]-D[ie]||G-ie),P=V.map(G=>x[G]),{groups:Q,k:K}=kt(P)>=t1?Re([P]):{groups:[P],k:0},X=Q[0],Y=0;for(let G of x)Y+=K===0?G:W(G,-K);let se=V.map(G=>n[M[G]].slice()),H=new Array(y).fill(0),j=new Array(y).fill(1),te=new Array(y).fill(0),B=new Set(f);for(let G=0;G<y;G++){if(se.every(ze=>B.has(ze[G])))continue;let ie=se.map(ze=>ze[G]),Me=ze=>{let $t=0;for(let Wn=0;Wn<R;Wn++)$t+=(p===void 0?X[Wn]:1)*ze[Wn];let Yn=p===void 0?Y:R,Ar=$t/Yn,Zr=0,za=ze.map((Wn,Ts)=>(Zr+=(p===void 0?X[Ts]:1)*Math.abs(Wn-Ar),Wn-Ar)),Jr=Zr===0?1:Yn/Zr;return{m:Ar,s:Jr,d:za,ok:Number.isFinite(Ar)&&Jr>=2**-1022&&Jr<1/0}},je=Me(ie);if(!je.ok){let{groups:ze,k:$t}=Re([ie]);je=Me(ze[0]),te[G]=$t}for(let ze=0;ze<R;ze++)se[ze][G]=je.d[ze]*je.s;H[G]=W(je.m,te[G]),j[G]=je.s}let Z={time:V.map(G=>D[G]),...k===void 0?{}:{start:V.map(G=>k[G])},event:V.map(G=>t[M[G]]===1),weight:X,x:se,stratumEnd:V.map((G,ie)=>ie===R-1||L[V[ie+1]]!==L[G]),logUnit:K*Math.LN2},U=s==="efron",ee=c===void 0?new Array(y).fill(0):c.map((G,ie)=>W(G/j[ie],te[ie])),ae=Na(Z,ee,U),ve=[ae.loglik,ae.loglik],Ne=ae.info.map(G=>G.slice()),ue=zr(Ne,N),fe=Kr(Ne,ae.score),Ee=0;for(let G=0;G<y;G++)Ee+=ae.score[G]*fe[G];let pe=0,Ce,be=!1,ye=w===0;if(w===0||!Number.isFinite(ae.loglik))Ce=uo(Ne);else{let G=ae.loglik,ie=ee.map((je,ze)=>je+fe[ze]),Me=0;for(ye=!1,pe=1;pe<=w;pe++){ae=Na(Z,ie,U),Ne=ae.info.map(ze=>ze.slice()),ue=zr(Ne,N);let je=Number.isFinite(ae.loglik)&&ae.score.every(Number.isFinite)&&ae.info.every(ze=>ze.every(Number.isFinite));if(je&&(ae.loglik===G||Math.abs(1-G/ae.loglik)<=g)){ye=!0,ve[1]=ae.loglik,ee=ie;break}!je||ae.loglik<G?(Me++,ie=ie.map((ze,$t)=>(ze+Me*ee[$t])/(Me+1))):(Me=0,G=ae.loglik,ve[1]=G,fe=Kr(Ne,ae.score),ee=ie,ie=ie.map((ze,$t)=>ze+fe[$t]))}ye?Ce=uo(Ne):(pe=p===void 0?w+1:w,be=!0,p!==void 0?(ae=Na(Z,ee,U),ve[1]=ae.loglik,Ne=ae.info.map(je=>je.slice()),ue=zr(Ne,N),Ce=uo(Ne)):w>1?(ae=Na(Z,ee,U),ve[1]=ae.loglik,Ce=ae.info.map((je,ze)=>je.map(($t,Yn)=>ze===Yn?je[ze]>0?1/je[ze]:0:-0))):Ce=uo(Ne))}let Se=j.map(G=>Math.floor(Math.log2(G))),Ye=j.map((G,ie)=>W(G,-Se[ie])),De=Se.map((G,ie)=>G-te[ie]),Je=ee.map((G,ie)=>G*Ye[ie]),Be=ae.score.map((G,ie)=>G/Ye[ie]),Xe=Ce.map((G,ie)=>G.map((Me,je)=>Me*Ye[ie]*Ye[je])),me=Je.map((G,ie)=>W(G,De[ie])),ne=Xe.map((G,ie)=>G.map((Me,je)=>W(Me,De[ie]+De[je]-K))),we=me.slice();if(w>0&&Math.abs(ue)<y)for(let G=0;G<y;G++)Ce[G][G]===0&&(we[G]=NaN);let ge=p===void 0?me:me.map((G,ie)=>Number.isNaN(we[ie])?0:G),le=0;for(let G=0;G<y;G++)le+=ge[G]*H[G];let et=M.map(G=>{let ie=0;for(let Me=0;Me<y;Me++)ie+=n[G][Me]*ge[Me];return ie-le});if(w>1){let G=Xe.map((ie,Me)=>{let je=0;for(let ze=0;ze<y;ze++)je+=Be[ze]*Xe[ze][Me];return Math.abs(je)});if(be)(p===void 0||w>1)&&T.add("Ran out of iterations and did not converge"),p===void 0&&(kt(et)>500||G.some(ie=>!Number.isFinite(ie)))&&T.add("one or more coefficients may be infinite");else if(p===void 0){let ie=[];for(let Me=0;Me<y;Me++){let je=G[Me];(!Number.isFinite(Be[Me])||W(je,De[Me])>g&&je>b*Math.abs(Je[Me]))&&ie.push(Me+1)}ie.length>0&&T.add("Loglik converged before variable "+ie.join(",")+" ; coefficient may be infinite. ")}}let Oe=we.map((G,ie)=>Number.isNaN(G)?-1:ie).filter(G=>G>=0),nt=Oe.length,ke,$e=0,He=0,ce;if(C&&nt>0){let G=V.map((mt,Ue)=>Ue),ie=R,Me=h??(p===void 0?void 0:d);if(Me!==void 0){let mt=Pt(Me),Ue=new Map;G=V.map(bt=>{let Et=mt[M[bt]],Tt=Ue.get(Et);return Tt===void 0&&(Tt=Ue.size,Ue.set(Et,Tt)),Tt}),ie=Ue.size}if(ie<2)throw new RangeError(p===void 0?"at least two clusters are needed for the robust variance":"(subscript) logical subscript too long");let je=mt=>mt.map(Ue=>Ue.map((bt,Et)=>bt/Ye[Et])),ze=je(p===void 0?Cu(Z,ee,U):wc(Z,ee,U)),$t=ze.map(mt=>Xe.map((Ue,bt)=>{let Et=0;for(let Tt=0;Tt<y;Tt++)Et+=mt[Tt]*Xe[Tt][bt];return Et})),Yn;if(K===0)Yn=$t.map((mt,Ue)=>mt.map(bt=>X[Ue]*bt));else{let mt=bt=>bt===0||!Number.isFinite(bt)?0:Math.floor(Math.log2(Math.abs(bt))),Ue=-1/0;$t.forEach((bt,Et)=>bt.forEach(Tt=>Tt!==0&&Number.isFinite(Tt)&&(Ue=Math.max(Ue,mt(X[Et])+mt(Tt))))),$e=Ue===-1/0?0:-Ue,Yn=$t.map((bt,Et)=>{let Tt=mt(X[Et]);return bt.map(kr=>{let bl=mt(kr);return W(W(X[Et],-Tt)*W(kr,-bl),Tt+bl+$e)})})}let Ar=mt=>{let Ue=Array.from({length:ie},()=>new Array(y).fill(0));return mt.forEach((bt,Et)=>{let Tt=Ue[G[Et]];for(let kr=0;kr<y;kr++)Tt[kr]+=bt[kr]}),Ue},Zr=mt=>{let Ue=Array.from({length:y},()=>new Array(y).fill(0));for(let bt of mt)for(let Et=0;Et<y;Et++)for(let Tt=0;Tt<y;Tt++)Ue[Et][Tt]+=bt[Et]*bt[Tt];return Ue};ke=Zr(Ar(Yn));let za=Re([P]).groups[0],Jr=c===void 0?new Array(y).fill(0):c.map((mt,Ue)=>W(mt/j[Ue],te[Ue])),Wn=p===void 0?Cu(Z,Jr,U):wc(Z,Jr,U),Ts=Ar(je(Wn).map((mt,Ue)=>mt.map(bt=>za[Ue]*bt))),zo=new Array(y).fill(0);for(let mt of Ts)for(let Ue=0;Ue<y;Ue++)zo[Ue]+=mt[Ue];let Ka=Zr(Ts);if(y===1)He=zo[0]*zo[0]/Ka[0][0];else{zr(Ka,N);let mt=Kr(Ka,zo);for(let Ue=0;Ue<y;Ue++)He+=zo[Ue]*mt[Ue]}let gl=new Array(R),wl=new Array(R);V.forEach((mt,Ue)=>{gl[mt]=ze[Ue].map((bt,Et)=>W(bt,-De[Et])),wl[mt]=Yn[Ue].map((bt,Et)=>W(bt,De[Et]-$e))}),ce={scoreResiduals:gl,dfbeta:wl}}let xe=ke!==void 0,de=ke??Xe,Ae=ke===void 0?-K:-2*$e,We=Oe.map(G=>Je[G]-(c===void 0?0:W(c[G],-De[G]))),Fe=0;if(nt===1)Fe=We[0]*We[0]/de[Oe[0]][Oe[0]];else if(nt>1){let G=Oe.map(Me=>Oe.map(je=>de[Me][je]));zr(G,N);let ie=Kr(G,We);for(let Me=0;Me<nt;Me++)Fe+=We[Me]*ie[Me]}Fe=W(Fe,-Ae);let rt=(1-m)/2,dt=rt<1e-8?re(rt,{lowerTail:!1}):re((1+m)/2),Lt=(G,ie)=>{let Me=Math.floor(ie/2),je=G.map((ze,$t)=>Math.sqrt(W(ze[$t],ie-2*Me)));return{unit:je,h:Me,se:je.map((ze,$t)=>W(ze,De[$t]+Me))}},Ht=Lt(Xe,-K).se,{unit:or,h:Ve,se:Pe}=Lt(de,Ae),St=we.map((G,ie)=>W((Number.isNaN(G)?G:Je[ie])/or[ie],-Ve)),wt=St.map(G=>Ze(G*G,1,{lowerTail:!1})),_n=we.map((G,ie)=>Math.exp(G-dt*Pe[ie])),Vt=we.map((G,ie)=>Math.exp(G+dt*Pe[ie])),It=W(-2*(ve[0]-ve[1]),K),gt=[W(ve[0],K),W(ve[1],K)],Qt=W(Ee,K),sn=ke?.map((G,ie)=>G.map((Me,je)=>W(Me,De[ie]+De[je]-2*$e))),tn={coefficients:we,var:sn??ne,loglik:gt,score:Qt,wald:Fe,iter:pe,means:H,n:R,nevent:_,...A===void 0?{}:{nId:new Set(M.map(G=>A[G])).size},linearPredictors:et,se:Ht,z:St,p:wt,stdError:Ht,zValue:St,pValue:wt,lower:_n,upper:Vt,logtest:us(It,nt),sctest:us(Qt,nt),waldtest:us(Fe,nt),rsq:{rsq:1-Math.exp(-It/R),maxrsq:1-Math.exp(2*gt[0]/R)},usedRobust:xe,...xe?{naiveVar:ne,robustSe:Pe,rscore:He,robscore:us(He,nt)}:{},warnings:T.list},Ot=ae.score.map((G,ie)=>W(G/j[ie],te[ie]+K));Object.defineProperty(tn,"coxZphData",{enumerable:!1,value:{time:D.slice(),...k===void 0?{}:{start:k.slice()},status:M.map(G=>t[G]===1),weights:x.slice(),...q===void 0?{}:{strata:q.slice()},x:M.map(G=>n[G].slice()),sortedToInput:V.slice(),sortedTime:Z.time.slice(),...Z.start===void 0?{}:{sortedStart:Z.start.slice()},sortedEvent:Z.event.slice(),sortedWeight:Z.weight.slice(),sortedX:Z.x.map(G=>G.slice()),stratumIndex:V.map(G=>L[G]),stratumEnd:Z.stratumEnd.slice(),betaScaled:ee.slice(),inverseCovariateScale:Ye.map((G,ie)=>W(1/G,-De[ie])),weightExp:K,ties:s}});let nn=ce?.scoreResiduals;if(p!==void 0&&o&&nn===void 0){let G=wc(Z,ee,U);nn=new Array(R),V.forEach((ie,Me)=>{nn[ie]=G[Me].map((je,ze)=>W(je/Ye[ze],-De[ze]))})}return{result:tn,...ce,...nn===void 0?{}:{scoreResiduals:nn},first:Ot,converged:ye}}var s1=["rho"],a1=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),bc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function i1(e,t,n){if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n!==void 0&&n.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");let r=[];for(let c=0;c<e.length;c++){if(Number.isNaN(e[c])||Number.isNaN(t[c])||n!==void 0&&bc(n[c]))continue;let l=t[c];if(l!==0&&l!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[c]===-1/0||e[c]===1/0&&l===1)throw new RangeError("event times must be finite");r.push(c)}if(r.length===0)throw new RangeError("no observations without missing values");let o=[],s=new Map;n!==void 0&&(n=Pt(n)),n!==void 0&&(o=Array.from(new Set(r.map(c=>n[c]))).sort((c,l)=>typeof c=="number"&&typeof l=="number"?c-l:String(c)<String(l)?-1:String(c)>String(l)?1:0),o.forEach((c,l)=>s.set(c,l)));let i=vr(r.map(c=>e[c])),a=r.map((c,l)=>({time:i[l],event:t[c]===1,group:n===void 0?0:s.get(n[c])}));return a.sort((c,l)=>c.time-l.time),{obs:a,labels:o.map(c=>`group=${xt(c)}`)}}function c1(e,t,n,r={}){z(r,s1,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=i1(e,t,n),a=i.length;if(a<2)throw new RangeError("there is only 1 group");let c=new Array(a).fill(0);for(let b of s)c[b.group]++;let l=new Array(a).fill(0),f=new Array(a).fill(0),m=Array.from({length:a},()=>new Array(a).fill(0)),u=c.slice(),h=s.length,p=1;for(let b=0;b<s.length;){let E=s[b].time,v=b,y=new Array(a).fill(0),T=new Array(a).fill(0),M=0;for(;v<s.length&&s[v].time===E;){let R=s[v];T[R.group]++,R.event&&(y[R.group]++,M++),v++}if(M>0){let R=o===0?1:p**o;for(let x=0;x<a;x++)l[x]+=R*y[x],f[x]+=R*M*u[x]/h;if(h>1){let x=R*R*M*(h-M)/(h*(h-1));for(let A=0;A<a;A++)for(let S=0;S<a;S++)m[A][S]+=x*u[A]*((A===S?1:0)-u[S]/h)}p*=1-M/h}for(let R=0;R<a;R++)u[R]-=T[R];h-=v-b,b=v}let d=f.map((b,E)=>b>0?E:-1).filter(b=>b>=0),g=0,w=Math.max(d.length-1,0);if(d.length>=2){let b=d.slice(1),E=b.map(y=>l[y]-f[y]),v=b.map(y=>b.map(T=>m[y][T]));try{let y=ot(v,E);g=E.reduce((T,M,R)=>T+M*y[R],0)}catch{({chisq:g,df:w}=l1(v,E))}}let N=w===0?NaN:Ze(g,w,{lowerTail:!1});return{n:c,groups:i,obs:l,exp:f,var:m,chisq:g,df:w,pvalue:N,pValue:N,warnings:[]}}function l1(e,t){let n=wn(e,{symmetric:!0}),r=n.values,o=r.reduce((a,c)=>Math.max(a,c),0),s=0,i=0;return r.forEach((a,c)=>{if(!(o>0&&a>1e-10*o))return;let l=t.reduce((f,m,u)=>f+m*n.vectors[u][c],0);s+=l*l/a,i++}),{chisq:s,df:i}}function Yr(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 fr(e[r],e[s])}return e[r]}function Nr(e,t,n={}){z(n,a1,"survfit");let{group:r,start:o,id:s,weights:i,ctype:a=1,stype:c=1,rmean:l="common",confType:f="log",confInt:m=.95}=n;if(typeof f!="string")throw new TypeError("confType must be a string");let u=["log","log-log","plain","none","logit","arcsin"],h=typeof f=="string"?u.filter(q=>q.startsWith(f)):[],p=u.includes(f)?f:h.length===1?h[0]:f;if(typeof a!="number")throw new TypeError("ctype must be a number");if(typeof c!="number")throw new TypeError("stype must be a number");if(a!==1&&a!==2)throw new RangeError("ctype must be 1 or 2");if(c!==1&&c!==2)throw new RangeError("stype must be 1 or 2");if(!u.some(q=>q===p))throw new RangeError("'arg' should be one of \u201Clog\u201D, \u201Clog-log\u201D, \u201Cplain\u201D, \u201Cnone\u201D, \u201Clogit\u201D, \u201Carcsin\u201D");if(typeof m!="number")throw new TypeError("confInt must be a number");if(!(m>0&&m<1))throw new RangeError("confidence intervals must be between 0 and 1");if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(r!==void 0&&r.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(o!==void 0&&o.length!==e.length)throw new RangeError("'start' must have the same length as 'time'");if(s!==void 0&&s.length!==e.length)throw new RangeError("'id' must have the same length as 'time'");if(s!==void 0&&o===void 0)throw new RangeError("'id' requires 'start'");if(i!==void 0&&!Array.isArray(i))throw new TypeError("'weights' must be an array");if(i!==void 0&&i.length!==e.length)throw new RangeError("'weights' must have the same length as 'time'");let d=n.robust??(i!==void 0&&Array.from(i).some(q=>q!=null&&Number.isFinite(q)&&!Number.isInteger(q)));if(typeof d!="boolean")throw new TypeError("'robust' must be a boolean");if(d&&c===2&&i!==void 0&&Array.from(i).some(q=>q!=null&&Number.isFinite(q)&&!Number.isInteger(q)))throw new RangeError("non-integer weights with stype 2 and the robust variance are not supported");if(typeof l!="number"&&typeof l!="string")throw new TypeError("rmean must be a number or string");if(typeof l=="number"&&!Number.isFinite(l))throw new RangeError("'rmean' must be finite");if(typeof l!="number"&&l!=="none"&&l!=="common"&&l!=="individual")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Ccommon\u201D, \u201Cindividual\u201D");let g=[];for(let q=0;q<e.length;q++){let $=i?.[q];if(i&&$!=null&&typeof $!="number")throw new TypeError("weights must contain numbers");if(i&&($==null||Number.isNaN($)))continue;let V=$??1;if(!Number.isFinite(V))throw new RangeError("weights must be finite");if(V<0)throw new RangeError("weights must be non-negative");if(e[q]!=null&&typeof e[q]!="number")throw new TypeError("'time' must contain numbers");if(t[q]!=null&&typeof t[q]!="number")throw new TypeError("'status' must contain numbers");if(o!==void 0&&o[q]!=null&&typeof o[q]!="number")throw new TypeError("'start' must contain numbers");if(!(e[q]==null||t[q]==null||Number.isNaN(e[q])||Number.isNaN(t[q])||o!==void 0&&(o[q]==null||Number.isNaN(o[q]))||r!==void 0&&bc(r[q])||s!==void 0&&bc(s[q]))){if(o!==void 0&&!(o[q]<e[q]))throw new RangeError("stop time must be > start time");if(t[q]!==0&&t[q]!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[q]===-1/0||e[q]===1/0&&t[q]===1)throw new RangeError("event times must be finite");g.push(q)}}if(g.length===0)throw new RangeError("no observations without missing values");let w=r===void 0?[]:Pt(r),N=s===void 0?[]:Array.from(s),b=r===void 0?["all"]:Array.from(new Set(g.map(q=>w[q]))).sort((q,$)=>typeof q=="number"&&typeof $=="number"?q-$:String(q)<String($)?-1:String(q)>String($)?1:0).map(q=>`group=${xt(q)}`),E=new Map;r!==void 0&&Array.from(new Set(g.map(q=>w[q]))).sort((q,$)=>typeof q=="number"&&typeof $=="number"?q-$:String(q)<String($)?-1:String(q)>String($)?1:0).forEach((q,$)=>E.set(q,$));let v=new Map;s!==void 0&&Array.from(new Set(g.map(q=>N[q]))).forEach((q,$)=>v.set(q,$));let y=g.flatMap(q=>o===void 0?[e[q]]:[o[q],e[q]]),T=vr(y),M=new Map,R=new Map;g.forEach((q,$)=>{if(o===void 0)R.set(q,T[$]);else if(M.set(q,T[2*$]),R.set(q,T[2*$+1]),!(M.get(q)<R.get(q)))throw new Error("aeqSurv exception, an interval has effective length 0")});let x=g.map(q=>({time:R.get(q),start:o===void 0?-1/0:M.get(q),event:t[q]===1,group:r===void 0?0:E.get(w[q]),id:s===void 0?q:v.get(N[q]),weight:i?.[q]??1})).sort((q,$)=>q.time-$.time),A=s===void 0?void 0:new Map;if(A)for(let q of x){let $=A.get(q.id)??[];$.push(q),A.set(q.id,$)}let S=b,I=x.map(q=>q.time),C=Math.min(...I),_=Math.min(0,C);if(typeof l=="number"&&l<=_)throw new RangeError("'rmean' must be greater than the integration origin");let F=(1-m)/2,O=F<1e-8?re(F,{lowerTail:!1}):re(1-F),D=[],k={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:D,stdError:D,lower:[],upper:[],cumhaz:[],stdChaz:[],logse:!d||a===2,n:[],records:[],nMax:[],nStart:[],events:[],rmean:[],seRmean:[],median:[],medianRows:S,table:{colNames:[],rowNames:S,table:[]},warnings:[]};i!==void 0&&(k.counts={nRisk:[],nEvent:[],nCensor:[]});let L=[];for(let q=0;q<S.length;q++){let $=x.filter(me=>me.group===q);if(!$.some(me=>me.weight>0))throw new Error("a group has no positive weight");let V=k.time.length,P=$.length,Q=Math.max(...$.map(me=>me.time)),K=0,X=0,Y=1,se=0,H=0,j=0;if(o===void 0){let me=0;for(let ne of $)me+=ne.weight;K=me,X=$.length}let te=[],B=[],Z=[],U=s===void 0?void 0:new Map;if(U)for(let me of $){let ne=U.get(me.id)??[];ne.push(me),U.set(me.id,ne)}for(let me=0;me<$.length;){let ne=$[me].time,we=me,ge=0,le=0,et=0,Oe=X;for(;we<$.length&&$[we].time===ne;)we++;let nt=$.slice(me,we);if(o!==void 0){let Ve=$.filter(Pe=>Pe.start<ne&&Pe.time>=ne);K=Ve.reduce((Pe,St)=>Pe+St.weight,0),X=Ve.length,Oe=X}let ke=nt.filter(Ve=>Ve.event),$e=nt.filter(Ve=>!Ve.event&&(A===void 0||!(A.get(Ve.id)??[]).some(Pe=>Pe.start===ne&&Pe.time>ne)));for(let Ve of ke)ge+=Ve.weight,le++;for(let Ve of $e)et+=Ve.weight;let He=ke.length,ce=$e.length;if(s!==void 0&&ke.length===0&&$e.length===0){me=we;continue}let xe=0,de=0;if(ge>0){if(a===1)xe=ge/K,de=ge/(K*K);else for(let Pe=0;Pe<le;Pe++){let St=ge/le,wt=K-Pe*St;xe+=St/wt,de+=St/(wt*wt)}se+=xe,j+=de;let Ve=le===Oe&&Oe>0?1:ge/K;c===1?(Y*=1-Ve,H+=Ve<1?ge/K/(K-ge):1/0):Y=Math.exp(-se),Z.push({t:ne,d:ge,n:K,e:le,S:Y})}let Ae=Math.sqrt(c===1?H:j),We=0,Fe=0;if(d){let Ve=0,Pe=0,St=U===void 0?$:Array.from(U.values(),wt=>wt[0]);for(let wt of St){let _n=U?.get(wt.id)??[wt],Vt=0,It=0;for(let gt of Z){if(gt.t>ne)break;let Qt=_n.some(Ot=>Ot.start<gt.t&&Ot.time>=gt.t),sn=_n.some(Ot=>Ot.time===gt.t&&Ot.event),tn=gt.d/gt.n;if(a===1)It+=(Number(sn)-Number(Qt)*tn)/gt.n;else{let Ot=0,nn=0;for(let G=0;G<gt.e;G++){let ie=gt.d/gt.e,Me=gt.n-G*ie;Ot+=(Me+G*ie)/(gt.e*Me*Me),nn-=ie/(Me*Me)}It+=Ot*Number(sn)+nn*Number(Qt)}c===1&&tn<1&&(Vt-=Y*(Number(sn)-Number(Qt)*tn)/((1-tn)*gt.n))}Ve+=(wt.weight*Vt)**2,Pe+=(wt.weight*It)**2}We=Math.sqrt(Ve),Fe=Math.sqrt(Pe)}let rt=d?a===2?c===1?We:Fe:c===1?Y===0?NaN:We/Y:Fe:Ae,dt=d?a===2?Y*(c===1?We:Fe):c===1?We:Y*Fe:Y*Ae,Lt=NaN,Ht=NaN;if(p!=="none")if(p==="log")Lt=Y===0?dt===0?0:NaN:Y*Math.exp(-O*rt),Ht=Y===0?dt===0?0:NaN:Math.min(1,Y*Math.exp(O*rt));else if(p==="log-log")if(Y===0||Y===1)Lt=dt===0?Y:NaN,Ht=Lt;else{let Ve=Math.log(-Math.log(Y));Lt=Math.exp(-Math.exp(Ve-O*rt/Math.log(Y))),Ht=Math.exp(-Math.exp(Ve+O*rt/Math.log(Y)))}else if(p==="plain")Y===0&&dt===1/0?(Lt=NaN,Ht=NaN):(Lt=Math.max(0,Y-O*dt),Ht=Math.min(1,Y+O*dt));else if(p==="logit")if(Y===0||Y===1)Lt=dt===0?Y:NaN,Ht=Lt;else{let Ve=Math.log(Y/(1-Y)),Pe=O*dt/(Y*(1-Y));Lt=1-1/(1+Math.exp(Ve-Pe)),Ht=1-1/(1+Math.exp(Ve+Pe))}else if(Y>0&&Y<1){let Ve=Math.asin(Math.sqrt(Y)),Pe=O*dt/(2*Math.sqrt(Y*(1-Y)));Lt=Math.sin(Math.max(0,Ve-Pe))**2,Ht=Math.sin(Math.min(Math.PI/2,Ve+Pe))**2}else Lt=NaN,Ht=NaN;let or=K;k.strata.push(S[q]),k.time.push(ne),k.nRisk.push(or),k.nEvent.push(ge),k.nCensor.push(et),k.surv.push(Y),k.cumhaz.push(se),k.stdChaz.push(d?Fe:Math.sqrt(j)),k.stdErr.push(d?a===2?Y*(c===1?We:Fe):c===1?We:Y*Fe:Y*Ae),k.lower.push(Lt),k.upper.push(Ht),k.counts&&(k.counts.nRisk.push(Oe),k.counts.nEvent.push(He),k.counts.nCensor.push(ce)),o===void 0&&(K-=ge+et,X-=we-me),me=we}let ee=k.time.slice(V),ae=k.surv.slice(V),ve=k.lower.slice(V),Ne=k.upper.slice(V);k.median.push([Yr(ee,ae),p==="none"?NaN:Yr(ee,ve),p==="none"?NaN:Yr(ee,Ne)]),k.n.push(P),k.records.push(P);let ue=k.nRisk.slice(V);k.nMax.push(Math.max(...ue)),k.nStart.push(ue[0]??0),k.events.push($.reduce((me,ne)=>me+(ne.event?ne.weight:0),0));let fe=l==="none"?NaN:l==="common"?Math.max(...I):l==="individual"?Q:l;if(l!=="none"&&!Number.isFinite(fe))throw new RangeError("'rmean' must be finite");let Ee=$.filter(me=>Number.isFinite(me.time)).map(me=>Math.abs(me.time)),pe=Number.isFinite(fe)?Math.max(Math.abs(fe),Math.abs(_),...Ee):Math.max(Math.abs(_),...Ee),Ce=pe===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(pe)))),be=pe===0?1:2**Ce,ye=Number.isFinite(fe)&&!Number.isFinite(fe-_),Se=me=>{let ne=1;for(let we of Z)if(we.t<=me)ne=we.S;else break;return ne},Ye=me=>{let ne=1;for(let we of Z)if(we.t/be<=me)ne=we.S;else break;return ne},De=Un();if(ye){let me=Array.from(new Set([_/be,...Z.map(ne=>ne.t/be).filter(ne=>ne>_/be&&ne<fe/be),fe/be])).sort((ne,we)=>ne-we);for(let ne=0;ne<me.length-1;ne++){let we=me[ne],ge=me[ne+1];gn(De,Ye(we)*(ge-we),me.length+1)}}else{let me=Array.from(new Set([_,...Z.map(ne=>ne.t).filter(ne=>ne>_&&ne<fe),fe])).sort((ne,we)=>ne-we);for(let ne=0;ne<me.length-1;ne++){let we=me[ne],ge=me[ne+1];gn(De,Se(we)*(ge-we),me.length+1)}}let Je=Gn(De)*(ye?be:1),Be=Un();if(Number.isFinite(fe))for(let me of Z){if(me.t>fe||me.n<=me.d)continue;let ne=Un();if(ye){let ge=Array.from(new Set([me.t/be,...Z.map(le=>le.t/be).filter(le=>le>me.t/be&&le<fe/be),fe/be])).sort((le,et)=>le-et);for(let le=0;le<ge.length-1;le++)gn(ne,Ye(ge[le])*(ge[le+1]-ge[le]),ge.length+1)}else{let ge=Array.from(new Set([me.t,...Z.map(le=>le.t).filter(le=>le>me.t&&le<fe),fe])).sort((le,et)=>le-et);for(let le=0;le<ge.length-1;le++)gn(ne,Se(ge[le])*((ge[le+1]-ge[le])/be),ge.length+1)}let we=Gn(ne);gn(Be,we*we*me.d/(me.n*(me.n-me.d)),Z.length+1)}let Xe=Gn(Be);k.rmean.push(l==="none"?NaN:Je),k.seRmean.push(l==="none"?NaN:Math.sqrt(Xe)*be),L.push(l==="none"?p==="none"?[P,k.nMax[q],k.nStart[q],k.events[q],k.median[q][0]]:[P,k.nMax[q],k.nStart[q],k.events[q],k.median[q][0],k.median[q][1],k.median[q][2]]:p==="none"?[P,k.nMax[q],k.nStart[q],k.events[q],Je,Math.sqrt(Xe)*be,k.median[q][0]]:[P,k.nMax[q],k.nStart[q],k.events[q],Je,Math.sqrt(Xe)*be,k.median[q][0],k.median[q][1],k.median[q][2]])}return k.table={colNames:[...l==="none"?["records","n.max","n.start","events","median"]:["records","n.max","n.start","events","rmean","se(rmean)","median"],...p==="none"?[]:[`${m}LCL`,`${m}UCL`]],rowNames:S,table:L},J("survfit",k)}var f1=new Set(["newdata","confType","confInt","times","ties","weights","init","control","nocenter","confLevel","robust"]),m1=["eps","iterMax","tolerChol","tolerInf","timefix"];function vc(e,t,n,r={}){z(r,f1,"survfitCoxph"),z(_t(r,"control"),m1,"survfitCoxph","control");let{newdata:o,confType:s="log",confInt:i=.95,times:a,...c}=r;if(!["log","log-log","plain","none"].includes(s))throw new RangeError(`invalid 'confType': ${String(s)}`);if(!(i>0&&i<1))throw new RangeError("'confInt' must be between 0 and 1");let l=Ke(()=>ho(e,t,n,c)),{ties:f="efron",weights:m}=c,u=(n[0]??[]).map((Y,se)=>u1(n.map(H=>H[se]))),h=n.map(Y=>Y.map((se,H)=>u[H](se))),p=Ke(()=>ho(e,t,h,c)),d=p.coefficients.length,g=p.coefficients.map(Y=>Number.isFinite(Y)?Y:0),w=p.var.map(Y=>Y.map(se=>Number.isFinite(se)?se:0)),N=[];for(let Y=0;Y<e.length;Y++)Number.isNaN(e[Y])||Number.isNaN(t[Y])||n[Y].some(se=>Number.isNaN(se))||m!==void 0&&Number.isNaN(m[Y])||N.push(Y);let b=c.control?.timefix===!1?e.slice():vr(e);N.sort((Y,se)=>b[Y]-b[se]);let E=Y=>m===void 0?1:m[Y],v=Y=>Y.map((se,H)=>se-p.means[H]),y=new Map;for(let Y of N)y.set(Y,Math.exp(v(h[Y]).reduce((se,H,j)=>se+H*g[j],0)));let T=[];for(let Y of N)(T.length===0||T[T.length-1]!==b[Y])&&T.push(b[Y]);let M=T.length,R=new Array(M).fill(0),x=new Array(M).fill(0),A=Array.from({length:M},()=>new Array(d).fill(0)),S=new Array(M).fill(0),I=new Array(M).fill(0),C=new Array(M).fill(0),_=0,F=0,O=new Array(d).fill(0),D=N.length-1;for(let Y=M-1;Y>=0;Y--){let se=T[Y],H=0,j=0,te=0,B=new Array(d).fill(0);for(;D>=0&&b[N[D]]===se;D--){let U=N[D],ee=E(U),ae=y.get(U),ve=v(h[U]);_+=ee*ae,F+=ee;for(let Ne=0;Ne<d;Ne++)O[Ne]+=ee*ae*ve[Ne];if(t[U]===1){H++,j+=ee,te+=ee*ae;for(let Ne=0;Ne<d;Ne++)B[Ne]+=ee*ae*ve[Ne]}else C[Y]+=ee}if(S[Y]=F,I[Y]=j,H===0)continue;let Z=f==="efron"?H:1;for(let U=0;U<Z;U++){let ee=f==="efron"?U/H:0,ae=_-ee*te,Ne=j/Z/ae;R[Y]+=Ne,x[Y]+=Ne/ae;for(let ue=0;ue<d;ue++)A[Y][ue]+=Ne*((O[ue]-ee*B[ue])/ae)}}if(o?.some(Y=>Y.length!==d))throw new RangeError("each row of 'newdata' must have one value per column of 'x'");let k=(o??[l.means]).map(Y=>Y.map((se,H)=>u[H](se))),L=i<1-1e-8?re(1-(1-i)/2):re((1-i)/2,{lowerTail:!1}),q=[],$=[],V=[],P=[],Q=[];for(let Y of k){let se=v(Y),H=Math.exp(se.reduce((Ne,ue,fe)=>Ne+ue*g[fe],0)),j=0,te=0,B=new Array(d).fill(0),Z=[],U=[],ee=[],ae=[],ve=[];for(let Ne=0;Ne<M;Ne++){j+=R[Ne],te+=x[Ne];for(let ye=0;ye<d;ye++)B[ye]+=se[ye]*R[Ne]-A[Ne][ye];let ue=0;for(let ye=0;ye<d;ye++)for(let Se=0;Se<d;Se++)ue+=B[ye]*w[ye][Se]*B[Se];let fe=H*j,Ee=Math.exp(-fe),pe=H*Math.sqrt(te+ue);ve.push(fe),Z.push(Ee),U.push(pe);let[Ce,be]=h1(Ee,pe,L,s);ee.push(Ce),ae.push(be)}q.push(Z),$.push(U),V.push(ee),P.push(ae),Q.push(ve)}let K=q.map((Y,se)=>[Yr(T,Y),Yr(T,V[se]),Yr(T,P[se])]),X={fit:l,time:T,nRisk:S,nEvent:I,nCensor:C,surv:q,stdErr:$,lower:V,upper:P,cumhaz:Q,median:K,records:N.length,nStart:S[0]??0,events:I.reduce((Y,se)=>Y+se,0),warnings:l.warnings.slice()};return a!==void 0&&(X.at=p1(X,a)),J("survfitCoxph",X)}function u1(e){let t=s=>{let i=0;for(let a of s)Number.isFinite(a)&&Math.abs(a)>i&&(i=Math.abs(a));return i>0?Math.floor(Math.log2(i)):0},n=t(e),r=W(e.find(s=>Number.isFinite(s))??0,-n),o=t(e.map(s=>W(s,-n)-r));return s=>W(W(s,-n)-r,-o)}function h1(e,t,n,r){if(r==="none"||!Number.isFinite(t))return[NaN,NaN];if(r==="plain")return[Math.max(0,e-n*t*e),Math.min(1,e+n*t*e)];if(e===0)return[NaN,NaN];if(r==="log")return[Math.exp(Math.log(e)-n*t),Math.min(1,Math.exp(Math.log(e)+n*t))];if(e===1)return[NaN,NaN];let o=Math.log(-Math.log(e)),s=t/Math.log(e);return[Math.exp(-Math.exp(o-n*s)),Math.exp(-Math.exp(o+n*s))]}function p1(e,t){let n=[...t].sort((s,i)=>s-i),r={time:[],surv:e.surv.map(()=>[]),stdErr:e.surv.map(()=>[]),lower:e.surv.map(()=>[]),upper:e.surv.map(()=>[]),nRisk:[],nEvent:[]},o=0;for(let s of n){let i=-1;for(;i+1<e.time.length&&e.time[i+1]<=s;)i++;if(s>e.time[e.time.length-1])break;r.time.push(s);let a=e.time.findIndex(l=>l>=s);r.nRisk.push(a<0?0:e.nRisk[a]);let c=0;for(let l=o;l<=i;l++)c+=e.nEvent[l];r.nEvent.push(c),o=i+1,e.surv.forEach((l,f)=>{r.surv[f].push(i<0?1:e.surv[f][i]),r.stdErr[f].push(i<0?0:e.stdErr[f][i]),r.lower[f].push(i<0?1:e.lower[f][i]),r.upper[f].push(i<0?1:e.upper[f][i])})}return r}var d1=["vcov","df"],g1=["parm","level","vcov","df"];function Lu(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function Ou(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 Fu=e=>new Set(e).size===e.length;function Pu(e,t){sf(e);let n=e.coefficients,r=af(e),o=n.length,s=n.map(m=>Number.isNaN(m)),i,a,c=[],l;if(t===void 0){let m=cf(e),u=new Map(m.names.map((p,d)=>[p,d])),h=r.map((p,d)=>s[d]?void 0:u.get(p));i=h.map(p=>h.map(d=>p===void 0||d===void 0?NaN:m.vcov[p][d])),a=r,l=h.map(p=>p===void 0?NaN:Za(m.vcov,p,m.vcov[p][p])??Math.sqrt(m.vcov[p][p]))}else{let m=typeof t=="function"?Ke(()=>t(e)):t,u=Lu(m)?m.vcov:m;if(Lu(m)){if(i=Ou(m.vcov),a=m.names.slice(),a.length!==i.length)throw new RangeError("'vcov' must have one name per row");c=[...m.warnings??[]]}else i=Ou(m);l=i.map((h,p)=>{let d=h[p],g=typeof u=="object"&&u!==null?Za(u,p,d):void 0;if(g!==void 0&&!Number.isNaN(g))return g;if(!Number.isFinite(d))throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is not a finite number`);if(d<0)throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is negative`);return Math.sqrt(d)})}if(a!==void 0&&Fu(a)&&Fu(r)){let m=new Map(a.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:c}}let f;if(i.length===o)f=n.map((m,u)=>u);else if(i.length===e.rank)f=n.flatMap((m,u)=>s[u]?[]:[u]);else{let m=e.rank===o?`${o} x ${o}`:`${o} x ${o} or ${e.rank} x ${e.rank}`;throw new RangeError(`'vcov' must be ${m}, one row per coefficient`)}return{est:f.map(m=>n[m]),se:l,names:f.map(m=>r[m]),warnings:c}}function Du(e,t){if(t===void 0)return Ja(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 Vu=e=>Number.isFinite(e)&&e>0;function w1(e){return Ja(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function b1(e,t={}){z(t,d1,"coeftest");let{est:n,se:r,names:o,warnings:s}=Pu(e,t.vcov),i=Du(e,t.df),a=Vu(i),c=n.map((f,m)=>f/r[m]),l=c.map(f=>Number.isNaN(f)?NaN:a?2*at(-Math.abs(f),i,!0,!1):2*oe(-Math.abs(f)));return J("coeftest",{estimate:n,stdError:r,statistic:c,pValue:l,names:o,statisticName:a?"t value":"z value",df:i,method:a?"t test of coefficients":"z test of coefficients",nobs:w1(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function v1(e){let t=[100*e,100*(1-e)],n=t.map(o=>{if(o===0||!Number.isFinite(o))return 0;let s=Math.floor(Math.log10(Math.abs(o))),i=o.toFixed(Math.max(0,2-s));return(i.includes(".")?i.replace(/0+$/,"").split(".")[1]:"").length}),r=Math.max(...n);return[`${t[0].toFixed(r)} %`,`${t[1].toFixed(r)} %`]}function N1(e,t={}){z(t,g1,"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=Pu(e,t.vcov),o=Du(e,t.df),s=r.est.map((m,u)=>u),i=t.parm;if(i!==void 0){let m=Array.isArray(i)?i:[i];if(m.every(u=>typeof u=="string")){let u=new Set(m);s=s.filter(h=>u.has(r.names[h]))}else if(m.every(u=>typeof u=="number"))s=m.map(u=>{if(!Number.isInteger(u)||u<0)throw new RangeError("'parm' must hold 0-based indices or names");if(u>=r.est.length)throw new RangeError("subscript out of bounds");return u});else throw new TypeError("'parm' must hold 0-based indices or names")}let a=(1-n)/2,[c,l]=Vu(o)?[it(a,o,!0,!1),a<1e-8?it(a,o,!1,!1):it(1-a,o,!0,!1)]:[re(a),a<1e-8?re(a,{lowerTail:!1}):re(1-a)],f=(m,u,h)=>m+h*u;return J("coefci",{lower:s.map(m=>f(r.est[m],r.se[m],c)),upper:s.map(m=>f(r.est[m],r.se[m],l)),names:s.map(m=>r.names[m]),colNames:v1(a),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var M1=["test","dispersion"],y1=(e,t)=>vt(e,t/2,2,!1,!1);function E1(e,t,n){let{fit:r,w:o}=Cr(e,t,n,{start:void 0});return{dfRes:o.filter(i=>i>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(i=>t.map(a=>i[a])),intercept:e.intercept}}var Nc=(e,t)=>cc(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function T1(e,t={}){z(t,M1,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Qs),o=r.every(u=>u.k===r[0].k)?r[0].k:0,s=o===0?n:r.map(u=>u.fit),i=t.dispersion===void 0?void 0:rf(t.dispersion,o),a=i?.s??0,c=R1(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},a);if(o===0&&a===0)return J("glmAnova",c,...n);let l=new Set(["Deviance","Resid. Dev","Rao","Cp"]),f=c.colNames.map(u=>l.has(u)),m=c.table.map(u=>u.map((h,p)=>f[p]?W(h,2*o):h));if(a!==0&&c.colNames.includes("Cp")){let u=c.colNames.indexOf("Cp"),h=c.colNames.indexOf("Resid. Dev"),p=c.colNames.indexOf("Resid. Df"),d=s[0].model.x.length;for(let g of m)g[u]=g[h]+2*(d-g[p])*t.dispersion}return J("glmAnova",{...c,table:m},...n)}function R1(e,t,n,r=0){let o=new pt,s=e[0],i=Xa(s.model.familySpec);if(e.some(M=>{let R=Xa(M.model.familySpec);return R.family!==i.family||R.name!==i.name||R.varianceName!==i.varianceName}))throw new RangeError("models were not all fitted with the same family and link");let a=i.fixedDispersion,c=n.test===void 0?n.dispersion!==void 0||a?"Chisq":"F":n.test;if(c!==!1&&!["Rao","LRT","Chisq","F","Cp"].includes(c))throw new RangeError(`invalid 'test': ${String(c)}`);let l=[],f,m,u=[],h=new Set,p;if(t){let M=e[0].model.x.length;if(e.some(x=>x.model.x.length!==M))throw new RangeError("models were not all fitted to the same size of dataset");let R=(x,A)=>x.length===A.length&&x.every((S,I)=>JSON.stringify(S)===JSON.stringify(A[I]));if(e.some(x=>!R(x.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(x=>!R(x.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((x,A)=>l.push({name:String(A+1),dfRes:x.dfResidual,dev:x.deviance})),f=l.map((x,A)=>A===0?NaN:l[A-1].dfRes-x.dfRes),m=l.map((x,A)=>A===0?NaN:l[A-1].dev-x.dev),e.forEach((x,A)=>{if(A===0||f[A]===0)return;let[S,I]=f[A]>0?[e[A-1],x]:[x,e[A-1]],C=O=>O.filter((D,k)=>S.priorWeights[k]>0&&I.priorWeights[k]>0),_=S.model.offset.map((O,D)=>O-I.model.offset[D]),F=_.some((O,D)=>O!==0&&S.priorWeights[D]>0)?S.model.x.map((O,D)=>[...O,_[D]]):S.model.x;js(C(F),C(I.model.x))||h.add(A)}),c==="Rao"){let x=e.map(A=>({dfRes:A.dfResidual,dev:A.deviance,fit:$u(A),x:A.model.x,intercept:A.model.intercept}));u=l.map((A,S)=>S===0?NaN:f[S]<0?-Nc(x[S],x[S-1]):Nc(x[S-1],x[S]))}p=e.reduce((x,A)=>A.dfResidual<x.dfResidual?A:x)}else{let M=s,R=M.model,x=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,A=R.intercept?[0]:[],S=C=>[...A,...Array.from({length:C},(_,F)=>F+A.length)],I=[];for(let C=0;C<=x;C++)C===x?I.push({dfRes:M.dfResidual,dev:M.deviance,fit:$u(M),x:R.x,intercept:R.intercept}):C>0||c==="Rao"?I.push(E1(R,S(C),o)):I.push(void 0);l.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let C=1;C<=x;C++)l.push({name:`x${C}`,dfRes:I[C].dfRes,dev:I[C].dev});f=l.map((C,_)=>_===0?NaN:l[_-1].dfRes-C.dfRes),m=l.map((C,_)=>_===0?NaN:Math.max(l[_-1].dev-C.dev,0)),c==="Rao"&&(u=l.map((C,_)=>_===0?NaN:Nc(I[_-1],I[_]))),p=M}e.forEach((M,R)=>{M.converged||o.add(`model ${R+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let d=new pt,g=n.dispersion??Go(p,d);d.list.forEach(M=>o.add(M));let w=a?1/0:p.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],b=l.map((M,R)=>t?[M.dfRes,M.dev,f[R],m[R]]:[f[R],m[R],M.dfRes,M.dev]),E=[...N],v=M=>l.map((R,x)=>{if(x===0||f[x]===0||h.has(x))return NaN;let A=tt(M[x]/g*Math.sign(f[x]),-2*r);return A<0||Number.isNaN(A)?NaN:y1(A,Math.abs(f[x]))}),y=[];if(c==="Chisq"||c==="LRT")E.push("Pr(>Chi)"),y=[v(m)];else if(c==="Rao")E.push("Rao","Pr(>Chi)"),y=[u.map((M,R)=>h.has(R)?NaN:M),v(u)];else if(c==="F"){if(a&&o.add(`using F test with a '${i.family}' family is inappropriate`),w!==0){let M=l.map((R,x)=>{let A=tt(m[x]/f[x]/g,-2*r);return x===0||f[x]===0||h.has(x)||A<0?NaN:A});E.push("F","Pr(>F)"),y=[M,M.map((R,x)=>Number.isNaN(R)?NaN:Nt(R,Math.abs(f[x]),w,!1,!1))]}}else if(c==="Cp"){let M=p.model.x.length;E.push("Cp"),y=[l.map(R=>R.dev+2*(M-R.dfRes)*g)]}let T=b.map((M,R)=>[...M,...y.map(x=>x[R])]);return{colNames:E,rowNames:l.map(M=>M.name),table:T,warnings:o.list}}function $u(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 x1=["type"];function zu(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let i=e.y.flatMap((p,d)=>e.weights===void 0||e.weights[d]!==0?[d]:[]),a=i.map(p=>e.weights===void 0?1:Math.sqrt(e.weights[p])),c=Yt(i.map((p,d)=>s.map(g=>e.x[p][g]*a[d]))),l=Zl(e.weights===void 0?void 0:i.map(p=>W(e.weights[p],-o)),i.length),f=c.pivot.slice(0,c.rank),{qty:m}=Gs(f.map(p=>i.map((d,g)=>Kt(l[g],e.x[d][s[p]]))),i.map((p,d)=>Kt(l[d],W(e.y[p],-r)))),u=[0,0],h=0;return f.forEach((p,d)=>{p>=t.length&&(u=he(u,_e(m[d],m[d])),h++)}),{ss:u[0],df:h}}function Mc(e,t={}){if(z(t,x1,"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=fa([e]),i=ma([e],s)[0];if(o===0)throw new Error("residual df = 0");let a=Li(e,s),c=y=>{let T=a.map(([x,A])=>[W(x,-y),A]),M=T.reduce((x,[A])=>x+A,0),R=T.reduce((x,[A,S])=>x+A*S,0)/M;return Number.isFinite(M)&&Number.isFinite(R)?T.reduce((x,[A,S])=>x+A*(S-R)**2,0):NaN},l=0,f=c(0);if(Number.isFinite(f)||(l=ua(a),f=c(l)),Fr(a,i,l)||i===0)throw new Error("residual sum of squares is 0 (within rounding error)");let m=[...e.warnings],u=e.termLabels.length;r===2&&u===0&&e.intercept&&(m.push("the model contains only an intercept: Type III test substituted"),r=3);let h=y=>e.assign.flatMap((T,M)=>y.has(T)?[M]:[]),p=[...new Set(e.assign)],d=[],g=[],w=y=>{d.length=0;let T=[];if(r===3){if(e.coefficients.some(M=>Number.isNaN(M)))throw new Error("there are aliased coefficients in the model");for(let M of p){let R=h(new Set([M])),x=h(new Set(p.filter(A=>A!==M)));d.push(M===0?"(Intercept)":e.termLabels[M-1]),T.push({ss:zu(e,x,R,s,y).ss,df:R.length})}}else{let M=R=>e.factors.flatMap((x,A)=>x[R-1]>0?[A]:[]);for(let R of p){if(R===0)continue;let x=M(R),A=p.filter(C=>C!==0&&C!==R&&x.every(_=>e.factors[_][C-1]>0)),S=h(new Set(p.filter(C=>C!==R&&!A.includes(C)))),I=zu(e,S,h(new Set([R])),s,y);d.push(e.termLabels[R-1]),T.push(I)}}return T};g=w(l),l===0&&e.weights!==void 0&&g.some(y=>!Number.isFinite(y.ss))&&(l=ua(a),g=w(l));let N=y=>W(y,2*s+l),b=W(i,-l),E=b/o,v=g.map(({ss:y,df:T})=>{let M=y/T/E;return[N(y),T,M,Nt(M,T,o,!1,!1)]});return v.push([N(b),o,NaN,NaN]),J("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...d,"Residuals"],table:v,warnings:m},e)}var Ma=[0,0];var yc=e=>e.map(t=>t.map(n=>n[0]));function Ku(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function Wr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Ma);return r.forEach((s,i)=>{let a=t[i];for(let c=0;c<n;c++)o[c]=he(o[c],_e(s,a[c]))}),o})}function jr(e){let t=Ma;return e.forEach((n,r)=>t=he(t,n[r])),t}function Yu(e){let t=e.length,n=e.map(i=>i.slice()),r=Array.from({length:t},(i,a)=>a),o=1,s=!1;for(let i=0;i<t;i++){let a=i;for(let c=i+1;c<t;c++)Math.abs(n[c][i][0])>Math.abs(n[a][i][0])&&(a=c);if(n[a][i][0]===0){s=!0;continue}a!==i&&([n[i],n[a]]=[n[a],n[i]],[r[i],r[a]]=[r[a],r[i]],o=-o);for(let c=i+1;c<t;c++){let l=ut(n[c][i],n[i][i]);n[c][i]=l;for(let f=i+1;f<t;f++)n[c][f]=Le(n[c][f],_e(l,n[i][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Wu(e){let{a:t,sign:n,singular:r}=Yu(e);if(r)return Ma;let o=[n,0];return t.forEach((s,i)=>o=_e(o,s[i])),o}function ju(e){let t=e.length,{a:n,perm:r,singular:o}=Yu(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(Ma));for(let i=0;i<t;i++){let a=r.map(c=>[c===i?1:0,0]);for(let c=0;c<t;c++)for(let l=0;l<c;l++)a[c]=Le(a[c],_e(n[c][l],a[l]));for(let c=t-1;c>=0;c--){for(let l=c+1;l<t;l++)a[c]=Le(a[c],_e(n[c][l],a[l]));a[c]=ut(a[c],n[c][c])}for(let c=0;c<t;c++)s[c][i]=a[c]}return s}var S1=["group","idata","M","X"],Bu=(e,t)=>vt(e,t/2,2,!1,!1);function Ec(e){let t=e.length,n=ht(jr(e),t);return Wu(e.map(r=>r.map(o=>ut(o,n))))[0]}function Tc(e,t,n,r){let o=1-(2*t*t+t+2)/(6*t*n),s=(t+2)*(t-1)*(t-2)*(2*t**3+6*t*t+3*r+2)/(288*(n*t*o)**2),i=-n*o*Math.log(e),a=t*(t+1)/2-1,c=Bu(i,a),l=Bu(i,a+4);return c+s*(l-c)}var A1=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function k1(e,t,n){let r=e.length,o=w=>w.length===0?0:Yt(Array.from({length:r},(N,b)=>w.map(E=>E[b]))).rank,s=n.length===0?void 0:Yt(Array.from({length:r},(w,N)=>n.map(b=>b[N]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(w=>n[w]),a=i.length,c=[...i,...t],l=Yt(Array.from({length:r},(w,N)=>c.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(w=>c[w]),m=w=>w.map(N=>[N,0]),u=new Array(r).fill([0,0]),h=pr(f.map(m),u,e.map((w,N)=>e.map(b=>b[N]))).qtv,p=Array.from({length:r},(w,N)=>h.map(b=>b[N])),d=pr(f.map(m),u,p).qtv,g=Array.from({length:l.rank-a},(w,N)=>a+N);return g.map(w=>g.map(N=>d[w][N]))}function q1(e,t={}){z(t,S1,"mauchlyTest");let n=e.map(v=>Array.from(v)),r=n[0]?.length??0;if(r===0||n.some(v=>v.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=v=>v==null||typeof v=="number"&&Number.isNaN(v),i=n.map((v,y)=>!v.some(s)&&(o===void 0||!s(o[y])));if(n.some((v,y)=>i[y]&&v.some(T=>!Number.isFinite(T))))throw new RangeError("y must not contain infinite values");let a=Re(n.filter((v,y)=>i[y])).groups,c=a.length,l=o?.filter((v,y)=>i[y]).map(xt)??a.map(()=>""),f=[...new Set(l)].sort(Ln),m=a.map(v=>v.map(()=>[0,0]));for(let v of f){let y=l.flatMap((T,M)=>T===v?[M]:[]);for(let T=0;T<r;T++){let M=[0,0];for(let x of y)M=he(M,[a[x][T],0]);let R=ht(M,y.length);for(let x of y)m[x][T]=Le([a[x][T],0],R)}}let u=c-f.length;if(u<=0)throw new RangeError(`no residual degrees of freedom: ${c} complete row(s) for ${f.length} group(s)`);let h=Array.from({length:r},(v,y)=>Array.from({length:r},(T,M)=>{let R=[0,0];for(let x of m)R=he(R,_e(x[y],x[M]));return R})),p=t.idata??{index:Array.from({length:r},(v,y)=>y+1)},d=v=>{let y=Ke(()=>so(v,p));if(y.x.length!==r)throw new RangeError("idata must have one row per column of y");return A1(y.x,y.colNames.length)},g=t.M===void 0?Array.from({length:r},(v,y)=>Array.from({length:r},(T,M)=>M===y?1:0)):d(t.M),w=d(t.X??"~0"),N=k1(h,g,w),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=Ec(N);return{statistic:E,statisticName:"W",pValue:Tc(E,b,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var I1=["n","d","type","sigLevel","power","alternative"],_1=["n1","n2","d","sigLevel","power","alternative"],C1=["h","n","sigLevel","power","alternative"],L1=["h","n1","n2","sigLevel","power","alternative"],O1=["h","n","sigLevel","power","alternative"],F1=["d","n","sigLevel","power","alternative"],P1=["n","r","sigLevel","power","alternative"],D1=["w","N","df","sigLevel","power"],V1=["k","n","f","sigLevel","power"],$1=["u","v","f2","sigLevel","power"],z1=["size"],K1={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 Gu(e="p",...t){z(Ys(t),z1,"cohenES");let n=Ks(["size"],t).size??"small",r=Te(e,["p","t","r","anov","chisq","f2"]),o=Te(n,["small","medium","large"]);return{test:r,size:o,effectSize:K1[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function Y1(e,t){if(!ya(e)||!ya(t))throw new RangeError("p1 and p2 must be probabilities in [0, 1]");return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function W1(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(ya))throw new RangeError("'P0' and 'P1' must be probabilities in [0, 1]");let n=0;for(let r=0;r<e.length;r++)(e[r]!==0||t[r]!==0)&&(n+=Dt(t[r]-e[r],e[r]));return Math.sqrt(n)}function j1(e){if(e.length===0||e[0].length===0||e.some(s=>s.length!==e[0].length))throw new RangeError("'P' must be a non-empty matrix (rows of the same length)");if(!e.every(s=>s.every(ya)))throw new RangeError("'P' must contain probabilities in [0, 1]");let t=e.map(s=>s.reduce((i,a)=>i+a,0)),n=e[0]?.length??0,r=Array.from({length:n},(s,i)=>e.reduce((a,c)=>a+c[i],0)),o=0;for(let s=0;s<n;s++)for(let i=0;i<e.length;i++){let a=t[i]*r[s];if(!(a===0&&e[i][s]===0))if(a>=B1)o+=Dt(e[i][s]-a,a);else{let c=Math.sqrt(t[i]),l=Math.sqrt(r[s]),f=e[i][s]/c/l-c*l;o+=f*f}}return Math.sqrt(o)}var B1=2**-1022,ya=e=>e>=0&&e<=1,U1="\u2018sig.level\u2019 must be numeric in [0, 1]",G1="\u2018power\u2019 must be numeric in [0, 1]";function Mr(e,t){let n=r=>!qe(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(U1);if(n(t))throw new RangeError(G1)}function yr(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?Gu(t,e).effectSize:e}var hs=["two.sided","less","greater"];function Ea(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Hu(e,t,n,r){if(r==="less")return Uo(an(n,e),e,t,!0);if(r==="greater")return Uo(an(n,e,{lowerTail:!1}),e,t,!1);let o=an(n/2,e,{lowerTail:!1});return Uo(o,e,t,!1)+Uo(-o,e,t,!0)}var Uu=e=>e<1e-8?re(e,{lowerTail:!1}):re(1-e);function Qu(e,t,n){return n==="greater"?oe(Uu(t)-e,{lowerTail:!1}):n==="less"?oe(re(t)-e):oe(Uu(t/2)-e,{lowerTail:!1})+oe(re(t/2)-e)}function H1(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*=W(r,-o),n+=o}return W(t,n)}function Q1(e={}){z(e,I1,"pwrTTest");let{n:t,power:n}=e,r=yr(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Rn([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),Mr(o,n),!qe(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=Te(e.type,["two.sample","one.sample","paired"]),i=Te(e.alternative,hs),a=s==="two.sample"?2:1;i==="two.sided"&&!qe(r)&&(r=Math.abs(r));let c=(m,u,h)=>Hu((m-1)*a,Math.sqrt(m/a)*u,h,i);if(qe(n))n=c(t,r,o);else{let m=n;qe(t)?t=ft(u=>c(u,r,o)-m,2+1e-10,1e9):qe(r)?r=ft(u=>c(t,u,o)-m,...Ea(i,[1e-7,10])):o=ft(u=>c(t,r,u)-m,1e-10,1-1e-10)}let l=s==="paired"?"n is number of *pairs*":s==="two.sample"?"n is number in *each* group":void 0,f={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[s]+" t test power calculation";return{n:t,d:r,sigLevel:o,power:n,alternative:i,...l===void 0?{}:{note:l},method:f,warnings:[]}}function X1(e={}){z(e,_1,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=yr(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Rn([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),Mr(s,r),!qe(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!qe(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Te(e.alternative,hs);i==="two.sided"&&!qe(o)&&(o=Math.abs(o));let a=(c,l,f,m)=>Hu(c+l-2,f/Math.sqrt(1/c+1/l),m,i);if(qe(r))r=a(t,n,o,s);else{let c=r;qe(t)?t=ft(l=>a(l,n,o,s)-c,2+1e-10,1e9):qe(n)?n=ft(l=>a(t,l,o,s)-c,2+1e-10,1e9):qe(o)?o=ft(l=>a(t,n,l,s)-c,...Ea(i,[1e-7,10])):s=ft(l=>a(t,n,o,l)-c,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:s,power:r,alternative:i,method:"t test power calculation",warnings:[]}}function Rc(e,t,n,r,o,s){let{n:i,power:a}=t,c=yr(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(Rn([c,i,a,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),Mr(l,a),!qe(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=Te(t.alternative,hs);f==="two.sided"&&!qe(c)&&(c=Math.abs(c));let m=(u,h,p)=>Qu(u*r(h),p,f);if(qe(a))a=m(c,i,l);else{let u=a;qe(c)?c=ft(h=>m(h,i,l)-u,...Ea(f,[s,10])):qe(i)?i=ft(h=>m(c,h,l)-u,o,1e9):l=ft(h=>m(c,i,h)-u,1e-10,1-1e-10)}return{es:c,n:i,sigLevel:l,power:a,alternative:f}}function Z1(e={}){z(e,C1,"pwr2pTest");let t=Rc(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 J1(e={}){z(e,O1,"pwrPTest");let t=Rc(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 eb(e={}){z(e,F1,"pwrNormTest");let t=Rc(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 tb(e={}){z(e,L1,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=yr(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Rn([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),Mr(s,r),!qe(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!qe(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Te(e.alternative,hs);i==="two.sided"&&!qe(o)&&(o=Math.abs(o));let a=(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))},c=(l,f,m,u)=>Qu(l*Math.sqrt(a(f,m)),u,i);if(qe(r))r=c(o,t,n,s);else{let l=r;qe(o)?o=ft(f=>c(f,t,n,s)-l,...Ea(i,[1e-10,10])):qe(t)?t=ft(f=>c(o,f,n,s)-l,2+1e-10,1e9):qe(n)?n=ft(f=>c(o,t,f,s)-l,2+1e-10,1e9):s=ft(f=>c(o,t,n,f)-l,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:s,power:r,alternative:i,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes",warnings:[]}}function nb(e={}){z(e,P1,"pwrRTest");let{n:t,power:n}=e,r=yr(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Rn([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),Mr(o,n),!qe(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!qe(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=Te(e.alternative,hs);s==="two.sided"&&!qe(r)&&(r=Math.abs(r));let i=(c,l,f,m)=>{let u=an(m?f/2:f,c-2,{lowerTail:!1}),h=u**2+c-2,p=h===1/0&&Number.isFinite(u)?1/Math.sqrt(1+(c-2)/u/u):Math.sqrt(u**2/h),d=Math.atanh(l)+l/(2*(c-1)),g=Math.atanh(p),w=oe((d-g)*Math.sqrt(c-3));return m?w+oe((-d-g)*Math.sqrt(c-3)):w},a=(c,l,f)=>s==="two.sided"?i(c,l,f,!0):i(c,s==="less"?-l:l,f,!1);if(qe(n))n=a(t,r,o),s==="less"&&(r=-r);else{let c=n;if(qe(t))t=ft(l=>a(l,r,o)-c,4+1e-10,1e9);else if(qe(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=ft(m=>a(t,m,o)-c,l,f)}else o=ft(l=>a(t,r,l)-c,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:s,method:"approximate correlation power calculation (arctangh transformation)",warnings:[]}}function rb(e){z(e,D1,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=yr(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Rn([o,t,n,s],"exactly one of w, N, power, and sig.level must be NULL"),typeof r!="number")throw new TypeError('argument "df" is missing, with no default');if(!(r>0&&r<1/0))throw new RangeError("'df' must be a positive finite number");if(Mr(s,n),!qe(o)&&!(o>=0))throw new RangeError("w must be positive");if(!qe(t)&&!(t>=1))throw new RangeError("number of observations must be at least 1");let i=(a,c,l)=>{let f=c*a**2;return f===1/0?1:Ze(jn(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(qe(n))n=i(o,t,s);else{let a=n;qe(o)?o=ft(c=>i(c,t,s)-a,1e-10,1e9):qe(t)?t=ft(c=>i(o,c,s)-a,1+1e-10,1e9):s=ft(c=>i(o,t,c)-a,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 ob(e={}){z(e,V1,"pwrAnovaTest");let{k:t,n,power:r}=e,o=yr(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Rn([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),Mr(s,r),!qe(o)&&!(o>=0))throw new RangeError("f must be positive");if(!qe(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!qe(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");let i=(a,c,l,f)=>{let m=a-1,u=(c-1)*a,h=a*c*l**2;return!Number.isFinite(h)&&Number.isFinite(a)&&Number.isFinite(c)&&Number.isFinite(l)&&(h=H1([a,c,l,l])),h===1/0?1:Ga(m,u)?Ha(m,u,h,f):Cn(to(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(qe(r))r=i(t,n,o,s);else{let a=r;qe(t)?t=ft(c=>i(c,n,o,s)-a,2+1e-10,100):qe(n)?n=ft(c=>i(t,c,o,s)-a,2+1e-10,1e9):qe(o)?o=ft(c=>i(t,n,c,s)-a,1e-7,1e7):s=ft(c=>i(t,n,o,c)-a,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 sb(e={}){z(e,$1,"pwrF2Test");let{u:t,v:n,power:r}=e,o=yr(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Rn([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),Mr(s,r),!qe(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!qe(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!qe(n)&&!(n>=1))throw new RangeError("degree of freedom v for denominator must be at least 1");let i=(a,c,l,f)=>{let m=l*(a+c+1);return!Number.isFinite(m)&&Number.isFinite(a)&&Number.isFinite(c)&&Number.isFinite(l)&&(m=2*(l*(a/2+c/2+.5))),m===1/0?1:Ga(a,c)?Ha(a,c,m,f):Cn(to(f,a,c,{lowerTail:!1}),a,c,{ncp:m,lowerTail:!1})};if(qe(r))r=i(t,n,o,s);else{let a=r;qe(t)?t=ft(c=>i(c,n,o,s)-a,1+1e-10,100):qe(n)?n=ft(c=>i(t,c,o,s)-a,1+1e-10,1e9):qe(o)?o=ft(c=>i(t,n,c,s)-a,1e-7,1e7):s=ft(c=>i(t,n,o,c)-a,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:s,power:r,method:"Multiple regression power calculation",warnings:[]}}var ab=["id","dv","between","within","type","correction","es","observed"],Ta=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],ib=1e-12,Xu=1e-24;function Zu(e,t){let n=Object.prototype.hasOwnProperty.call(e,t)?e[t]:void 0;if(n===void 0)throw new RangeError(`object '${t}' not found`);return Array.from(n)}var ps=e=>typeof e=="boolean"?e?"TRUE":"FALSE":xt(e);function cb(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(ps):[...new Set(t.map(ps))].sort(Ln)}var Ju=(e,t)=>t.split(":").includes(e);function eh(e,t,n,r,o){let s=l=>r.some(f=>Ju(f,l));for(let l of r)if(!e.some(f=>Ju(l,f.name)))throw new RangeError(`Observed variable not in data: ${l}`);let i=[...t.values()].reduce((l,f)=>l+f,0),a=e.reduce((l,f)=>s(f.name)?l+f.ss:l,0),c=["pes","ges"].filter(l=>n.includes(l));return{colNames:["num Df","den Df","MSE","F",...c,"Pr(>F)"],rowNames:e.map(l=>l.name),table:e.map(l=>{let f=c.map(m=>m==="pes"?l.ss/(l.ss+l.errSS):l.ss/(l.ss+i+a-(s(l.name)?l.ss:0)));return[l.numDf,l.denDf,o(l.errSS/l.denDf),l.f,...f,l.p]})}}function lb(e,t){z(t,ab,"aovEz");let n=Ta(t.between),r=Ta(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[B,Z]of[["between",n],["within",r]]){let U=Z.filter((ee,ae)=>Z.indexOf(ee)!==ae);if(U.length>0)throw new RangeError(`duplicated factor names in '${B}': ${[...new Set(U)].join(", ")}`)}let o=n.filter(B=>r.includes(B));if(o.length>0)throw new RangeError(`factors in both 'between' and 'within': ${o.join(", ")}`);let s=t.type??3;if(s!==2&&s!==3)throw new RangeError(`invalid type (${String(s)}): use 2 or 3`);let i=t.correction??"GG";if(!["GG","HF","none"].includes(i))throw new RangeError(`invalid 'correction': ${String(i)}`);let a=Ta(t.es??"ges");for(let B of a)if(B!=="ges"&&B!=="pes")throw new RangeError(`invalid 'es': ${String(B)}`);let c=Ta(t.observed),l=[],f=B=>B==null||typeof B=="number"&&Number.isNaN(B),m=Zu(e,t.dv),u=[t.id,...n,...r].map(B=>Zu(e,B));for(let B of u)if(B.length!==m.length)throw new RangeError("the columns must have the same length");let h=m.map((B,Z)=>u.every(U=>!f(U[Z]))),p=B=>B.filter((Z,U)=>h[U]),d=Pt(p(u[0])).map(ps),g=p(m).map(B=>{if(f(B))return NaN;if(typeof B!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(B))throw new RangeError(`the response '${t.dv}' must be finite`);return B}),w=n.map((B,Z)=>Pt(p(u[1+Z]))),N=r.map((B,Z)=>Pt(p(u[1+n.length+Z]))),b=d.length;for(let B of[g,...w,...N])if(B.length!==b)throw new RangeError("the columns must have the same length");let E=w.map(B=>cb(B)),v=w.map(B=>B.map(ps)),y=N.map(B=>B.map(ps)),T=y.map(B=>[...new Set(B)]);E.forEach((B,Z)=>{if(B.length<2)throw new Error(`Factor "${n[Z]}" consists of one level only. Remove factor from model?`)}),T.forEach((B,Z)=>{if(B.length<2)throw new Error(`Factor "${r[Z]}" consists of one level only. Remove factor from model?`)});let M=[...new Set(d)],R=new Map,x=new Set;if(d.forEach((B,Z)=>{let U=JSON.stringify(v.map(ae=>ae[Z])),ee=R.get(B);ee===void 0?R.set(B,U):ee!==U&&x.add(B)}),x.size>0)throw new Error(`Following ids are in more than one between subjects condition:
|
|
8
|
+
${M.filter(B=>x.has(B)).join(", ")}`);let A=[[]];for(let B of T)A=A.flatMap(Z=>B.map(U=>[...Z,U]));let S=B=>JSON.stringify(y.map(Z=>Z[B])),I=new Map(A.map((B,Z)=>[JSON.stringify(B),Z])),C=new Set(d.map((B,Z)=>S(Z)));if(A.some(B=>!C.has(JSON.stringify(B))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let _=new Map,F=new Set;d.forEach((B,Z)=>{let U=_.get(B);U===void 0&&_.set(B,U=new Set),U.add(S(Z)),Number.isNaN(g[Z])&&F.add(B)});let O=M.filter(B=>F.has(B)||_.get(B).size<A.length),D=new Set(O),k=Re([g.filter((B,Z)=>!D.has(d[Z]))]).k,L=k===0?g:g.map(B=>W(B,-k)),q=B=>W(B,2*k),$=A.length,V=new Map;if(d.forEach((B,Z)=>{let U=V.get(B);U===void 0&&V.set(B,U={s:A.map(()=>[0,0]),n:A.map(()=>0)});let ee=I.get(S(Z));U.s[ee]=he(U.s[ee],[L[Z],0]),U.n[ee]++}),[...V.values()].some(B=>B.n.some(Z=>Z>1))&&l.push("More than one observation per design cell, aggregating data using `fun_aggregate = mean`.\nTo turn off this warning, pass `fun_aggregate = mean` explicitly."),O.length>0){let B=[...O].sort(Ln),Z=B.length>=10?`${B.slice(0,10).join(", ")}, ... [showing first 10 only]`:B.join(", ");l.push(`Missing values for ${O.length} ID(s), which were removed before analysis:
|
|
9
|
+
${Z}`)}let P=M.filter(B=>!O.includes(B)),Q=new Map;d.forEach((B,Z)=>Q.has(B)||Q.set(B,Z));let K=P.map(B=>{let Z=V.get(B);return Z.s.map((U,ee)=>ht(U,Z.n[ee]))}),X={};n.forEach((B,Z)=>X[B]=P.map(U=>v[Z][Q.get(U)]));let Y=Object.fromEntries(n.map(B=>[B,"contr.sum"])),se=Object.fromEntries(n.map((B,Z)=>[B,E[Z]])),H="Rank deficient model matrix; insufficient data to estimate full model.";if($===1)return J("aovEz",j());return J("aovEz",te());function j(){let B="y";for(;n.includes(B);)B+="_";let Z={...X,[B]:K.map(fe=>fe[0][0])},U=Ke(()=>Xn(`${B} ~ ${n.length>0?n.join(" * "):"1"}`,Z,{contrasts:Y,levels:se}));if(U.rank<U.coefNames.length)throw new Error(H);if(U.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${K.length} subject(s) for ${U.rank} parameter(s)`);let ee=Ke(()=>Mc(U,{type:s}));l.push(...ee.warnings.filter(fe=>!l.includes(fe)));let ae=U.dfResidual,ve=ee.rowNames.flatMap((fe,Ee)=>{if(fe==="(Intercept)"||fe==="Residuals")return[];let[pe,Ce,be,ye]=ee.table[Ee];return[{name:fe,ss:pe,numDf:Ce,errSS:U.rss,denDf:ae,f:be,p:ye}]}),Ne=ee.colNames.indexOf("Sum Sq"),ue={anova:eh(ve,new Map([[0,U.rss]]),a,c,q),univariate:{colNames:ee.colNames,rowNames:ee.rowNames,table:ee.table.map(fe=>fe.map((Ee,pe)=>pe===Ne?q(Ee):Ee))},within:r,warnings:l};return jt.set(ue,{colNames:ee.colNames,rowNames:ee.rowNames,table:ee.table}),ue}function te(){let B=P.length,Z=K.reduce((ke,$e)=>$e.reduce((He,ce)=>He+ce[0]*ce[0],ke),0),U=n.length>0?Ke(()=>so(`~ ${n.join(" * ")}`,X,{contrasts:Y,levels:se})):{colNames:["(Intercept)"],assign:[0],x:P.map(()=>[1]),termLabels:[],factors:[]},ee=U.colNames.length;if(B===0||Yt(U.x).rank<ee)throw new Error(H);let ae=B-ee;if(ae<=0)throw new RangeError(`no residual degrees of freedom: ${B} subject(s) for ${ee} between-subject parameter(s)`);let ve=[0,...U.termLabels.map((ke,$e)=>$e+1)],Ne=["(Intercept)",...U.termLabels],ue=ke=>U.assign.flatMap(($e,He)=>ke.includes($e)?[He]:[]),fe=ke=>U.factors.flatMap(($e,He)=>$e[ke-1]>0?[He]:[]),Ee=ke=>U.x.map($e=>[$e[ke],0]),pe=new Array(B).fill([0,0]),Ce=(ke,$e,He)=>{let{qtv:ce}=pr([...ke,...$e].map(Ee),pe,He);return ce.map(xe=>xe.slice(ke.length,ke.length+$e.length))},be=ke=>{let{qtv:$e}=pr(U.colNames.map((He,ce)=>Ee(ce)),pe,ke);return $e.map(He=>He.slice(ee))},ye=Object.fromEntries(r.map((ke,$e)=>[ke,A.map(He=>He[$e])])),Se=Ke(()=>so(`~ ${r.join(" * ")}`,ye,{contrasts:Object.fromEntries(r.map(ke=>[ke,"contr.sum"])),levels:Object.fromEntries(r.map((ke,$e)=>[ke,T[$e]]))})),Ye=[0,...Se.termLabels.map((ke,$e)=>$e+1)],De=["(Intercept)",...Se.termLabels],Je=ke=>ke.map($e=>ke.map(He=>{let ce=[0,0];return $e.forEach((xe,de)=>ce=he(ce,_e(xe,He[de]))),ce})),Be=[],Xe=[],me=[],ne=[],we=[],ge=[],le=[],et=new Map,Oe=!1;for(let ke of Ye){let $e=Se.assign.flatMap((Ve,Pe)=>Ve===ke?[Pe]:[]),He=$e.length,ce=$e.map(Ve=>K.map(Pe=>Pe.reduce((St,wt,_n)=>he(St,Kt(wt,Se.x[_n][Ve])),[0,0]))),xe=$e.map(Ve=>$e.map(Pe=>[Se.x.reduce((St,wt)=>St+wt[Ve]*wt[Pe],0),0])),de=ju(xe),Ae=Je(be(ce)),We=Wr(Ae,de),Fe=jr(We)[0],rt=NaN,dt=NaN,Lt=NaN,Ht=NaN,or=!1;if(He>=2){let Ve=De[ke],Pe=Jl(yc(de)).map(Qt=>Qt.map(sn=>[sn,0])),St=yc(Wr(Wr(Pe,Ae),Ku(Pe))),wt=wn(St,{symmetric:!0,onlyValues:!0}).values,_n=wt.reduce((Qt,sn)=>Math.max(Qt,sn),0),Vt=Math.max(_n*ib,Z*Xu),It=jr(We),gt=jr(Wr(We,We));_n>Z*Xu&>[0]>0&&Number.isFinite(gt[0])?(or=!0,rt=ut(_e(It,It),Kt(gt,He))[0],ae-He*rt>0?dt=((ae+1)*He*rt-2)/(He*(ae-He*rt)):l.push(`The Huynh-Feldt epsilon of ${Ve} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${Ve} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),or&&wt.every(Qt=>Qt>Vt)?(Lt=Ec(Wr(de,Ae)),Ht=Tc(Lt,He,ae,$)):or&&l.push(`The error SSP matrix of ${Ve} is singular: no Mauchly test (the epsilons are still defined)`)}for(let Ve=0;Ve<ve.length;Ve++){let Pe=ve[Ve],St;if(s===3)St=Ce(ue(ve.filter(Ot=>Ot!==Pe)),ue([Pe]),ce);else if(Pe===0)St=Ce([],ue([0]),ce);else{let Ot=fe(Pe),nn=ve.filter(G=>G!==0&&G!==Pe&&Ot.every(ie=>U.factors[ie][G-1]>0));St=Ce(ue(ve.filter(G=>G!==Pe&&!nn.includes(G))),ue([Pe]),ce)}let wt=jr(Wr(Je(St),de))[0],Vt=(Pe===0?1:ue([Pe]).length)*He,It=ae*He,gt=wt/Vt/(Fe/It),Qt=Nt(gt,Vt,It,!1,!1),sn=ke===0?Ne[Ve]:Pe===0?De[ke]:`${Ne[Ve]}:${De[ke]}`;Be.push([q(wt),Vt,q(Fe),It,gt,Qt]),Xe.push([wt,Vt,Fe,It,gt,Qt]),me.push(sn),et.set(ke,Fe);let tn={name:sn,ss:wt,numDf:Vt,errSS:Fe,denDf:It,f:gt,p:Qt};if(or){let Ot=Math.min(1,dt),nn=Nt(gt,rt*Vt,rt*It,!1,!1),G=Nt(gt,Ot*Vt,Ot*It,!1,!1);ne.push([rt,nn,dt,G]),we.push([Lt,Ht]),ge.push(sn),i==="GG"?tn={...tn,numDf:rt*Vt,denDf:rt*It,p:nn}:i==="HF"&&(dt>1&&(Oe=!0),tn=Number.isNaN(dt)?{...tn,numDf:rt*Vt,denDf:rt*It,p:nn}:{...tn,numDf:Ot*Vt,denDf:Ot*It,p:G})}ke===0&&Pe===0||le.push(tn)}}Oe&&l.push("HF eps > 1 treated as 1");let nt={anova:eh(le,et,a,c,q),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:me,table:Be},within:r,warnings:l};return ne.length>0&&(nt.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:ge,table:ne},nt.sphericity={colNames:["Test statistic","p-value"],rowNames:[...ge],table:we}),jt.set(nt,{colNames:nt.univariate.colNames,rowNames:me,table:Xe}),nt}}var fb=["tau","covariates","covariateNames","confLevel"];function ds(e){return 2*oe(-Math.abs(e))}function rh(e,t,n){return[e-n*t,e+n*t]}function th(e,t,n){return{estimate:e,se:t,confInt:rh(e,t,n)}}function mb(e,t,n,r){return{estimate:e,confInt:rh(e,t,n),pValue:ds(r)}}function nh(e,t){let n=Nr(e.map(l=>l.time),e.map(l=>l.status),{rmean:t}),r=n.rmean[0],o=n.seRmean[0],s=0,i=1,a=Un();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];gn(a,(1-i)*(f-s),n.time.length+1),i=n.surv[l],s=f}gn(a,(1-i)*(t-s),n.time.length+1);let c=Gn(a);return{rmst:{estimate:r,se:o},rmtl:{estimate:c,se:o}}}function xc(e,t,n,r){if(n==="difference"){let l=e.estimate-t.estimate,f=Math.hypot(e.se,t.se);return mb(l,f,r,l/f)}let o=e.estimate,s=t.estimate;if(o===0||s===0)return{estimate:o===0?s===0?NaN:0:1/0,confInt:[NaN,NaN],pValue:NaN};if(!(o>0&&s>0))return{estimate:NaN,confInt:[NaN,NaN],pValue:NaN};let i=o/s,a=Math.log(i),c=Math.hypot(e.se/o,t.se/s);return{estimate:i,confInt:[Math.exp(a-r*c),Math.exp(a+r*c)],pValue:ds(a/c)}}function ub(e){let t=new Array(e.length).fill(0);for(let n of[0,1]){let r=e.flatMap((s,i)=>s.arm===n?[i]:[]),o=Nr(r.map(s=>e[s].time),r.map(s=>e[s].status===0?1:0),{rmean:"none"});for(let s of r){let i=e[s];if(i.status===0)continue;let a=1;for(let c=0;c<o.time.length&&o.time[c]<=i.time;c++)a=o.surv[c];if(!(a>0))throw new Error("the censoring survival probability is zero");t[s]=1/a}}return t}function Sc(e,t,n,r,o,s){let i=e.map(C=>[C.arm,...C.x]),a=i[0].map((C,_)=>{let F=i.reduce((D,k)=>Math.max(D,Math.abs(k[_])),0);if(F===0)return 1;let O=Math.floor(Math.log2(F));return O>500||O<-500?2**O:1}),c=i.map(C=>C.map((_,F)=>_/a[F])),l=e.map((C,_)=>[1,...c[_]]),f=[1,...a],m=ub(e),u=r?xn(c,n,{family:"poisson",weights:m}):Xn(c,n,{weights:m});if(r&&!u.converged)throw new Error("the adjusted RMST Poisson model did not converge");let p=u.coefficients.map((C,_)=>C/f[_]),d=u.fittedValues;if(p.some(C=>!Number.isFinite(C))||d.some(C=>!Number.isFinite(C)))throw new Error("the adjusted RMST model is singular or has non-finite fitted values");let g=l.map((C,_)=>C.map(F=>m[_]*F*(n[_]-d[_]))),w=g.map(C=>C.slice());for(let C of[0,1]){let _=e.flatMap((O,D)=>O.arm===C?[D]:[]),F=Nr(_.map(O=>e[O].time),_.map(O=>e[O].status===0?1:0),{rmean:"none"});for(let O=0;O<F.time.length;O++){let D=F.time[O],k=F.nEvent[O];if(!(D<t)||k===0)continue;let L=F.nRisk[O];if(!(L>0))continue;let q=new Array(l[0].length).fill(0);for(let V=0;V<q.length;V++){let P=Un();for(let Q of _)e[Q].time>=D&&gn(P,g[Q][V],_.length);q[V]=Gn(P)}let $=k/L;for(let V of _){let P=e[V],Q=P.time===D&&P.status===0,K=P.time>=D;for(let X=0;X<q.length;X++)w[V][X]+=q[X]/L*(Number(Q)-(K?$:0))}}}let N=l.map((C,_)=>C.map(F=>F*Math.sqrt(r?d[_]:1))),b=ti(N),E=ti(w),v=ot(b),T=ni(ni(v,E),v).map((C,_)=>C.map((F,O)=>F/f[_]/f[O])),M=T.map((C,_)=>Math.sqrt(Math.max(0,C[_]))),R=p.map((C,_)=>C/M[_]),x=R.map(ds),A=p.map((C,_)=>C-s*M[_]),S=p.map((C,_)=>C+s*M[_]),I=r?[A.map(Math.exp),S.map(Math.exp)]:[A,S];return{coef:p,cov:T,table:{names:["intercept","arm",...o],estimate:p,se:M,z:R,pValue:x,confInt:I}}}function hb(e,t,n,r={}){if(z(r,fb,"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((K,X)=>Number.isFinite(X)?Math.max(K,Math.abs(X)):K,0),i=s>2**500?2**Math.floor(Math.log2(s)):1,c=e.every(K=>!Number.isFinite(K)||K===0||K/i!==0)&&(r.tau===void 0||r.tau===0||r.tau/i!==0)?i:1,l=K=>K/c,f=Array.from(t),m=new Set;for(let K=0;K<f.length;K++){let X=n[K]===!0?1:n[K]===!1?0:n[K];(X===0||X===1)&&f[K]===2&&m.add(X)}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(K=>[K]):r.covariates.map(K=>Array.from(K));if(p&&p.length!==e.length)throw new RangeError("'covariates' must have one row per observation");if(p&&p.some(K=>K.length!==p[0].length))throw new RangeError("'covariates' must have the same number of columns in every row");if(p&&p.some(K=>K.some(X=>typeof X!="number")))throw new TypeError("'covariates' must contain numeric values");if(p&&p.some(K=>K.some(X=>!Number.isFinite(X)&&!Number.isNaN(X))))throw new RangeError("'covariates' must be finite");let d=[],g=[],w=[],N=[];for(let K=0;K<e.length;K++){if(e[K]==null||Number.isNaN(e[K])||f[K]==null||Number.isNaN(f[K])||n[K]==null||Number.isNaN(n[K]))throw new Error("missing value where TRUE/FALSE needed");if(typeof e[K]!="number")throw new TypeError("'time' must contain numbers");if(!Number.isFinite(e[K]))throw new RangeError("'time' must be finite");if(typeof n[K]!="number"&&typeof n[K]!="boolean")throw new TypeError("'arm' must contain numbers or booleans");let X=n[K]===!0?1:n[K]===!1?0:n[K];if(X!==0&&X!==1){w.push(l(e[K]));continue}if(typeof f[K]!="number")throw new TypeError("'status' must contain numbers");let Y=f[K],se=Y===1?1:0,H=p?p[K]:[];if(H.some(te=>!Number.isFinite(te)))continue;g.push({time:l(e[K]),status:se,arm:X,x:H,index:K});let j=Y;if(m.has(X))if(Y===2)j=1;else if(Y===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}d.push({time:l(e[K]),status:j,arm:X,x:H,index:K})}if(g.some(K=>K.time<0))throw new Error("negative follow-up times are not supported");if(!d.some(K=>K.arm===0)||!d.some(K=>K.arm===1))throw new Error("both arms 0 and 1 are required");let b=[0,1].map(K=>Math.max(...g.filter(X=>X.arm===K).map(X=>X.time))),E=[0,1].map(K=>{let X=b[K];return g.some(Y=>Y.arm===K&&Y.time===X&&Y.status===0)}),v=E.some(Boolean)?Math.min(...b.filter((K,X)=>E[X])):Math.max(...b),y=r.tau===void 0,T=r.tau===void 0?v:l(r.tau),M=r.tau===void 0?T*c:r.tau;if(w.some(K=>K<=T))throw new RangeError("'arm' must contain only 0 and 1 through tau");if(!(T>0))throw new RangeError("'tau' must be greater than zero");if(T>v)throw new RangeError(`'tau' must be at most ${v*c}: the minimum of the largest times allowed by both arms`);if(T<Math.min(...g.map(K=>K.time)))throw new RangeError("'tau' must not be below the first observed time");let R=(1-o)/2,x=R<1e-8?re(R,{lowerTail:!1}):re(1-R),A=d.filter(K=>K.arm===1),S=d.filter(K=>K.arm===0),I=nh(A,T),C=nh(S,T),_=K=>({estimate:K.estimate*c,se:K.se*c}),F={rmst:_(I.rmst),rmtl:_(I.rmtl)},O={rmst:_(C.rmst),rmtl:_(C.rmtl)},D=th(F.rmst.estimate,F.rmst.se,x),k=th(O.rmst.estimate,O.rmst.se,x),L={estimate:F.rmtl.estimate,se:F.rmtl.se,confInt:[M-D.confInt[1],M-D.confInt[0]]},q={estimate:O.rmtl.estimate,se:O.rmtl.se,confInt:[M-k.confInt[1],M-k.confInt[0]]},$=xc(F.rmst,O.rmst,"difference",x),V=xc(F.rmst,O.rmst,"ratio",x),P=xc(F.rmtl,O.rmtl,"rmtlRatio",x),Q={tau:M,tauDefault:y,confLevel:o,arm1:{rmst:D,rmtl:L},arm0:{rmst:k,rmtl:q},difference:$,ratio:V,rmtlRatio:P,warnings:N};if(p){let K=r.covariateNames===void 0?h?["covariates"]:Array.from({length:p[0].length},()=>""):Array.from(r.covariateNames);if(K.some(ue=>typeof ue!="string"))throw new TypeError("'covariateNames' must contain strings");if(K.length!==p[0].length)throw new RangeError("'covariateNames' must have one name per covariate column");let X=d.map(ue=>({...ue,time:Math.min(ue.time,T),status:ue.time>=T?1:ue.status})),Y=X.map(ue=>ue.time),se=X.map(ue=>T-ue.time),H=Sc(X,T,Y,!1,K,x),j=!(F.rmst.estimate>0&&O.rmst.estimate>0),te=!(F.rmtl.estimate>0&&O.rmtl.estimate>0),B=j?void 0:Sc(X,T,Y,!0,K,x),Z=te?void 0:Sc(X,T,se,!0,K,x),U=(ue,fe)=>{let Ee=ue.coef[1],pe=Math.sqrt(Math.max(0,ue.cov[1][1]));return fe?{estimate:Math.exp(Ee),confInt:[Math.exp(Ee-x*pe),Math.exp(Ee+x*pe)],pValue:ds(Ee/pe)}:{estimate:Ee*c,confInt:[(Ee-x*pe)*c,(Ee+x*pe)*c],pValue:ds(Ee/pe)}},ee=ue=>({...ue,estimate:ue.estimate.map(fe=>fe*c),se:ue.se.map(fe=>fe*c),confInt:[ue.confInt[0].map(fe=>fe*c),ue.confInt[1].map(fe=>fe*c)]}),ae=ue=>({...ue,estimate:ue.estimate.map((fe,Ee)=>Ee===0?fe+Math.log(c):fe),confInt:[ue.confInt[0].map((fe,Ee)=>Ee===0?fe+Math.log(c):fe),ue.confInt[1].map((fe,Ee)=>Ee===0?fe+Math.log(c):fe)]}),ve={names:["intercept","arm",...K],estimate:Array(H.coef.length).fill(NaN),se:Array(H.coef.length).fill(NaN),z:Array(H.coef.length).fill(NaN),pValue:Array(H.coef.length).fill(NaN),confInt:[Array(H.coef.length).fill(NaN),Array(H.coef.length).fill(NaN)]},Ne={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};Q.adjusted={difference:U(H,!1),ratio:j?Ne:U(B,!0),rmtlRatio:te?Ne:U(Z,!0)},Q.coefficients={difference:ee(H.table),ratio:B?ae(B.table):ve,rmtlRatio:Z?ae(Z.table):ve},delete Q.arm1,delete Q.arm0}return J("rmst2",Q)}var pb=["method","confLevel","control"],db=["eps","iterMax"];function Ac(e,t){let n=t.length,r=0,o=new Array(n).fill(0),s=Array.from({length:n},()=>new Array(n).fill(0));for(let{x:i,cases:a}of e){let c=i.length;if(a===0||a===c)continue;let l=i.map(g=>g.reduce((w,N,b)=>w+N*t[b],0)),f=Math.max(...l),m=new Array(a+1).fill(0),u=new Array(a+1).fill(0),h=Array.from({length:a+1},()=>new Array(n).fill(0)),p=Array.from({length:a+1},()=>Array.from({length:n},()=>new Array(n).fill(0)));m[0]=1;for(let g=0;g<c;g++){let w=i[g],N=Math.exp(l[g]-f);for(let b=Math.min(a,g+1);b>=1;b--){let E=m[b]===0?u[b-1]:Math.max(u[b],u[b-1]),v=m[b]===0?0:2**(u[b]-E),y=N*2**(u[b-1]-E),T=m[b-1],M=h[b-1],R=p[b-1],x=h[b],A=p[b];m[b]=m[b]*v+y*T;for(let S=0;S<n;S++){let I=x[S];x[S]=I*v+y*(w[S]*T+M[S]);for(let C=0;C<n;C++)A[S][C]=A[S][C]*v+y*(w[S]*w[C]*T+w[S]*M[C]+w[C]*M[S]+R[S][C])}if(u[b]=E,m[b]>2**256||m[b]>0&&m[b]<2**-256){let S=Math.floor(Math.log2(m[b])),I=2**-S;m[b]=m[b]*I;for(let C=0;C<n;C++){x[C]=x[C]*I;for(let _=0;_<n;_++)A[C][_]=A[C][_]*I}u[b]=E+S}}}let d=0;for(let g=0;g<a;g++)d+=l[g]-f;r+=d-(Math.log(m[a])+u[a]*Math.LN2);for(let g=0;g<n;g++){let w=0;for(let b=0;b<a;b++)w+=i[b][g];let N=h[a][g]/m[a];o[g]+=w-N;for(let b=0;b<n;b++)s[g][b]+=p[a][g][b]/m[a]-N*(h[a][b]/m[a])}}return{loglik:r,score:o,info:s}}function kc(e,t){return{statistic:e,df:t,pValue:Ze(e,t,{lowerTail:!1})}}function gb(e,t,n,r={}){z(r,pb,"clogit"),z(_t(r,"control"),db,"clogit","control");let{method:o="exact",confLevel:s=.95,control:i={}}=r,{eps:a=1e-9,iterMax:c=20}=i,l=e.length;if(t.length!==l||n.length!==l)throw new RangeError("'y', 'x' and 'strata' must have the same length");if(o!=="exact"&&o!=="approximate"&&o!=="breslow")throw new RangeError(`invalid 'method': ${String(o)}`);e.forEach((L,q)=>{if(L!==0&&L!==1)throw new RangeError(`'y' must be 0 or 1 (element ${q+1})`)});let f=t[0]?.length??0;if(f===0||t.some(L=>L.length!==f||L.some(q=>!Number.isFinite(q))))throw new RangeError("'x' must have the same positive number of finite covariates in every row");let m=re((1+s)/2),u=(L,q,$,V,P,Q,K,X)=>{let Y=q.map((H,j)=>Math.sqrt(H[j])),se=L.map((H,j)=>H/Y[j]);return{coefficients:L,var:q,loglik:$,score:V,wald:P,iter:Q,n:l,nevent:e.reduce((H,j)=>H+j,0),method:K,se:Y,z:se,p:se.map(H=>Ze(H*H,1,{lowerTail:!1})),lower:L.map((H,j)=>Math.exp(H-m*Y[j])),upper:L.map((H,j)=>Math.exp(H+m*Y[j])),logtest:kc(2*($[1]-$[0]),f),sctest:kc(V,f),waldtest:kc(P,f),warnings:X}};if(o!=="exact"){let L=ho(new Array(l).fill(1),e,t,{ties:"breslow",strata:n,confLevel:s,control:{eps:a,iterMax:c}});return u(L.coefficients,L.var,L.loglik,L.score,L.wald,L.iter,"breslow",L.warnings)}let h=Array.from({length:f},(L,q)=>t.reduce(($,V)=>$+V[q],0)/l),p=new Map;for(let L=0;L<l;L++){let q=p.get(n[L]);q===void 0&&(q={cases:[],controls:[]},p.set(n[L],q)),(e[L]===1?q.cases:q.controls).push(t[L].map(($,V)=>$-h[V]))}let d=[...p.values()].map(L=>({x:[...L.cases,...L.controls],cases:L.cases.length})),g=2**-39,w=L=>{let q=L.map($=>$.slice());return{f:q,rank:zr(q,g)}},N=new Array(f).fill(0),b=Ac(d,N),E=[b.loglik,b.loglik],{f:v}=w(b.info),y=Kr(v,b.score),T=b.score.reduce((L,q,$)=>L+q*y[$],0),M=[],R=[N],x=b.loglik,A=N.map((L,q)=>L+y[q]),S=0,I=!1,C=0;for(C=1;C<=c;C++){b=Ac(d,A);let L=Number.isFinite(b.loglik)&&b.score.every(Number.isFinite);if(L&&Math.abs(1-x/b.loglik)<=a){I=!0,N=A;break}!L||b.loglik<x?(S++,A=A.map((q,$)=>(q+S*N[$])/(S+1)),R.push(N)):(S=0,x=b.loglik,{f:v}=w(b.info),y=Kr(v,b.score),N=A,R.push(N),A=A.map((q,$)=>q+y[$]))}I||(C=c,N=R[Math.max(1,c-1)]??N,M.push("Ran out of iterations and did not converge")),b=Ac(d,N),E[1]=b.loglik;let _=w(b.info),F=uo(_.f),O=N.map((L,q)=>_.rank<f&&F[q][q]===0?NaN:L),D=O.map((L,q)=>Number.isNaN(L)?-1:q).filter(L=>L>=0),k=0;for(let L of D)for(let q of D)k+=N[L]*b.info[L][q]*N[q];return u(O,F,E,T,k,C,"exact",M)}var oh=["transform","terms","collapseTerms","global"],er=e=>new Array(e).fill(0),Er=e=>Array.from({length:e},()=>er(e));function wb(e){let t=Array.from({length:e.length},(r,o)=>o).sort((r,o)=>e[r]-e[o]||r-o),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&e[t[o]]===e[t[r]];)o++;let s=(r+1+o)/2;for(let i=r;i<o;i++)n[t[i]]=s;r=o}return n}function sh(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 bb(e){let t=e.time.length,n=new Array(t),r=new Map;for(let c=0;c<t;c++){let l=e.time[c],f=r.get(l)??{rows:[],deaths:0};f.rows.push(c),e.status[c]&&f.deaths++,r.set(l,f)}let o=[...r.keys()].sort((c,l)=>c-l),s=e.time.slice().sort((c,l)=>c-l),i=(e.start??new Array(t).fill(-1/0)).slice().sort((c,l)=>c-l),a=1;for(let c of o){let l=r.get(c);for(let m of l.rows)n[m]=1-a;let f=sh(i,c)-sh(s,c);l.deaths>0&&(a*=1-l.deaths/f)}return n}function vb(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=bb(e);else if(t==="rank")o=wb(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 Nb(e,t={}){z(t,oh,"coxZph"),z(t,oh,"coxZph");let n=t.transform??"km",r=t.global??!0,o=t.collapseTerms??!0;if(typeof r!="boolean")throw new TypeError("'global' must be a boolean");if(typeof o!="boolean")throw new TypeError("'collapseTerms' must be a boolean");if(t.terms!==void 0&&t.terms!==!1&&!Array.isArray(t.terms))throw new TypeError("'terms' must be an array of column groups or false");if(e.nevent===0)throw new Error("the proportional-hazards test requires events");let s=e.coxZphData;if(s===void 0)throw new TypeError("coxZph requires a CoxphResult returned by coxph");let i=e.coefficients.map((j,te)=>Number.isNaN(j)?-1:te).filter(j=>j>=0),a=i.length;if(a===0)throw new Error("the proportional-hazards test has no estimable coefficients");let c=[];for(let j=0;j<s.sortedTime.length;){let te=j;for(;!s.stratumEnd[te];)te++;for(let B=j;B<=te;){let Z=s.sortedTime[B],U=B;for(;U<te&&s.sortedTime[U+1]===Z;)U++;let ee=[];for(let ae=B;ae<=U;ae++)s.sortedEvent[ae]&&ee.push(ae);s.start!==void 0&&ee.reverse();for(let ae of ee)c.push(s.sortedToInput[ae]);B=U+1}j=te+1}if(c.length===0)throw new Error("the proportional-hazards test requires events");let l=c.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=vb(s,n,c,f),u=m[c[0]];if(c.every(j=>m[j]===u))throw new Error("the transform is constant at the event times");let h=c.reduce((j,te)=>Math.max(j,Math.abs(m[te])),0),p=h===0?0:Math.floor(Math.log2(h)),d=m.map(j=>Number.isFinite(j)?W(j,-p):j),g=c.reduce((j,te)=>j+d[te],0)/c.length,w=d.map(j=>j-g);if(c.every(j=>w[j]===0))throw new Error("the transform is constant at the event times");let N=i.map(j=>s.betaScaled[j]),b=Er(a),E=Er(a),v=Er(a),y=er(a),T=[];for(let j=0;j<s.sortedTime.length;){let te=j;for(;!s.stratumEnd[te];)te++;for(let B=j;B<=te;){let Z=s.sortedTime[B],U=B;for(;U<te&&s.sortedTime[U+1]===Z;)U++;let ee=[];for(let ae=B;ae<=U;ae++)s.sortedEvent[ae]&&ee.push(ae);if(ee.length>0){let ae=[],ve=-1/0;for(let ne=j;ne<=te;ne++){if(!((s.sortedStart===void 0||s.sortedStart[ne]<Z)&&s.sortedTime[ne]>=Z))continue;ae.push(ne);let ge=0;for(let le=0;le<a;le++)ge+=N[le]*s.sortedX[ne][i[le]];ve=Math.max(ve,ge)}if(ae.length===0||!Number.isFinite(ve))throw new Error("empty Cox proportional-hazards risk set");let Ne=er(a),ue=Er(a),fe=er(a),Ee=Er(a),pe=0,Ce=0;for(let ne of ae){let we=0;for(let le=0;le<a;le++)we+=N[le]*s.sortedX[ne][i[le]];let ge=s.sortedWeight[ne]*Math.exp(we-ve);pe+=ge;for(let le=0;le<a;le++){let et=s.sortedX[ne][i[le]];Ne[le]+=ge*et;for(let Oe=0;Oe<a;Oe++)ue[le][Oe]+=ge*et*s.sortedX[ne][i[Oe]]}if(s.sortedEvent[ne]&&s.sortedTime[ne]===Z){Ce+=ge;for(let le=0;le<a;le++){let et=s.sortedX[ne][i[le]];fe[le]+=ge*et;for(let Oe=0;Oe<a;Oe++)Ee[le][Oe]+=ge*et*s.sortedX[ne][i[Oe]]}}}let be=0,ye=er(a);for(let ne of ee){let we=s.sortedWeight[ne];be+=we;for(let ge=0;ge<a;ge++)ye[ge]+=we*s.sortedX[ne][i[ge]]}let Se=ee.length,Ye=s.ties==="efron"&&Se>1?Se:1,De=er(a),Je=Er(a);for(let ne=0;ne<Ye;ne++){let we=ne/Ye,ge=pe-we*Ce,le=Ne.map((Oe,nt)=>(Oe-we*fe[nt])/ge);for(let Oe=0;Oe<a;Oe++)De[Oe]+=le[Oe]/Ye;let et=be/Ye;for(let Oe=0;Oe<a;Oe++)for(let nt=0;nt<a;nt++)Je[Oe][nt]+=et*((ue[Oe][nt]-we*Ee[Oe][nt])/ge-le[Oe]*le[nt])}let Be=ye.map((ne,we)=>ne-be*De[we]),Xe=s.sortedToInput[ee[0]],me=w[Xe];for(let ne=0;ne<a;ne++){y[ne]+=me*Be[ne];for(let we=0;we<a;we++)b[ne][we]+=Je[ne][we],E[ne][we]+=me*Je[ne][we],v[ne][we]+=me*me*Je[ne][we]}s.start!==void 0&&ee.reverse();for(let ne of ee){let we=s.sortedToInput[ne],ge=i.map(le=>s.sortedX[ne][le]);for(let le=0;le<a;le++)ge[le]-=De[le];T.push({input:we,time:Z,stratum:s.stratumIndex[ne],g:m[we],residual:ge})}}B=U+1}j=te+1}let M;try{M=ot(b,E)}catch{throw new Error("the proportional-hazards information is singular")}let R=Er(a);for(let j=0;j<a;j++)for(let te=0;te<a;te++){let B=0;for(let Z=0;Z<a;Z++)B+=E[Z][j]*M[Z][te];R[j][te]=v[j][te]-B}for(let j=0;j<a;j++)for(let te=0;te<j;te++){let B=(R[j][te]+R[te][j])/2;R[j][te]=B,R[te][j]=B}let x=t.terms===!1||t.terms===void 0?i.map(j=>[j]):t.terms,A=new Set,S=[],I=[];if(x.forEach((j,te)=>{if(!Array.isArray(j)||j.length===0)throw new RangeError("each terms group must be a non-empty array");let B=[];for(let Z of j){if(!Number.isInteger(Z)||Z<0||Z>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(A.has(Z))throw new RangeError("terms groups must not overlap");A.add(Z),i.includes(Z)&&B.push(Z)}B.length>0&&(S.push(B),I.push(te))}),i.forEach(j=>{A.has(j)||(S.push([j]),I.push(x.length))}),S.length===0)throw new Error("the proportional-hazards test has no estimable terms");let C=[];S.forEach((j,te)=>{if(o&&j.length>1)C.push({col:-1,group:te,collapsed:!0});else for(let B of j)C.push({col:B,group:te,collapsed:!1})});let _=C.length,F=Array.from({length:a},()=>er(_)),O=[],D=[];C.forEach((j,te)=>{let B=S[j.group],Z=I[j.group];if(j.collapsed){for(let U of B){let ee=i.indexOf(U);F[ee][te]=N[ee]}O.push(1),D.push(`g${Z+1}`)}else{let U=i.indexOf(j.col);F[U][te]=s.inverseCovariateScale[j.col],O.push(e.coefficients[j.col]),D.push(B.length>1?`g${Z+1}x${j.col+1}`:`x${j.col+1}`)}});let k=Array.from({length:_},(j,te)=>{let B=0;for(let Z=0;Z<a;Z++)B=Math.max(B,Math.abs(F[Z][te]));if(!(B>0&&Number.isFinite(B)))throw new Error("the proportional-hazards residual variance is singular");return Math.floor(Math.log2(B))}),L=F.map(j=>j.map((te,B)=>W(te,-k[B]))),q=Array.from({length:a},()=>er(_));for(let j=0;j<a;j++)for(let te=0;te<_;te++)for(let B=0;B<a;B++)q[j][te]+=b[j][B]*L[B][te];let $=Er(_);for(let j=0;j<_;j++)for(let te=0;te<_;te++)for(let B=0;B<a;B++)$[j][te]+=L[B][j]*q[B][te];for(let j=0;j<_;j++)for(let te=0;te<j;te++){let B=($[j][te]+$[te][j])/2;$[j][te]=B,$[te][j]=B}let V;try{V=ot($)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let j=0;j<_;j++)for(let te=0;te<j;te++){let B=(V[j][te]+V[te][j])/2;V[j][te]=B,V[te][j]=B}let P=V.map((j,te)=>j.map((B,Z)=>c.length*tt(B,-k[te]-k[Z]-s.weightExp))),Q=T.map(j=>{let te=er(_);for(let B=0;B<_;B++)for(let Z=0;Z<a;Z++)te[B]+=L[Z][B]*j.residual[Z];return O.map((B,Z)=>{let U=0;for(let ee=0;ee<_;ee++)U+=te[ee]*V[ee][Z];return B+c.length*tt(U,-k[Z]-s.weightExp)})}),K=[],X=[],Y=[],se=C.map(j=>j.collapsed?S[j.group].map(te=>i.indexOf(te)):[i.indexOf(j.col)]);for(let j of se){let te=j.map(ee=>j.map(ae=>R[ee][ae])),B=j.map(ee=>y[ee]),Z;try{Z=ot(te,B)}catch{throw new Error("the proportional-hazards term information is singular")}let U=W(Math.max(0,B.reduce((ee,ae,ve)=>ee+ae*Z[ve],0)),s.weightExp);K.push(U),X.push(j.length),Y.push(Ze(U,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 te=W(Math.max(0,y.reduce((B,Z,U)=>B+Z*j[U],0)),s.weightExp);K.push(te),X.push(a),Y.push(Ze(te,a,{lowerTail:!1})),D.push("GLOBAL")}let H={rowNames:D,chisq:K,df:X,p:Y,pValue:Y,x:T.map(j=>j.g),time:T.map(j=>j.time),...s.strata===void 0?{}:{strata:T.map(j=>xt(s.strata[j.input]))},y:Q,var:P,transform:n,warnings:f};return J("coxZph",H,e)}var Mb=["center"],yb=2**-1022;function Eb(e,t,n={}){z(n,Mb,"leveneTest");let r=Te(n.center??"median",["median","mean"]),{groups:o}=hr(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"?Bs(l):ct(l);return l.map(m=>Math.abs(m-f))},i=Re(o).groups.map(s),a=(l,f)=>l.some(m=>m>0&&m<yb)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(a)){let l=o.map(u=>{let h=Re([u]);return{d:s(h.groups[0]),k:h.k}}),f=Math.max(...l.map(u=>u.d.some(h=>h>0)?Math.floor(Math.log2(Math.max(...u.d)))+u.k:-1/0)),m=Number.isFinite(f)?f:0;i=l.map(u=>u.d.map(h=>W(h,u.k-m)))}let c=pu(Ir(i));return{colNames:["Df","F value","Pr(>F)"],rowNames:["group",""],table:[[c.df[0],c.fValue,c.pValue],[c.df[1],NaN,NaN]],warnings:[]}}var Tb=["score"],Rb=["alternative","exact","variant","levels"];function xb(e,t,...n){z(Ys(n),Tb,"propTrendTest");let{score:r}=Ks(["score"],n),o=Array.from(e),s=Array.from(t),i=o.length,a=r===void 0?o.map((N,b)=>b+1):Array.from(r);if(s.length!==i||a.length!==i)throw new RangeError("'x', 'n' and 'score' must have the same length");if(s.some(N=>!(N>0&&N<1/0)))throw new RangeError("elements of 'n' must be positive and finite");if(o.some((N,b)=>!(N>=0&&N<=s[b])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(N=>N===0)||o.every((N,b)=>N===s[b]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let c=s.reduce((N,b)=>N+b,0),l=o.reduce((N,b)=>N+b,0)/c,f=s.map(N=>N/(l*(1-l))),m=o.map((N,b)=>N/s[b]),u=a.flatMap((N,b)=>Number.isNaN(N)?[]:[b]);if(u.length===0)throw new RangeError("no non-missing scores");if(u.some(N=>!Number.isFinite(a[N])))throw new RangeError("non-missing scores must be finite");if(u.every(N=>a[N]===a[u[0]]))throw new RangeError("at least 2 distinct non-missing scores are needed");let h=a,{sw:p,sxy:d,sxx:g}=qc(f,m,h,u);if(!(g>=2**-1022&&g<1/0&&Math.abs(d)<1/0)){let N=Re([u.map(b=>a[b])]);N.k!==0&&(h=a.slice(),u.forEach((b,E)=>h[b]=N.groups[0][E]),{sw:p,sxy:d,sxx:g}=qc(f,m,h,u))}let w=Dt(d,g);if(!(c<1/0)||l>0&&l<1&&!(p<1/0&&Math.abs(d)<1/0&&g<1/0)){let{groups:[N,b],k:E}=Re([o,s]),v=N.reduce((T,M)=>T+M,0)/b.reduce((T,M)=>T+M,0),y=qc(b,m,h,u);w=W(y.sxy/v*(y.sxy/y.sxx/(1-v)),E)}return{method:"Chi-squared Test for Trend in Proportions",statistic:w,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:vt(w,.5,2,!1,!1),warnings:[]}}function qc(e,t,n,r){let o=r.reduce((l,f)=>l+e[f],0),s=r.reduce((l,f)=>l+e[f]*n[f],0)/o,i=r.reduce((l,f)=>l+e[f]*t[f],0)/o,a=0,c=0;for(let l of r){let f=n[l]-s;a+=e[l]*f*(t[l]-i),c+=e[l]*f*f}return{sw:o,sxy:a,sxx:c}}var Sb=2**24,Ab=2**32,kb=`Sample size > 100 or data with ties
|
|
10
|
+
p-value based on normal approximation. Specify nperm for permutation p-value`,qb=`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)),i=o.flatMap((f,m)=>s(f)||s(t[m])?[]:[m]);n===void 0&&(t=Pt(t));let a;if(n!==void 0)a=n.map(xt);else{let f=[...new Set(i.map(m=>t[m]))];a=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(xt):[...new Set(f.map(xt))].sort(Ln)}let c=a.map(()=>[]),l=new Map(a.map((f,m)=>[f,m]));for(let f of i){let m=l.get(xt(t[f]));if(m===void 0)throw new RangeError(`the group '${xt(t[f])}' is not among 'levels'`);c[m].push(o[f])}return c}function _b(e,t){let n=new Map;for(let l of t)n.set(l,(n.get(l)??0)+1);let r=[...n.values()],o=t.length,s=(l,f)=>l.reduce((m,u)=>m+f(u),0),i=(o*(o-1)*(2*o+5)-s(e,l=>l*(l-1)*(2*l+5))-s(r,l=>l*(l-1)*(2*l+5)))/72,a=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)),c=s(e,l=>l*(l-1))*s(r,l=>l*(l-1))/(8*o*(o-1));return i+a+c}function Cb(e,t,n={}){z(n,Rb,"jonckheereTest");let r=Te(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 i=s.flat(),a=i.length,c=new Set(i).size<a,l=0,f=[],m=a;s.forEach((N,b)=>{m-=N.length,f.push([N.length,m]);for(let E=b+1;E<s.length;E++)for(let v of N)for(let y of s[E])l+=v<y?1:v===y?.5:0});let u=[],h=!c&&a<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&c&&(h=!1,u.push(qb))):h||u.push(kb);let p,d;if(h){let N=1,b=0;for(let[v,y]of f)v===0||y===0||(b+=N*(v*y+1),N+=v*y);if(N>Sb||b>Ab)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[v,y]of f){if(v===0||y===0)continue;let T=Array.from({length:v*y+1},(R,x)=>_l(x,v,y,!1)),M=new Array(E.length+T.length-1).fill(0);E.forEach((R,x)=>T.forEach((A,S)=>M[x+S]+=R*A)),E=M}p=0,d=0,E.forEach((v,y)=>{y<=l&&(p+=v),y>=l&&(d+=v)})}else{let N=f.reduce((v,[y,T])=>v+y*T,0)/2,b=c?_b(s.map(v=>v.length),i):f.reduce((v,[y,T])=>v+y*T*(y+T+1)/12,0),E=(l-N)/Math.sqrt(b);p=oe(E),d=1-p,d<1e-8&&(d=oe(E,{lowerTail:!1}))}let w=r==="increasing"?d:r==="decreasing"?p:Math.min(1,2*Math.min(d,p,o==="clinfun"?.5:1));return J("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:w,warnings:u})}var Lb=["nClasses","adjust"];function gs(e){return Ft(Array.from(e)).sort((t,n)=>t-n)}function ah(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function Ra(e){ah(e);let t=qs(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function Ob(e){let t=gs(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Ra(t),o=0;for(let c=1;c<=n;c++)o+=(2*c-1)*(oe(r[c-1],{logP:!0})+oe(r[n-c],{lowerTail:!1,logP:!0}));let s=-n-o/n,i=(1+.75/n+2.25/n**2)*s,a;return i<.2?a=1-Math.exp(-13.436+101.14*i-223.73*i**2):i<.34?a=1-Math.exp(-8.318+42.796*i-59.938*i**2):i<.6?a=Math.exp(.9177-4.279*i-1.38*i**2):i<10?a=Math.exp(1.2937-5.709*i+.0186*i**2):a=37e-25,{method:"Anderson-Darling normality test",statistic:s,statisticName:"A",pValue:a,warnings:[]}}function Fb(e){let t=gs(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Ra(t),o=1/(12*n);r.forEach((c,l)=>{o+=(oe(c)-(2*(l+1)-1)/(2*n))**2});let s=(1+.5/n)*o,i=[],a;return s<.0275?a=1-Math.exp(-13.953+775.5*s-12542.61*s**2):s<.051?a=1-Math.exp(-5.903+179.546*s-1515.29*s**2):s<.092?a=Math.exp(.886-31.62*s+10.897*s**2):s<1.1?a=Math.exp(1.111-34.242*s+12.832*s**2):(a=737e-12,i.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),J("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:a,warnings:i})}function Pb(e){let t=gs(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=Ra(t).map(f=>oe(f)),o=-1/0,s=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),s=Math.max(s,f-m/n)});let i=Math.max(o,s),a=n<=100?i:i*(n/100)**.49,c=n<=100?n:100,l=Math.exp(-7.01256*a**2*(c+2.78019)+2.99587*a*Math.sqrt(c+2.78019)-.122119+.974598/Math.sqrt(c)+1.67997/c);if(l>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*i;f<=.302?l=1:f<=.5?l=2.76773-19.828315*f+80.709644*f**2-138.55152*f**3+81.218052*f**4:f<=.9?l=-4.901232+40.662806*f-97.490286*f**2+94.029866*f**3-32.355711*f**4:f<=1.31?l=6.198765-19.558097*f+23.186922*f**2-12.234627*f**3+2.423045*f**4:l=0}return{method:"Lilliefors (Kolmogorov-Smirnov) normality test",statistic:i,statisticName:"D",pValue:l,warnings:[]}}function Db(e){let t=gs(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(ah(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>re((u+1-3/8)/(n+1/4))),o=Re([t]).groups[0],s=Math.min(Al(o,r)**2/(st(o)*st(r)),1),i=Math.log(n),a=Math.log(i),c=-1.2725+1.0521*(a-i),l=1.0308-.26758*(a+2/i),f=oe((Math.log(1-s)-c)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function Vb(e,t={}){z(t,Lb,"pearsonTest");let n=gs(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),s=t.adjust??!0;if(!(Number.isInteger(o)&&o-1-(s?2:0)>0))throw new RangeError("'nClasses' must be an integer leaving positive degrees of freedom");if(o>2147483647)throw new RangeError("attempt to make a table with >= 2^31 elements");let i=[];for(let f of Ra(n)){let m=Math.min(o,Math.floor(1+o*oe(f)));m>=1&&i.push(m)}i.sort((f,m)=>f-m);let a=r/o,c=0;for(let f=1,m=0;f<=o;f++){let u=0;for(;m<i.length&&i[m]===f;)u++,m++;c=c+(u-a)**2/a}let l=o-1-(s?2:0);return{method:"Pearson chi-square normality test",statistic:c,statisticName:"P",pValue:vt(c,l/2,2,!1,!1),warnings:[]}}var $b=["weight"],zb=["exact"],Kb=["model","type","unit","r0","confLevel"];function ws(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 xa(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 ch=e=>2*oe(-Math.abs(e));function Yb(e,t={}){z(t,$b,"kappa2");let n=Te(t.weight,["unweighted","equal","squared"]);if(e.some(T=>T.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(T=>!T.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(T=>Hn(T[0])),i=r.map(T=>Hn(T[1])),a=[...new Set([...s,...i])].sort((T,M)=>T<M?-1:T>M?1:0),c=a.length,l=new Map(a.map((T,M)=>[T,M])),f=Array.from({length:c},()=>new Array(c).fill(0));for(let T=0;T<o;T++)f[l.get(s[T])][l.get(i[T])]++;let m=T=>n==="unweighted"?T===0?1:0:n==="equal"?1-T/(c-1):1-T*T/((c-1)*(c-1)),u=Array.from({length:c},(T,M)=>Array.from({length:c},(R,x)=>m(Math.abs(M-x)))),h=f.map(T=>Ie(T)/o),p=Array.from({length:c},(T,M)=>Ie(f.map(R=>R[M]))/o),d=T=>{let M=[];for(let R=0;R<c;R++)for(let x=0;x<c;x++)M.push(T(R,x));return Ie(M)},g=d((T,M)=>f[T][M]*u[T][M])/o,w=d((T,M)=>h[T]*p[M]*u[T][M]),N=(g-w)/(1-w),b=Array.from({length:c},(T,M)=>Ie(p.map((R,x)=>R*u[M][x]))),E=Array.from({length:c},(T,M)=>Ie(h.map((R,x)=>R*u[x][M]))),v=(d((T,M)=>h[T]*p[M]*(u[T][M]-(b[T]+E[M]))**2)-w*w)/(o*(1-w)**2),y=N/Math.sqrt(v);return{value:N,statistic:y,pValue:ch(y),warnings:[]}}function Wb(e,t={}){z(t,zb,"kappamFleiss");let n=t.exact??!1,r=ws(e,"ratings");xa(e,"ratings");let o=e.length,s=r.length;if(o<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let i=e.flat(),a=Pt(i),c=[...new Set(a)].sort((v,y)=>v-y),l=new Map(c.map((v,y)=>[v,y])),f=new Map(i.map((v,y)=>[v,l.get(a[y])])),m=c.length,u=e.map(v=>{let y=new Array(m).fill(0);for(let T of v)y[f.get(T)]++;return y}),h=Ie(u.map(v=>(Ie(v.map(y=>y*y))-s)/(s*(s-1))))/o,p=Array.from({length:m},(v,y)=>Ie(u.map(T=>T[y]))/(o*s)),d=Ie(p.map(v=>v*v));if(n){let v=r.map(T=>{let M=new Array(m).fill(0);for(let R of T)M[f.get(R)]++;return M.map(R=>R/o)}),y=Array.from({length:m},(T,M)=>st(v.map(R=>R[M]))*(s-1)/s);d-=Ie(y)/(s-1)}let g=(h-d)/(1-d);if(n)return{value:g,warnings:[]};let w=Ie(p.map(v=>v*(1-v))),N=Ie(p.map(v=>v*(1-v)*(1-v-v))),b=2/(w*w*o*s*(s-1))*(w*w-N),E=g/Math.sqrt(b);return{value:g,statistic:E,pValue:ch(E),warnings:[]}}function jb(e,t={}){z(t,Kb,"icc");let n=Te(t.model,["oneway","twoway"]),r=Te(t.type,["consistency","agreement"]),o=Te(t.unit,["single","average"]),s=t.r0??0,i=t.confLevel??.95;Ct(i),ws(e,"ratings"),xa(e,"ratings");let a=Re(e).groups,c=ws(a,"ratings"),l=a.length,f=c.length;if(l<2||f<2)throw new RangeError("at least 2 subjects and 2 raters are needed");if(!(s>=-1&&s<1))throw new RangeError("'r0' must be in [-1, 1)");let m=1-i,u=st(a.map(L=>ct(L)))*f,h=st(c.map(L=>ct(L)))*l,p=ct(a.map(L=>st(L))),d=st(a.flat())*(l*f-1),g=d-u*(l-1)-h*(f-1),w=g>2**-20*d?g/((l-1)*(f-1)):Hb(a)/((l-1)*(f-1)),N=(L,q,$)=>Nt(L,q,$,!1,!1),b=(L,q)=>m/2<1e-8?pn(m/2,L,q,!1,!1):pn(1-m/2,L,q,!0,!1),E=l-1;if(n==="oneway"||r==="consistency"){let L=n==="oneway"?p:w,q=n==="oneway"?l*(f-1):(l-1)*(f-1),$=u/L,V=$/b(E,q),P=$*b(q,E),[Q,K,X]=o==="single"?[(u-L)/(u+(f-1)*L),$*(1-s)/(1+(f-1)*s),Y=>(Y-1)/(Y+f-1)]:[(u-L)/u,$*(1-s),Y=>1-1/Y];return{value:Q,Fvalue:K,fValue:K,df1:E,df2:q,pValue:N(K,E,q),lbound:X(V),ubound:X(P),warnings:[]}}let v=o==="single"?f:1,y=L=>[v*L/(l*(1-L)),1+v*L*(l-1)/(l*(1-L))],T=(L,q)=>kl([L*h,q*w],[f-1,(l-1)*(f-1)]),M=o==="single"?(u-w)/(u+(f-1)*w+f/l*(h-w)):(u-w)/(u+(h-w)/l),[R,x]=y(s),A=u/(R*h+x*w),S=T(R,x),[I,C]=[f*M/(l*(1-M)),1+f*M*(l-1)/(l*(1-M))],_=T(I,C),F=b(E,_),O=b(_,E),[D,k]=o==="single"?[l*(u-F*w)/(F*(f*h+(f*l-f-l)*w)+l*u),l*(O*u-w)/(f*h+(f*l-f-l)*w+l*O*u)]:[l*(u-F*w)/(F*(h-w)+l*u),l*(O*u-w)/(h-w+l*O*u)];return{value:M,Fvalue:A,fValue:A,df1:E,df2:S,pValue:N(A,E,S),lbound:D,ubound:k,warnings:[]}}function ih(e,t){let n=t.length,r=Ie(t.flatMap(s=>t.map(i=>e[s][i])));return(1-Ie(t.map(s=>e[s][s]))/r)*n/(n-1)}function Bb(e){let t=ws(e,"x").length;if(xa(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=Xs(Re(e).groups),r=Ho(e),o=Array.from({length:t},(a,c)=>c),s=Ie(r.flat()),i;try{let a=ot(r);i=o.map(c=>1-1/a[c][c])}catch(a){if(!r.flat().every(Number.isFinite))throw a;let c=Re(e).groups;i=o.map(l=>{let f=c.map(h=>h[l]),m=Ws(Yt(c.map(h=>[1,...h.filter((p,d)=>d!==l)])),f),u=ct(f);return 1-Ie(m.map(h=>h*h))/Ie(f.map(h=>(h-u)**2))})}return J("cronbachAlpha",{rawAlpha:ih(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-Ie(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(a=>ih(n,o.filter(c=>c!==a))),warnings:Ub(e,r)})}function Ub(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}=wn(r.map(a=>r.map(c=>t[a][c])),{symmetric:!0}),s=o.map(a=>a[0]),i=Ie(s)<0?-1:1;s.some(a=>i*a<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 $1(e,t,n){let r=Ko(e,"x").length;Zs(e,"x");for(let a of[t,n])if(!Number.isInteger(a)||a<0||a>=r)throw new RangeError(`column index ${a} out of range (0 to ${r-1})`);if(t===n)throw new RangeError("'i' and 'j' must be different columns");let o=Mo(e),s=[t,n],i=Array.from({length:r},(a,l)=>l).filter(a=>a!==t&&a!==n),c=s.map(a=>s.map(l=>o[a][l]));if(i.length>0){let a=i.map(m=>i.map(u=>o[m][u])),l=i.map(m=>s.map(u=>o[m][u])),f=rt(a,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=Ie(i.map((h,p)=>o[s[m]][h]*f[p][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function z1(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>ue(l,[f,0]),[0,0]),o=e.map(a=>ft(r(a),n)),s=e[0].map((a,l)=>ft(r(e.map(f=>f[l])),t)),i=ft(r(e.flat()),t*n),c=[0,0];return e.forEach((a,l)=>a.forEach((f,m)=>{let u=ue(Ce(Ce([f,0],o[l]),s[m]),i);c=ue(c,_e(u,u))})),c[0]+c[1]}var j1=["controls","cases"],wu=["levels","direction","naRm"],K1=["partialAuc","focus","correct"],W1=["bestMethod","bestWeights"],Eu=new WeakMap;function ea(e){return Eu.get(e)}var Y1=["auto","<",">"];function Ji(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:Pn(e);if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="string")return e;throw new TypeError("the response must hold numbers, strings or booleans")}function B1(e){let t=e.filter(s=>Ji(s)!==null),n=new Set(t.map(s=>typeof s));if(n.size>1)throw new TypeError("the response mixes values of different types");let r=[...n][0],o;return r==="number"?o=[...new Set(t)].sort((s,i)=>s-i).map(Pn):r==="boolean"?o=[...new Set(t)].sort((s,i)=>Number(s)-Number(i)).map(s=>s?"TRUE":"FALSE"):o=[...new Set(t)].sort((s,i)=>s<i?-1:s>i?1:0),[...new Set(o)]}function Yo(e){return Float64Array.from(e).sort()}function ec(e,t){let n=Yo(e),r=Yo(t),o=[],s=[],i=[],c=0,a=0;for(;c<n.length||a<r.length;){let l=a>=r.length||c<n.length&&n[c]<=r[a]?n[c]:r[a];for(;c<n.length&&n[c]===l;)c++;for(;a<r.length&&r[a]===l;)a++;o.push(l),s.push(c),i.push(a)}return{u:o,ctrlLE:s,caseLE:i}}function tc(e,t,n){let r=0;for(let o=0;o<e.u.length;o++){let s=o>0?e.ctrlLE[o-1]:0,i=e.caseLE[o]-(o>0?e.caseLE[o-1]:0),c=e.ctrlLE[o]-s,a=n==="<"?s:t-e.ctrlLE[o];r+=i*(2*a+c)}return r}function nc(e,t,n){let r=e.length,o=t.length;return tc(ec(e,t),r,n)/(2*o*r)}function rc(e,t,n){return tc(ec(e,t),e.length,n)}var bu=e=>e===0n?0:e.toString(2).length;function Zr(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(bu(r)-bu(t)),s=o>=0?r<<BigInt(o):r,i=o>=0?t:t<<BigInt(-o),c=s/i;c*i!==s&&(c|=1n);let a=et(Number(c),-o);return n?-a:a}function U1(e,t,n){let r=(e+t)/2;return Math.abs(r)<=1e100||(r=e/2+t/2),(r===e||r===t)&&(r=n==="<"?t:e),r}function G1(e,t,n){let r=ec(e,t),o=e.length,s=t.length,i=r.u.length,c=[],a=[],l=[],f=m=>{let u=m===0?-1/0:m===i?1/0:U1(r.u[m-1],r.u[m],n),h=m===0?0:r.caseLE[m-1],p=m===0?0:r.ctrlLE[m-1];c.push(u),a.push((n==="<"?s-h:h)/s),l.push((n==="<"?p:o-p)/o)};if(n==="<")for(let m=0;m<=i;m++)f(m);else for(let m=i;m>=0;m--)f(m);return{thresholds:c,sensitivities:a,specificities:l,auc:tc(r,o,n)/(2*s*o)}}function H1(e,t){let n=r=>{let o=Yo(r),s=o.length>>1;return o.length%2===1?o[s]:Hn(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function xu(e,t,n,r,o,s){if(e.length===0)throw new Error("No control observation.");if(t.length===0)throw new Error("No case observation.");if(e.some(l=>!Number.isFinite(l))||t.some(l=>!Number.isFinite(l)))throw new RangeError("infinite values in the predictor: cannot build a valid ROC curve");let i=n==="auto"?H1(e,t):n,a={...G1(e,t,i),direction:i,levels:r,controls:e,cases:t,warnings:s};return Eu.set(a,o),a}function Ru(e){if(e===void 0)return"auto";if(!Y1.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function Xr(e,t,n){let r=Ru(n.direction),o=new mt,s=e.map(Ji),i;if(n.levels===void 0){if(i=B1(e),i.length>2&&(o.add("'response' has more than two levels. Consider setting 'levels' explicitly or using 'multiclass.roc' instead"),i=i.slice(0,2)),i.length<2)throw new RangeError("'response' must have two levels")}else{if(!Array.isArray(n.levels)||n.levels.length!==2)throw new RangeError("'levels' argument must have length 2");i=n.levels.map(p=>{let d=Ji(p);if(d===null)throw new RangeError("'levels' must not be missing");return d})}if(t.some(p=>typeof p!="number"))throw new TypeError("Predictor must be numeric.");if(s.length!==t.length)throw new TypeError("Response and predictor must be vectors of the same length.");let c=s.map((p,d)=>p===null||Number.isNaN(t[d]));if(n.naRm===!1&&c.some(p=>p))throw new RangeError("missing values in the response or the predictor with naRm false");let a=[],l=[],f=[],m=[],u=[];s.forEach((p,d)=>{if(c[d])return;let g=t[d];if(p===i[0])a.push(g);else if(p===i[1])l.push(g);else return;f.push(g),m.push(p===i[1]),u.push(d)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return xu(a,l,r,[i[0],i[1]],h,o.list)}function X1(e,t){let n=jr(Xn(e),Object.keys(t));if(n.response!==0||n.variables.length!==2||n.terms.length!==1)throw new TypeError("the formula must be of the form response ~ predictor");let r=fs(t,n.variables),[o,s]=r.variables;if(s.kind!=="numeric")throw new TypeError("Predictor must be numeric.");return{response:o.kind==="numeric"?o.values:o.codes.map(c=>o.levels[c]),predictor:s.values}}function Q1(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function Z1(e,t,n={}){if(at(e)&&V(e,j1,"roc"),typeof e!="string"&&at(t)&&V(t,wu,"roc"),V(n,wu,"roc"),typeof e=="string"){let r=X1(e,t);return H("roc",Xr(r.response,r.predictor,n))}if(Q1(e)){let r=t??{};if(r.levels!==void 0)throw new RangeError("'levels' cannot be given with controls and cases");let o=e;if(o.controls===void 0||o.cases===void 0)throw new TypeError("'controls' and 'cases' must both be given");let s=Ru(r.direction),i=Array.from(o.controls),c=Array.from(o.cases);if([...i,...c].some(u=>typeof u!="number"))throw new TypeError("Predictor must be numeric.");if(r.naRm===!1&&[...i,...c].some(Number.isNaN))throw new RangeError("missing values in the controls or the cases with naRm false");let a=i.filter(u=>!Number.isNaN(u)),l=c.filter(u=>!Number.isNaN(u)),f=[...a,...l],m={labels:null,predictor:f,values:f,isCase:f.map((u,h)=>h>=a.length),kept:f.map((u,h)=>h)};return H("roc",xu(a,l,s,["0","1"],m,[]))}return H("roc",Xr(Array.from(e),Array.from(t),n))}var Js=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function J1(e,t,n,r){let o=[];if(e.forEach((a,l)=>{a>=n&&a<=r&&o.push(l)}),o.length===0){let a=e.findIndex(u=>u>=r),l=a-1,f=Js(e,t,r,a,l),m=Js(e,t,n,a,l);return(f+m)/2*(r-n)}let s=0;for(let a=1;a<o.length;a++){let l=o[a-1],f=o[a];s+=(e[f]-e[l])*(t[l]+t[f])/2}let i=o[o.length-1];if(e[i]!==r){let a=e.findIndex(m=>m>=r),l=a-1,f=Js(e,t,r,a,l);s+=(r-e[l])*(t[l]+f)/2}let c=o[0];if(e[c]!==n){let a=e.findIndex(m=>m>n)-1,l=a+1,f=Js(e,t,n,l,a);s+=(e[l]-n)*(t[l]+f)/2}return s}var fr=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],Ir=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],An=(e,t)=>[e[0]*t[0],e[1]*t[1]],vu=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],Wo=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function Nu(e,t){if(!(t>=1))return;let n=e.map(r=>Math.round(r*t));return n.every((r,o)=>r/t===e[o])?n:void 0}function yu(e,t,n,r,o,s){let i=Nu(e,n),c=Nu(t,r);if(i!==void 0&&c!==void 0)return Mu(i.map(BigInt),c.map(BigInt),BigInt(n),BigInt(r),o,s);if(!(e.length>=2&&e.length===t.length&&e[0]===0&&e[e.length-1]===1&&e.every((h,p)=>h>=0&&h<=1&&(p===0||h>=e[p-1]))&&t.every(h=>h>=0&&h<=1)))throw new RangeError("the curve's sensitivities and specificities must be numbers in [0, 1] forming a ROC curve (use a curve from roc)");let l=e.map(Qr),f=t.map(Qr),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return Mu(l.map(u),f.map(u),m,m,o,s)}function Mu(e,t,n,r,o,s){let i=n*r,c=g=>[t[g]*n+e[g]*r-i,i],a=g=>[e[g],n],l=Qr(o),f=Qr(s),m=(g,w,N)=>fr(c(w),An(vu(Ir(g,a(w)),Ir(a(N),a(w))),Ir(c(N),c(w)))),u=[];e.forEach((g,w)=>{Wo(a(w),l)>=0&&Wo(a(w),f)<=0&&u.push(w)});let h;if(u.length===0){let g=e.findIndex((N,v)=>Wo(a(v),f)>=0),w=g-1;h=An(fr(m(f,w,g),m(l,w,g)),An(Ir(f,l),[1n,2n]))}else{let g=0n;for(let v=1;v<u.length;v++){let E=u[v-1],b=u[v];g+=(e[b]-e[E])*(c(E)[0]+c(b)[0])}h=[g,2n*n*i];let w=u[u.length-1];if(Wo(a(w),f)!==0){let v=w+1;h=fr(h,An(Ir(f,a(w)),An(fr(c(w),m(f,w,v)),[1n,2n])))}let N=u[0];if(Wo(a(N),l)!==0){let v=N-1;h=fr(h,An(Ir(a(N),l),An(fr(c(N),m(l,N,v)),[1n,2n])))}}if(h[0]<0n)throw new Error("Partial AUC correction not defined for ROC curves below the diagonal.");let p=An(An(Ir(f,l),fr(f,l)),[1n,2n]),d=vu(fr(p,h),An(p,[2n,1n]));return Zr(d[0],d[1])}function eb(e,t={}){V(t,K1,"auc");let n=xe(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return H("auc",{auc:e.auc,partialAuc:null,focus:n,correct:r,warnings:[]});let o=t.partialAuc;if(!Array.isArray(o)||o.length!==2||o.some(p=>typeof p!="number"))throw new TypeError("partialAuc must be a numeric vector of length 2");let s=Math.max(o[0],o[1]),i=Math.min(o[0],o[1]);if(!(i>=0&&s<=1))throw new RangeError("the limits of partialAuc must be within [0, 1]");if(!(i<s))throw new RangeError("the limits of partialAuc must differ");let c=e.sensitivities,a=e.specificities,[l,f]=n==="specificity"?[a,c]:[[...c].reverse(),[...a].reverse()],m,u=e.controls.length,h=e.cases.length;return r?m=n==="specificity"?yu(l,f,u,h,i,s):yu(l,f,h,u,i,s):m=J1(l,f,i,s),H("auc",{auc:m,partialAuc:[s,i],focus:n,correct:r,warnings:[]})}function Qr(e){if(e===0)return[0n,1n];let t=0,n=e;for(;!Number.isInteger(n);)n*=2,t++;return[BigInt(n),1n<<BigInt(t)]}function tb(e,t){let n=Yo(e.controls),r=Yo(e.cases),o=(i,c,a)=>{let l=0,f=i.length;for(;l<f;){let m=l+f>>1;(a?i[m]<=c:i[m]<c)?l=m+1:f=m}return l},s=e.direction==="<";return{tp:t.map(i=>s?r.length-o(r,i,!1):o(r,i,!0)),tn:t.map(i=>s?o(n,i,!1):n.length-o(n,i,!0))}}function nb(e,t="all",n={}){V(n,W1,"coords");let r=xe(n.bestMethod,["youden","closest.topleft","topleft"]),o=n.bestWeights??[1,.5];if(!Array.isArray(o)||o.length!==2||o.some(g=>typeof g!="number"))throw new TypeError("'best.weights' must be a numeric vector of length 2");let[s,i]=o;if(!(i>0&&i<1))throw new RangeError("prevalence ('best.weights[2]') must be in the interval ]0,1[.");if(!(s>0&&s<1/0))throw new RangeError("the cost ('bestWeights[0]') must be a positive finite number");let c=(1-i)/(s*i),a,l=null;if(typeof t=="string"&&(l=xe(t,["all","best"])),l!==null)a=[...e.thresholds];else{if(a=typeof t=="number"?[t]:Array.from(t),a.length===0)throw new RangeError("Numeric 'x' has length 0");if(a.some(g=>typeof g!="number"||Number.isNaN(g)))throw new RangeError("the thresholds must be numbers (not NaN)")}let f=e.cases.length,m=e.controls.length,{tp:u,tn:h}=tb(e,a);if(l==="best"){let[g,w]=Qr(s),[N,v]=Qr(i),E=(v-N)*w,b=g*N,M=BigInt(f),x=BigInt(m),y=u.map((S,k)=>{if(r==="youden")return BigInt(S)*x*b+E*BigInt(h[k])*M;let L=BigInt(f-S),_=BigInt(m-h[k]);return-(L*L*x*x*b+E*_*_*M*M)}),R=y.reduce((S,k)=>k>S?k:S),T=y.map((S,k)=>S===R?k:-1).filter(S=>S>=0);a=T.map(S=>a[S]),u=T.map(S=>u[S]),h=T.map(S=>h[S])}let p=u.map(g=>f-g),d=h.map(g=>m-g);return H("coords",{threshold:a,sensitivity:u.map(g=>g/f),specificity:h.map(g=>g/m),accuracy:u.map((g,w)=>(g+h[w])/(f+m)),tn:h,tp:u,fn:p,fp:d,npv:h.map((g,w)=>g/(g+p[w])),ppv:u.map((g,w)=>g/(g+d[w])),fpr:d.map(g=>g/m),lrPos:u.map((g,w)=>g*m/(f*d[w])),lrNeg:p.map((g,w)=>g*m/(f*h[w])),youden:u.map((g,w)=>g/f+c*(h[w]/m)),closestTopleft:p.map((g,w)=>(g/f)**2+c*(d[w]/m)**2),warnings:[]})}var rb=["confLevel","method","bootN","bootStratified"],Tu=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function ra(e){let t=Float64Array.from(e.controls).sort(),n=Float64Array.from(e.cases).sort(),r=t.length,o=n.length,s=(l,f)=>{let m=0,u=l.length;for(;m<u;){let p=m+u>>1;l[p]<f?m=p+1:u=p}let h=m;for(;h<l.length&&l[h]===f;)h++;return[m,h-m]},i=e.direction==="<",c=e.cases.map(l=>{let[f,m]=s(t,l);return 2*(i?f:r-f-m)+m}),a=e.controls.map(l=>{let[f,m]=s(n,l);return 2*(i?o-f-m:f)+m});return{k10:c,k01:a}}function ic(e,t,n,r){return ot(e)/(4*n*n)/r+ot(t)/(4*r*r)/n}function Su(e){let{k10:t,k01:n}=ra(e);return ic(t,n,e.controls.length,e.cases.length)}function ta(e,t,n,r,o,s){let i=BigInt(rc(e,t,n)),c=BigInt(rc(r,o,s)),a=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return Zr(i*l-c*a,2n*a*l)}var sc=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function eo(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function ku(e){let t=(1-e)/2;return t<1e-8?oe(t,{lowerTail:!1}):oe(1-t)}function qu(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function to(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[wo(e.length)];return t}function ac(e){let t=ea(e);if(t!==void 0)return{values:t.values,isCase:t.isCase};let n=[...e.controls,...e.cases];return{values:n,isCase:n.map((r,o)=>o>=e.controls.length)}}function Bo(e,t,n){let r=[],o=[];for(let s of n)(t[s]?o:r).push(e[s]);if(!(r.length===0||o.length===0))return{controls:r,cases:o}}function ob(e,t,n,r){let o=Bo(e,t,n);return o===void 0?NaN:nc(o.controls,o.cases,r)}function Jr(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=wo(e);return t}function sb(e,t={}){V(t,rb,"ciAuc");let n=t.confLevel??.95;an(n);let r=xe(t.method,["delong","bootstrap"]),o=new mt;sc(e)&&o.add("ci.auc() of a ROC curve with AUC == 1 is always 1-1 and can be misleading.");let s=1-n;if(r==="delong"){eo(e,"DeLong's interval");let{k10:m,k01:u}=ra(e),h=e.auc,p=ic(m,u,e.controls.length,e.cases.length),d=Math.sqrt(p),g=v=>Math.min(1,Math.max(0,v)),w=s/2,N=n===1?[0,1]:d===0?[h,h]:[g(h+oe(w)*d),g(h+ku(n)*d)];return H("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:p,warnings:o.list})}let i=t.bootN??2e3;qu(i);let c=t.bootStratified??!0,a=[],l=0;if(c){eo(e,"a stratified bootstrap");for(let m=0;m<i;m++){let u=to(e.controls),h=to(e.cases);a.push(nc(u,h,e.direction))}}else{let{values:m,isCase:u}=ac(e);for(let h=0;h<i;h++){let p=ob(m,u,Jr(m.length),e.direction);Number.isNaN(p)?l++:a.push(p)}}if(l>0&&o.add("NA value(s) produced during bootstrap were ignored."),a.length===0)throw new Error("no bootstrap replicate had both controls and cases");let f=Es(a,[s/2,1-s/2]);return H("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:a.length>1?ot(a):NaN,warnings:o.list})}var Au=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,na=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function ab(e,t){let n=ea(e),r=ea(t);if(n?.labels==null||r?.labels==null||!na(e.levels,t.levels))return;let o=c=>c.kept.map(a=>c.labels[a]);if(na(o(n),o(r))&&na(n.isCase,r.isCase))return[e,t];if(!na(n.labels,r.labels))return;let s=n.labels.map((c,a)=>a).filter(c=>n.labels[c]!==null&&!Number.isNaN(n.predictor[c])&&!Number.isNaN(r.predictor[c])),i=(c,a)=>Xr(s.map(l=>a.labels[l]),s.map(l=>a.predictor[l]),{levels:c.levels,direction:c.direction});return[i(e,n),i(t,r)]}function oc(e,t){return t==="two.sided"?2*ie(-Math.abs(e)):t==="greater"?ie(e,{lowerTail:!1}):ie(e)}function ib(e,t,n,r){at(n)&&V(n,Tu,"rocTest"),V(r,Tu,"rocTest");let o,s=new mt,i,c;if(Au(e)){if(!Au(t))throw new TypeError("roc2 must be a curve from roc()");if(o=n??{},o.levels!==void 0||o.direction!==void 0)throw new RangeError("'levels' and 'direction' apply only to a response and two predictors");i=e,c=t}else{o=r??{};let b=Array.from(e),M=Array.from(t),x=Array.from(n);if(b.length!==M.length||b.length!==x.length)throw new TypeError("the response and the predictors must have the same length");let y=b.map((S,k)=>k).filter(S=>{let k=b[S];return k!=null&&!(typeof k=="number"&&Number.isNaN(k))&&!Number.isNaN(M[S])&&!Number.isNaN(x[S])}),R=y.map(S=>b[S]),T={levels:o.levels,direction:o.direction};i=Xr(R,y.map(S=>M[S]),T),c=Xr(R,y.map(S=>x[S]),T);for(let S of i.warnings)s.add(S)}let a=Ge(o.alternative),l=xe(o.method,["delong","bootstrap"]),f=o.confLevel??.95;an(f),sc(i)&&sc(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=ab(i,c),u;if(o.paired===!0){if(m===void 0)throw new RangeError("The paired ROC test cannot be applied to unpaired curves.");u=!0}else o.paired===!1?(m!==void 0&&s.add("The ROC curves seem to be paired. Consider performing a paired roc.test."),u=!1):u=m!==void 0;u&&([i,c]=m);let h={alternative:a,estimate:[i.auc,c.auc],estimateNames:["AUC of roc1","AUC of roc2"],nullValue:0,nullValueName:"difference in AUC",roc1:i,roc2:c},p=ta(i.controls,i.cases,i.direction,c.controls,c.cases,c.direction);if(l==="delong"){if(i.direction!==c.direction&&s.add("DeLong's test should not be applied to ROC curves with a different direction."),eo(i,"DeLong's test"),eo(c,"DeLong's test"),u){let L=ra(i),_=ra(c),I=L.k10.map((O,q)=>O-_.k10[q]),F=L.k01.map((O,q)=>O-_.k01[q]),C=Math.sqrt(ic(I,F,i.controls.length,i.cases.length)),D=p===0&&C===0?0:p/C,A=C===0?0:ku(f)*C;return H("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:D,statisticName:"Z",pValue:oc(D,a),confInt:[p-A,p+A],confLevel:f,...h,warnings:s.list})}let b=Su(i),M=Su(c),x=i.controls.length+i.cases.length,y=c.controls.length+c.cases.length,R=b+M,T,S,k;return R===0?(T=p===0?0:p>0?1/0:-1/0,S=NaN,k=oc(T,a)):(T=p/Math.sqrt(R),S=R*R/(b*b/(x-1)+M*M/(y-1)),k=a==="two.sided"?2*vt(-Math.abs(T),S,!0,!1):a==="greater"?vt(T,S,!1,!1):vt(T,S,!0,!1)),H("rocTest",{method:"DeLong's test for two ROC curves",statistic:T,statisticName:"D",parameter:S,parameterName:"df",pValue:k,...h,warnings:s.list})}let d=o.bootN??2e3;qu(d);let g=o.bootStratified??!0,w=[],N=0;if(g){eo(i,"a stratified bootstrap"),eo(c,"a stratified bootstrap");for(let b=0;b<d;b++)if(u){let M=Jr(i.controls.length),x=Jr(i.cases.length);w.push(ta(M.map(y=>i.controls[y]),x.map(y=>i.cases[y]),i.direction,M.map(y=>c.controls[y]),x.map(y=>c.cases[y]),c.direction))}else{let M=to(i.controls),x=to(i.cases),y=to(c.controls),R=to(c.cases);w.push(ta(M,x,i.direction,y,R,c.direction))}}else{let b=ac(i),M=ac(c);for(let x=0;x<d;x++){let y,R;if(u){let T=Jr(b.values.length);y=Bo(b.values,b.isCase,T),R=Bo(M.values,M.isCase,T)}else y=Bo(b.values,b.isCase,Jr(b.values.length)),R=Bo(M.values,M.isCase,Jr(M.values.length));y===void 0||R===void 0?N++:w.push(ta(y.controls,y.cases,i.direction,R.controls,R.cases,c.direction))}}if(N>0&&s.add(`${N} NA value(s) produced during bootstrap were ignored.`),w.length<2)throw new Error("fewer than 2 bootstrap replicates had both controls and cases");let v=Math.sqrt(ot(w)),E=p===0&&v===0?0:p/v;return H("rocTest",{method:u?"Bootstrap test for two correlated ROC curves":"Bootstrap test for two ROC curves",statistic:E,statisticName:"D",parameter:d,parameterName:"boot.n",pValue:oc(E,a),bootStratified:g,...h,warnings:s.list})}var cb=["method","confLevel","prevalence"];function lb(e,t={}){V(t,cb,"diagnosticTests");let n=xe(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(It(r),!Array.isArray(e)||e.length!==2||e.some(b=>!(Array.isArray(b)||ArrayBuffer.isView(b))||b.length!==2))throw new TypeError("'table' must be a 2 x 2 table (an array of two rows of two counts)");let o=[e[0][0],e[0][1],e[1][0],e[1][1]];if(o.some(b=>typeof b!="number"))throw new TypeError("the counts must be numbers");if(!o.every(wn))throw new RangeError("the counts must be nonnegative integers");let[s,i,c,a]=o.map(Math.round),l=s+c,f=i+a,m=s+i,u=c+a,h=l+f;if(l===0||f===0||m===0||u===0)throw new RangeError("every row and column total of the table must be positive");let p=(b,M)=>{let x=je(()=>Xa(b,M,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:x.estimate,confInt:x.confInt}},d=1-r,g=d/2<1e-8?oe(d/2,{lowerTail:!1}):oe(1-d/2),w=(b,M)=>{let x=g*Math.sqrt(M);return{estimate:b,confInt:[Math.exp(Math.log(b)-x),Math.exp(Math.log(b)+x)]}},N=s/l,v=a/f,E={sensitivity:p(s,l),specificity:p(a,f),ppv:p(s,m),npv:p(a,u),lrPos:w(i===0?N/0:Zr(BigInt(s)*BigInt(f),BigInt(l)*BigInt(i)),c/(s*l)+a/(i*f)),lrNeg:w(a===0?c/l/v:Zr(BigInt(c)*BigInt(f),BigInt(l)*BigInt(a)),s/(c*l)+i/(a*f)),accuracy:p(s+a,h),diagnosticOR:w(s*a/(i*c),1/s+1/i+1/c+1/a),prevalence:p(l,h),apparentPrevalence:p(m,h),method:n,confLevel:r,warnings:[]};if(t.prevalence!==void 0){let b=t.prevalence;if(!(typeof b=="number"&&b>0&&b<1))throw new RangeError("'prevalence' must be a number in (0, 1)");let M=b<2**-500?2**600:1,x=N*(b*M),y=i/f*((1-b)*M),R=v*(1-b),T=c/l*b;E.ppvAtPrevalence=x/(x+y),E.npvAtPrevalence=R/(R+T)}return H("diagnosticTests",E)}var fb=["two","mode","confLevel","ciMethod"],mb=["package","exact"],kn=(e,t)=>t===0?0:e*t;function ub(e,t,n={}){if(V(n,fb,"blandAltman"),e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let r=n.two??1.96;if(!(typeof r=="number"&&r>0&&r<1/0))throw new RangeError("'two' must be a positive number");let o=n.mode??1;if(o!==1&&o!==2)throw new RangeError("'mode' must be 1 or 2");let s=n.confLevel??.95;an(s);let i=xe(n.ciMethod,mb),c=[],a=[];for(let C=0;C<e.length;C++){let D=e[C],A=t[C];if(typeof D!="number"||typeof A!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(D)||Number.isNaN(A))){if(!Number.isFinite(D)||!Number.isFinite(A))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(D),a.push(A)}}let l=c.length;if(l<2)throw new RangeError("fewer than 2 complete pairs");let[f,m]=o===1?[c,a]:[a,c],u=c.map((C,D)=>{let A=(C+a[D])/2;return Number.isFinite(A)?A:C/2+a[D]/2}),h=f.map((C,D)=>C-m[D]),p=!h.every(Number.isFinite),{groups:d,k:g}=Re([p?f.map((C,D)=>C/2-m[D]/2):h]),w=d[0],N=g+(p?1:0),v=st(w),E=Un(w),b=r*E,M=v-b,x=v+b,y=l-1,R=(1-s)/2,T=xt(R,y,!0,!1),S=R<1e-8?xt(R,y,!1,!1):xt((s+1)/2,y,!0,!1),k=[v+kn(T,E)/Math.sqrt(l),v+kn(S,E)/Math.sqrt(l)],L,_;if(i==="package"){let C=3*E*E/l,D=C>=2**-1022||C===0?Math.sqrt(C):E*Math.sqrt(3/l);L=[M+kn(T,D),M+kn(S,D)],_=[x+kn(T,D),x+kn(S,D)]}else{let C=r*Math.sqrt(l),D=da(R,y,C,!0,!1)/Math.sqrt(l),A=da(R,y,C,!1,!1)/Math.sqrt(l);L=[v-kn(A,E),v-kn(D,E)],_=[v+kn(D,E),v+kn(A,E)]}let I=C=>$(C,N),F=C=>[I(C[0]),I(C[1])];return H("blandAltman",{means:u,diffs:h,n:l,meanDiff:I(v),lowerLimit:I(M),upperLimit:I(x),criticalDiff:I(b),meanDiffConfInt:F(k),lowerLimitConfInt:F(L),upperLimitConfInt:F(_),confLevel:s,two:r,mode:o,ciMethod:i,warnings:[]})}var hb=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),pb=["PaBa","Deming","WDeming"],db=["analytical","jackknife","bootstrap","nestedbootstrap"],gb=["quantile","Student","BCa","tBoot"],Iu=5793,wb=2**27;function fc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function bb(e,t){let n=Bn(e),r=Bn(t),o=[0,0],s=[0,0],i=[0,0];for(let c=0;c<e.length;c++){let a=Ce([e[c],0],n),l=Ce([t[c],0],r);o=ue(o,_e(a,a)),s=ue(s,_e(l,l)),i=ue(i,_e(a,l))}return{u:o,q:s,p:i,mx:n,my:r}}function Ou(e,t,n,r){if(n===0)return NaN;if(r<=1){let i=r*t-e,c=Math.sqrt(i*i+4*r*n*n);return i>=0?(i+c)/(2*r*n):2*n/(c-i)}let o=t-e/r,s=Math.sqrt(o*o+4*n*n/r);return o>=0?(o+s)/(2*n):2*n/r/(s-o)}function mc(e,t,n){let r=e.length,{u:o,q:s,p:i,mx:c,my:a}=bb(e,t),l=Ou(o[0],s[0],i[0],n),f=Ce(a,_e([l,0],c))[0],m=_e(o,s),u=i[0]*i[0]/(o[0]*s[0]),h=Ce(m,_e(i,i))[0]/(m[0]+m[1]);Math.abs(h)<1e-12*(1+u)/2&&(h=0);let p=Math.sqrt(l*l*h/u/(r-2)),d=[0,0];for(let w of e)d=ue(d,_e([w,0],[w,0]));let g=Math.sqrt(p*p*ft(d,r)[0]);return{b0:f,b1:l,se0:g,se1:p,xmean:c[0]}}function _u(e,t,n,r,o,s,i,c,a){let l=e.length,f=mc(e,t,n),m=f.b0,u=f.b1,h=NaN,p=new Array(l),d=s;for(let b=0;b<s;b++){let M=1+n*u*u,x=1/0;for(let A=0;A<l;A++){let O=t[A]-(m+u*e[A]),q=e[A]+n*u*O/M,P=t[A]-O/M;p[A]=r>1?q/r+P:q+r*P,x=Math.min(x,Math.abs(p[A]))}let y=[0,0],R=[0,0],T=[0,0];for(let A=0;A<l;A++){let O=x/p[A],q=[O*O,0];y=ue(y,q),R=ue(R,_e(q,[e[A],0])),T=ue(T,_e(q,[t[A],0]))}let S=ft(R,y[0]+y[1]),k=ft(T,y[0]+y[1]),L=[0,0],_=[0,0],I=[0,0];for(let A=0;A<l;A++){let O=ft(Ce([e[A],0],S),p[A]),q=ft(Ce([t[A],0],k),p[A]);L=ue(L,_e(O,O)),_=ue(_,_e(q,q)),I=ue(I,_e(O,q))}let F=Ou(L[0],_[0],I[0],n),C=Ce(k,_e([F,0],S))[0];h=S[0];let D=$(Math.abs(u-F),a-c)<i&&$(Math.abs(m-C),a)<i;if(m=C,u=F,D){d=b+1;break}}let[g,w]=cc(o),[N,v]=cc(1+o),E=p.map(b=>{if(b===0)return 1/0;let[M,x]=cc(Math.abs(b));return r>1?et((N/(g*M))**2,2*(v-w-x-a)):et((N/M)**2,2*(v-x-c))});return{b0:m,b1:u,xmean:h,weights:E,converged:d<s}}function cc(e){let t=Math.floor(Math.log2(e));return[et(e,-t),t]}var oo=e=>Math.atan(e),ro=e=>Number.isFinite(e)?e:Math.tan(oo(e));function vb(e,t){let n=e.length,r=0;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++)r+=Math.sign(e[u]-e[m])*Math.sign(t[u]-t[m]);let o=r>=0,s=new Float64Array(n*(n-1)/2),i=0,c=0,a=0,l=Math.PI/4;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++){let h=fc(e[u],e[m]),p=fc(t[u],t[m]),d;if(h!==0)d=p/h;else if(p!==0)d=o?1/0:-1/0;else continue;s[i++]=d;let g=oo(d);o?g===-l?c++:g<-l&&a++:g===l?c++:g>l&&a++}return{sorted:s.subarray(0,i).sort(),offset:o?2*a+c:-(2*a+c),e:c}}function Nb(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?ro(e[c-1]):NaN}let o=t/2;if(o<1||o+1>r)return NaN;let s=e[o-1],i=e[o];return n?(ro(s)+ro(i))/2:Math.tan((oo(s)+oo(i))/2)}function yb(e){let t=Float64Array.from(e).sort(),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function uc(e,t,n){return Number.isFinite(n)?yb(e.map((r,o)=>fc(t[o],n*r))):-n}function Lu(e,t,n){let r=vb(e,t),o=Nb(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:uc(e,t,o),b1:o,slopes:r}}function Mb(e,t,n,r,o){let s=e.length,{sorted:i,offset:c,e:a}=n,l=i.length,f=Math.round(r*Math.sqrt(s*(s-1)*(2*s+5)/18)),m=(p,d)=>{let g=Math.trunc((p+1)/2);if(d?g<2*a+1:g>l)return NaN;if(p%2===0){if(g+1>l)return NaN;let w=i[g-1],N=i[g];return o?(ro(w)+ro(N))/2:Math.tan((oo(w)+oo(N))/2)}return ro(i[g-1])},u=m(l-f+c,!0),h=m(l+f+c,!1);return Number.isNaN(u)||Number.isNaN(h)?{slope:[-1/0,1/0],intercept:[-1/0,1/0],defined:!1}:{slope:[u,h],intercept:[uc(e,t,h),uc(e,t,u)],defined:!0}}function Eb(e,t,n,r){let o=e.length,s=e.map((p,d)=>t[d]-n-r*p),i=s.filter(p=>p>0).length,c=s.filter(p=>p<0).length,a=Math.sqrt(c/i),l=-Math.sqrt(i/c),f=e.map((p,d)=>(t[d]+p/r-n)/Math.sqrt(1+1/(r*r))),m=Array.from({length:o},(p,d)=>d).sort((p,d)=>f[p]-f[d]||p-d),u=0,h=0;for(let p of m){let d=s[p];u+=d>0?a:d<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:i,nNeg:c,maxCusum:h}}var oa=e=>e.every(t=>t===e[0]);function lc(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var no=(e,t)=>Es(e,t),Cu=e=>Un(e.filter(t=>!Number.isNaN(t)));function xb(e,t,n={}){V(n,hb,"mcreg");let r=xe(n.methodReg,pb),o=xe(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),db),s=o==="bootstrap"||o==="nestedbootstrap",i=xe(n.methodBootstrapCi,gb),c=n.confLevel??.95;if(!(typeof c=="number"&&c>0&&c<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let l=xe(n.slopeMeasure,["radian","tangent"])==="tangent",f=r!=="PaBa",m=f?n.errorRatio??1:1;if(f&&!(typeof m=="number"&&m>0&&m<1/0))throw new RangeError("'errorRatio' must be a positive finite number");let u=lc(n.nsamples??999,"nsamples"),h=lc(n.nnested??25,"nnested"),p=lc(n.iterMax??30,"iterMax"),d=n.threshold??1e-6;if(!(typeof d=="number"&&d>=0&&d<1/0))throw new RangeError("'threshold' must be a nonnegative number");if(r==="WDeming"&&o==="analytical")throw new RangeError('there is no analytical interval for weighted Deming regression (use methodCi "jackknife" or a bootstrap)');if(o==="bootstrap"&&i==="tBoot"&&r!=="Deming")throw new RangeError(`the bootstrap-t interval of ${r} needs methodCi "nestedbootstrap" (there is no analytical standard error)`);if(e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let g=[],w=[];for(let X=0;X<e.length;X++){let K=e[X],Z=t[X];if(typeof K!="number"||typeof Z!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(K)||Number.isNaN(Z)){if(n.naRm===!0)continue;throw new RangeError("'x' or 'y' has missing values (use naRm: true to drop the incomplete pairs)")}if(!Number.isFinite(K)||!Number.isFinite(Z))throw new RangeError("'x' and 'y' must not contain infinite values");g.push(K),w.push(Z)}let N=g.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(oa(g)||oa(w))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(g.some(K=>K<0)||w.some(K=>K<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>Iu)throw new RangeError(`Passing-Bablok regression is limited to ${Iu} points (2^24 pairwise slopes)`);if((1+(o==="jackknife"||s&&i==="BCa"?N:0)+(s?u*(1+(o==="nestedbootstrap"?h:0)):0))*(N*(N-1)/2)>wb)throw new RangeError("the Passing-Bablok fits would take more than 2^27 pairwise slopes in all (reduce nsamples or nnested)")}if(r==="WDeming"&&(g.some(X=>X<=0)||w.some(X=>X<=0)))throw new RangeError("weighted Deming regression needs positive values");let v=Re([g,w]),[E,b]=f?[Re([g]).k,Re([w]).k]:[v.k,v.k],M=f?g.map(X=>$(X,-E)):v.groups[0],x=f?w.map(X=>$(X,-b)):v.groups[1],y=Math.min(Math.max(et(m,2*(b-E)),Number.MIN_VALUE),2**1e3),R=et(m,b-E),T=new Set,S=`no konvergence after ${p} iterations`,k=(X,K)=>{if(r==="Deming")return mc(X,K,y);if(r==="WDeming"){let Z=_u(X,K,y,R,m,p,d,E,b);return Z.converged||T.add(S),Z}return Lu(X,K,l)},L,_=NaN,I,F,C;if(r==="Deming"){let X=mc(M,x,y);L=X,_=$(X.xmean,E),F=[X.se0,X.se1]}else if(r==="WDeming"){let X=_u(M,x,y,R,m,p,d,E,b);X.converged||T.add(S),L=X,_=$(X.xmean,E),I=X.weights}else{let X=Lu(M,x,l);L=X,C=X.slopes}if(!Number.isFinite(L.b1))throw new RangeError(r==="PaBa"?"the Passing-Bablok slope is not defined for these data":"the covariance of x and y is 0: the Deming slope is not defined");let D=1-c,A=xt(D/2,N-2,!1,!1),O=oe(D/2,{lowerTail:!1}),q=[L.b0,L.b1],P=[NaN,NaN],B=[[NaN,NaN],[NaN,NaN]],J,ce,W,te=()=>{let X=[],K=[];for(let Z=0;Z<N;Z++){let z=k(M.filter((G,j)=>j!==Z),x.filter((G,j)=>j!==Z));X.push(z.b0),K.push(z.b1)}J=[X,K],ce=[(N-1)*Un(X)/Math.sqrt(N),(N-1)*Un(K)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let X of[0,1])P[X]=F[X],B[X]=[q[X]-A*P[X],q[X]+A*P[X]];else{let X=Mb(M,x,C,O,l);X.defined||T.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),B[0]=X.intercept,B[1]=X.slope}else if(o==="jackknife"){te();for(let X of[0,1])P[X]=ce[X],B[X]=[q[X]-A*P[X],q[X]+A*P[X]]}else{if(i==="BCa"&&(te(),J.some(he=>he.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let X=r==="Deming"?F:r==="WDeming"&&ce!==void 0?ce:[NaN,NaN],K=o==="nestedbootstrap",Z=[],z=[],G=[],j=[],Q=0,ne=()=>Array.from({length:N},()=>wo(N)),be=he=>{let Le=he.map(Me=>M[Me]),we=he.map(Me=>x[Me]);if(oa(Le)||oa(we)){if(r==="PaBa")return T.add("Resampling PaBa regression: x or y coordinates of all data points in subsample identical - regression coefficients undetermined!"),{b0:NaN,b1:NaN};T.add("Resampling Deming regression: all x coordinates in subsample identical - regression coefficients undetermined!")}return k(Le,we)},Ne=()=>{for(let he=0;he<u;he++){let Le=ne(),we=be(Le),Me;if(K){let Se=[],Ke=[];for(let Pe=0;Pe<h;Pe++){let Ze=be(ne().map(Be=>Le[Be]));Se.push(Ze.b0),Ke.push(Ze.b1)}Me=[Cu(Se),Cu(Ke)]}else r==="Deming"?Me=[we.se0,we.se1]:Me=X;if(Number.isNaN(we.b0)||Number.isNaN(we.b1)){Q++;continue}Z.push(we.b0),z.push(we.b1),G.push(Me[0]),j.push(Me[1])}};if(n.rngSeed!==void 0){let he=Bc()?Hc():void 0;try{Gc({kind:"Mersenne-Twister"}),Uc(n.rngSeed),Ne()}finally{he!==void 0&&Xc(he)}}else Ne();if(Q>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let me=Z.length,le=[Z,z],Ee=[G,j];for(let he of[0,1]){let Le=le[he],we=q[he];if(i==="quantile")B[he]=[no(Le,D/2),no(Le,1-D/2)];else if(i==="Student")P[he]=Un(Le),B[he]=[we-A*P[he],we+A*P[he]];else if(i==="BCa"){let Me=Le.filter(ae=>ae<we).length;Le.every(ae=>ae===we)?Me=me/2:Me===0?Me=.5:Me===me&&(Me=me-.5);let Se=oe(Me/me),Ke=J[he],Pe=Ke.reduce((ae,Je)=>ae+Je,0)/Ke.length,Ze=0,Be=0;for(let ae of Ke){let Je=Pe-ae;Ze+=Je*Je,Be+=Je*Je*Je}let Qe=Ze===0?0:Be/(6*Ze**1.5),fe=-O,ee=O,ge=ie(Se-(fe-Se)/(1+Qe*(fe-Se))),de=ie(Se-(ee-Se)/(1+Qe*(ee-Se)));B[he]=[no(Le,de),no(Le,ge)]}else{let Me=Le.map((Ke,Pe)=>(Ke-we)/Ee[he][Pe]),Se=Number.isNaN(X[he])?Un(Le):X[he];P[he]=Se,B[he]=[we-no(Me,1-D/2)*Se,we-no(Me,D/2)*Se]}}W={intercept:Z.map(he=>$(he,b)),slope:z.map(he=>$(he,b-E)),sigmaIntercept:G.map(he=>$(he,b)),sigmaSlope:j.map(he=>$(he,b-E)),globalSigma:[$(X[0],b),$(X[1],b-E)],nsamples:me}}let U=X=>{let K=Z=>$(Z,X===0?b:b-E);return{estimate:K(q[X]),se:K(P[X]),confInt:[K(B[X][0]),K(B[X][1])]}},ve={methodReg:r,methodCi:o,...s?{methodBootstrapCi:i}:{},confLevel:c,errorRatio:m,n:N,intercept:U(0),slope:U(1),xmean:_,...I===void 0?{}:{weights:I},...J===void 0?{}:{jackknife:{intercept:J[0].map(X=>$(X,b)),slope:J[1].map(X=>$(X,b-E))}},...W===void 0?{}:{bootstrap:W},...r==="PaBa"?{cusum:Eb(M,x,L.b0,L.b1)}:{},warnings:[...T]};return H("mcreg",ve)}var Rb=["alpha","confLevel","lmer"],Tb={ICC1:"Single_raters_absolute",ICC2:"Single_random_raters",ICC3:"Single_fixed_raters",ICC1k:"Average_raters_absolute",ICC2k:"Average_random_raters",ICC3k:"Average_fixed_raters"};function sa(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function Sb(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,i]=sa(r,n[0]),[c,a]=sa(t[o],t[0]);return s===c&&i===a}))}var Fu=(e,t)=>e.every((n,r)=>n===t[r]);function _r(e){let t=[0,0];for(let n of e)t=ue(t,n);return ft(t,e.length)}var so=(e,t)=>({v:ft(e.v,t),e:e.e});function ao(...e){let t=e.filter(r=>r.v[0]!==0),n=t.length===0?0:Math.max(...t.map(r=>r.e));return e.map(r=>({at:et(r.v[0],r.e-n),E:n}))}function Du(e,t){if(t.v[0]===0)return e;if(e.v[0]===0)return t;let n=Math.max(e.e,t.e),r=o=>[et(o.v[0],o.e-n),et(o.v[1],o.e-n)];return{v:ue(r(e),r(t)),e:n}}var Pu=(e,t)=>et(e.v[0]/t.v[0],e.e-t.e),Ab=(e,t)=>{let[n,r]=ao(e,t);return n.at<r.at},kb=(e,t)=>{let[n,r]=ao(e,t);return n.at<=r.at};function Vu(e,t,n){let r=[0,0];for(let o of e){let s=Ce(o,t);r=ue(r,_e(s,s))}return _e(r,[n,0])}function qb(e,t={}){V(t,Rb,"ICC");let n=t.lmer??!0;if(typeof n!="boolean")throw new TypeError("'lmer' must be true or false");let r,o;if(t.alpha!==void 0){if(r=t.alpha,!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'alpha' must be a single number in (0, 1)");o=1-r}else o=t.confLevel??.95,It(o),r=1-o;if(!Array.isArray(e)||e.length===0||!e.every(se=>Array.isArray(se)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(se=>se.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(se=>se.some(Te=>typeof Te!="number")))throw new TypeError("'x' must contain numbers");if(e.some(se=>se.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(se=>se.some(Te=>!Number.isFinite(Te))))throw new RangeError("'x' contains infinite ratings");let i=e.length;if(i<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let{groups:c}=Re(e);if(c.every(se=>Fu(se,c[0])))throw new RangeError(c[0].every(se=>se===c[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let a=Array.from({length:s},(se,Te)=>c.map(pe=>pe[Te])),l=0;for(let se of e)for(let Te of se)l=Math.max(l,Math.abs(Te));let f=l>2**1020?3:0,m=f>0?e.map(se=>se.map(Te=>Te/8)):e,u=m.map(se=>se.map((Te,pe)=>sa(Te,m[0][pe]))),h=m.map(se=>se.map(Te=>sa(Te,se[0]))),p=h.map(se=>se.map((Te,pe)=>Ce(Te,h[0][pe]))),d=se=>{let Te=0;for(let pe of se)for(let Ae of pe)Te=Math.max(Te,Math.abs(Ae[0]));return Te===0?0:Math.floor(Math.log2(Te))},g=(se,Te)=>se.map(pe=>pe.map(Ae=>[$(Ae[0],-Te),$(Ae[1],-Te)])),[w,N,v]=[d(u),d(h),d(p)],[E,b,M]=[g(u,w),g(h,N),g(p,v)],x=se=>Array.from({length:s},(Te,pe)=>se.map(Ae=>Ae[pe])),y=Vu(E.map(_r),_r(E.flat()),s),R=a.every(se=>Fu(se,a[0]))?[0,0]:Vu(x(b).map(_r),_r(b.flat()),i),T=[0,0];if(!Sb(c)){let se=M.map(_r),Te=x(M).map(_r),pe=_r(M.flat());M.forEach((Ae,We)=>Ae.forEach((Fe,nt)=>{let ut=ue(Ce(Ce(Fe,se[We]),Te[nt]),pe);T=ue(T,_e(ut,ut))}))}let S=(se,Te)=>({v:se,e:2*(Te+f)}),k=S(y,w),L=S(R,N),_=S(T,v),I=i-1,F=s-1,C=(i-1)*(s-1),D=so(k,I),A=so(L,F),O=so(_,C),q=so(Du(L,_),F+C),P=[];if(n){let se={B:{ss:k,df:I,ms:D,pooled:!1},J:{ss:L,df:F,ms:A,pooled:!1}},Te=_,pe=C;for(let We of kb(D,A)?["B","J"]:["J","B"]){let Fe=se[We];Ab(Fe.ms,so(Te,pe))&&(Te=Du(Te,Fe.ss),pe+=Fe.df,Fe.pooled=!0)}let Ae=so(Te,pe);if(se.B.pooled&&(D=Ae),se.J.pooled&&(A=Ae),O=Ae,se.B.pooled){let[We,Fe]=ao(O,A);q={v:[We.at+(Fe.at-We.at)/i,0],e:We.E}}}else T[0]===0&&P.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let B=(se,Te)=>r/2<1e-8?ln(r/2,se,Te,!1,!1):ln(1-r/2,se,Te,!0,!1),J=(se,Te,pe)=>wt(se,Te,pe,!1,!1),ce=se=>se===1/0?1:(se-1)/(se+s-1),W=se=>1-1/se,te=i*(s-1),U=Pu(D,q),ve=Pu(D,O),[X,K]=[U/B(I,te),U*B(te,I)],[Z,z]=[ve/B(I,C),ve*B(C,I)],[G,j]=ao(D,q).map(se=>se.at),[Q,ne]=ao(D,O).map(se=>se.at),[be,Ne,me]=ao(D,A,O).map(se=>se.at),le=(G-j)/(G+(s-1)*j),Ee=(be-me)/(be+(s-1)*me+s*(Ne-me)/i),he=(Q-ne)/(Q+(s-1)*ne),Le=s*Ee*Ne,we=(i*(1+(s-1)*Ee)-s*Ee)*me,Me=Math.max(Math.abs(Le),Math.abs(we)),Se=Me>0&&Number.isFinite(Me)?Math.floor(Math.log2(Me)):0,Ke=$(Le,-Se),Pe=$(we,-Se),Ze=(i-1)*Ke*Ke+Pe*Pe,Be=Ze===0?s-1:(s-1)*(i-1)*(Ke+Pe)**2/Ze,Qe=B(I,Be),fe=B(Be,I),ee=s*Ne+(s*i-s-i)*me,ge=(se,Te)=>se>.5?1-(i*me+ee)/Te:se,de=ge(i*(be/Qe-me)/(ee+i*be/Qe),ee+i*be/Qe),ae=fe>1?ge(i*(be-me/fe)/(ee/fe+i*be),ee+i*fe*be):ge(i*(fe*be-me)/(ee+i*fe*be),ee+i*fe*be),Je=se=>se*s/(1+se*(s-1)),Oe=J(U,I,te),tt=J(ve,I,C),ke=(se,Te,pe,Ae,We,Fe,nt)=>({type:se,name:Tb[se],ICC:Te,F:pe,df1:I,df2:Ae,pValue:We,lower:Fe,upper:nt}),$e=[ke("ICC1",le,U,te,Oe,ce(X),ce(K)),ke("ICC2",Ee,ve,C,tt,de,ae),ke("ICC3",he,ve,C,tt,ce(Z),ce(z)),ke("ICC1k",(G-j)/G,U,te,Oe,W(X),W(K)),ke("ICC2k",(be-me)/(be+(Ne-me)/i),ve,C,tt,Je(de),Je(ae)),ke("ICC3k",(Q-ne)/Q,ve,C,tt,W(Z),W(z))],He=se=>et(se.v[0],se.e);return H("ICC",{results:$e,MSB:He(D),MSJ:He(A),MSE:He(O),MSW:He(q),n:i,k:s,confLevel:o,lmer:n,warnings:P})}function aa(e){let t=e.length;if(t===0||e.some(n=>n.length!==t))throw new RangeError("'covmat' must be a square matrix");if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError("'covmat' must be finite");if(e.some((n,r)=>!(n[r]>0)))throw new RangeError("the diagonal of 'covmat' must be positive");if(!hl(sl(e)))throw new RangeError("'covmat' must be symmetric")}var Ib=["covmat","nObs","rotation","scores","control"],_b=["lower","start"];function St(e){let t=0,n=0;for(let r of e){let[o,s]=$t(t,r);t=o,n+=s}return t+n}var hc=(e,t)=>e[0].map((n,r)=>St(e.map(o=>t(o[r]))));function ju(e,t){let n=t.map(r=>1/Math.sqrt(r));return e.map((r,o)=>r.map((s,i)=>n[o]*s*n[i]))}function $u(e,t,n){let r=un(ju(e,t),{symmetric:!0}),o=r.values.slice(0,n).map(s=>Math.sqrt(Math.max(s-1,0)));return r.vectors.map((s,i)=>s.slice(0,n).map((c,a)=>Math.sqrt(t[i])*(c*o[a])))}function zu(e,t){let n=hc(e,c=>c*c),r=n.map((c,a)=>a).sort((c,a)=>n[a]-n[c]||c-a),o=e.map(c=>r.map(a=>c[a])),s=t?.map(c=>r.map(a=>c[a])),i=hc(o,c=>c).map(c=>c<0?-1:1);return o=o.map(c=>c.map((a,l)=>i[l]<0?-a:a)),s=s?.map(c=>c.map((a,l)=>i[l]<0?-a:a)),s===void 0?{L:o}:{L:o,rot:s}}var qn=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let i=0;i<n.length;i++)s+=n[i]*t[i][o];return s})),io=e=>e[0].map((t,n)=>e.map(r=>r[n]));function ia(e){let t=e[0].length,n=e.map(m=>Math.sqrt(St(m.map(u=>u*u)))),r=e.flatMap((m,u)=>n[u]>0?[u]:[]),o=r.map(m=>e[m].map(u=>u/n[m])),s=o.length,i=Array.from({length:t},(m,u)=>Array.from({length:t},(h,p)=>u===p?1:0));if(s===0)return{loadings:e.map(m=>m.slice()),rotmat:i};let c=io(o),a=0;for(let m=0;m<1e3;m++){let u=qn(o,i),h=u[0].map((N,v)=>{let E=0;for(let b=0;b<s;b++)E+=u[b][v]*u[b][v];return E}),p=u.map(N=>N.map((v,E)=>v**3-v*h[E]/s)),d=qn(c,p),g=bs(d);i=qn(g.u,io(g.v));let w=a;if(a=St(g.d),a<w*(1+1e-5))break}let l=qn(o,i),f=e.map(m=>m.map(()=>0));return r.forEach((m,u)=>f[m]=l[u].map(h=>h*n[m])),{loadings:f,rotmat:i}}function Lb(e){let t=ia(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=al(jt(n),r),s=rt(qn(io(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:qn(n,i),rotmat:qn(t.rotmat,i)}}function Cb(e,t,n={}){V(n,Ib,"factanal"),V(Ft(n,"control"),_b,"factanal","control");let r=n.scores??"none";if(r!=="none"&&r!=="regression"&&r!=="Bartlett")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Cregression\u201D, \u201CBartlett\u201D");let o,s;if(n.covmat!==void 0){if(r!=="none")throw new RangeError("requested scores without an 'x' matrix");aa(n.covmat),o=n.covmat.map(x=>x.slice()),s=n.nObs??NaN}else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");o=Ms(e),s=e.length}let i=o.length;if(i<3)throw new RangeError("factor analysis requires at least three variables");let c=t;if(!Number.isInteger(c)||c<1)throw new RangeError("'factors' must be a positive integer");let a=((i-c)**2-i-c)/2;if(a<0)throw new RangeError(`${c} ${c===1?"factor is":"factors are"} too many for ${i} variables`);let l=o.map((x,y)=>Math.sqrt(x[y])),f=o.map((x,y)=>x.map((R,T)=>R/(l[y]*l[T]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??rt(f).map((x,y)=>(1-c/(2*i))/x[y]),h=x=>{let y=un(ju(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(St(y.map(R=>Math.log(R)-R))-c+i)},p=x=>{let y=$u(f,x,c);return x.map((R,T)=>{let S=0;for(let k=0;k<c;k++)S+=y[T][k]*y[T][k];return(S+R-f[T][T])/(R*R)})},d=je(()=>Eo(u,h,{gr:p,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(d.convergence!==0)throw new Error("unable to optimize from this starting value");let g=d.par,w=zu($u(f,g,c)).L,N,v=n.rotation??"varimax";if(v!=="varimax"&&v!=="promax"&&v!=="none")throw new RangeError(`unknown rotation '${String(v)}'`);if(v!=="none"&&c>=2){let x=v==="varimax"?ia(w):Lb(w),y=zu(x.loadings,x.rotmat);w=y.L,N=y.rot}let E=NaN,b=NaN;!Number.isNaN(s)&&a>0&&(E=(s-1-(2*i+5)/6-2*c/3)*d.value,b=Mt(E,a,{lowerTail:!1}));let M={uniquenesses:g,loadings:w,STATISTIC:E,dof:a,PVAL:b,converged:!0,objective:d.value,counts:d.counts,warnings:[]};return r!=="none"&&(M.scores=Ob(e,f,g,w,r)),N!==void 0&&(M.rotmat=N),M}function Ob(e,t,n,r,o){let s=e.length,i=e[0].map((m,u)=>St(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-i[h])),a=hc(c,m=>m*m).map(m=>Math.sqrt(m/(s-1))),l=c.map(m=>m.map((u,h)=>u/a[h]));if(o==="regression")return qn(l,rt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return qn(l,io(rt(qn(io(r),f),io(f))))}var Fb=["nfactors","covmat"],co=2**-52,Db=`
|
|
13
|
+
To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function Gb(e,t,n){let r=ws(e,"x").length;xa(e,"x");for(let c of[t,n])if(!Number.isInteger(c)||c<0||c>=r)throw new RangeError(`column index ${c} out of range (0 to ${r-1})`);if(t===n)throw new RangeError("'i' and 'j' must be different columns");let o=Ho(e),s=[t,n],i=Array.from({length:r},(c,l)=>l).filter(c=>c!==t&&c!==n),a=s.map(c=>s.map(l=>o[c][l]));if(i.length>0){let c=i.map(m=>i.map(u=>o[m][u])),l=i.map(m=>s.map(u=>o[m][u])),f=ot(c,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)a[m][u]-=Ie(i.map((h,p)=>o[s[m]][h]*f[p][u]))}return a[0][1]/Math.sqrt(a[0][0]*a[1][1])}function Hb(e){let t=e.length,n=e[0].length,r=c=>c.reduce((l,f)=>he(l,[f,0]),[0,0]),o=e.map(c=>ht(r(c),n)),s=e[0].map((c,l)=>ht(r(e.map(f=>f[l])),t)),i=ht(r(e.flat()),t*n),a=[0,0];return e.forEach((c,l)=>c.forEach((f,m)=>{let u=he(Le(Le([f,0],o[l]),s[m]),i);a=he(a,_e(u,u))})),a[0]+a[1]}var Qb=["controls","cases"],lh=["levels","direction","naRm"],Xb=["partialAuc","focus","correct"],Zb=["bestMethod","bestWeights"],dh=new WeakMap;function Aa(e){return dh.get(e)}var Jb=["auto","<",">"];function Ic(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:Hn(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 ev(e){let t=e.filter(s=>Ic(s)!==null),n=new Set(t.map(s=>typeof s));if(n.size>1)throw new TypeError("the response mixes values of different types");let r=[...n][0],o;return r==="number"?o=[...new Set(t)].sort((s,i)=>s-i).map(Hn):r==="boolean"?o=[...new Set(t)].sort((s,i)=>Number(s)-Number(i)).map(s=>s?"TRUE":"FALSE"):o=[...new Set(t)].sort((s,i)=>s<i?-1:s>i?1:0),[...new Set(o)]}function vs(e){return Float64Array.from(e).sort()}function _c(e,t){let n=vs(e),r=vs(t),o=[],s=[],i=[],a=0,c=0;for(;a<n.length||c<r.length;){let l=c>=r.length||a<n.length&&n[a]<=r[c]?n[a]:r[c];for(;a<n.length&&n[a]===l;)a++;for(;c<r.length&&r[c]===l;)c++;o.push(l),s.push(a),i.push(c)}return{u:o,ctrlLE:s,caseLE:i}}function Cc(e,t,n){let r=0;for(let o=0;o<e.u.length;o++){let s=o>0?e.ctrlLE[o-1]:0,i=e.caseLE[o]-(o>0?e.caseLE[o-1]:0),a=e.ctrlLE[o]-s,c=n==="<"?s:t-e.ctrlLE[o];r+=i*(2*c+a)}return r}function Lc(e,t,n){let r=e.length,o=t.length;return Cc(_c(e,t),r,n)/(2*o*r)}function Oc(e,t,n){return Cc(_c(e,t),e.length,n)}var fh=e=>e===0n?0:e.toString(2).length;function wo(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(fh(r)-fh(t)),s=o>=0?r<<BigInt(o):r,i=o>=0?t:t<<BigInt(-o),a=s/i;a*i!==s&&(a|=1n);let c=tt(Number(a),-o);return n?-c:c}function tv(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 nv(e,t,n){let r=_c(e,t),o=e.length,s=t.length,i=r.u.length,a=[],c=[],l=[],f=m=>{let u=m===0?-1/0:m===i?1/0:tv(r.u[m-1],r.u[m],n),h=m===0?0:r.caseLE[m-1],p=m===0?0:r.ctrlLE[m-1];a.push(u),c.push((n==="<"?s-h:h)/s),l.push((n==="<"?p:o-p)/o)};if(n==="<")for(let m=0;m<=i;m++)f(m);else for(let m=i;m>=0;m--)f(m);return{thresholds:a,sensitivities:c,specificities:l,auc:Cc(r,o,n)/(2*s*o)}}function rv(e,t){let n=r=>{let o=vs(r),s=o.length>>1;return o.length%2===1?o[s]:fr(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function gh(e,t,n,r,o,s){if(e.length===0)throw new Error("No control observation.");if(t.length===0)throw new Error("No case observation.");if(e.some(l=>!Number.isFinite(l))||t.some(l=>!Number.isFinite(l)))throw new RangeError("infinite values in the predictor: cannot build a valid ROC curve");let i=n==="auto"?rv(e,t):n,c={...nv(e,t,i),direction:i,levels:r,controls:e,cases:t,warnings:s};return dh.set(c,o),c}function wh(e){if(e===void 0)return"auto";if(!Jb.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function po(e,t,n){let r=wh(n.direction),o=new pt,s=e.map(Ic),i;if(n.levels===void 0){if(i=ev(e),i.length>2&&(o.add("'response' has more than two levels. Consider setting 'levels' explicitly or using 'multiclass.roc' instead"),i=i.slice(0,2)),i.length<2)throw new RangeError("'response' must have two levels")}else{if(!Array.isArray(n.levels)||n.levels.length!==2)throw new RangeError("'levels' argument must have length 2");i=n.levels.map(p=>{let d=Ic(p);if(d===null)throw new RangeError("'levels' must not be missing");return d})}if(t.some(p=>typeof p!="number"))throw new TypeError("Predictor must be numeric.");if(s.length!==t.length)throw new TypeError("Response and predictor must be vectors of the same length.");let a=s.map((p,d)=>p===null||Number.isNaN(t[d]));if(n.naRm===!1&&a.some(p=>p))throw new RangeError("missing values in the response or the predictor with naRm false");let c=[],l=[],f=[],m=[],u=[];s.forEach((p,d)=>{if(a[d])return;let g=t[d];if(p===i[0])c.push(g);else if(p===i[1])l.push(g);else return;f.push(g),m.push(p===i[1]),u.push(d)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return gh(c,l,r,[i[0],i[1]],h,o.list)}function ov(e,t){let n=oo(mr(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=Vs(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(a=>o.levels[a]),predictor:s.values}}function sv(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function av(e,t,n={}){if(lt(e)&&z(e,Qb,"roc"),typeof e!="string"&<(t)&&z(t,lh,"roc"),z(n,lh,"roc"),typeof e=="string"){let r=ov(e,t);return J("roc",po(r.response,r.predictor,n))}if(sv(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=wh(r.direction),i=Array.from(o.controls),a=Array.from(o.cases);if([...i,...a].some(u=>typeof u!="number"))throw new TypeError("Predictor must be numeric.");if(r.naRm===!1&&[...i,...a].some(Number.isNaN))throw new RangeError("missing values in the controls or the cases with naRm false");let c=i.filter(u=>!Number.isNaN(u)),l=a.filter(u=>!Number.isNaN(u)),f=[...c,...l],m={labels:null,predictor:f,values:f,isCase:f.map((u,h)=>h>=c.length),kept:f.map((u,h)=>h)};return J("roc",gh(c,l,s,["0","1"],m,[]))}return J("roc",po(Array.from(e),Array.from(t),n))}var Sa=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function iv(e,t,n,r){let o=[];if(e.forEach((c,l)=>{c>=n&&c<=r&&o.push(l)}),o.length===0){let c=e.findIndex(u=>u>=r),l=c-1,f=Sa(e,t,r,c,l),m=Sa(e,t,n,c,l);return(f+m)/2*(r-n)}let s=0;for(let c=1;c<o.length;c++){let l=o[c-1],f=o[c];s+=(e[f]-e[l])*(t[l]+t[f])/2}let i=o[o.length-1];if(e[i]!==r){let c=e.findIndex(m=>m>=r),l=c-1,f=Sa(e,t,r,c,l);s+=(r-e[l])*(t[l]+f)/2}let a=o[0];if(e[a]!==n){let c=e.findIndex(m=>m>n)-1,l=c+1,f=Sa(e,t,n,l,c);s+=(e[l]-n)*(t[l]+f)/2}return s}var Tr=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],Br=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],Pn=(e,t)=>[e[0]*t[0],e[1]*t[1]],mh=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],bs=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function uh(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 hh(e,t,n,r,o,s){let i=uh(e,n),a=uh(t,r);if(i!==void 0&&a!==void 0)return ph(i.map(BigInt),a.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(go),f=t.map(go),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return ph(l.map(u),f.map(u),m,m,o,s)}function ph(e,t,n,r,o,s){let i=n*r,a=g=>[t[g]*n+e[g]*r-i,i],c=g=>[e[g],n],l=go(o),f=go(s),m=(g,w,N)=>Tr(a(w),Pn(mh(Br(g,c(w)),Br(c(N),c(w))),Br(a(N),a(w)))),u=[];e.forEach((g,w)=>{bs(c(w),l)>=0&&bs(c(w),f)<=0&&u.push(w)});let h;if(u.length===0){let g=e.findIndex((N,b)=>bs(c(b),f)>=0),w=g-1;h=Pn(Tr(m(f,w,g),m(l,w,g)),Pn(Br(f,l),[1n,2n]))}else{let g=0n;for(let b=1;b<u.length;b++){let E=u[b-1],v=u[b];g+=(e[v]-e[E])*(a(E)[0]+a(v)[0])}h=[g,2n*n*i];let w=u[u.length-1];if(bs(c(w),f)!==0){let b=w+1;h=Tr(h,Pn(Br(f,c(w)),Pn(Tr(a(w),m(f,w,b)),[1n,2n])))}let N=u[0];if(bs(c(N),l)!==0){let b=N-1;h=Tr(h,Pn(Br(c(N),l),Pn(Tr(a(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=Pn(Pn(Br(f,l),Tr(f,l)),[1n,2n]),d=mh(Tr(p,h),Pn(p,[2n,1n]));return wo(d[0],d[1])}function cv(e,t={}){z(t,Xb,"auc");let n=Te(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return J("auc",{auc:e.auc,partialAuc:null,focus:n,correct:r,warnings:[]});let o=t.partialAuc;if(!Array.isArray(o)||o.length!==2||o.some(p=>typeof p!="number"))throw new TypeError("partialAuc must be a numeric vector of length 2");let s=Math.max(o[0],o[1]),i=Math.min(o[0],o[1]);if(!(i>=0&&s<=1))throw new RangeError("the limits of partialAuc must be within [0, 1]");if(!(i<s))throw new RangeError("the limits of partialAuc must differ");let a=e.sensitivities,c=e.specificities,[l,f]=n==="specificity"?[c,a]:[[...a].reverse(),[...c].reverse()],m,u=e.controls.length,h=e.cases.length;return r?m=n==="specificity"?hh(l,f,u,h,i,s):hh(l,f,h,u,i,s):m=iv(l,f,i,s),J("auc",{auc:m,partialAuc:[s,i],focus:n,correct:r,warnings:[]})}function go(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 lv(e,t){let n=vs(e.controls),r=vs(e.cases),o=(i,a,c)=>{let l=0,f=i.length;for(;l<f;){let m=l+f>>1;(c?i[m]<=a:i[m]<a)?l=m+1:f=m}return l},s=e.direction==="<";return{tp:t.map(i=>s?r.length-o(r,i,!1):o(r,i,!0)),tn:t.map(i=>s?o(n,i,!1):n.length-o(n,i,!0))}}function fv(e,t="all",n={}){z(n,Zb,"coords");let r=Te(n.bestMethod,["youden","closest.topleft","topleft"]),o=n.bestWeights??[1,.5];if(!Array.isArray(o)||o.length!==2||o.some(g=>typeof g!="number"))throw new TypeError("'best.weights' must be a numeric vector of length 2");let[s,i]=o;if(!(i>0&&i<1))throw new RangeError("prevalence ('best.weights[2]') must be in the interval ]0,1[.");if(!(s>0&&s<1/0))throw new RangeError("the cost ('bestWeights[0]') must be a positive finite number");let a=(1-i)/(s*i),c,l=null;if(typeof t=="string"&&(l=Te(t,["all","best"])),l!==null)c=[...e.thresholds];else{if(c=typeof t=="number"?[t]:Array.from(t),c.length===0)throw new RangeError("Numeric 'x' has length 0");if(c.some(g=>typeof g!="number"||Number.isNaN(g)))throw new RangeError("the thresholds must be numbers (not NaN)")}let f=e.cases.length,m=e.controls.length,{tp:u,tn:h}=lv(e,c);if(l==="best"){let[g,w]=go(s),[N,b]=go(i),E=(b-N)*w,v=g*N,y=BigInt(f),T=BigInt(m),M=u.map((A,S)=>{if(r==="youden")return BigInt(A)*T*v+E*BigInt(h[S])*y;let I=BigInt(f-A),C=BigInt(m-h[S]);return-(I*I*T*T*v+E*C*C*y*y)}),R=M.reduce((A,S)=>S>A?S:A),x=M.map((A,S)=>A===R?S:-1).filter(A=>A>=0);c=x.map(A=>c[A]),u=x.map(A=>u[A]),h=x.map(A=>h[A])}let p=u.map(g=>f-g),d=h.map(g=>m-g);return J("coords",{threshold:c,sensitivity:u.map(g=>g/f),specificity:h.map(g=>g/m),accuracy:u.map((g,w)=>(g+h[w])/(f+m)),tn:h,tp:u,fn:p,fp:d,npv:h.map((g,w)=>g/(g+p[w])),ppv:u.map((g,w)=>g/(g+d[w])),fpr:d.map(g=>g/m),lrPos:u.map((g,w)=>g*m/(f*d[w])),lrNeg:p.map((g,w)=>g*m/(f*h[w])),youden:u.map((g,w)=>g/f+a*(h[w]/m)),closestTopleft:p.map((g,w)=>(g/f)**2+a*(d[w]/m)**2),warnings:[]})}var mv=["confLevel","method","bootN","bootStratified"],bh=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function Ia(e){let t=Float64Array.from(e.controls).sort(),n=Float64Array.from(e.cases).sort(),r=t.length,o=n.length,s=(l,f)=>{let m=0,u=l.length;for(;m<u;){let p=m+u>>1;l[p]<f?m=p+1:u=p}let h=m;for(;h<l.length&&l[h]===f;)h++;return[m,h-m]},i=e.direction==="<",a=e.cases.map(l=>{let[f,m]=s(t,l);return 2*(i?f:r-f-m)+m}),c=e.controls.map(l=>{let[f,m]=s(n,l);return 2*(i?o-f-m:f)+m});return{k10:a,k01:c}}function Vc(e,t,n,r){return st(e)/(4*n*n)/r+st(t)/(4*r*r)/n}function vh(e){let{k10:t,k01:n}=Ia(e);return Vc(t,n,e.controls.length,e.cases.length)}function ka(e,t,n,r,o,s){let i=BigInt(Oc(e,t,n)),a=BigInt(Oc(r,o,s)),c=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return wo(i*l-a*c,2n*c*l)}var Pc=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function vo(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function Mh(e){let t=(1-e)/2;return t<1e-8?re(t,{lowerTail:!1}):re(1-t)}function yh(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function No(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[Yo(e.length)];return t}function Dc(e){let t=Aa(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 Ns(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 uv(e,t,n,r){let o=Ns(e,t,n);return o===void 0?NaN:Lc(o.controls,o.cases,r)}function bo(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=Yo(e);return t}function hv(e,t={}){z(t,mv,"ciAuc");let n=t.confLevel??.95;fn(n);let r=Te(t.method,["delong","bootstrap"]),o=new pt;Pc(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"){vo(e,"DeLong's interval");let{k10:m,k01:u}=Ia(e),h=e.auc,p=Vc(m,u,e.controls.length,e.cases.length),d=Math.sqrt(p),g=b=>Math.min(1,Math.max(0,b)),w=s/2,N=n===1?[0,1]:d===0?[h,h]:[g(h+re(w)*d),g(h+Mh(n)*d)];return J("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:p,warnings:o.list})}let i=t.bootN??2e3;yh(i);let a=t.bootStratified??!0,c=[],l=0;if(a){vo(e,"a stratified bootstrap");for(let m=0;m<i;m++){let u=No(e.controls),h=No(e.cases);c.push(Lc(u,h,e.direction))}}else{let{values:m,isCase:u}=Dc(e);for(let h=0;h<i;h++){let p=uv(m,u,bo(m.length),e.direction);Number.isNaN(p)?l++:c.push(p)}}if(l>0&&o.add("NA value(s) produced during bootstrap were ignored."),c.length===0)throw new Error("no bootstrap replicate had both controls and cases");let f=io(c,[s/2,1-s/2]);return J("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:c.length>1?st(c):NaN,warnings:o.list})}var Nh=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,qa=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function pv(e,t){let n=Aa(e),r=Aa(t);if(n?.labels==null||r?.labels==null||!qa(e.levels,t.levels))return;let o=a=>a.kept.map(c=>a.labels[c]);if(qa(o(n),o(r))&&qa(n.isCase,r.isCase))return[e,t];if(!qa(n.labels,r.labels))return;let s=n.labels.map((a,c)=>c).filter(a=>n.labels[a]!==null&&!Number.isNaN(n.predictor[a])&&!Number.isNaN(r.predictor[a])),i=(a,c)=>po(s.map(l=>c.labels[l]),s.map(l=>c.predictor[l]),{levels:a.levels,direction:a.direction});return[i(e,n),i(t,r)]}function Fc(e,t){return t==="two.sided"?2*oe(-Math.abs(e)):t==="greater"?oe(e,{lowerTail:!1}):oe(e)}function dv(e,t,n,r){lt(n)&&z(n,bh,"rocTest"),z(r,bh,"rocTest");let o,s=new pt,i,a;if(Nh(e)){if(!Nh(t))throw new TypeError("roc2 must be a curve from roc()");if(o=n??{},o.levels!==void 0||o.direction!==void 0)throw new RangeError("'levels' and 'direction' apply only to a response and two predictors");i=e,a=t}else{o=r??{};let v=Array.from(e),y=Array.from(t),T=Array.from(n);if(v.length!==y.length||v.length!==T.length)throw new TypeError("the response and the predictors must have the same length");let M=v.map((A,S)=>S).filter(A=>{let S=v[A];return S!=null&&!(typeof S=="number"&&Number.isNaN(S))&&!Number.isNaN(y[A])&&!Number.isNaN(T[A])}),R=M.map(A=>v[A]),x={levels:o.levels,direction:o.direction};i=po(R,M.map(A=>y[A]),x),a=po(R,M.map(A=>T[A]),x);for(let A of i.warnings)s.add(A)}let c=Ge(o.alternative),l=Te(o.method,["delong","bootstrap"]),f=o.confLevel??.95;fn(f),Pc(i)&&Pc(a)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=pv(i,a),u;if(o.paired===!0){if(m===void 0)throw new RangeError("The paired ROC test cannot be applied to unpaired curves.");u=!0}else o.paired===!1?(m!==void 0&&s.add("The ROC curves seem to be paired. Consider performing a paired roc.test."),u=!1):u=m!==void 0;u&&([i,a]=m);let h={alternative:c,estimate:[i.auc,a.auc],estimateNames:["AUC of roc1","AUC of roc2"],nullValue:0,nullValueName:"difference in AUC",roc1:i,roc2:a},p=ka(i.controls,i.cases,i.direction,a.controls,a.cases,a.direction);if(l==="delong"){if(i.direction!==a.direction&&s.add("DeLong's test should not be applied to ROC curves with a different direction."),vo(i,"DeLong's test"),vo(a,"DeLong's test"),u){let I=Ia(i),C=Ia(a),_=I.k10.map((L,q)=>L-C.k10[q]),F=I.k01.map((L,q)=>L-C.k01[q]),O=Math.sqrt(Vc(_,F,i.controls.length,i.cases.length)),D=p===0&&O===0?0:p/O,k=O===0?0:Mh(f)*O;return J("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:D,statisticName:"Z",pValue:Fc(D,c),confInt:[p-k,p+k],confLevel:f,...h,warnings:s.list})}let v=vh(i),y=vh(a),T=i.controls.length+i.cases.length,M=a.controls.length+a.cases.length,R=v+y,x,A,S;return R===0?(x=p===0?0:p>0?1/0:-1/0,A=NaN,S=Fc(x,c)):(x=p/Math.sqrt(R),A=R*R/(v*v/(T-1)+y*y/(M-1)),S=c==="two.sided"?2*at(-Math.abs(x),A,!0,!1):c==="greater"?at(x,A,!1,!1):at(x,A,!0,!1)),J("rocTest",{method:"DeLong's test for two ROC curves",statistic:x,statisticName:"D",parameter:A,parameterName:"df",pValue:S,...h,warnings:s.list})}let d=o.bootN??2e3;yh(d);let g=o.bootStratified??!0,w=[],N=0;if(g){vo(i,"a stratified bootstrap"),vo(a,"a stratified bootstrap");for(let v=0;v<d;v++)if(u){let y=bo(i.controls.length),T=bo(i.cases.length);w.push(ka(y.map(M=>i.controls[M]),T.map(M=>i.cases[M]),i.direction,y.map(M=>a.controls[M]),T.map(M=>a.cases[M]),a.direction))}else{let y=No(i.controls),T=No(i.cases),M=No(a.controls),R=No(a.cases);w.push(ka(y,T,i.direction,M,R,a.direction))}}else{let v=Dc(i),y=Dc(a);for(let T=0;T<d;T++){let M,R;if(u){let x=bo(v.values.length);M=Ns(v.values,v.isCase,x),R=Ns(y.values,y.isCase,x)}else M=Ns(v.values,v.isCase,bo(v.values.length)),R=Ns(y.values,y.isCase,bo(y.values.length));M===void 0||R===void 0?N++:w.push(ka(M.controls,M.cases,i.direction,R.controls,R.cases,a.direction))}}if(N>0&&s.add(`${N} NA value(s) produced during bootstrap were ignored.`),w.length<2)throw new Error("fewer than 2 bootstrap replicates had both controls and cases");let b=Math.sqrt(st(w)),E=p===0&&b===0?0:p/b;return J("rocTest",{method:u?"Bootstrap test for two correlated ROC curves":"Bootstrap test for two ROC curves",statistic:E,statisticName:"D",parameter:d,parameterName:"boot.n",pValue:Fc(E,c),bootStratified:g,...h,warnings:s.list})}var gv=["times","cause","weighting","otherMarkers","iid","roc"],wv=["level","nSim"],bv=["adjusted"],vv=["fp","level","nSim"],Nv=["level","fp"],Mv=["times","cause","weighting","otherMarkers","iid"],yv=["predictTime","cutValues","method"],zc=new WeakMap;function Rr(e,t,n=!1){if(!Array.isArray(e)&&!ArrayBuffer.isView(e))throw new TypeError(`'${t}' must be an array of numbers`);let r=Array.from(e);for(let o of r){if(typeof o!="number")throw new TypeError(`'${t}' must be an array of numbers`);if(Number.isNaN(o)?!n:!Number.isFinite(o))throw new RangeError(`'${t}' must contain finite numbers${n?" (or NaN for NA)":""}`)}return r}function Rh(e,t,n,r){let o=Rr(e,"time",!0),s=Rr(t,"delta",!0),i=Rr(n,"marker",!0);if(s.length!==o.length||i.length!==o.length)throw new RangeError("'time', 'delta' and 'marker' must have the same length");return o.map((a,c)=>c).filter(a=>!Number.isNaN(o[a])&&!Number.isNaN(s[a])&&!Number.isNaN(i[a])&&!(r?.[a]?.some(c=>Number.isNaN(c))??!1))}function Ev(e,t,n){let r=Rr(e,"time"),o=Rr(t,"delta"),s=Rr(n,"marker"),i=r.length;if(i===0)throw new RangeError("no subjects");if(o.length!==i||s.length!==i)throw new RangeError("'time', 'delta' and 'marker' must have the same length");for(let w of o)if(!Number.isInteger(w)||w<0)throw new RangeError("'delta' must hold non-negative integers (0 censored)");let a=Array.from({length:i},(w,N)=>N).sort((w,N)=>r[w]-r[N]||w-N),c=[],l=new Array(i);for(let w of a)(c.length===0||r[w]!==c[c.length-1])&&c.push(r[w]),l[w]=c.length-1;let f=c.length,m=new Array(f+1).fill(0),u=new Array(f).fill(0),h=new Array(f).fill(0);for(let w=0;w<i;w++)m[l[w]]++,o[w]===0?u[l[w]]++:h[l[w]]++;for(let w=f-1;w>=0;w--)m[w]+=m[w+1];let p=c.map((w,N)=>m[N]-h[N]),d=new Array(f),g=1;for(let w=0;w<f;w++)p[w]>0&&(g*=1-u[w]/p[w]),d[w]=g;return{n:i,time:r,delta:o,marker:s,order:a,u:c,ti:l,R:p,C:u,G:d}}function Yc(e,t,n){let r=0,o=e.length;for(;r<o;){let s=r+o>>1;(n?e[s]<t:e[s]<=t)?r=s+1:o=s}return r-1}function Eh(e,t,n,r,o){let{n:s,time:i,delta:a}=e,c=new Array(s).fill(0),l=new Array(s).fill(!1),f=new Array(s).fill(0);for(let m=0;m<s;m++){let u=i[m];u>t?(f[m]=2,c[m]=1/o(m)):a[m]!==0&&(f[m]=u<t?a[m]===n?1:3:4,c[m]=1/r(m),l[m]=!0)}return{t,w:c,left:l,role:f}}function Wc(e,t){let n=Yc(e.u,t,!1),r=n>=0?e.G[n]:1;return{gLeft:o=>e.ti[o]>0?e.G[e.ti[o]-1]:1,gAt:()=>r,Gt:r}}function yo(e,t,n,r=!1){let{n:o,u:s,ti:i,R:a,C:c,delta:l}=e,f=s.length,m=new Array(f).fill(0),u=0,h=0;for(let N=0;N<o;N++){let b=t.w[N]*n[N];b!==0&&(h+=b,t.left[N]?m[i[N]]+=b:u+=b)}h/=o;let p=new Array(f).fill(0),d=0;for(let N=f-1;N>=0;N--){let b=d+(s[N]<=t.t?u:0),E=r?a[N]-c[N]:a[N];p[N]=E>0?b/E:0,d+=m[N]}let g=new Array(f+1).fill(0);for(let N=0;N<f;N++)g[N+1]=g[N]+(a[N]>0?c[N]/a[N]*p[N]:0);let w=new Array(o);for(let N=0;N<o;N++){let b=i[N],E=-g[b];l[N]===0&&(E+=p[b]-(a[b]>0?c[b]/a[b]*p[b]:0)),w[N]=t.w[N]*n[N]-h+E}return{value:h,inf:w}}function Mo(e,t){let n=e.value/t.value;return e.inf.map((r,o)=>(r-n*t.inf[o])/t.value)}function Tv(e,t,n){let{n:r,marker:o}=e,s=[],i=[];for(let p=0;p<r;p++)t.role[p]===1?s.push(p):n(t.role[p])&&i.push(p);let a=p=>{let d=[...p].sort((b,E)=>o[b]-o[E]),g=[],w=[0],N=[];for(let b of d)g.length&&g[g.length-1]===o[b]?(N[N.length-1]+=t.w[b],w[w.length-1]+=t.w[b]):(g.push(o[b]),N.push(t.w[b]),w.push(w[w.length-1]+t.w[b]));return b=>{let E=0,v=g.length;for(;E<v;){let y=E+v>>1;g[y]<b?E=y+1:v=y}return w[E]+(E<g.length&&g[E]===b?N[E]/2:0)}},c=p=>p.reduce((d,g)=>d+t.w[g],0),l=a(i),f=a(s),m=c(s),u=new Array(r).fill(0),h=0;for(let p of s){let d=l(o[p]);u[p]=d/r,h+=t.w[p]*d}for(let p of i)u[p]=(m-f(o[p]))/r;return{N:h/(r*r),v:u}}function _a(e,t,n,r){let{n:o,marker:s}=e,i=0,a=[];for(let m=0;m<o;m++)n(t.role[m])&&(i+=t.w[m],a.push(m));a.sort((m,u)=>s[u]-s[m]);let c=[],l=0,f=0;for(let m of r){for(;l<a.length&&s[a[l]]>m;)f+=t.w[a[l++]];c.push(f/i)}return c.push(1),c}function Rv(e,t){let{n,marker:r}=e;if(t!==void 0){if(!Array.isArray(t)||t.length!==n)throw new RangeError("'otherMarkers' must have one row per subject");let o=t[0]?.length??0;for(let s of t){if(!Array.isArray(s))throw new TypeError("'otherMarkers' must be an array of rows");if(s.length!==o)throw new RangeError("the rows of 'otherMarkers' must have the same length");for(let i of s)if(typeof i!="number"||!Number.isFinite(i))throw new RangeError("'otherMarkers' must contain finite numbers (or NaN for NA)")}}return r.map((o,s)=>[o,...t?.[s]??[]])}function xv(e,t,n){let{n:r,time:o,delta:s}=e;if(!s.some(u=>u===0))return{left:new Array(r).fill(1),at:n.map(()=>new Array(r).fill(1))};let i=vc(o,s.map(u=>u===0?1:0),t,{ties:"efron",confType:"none",control:{timefix:!1}}),a=i.fit.coefficients.map(u=>Number.isNaN(u)?0:u),c=i.fit.means,l=i.surv[0].map(u=>Math.log(-Math.log(u))),f=t.map(u=>u.reduce((h,p,d)=>h+a[d]*(p-c[d]),0)),m=(u,h,p)=>{let d=Yc(i.time,h,p);return d>=0?Math.exp(-Math.exp(l[d]+f[u])):1};return{left:Array.from({length:r},(u,h)=>m(h,o[h],!0)),at:n.map(u=>Array.from({length:r},(h,p)=>m(p,u,!1)))}}function Sv(e,t){let n=t.length,r=e.map((i,a)=>[...i,t[a]]),o=Math.max(...e.map((i,a)=>Math.abs(i[a])));for(let i=0;i<n;i++){let a=i;for(let c=i+1;c<n;c++)Math.abs(r[c][i])>Math.abs(r[a][i])&&(a=c);if(!(Math.abs(r[a][i])>1e-10*o))return null;[r[i],r[a]]=[r[a],r[i]];for(let c=i+1;c<n;c++){let l=r[c][i]/r[i][i];for(let f=i;f<=n;f++)r[c][f]-=l*r[i][f]}}let s=new Array(n).fill(0);for(let i=n-1;i>=0;i--){let a=r[i][n];for(let c=i+1;c<n;c++)a-=r[i][c]*s[c];s[i]=a/r[i][i]}return s}function Av(e,t,n){let{n:r,delta:o,u:s,ti:i,C:a}=e,c=t[0].length,l=t.map(()=>[1]);for(let E=0;E<c;E++){let v=t.map(R=>R[E]),y=(Math.max(...v)+Math.min(...v))/2,T=(Math.max(...v)-Math.min(...v))/2,M=T>0?2**Math.round(Math.log2(T)):1;for(let R=0;R<r;R++)l[R].push((v[R]-y)/M)}let f=[],m=[];for(let E=1;E<=c;E++){let v=l.map(x=>x[E]),y=v.reduce((x,A)=>x+A,0)/r,T=v.map(x=>x-y),M=Math.hypot(...T);for(let x of f){let A=T.reduce((S,I,C)=>S+I*x[C],0);for(let S=0;S<r;S++)T[S]-=A*x[S]}let R=Math.hypot(...T);M>0&&R>1e-7*M&&(f.push(T.map(x=>x/R)),m.push(E))}for(let E=0;E<r;E++)l[E]=[1,...m.map(v=>l[E][v])];let u=m.length+1,h=[],p=new Array(u).fill(0),d=!1,g=s.map(()=>[]);for(let E=0;E<r;E++)g[i[E]].push(E);let w=[],N=Array.from({length:u},()=>new Array(u).fill(0));for(let E=s.length-1;E>=0;E--){for(let v of g[E])if(o[v]===0)for(let y=0;y<u;y++)for(let T=0;T<u;T++)N[y][T]+=l[v][y]*l[v][T];w[E]=N.map(v=>[...v]);for(let v of g[E])if(o[v]!==0)for(let y=0;y<u;y++)for(let T=0;T<u;T++)N[y][T]+=l[v][y]*l[v][T]}for(let E=0;E<s.length;E++){if(!d&&a[E]>0){let v=new Array(u).fill(0);for(let T of g[E])if(o[T]===0)for(let M=0;M<u;M++)v[M]+=l[T][M];let y=Sv(w[E],v);y===null?d=!0:p=p.map((T,M)=>T+y[M])}h.push(p)}let b=(E,v)=>{if(v<0)return 1;let y=0;for(let T=0;T<u;T++)y+=l[E][T]*h[v][T];return Math.min(1,Math.max(0,Math.exp(-y)))};return{left:Array.from({length:r},(E,v)=>b(v,i[v]-1)),at:n.map(E=>{let v=Yc(s,E,!1);return Array.from({length:r},(y,T)=>b(T,v))})}}function xh(e,t,n,r,o){if(r===null||typeof r!="object")throw new TypeError(`${o}: 'options' with 'times' is required`);if(r.otherMarkers!==void 0&&(!Array.isArray(r.otherMarkers)||r.otherMarkers.length!==e.length))throw new RangeError("'otherMarkers' must have one row per subject");let s=Rh(e,t,n,r.otherMarkers),i=Ev(s.map(h=>e[h]),s.map(h=>t[h]),s.map(h=>n[h])),a=r.otherMarkers===void 0?void 0:s.map(h=>r.otherMarkers[h]),c=Rr(r.times??[],"times");if(c.length===0)throw new RangeError("'times' must have at least one time");let l=r.cause??1;if(!Number.isInteger(l)||l<=0)throw new RangeError("'cause' must be a positive integer");let f=r.weighting??"marginal";if(!["marginal","cox","aalen"].includes(f))throw new RangeError(`'weighting' must be "marginal", "cox" or "aalen"`);if(r.otherMarkers!==void 0&&f==="marginal")throw new RangeError("'otherMarkers' is used by the cox and aalen weights only");let m=i.delta.some(h=>h!==0&&h!==l),u=null;if(f!=="marginal"){let h=Rv(i,a);u=f==="cox"?xv(i,h,c):Av(i,h,c)}return{data:i,cause:l,competing:m,times:c,weighting:f,cond:u}}function Sh(e){return e.times.map((t,n)=>e.cond===null?(()=>{let r=Wc(e.data,t);return Eh(e.data,t,e.cause,r.gLeft,r.gAt)})():Eh(e.data,t,e.cause,r=>e.cond.left[r],r=>e.cond.at[n][r]))}function Ah(e,t){let n=0,r=0,o=0;for(let i of e.role)i===1?n++:i===2?r++:i===3&&o++;let s=e.role.length;return t?{cases:n,survivors:r,otherEvents:o,censored:s-n-r-o}:{cases:n,survivors:r,censored:s-n-r}}var Dn=(e,t)=>e.order.map(n=>t[n]),Eo=e=>{let t=e.length,n=e.reduce((r,o)=>r+o,0)/t;return Math.sqrt(e.reduce((r,o)=>r+(o-n)*(o-n),0)/(t-1)/t)};function kv(e,t,n,r=!1){let o=n==="i"?h=>h===2:h=>h===2||h===3,{N:s,v:i}=Tv(e,t,o),c=yo(e,t,i,r).inf,l=yo(e,t,t.role.map(h=>h===1?1:0),r),f;if(n==="i")f=yo(e,t,t.role.map(h=>h===2?1:0),r);else{let h=yo(e,t,t.role.map(d=>d===4?1:0),r);f={value:yo(e,t,t.role.map(d=>d===2||d===3?1:0),r).value,inf:l.inf.map((d,g)=>-d-h.inf[g])}}let m=s/(l.value*f.value),u=c.map((h,p)=>(h-m*(f.value*l.inf[p]+l.value*f.inf[p]))/(l.value*f.value));return{auc:m,inf:u}}function qv(e,t,n,r){z(r,gv,"timeROC");let o=xh(e,t,n,r,"timeROC"),s=r.iid??!1;if(s&&o.weighting!=="marginal")throw new RangeError("weighting must be marginal for computing the iid representation (as R)");let i=r.roc??!0,{data:a,competing:c}=o,l=Sh(o),f=[...new Set(a.marker)].sort((g,w)=>w-g),m=l.map(g=>g.w.reduce((w,N,b)=>w+(g.role[b]===1?N:0),0)/a.n),u={times:o.times,n:a.n,cumulativeIncidence:m,survProb:l.map(g=>g.w.reduce((w,N,b)=>w+(g.role[b]===2?N:0),0)/a.n),stats:l.map(g=>Ah(g,c)),weights:o.cond===null?{method:"marginal",ipcwTimes:o.times.map(g=>Wc(a,g).Gt),ipcwSubjectTimes:Dn(a,a.ti.map(g=>g>0?a.G[g-1]:1))}:{method:o.weighting,ipcwTimes:o.cond.at.map(g=>Dn(a,g)),ipcwSubjectTimes:Dn(a,o.cond.left)},...i?{tp:l.map(g=>_a(a,g,w=>w===1,f))}:{}},h=g=>l.map(w=>kv(a,w,g)),p=h("i"),d;if(!c)d={...u,competingRisks:!1,auc:p.map(g=>g.auc),...i?{fp:l.map(g=>_a(a,g,w=>w===2,f))}:{},...s?{inference:{iid:p.map(g=>Dn(a,g.inf)),sd:p.map(g=>Eo(g.inf))}}:{}};else{let g=h("ii");d={...u,competingRisks:!0,auc1:p.map(w=>w.auc),auc2:g.map(w=>w.auc),...i?{fp1:l.map(w=>_a(a,w,N=>N===2,f)),fp2:l.map(w=>_a(a,w,N=>N===2||N===3,f))}:{},...s?{inference:{iid1:p.map(w=>Dn(a,w.inf)),sd1:p.map(w=>Eo(w.inf)),iid2:g.map(w=>Dn(a,w.inf)),sd2:g.map(w=>Eo(w.inf))}}:{}}}return zc.set(d,{time:a.time,delta:a.delta}),d}function mn(e,t){if(e===null||typeof e!="object"||!("competingRisks"in e))throw new TypeError("'fit' must be a result of timeROC");if(e.inference===void 0)throw new RangeError("the fit has no iid representation: call timeROC with iid: true (marginal weights)");return e.competingRisks?t===1?{auc:e.auc1,iid:e.inference.iid1,sd:e.inference.sd1}:{auc:e.auc2,iid:e.inference.iid2,sd:e.inference.sd2}:{auc:e.auc,iid:e.inference.iid,sd:e.inference.sd}}function Kc(e,t,n){let r=re((1+t)/2),o=e[0].iid[0].length,s=e.map(()=>new Array(n)),i=new Array(o);for(let a=0;a<n;a++){for(let c=0;c<o;c++)i[c]=$l();e.forEach((c,l)=>{let f=-1/0;for(let m=0;m<c.auc.length;m++){let u=c.iid[m],h=0;for(let d=0;d<o;d++)h+=i[d]*u[d];let p=Math.abs(h/(o*c.sd[m]));p>f&&(f=p)}s[l][a]=f})}return e.map((a,c)=>{let l=io(s[c],t);return{ci:a.auc.map((f,m)=>[100*(f-r*a.sd[m]),100*(f+r*a.sd[m])]),band:a.auc.map((f,m)=>[100*(f-l*a.sd[m]),100*(f+l*a.sd[m])]),cAlpha:l}})}function Iv(e,t={}){z(t,wv,"timeROCConfint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be in (0, 1)");let r=t.nSim??2e3;if(!Number.isInteger(r)||r<1)throw new RangeError("'nSim' must be a positive integer");let o=mn(e,1);if(!e.competingRisks)return{times:e.times,level:n,auc:Kc([o],n,r)[0]};let[s,i]=Kc([o,mn(e,2)],n,r);return{times:e.times,level:n,auc1:s,auc2:i}}function $c(e,t,n){let r=e.auc.length,o=e.iid.map((h,p)=>h.map((d,g)=>d-t.iid[p][g])),s=o.map(Eo),i=e.auc.map((h,p)=>(h-t.auc[p])/s[p]),a=i.map(h=>2*oe(-Math.abs(h)));if(!n)return{pValues:a};let c=o[0].length,l=s.map(h=>h*Math.sqrt(c)),f=o.map(h=>h.reduce((p,d)=>p+d,0)/c),m=Array.from({length:r},(h,p)=>Array.from({length:r},(d,g)=>{let w=0;for(let N=0;N<c;N++)w+=(o[p][N]-f[p])*(o[g][N]-f[g]);return w/(c-1)/(l[p]*l[g])})),u=i.map(h=>{let p=Math.abs(h);return Number.isNaN(p)?NaN:_v(p,m)});return{pValues:a,adjusted:u,cor:m}}function kh(e,t,n={}){z(n,bv,"timeROCCompare");let r=n.adjusted??!1,o=zc.get(e),s=zc.get(t);if(mn(e,1),mn(t,1),e.n!==t.n||e.times.length!==t.times.length||e.times.some((i,a)=>i!==t.times[a])||e.competingRisks!==t.competingRisks||o&&s&&(o.time.some((i,a)=>i!==s.time[a])||o.delta.some((i,a)=>i!==s.delta[a])))throw new RangeError("the two fits must come from the same subjects and times");return e.competingRisks?{times:e.times,auc1:$c(mn(e,1),mn(t,1),r),auc2:$c(mn(e,2),mn(t,2),r)}:{times:e.times,auc:$c(mn(e,1),mn(t,1),r)}}function _v(e,t){let n=t.length;if(Number.isNaN(e))return NaN;if(e===1/0)return 0;if(!(e>0))return 1;let r=0;for(let o=1;o<=n;o++)r+=Cv(e,t,o);return Math.min(1,2*r)}function Cv(e,t,n){if(n===1)return Rs(-e,0,1,!0,!1);let r=(a,c)=>Math.max(-1,Math.min(1,t[a][c]));if(n===2)return lr([-e,e],[e,1/0],r(0,1));if(n===3){let a=r(0,2),c=r(1,2);if(Math.abs(a)<1-1e-9&&Math.abs(c)<1-1e-9){let l=Math.sqrt(1-a*a),f=Math.sqrt(1-c*c),m=Math.max(-1,Math.min(1,(r(0,1)-a*c)/(l*f)));return Fv(h=>Math.exp(-h*h/2)/Math.sqrt(2*Math.PI)*lr([(-e-a*h)/l,(-e-c*h)/f],[(e-a*h)/l,(e-c*h)/f],m),e,e+40)}}let o=[n-1,...Array.from({length:n-1},(a,c)=>c)],s=o.map(a=>a===n-1?e:-e),i=o.map(a=>a===n-1?1/0:e);return Pv(s,i,o.map(a=>o.map(c=>t[a][c])))}var Lv=[-.9739065285171717,-.8650633666889845,-.6794095682990244,-.4333953941292472,-.1488743389816312,.1488743389816312,.4333953941292472,.6794095682990244,.8650633666889845,.9739065285171717],Ov=[.0666713443086881,.1494513491505806,.219086362515982,.2692667193099963,.2955242247147529,.2955242247147529,.2692667193099963,.219086362515982,.1494513491505806,.0666713443086881];function Fv(e,t,n){let r=(l,f)=>{let m=(l+f)/2,u=(f-l)/2,h=0;for(let p=0;p<10;p++)h+=Ov[p]*e(m+u*Lv[p]);return h*u},o=[t];for(let l=1/64;o[o.length-1]<n;l*=2)o.push(Math.min(n,o[o.length-1]+l));let s=o.slice(1).map((l,f)=>({lo:o[f],hi:l,v:r(o[f],l)})),i=Math.abs(s.reduce((l,f)=>l+f.v,0)),a=0,c=s.map(l=>({...l,depth:0}));for(;c.length;){let l=c.pop(),f=(l.lo+l.hi)/2,m=r(l.lo,f),u=r(f,l.hi);Math.abs(m+u-l.v)<=1e-13*i||l.depth>=40?a+=m+u:c.push({lo:l.lo,hi:f,v:m,depth:l.depth+1},{lo:f,hi:l.hi,v:u,depth:l.depth+1})}return a}function Pv(e,t,n){let r=n.length,o=Array.from({length:r},()=>new Array(r).fill(0));for(let w=0;w<r;w++)for(let N=0;N<=w;N++){let b=n[w][N];for(let E=0;E<N;E++)b-=o[w][E]*o[N][E];w===N?o[w][w]=b>1e-12?Math.sqrt(b):0:o[w][N]=o[N][N]>0?b/o[N][N]:0}let s=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97],i=Array.from({length:Math.max(1,r-1)},(w,N)=>{let b=Math.sqrt(s[N%s.length]+100*Math.floor(N/s.length));return b-Math.floor(b)}),a=625341585,c=()=>(a^=a<<13,a^=a>>>17,a^=a<<5,(a>>>0)/4294967296),l=w=>w===-1/0?0:w===1/0?1:Rs(w,0,1,!0,!1),f=w=>w===-1/0?1:w===1/0?0:Rs(w,0,1,!1,!1),m=new Array(r),u=w=>{let N=1;for(let b=0;b<r;b++){let E=0;for(let S=0;S<b;S++)E+=o[b][S]*m[S];let v=o[b][b];if(v===0){if(!(E>=e[b]&&E<=t[b]))return 0;b<r-1&&(m[b]=0);continue}let y=(e[b]-E)/v,T=(t[b]-E)/v,M=y>0,R=M?f(y):l(y),x=M?f(T):l(T),A=M?R-x:x-R;if(N*=A,!(N>0))return 0;if(b<r-1){let S=Math.max(M?R-w[b]*A:R+w[b]*A,1e-300);m[b]=M?-Wa(S,0,1,!0,!1):Wa(Math.min(S,1-1e-16),0,1,!0,!1)}}return N},h=10,p=1009,d=NaN,g=new Array(r-1);for(let w=0;w<10;w++){let N=[];for(let v=0;v<h;v++){let y=i.map(()=>c()),T=0;for(let M=1;M<=p;M++){for(let R=0;R<r-1;R++){let x=(M*i[R]+y[R])%1;g[R]=Math.abs(2*x-1)}T+=u(g)}N.push(T/p)}let b=N.reduce((v,y)=>v+y,0)/h,E=Math.sqrt(N.reduce((v,y)=>v+(y-b)**2,0)/(h-1)/h);if(d=b,2*E<=1e-6*b||b===0)break;p*=2}return d}function Dv(e,t,n,r,o){if(z(o,Mv,"seSpPPVNPV"),typeof e!="number"||Number.isNaN(e))throw new RangeError("'cutpoint' must be a number");let s=xh(t,n,r,o,"seSpPPVNPV"),i=o.iid??!1;if(i&&s.weighting!=="marginal")throw new RangeError("weighting must be marginal for computing the iid representation (as R)");let{data:a,competing:c}=s,l=a.marker.map(d=>d>e?1:0),m=Sh(s).map(d=>{let g=q=>d.role.map(($,V)=>q($,V)?1:0),w=q=>q===2,N=q=>q===2||q===3,b=q=>yo(a,d,q),E=b(g((q,$)=>q===1&&l[$]===1)),v=b(g(q=>q===1)),y=b(g((q,$)=>q===1&&l[$]===0)),T=b(g((q,$)=>w(q)&&l[$]===1)),M=b(g(w)),R=b(g((q,$)=>w(q)&&l[$]===0)),x=b(g((q,$)=>N(q)&&l[$]===1)),A=b(g(N)),S=b(g((q,$)=>N(q)&&l[$]===0)),I=(q,$)=>({value:q.value+$.value,inf:q.inf.map((V,P)=>V+$.inf[P])}),C={value:E.value/v.value,inf:Mo(E,v)},_=T.value/M.value,F=x.value/A.value,O=Mo(T,M).map(q=>-q),D=Mo(x,A).map(q=>-q),k=I(E,x),L=I(S,y);return{tp:C.value,fp1:_,fp2:F,ppv:E.value/k.value,npv1:R.value/L.value,npv2:S.value/L.value,inf:{se:C.inf,sp1:O,sp2:D,ppv:Mo(E,k),npv1:Mo(R,L),npv2:Mo(S,L)},stats:{...Ah(d,c),positive:l.reduce((q,$)=>q+$,0),negative:l.length-l.reduce((q,$)=>q+$,0)}}}),u=["se","sp1","sp2","ppv","npv1","npv2"],h=i?{se:Object.fromEntries(u.map(d=>[d,m.map(g=>Eo(g.inf[d]))])),iid:Object.fromEntries(u.map(d=>[d,m.map(g=>Dn(a,g.inf[d]))]))}:void 0,p=s.cond===null?{method:"marginal",ipcwTimes:s.times.map(d=>Wc(a,d).Gt),ipcwSubjectTimes:Dn(a,a.ti.map(d=>d>0?a.G[d-1]:1))}:{method:s.weighting,ipcwTimes:s.cond.at.map(d=>Dn(a,d)),ipcwSubjectTimes:Dn(a,s.cond.left)};return{cutpoint:e,times:s.times,n:a.n,competingRisks:c,tp:m.map(d=>d.tp),...c?{fp1:m.map(d=>d.fp1),fp2:m.map(d=>d.fp2)}:{fp:m.map(d=>d.fp1)},ppv:m.map(d=>d.ppv),...c?{npv1:m.map(d=>d.npv1),npv2:m.map(d=>d.npv2)}:{npv:m.map(d=>d.npv1)},stats:m.map(d=>d.stats),weights:p,...h?{inference:h}:{}}}function Vv(e,t={}){z(t,vv,"timeROCAucCurve");let n=t.fp??1;if(n!==1&&n!==2)throw new RangeError("'fp' must be 1 or 2");let r=t.level??.95;if(!(r>0&&r<1))throw new RangeError("'level' must be in (0, 1)");let o=t.nSim??2e3;if(!Number.isInteger(o)||o<1)throw new RangeError("'nSim' must be a positive integer");let s=mn(e,n),i=Kc([s],r,o)[0];return{times:e.times,auc:s.auc,lower:i.ci.map(a=>a[0]/100),upper:i.ci.map(a=>a[1]/100),bandLower:i.band.map(a=>a[0]/100),bandUpper:i.band.map(a=>a[1]/100)}}function $v(e,t,n={}){z(n,Nv,"timeROCAucDiff");let r=n.fp??1;if(r!==1&&r!==2)throw new RangeError("'fp' must be 1 or 2");let o=n.level??.95;if(!(o>0&&o<1))throw new RangeError("'level' must be in (0, 1)");kh(e,t);let s=mn(e,r),i=mn(t,r),a=re((1+o)/2),c=s.auc.map((f,m)=>f-i.auc[m]),l=s.iid.map((f,m)=>Eo(f.map((u,h)=>u-i.iid[m][h])));return{times:e.times,diff:c,lower:c.map((f,m)=>f-a*l[m]),upper:c.map((f,m)=>f+a*l[m])}}function Th(e,t,n,r,o){let s=0;for(let c of n)r(c)&&s++;let i=1,a=0;for(;a<n.length&&e[n[a]]<=o;){let c=e[n[a]],l=0,f=0;for(;a<n.length&&e[n[a]]===c;a++){let m=n[a];r(m)&&(t[m]===1&&l++,f++)}s>0&&l>0&&(i*=1-l/s),s-=f}return i}function zv(e,t,n,r){if(z(r,yv,"survivalROC"),r===null||typeof r!="object")throw new TypeError("survivalROC: 'options' with 'predictTime' is required");if((r.method??"KM")!=="KM")throw new RangeError('only method "KM" is offered (the nearest-neighbour estimator "NNE" is not)');let o=Rh(e,t,n),s=o.map(w=>e[w]),i=o.map(w=>t[w]),a=o.map(w=>n[w]),c=s.length;if(c===0)throw new RangeError("no subjects (without NA)");for(let w of i)if(w!==0&&w!==1)throw new RangeError("'status' must be 0 or 1");let l=r.predictTime;if(typeof l!="number"||!Number.isFinite(l))throw new RangeError("'predictTime' must be a finite number");let f=r.cutValues===void 0?[...new Set(a)].sort((w,N)=>w-N):Rr(r.cutValues,"cutValues").sort((w,N)=>w-N),m=Array.from({length:c},(w,N)=>N).sort((w,N)=>s[w]-s[N]),u=Th(s,i,m,()=>!0,l),h=[-1/0,...f],p=[],d=[];for(let[w,N]of h.entries()){if(w===h.length-1&&h.length>2){p.push(0),d.push(0);break}let b=T=>a[T]>N,E=a.reduce((T,M)=>T+(M>N?1:0),0),v=E/c,y=E>0?Th(s,i,m,b,l)*v:0;p.push((v-y)/(1-u)),d.push(y/u)}let g=0;for(let w=0;w+1<d.length;w++)g+=(d[w]-d[w+1])*(p[w]+p[w+1])/2;return{cutValues:h,tp:p,fp:d,predictTime:l,survival:u,auc:g}}var Kv=["method","confLevel","prevalence"];function Yv(e,t={}){z(t,Kv,"diagnosticTests");let n=Te(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(Ct(r),!Array.isArray(e)||e.length!==2||e.some(v=>!(Array.isArray(v)||ArrayBuffer.isView(v))||v.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(v=>typeof v!="number"))throw new TypeError("the counts must be numbers");if(!o.every(Tn))throw new RangeError("the counts must be nonnegative integers");let[s,i,a,c]=o.map(Math.round),l=s+a,f=i+c,m=s+i,u=a+c,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=(v,y)=>{let T=Ke(()=>Si(v,y,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:T.estimate,confInt:T.confInt}},d=1-r,g=d/2<1e-8?re(d/2,{lowerTail:!1}):re(1-d/2),w=(v,y)=>{let T=g*Math.sqrt(y);return{estimate:v,confInt:[Math.exp(Math.log(v)-T),Math.exp(Math.log(v)+T)]}},N=s/l,b=c/f,E={sensitivity:p(s,l),specificity:p(c,f),ppv:p(s,m),npv:p(c,u),lrPos:w(i===0?N/0:wo(BigInt(s)*BigInt(f),BigInt(l)*BigInt(i)),a/(s*l)+c/(i*f)),lrNeg:w(c===0?a/l/b:wo(BigInt(a)*BigInt(f),BigInt(l)*BigInt(c)),s/(a*l)+i/(c*f)),accuracy:p(s+c,h),diagnosticOR:w(s*c/(i*a),1/s+1/i+1/a+1/c),prevalence:p(l,h),apparentPrevalence:p(m,h),method:n,confLevel:r,warnings:[]};if(t.prevalence!==void 0){let v=t.prevalence;if(!(typeof v=="number"&&v>0&&v<1))throw new RangeError("'prevalence' must be a number in (0, 1)");let y=v<2**-500?2**600:1,T=N*(v*y),M=i/f*((1-v)*y),R=b*(1-v),x=a/l*v;E.ppvAtPrevalence=T/(T+M),E.npvAtPrevalence=R/(R+x)}return J("diagnosticTests",E)}var Wv=["two","mode","confLevel","ciMethod"],jv=["package","exact"],Vn=(e,t)=>t===0?0:e*t;function Bv(e,t,n={}){if(z(n,Wv,"blandAltman"),e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let r=n.two??1.96;if(!(typeof r=="number"&&r>0&&r<1/0))throw new RangeError("'two' must be a positive number");let o=n.mode??1;if(o!==1&&o!==2)throw new RangeError("'mode' must be 1 or 2");let s=n.confLevel??.95;fn(s);let i=Te(n.ciMethod,jv),a=[],c=[];for(let O=0;O<e.length;O++){let D=e[O],k=t[O];if(typeof D!="number"||typeof k!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(D)||Number.isNaN(k))){if(!Number.isFinite(D)||!Number.isFinite(k))throw new RangeError("'x' and 'y' must not contain infinite values");a.push(D),c.push(k)}}let l=a.length;if(l<2)throw new RangeError("fewer than 2 complete pairs");let[f,m]=o===1?[a,c]:[c,a],u=a.map((O,D)=>{let k=(O+c[D])/2;return Number.isFinite(k)?k:O/2+c[D]/2}),h=f.map((O,D)=>O-m[D]),p=!h.every(Number.isFinite),{groups:d,k:g}=Re([p?f.map((O,D)=>O/2-m[D]/2):h]),w=d[0],N=g+(p?1:0),b=ct(w),E=ir(w),v=r*E,y=b-v,T=b+v,M=l-1,R=(1-s)/2,x=it(R,M,!0,!1),A=R<1e-8?it(R,M,!1,!1):it((s+1)/2,M,!0,!1),S=[b+Vn(x,E)/Math.sqrt(l),b+Vn(A,E)/Math.sqrt(l)],I,C;if(i==="package"){let O=3*E*E/l,D=O>=2**-1022||O===0?Math.sqrt(O):E*Math.sqrt(3/l);I=[y+Vn(x,D),y+Vn(A,D)],C=[T+Vn(x,D),T+Vn(A,D)]}else{let O=r*Math.sqrt(l),D=Ba(R,M,O,!0,!1)/Math.sqrt(l),k=Ba(R,M,O,!1,!1)/Math.sqrt(l);I=[b-Vn(k,E),b-Vn(D,E)],C=[b+Vn(D,E),b+Vn(k,E)]}let _=O=>W(O,N),F=O=>[_(O[0]),_(O[1])];return J("blandAltman",{means:u,diffs:h,n:l,meanDiff:_(b),lowerLimit:_(y),upperLimit:_(T),criticalDiff:_(v),meanDiffConfInt:F(S),lowerLimitConfInt:F(I),upperLimitConfInt:F(C),confLevel:s,two:r,mode:o,ciMethod:i,warnings:[]})}var Uv=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),Gv=["PaBa","Deming","WDeming"],Hv=["analytical","jackknife","bootstrap","nestedbootstrap"],Qv=["quantile","Student","BCa","tBoot"],qh=5793,Xv=2**27;function Uc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function Zv(e,t){let n=ar(e),r=ar(t),o=[0,0],s=[0,0],i=[0,0];for(let a=0;a<e.length;a++){let c=Le([e[a],0],n),l=Le([t[a],0],r);o=he(o,_e(c,c)),s=he(s,_e(l,l)),i=he(i,_e(c,l))}return{u:o,q:s,p:i,mx:n,my:r}}function Lh(e,t,n,r){if(n===0)return NaN;if(r<=1){let i=r*t-e,a=Math.sqrt(i*i+4*r*n*n);return i>=0?(i+a)/(2*r*n):2*n/(a-i)}let o=t-e/r,s=Math.sqrt(o*o+4*n*n/r);return o>=0?(o+s)/(2*n):2*n/r/(s-o)}function Gc(e,t,n){let r=e.length,{u:o,q:s,p:i,mx:a,my:c}=Zv(e,t),l=Lh(o[0],s[0],i[0],n),f=Le(c,_e([l,0],a))[0],m=_e(o,s),u=i[0]*i[0]/(o[0]*s[0]),h=Le(m,_e(i,i))[0]/(m[0]+m[1]);Math.abs(h)<1e-12*(1+u)/2&&(h=0);let p=Math.sqrt(l*l*h/u/(r-2)),d=[0,0];for(let w of e)d=he(d,_e([w,0],[w,0]));let g=Math.sqrt(p*p*ht(d,r)[0]);return{b0:f,b1:l,se0:g,se1:p,xmean:a[0]}}function Ih(e,t,n,r,o,s,i,a,c){let l=e.length,f=Gc(e,t,n),m=f.b0,u=f.b1,h=NaN,p=new Array(l),d=s;for(let v=0;v<s;v++){let y=1+n*u*u,T=1/0;for(let k=0;k<l;k++){let L=t[k]-(m+u*e[k]),q=e[k]+n*u*L/y,$=t[k]-L/y;p[k]=r>1?q/r+$:q+r*$,T=Math.min(T,Math.abs(p[k]))}let M=[0,0],R=[0,0],x=[0,0];for(let k=0;k<l;k++){let L=T/p[k],q=[L*L,0];M=he(M,q),R=he(R,_e(q,[e[k],0])),x=he(x,_e(q,[t[k],0]))}let A=ht(R,M[0]+M[1]),S=ht(x,M[0]+M[1]),I=[0,0],C=[0,0],_=[0,0];for(let k=0;k<l;k++){let L=ht(Le([e[k],0],A),p[k]),q=ht(Le([t[k],0],S),p[k]);I=he(I,_e(L,L)),C=he(C,_e(q,q)),_=he(_,_e(L,q))}let F=Lh(I[0],C[0],_[0],n),O=Le(S,_e([F,0],A))[0];h=A[0];let D=W(Math.abs(u-F),c-a)<i&&W(Math.abs(m-O),c)<i;if(m=O,u=F,D){d=v+1;break}}let[g,w]=jc(o),[N,b]=jc(1+o),E=p.map(v=>{if(v===0)return 1/0;let[y,T]=jc(Math.abs(v));return r>1?tt((N/(g*y))**2,2*(b-w-T-c)):tt((N/y)**2,2*(b-T-a))});return{b0:m,b1:u,xmean:h,weights:E,converged:d<s}}function jc(e){let t=Math.floor(Math.log2(e));return[tt(e,-t),t]}var xo=e=>Math.atan(e),Ro=e=>Number.isFinite(e)?e:Math.tan(xo(e));function Jv(e,t){let n=e.length,r=0;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++)r+=Math.sign(e[u]-e[m])*Math.sign(t[u]-t[m]);let o=r>=0,s=new Float64Array(n*(n-1)/2),i=0,a=0,c=0,l=Math.PI/4;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++){let h=Uc(e[u],e[m]),p=Uc(t[u],t[m]),d;if(h!==0)d=p/h;else if(p!==0)d=o?1/0:-1/0;else continue;s[i++]=d;let g=xo(d);o?g===-l?a++:g<-l&&c++:g===l?a++:g>l&&c++}return{sorted:s.subarray(0,i).sort(),offset:o?2*c+a:-(2*c+a),e:a}}function eN(e,t,n){let r=e.length;if(t%2!==0){let a=(t+1)/2;return a>=1&&a<=r?Ro(e[a-1]):NaN}let o=t/2;if(o<1||o+1>r)return NaN;let s=e[o-1],i=e[o];return n?(Ro(s)+Ro(i))/2:Math.tan((xo(s)+xo(i))/2)}function tN(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 Hc(e,t,n){return Number.isFinite(n)?tN(e.map((r,o)=>Uc(t[o],n*r))):-n}function _h(e,t,n){let r=Jv(e,t),o=eN(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:Hc(e,t,o),b1:o,slopes:r}}function nN(e,t,n,r,o){let s=e.length,{sorted:i,offset:a,e:c}=n,l=i.length,f=Math.round(r*Math.sqrt(s*(s-1)*(2*s+5)/18)),m=(p,d)=>{let g=Math.trunc((p+1)/2);if(d?g<2*c+1:g>l)return NaN;if(p%2===0){if(g+1>l)return NaN;let w=i[g-1],N=i[g];return o?(Ro(w)+Ro(N))/2:Math.tan((xo(w)+xo(N))/2)}return Ro(i[g-1])},u=m(l-f+a,!0),h=m(l+f+a,!1);return Number.isNaN(u)||Number.isNaN(h)?{slope:[-1/0,1/0],intercept:[-1/0,1/0],defined:!1}:{slope:[u,h],intercept:[Hc(e,t,h),Hc(e,t,u)],defined:!0}}function rN(e,t,n,r){let o=e.length,s=e.map((p,d)=>t[d]-n-r*p),i=s.filter(p=>p>0).length,a=s.filter(p=>p<0).length,c=Math.sqrt(a/i),l=-Math.sqrt(i/a),f=e.map((p,d)=>(t[d]+p/r-n)/Math.sqrt(1+1/(r*r))),m=Array.from({length:o},(p,d)=>d).sort((p,d)=>f[p]-f[d]||p-d),u=0,h=0;for(let p of m){let d=s[p];u+=d>0?c:d<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:i,nNeg:a,maxCusum:h}}var Ca=e=>e.every(t=>t===e[0]);function Bc(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var To=(e,t)=>io(e,t),Ch=e=>ir(e.filter(t=>!Number.isNaN(t)));function oN(e,t,n={}){z(n,Uv,"mcreg");let r=Te(n.methodReg,Gv),o=Te(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),Hv),s=o==="bootstrap"||o==="nestedbootstrap",i=Te(n.methodBootstrapCi,Qv),a=n.confLevel??.95;if(!(typeof a=="number"&&a>0&&a<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let l=Te(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=Bc(n.nsamples??999,"nsamples"),h=Bc(n.nnested??25,"nnested"),p=Bc(n.iterMax??30,"iterMax"),d=n.threshold??1e-6;if(!(typeof d=="number"&&d>=0&&d<1/0))throw new RangeError("'threshold' must be a nonnegative number");if(r==="WDeming"&&o==="analytical")throw new RangeError('there is no analytical interval for weighted Deming regression (use methodCi "jackknife" or a bootstrap)');if(o==="bootstrap"&&i==="tBoot"&&r!=="Deming")throw new RangeError(`the bootstrap-t interval of ${r} needs methodCi "nestedbootstrap" (there is no analytical standard error)`);if(e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let g=[],w=[];for(let H=0;H<e.length;H++){let j=e[H],te=t[H];if(typeof j!="number"||typeof te!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(j)||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(j)||!Number.isFinite(te))throw new RangeError("'x' and 'y' must not contain infinite values");g.push(j),w.push(te)}let N=g.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(Ca(g)||Ca(w))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(g.some(j=>j<0)||w.some(j=>j<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>qh)throw new RangeError(`Passing-Bablok regression is limited to ${qh} points (2^24 pairwise slopes)`);if((1+(o==="jackknife"||s&&i==="BCa"?N:0)+(s?u*(1+(o==="nestedbootstrap"?h:0)):0))*(N*(N-1)/2)>Xv)throw new RangeError("the Passing-Bablok fits would take more than 2^27 pairwise slopes in all (reduce nsamples or nnested)")}if(r==="WDeming"&&(g.some(H=>H<=0)||w.some(H=>H<=0)))throw new RangeError("weighted Deming regression needs positive values");let b=Re([g,w]),[E,v]=f?[Re([g]).k,Re([w]).k]:[b.k,b.k],y=f?g.map(H=>W(H,-E)):b.groups[0],T=f?w.map(H=>W(H,-v)):b.groups[1],M=Math.min(Math.max(tt(m,2*(v-E)),Number.MIN_VALUE),2**1e3),R=tt(m,v-E),x=new Set,A=`no konvergence after ${p} iterations`,S=(H,j)=>{if(r==="Deming")return Gc(H,j,M);if(r==="WDeming"){let te=Ih(H,j,M,R,m,p,d,E,v);return te.converged||x.add(A),te}return _h(H,j,l)},I,C=NaN,_,F,O;if(r==="Deming"){let H=Gc(y,T,M);I=H,C=W(H.xmean,E),F=[H.se0,H.se1]}else if(r==="WDeming"){let H=Ih(y,T,M,R,m,p,d,E,v);H.converged||x.add(A),I=H,C=W(H.xmean,E),_=H.weights}else{let H=_h(y,T,l);I=H,O=H.slopes}if(!Number.isFinite(I.b1))throw new RangeError(r==="PaBa"?"the Passing-Bablok slope is not defined for these data":"the covariance of x and y is 0: the Deming slope is not defined");let D=1-a,k=it(D/2,N-2,!1,!1),L=re(D/2,{lowerTail:!1}),q=[I.b0,I.b1],$=[NaN,NaN],V=[[NaN,NaN],[NaN,NaN]],P,Q,K,X=()=>{let H=[],j=[];for(let te=0;te<N;te++){let B=S(y.filter((Z,U)=>U!==te),T.filter((Z,U)=>U!==te));H.push(B.b0),j.push(B.b1)}P=[H,j],Q=[(N-1)*ir(H)/Math.sqrt(N),(N-1)*ir(j)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let H of[0,1])$[H]=F[H],V[H]=[q[H]-k*$[H],q[H]+k*$[H]];else{let H=nN(y,T,O,L,l);H.defined||x.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),V[0]=H.intercept,V[1]=H.slope}else if(o==="jackknife"){X();for(let H of[0,1])$[H]=Q[H],V[H]=[q[H]-k*$[H],q[H]+k*$[H]]}else{if(i==="BCa"&&(X(),P.some(pe=>pe.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let H=r==="Deming"?F:r==="WDeming"&&Q!==void 0?Q:[NaN,NaN],j=o==="nestedbootstrap",te=[],B=[],Z=[],U=[],ee=0,ae=()=>Array.from({length:N},()=>Yo(N)),ve=pe=>{let Ce=pe.map(ye=>y[ye]),be=pe.map(ye=>T[ye]);if(Ca(Ce)||Ca(be)){if(r==="PaBa")return x.add("Resampling PaBa regression: x or y coordinates of all data points in subsample identical - regression coefficients undetermined!"),{b0:NaN,b1:NaN};x.add("Resampling Deming regression: all x coordinates in subsample identical - regression coefficients undetermined!")}return S(Ce,be)},Ne=()=>{for(let pe=0;pe<u;pe++){let Ce=ae(),be=ve(Ce),ye;if(j){let Se=[],Ye=[];for(let De=0;De<h;De++){let Je=ve(ae().map(Be=>Ce[Be]));Se.push(Je.b0),Ye.push(Je.b1)}ye=[Ch(Se),Ch(Ye)]}else r==="Deming"?ye=[be.se0,be.se1]:ye=H;if(Number.isNaN(be.b0)||Number.isNaN(be.b1)){ee++;continue}te.push(be.b0),B.push(be.b1),Z.push(ye[0]),U.push(ye[1])}};if(n.rngSeed!==void 0){let pe=Ol()?Dl():void 0;try{Pl({kind:"Mersenne-Twister"}),Fl(n.rngSeed),Ne()}finally{pe!==void 0&&Vl(pe)}}else Ne();if(ee>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let ue=te.length,fe=[te,B],Ee=[Z,U];for(let pe of[0,1]){let Ce=fe[pe],be=q[pe];if(i==="quantile")V[pe]=[To(Ce,D/2),To(Ce,1-D/2)];else if(i==="Student")$[pe]=ir(Ce),V[pe]=[be-k*$[pe],be+k*$[pe]];else if(i==="BCa"){let ye=Ce.filter(le=>le<be).length;Ce.every(le=>le===be)?ye=ue/2:ye===0?ye=.5:ye===ue&&(ye=ue-.5);let Se=re(ye/ue),Ye=P[pe],De=Ye.reduce((le,et)=>le+et,0)/Ye.length,Je=0,Be=0;for(let le of Ye){let et=De-le;Je+=et*et,Be+=et*et*et}let Xe=Je===0?0:Be/(6*Je**1.5),me=-L,ne=L,we=oe(Se-(me-Se)/(1+Xe*(me-Se))),ge=oe(Se-(ne-Se)/(1+Xe*(ne-Se)));V[pe]=[To(Ce,ge),To(Ce,we)]}else{let ye=Ce.map((Ye,De)=>(Ye-be)/Ee[pe][De]),Se=Number.isNaN(H[pe])?ir(Ce):H[pe];$[pe]=Se,V[pe]=[be-To(ye,1-D/2)*Se,be-To(ye,D/2)*Se]}}K={intercept:te.map(pe=>W(pe,v)),slope:B.map(pe=>W(pe,v-E)),sigmaIntercept:Z.map(pe=>W(pe,v)),sigmaSlope:U.map(pe=>W(pe,v-E)),globalSigma:[W(H[0],v),W(H[1],v-E)],nsamples:ue}}let Y=H=>{let j=te=>W(te,H===0?v:v-E);return{estimate:j(q[H]),se:j($[H]),confInt:[j(V[H][0]),j(V[H][1])]}},se={methodReg:r,methodCi:o,...s?{methodBootstrapCi:i}:{},confLevel:a,errorRatio:m,n:N,intercept:Y(0),slope:Y(1),xmean:C,..._===void 0?{}:{weights:_},...P===void 0?{}:{jackknife:{intercept:P[0].map(H=>W(H,v)),slope:P[1].map(H=>W(H,v-E))}},...K===void 0?{}:{bootstrap:K},...r==="PaBa"?{cusum:rN(y,T,I.b0,I.b1)}:{},warnings:[...x]};return J("mcreg",se)}var sN=["alpha","confLevel","lmer"],aN={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 La(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function iN(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,i]=La(r,n[0]),[a,c]=La(t[o],t[0]);return s===a&&i===c}))}var Oh=(e,t)=>e.every((n,r)=>n===t[r]);function Ur(e){let t=[0,0];for(let n of e)t=he(t,n);return ht(t,e.length)}var So=(e,t)=>({v:ht(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:tt(r.v[0],r.e-n),E:n}))}function Fh(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=>[tt(o.v[0],o.e-n),tt(o.v[1],o.e-n)];return{v:he(r(e),r(t)),e:n}}var Ph=(e,t)=>tt(e.v[0]/t.v[0],e.e-t.e),cN=(e,t)=>{let[n,r]=Ao(e,t);return n.at<r.at},lN=(e,t)=>{let[n,r]=Ao(e,t);return n.at<=r.at};function Dh(e,t,n){let r=[0,0];for(let o of e){let s=Le(o,t);r=he(r,_e(s,s))}return _e(r,[n,0])}function fN(e,t={}){z(t,sN,"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(ce=>Array.isArray(ce)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(ce=>ce.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(ce=>ce.some(xe=>typeof xe!="number")))throw new TypeError("'x' must contain numbers");if(e.some(ce=>ce.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(ce=>ce.some(xe=>!Number.isFinite(xe))))throw new RangeError("'x' contains infinite ratings");let i=e.length;if(i<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let{groups:a}=Re(e);if(a.every(ce=>Oh(ce,a[0])))throw new RangeError(a[0].every(ce=>ce===a[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let c=Array.from({length:s},(ce,xe)=>a.map(de=>de[xe])),l=0;for(let ce of e)for(let xe of ce)l=Math.max(l,Math.abs(xe));let f=l>2**1020?3:0,m=f>0?e.map(ce=>ce.map(xe=>xe/8)):e,u=m.map(ce=>ce.map((xe,de)=>La(xe,m[0][de]))),h=m.map(ce=>ce.map(xe=>La(xe,ce[0]))),p=h.map(ce=>ce.map((xe,de)=>Le(xe,h[0][de]))),d=ce=>{let xe=0;for(let de of ce)for(let Ae of de)xe=Math.max(xe,Math.abs(Ae[0]));return xe===0?0:Math.floor(Math.log2(xe))},g=(ce,xe)=>ce.map(de=>de.map(Ae=>[W(Ae[0],-xe),W(Ae[1],-xe)])),[w,N,b]=[d(u),d(h),d(p)],[E,v,y]=[g(u,w),g(h,N),g(p,b)],T=ce=>Array.from({length:s},(xe,de)=>ce.map(Ae=>Ae[de])),M=Dh(E.map(Ur),Ur(E.flat()),s),R=c.every(ce=>Oh(ce,c[0]))?[0,0]:Dh(T(v).map(Ur),Ur(v.flat()),i),x=[0,0];if(!iN(a)){let ce=y.map(Ur),xe=T(y).map(Ur),de=Ur(y.flat());y.forEach((Ae,We)=>Ae.forEach((Fe,rt)=>{let dt=he(Le(Le(Fe,ce[We]),xe[rt]),de);x=he(x,_e(dt,dt))}))}let A=(ce,xe)=>({v:ce,e:2*(xe+f)}),S=A(M,w),I=A(R,N),C=A(x,b),_=i-1,F=s-1,O=(i-1)*(s-1),D=So(S,_),k=So(I,F),L=So(C,O),q=So(Fh(I,C),F+O),$=[];if(n){let ce={B:{ss:S,df:_,ms:D,pooled:!1},J:{ss:I,df:F,ms:k,pooled:!1}},xe=C,de=O;for(let We of lN(D,k)?["B","J"]:["J","B"]){let Fe=ce[We];cN(Fe.ms,So(xe,de))&&(xe=Fh(xe,Fe.ss),de+=Fe.df,Fe.pooled=!0)}let Ae=So(xe,de);if(ce.B.pooled&&(D=Ae),ce.J.pooled&&(k=Ae),L=Ae,ce.B.pooled){let[We,Fe]=Ao(L,k);q={v:[We.at+(Fe.at-We.at)/i,0],e:We.E}}}else x[0]===0&&$.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let V=(ce,xe)=>r/2<1e-8?pn(r/2,ce,xe,!1,!1):pn(1-r/2,ce,xe,!0,!1),P=(ce,xe,de)=>Nt(ce,xe,de,!1,!1),Q=ce=>ce===1/0?1:(ce-1)/(ce+s-1),K=ce=>1-1/ce,X=i*(s-1),Y=Ph(D,q),se=Ph(D,L),[H,j]=[Y/V(_,X),Y*V(X,_)],[te,B]=[se/V(_,O),se*V(O,_)],[Z,U]=Ao(D,q).map(ce=>ce.at),[ee,ae]=Ao(D,L).map(ce=>ce.at),[ve,Ne,ue]=Ao(D,k,L).map(ce=>ce.at),fe=(Z-U)/(Z+(s-1)*U),Ee=(ve-ue)/(ve+(s-1)*ue+s*(Ne-ue)/i),pe=(ee-ae)/(ee+(s-1)*ae),Ce=s*Ee*Ne,be=(i*(1+(s-1)*Ee)-s*Ee)*ue,ye=Math.max(Math.abs(Ce),Math.abs(be)),Se=ye>0&&Number.isFinite(ye)?Math.floor(Math.log2(ye)):0,Ye=W(Ce,-Se),De=W(be,-Se),Je=(i-1)*Ye*Ye+De*De,Be=Je===0?s-1:(s-1)*(i-1)*(Ye+De)**2/Je,Xe=V(_,Be),me=V(Be,_),ne=s*Ne+(s*i-s-i)*ue,we=(ce,xe)=>ce>.5?1-(i*ue+ne)/xe:ce,ge=we(i*(ve/Xe-ue)/(ne+i*ve/Xe),ne+i*ve/Xe),le=me>1?we(i*(ve-ue/me)/(ne/me+i*ve),ne+i*me*ve):we(i*(me*ve-ue)/(ne+i*me*ve),ne+i*me*ve),et=ce=>ce*s/(1+ce*(s-1)),Oe=P(Y,_,X),nt=P(se,_,O),ke=(ce,xe,de,Ae,We,Fe,rt)=>({type:ce,name:aN[ce],ICC:xe,F:de,df1:_,df2:Ae,pValue:We,lower:Fe,upper:rt}),$e=[ke("ICC1",fe,Y,X,Oe,Q(H),Q(j)),ke("ICC2",Ee,se,O,nt,ge,le),ke("ICC3",pe,se,O,nt,Q(te),Q(B)),ke("ICC1k",(Z-U)/Z,Y,X,Oe,K(H),K(j)),ke("ICC2k",(ve-ue)/(ve+(Ne-ue)/i),se,O,nt,et(ge),et(le)),ke("ICC3k",(ee-ae)/ee,se,O,nt,K(te),K(B))],He=ce=>tt(ce.v[0],ce.e);return J("ICC",{results:$e,MSB:He(D),MSJ:He(k),MSE:He(L),MSW:He(q),n:i,k:s,confLevel:o,lmer:n,warnings:$})}function Oa(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(!of(Ql(e)))throw new RangeError("'covmat' must be symmetric")}var mN=["covmat","nObs","rotation","scores","control"],uN=["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 Qc=(e,t)=>e[0].map((n,r)=>At(e.map(o=>t(o[r]))));function zh(e,t){let n=t.map(r=>1/Math.sqrt(r));return e.map((r,o)=>r.map((s,i)=>n[o]*s*n[i]))}function Vh(e,t,n){let r=wn(zh(e,t),{symmetric:!0}),o=r.values.slice(0,n).map(s=>Math.sqrt(Math.max(s-1,0)));return r.vectors.map((s,i)=>s.slice(0,n).map((a,c)=>Math.sqrt(t[i])*(a*o[c])))}function $h(e,t){let n=Qc(e,a=>a*a),r=n.map((a,c)=>c).sort((a,c)=>n[c]-n[a]||a-c),o=e.map(a=>r.map(c=>a[c])),s=t?.map(a=>r.map(c=>a[c])),i=Qc(o,a=>a).map(a=>a<0?-1:1);return o=o.map(a=>a.map((c,l)=>i[l]<0?-c:c)),s=s?.map(a=>a.map((c,l)=>i[l]<0?-c:c)),s===void 0?{L:o}:{L:o,rot:s}}var $n=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let i=0;i<n.length;i++)s+=n[i]*t[i][o];return s})),ko=e=>e[0].map((t,n)=>e.map(r=>r[n]));function Fa(e){let t=e[0].length,n=e.map(m=>Math.sqrt(At(m.map(u=>u*u)))),r=e.flatMap((m,u)=>n[u]>0?[u]:[]),o=r.map(m=>e[m].map(u=>u/n[m])),s=o.length,i=Array.from({length:t},(m,u)=>Array.from({length:t},(h,p)=>u===p?1:0));if(s===0)return{loadings:e.map(m=>m.slice()),rotmat:i};let a=ko(o),c=0;for(let m=0;m<1e3;m++){let u=$n(o,i),h=u[0].map((N,b)=>{let E=0;for(let v=0;v<s;v++)E+=u[v][b]*u[v][b];return E}),p=u.map(N=>N.map((b,E)=>b**3-b*h[E]/s)),d=$n(a,p),g=Us(d);i=$n(g.u,ko(g.v));let w=c;if(c=At(g.d),c<w*(1+1e-5))break}let l=$n(o,i),f=e.map(m=>m.map(()=>0));return r.forEach((m,u)=>f[m]=l[u].map(h=>h*n[m])),{loadings:f,rotmat:i}}function hN(e){let t=Fa(e),n=t.loadings,r=n.map(a=>a.map(c=>c*Math.abs(c)**3)),o=Xl(Yt(n),r),s=ot($n(ko(o),o)).map((a,c)=>a[c]),i=o.map(a=>a.map((c,l)=>c*Math.sqrt(s[l])));return{loadings:$n(n,i),rotmat:$n(t.rotmat,i)}}function pN(e,t,n={}){z(n,mN,"factanal"),z(_t(n,"control"),uN,"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");Oa(n.covmat),o=n.covmat.map(T=>T.slice()),s=n.nObs??NaN}else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");o=Xs(e),s=e.length}let i=o.length;if(i<3)throw new RangeError("factor analysis requires at least three variables");let a=t;if(!Number.isInteger(a)||a<1)throw new RangeError("'factors' must be a positive integer");let c=((i-a)**2-i-a)/2;if(c<0)throw new RangeError(`${a} ${a===1?"factor is":"factors are"} too many for ${i} variables`);let l=o.map((T,M)=>Math.sqrt(T[M])),f=o.map((T,M)=>T.map((R,x)=>R/(l[M]*l[x]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??ot(f).map((T,M)=>(1-a/(2*i))/T[M]),h=T=>{let M=wn(zh(f,T),{symmetric:!0,onlyValues:!0}).values.slice(a);return-(At(M.map(R=>Math.log(R)-R))-a+i)},p=T=>{let M=Vh(f,T,a);return T.map((R,x)=>{let A=0;for(let S=0;S<a;S++)A+=M[x][S]*M[x][S];return(A+R-f[x][x])/(R*R)})},d=Ke(()=>Qo(u,h,{gr:p,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(d.convergence!==0)throw new Error("unable to optimize from this starting value");let g=d.par,w=$h(Vh(f,g,a)).L,N,b=n.rotation??"varimax";if(b!=="varimax"&&b!=="promax"&&b!=="none")throw new RangeError(`unknown rotation '${String(b)}'`);if(b!=="none"&&a>=2){let T=b==="varimax"?Fa(w):hN(w),M=$h(T.loadings,T.rotmat);w=M.L,N=M.rot}let E=NaN,v=NaN;!Number.isNaN(s)&&c>0&&(E=(s-1-(2*i+5)/6-2*a/3)*d.value,v=Ze(E,c,{lowerTail:!1}));let y={uniquenesses:g,loadings:w,STATISTIC:E,dof:c,PVAL:v,converged:!0,objective:d.value,counts:d.counts,warnings:[]};return r!=="none"&&(y.scores=dN(e,f,g,w,r)),N!==void 0&&(y.rotmat=N),y}function dN(e,t,n,r,o){let s=e.length,i=e[0].map((m,u)=>At(e.map(h=>h[u]))/s),a=e.map(m=>m.map((u,h)=>u-i[h])),c=Qc(a,m=>m*m).map(m=>Math.sqrt(m/(s-1))),l=a.map(m=>m.map((u,h)=>u/c[h]));if(o==="regression")return $n(l,ot(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return $n(l,ko(ot($n(ko(r),f),ko(f))))}var gN=["nfactors","covmat"],qo=2**-52,wN=`
|
|
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
|
-
`,
|
|
19
|
-
`),mr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let q=$b(c);if(q.converged||mr(s,"oblimin did not converge in 2000 iterations"),a=q.loadings,l=q.phi,n===2){let P=l[0][1],B=Math.sqrt(Math.abs(P));f=[B,ca(P)*B],mr(i,Db)}else f=Ku(l,1,s,i).map(P=>P[0])}let m=a.map(q=>{let P=0;for(let B=0;B<n;B++)P+=q[B]*f[B];return P}),u=Wu(c,q=>q*q),h=u.map((q,P)=>r[P][P]-q),p=a.map((q,P)=>q.map((B,J)=>{if(n===1)return 0;let ce=r[P][P],W=ce-(ce-B*B)-(B*f[J])**2;return W<0&&(W=0),ca(B)*Math.sqrt(W)})),d=m.map((q,P)=>q*q/u[P]),g=m.map((q,P)=>{let B=[q,...p[P]];return St(B.map(J=>J*J))**2/St(B.map(J=>J**4))}),w=m.map(ca);w.some(q=>q<0)&&(r=r.map((q,P)=>q.map((B,J)=>w[P]*B*w[J])));let N=m.map((q,P)=>w[P]*q),v=p.map((q,P)=>q.map(B=>w[P]*B)),E=N.map((q,P)=>[q,...v[P],u[P],h[P],d[P],g[P]]),b=fo(r),M=St(r.map((q,P)=>q[P])),x=St(N)**2,y=St(h),R=x/b,T=(b-y)/b,S=x/(b-y),k=(b-M)/b*o/(o-1),L=(b+St(Yu(r,i))-M)/b,_=Go(E.map(q=>q.slice(0,n+1)),q=>q*q),I=_[0]/St(_),F=v.map(q=>{let P=0;for(let B=1;B<n;B++)Math.abs(q[B])>Math.abs(q[P])&&(P=B);return P}),C=[],D=[];for(let q=0;q<n;q++){let P=F.map((W,te)=>te).filter(W=>F[W]===q),B=St(P.flatMap(W=>P.map(te=>r[te][W]))),J=St(P.map(W=>v[W][q])),ce=St(P.map(W=>N[W]));D.push(J*J),C.push([(J*J+ce*ce)/B,ce*ce/B,J*J/B])}let A=[[T,St(N)**2/b,St(D)/b],...C],O={omegaH:R,omegaLim:S,alpha:k,omegaTot:T,G6:L,ECV:I,sl:E,slNames:["g",...Array.from({length:n},(q,P)=>`F${P+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:w,omegaGroup:A,warnings:s,messages:i};return l!==void 0&&(O.phi=l),H("omega",O)}var Bu=["ML","stdErr"],jb=["ML","stdErr"],tn=.9999;function bc(e){let t=Ie(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(oe(r/t));return n}var vc=e=>[...new Set(e)].sort((t,n)=>t-n),Uu=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Gu=e=>Array.isArray(e)||ArrayBuffer.isView(e),Hu=(e,t)=>e>0?ie(e,{lowerTail:!1})-ie(t,{lowerTail:!1}):ie(t)-ie(e);function Nc(e){let t=Re([e]).groups[0],n=st(t),r=t.map(s=>s-n),o=Math.sqrt(ot(r));return r.map(s=>s/o)}function Xu(e){if(Math.abs(e)>=.1)return e-Math.log1p(e);let t=e,n=0;for(let r=2;r<40;r++){t*=-e;let o=t/r;if(n+=o,Math.abs(o)<=1e-17*Math.abs(n))break}return-n}var la=e=>Array.from({length:e},()=>new Array(e).fill(0));function Ju(e,t,n,r,o){e.value+=t*Math.log(n);let s=r.length;for(let i=0;i<s;i++){e.gradient[i]+=t*r[i]/n;for(let c=0;c<s;c++)e.hessian[i][c]+=t*(o[i][c]/n-r[i]*r[c]/(n*n))}}var wc={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function Kb(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return wc;if(e===1/0)return{...wc,k:Xt(t),kk:-t*Xt(t)};if(t===1/0)return{...wc,h:Xt(e),hh:-e*Xt(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),s=e*e-2*n*e*t+t*t,i=Math.exp(-s/(2*r))/(2*Math.PI*o),c=(t-n*e)/o,a=(e-n*t)/o,l=Xt(e),f=Xt(t);return{r:i,h:l*ie(c),k:f*ie(a),rr:i*(n/r+(e*t*r-n*s)/(r*r)),rh:-i*(e-n*t)/r,rk:-i*(t-n*e)/r,hh:-e*l*ie(c)-n/o*l*Xt(c),hk:i,kk:-t*f*ie(a)-n/o*f*Xt(a)}}function Qu(e,t,n,r,o){let s=e.length,i=e[0].length,c=o?s+i-1:1,a={value:0,gradient:new Array(c).fill(0),hessian:la(c)},l=h=>o&&h>=1&&h<=s-1?h:-1,f=h=>o&&h>=1&&h<=i-1?s-1+h:-1,m=t.map(h=>n.map(p=>Kb(h,p,r))),u=[];for(let h=0;h<s;h++)for(let p=0;p<i;p++){let d=e[h][p];if(d===0)continue;let g=go([t[h],n[p]],[t[h+1],n[p+1]],r),w=new Array(c).fill(0),N=la(c);for(let[v,E,b]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let M=m[v][E],x=l(v),y=f(E);w[0]+=b*M.r,N[0][0]+=b*M.rr,x>=0&&(w[x]+=b*M.h,N[0][x]+=b*M.rh,N[x][0]+=b*M.rh,N[x][x]+=b*M.hh),y>=0&&(w[y]+=b*M.k,N[0][y]+=b*M.rk,N[y][0]+=b*M.rk,N[y][y]+=b*M.kk),x>=0&&y>=0&&(N[x][y]+=b*M.hk,N[y][x]+=b*M.hk)}Ju(a,d,g,w,N),u.push(d*Math.log(g))}return a.value=Ie(u),a}function Wb(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:la(s)},c=(1-r)*(1+r),a=Math.sqrt(c),l=c*a,f=new Array(s),m=la(s),u=[],h=(p,d,g)=>{let w=n[p],N=(w-r*d)/a,v=Xt(N),E=(r*w-d)/l,b=1/a,M=w/l+3*r*(r*w-d)/(l*c),x=r/l;if(f[0]+=g*v*E,m[0][0]+=g*v*(M-N*E*E),o){let y=p+1;f[y]+=g*v*b;let R=g*v*(x-N*E*b);m[0][y]+=R,m[y][0]+=R,m[y][y]+=g*v*(-N*b*b)}};for(let p=0;p<e.length;p++){let d=e[p],g=t[p];f.fill(0);for(let v of m)v.fill(0);let w=g<n.length?(n[g]-r*d)/a:1/0,N=g>0?(n[g-1]-r*d)/a:-1/0;g<n.length&&h(g,d,1),g>0&&h(g-1,d,-1),Ju(i,1,Hu(N,w),f,m),u.push(Math.log(Hu(N,w)),Xt(d,{log:!0}))}return i.value=Ie(u),i}function Yb(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(i=>e.gradient[i]),o=t.map(i=>t.map(c=>-e.hessian[i][c])),s;try{s=rt(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||Ie(s.map((i,c)=>i*r[c]))<=0)&&(s=r.map((i,c)=>i/Math.max(Math.abs(o[c][c]),1))),t.forEach((i,c)=>n[i]=s[c]),n}var eh=(e,t)=>e>=t-1e-12*Math.abs(t);function Zu(e,t,n){let r=[...t],o=e(r),s=n,i=r.map((c,a)=>a);for(let c=0;c<200;c++){let a=s?i.slice(1):i;if(a.length===0)break;let l=Yb(o,a);if(!s&&Math.abs(r[0])===tn&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>tn?(Math.sign(l[0])*tn-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((d,g)=>d+f*l[g]);f*l[0]!==0&&Math.abs(h[0])>=tn*(1-1e-15)&&(h[0]=Math.sign(h[0])*tn);let p=e(h);if(eh(p.value,o.value)){let d=At(h.map((g,w)=>Math.abs(g-r[w])/Math.max(1,Math.abs(r[w]))));r=h,o=p,m=d>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function th(e,t){let n=Zu(e,[Math.max(-tn,Math.min(tn,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Zu(e,[Math.sign(n.theta[0])*tn,...n.theta.slice(1)],!0);return eh(r.at.value,n.at.value)?r:n}function nh(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=rt(e.map(o=>o.map(s=>-s)))}catch{return n()}return r.map((o,s)=>o.map((i,c)=>(i+r[c][s])/2))}var rh=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function Bb(e,t,n){at(t)&&V(t,Bu,"polychor"),V(n,Bu,"polychor");let r,o;if(Gu(t)){let C=e,D=t;if(C.length!==D.length)throw new RangeError("'x' and 'y' must have the same length");let A=C.map((J,ce)=>!Number.isNaN(J)&&!Number.isNaN(D[ce])),O=Ot(C.filter((J,ce)=>A[ce])),q=Ot(D.filter((J,ce)=>A[ce])),P=new Map(vc(O).map((J,ce)=>[J,ce])),B=new Map(vc(q).map((J,ce)=>[J,ce]));r=Array.from({length:P.size},()=>new Array(B.size).fill(0)),O.forEach((J,ce)=>r[P.get(J)][B.get(q[ce])]++),o=n??{}}else{let C=e;if(!C.every(Gu))throw new TypeError("give a table (array of rows) or two vectors");let D=C[0]?.length??0;if(C.some(A=>A.length!==D))throw new TypeError("the table must have rows of equal length");if(r=C.map(A=>Array.from(A)),r.some(A=>A.some(O=>!(O>=0&&O<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:i=!1}=o,c=[],a=0;Ie(r.map(C=>Ie(C)))<2**1e3||({groups:r,k:a}=Re(r));let l=r.map(C=>Ie(C)),f=l.filter(C=>C===0).length;f>0&&(c.push(Uu(f,"row")),r=r.filter((C,D)=>l[D]!==0));let m=(r[0]??[]).map((C,D)=>Ie(r.map(A=>A[D]))),u=m.filter(C=>C===0).length;u>0&&(c.push(Uu(u,"column")),r=r.map(C=>C.filter((D,A)=>m[A]!==0)));let h=r.length,p=r[0]?.length??0;if(h<2&&c.push("the table has fewer than 2 rows"),p<2&&c.push("the table has fewer than 2 columns"),h<2||p<2)return H("polychor",{rho:NaN,warnings:c});let d=[-1/0,...bc(r.map(C=>Ie(C))),1/0],g=[-1/0,...bc(r[0].map((C,D)=>Ie(r.map(A=>A[D])))),1/0],N=rl(C=>{let D=Math.max(-tn,Math.min(tn,C)),A=[];for(let O=0;O<h;O++)for(let q=0;q<p;q++){if(r[O][q]===0)continue;let P=go([d[O],g[q]],[d[O+1],g[q+1]],D);A.push(r[O][q]*Math.log(P))}return-Ie(A)},-tn,tn).optimum;if(!s&&!i)return H("polychor",{rho:N,warnings:c});let v=C=>[-1/0,...C,1/0],E=d.slice(1,h),b=g.slice(1,p),M=C=>s?Qu(r,v(C.slice(1,h)),v(C.slice(h)),C[0],!0):Qu(r,d,g,C[0],!1),{theta:x,at:y,bounded:R}=th(M,s?[N,...E,...b]:[N]),T=x[0];if(R&&c.push(rh(T)),!i)return H("polychor",{rho:T,warnings:c});let S=s?v(x.slice(1,h)):d,k=s?v(x.slice(h)):g,L=Ie(r.map(C=>Ie(C))),_=[],I=[];for(let C=0;C<h;C++)for(let D=0;D<p;D++){let A=r[C][D],O=go([S[C],k[D]],[S[C+1],k[D+1]],T);if(A===0)_.push(L*O);else if(a===0)_.push(A*Math.log1p(1e-6/A),A*Xu(L*O/A-1));else{let q=$(A,a);I.push(q*Math.log1p(1e-6/q)),_.push(A*Xu(L*O/A-1))}}let F=nh(y.hessian,R);return H("polychor",{rho:T,warnings:c,var:a===0?F:F.map(C=>C.map(D=>$(D,-a))),chisq:a===0?2*Ie(_):$(2*Ie(_),a)+2*Ie(I),df:h*p-h-p,n:$(L,a),rowCuts:S.slice(1,h),colCuts:k.slice(1,p)})}function Ub(e,t,n,r){let o=e.length,s=[oe(.25),oe(.5),oe(.75)],i=(h,p)=>p.filter(d=>d<h).length,c=Nc(e).map(h=>i(h,n)),a=Nc(t).map(h=>i(h,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));c.forEach((h,p)=>l[h][a[p]]++);let f=[-1/0,...n,1/0],m=[-1/0,...s,1/0],u=[];return l.forEach((h,p)=>h.forEach((d,g)=>{if(d===0)return;let w=go([f[p],m[g]],[f[p+1],m[g+1]],r);u.push(d*Math.log((d+1e-6)/(w*o)))})),2*Ie(u)}function Gb(e,t,n={}){if(V(n,jb,"polyserial"),e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,s=e.map((y,R)=>!Number.isNaN(y)&&!Number.isNaN(t[R])),i=e.filter((y,R)=>s[R]),c=Ot(t.filter((y,R)=>s[R])),a=i.length,l=vc(c);if(l.length<2)return H("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((y,R)=>[y,R+1])),m=c.map(y=>f.get(y)),u=l.map(()=>0);for(let y of m)u[y-1]++;let h=bc(u),p=[],d=Math.sqrt((a-1)/a)*Math.sqrt(ot(m))*ol(i,m)[0]/Ie(h.map(y=>Xt(y)));if(Math.abs(d)>tn){let y=Math.sign(d)*tn;p.push(`initial correlation inadmissible, ${Pn(d)}, set to ${Pn(y)}`),d=y}if(!r&&!o)return H("polyserial",{rho:d,warnings:p});let g=Nc(i),w=m.map(y=>y-1),N=y=>Wb(g,w,r?y.slice(1):h,y[0],r),{theta:v,at:E,bounded:b}=th(N,r?[d,...h]:[d]);if(b&&p.push(rh(v[0])),!o)return H("polyserial",{rho:v[0],warnings:p});let M=r?v.slice(1):h,x=l.length;return H("polyserial",{rho:v[0],warnings:p,var:nh(E.hessian,b),chisq:Ub(m,g,M,r?v[0]:d),df:4*x-x-1,n:a,cuts:M})}var Hb=["group","strata","rho","cencode"],Xb=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function fa(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function yc(e){let t=en(e),n=Array.from(new Set(t)).sort(Vn);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function Qb(e,t,n){if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n.group!==void 0&&n.group.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(n.strata!==void 0&&n.strata.length!==e.length)throw new RangeError("'strata' must have the same length as 'time'")}function Zb(e,t,n={}){V(n,Hb,"cuminc"),Qb(e,t,n);let{group:r,strata:o,rho:s=0,cencode:i=0}=n,c=[],a=new mt;for(let b=0;b<e.length;b++)if(!(Number.isNaN(e[b])||fa(t[b])||r!==void 0&&fa(r[b])||o!==void 0&&fa(o[b]))){if(!Number.isFinite(e[b]))throw new RangeError("'time' must contain only finite values");if(e[b]<0)throw new RangeError("'time' must be non-negative");c.push(b)}let l=e.length-c.length;if(l>0&&a.add(`${l} cases omitted due to missing values`),c.length===0)throw new RangeError("no complete observations");let f=r===void 0?[1]:en(c.map(b=>r[b])),m=o===void 0?[1]:en(c.map(b=>o[b])),u=en([...c.map(b=>t[b]),i]),h=yc(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let p=yc(m),d=yc(u),g=u[u.length-1],w=d.labels.filter(b=>b!==g);if(w.length===0)throw new Error("there are no events of any cause");let N=c.map((b,M)=>({time:e[b],status:u[M],group:r===void 0?0:h.index.get(f[M]),strata:o===void 0?0:p.index.get(m[M]),source:b})).sort((b,M)=>b.time-M.time),v=[];for(let b of w){let M=Rt(b);for(let x=0;x<h.labels.length;x++){let y=N.filter(A=>A.group===x),R=y.reduce((A,O)=>Math.max(A,O.time),-1/0),T=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,S=ar(y.map(A=>A.time*T),y.map(A=>A.status===g?0:1)),k=[0],L=[0],_=[0],I=[],F=1;for(let A=0;A<S.time.length;A++){let O=S.nEvent[A];if(O===0)continue;let q=S.time[A]/T,P=S.nRisk[A],B=y.filter(J=>J.time===q&&J.status===b).length;I.push({t:q,Y:P,d:O,dk:B,sb:F,sa:S.surv[A]}),F=S.surv[A]}let C=0,D=0;for(let A=0;A<I.length;A++){let O=I[A],q=C,P=D;C+=O.sb*O.dk/O.Y;let B=0;for(let J=0;J<=A;J++){let ce=I[J],W=ce.Y,te=ce.d,U=ce.dk,ve=W===te?0:(te*(W-te)+2*U*(te-U))/((W-1)*(W-te)**2),X=W===1?U:U*(W-U)/(W*W*(W-1)),K=W===te||W===1?0:U*(W-U)/(W*(W-te)*(W-1)),Z=0;for(let G=0;G<=J;G++){let j=I[G];Z+=j.sb*j.dk/j.Y}let z=C-Z;B+=z*z*ve+ce.sb*ce.sb*X-2*z*ce.sb*K}D=Math.max(0,B),O.dk>0&&(k.push(O.t,O.t),L.push(q,C),_.push(P,D))}k.push(R),L.push(C),_.push(D),v.push({name:`${Rt(h.labels[x])} ${M}`,group:Rt(h.labels[x]),cause:M,time:k,est:L,var:_})}}let E=h.labels.length>=2?Jb(N,h.labels,w,p.labels.length,s,g):void 0;return H("cuminc",{curves:v,...E===void 0?{}:{tests:E},warnings:a.list})}function Jb(e,t,n,r,o,s){let i=[];for(let c of n){let a=t.length,l=new Array(a).fill(0),f=Array.from({length:a},()=>new Array(a).fill(0));for(let d=0;d<r;d++){let g=e.filter(b=>b.strata===d),w=Array.from(new Set(g.filter(b=>b.status!==s).map(b=>b.time))).sort((b,M)=>b-M),N=new Array(a).fill(1),v=new Array(a).fill(0),E=[];for(let b of w){let M=new Array(a).fill(0),x=new Array(a).fill(0),y=new Array(a).fill(0);for(let A of g)A.time>=b&&M[A.group]++;for(let A of g)A.time===b&&A.status!==s&&(A.status===c?x[A.group]++:y[A.group]++);let R=N.slice(),T=v.slice(),S=M.map((A,O)=>A===0?0:A/R[O]),k=M.map((A,O)=>A===0?0:A*(1-T[O])/R[O]),L=x.reduce((A,O)=>A+O,0),_=S.reduce((A,O)=>A+O,0),I=_===0?0:L/_,F=E.length===0?1:E[E.length-1].Gafter,C=F-I,D=k.reduce((A,O)=>A+O,0);for(let A=0;A<a;A++){let O=F**o;D>0&&(l[A]+=O*(x[A]-k[A]*L/D)),M[A]>0?(N[A]=N[A]*(1-(x[A]+y[A])/M[A]),v[A]+=R[A]*x[A]/M[A]):N[A]=0}E.push({time:b,Y:M,d1:x,d2:y,Sbefore:R,Safter:N.slice(),Fbefore:T,h:S,R:k,dF0:I,Gbefore:F,Gafter:C})}for(let b=0;b<a;b++)for(let M=0;M<a;M++){let x=0;for(let y=0;y<a;y++)for(let R=0;R<E.length;R++){let T=E[R],S=T.h.reduce((te,U)=>te+U,0);if(!(S>0)||!(T.h[y]>0))continue;let k=T.Gbefore**o,L=k*T.h[b]*((b===y?1:0)-T.h[y]/S),_=k*T.h[M]*((M===y?1:0)-T.h[y]/S),I=0,F=0;for(let te=R+1;te<E.length;te++){let U=E[te],ve=U.h.reduce((G,j)=>G+j,0);if(!(ve>0))continue;let X=U.Gbefore**o,K=X*U.h[b]*((b===y?1:0)-U.h[y]/ve),Z=X*U.h[M]*((M===y?1:0)-U.h[y]/ve),z=U.Gbefore===0?0:U.dF0/U.Gbefore;I+=K*z,F+=Z*z}let C=T.Safter[y]===0?0:T.Gafter/T.Safter[y],D=L+(1-C)*I,A=_+(1-C)*F,O=-C*I,q=-C*F,P=T.Y[y]===0?0:T.Sbefore[y]*T.d2[y]/T.Y[y],B=(D*A*T.dF0+O*q*P)/T.h[y],J=T.d1.reduce((te,U)=>te+U,0),ce=0;if(J>1&&T.Y[y]>0&&T.Sbefore[y]>0){let te=1/T.Sbefore[y];S>te&&(ce=J*(J-1)/S*(T.Y[y]*te*te/(S-te))*(D*A/(T.h[y]*T.h[y])))}let W=0;T.d2[y]>1&&T.Y[y]>1&&(W=(T.d2[y]-1)/(T.Y[y]-1)*(P/T.h[y])*O*q),x+=B-ce-W}f[b][M]+=x}}let m=a-1,u=NaN,h=NaN,p=f.slice(0,m).map(d=>d.slice(0,m));try{let d=rt(p,l.slice(0,m),{tol:1e-12});u=l.slice(0,m).reduce((g,w,N)=>g+w*d[N],0),h=Mt(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}i.push({cause:Rt(c),stat:u,df:m,pValue:h})}return i}function ev(e,t){let n=Array.from(new Set(t.filter(s=>!Number.isNaN(s)))).sort((s,i)=>s-i),r=[],o=[];for(let s of e.curves){let i=[],c=[];for(let a of n){let l=-1;for(let f=0;f<s.time.length;f++)s.time[f]<=a&&(l=f);a<0||l<0||a>s.time[s.time.length-1]?(i.push(NaN),c.push(NaN)):(i.push(s.est[l]),c.push(s.var[l]))}r.push(i),o.push(c)}return{curveNames:e.curves.map(s=>s.name),times:n,est:r,var:o}}function In(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function tv(e,t,n,r){for(let o=0;o<t.length;o++)for(let s=0;s<n.length;s++)e[o][s]+=r*t[o]*n[s]}function Mc(e,t){return Ie(e.map((n,r)=>n*t[r]))}function nv(e){return e.every(t=>t.every(Number.isFinite))}function rv(e,t,n,r={}){V(r,Xb,"crr");let{cov2:o,tf:s,failcode:i=1,cencode:c=0,gtol:a=1e-6,maxiter:l=10,init:f,variance:m=!0,confLevel:u=.95,names:h}=r;if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n!==void 0&&n.length!==e.length)throw new RangeError("'cov1' must have one row per observation");if(o!==void 0&&o.length!==e.length)throw new RangeError("'cov2' must have one row per observation");if(o!==void 0&&s===void 0)throw new TypeError("'tf' is required when 'cov2' is supplied");if(!(a>0)||!Number.isFinite(a))throw new RangeError("'gtol' must be finite and positive");if(!Number.isInteger(l)||l<0)throw new RangeError("'maxiter' must be a non-negative integer");if(!(u>0&&u<1))throw new RangeError("'confLevel' must be between 0 and 1");let p=n?.[0]?.length??0,d=o?.[0]?.length??0,g=p+d;if(g===0)throw new RangeError("at least one covariate is required");if(n!==void 0&&n.some(j=>j.length!==p))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(j=>j.length!==d))throw new RangeError("rows of 'cov2' must have equal length");if(f!==void 0&&f.length!==g)throw new RangeError("'init' must have one value per coefficient");if(h!==void 0&&h.length!==g)throw new RangeError("'names' must have one name per coefficient");let w=[];for(let j=0;j<e.length;j++)if(!(Number.isNaN(e[j])||fa(t[j])||n!==void 0&&n[j].some(Number.isNaN)||o!==void 0&&o[j].some(Number.isNaN))){if(!Number.isFinite(e[j]))throw new RangeError("'time' must contain only finite values");if(n!==void 0&&!n[j].every(Number.isFinite)||o!==void 0&&!o[j].every(Number.isFinite))throw new RangeError("covariates must be finite");w.push(j)}let N=e.length-w.length;if(w.length===0)throw new RangeError("no complete observations");let v=w.map(j=>e[j]),E=en([...w.map(j=>t[j]),c,i]),b=E.slice(0,w.length),M=E[E.length-2],x=E[E.length-1],y=w.map(j=>n===void 0?[]:Array.from(n[j])),R=w.map(j=>o===void 0?[]:Array.from(o[j])),T=Array.from(new Set(v.filter((j,Q)=>b[Q]===x))).sort((j,Q)=>j-Q);if(T.length===0)throw new RangeError("no observations of 'failcode'");let S=d===0?[[0]]:s(T).map(j=>Array.from(j));if(d>0&&(S.length!==T.length||S.some(j=>j.length!==d)||!nv(S)))throw new RangeError("'tf' must return a finite matrix with one row per unique failure time and one column per cov2 column");let k=T.map((j,Q)=>v.map((ne,be)=>[...y[be],...R[be].map((Ne,me)=>Ne*(S[Q][me]??0))])),L=v.reduce((j,Q)=>Math.max(j,Math.abs(Q)),0),_=L>0&&L<1?2**Math.min(1023,Math.ceil(-Math.log2(L))):1,I=ar(v.map(j=>j*_),b.map(j=>j===M?1:0)),F=j=>{let Q=j*_,ne=1;for(let be=0;be<I.time.length&&!(I.time[be]>=Q);be++)ne=I.surv[be];return ne},C=T.map(j=>v.map((Q,ne)=>{if(Q>=j)return 1;if(b[ne]===M||b[ne]===x)return 0;let be=F(Q);return be===0?0:F(j)/be}));for(let j=0;j<T.length;j++)for(let Q=0;Q<v.length;Q++)v[Q]===T[j]&&b[Q]===x&&(C[j][Q]=1);let D=j=>{let Q=Array.from({length:g},()=>[]),ne=Array.from({length:g},()=>Array.from({length:g},()=>[])),be=[],Ne=In(T.length,g),me=[],le=[],Ee=[];for(let Se=0;Se<T.length;Se++){let Ke=T[Se],Pe=v.map((pe,Ae)=>pe===Ke&&b[Ae]===x?Ae:-1).filter(pe=>pe>=0),Ze=Pe.length,Be=k[Se],Qe=Be.map(pe=>Mc(j,pe)),fe=C[Se].slice();for(let pe of Pe)fe[pe]=1;let ee=Math.max(...Qe.map((pe,Ae)=>fe[Ae]>0?pe:-1/0));if(ee===-1/0)throw new Error("empty Fine-Gray risk set");let ge=ee<500&&ee>-500,de=ge?Qe.map((pe,Ae)=>fe[Ae]*Math.exp(pe)):Qe.map((pe,Ae)=>fe[Ae]*Math.exp(pe-ee)),ae=Ie(de),Je=Array.from({length:g},(pe,Ae)=>Ie(de.map((We,Fe)=>We*Be[Fe][Ae])));if(!(ae>0))throw new Error("empty Fine-Gray risk set");let Oe=Je.map(pe=>pe/ae),ke=Array.from({length:g},(pe,Ae)=>{let We=Be[0][Ae],Fe=0,nt=0;for(let ut of Be)Fe=Math.max(Fe,Math.abs(ut[Ae]-We)),nt=Math.max(nt,Math.abs(ut[Ae]));return Fe>0&&nt/Fe>1e6}).some(Boolean),$e=ke?Array.from({length:g},(pe,Ae)=>Be.map(We=>We[Ae]-Be[0][Ae])):[],He=ke?$e.map(pe=>Ie(pe.map((Ae,We)=>de[We]*Ae))/ae):[],se=ke?void 0:Array.from({length:g},(pe,Ae)=>Array.from({length:g},(We,Fe)=>Ie(de.map((nt,ut)=>nt*Be[ut][Ae]*Be[ut][Fe]))));le.push(Oe),Ee.push(de.map(pe=>pe/ae));let Te=ge?Ze/ae:Ze*Math.exp(-ee)/ae;me.push(Te),be.push(Ie(Pe.map(pe=>Mc(j,Be[pe])))-Ze*(ge?Math.log(ae):ee+Math.log(ae)));for(let pe=0;pe<g;pe++){let We=Ie(Pe.map(Fe=>Be[Fe][pe]))-Ze*Oe[pe];Q[pe].push(We),Ne[Se][pe]=We}for(let pe=0;pe<g;pe++)for(let Ae=0;Ae<g;Ae++){let We=ke?Ie(de.map((Fe,nt)=>Fe*($e[pe][nt]-He[pe])*($e[Ae][nt]-He[Ae])))/ae:se[pe][Ae]/ae-Oe[pe]*Oe[Ae];ne[pe][Ae].push(Ze*We)}}let he=In(v.length,g);for(let Se=0;Se<T.length;Se++){let Ke=k[Se],Pe=Ee[Se],Ze=le[Se],Be=T[Se];for(let Qe=0;Qe<v.length;Qe++){let fe=v[Qe]===Be&&b[Qe]===x?1:0,ee=v.filter((de,ae)=>de===Be&&b[ae]===x).length,ge=fe-Pe[Qe]*ee;for(let de=0;de<g;de++)he[Qe][de]+=(Ke[Qe][de]-Ze[de])*ge}}let Le=Q.map(Ie),we=ne.map(Se=>Se.map(Ie));return{loglik:Ie(be),score:Le,inf:we,bfitj:me,res:Ne,eta:he,means:le,riskFactors:Ee,zByTime:k}},A=f===void 0?new Array(g).fill(0):Array.from(f);if(!A.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let O=A.slice(),q=D(O),P=!1,B=0,J=(j,Q)=>Math.max(...j.score.map((ne,be)=>Math.abs(ne)*Math.max(Math.abs(Q[be]),1)))<a*Math.max(Math.abs(j.loglik),1);for(;;){if(J(q,O)){P=!0;break}if(B>=l)break;let j=rt(q.inf,q.score,{tol:0});O=O.map((Q,ne)=>Q+j[ne]),B++,q=D(O)}let ce=rt(q.inf),W=In(g);if(m){let j=In(g),Q=v.length,ne=Array.from(new Set(v)).sort((le,Ee)=>le-Ee),be=ne.map(le=>{let Ee=v.filter(Le=>Le>=le).length,he=v.filter((Le,we)=>Le===le&&b[we]===M).length;return Ee===0?0:he/Ee}),Ne=In(Q,g);for(let le=0;le<ne.length;le++){let Ee=ne[le],he=new Array(g).fill(0);for(let we=0;we<Q;we++)if(!(!(v[we]<Ee)||b[we]===M||b[we]===x))for(let Me=0;Me<T.length;Me++){if(T[Me]<Ee)continue;let Se=q.riskFactors[Me][we],Ke=v.filter((Be,Qe)=>Be===T[Me]&&b[Qe]===x).length,Pe=q.zByTime[Me][we],Ze=q.means[Me];for(let Be=0;Be<g;Be++)he[Be]+=(Pe[Be]-Ze[Be])*Se*Ke}let Le=v.filter(we=>we>=Ee).length;if(Le!==0)for(let we=0;we<Q;we++){let Me=v[we]===Ee&&b[we]===M?1:0,Se=v[we]>=Ee?be[le]:0;for(let Ke=0;Ke<g;Ke++)Ne[we][Ke]+=he[Ke]/Le*(Me-Se)}}for(let le=0;le<Q;le++)tv(j,q.eta[le].map((Ee,he)=>Ee+Ne[le][he]),q.eta[le].map((Ee,he)=>Ee+Ne[le][he]),1);W=In(g);let me=oh(ce,j);W=oh(me,ce)}else W=In(g).map(j=>j.map(()=>NaN));let te=m?W.map((j,Q)=>Math.sqrt(j[Q])):void 0,U=te===void 0?void 0:O.map((j,Q)=>j/te[Q]),ve=U===void 0?void 0:U.map(j=>2*ie(-Math.abs(j))),X=m?oe((1+u)/2):void 0,K=X===void 0?void 0:O.map((j,Q)=>Math.exp(j-X*te[Q])),Z=X===void 0?void 0:O.map((j,Q)=>Math.exp(j+X*te[Q])),z=m?O.map(Math.exp):void 0,G=h===void 0?[...Array.from({length:p},(j,Q)=>`cov1${Q+1}`),...Array.from({length:d},(j,Q)=>`cov2${Q+1}*tf${Q+1}`)]:Array.from(h);return H("crr",{coefficients:O,names:G,var:W,...te===void 0?{}:{se:te,z:U,pValue:ve,lower:K,upper:Z,expCoef:z},loglik:q.loglik,loglikNull:D(new Array(g).fill(0)).loglik,logtest:{statistic:2*(q.loglik-D(new Array(g).fill(0)).loglik),df:g},score:q.score,inf:m?q.inf:In(g).map(j=>j.map(()=>NaN)),invinf:m?ce:In(g).map(j=>j.map(()=>NaN)),converged:P,iterations:B,n:w.length,nMissing:N,uftime:T,bfitj:q.bfitj,res:m?q.res:null,tfs:S,warnings:[],covariateDimensions:[p,d]})}function oh(e,t){let n=In(e.length,t[0]?.length??0);for(let r=0;r<e.length;r++)for(let o=0;o<(t[0]?.length??0);o++)for(let s=0;s<t.length;s++)n[r][o]+=e[r][s]*t[s][o];return n}function ov(e,t,n){let[r,o]=e.covariateDimensions;if(r>0&&t===void 0)throw new TypeError("'cov1' is required by this fit");if(o>0&&n===void 0)throw new TypeError("'cov2' is required by this fit");if(r>0&&t.some(a=>a.length!==r))throw new RangeError("'cov1' has the wrong number of columns");if(o>0&&n.some(a=>a.length!==o))throw new RangeError("'cov2' has the wrong number of columns");if(r>0&&o>0&&t.length!==n.length)throw new RangeError("'cov1' and 'cov2' must have the same number of rows");let s=r>0?t.length:n.length,i=o===0?e.uftime.map(()=>[0]):e.tfs;return e.uftime.map((a,l)=>{let f=[a];for(let m=0;m<s;m++){let u=0;for(let h=0;h<=l;h++){let p=[...r===0?[]:t[m],...o===0?[]:n[m].map((v,E)=>v*i[h][E])],d=e.bfitj[h],g=Mc(e.coefficients,p),w=d*Math.exp(g),N=Number.isFinite(w)&&(w!==0||d===0)?w:d===0?0:Math.exp(Math.log(d)+g);u+=N}f.push(-Math.expm1(-u))}return f})}var sv=["n","power","r","type","nfractional","alpha","sidedTest"],av=["n","power","r","type","nfractional","alpha","sidedTest"],iv=["n","power","r","type","nfractional","alpha","sidedTest"],cv=["n","power","r","type","nfractional","alpha","sidedTest"],lv=["n","power","r","type","nfractional","alpha","sidedTest"],fv=["n","power","r","type","nfractional","alpha","sidedTest"],mv=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],uv=["alpha"],hv=["alpha"],pv=["alpha"],dv=["alpha"],gv=["alpha"],wv=["nmax"],Cr=.05,cn=e=>oe(e),Kn=e=>ie(e),Lr=e=>Number.isFinite(e),kt=e=>Lr(e)&&e>=0&&e<=1,Nn=e=>Lr(e)&&e>0;function Xe(e,t,n,r){if(typeof e!="number")throw new TypeError(`'${t}' must be numeric`);if(!n(e))throw new RangeError(r)}function Or(e){Xe(e,"alpha",kt,"'alpha' must be a probability in [0, 1]")}function bv(e,t){return[e*t/(1+t),e/(1+t)]}function uo(e,t,n,r,o,s,i={}){let{n:c,power:a}=i,l=i.r??1,f=i.alpha??Cr,m=i.nfractional??!1,u=e==="equivalence"?xe(i.type,["equivalence","equality"]):"equivalence",h=i.sidedTest??1;if(e==="equivalence"&&u!=="equivalence"&&u!=="equality")throw new RangeError("'type' should be one of \u201Cequivalence\u201D, \u201Cequality\u201D");if(Xe(n,"treat",Lr,"'treat' must be finite"),Xe(r,"control",Lr,"'control' must be finite"),t==="binary"?(Xe(n,"treat",kt,"'treat' must be a probability in [0, 1]"),Xe(r,"control",kt,"'control' must be a probability in [0, 1]")):Xe(o,"sigma",Nn,"'sigma' must be positive and finite"),Xe(l,"r",Nn,"'r' must be positive and finite"),Or(f),(e!=="equivalence"||u!=="equality")&&Xe(s,"delta",L=>Lr(L)&&L>=0,"'delta' must be nonnegative and finite"),e==="superiority"&&h!==1&&h!==2)throw new RangeError("'sidedTest' must be 1 or 2");if(typeof m!="boolean")throw new TypeError("'nfractional' must be logical");bn([c,a],"exactly one of 'n' and 'power' must be NULL"),qe(a)||Xe(a,"power",L=>kt(L)&&L>0&&L<1,"'power' must be strictly between 0 and 1"),c!=null&&Xe(c,"n",L=>Lr(L)&&L>0,"'n' must be positive and finite");let p=n-r,d=t==="binary"?n*(1-n):o*o,g=t==="binary"?r*(1-r):o*o,w=e==="equivalence"&&u==="equality",N=w?void 0:s,v=e==="noninferiority"?p+N:e==="superiority"?p-N:w?p:N-Math.abs(p);if(e==="noninferiority"&&v<=0)throw new RangeError("Target power is not reachable under the specified hypotheses");let E=e==="superiority"?oe(f/h,{lowerTail:!1}):w?oe(f/2,{lowerTail:!1}):oe(f,{lowerTail:!1}),b=a??.8,M=w||e!=="equivalence"?oe(1-b,{lowerTail:!1}):oe((1-b)/2,{lowerTail:!1}),x,y,R,T,S;if(qe(c))v===0?(R=1/0,T=1/0):(T=(E+M)**2*(d/l+g)/(v*v),R=l*T),m?(x=R,y=T):(y=Math.ceil(T),x=Math.ceil(e==="equivalence"&&t==="continuous"||e==="superiority"?l*y:R)),S=a;else{[R,T]=bv(c,l),x=m?R:Math.ceil(R),y=m?T:Math.ceil(T);let L=Math.sqrt(d/x+g/y);if(e==="equivalence"&&!w)S=Math.max(0,Kn((s-p)/L-cn(1-f))+Kn((s+p)/L-cn(1-f))-1);else if(w)S=Kn(Math.abs(p)/L-cn(1-f/2))+Kn(-Math.abs(p)/L-cn(1-f/2));else if(e==="noninferiority")S=Kn((p+s)/L-cn(1-f));else{let _=Math.abs(p-s)/L;S=Kn(_-E)+Kn(-_-E)}}return{nTotal:x+y,nTreat:x,nControl:y,nTotalUnrounded:R+T,nTreatUnrounded:R,nControlUnrounded:T,power:S,...N===void 0?{}:{delta:N},warnings:[]}}function vv(e,t,n,r={}){return V(r,sv,"epiSsequb"),H("epiSsequb",uo("equivalence","binary",e,t,void 0,n??void 0,r))}function Nv(e,t,n,r,o={}){V(o,av,"epiSsequc");let s=uo("equivalence","continuous",e,t,n,r??void 0,o);return H("epiSsequc",s)}function yv(e,t,n,r={}){return V(r,iv,"epiSsninfb"),H("epiSsninfb",uo("noninferiority","binary",e,t,void 0,n,r))}function Mv(e,t,n,r,o={}){return V(o,cv,"epiSsninfc"),H("epiSsninfc",uo("noninferiority","continuous",e,t,n,r,o))}function Ev(e,t,n,r={}){return V(r,lv,"epiSssupb"),H("epiSssupb",uo("superiority","binary",e,t,void 0,n,r))}function xv(e,t,n,r,o={}){return V(o,fv,"epiSssupc"),H("epiSssupc",uo("superiority","continuous",e,t,n,r,o))}function mo(e,t){let n=new Array(e+1).fill(0);if(t===0)return n[0]=1,n;if(t===1)return n[e]=1,n;if(t>.5)return mo(e,1-t).reverse();let r=Math.floor((e+1)*t);n[r]=Math.exp(e*Math.log1p(-t)+r*Math.log(t/(1-t))+Array.from({length:r},(o,s)=>Math.log(e-s)-Math.log(s+1)).reduce((o,s)=>o+s,0));for(let o=r;o>0;o--)n[o-1]=n[o]*(o/(e-o+1))*((1-t)/t);for(let o=r;o<e;o++)n[o+1]=n[o]*((e-o)/(o+1))*(t/(1-t));return n}function Rv(e,t,n,r={}){V(r,mv,"powerPaired2x2");let o=r.sigLevel??Cr,s=xe(r.alternative,["two.sided","one.sided"]),i=r.strict??!1,c=r.nullOddsRatio??1,a=r.errbound??1e-6,l=xe(r.tsmethod,["central","minlike","blaker"]);if(Xe(e,"pb",kt,"'pb' must be in [0, 1]"),Xe(t,"pc",kt,"'pc' must be in [0, 1]"),Xe(n,"npairs",M=>Number.isSafeInteger(M)&&M>=0&&M<=1e5,"'npairs' must be a nonnegative safe integer"),Or(o),Xe(c,"nullOddsRatio",Nn,"'nullOddsRatio' must be positive and finite"),Xe(a,"errbound",M=>Lr(M)&&M>=0,"'errbound' must be nonnegative and finite"),e+t>1)throw new RangeError("probabilities must be finite, non-negative and not all 0");if(s!=="two.sided"&&s!=="one.sided")throw new RangeError("'alternative' should be one of \u201Ctwo.sided\u201D, \u201Cone.sided\u201D");if(!["central","minlike","blaker"].includes(l))throw new RangeError("'tsmethod' should be one of \u201Ccentral\u201D, \u201Cminlike\u201D, \u201Cblaker\u201D");let f=e+t,m=f===0?.5:e/f,u=c/(1+c),h=e>=t?1:-1,p=s==="two.sided"&&i&&l!=="blaker",d=s==="one.sided",g=mo(n,f),w=0,N=n;if(a>0){let M=0;for(;w<=n&&(M+=g[w])<=a/2;)w++;for(M=0;N>=0&&(M+=g[N])<=a/2;)N--}let v=0;for(let M=w;M<=N;M++){let x=g[M];if(x===0)continue;let y=mo(M,m),R=mo(M,u),T=0,S=0,k=new Array(M+1),L=new Array(M+1);for(let _=0;_<=M;_++)T+=R[_],k[_]=T;for(let _=M;_>=0;_--)S+=R[_],L[_]=S;for(let _=0;_<=M;_++){let I=k[_],F=L[_],C,D=u===.5&&o===1;d?C=D||(h>0?F<=o:I<=o):p?C=D||I<=o/2||F<=o/2:C=h>0?D?_>Math.floor(M/2):F<=o/2:D?_<Math.ceil(M/2):I<=o/2,C&&(v+=x*y[_])}}let E=`errbound= ${a===0?"0":a>=.001?String(a):`${a.toExponential(0).replace(/e([+-]?)(\\d+)$/,(M,x,y)=>`e${x||"+"}${y.padStart(2,"0")}`)}`}${e+t===1?" no non-informative pairs: pa+pd=0":""}`,b="Power for McNemar's Exact Test"+(p?", including power to reject in the wrong direction":"")+(l==="central"&&r.tsmethod!==void 0?`, using (tsmethod=${l})`:l==="minlike"?`, using (tsmethod=${l})`:l==="blaker"?`, using (tsmethod=${l})`:"");return H("powerPaired2x2",{power:v,npairs:n,pb:e,pc:t,sigLevel:o,alternative:s,nullOddsRatio:c,note:E,method:b,warnings:[]})}function Tv(e,t,n,r,o,s={}){V(s,hv,"powerCT");let i=s.alpha??Cr;Or(i);for(let[l,f]of[[e,"nE"],[t,"nC"]])Xe(l,f,Nn,`'${f}' must be positive and finite`);for(let[l,f]of[[n,"pE"],[r,"pC"]])Xe(l,f,kt,`'${f}' must be in [0, 1]`);Xe(o,"RR",Nn,"'RR' must be positive and finite");let c=e/t,a=e*n+t*r;return Kn(Math.sqrt(c*a)*Math.abs(o-1)/(c*o+1)-cn(1-i/2))}function Sv(e,t,n,r,o,s={}){V(s,uv,"ssizeCT");let i=s.alpha??Cr;Or(i),Xe(e,"power",f=>kt(f)&&f>0&&f<1,"'power' must be strictly between 0 and 1"),Xe(t,"k",Nn,"'k' must be positive and finite"),Xe(n,"pE",kt,"'pE' must be in [0, 1]"),Xe(r,"pC",kt,"'pC' must be in [0, 1]"),Xe(o,"RR",Nn,"'RR' must be positive and finite");let c=cn(1-i/2)+cn(e),a=1/t*((t*o+1)/(o-1))**2*(c*c),l=t*n+r;return{nE:Math.ceil(a*t/l),nC:Math.ceil(a/l)}}function Av(e,t,n,r,o,s={}){V(s,dv,"powerEpi");let i=s.alpha??Cr;Or(i),Xe(e,"n",Nn,"'n' must be positive and finite"),Xe(t,"theta",Nn,"'theta' must be positive and finite"),Xe(n,"p",kt,"'p' must be in [0, 1]"),Xe(r,"psi",kt,"'psi' must be in [0, 1]"),Xe(o,"rho2",kt,"'rho2' must be in [0, 1]");let c=e*Math.log(t)**2*n*(1-n)*r*(1-o);return Kn(Math.sqrt(c)-cn(1-i/2))}function kv(e,t,n,r,o,s={}){V(s,pv,"ssizeEpi");let i=s.alpha??Cr;Or(i),Xe(e,"power",kt,"'power' must be in [0, 1]"),Xe(t,"theta",Nn,"'theta' must be positive and finite"),Xe(n,"p",kt,"'p' must be in [0, 1]"),Xe(r,"psi",kt,"'psi' must be in [0, 1]"),Xe(o,"rho2",kt,"'rho2' must be in [0, 1]");let c=Math.log(t)**2*n*(1-n)*r*(1-o);return Math.ceil((cn(1-i/2)+cn(e))**2/c)}function qv(e,t,n,r,o={}){V(o,gv,"numDEpi");let s=o.alpha??Cr;Or(s),Xe(e,"power",c=>kt(c)&&c>0&&c<1,"'power' must be strictly between 0 and 1"),Xe(t,"theta",Nn,"'theta' must be positive and finite"),Xe(n,"p",kt,"'p' must be in [0, 1]"),Xe(r,"rho2",kt,"'rho2' must be in [0, 1]");let i=Math.log(t)**2*n*(1-n)*(1-r);return Math.ceil((cn(1-s/2)+cn(e))**2/i)}function sh(e){let t=new Array(e.length),n=0,r=0;for(let o=0;o<e.length;o++){let s=e[o]-r,i=n+s;r=i-n-s,n=i,t[o]=n}return t}function Iv(e){let t=new Array(e.length),n=0,r=0;for(let o=e.length-1;o>=0;o--){let s=e[o]-r,i=n+s;r=i-n-s,n=i,t[o]=n}return t}function ah(e,t,n,r,o,s){let i=0,c=0;c=r.slice(0,e+1).reduce((a,l)=>a+l,0);for(let a=e+1;a<n.length;a++){let l=t-a,f=l<0?1:l+1>=o.length?0:o[l+1],m=l<0?0:s[Math.min(l,s.length-1)];i+=n[a]*f,c+=r[a]*m}return[i,c]}function _v(e,t,n,r,o={}){V(o,wv,"ph2simon");let s=o.nmax??100;for(let[E,b]of[[e,"pu"],[t,"pa"],[n,"ep1"],[r,"ep2"]])Xe(E,b,kt,`'${b}' must be in [0, 1]`);Xe(s,"nmax",E=>Number.isInteger(E)&&E>=1&&E<=1e3,"nmax cannot exceed 1000");let i=Array.from({length:s+1},(E,b)=>mo(b,e)),c=Array.from({length:s+1},(E,b)=>mo(b,t)),a=i.map(sh),l=c.map(sh),f=i.map(Iv),m=[];for(let E=2;E<=s;E++){let b;for(let M=1;M<E&&(b===void 0||M<b.enP0);M++){let x=E-M,y=i[M],R=c[M],T=l[x],S=a[M];for(let k=0;k<M;k++){let L=S[k],_=M+x*(1-L),I=k,F=E-1,C=-1;for(;I<=F;){let D=Math.floor((I+F)/2),[,A]=ah(k,D,y,R,f[x],T);A<=r?(C=D,I=D+1):F=D-1}if(C>=0){let[D]=ah(k,C,y,R,f[x],T);D>n&&(C=-1)}C>=0&&(b===void 0||_<b.enP0)&&(b={r1:k,n1:M,r:C,n:E,enP0:_,petP0:L})}}b&&m.push(b)}if(m.length===0)throw new Error(` No feasible solution found.
|
|
20
|
-
Increase maximum sample size. Current nmax value = ${s}.`);let u=m.filter((E,
|
|
18
|
+
`,Io=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let i=0;i<n.length;i++)s+=n[i]*t[i][o];return s})),Ms=e=>e[0].map((t,n)=>e.map(r=>r[n])),ys=(e,t=n=>n)=>e[0].map((n,r)=>At(e.map(o=>t(o[r])))),Yh=(e,t=n=>n)=>e.map(n=>At(n.map(t))),_o=(e,t=n=>n)=>At(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),Pa=e=>e<0?-1:1;function Jc(e){let t=e.map((n,r)=>Math.sqrt(n[r]));return e.map((n,r)=>n.map((o,s)=>r===s?1:o/(t[r]*t[s])))}function Wh(e,t){let n=Jc(e),{d:r,u:o,v:s}=Us(n),i=r.map((c,l)=>l).filter(c=>r[c]>Math.sqrt(qo)*r[0]),a=n.map((c,l)=>{let f=0;for(let m of i)f+=s[l][m]*(1/r[m])*o[l][m];return 1-1/f});return a.some(c=>c>1)&&xr(t,"In smc, smcs > 1 were set to 1.0"),a.some(c=>c<0)&&xr(t,"In smc, smcs < 0 were set to .0"),a.map(c=>c>1?1:c<0?0:c)}var xr=(e,t)=>{e.includes(t)||e.push(t)};function Xc(e,t,n,r){let o=e.map((a,c)=>a.map((l,f)=>c===f?t[c]:l)),s=wn(o,{symmetric:!0}),i=s.values.slice(0,n).map(a=>Math.sqrt(r(a)));return s.vectors.map(a=>a.slice(0,n).map((c,l)=>c*i[l]))}var bN=e=>Io(e,Ms(e));function Kh(e,t,n,r){let o=Jc(e),s=o.length,i=Wh(o,r),a=g=>{let w=Xc(o,g.map(E=>1-E),t,E=>E<qo?100*qo:E),N=bN(w),b=[];for(let E=0;E<s;E++)for(let v=E+1;v<s;v++)b.push((o[v][E]-N[v][E])**2);return At(b)},c=g=>{let w=Xc(o,g.map((N,b)=>o[b][b]-N),t,N=>Math.max(N,0));return g.map((N,b)=>{let E=0;for(let v=0;v<t;v++)E+=w[b][v]*w[b][v];return E+N-o[b][b]})},l=g=>{let w=wn(o.map((x,A)=>x.map((S,I)=>A===I?1-g[A]:S)),{symmetric:!0}),N=w.values,b=w.vectors,E=N.map(x=>x>=qo),v=N.map(x=>x<qo?100*qo:x),y=o.map((x,A)=>x.map((S,I)=>{if(A===I)return 0;let C=0;for(let _=0;_<t;_++)C+=v[_]*b[A][_]*b[I][_];return S-C})),T=Io(y,b),M=(x,A)=>{let S=0;for(let I=0;I<s;I++)S+=b[I][x]*T[I][A];return S},R=new Array(s).fill(0);for(let x=0;x<t;x++){let A=(S,I)=>{if(I===0)return;let C=I*M(x,S);for(let _=0;_<s;_++)R[_]+=C*b[_][x]*b[_][S]};E[x]&&A(x,1);for(let S=x+1;S<t;S++)A(S,2*(E[x]&&E[S]?1:(v[x]-v[S])/(N[x]-N[S])));for(let S=t;S<s;S++)A(S,2*v[x]/(N[x]-N[S]))}return R},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(kt(i),1)},u=new Array(s).fill(.01),h=Ke(()=>Qo(i.map(g=>1-g),a,{gr:c,...m,control:{fnscale:1,parscale:u}}));h.par.some(g=>g===f)&&(h=Ke(()=>Qo(h.par,a,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&xr(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let p=h.par,d=Xc(o,p.map((g,w)=>o[w][w]-g),t,g=>Math.max(g,0));if(d=Zc(d,t),d=d.map(g=>g.map(w=>w===0?1e-15:w)),Yh(d,g=>g*g).some(g=>g>1)&&xr(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){d=Zc(Fa(d).loadings,t);let g=ys(d,N=>N*N),w=g.map((N,b)=>b).sort((N,b)=>g[b]-g[N]||N-b);d=d.map(N=>w.map(b=>N[b]))}else d=Zc(d,t);return d}function Zc(e,t){let n=ys(e).map(Pa);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 vN(e){let t=e[0].length,n=e.map(o=>o.map(s=>s*s)),r=n.map(o=>o.map((s,i)=>{let a=0;for(let c=0;c<t;c++)c!==i&&(a+=o[c]);return a}));return{f:_o(n.map((o,s)=>o.map((i,a)=>i*r[s][a])))/4,Gq:e.map((o,s)=>o.map((i,a)=>i*r[s][a]))}}function NN(e){let t=e[0].length,n=l=>{let f=ot(l),m=Io(e,Ms(f)),{f:u,Gq:h}=vN(m),p=Ms(Io(Io(Ms(m),h),f)).map(d=>d.map(g=>-g));return{L:m,f:u,G:p}},r=Array.from({length:t},(l,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),o=1,s=n(r),i,a=[],c=!1;for(let l=0;l<=2e3;l++){let f=ys(r.map((g,w)=>g.map((N,b)=>N*s.G[w][b]))),m=s.G.map((g,w)=>g.map((N,b)=>N-r[w][b]*f[b])),u=Math.sqrt(_o(m,g=>g*g));if(a.push(s.f),u<1e-5){c=!0;break}if(i!==void 0){let g=i,w=r.map((b,E)=>b.map((v,y)=>v-g.T[E][y])),N=m.map((b,E)=>b.map((v,y)=>v-g.Gp[E][y]));_o(N,b=>b*b)>0&&(o=_o(w,b=>b*b)/Math.abs(_o(w.map((b,E)=>b.map((v,y)=>v*N[E][y])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=kt(a.slice(-10)),p=s,d=r;for(let g=0;g<=10;g++){let w=r.map((b,E)=>b.map((v,y)=>v-o*m[E][y])),N=ys(w,b=>b*b).map(b=>1/Math.sqrt(b));if(d=w.map(b=>b.map((E,v)=>E*N[v])),p=n(d),h-p.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=d,s=p}return{loadings:s.L,phi:Io(Ms(r),r),converged:c}}function MN(e,t={}){z(t,gN,"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)Oa(t.covmat),r=Jc(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Ho(e,void 0,{use:"pairwise.complete.obs"})}let o=r.length;if(o<2)throw new RangeError(`omega needs at least 2 variables (got ${o})`);if(n>o)throw new RangeError(`'nfactors' (${n}) must not exceed the number of variables (${o})`);let s=[],i=[],a=Kh(r,n,s,i),c,l,f;if(n===1)c=a.map(q=>q.slice()),f=[1],xr(i,`Omega_h for 1 factor is not meaningful, just omega_t
|
|
19
|
+
`),xr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let q=NN(a);if(q.converged||xr(s,"oblimin did not converge in 2000 iterations"),c=q.loadings,l=q.phi,n===2){let $=l[0][1],V=Math.sqrt(Math.abs($));f=[V,Pa($)*V],xr(i,wN)}else f=Kh(l,1,s,i).map($=>$[0])}let m=c.map(q=>{let $=0;for(let V=0;V<n;V++)$+=q[V]*f[V];return $}),u=Yh(a,q=>q*q),h=u.map((q,$)=>r[$][$]-q),p=c.map((q,$)=>q.map((V,P)=>{if(n===1)return 0;let Q=r[$][$],K=Q-(Q-V*V)-(V*f[P])**2;return K<0&&(K=0),Pa(V)*Math.sqrt(K)})),d=m.map((q,$)=>q*q/u[$]),g=m.map((q,$)=>{let V=[q,...p[$]];return At(V.map(P=>P*P))**2/At(V.map(P=>P**4))}),w=m.map(Pa);w.some(q=>q<0)&&(r=r.map((q,$)=>q.map((V,P)=>w[$]*V*w[P])));let N=m.map((q,$)=>w[$]*q),b=p.map((q,$)=>q.map(V=>w[$]*V)),E=N.map((q,$)=>[q,...b[$],u[$],h[$],d[$],g[$]]),v=_o(r),y=At(r.map((q,$)=>q[$])),T=At(N)**2,M=At(h),R=T/v,x=(v-M)/v,A=T/(v-M),S=(v-y)/v*o/(o-1),I=(v+At(Wh(r,i))-y)/v,C=ys(E.map(q=>q.slice(0,n+1)),q=>q*q),_=C[0]/At(C),F=b.map(q=>{let $=0;for(let V=1;V<n;V++)Math.abs(q[V])>Math.abs(q[$])&&($=V);return $}),O=[],D=[];for(let q=0;q<n;q++){let $=F.map((K,X)=>X).filter(K=>F[K]===q),V=At($.flatMap(K=>$.map(X=>r[X][K]))),P=At($.map(K=>b[K][q])),Q=At($.map(K=>N[K]));D.push(P*P),O.push([(P*P+Q*Q)/V,Q*Q/V,P*P/V])}let k=[[x,At(N)**2/v,At(D)/v],...O],L={omegaH:R,omegaLim:A,alpha:S,omegaTot:x,G6:I,ECV:_,sl:E,slNames:["g",...Array.from({length:n},(q,$)=>`F${$+1}*`),"h2","u2","p2","com"],orthog:a,oblique:c,gloading:f,key:w,omegaGroup:k,warnings:s,messages:i};return l!==void 0&&(L.phi=l),J("omega",L)}var jh=["ML","stdErr"],yN=["ML","stdErr"],on=.9999;function tl(e){let t=Ie(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(re(r/t));return n}var nl=e=>[...new Set(e)].sort((t,n)=>t-n),Bh=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Uh=e=>Array.isArray(e)||ArrayBuffer.isView(e),Gh=(e,t)=>e>0?oe(e,{lowerTail:!1})-oe(t,{lowerTail:!1}):oe(t)-oe(e);function rl(e){let t=Re([e]).groups[0],n=ct(t),r=t.map(s=>s-n),o=Math.sqrt(st(r));return r.map(s=>s/o)}function Hh(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 Da=e=>Array.from({length:e},()=>new Array(e).fill(0));function Zh(e,t,n,r,o){e.value+=t*Math.log(n);let s=r.length;for(let i=0;i<s;i++){e.gradient[i]+=t*r[i]/n;for(let a=0;a<s;a++)e.hessian[i][a]+=t*(o[i][a]/n-r[i]*r[a]/(n*n))}}var el={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function EN(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return el;if(e===1/0)return{...el,k:Zt(t),kk:-t*Zt(t)};if(t===1/0)return{...el,h:Zt(e),hh:-e*Zt(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),s=e*e-2*n*e*t+t*t,i=Math.exp(-s/(2*r))/(2*Math.PI*o),a=(t-n*e)/o,c=(e-n*t)/o,l=Zt(e),f=Zt(t);return{r:i,h:l*oe(a),k:f*oe(c),rr:i*(n/r+(e*t*r-n*s)/(r*r)),rh:-i*(e-n*t)/r,rk:-i*(t-n*e)/r,hh:-e*l*oe(a)-n/o*l*Zt(a),hk:i,kk:-t*f*oe(c)-n/o*f*Zt(c)}}function Qh(e,t,n,r,o){let s=e.length,i=e[0].length,a=o?s+i-1:1,c={value:0,gradient:new Array(a).fill(0),hessian:Da(a)},l=h=>o&&h>=1&&h<=s-1?h:-1,f=h=>o&&h>=1&&h<=i-1?s-1+h:-1,m=t.map(h=>n.map(p=>EN(h,p,r))),u=[];for(let h=0;h<s;h++)for(let p=0;p<i;p++){let d=e[h][p];if(d===0)continue;let g=lr([t[h],n[p]],[t[h+1],n[p+1]],r),w=new Array(a).fill(0),N=Da(a);for(let[b,E,v]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let y=m[b][E],T=l(b),M=f(E);w[0]+=v*y.r,N[0][0]+=v*y.rr,T>=0&&(w[T]+=v*y.h,N[0][T]+=v*y.rh,N[T][0]+=v*y.rh,N[T][T]+=v*y.hh),M>=0&&(w[M]+=v*y.k,N[0][M]+=v*y.rk,N[M][0]+=v*y.rk,N[M][M]+=v*y.kk),T>=0&&M>=0&&(N[T][M]+=v*y.hk,N[M][T]+=v*y.hk)}Zh(c,d,g,w,N),u.push(d*Math.log(g))}return c.value=Ie(u),c}function TN(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:Da(s)},a=(1-r)*(1+r),c=Math.sqrt(a),l=a*c,f=new Array(s),m=Da(s),u=[],h=(p,d,g)=>{let w=n[p],N=(w-r*d)/c,b=Zt(N),E=(r*w-d)/l,v=1/c,y=w/l+3*r*(r*w-d)/(l*a),T=r/l;if(f[0]+=g*b*E,m[0][0]+=g*b*(y-N*E*E),o){let M=p+1;f[M]+=g*b*v;let R=g*b*(T-N*E*v);m[0][M]+=R,m[M][0]+=R,m[M][M]+=g*b*(-N*v*v)}};for(let p=0;p<e.length;p++){let d=e[p],g=t[p];f.fill(0);for(let b of m)b.fill(0);let w=g<n.length?(n[g]-r*d)/c:1/0,N=g>0?(n[g-1]-r*d)/c:-1/0;g<n.length&&h(g,d,1),g>0&&h(g-1,d,-1),Zh(i,1,Gh(N,w),f,m),u.push(Math.log(Gh(N,w)),Zt(d,{log:!0}))}return i.value=Ie(u),i}function RN(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(i=>e.gradient[i]),o=t.map(i=>t.map(a=>-e.hessian[i][a])),s;try{s=ot(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||Ie(s.map((i,a)=>i*r[a]))<=0)&&(s=r.map((i,a)=>i/Math.max(Math.abs(o[a][a]),1))),t.forEach((i,a)=>n[i]=s[a]),n}var Jh=(e,t)=>e>=t-1e-12*Math.abs(t);function Xh(e,t,n){let r=[...t],o=e(r),s=n,i=r.map((a,c)=>c);for(let a=0;a<200;a++){let c=s?i.slice(1):i;if(c.length===0)break;let l=RN(o,c);if(!s&&Math.abs(r[0])===on&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>on?(Math.sign(l[0])*on-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((d,g)=>d+f*l[g]);f*l[0]!==0&&Math.abs(h[0])>=on*(1-1e-15)&&(h[0]=Math.sign(h[0])*on);let p=e(h);if(Jh(p.value,o.value)){let d=kt(h.map((g,w)=>Math.abs(g-r[w])/Math.max(1,Math.abs(r[w]))));r=h,o=p,m=d>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function ep(e,t){let n=Xh(e,[Math.max(-on,Math.min(on,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Xh(e,[Math.sign(n.theta[0])*on,...n.theta.slice(1)],!0);return Jh(r.at.value,n.at.value)?r:n}function tp(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((i,a)=>(i+r[a][s])/2))}var np=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function xN(e,t,n){lt(t)&&z(t,jh,"polychor"),z(n,jh,"polychor");let r,o;if(Uh(t)){let O=e,D=t;if(O.length!==D.length)throw new RangeError("'x' and 'y' must have the same length");let k=O.map((P,Q)=>!Number.isNaN(P)&&!Number.isNaN(D[Q])),L=Pt(O.filter((P,Q)=>k[Q])),q=Pt(D.filter((P,Q)=>k[Q])),$=new Map(nl(L).map((P,Q)=>[P,Q])),V=new Map(nl(q).map((P,Q)=>[P,Q]));r=Array.from({length:$.size},()=>new Array(V.size).fill(0)),L.forEach((P,Q)=>r[$.get(P)][V.get(q[Q])]++),o=n??{}}else{let O=e;if(!O.every(Uh))throw new TypeError("give a table (array of rows) or two vectors");let D=O[0]?.length??0;if(O.some(k=>k.length!==D))throw new TypeError("the table must have rows of equal length");if(r=O.map(k=>Array.from(k)),r.some(k=>k.some(L=>!(L>=0&&L<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:i=!1}=o,a=[],c=0;Ie(r.map(O=>Ie(O)))<2**1e3||({groups:r,k:c}=Re(r));let l=r.map(O=>Ie(O)),f=l.filter(O=>O===0).length;f>0&&(a.push(Bh(f,"row")),r=r.filter((O,D)=>l[D]!==0));let m=(r[0]??[]).map((O,D)=>Ie(r.map(k=>k[D]))),u=m.filter(O=>O===0).length;u>0&&(a.push(Bh(u,"column")),r=r.map(O=>O.filter((D,k)=>m[k]!==0)));let h=r.length,p=r[0]?.length??0;if(h<2&&a.push("the table has fewer than 2 rows"),p<2&&a.push("the table has fewer than 2 columns"),h<2||p<2)return J("polychor",{rho:NaN,warnings:a});let d=[-1/0,...tl(r.map(O=>Ie(O))),1/0],g=[-1/0,...tl(r[0].map((O,D)=>Ie(r.map(k=>k[D])))),1/0],N=Gl(O=>{let D=Math.max(-on,Math.min(on,O)),k=[];for(let L=0;L<h;L++)for(let q=0;q<p;q++){if(r[L][q]===0)continue;let $=lr([d[L],g[q]],[d[L+1],g[q+1]],D);k.push(r[L][q]*Math.log($))}return-Ie(k)},-on,on).optimum;if(!s&&!i)return J("polychor",{rho:N,warnings:a});let b=O=>[-1/0,...O,1/0],E=d.slice(1,h),v=g.slice(1,p),y=O=>s?Qh(r,b(O.slice(1,h)),b(O.slice(h)),O[0],!0):Qh(r,d,g,O[0],!1),{theta:T,at:M,bounded:R}=ep(y,s?[N,...E,...v]:[N]),x=T[0];if(R&&a.push(np(x)),!i)return J("polychor",{rho:x,warnings:a});let A=s?b(T.slice(1,h)):d,S=s?b(T.slice(h)):g,I=Ie(r.map(O=>Ie(O))),C=[],_=[];for(let O=0;O<h;O++)for(let D=0;D<p;D++){let k=r[O][D],L=lr([A[O],S[D]],[A[O+1],S[D+1]],x);if(k===0)C.push(I*L);else if(c===0)C.push(k*Math.log1p(1e-6/k),k*Hh(I*L/k-1));else{let q=W(k,c);_.push(q*Math.log1p(1e-6/q)),C.push(k*Hh(I*L/k-1))}}let F=tp(M.hessian,R);return J("polychor",{rho:x,warnings:a,var:c===0?F:F.map(O=>O.map(D=>W(D,-c))),chisq:c===0?2*Ie(C):W(2*Ie(C),c)+2*Ie(_),df:h*p-h-p,n:W(I,c),rowCuts:A.slice(1,h),colCuts:S.slice(1,p)})}function SN(e,t,n,r){let o=e.length,s=[re(.25),re(.5),re(.75)],i=(h,p)=>p.filter(d=>d<h).length,a=rl(e).map(h=>i(h,n)),c=rl(t).map(h=>i(h,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));a.forEach((h,p)=>l[h][c[p]]++);let f=[-1/0,...n,1/0],m=[-1/0,...s,1/0],u=[];return l.forEach((h,p)=>h.forEach((d,g)=>{if(d===0)return;let w=lr([f[p],m[g]],[f[p+1],m[g+1]],r);u.push(d*Math.log((d+1e-6)/(w*o)))})),2*Ie(u)}function AN(e,t,n={}){if(z(n,yN,"polyserial"),e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,s=e.map((M,R)=>!Number.isNaN(M)&&!Number.isNaN(t[R])),i=e.filter((M,R)=>s[R]),a=Pt(t.filter((M,R)=>s[R])),c=i.length,l=nl(a);if(l.length<2)return J("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((M,R)=>[M,R+1])),m=a.map(M=>f.get(M)),u=l.map(()=>0);for(let M of m)u[M-1]++;let h=tl(u),p=[],d=Math.sqrt((c-1)/c)*Math.sqrt(st(m))*Hl(i,m)[0]/Ie(h.map(M=>Zt(M)));if(Math.abs(d)>on){let M=Math.sign(d)*on;p.push(`initial correlation inadmissible, ${Hn(d)}, set to ${Hn(M)}`),d=M}if(!r&&!o)return J("polyserial",{rho:d,warnings:p});let g=rl(i),w=m.map(M=>M-1),N=M=>TN(g,w,r?M.slice(1):h,M[0],r),{theta:b,at:E,bounded:v}=ep(N,r?[d,...h]:[d]);if(v&&p.push(np(b[0])),!o)return J("polyserial",{rho:b[0],warnings:p});let y=r?b.slice(1):h,T=l.length;return J("polyserial",{rho:b[0],warnings:p,var:tp(E.hessian,v),chisq:SN(m,g,y,r?b[0]:d),df:4*T-T-1,n:c,cuts:y})}var kN=["group","strata","rho","cencode"],qN=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function Va(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function ol(e){let t=rn(e),n=Array.from(new Set(t)).sort(Qn);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function IN(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 _N(e,t,n={}){z(n,kN,"cuminc"),IN(e,t,n);let{group:r,strata:o,rho:s=0,cencode:i=0}=n,a=[],c=new pt;for(let v=0;v<e.length;v++)if(!(Number.isNaN(e[v])||Va(t[v])||r!==void 0&&Va(r[v])||o!==void 0&&Va(o[v]))){if(!Number.isFinite(e[v]))throw new RangeError("'time' must contain only finite values");if(e[v]<0)throw new RangeError("'time' must be non-negative");a.push(v)}let l=e.length-a.length;if(l>0&&c.add(`${l} cases omitted due to missing values`),a.length===0)throw new RangeError("no complete observations");let f=r===void 0?[1]:rn(a.map(v=>r[v])),m=o===void 0?[1]:rn(a.map(v=>o[v])),u=rn([...a.map(v=>t[v]),i]),h=ol(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let p=ol(m),d=ol(u),g=u[u.length-1],w=d.labels.filter(v=>v!==g);if(w.length===0)throw new Error("there are no events of any cause");let N=a.map((v,y)=>({time:e[v],status:u[y],group:r===void 0?0:h.index.get(f[y]),strata:o===void 0?0:p.index.get(m[y]),source:v})).sort((v,y)=>v.time-y.time),b=[];for(let v of w){let y=xt(v);for(let T=0;T<h.labels.length;T++){let M=N.filter(k=>k.group===T),R=M.reduce((k,L)=>Math.max(k,L.time),-1/0),x=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,A=Nr(M.map(k=>k.time*x),M.map(k=>k.status===g?0:1)),S=[0],I=[0],C=[0],_=[],F=1;for(let k=0;k<A.time.length;k++){let L=A.nEvent[k];if(L===0)continue;let q=A.time[k]/x,$=A.nRisk[k],V=M.filter(P=>P.time===q&&P.status===v).length;_.push({t:q,Y:$,d:L,dk:V,sb:F,sa:A.surv[k]}),F=A.surv[k]}let O=0,D=0;for(let k=0;k<_.length;k++){let L=_[k],q=O,$=D;O+=L.sb*L.dk/L.Y;let V=0;for(let P=0;P<=k;P++){let Q=_[P],K=Q.Y,X=Q.d,Y=Q.dk,se=K===X?0:(X*(K-X)+2*Y*(X-Y))/((K-1)*(K-X)**2),H=K===1?Y:Y*(K-Y)/(K*K*(K-1)),j=K===X||K===1?0:Y*(K-Y)/(K*(K-X)*(K-1)),te=0;for(let Z=0;Z<=P;Z++){let U=_[Z];te+=U.sb*U.dk/U.Y}let B=O-te;V+=B*B*se+Q.sb*Q.sb*H-2*B*Q.sb*j}D=Math.max(0,V),L.dk>0&&(S.push(L.t,L.t),I.push(q,O),C.push($,D))}S.push(R),I.push(O),C.push(D),b.push({name:`${xt(h.labels[T])} ${y}`,group:xt(h.labels[T]),cause:y,time:S,est:I,var:C})}}let E=h.labels.length>=2?CN(N,h.labels,w,p.labels.length,s,g):void 0;return J("cuminc",{curves:b,...E===void 0?{}:{tests:E},warnings:c.list})}function CN(e,t,n,r,o,s){let i=[];for(let a of n){let c=t.length,l=new Array(c).fill(0),f=Array.from({length:c},()=>new Array(c).fill(0));for(let d=0;d<r;d++){let g=e.filter(v=>v.strata===d),w=Array.from(new Set(g.filter(v=>v.status!==s).map(v=>v.time))).sort((v,y)=>v-y),N=new Array(c).fill(1),b=new Array(c).fill(0),E=[];for(let v of w){let y=new Array(c).fill(0),T=new Array(c).fill(0),M=new Array(c).fill(0);for(let k of g)k.time>=v&&y[k.group]++;for(let k of g)k.time===v&&k.status!==s&&(k.status===a?T[k.group]++:M[k.group]++);let R=N.slice(),x=b.slice(),A=y.map((k,L)=>k===0?0:k/R[L]),S=y.map((k,L)=>k===0?0:k*(1-x[L])/R[L]),I=T.reduce((k,L)=>k+L,0),C=A.reduce((k,L)=>k+L,0),_=C===0?0:I/C,F=E.length===0?1:E[E.length-1].Gafter,O=F-_,D=S.reduce((k,L)=>k+L,0);for(let k=0;k<c;k++){let L=F**o;D>0&&(l[k]+=L*(T[k]-S[k]*I/D)),y[k]>0?(N[k]=N[k]*(1-(T[k]+M[k])/y[k]),b[k]+=R[k]*T[k]/y[k]):N[k]=0}E.push({time:v,Y:y,d1:T,d2:M,Sbefore:R,Safter:N.slice(),Fbefore:x,h:A,R:S,dF0:_,Gbefore:F,Gafter:O})}for(let v=0;v<c;v++)for(let y=0;y<c;y++){let T=0;for(let M=0;M<c;M++)for(let R=0;R<E.length;R++){let x=E[R],A=x.h.reduce((X,Y)=>X+Y,0);if(!(A>0)||!(x.h[M]>0))continue;let S=x.Gbefore**o,I=S*x.h[v]*((v===M?1:0)-x.h[M]/A),C=S*x.h[y]*((y===M?1:0)-x.h[M]/A),_=0,F=0;for(let X=R+1;X<E.length;X++){let Y=E[X],se=Y.h.reduce((Z,U)=>Z+U,0);if(!(se>0))continue;let H=Y.Gbefore**o,j=H*Y.h[v]*((v===M?1:0)-Y.h[M]/se),te=H*Y.h[y]*((y===M?1:0)-Y.h[M]/se),B=Y.Gbefore===0?0:Y.dF0/Y.Gbefore;_+=j*B,F+=te*B}let O=x.Safter[M]===0?0:x.Gafter/x.Safter[M],D=I+(1-O)*_,k=C+(1-O)*F,L=-O*_,q=-O*F,$=x.Y[M]===0?0:x.Sbefore[M]*x.d2[M]/x.Y[M],V=(D*k*x.dF0+L*q*$)/x.h[M],P=x.d1.reduce((X,Y)=>X+Y,0),Q=0;if(P>1&&x.Y[M]>0&&x.Sbefore[M]>0){let X=1/x.Sbefore[M];A>X&&(Q=P*(P-1)/A*(x.Y[M]*X*X/(A-X))*(D*k/(x.h[M]*x.h[M])))}let K=0;x.d2[M]>1&&x.Y[M]>1&&(K=(x.d2[M]-1)/(x.Y[M]-1)*($/x.h[M])*L*q),T+=V-Q-K}f[v][y]+=T}}let m=c-1,u=NaN,h=NaN,p=f.slice(0,m).map(d=>d.slice(0,m));try{let d=ot(p,l.slice(0,m),{tol:1e-12});u=l.slice(0,m).reduce((g,w,N)=>g+w*d[N],0),h=Ze(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}i.push({cause:xt(a),stat:u,df:m,pValue:h})}return i}function LN(e,t){let n=Array.from(new Set(t.filter(s=>!Number.isNaN(s)))).sort((s,i)=>s-i),r=[],o=[];for(let s of e.curves){let i=[],a=[];for(let c of n){let l=-1;for(let f=0;f<s.time.length;f++)s.time[f]<=c&&(l=f);c<0||l<0||c>s.time[s.time.length-1]?(i.push(NaN),a.push(NaN)):(i.push(s.est[l]),a.push(s.var[l]))}r.push(i),o.push(a)}return{curveNames:e.curves.map(s=>s.name),times:n,est:r,var:o}}function zn(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function ON(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 sl(e,t){return Ie(e.map((n,r)=>n*t[r]))}function FN(e){return e.every(t=>t.every(Number.isFinite))}function PN(e,t,n,r={}){z(r,qN,"crr");let{cov2:o,tf:s,failcode:i=1,cencode:a=0,gtol:c=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(!(c>0)||!Number.isFinite(c))throw new RangeError("'gtol' must be finite and positive");if(!Number.isInteger(l)||l<0)throw new RangeError("'maxiter' must be a non-negative integer");if(!(u>0&&u<1))throw new RangeError("'confLevel' must be between 0 and 1");let p=n?.[0]?.length??0,d=o?.[0]?.length??0,g=p+d;if(g===0)throw new RangeError("at least one covariate is required");if(n!==void 0&&n.some(U=>U.length!==p))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(U=>U.length!==d))throw new RangeError("rows of 'cov2' must have equal length");if(f!==void 0&&f.length!==g)throw new RangeError("'init' must have one value per coefficient");if(h!==void 0&&h.length!==g)throw new RangeError("'names' must have one name per coefficient");let w=[];for(let U=0;U<e.length;U++)if(!(Number.isNaN(e[U])||Va(t[U])||n!==void 0&&n[U].some(Number.isNaN)||o!==void 0&&o[U].some(Number.isNaN))){if(!Number.isFinite(e[U]))throw new RangeError("'time' must contain only finite values");if(n!==void 0&&!n[U].every(Number.isFinite)||o!==void 0&&!o[U].every(Number.isFinite))throw new RangeError("covariates must be finite");w.push(U)}let N=e.length-w.length;if(w.length===0)throw new RangeError("no complete observations");let b=w.map(U=>e[U]),E=rn([...w.map(U=>t[U]),a,i]),v=E.slice(0,w.length),y=E[E.length-2],T=E[E.length-1],M=w.map(U=>n===void 0?[]:Array.from(n[U])),R=w.map(U=>o===void 0?[]:Array.from(o[U])),x=Array.from(new Set(b.filter((U,ee)=>v[ee]===T))).sort((U,ee)=>U-ee);if(x.length===0)throw new RangeError("no observations of 'failcode'");let A=d===0?[[0]]:s(x).map(U=>Array.from(U));if(d>0&&(A.length!==x.length||A.some(U=>U.length!==d)||!FN(A)))throw new RangeError("'tf' must return a finite matrix with one row per unique failure time and one column per cov2 column");let S=x.map((U,ee)=>b.map((ae,ve)=>[...M[ve],...R[ve].map((Ne,ue)=>Ne*(A[ee][ue]??0))])),I=b.reduce((U,ee)=>Math.max(U,Math.abs(ee)),0),C=I>0&&I<1?2**Math.min(1023,Math.ceil(-Math.log2(I))):1,_=Nr(b.map(U=>U*C),v.map(U=>U===y?1:0)),F=U=>{let ee=U*C,ae=1;for(let ve=0;ve<_.time.length&&!(_.time[ve]>=ee);ve++)ae=_.surv[ve];return ae},O=x.map(U=>b.map((ee,ae)=>{if(ee>=U)return 1;if(v[ae]===y||v[ae]===T)return 0;let ve=F(ee);return ve===0?0:F(U)/ve}));for(let U=0;U<x.length;U++)for(let ee=0;ee<b.length;ee++)b[ee]===x[U]&&v[ee]===T&&(O[U][ee]=1);let D=U=>{let ee=Array.from({length:g},()=>[]),ae=Array.from({length:g},()=>Array.from({length:g},()=>[])),ve=[],Ne=zn(x.length,g),ue=[],fe=[],Ee=[];for(let Se=0;Se<x.length;Se++){let Ye=x[Se],De=b.map((de,Ae)=>de===Ye&&v[Ae]===T?Ae:-1).filter(de=>de>=0),Je=De.length,Be=S[Se],Xe=Be.map(de=>sl(U,de)),me=O[Se].slice();for(let de of De)me[de]=1;let ne=Math.max(...Xe.map((de,Ae)=>me[Ae]>0?de:-1/0));if(ne===-1/0)throw new Error("empty Fine-Gray risk set");let we=ne<500&&ne>-500,ge=we?Xe.map((de,Ae)=>me[Ae]*Math.exp(de)):Xe.map((de,Ae)=>me[Ae]*Math.exp(de-ne)),le=Ie(ge),et=Array.from({length:g},(de,Ae)=>Ie(ge.map((We,Fe)=>We*Be[Fe][Ae])));if(!(le>0))throw new Error("empty Fine-Gray risk set");let Oe=et.map(de=>de/le),ke=Array.from({length:g},(de,Ae)=>{let We=Be[0][Ae],Fe=0,rt=0;for(let dt of Be)Fe=Math.max(Fe,Math.abs(dt[Ae]-We)),rt=Math.max(rt,Math.abs(dt[Ae]));return Fe>0&&rt/Fe>1e6}).some(Boolean),$e=ke?Array.from({length:g},(de,Ae)=>Be.map(We=>We[Ae]-Be[0][Ae])):[],He=ke?$e.map(de=>Ie(de.map((Ae,We)=>ge[We]*Ae))/le):[],ce=ke?void 0:Array.from({length:g},(de,Ae)=>Array.from({length:g},(We,Fe)=>Ie(ge.map((rt,dt)=>rt*Be[dt][Ae]*Be[dt][Fe]))));fe.push(Oe),Ee.push(ge.map(de=>de/le));let xe=we?Je/le:Je*Math.exp(-ne)/le;ue.push(xe),ve.push(Ie(De.map(de=>sl(U,Be[de])))-Je*(we?Math.log(le):ne+Math.log(le)));for(let de=0;de<g;de++){let We=Ie(De.map(Fe=>Be[Fe][de]))-Je*Oe[de];ee[de].push(We),Ne[Se][de]=We}for(let de=0;de<g;de++)for(let Ae=0;Ae<g;Ae++){let We=ke?Ie(ge.map((Fe,rt)=>Fe*($e[de][rt]-He[de])*($e[Ae][rt]-He[Ae])))/le:ce[de][Ae]/le-Oe[de]*Oe[Ae];ae[de][Ae].push(Je*We)}}let pe=zn(b.length,g);for(let Se=0;Se<x.length;Se++){let Ye=S[Se],De=Ee[Se],Je=fe[Se],Be=x[Se];for(let Xe=0;Xe<b.length;Xe++){let me=b[Xe]===Be&&v[Xe]===T?1:0,ne=b.filter((ge,le)=>ge===Be&&v[le]===T).length,we=me-De[Xe]*ne;for(let ge=0;ge<g;ge++)pe[Xe][ge]+=(Ye[Xe][ge]-Je[ge])*we}}let Ce=ee.map(Ie),be=ae.map(Se=>Se.map(Ie));return{loglik:Ie(ve),score:Ce,inf:be,bfitj:ue,res:Ne,eta:pe,means:fe,riskFactors:Ee,zByTime:S}},k=f===void 0?new Array(g).fill(0):Array.from(f);if(!k.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let L=k.slice(),q=D(L),$=!1,V=0,P=(U,ee)=>Math.max(...U.score.map((ae,ve)=>Math.abs(ae)*Math.max(Math.abs(ee[ve]),1)))<c*Math.max(Math.abs(U.loglik),1);for(;;){if(P(q,L)){$=!0;break}if(V>=l)break;let U=ot(q.inf,q.score,{tol:0});L=L.map((ee,ae)=>ee+U[ae]),V++,q=D(L)}let Q=ot(q.inf),K=zn(g);if(m){let U=zn(g),ee=b.length,ae=Array.from(new Set(b)).sort((fe,Ee)=>fe-Ee),ve=ae.map(fe=>{let Ee=b.filter(Ce=>Ce>=fe).length,pe=b.filter((Ce,be)=>Ce===fe&&v[be]===y).length;return Ee===0?0:pe/Ee}),Ne=zn(ee,g);for(let fe=0;fe<ae.length;fe++){let Ee=ae[fe],pe=new Array(g).fill(0);for(let be=0;be<ee;be++)if(!(!(b[be]<Ee)||v[be]===y||v[be]===T))for(let ye=0;ye<x.length;ye++){if(x[ye]<Ee)continue;let Se=q.riskFactors[ye][be],Ye=b.filter((Be,Xe)=>Be===x[ye]&&v[Xe]===T).length,De=q.zByTime[ye][be],Je=q.means[ye];for(let Be=0;Be<g;Be++)pe[Be]+=(De[Be]-Je[Be])*Se*Ye}let Ce=b.filter(be=>be>=Ee).length;if(Ce!==0)for(let be=0;be<ee;be++){let ye=b[be]===Ee&&v[be]===y?1:0,Se=b[be]>=Ee?ve[fe]:0;for(let Ye=0;Ye<g;Ye++)Ne[be][Ye]+=pe[Ye]/Ce*(ye-Se)}}for(let fe=0;fe<ee;fe++)ON(U,q.eta[fe].map((Ee,pe)=>Ee+Ne[fe][pe]),q.eta[fe].map((Ee,pe)=>Ee+Ne[fe][pe]),1);K=zn(g);let ue=rp(Q,U);K=rp(ue,Q)}else K=zn(g).map(U=>U.map(()=>NaN));let X=m?K.map((U,ee)=>Math.sqrt(U[ee])):void 0,Y=X===void 0?void 0:L.map((U,ee)=>U/X[ee]),se=Y===void 0?void 0:Y.map(U=>2*oe(-Math.abs(U))),H=m?re((1+u)/2):void 0,j=H===void 0?void 0:L.map((U,ee)=>Math.exp(U-H*X[ee])),te=H===void 0?void 0:L.map((U,ee)=>Math.exp(U+H*X[ee])),B=m?L.map(Math.exp):void 0,Z=h===void 0?[...Array.from({length:p},(U,ee)=>`cov1${ee+1}`),...Array.from({length:d},(U,ee)=>`cov2${ee+1}*tf${ee+1}`)]:Array.from(h);return J("crr",{coefficients:L,names:Z,var:K,...X===void 0?{}:{se:X,z:Y,pValue:se,lower:j,upper:te,expCoef:B},loglik:q.loglik,loglikNull:D(new Array(g).fill(0)).loglik,logtest:{statistic:2*(q.loglik-D(new Array(g).fill(0)).loglik),df:g},score:q.score,inf:m?q.inf:zn(g).map(U=>U.map(()=>NaN)),invinf:m?Q:zn(g).map(U=>U.map(()=>NaN)),converged:$,iterations:V,n:w.length,nMissing:N,uftime:x,bfitj:q.bfitj,res:m?q.res:null,tfs:A,warnings:[],covariateDimensions:[p,d]})}function rp(e,t){let n=zn(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 DN(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(c=>c.length!==r))throw new RangeError("'cov1' has the wrong number of columns");if(o>0&&n.some(c=>c.length!==o))throw new RangeError("'cov2' has the wrong number of columns");if(r>0&&o>0&&t.length!==n.length)throw new RangeError("'cov1' and 'cov2' must have the same number of rows");let s=r>0?t.length:n.length,i=o===0?e.uftime.map(()=>[0]):e.tfs;return e.uftime.map((c,l)=>{let f=[c];for(let m=0;m<s;m++){let u=0;for(let h=0;h<=l;h++){let p=[...r===0?[]:t[m],...o===0?[]:n[m].map((b,E)=>b*i[h][E])],d=e.bfitj[h],g=sl(e.coefficients,p),w=d*Math.exp(g),N=Number.isFinite(w)&&(w!==0||d===0)?w:d===0?0:Math.exp(Math.log(d)+g);u+=N}f.push(-Math.expm1(-u))}return f})}var VN=["n","power","r","type","nfractional","alpha","sidedTest"],$N=["n","power","r","type","nfractional","alpha","sidedTest"],zN=["n","power","r","type","nfractional","alpha","sidedTest"],KN=["n","power","r","type","nfractional","alpha","sidedTest"],YN=["n","power","r","type","nfractional","alpha","sidedTest"],WN=["n","power","r","type","nfractional","alpha","sidedTest"],jN=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],BN=["alpha"],UN=["alpha"],GN=["alpha"],HN=["alpha"],QN=["alpha"],XN=["nmax"],Hr=.05,un=e=>re(e),tr=e=>oe(e),Gr=e=>Number.isFinite(e),qt=e=>Gr(e)&&e>=0&&e<=1,An=e=>Gr(e)&&e>0;function Qe(e,t,n,r){if(typeof e!="number")throw new TypeError(`'${t}' must be numeric`);if(!n(e))throw new RangeError(r)}function Qr(e){Qe(e,"alpha",qt,"'alpha' must be a probability in [0, 1]")}function ZN(e,t){return[e*t/(1+t),e/(1+t)]}function Lo(e,t,n,r,o,s,i={}){let{n:a,power:c}=i,l=i.r??1,f=i.alpha??Hr,m=i.nfractional??!1,u=e==="equivalence"?Te(i.type,["equivalence","equality"]):"equivalence",h=i.sidedTest??1;if(e==="equivalence"&&u!=="equivalence"&&u!=="equality")throw new RangeError("'type' should be one of \u201Cequivalence\u201D, \u201Cequality\u201D");if(Qe(n,"treat",Gr,"'treat' must be finite"),Qe(r,"control",Gr,"'control' must be finite"),t==="binary"?(Qe(n,"treat",qt,"'treat' must be a probability in [0, 1]"),Qe(r,"control",qt,"'control' must be a probability in [0, 1]")):Qe(o,"sigma",An,"'sigma' must be positive and finite"),Qe(l,"r",An,"'r' must be positive and finite"),Qr(f),(e!=="equivalence"||u!=="equality")&&Qe(s,"delta",I=>Gr(I)&&I>=0,"'delta' must be nonnegative and finite"),e==="superiority"&&h!==1&&h!==2)throw new RangeError("'sidedTest' must be 1 or 2");if(typeof m!="boolean")throw new TypeError("'nfractional' must be logical");Rn([a,c],"exactly one of 'n' and 'power' must be NULL"),qe(c)||Qe(c,"power",I=>qt(I)&&I>0&&I<1,"'power' must be strictly between 0 and 1"),a!=null&&Qe(a,"n",I=>Gr(I)&&I>0,"'n' must be positive and finite");let p=n-r,d=t==="binary"?n*(1-n):o*o,g=t==="binary"?r*(1-r):o*o,w=e==="equivalence"&&u==="equality",N=w?void 0:s,b=e==="noninferiority"?p+N:e==="superiority"?p-N:w?p:N-Math.abs(p);if(e==="noninferiority"&&b<=0)throw new RangeError("Target power is not reachable under the specified hypotheses");let E=e==="superiority"?re(f/h,{lowerTail:!1}):w?re(f/2,{lowerTail:!1}):re(f,{lowerTail:!1}),v=c??.8,y=w||e!=="equivalence"?re(1-v,{lowerTail:!1}):re((1-v)/2,{lowerTail:!1}),T,M,R,x,A;if(qe(a))b===0?(R=1/0,x=1/0):(x=(E+y)**2*(d/l+g)/(b*b),R=l*x),m?(T=R,M=x):(M=Math.ceil(x),T=Math.ceil(e==="equivalence"&&t==="continuous"||e==="superiority"?l*M:R)),A=c;else{[R,x]=ZN(a,l),T=m?R:Math.ceil(R),M=m?x:Math.ceil(x);let I=Math.sqrt(d/T+g/M);if(e==="equivalence"&&!w)A=Math.max(0,tr((s-p)/I-un(1-f))+tr((s+p)/I-un(1-f))-1);else if(w)A=tr(Math.abs(p)/I-un(1-f/2))+tr(-Math.abs(p)/I-un(1-f/2));else if(e==="noninferiority")A=tr((p+s)/I-un(1-f));else{let C=Math.abs(p-s)/I;A=tr(C-E)+tr(-C-E)}}return{nTotal:T+M,nTreat:T,nControl:M,nTotalUnrounded:R+x,nTreatUnrounded:R,nControlUnrounded:x,power:A,...N===void 0?{}:{delta:N},warnings:[]}}function JN(e,t,n,r={}){return z(r,VN,"epiSsequb"),J("epiSsequb",Lo("equivalence","binary",e,t,void 0,n??void 0,r))}function eM(e,t,n,r,o={}){z(o,$N,"epiSsequc");let s=Lo("equivalence","continuous",e,t,n,r??void 0,o);return J("epiSsequc",s)}function tM(e,t,n,r={}){return z(r,zN,"epiSsninfb"),J("epiSsninfb",Lo("noninferiority","binary",e,t,void 0,n,r))}function nM(e,t,n,r,o={}){return z(o,KN,"epiSsninfc"),J("epiSsninfc",Lo("noninferiority","continuous",e,t,n,r,o))}function rM(e,t,n,r={}){return z(r,YN,"epiSssupb"),J("epiSssupb",Lo("superiority","binary",e,t,void 0,n,r))}function oM(e,t,n,r,o={}){return z(o,WN,"epiSssupc"),J("epiSssupc",Lo("superiority","continuous",e,t,n,r,o))}function Co(e,t){let n=new Array(e+1).fill(0);if(t===0)return n[0]=1,n;if(t===1)return n[e]=1,n;if(t>.5)return Co(e,1-t).reverse();let r=Math.floor((e+1)*t);n[r]=Math.exp(e*Math.log1p(-t)+r*Math.log(t/(1-t))+Array.from({length:r},(o,s)=>Math.log(e-s)-Math.log(s+1)).reduce((o,s)=>o+s,0));for(let o=r;o>0;o--)n[o-1]=n[o]*(o/(e-o+1))*((1-t)/t);for(let o=r;o<e;o++)n[o+1]=n[o]*((e-o)/(o+1))*(t/(1-t));return n}function sM(e,t,n,r={}){z(r,jN,"powerPaired2x2");let o=r.sigLevel??Hr,s=Te(r.alternative,["two.sided","one.sided"]),i=r.strict??!1,a=r.nullOddsRatio??1,c=r.errbound??1e-6,l=Te(r.tsmethod,["central","minlike","blaker"]);if(Qe(e,"pb",qt,"'pb' must be in [0, 1]"),Qe(t,"pc",qt,"'pc' must be in [0, 1]"),Qe(n,"npairs",y=>Number.isSafeInteger(y)&&y>=0&&y<=1e5,"'npairs' must be a nonnegative safe integer"),Qr(o),Qe(a,"nullOddsRatio",An,"'nullOddsRatio' must be positive and finite"),Qe(c,"errbound",y=>Gr(y)&&y>=0,"'errbound' must be nonnegative and finite"),e+t>1)throw new RangeError("probabilities must be finite, non-negative and not all 0");if(s!=="two.sided"&&s!=="one.sided")throw new RangeError("'alternative' should be one of \u201Ctwo.sided\u201D, \u201Cone.sided\u201D");if(!["central","minlike","blaker"].includes(l))throw new RangeError("'tsmethod' should be one of \u201Ccentral\u201D, \u201Cminlike\u201D, \u201Cblaker\u201D");let f=e+t,m=f===0?.5:e/f,u=a/(1+a),h=e>=t?1:-1,p=s==="two.sided"&&i&&l!=="blaker",d=s==="one.sided",g=Co(n,f),w=0,N=n;if(c>0){let y=0;for(;w<=n&&(y+=g[w])<=c/2;)w++;for(y=0;N>=0&&(y+=g[N])<=c/2;)N--}let b=0;for(let y=w;y<=N;y++){let T=g[y];if(T===0)continue;let M=Co(y,m),R=Co(y,u),x=0,A=0,S=new Array(y+1),I=new Array(y+1);for(let C=0;C<=y;C++)x+=R[C],S[C]=x;for(let C=y;C>=0;C--)A+=R[C],I[C]=A;for(let C=0;C<=y;C++){let _=S[C],F=I[C],O,D=u===.5&&o===1;d?O=D||(h>0?F<=o:_<=o):p?O=D||_<=o/2||F<=o/2:O=h>0?D?C>Math.floor(y/2):F<=o/2:D?C<Math.ceil(y/2):_<=o/2,O&&(b+=T*M[C])}}let E=`errbound= ${c===0?"0":c>=.001?String(c):`${c.toExponential(0).replace(/e([+-]?)(\\d+)$/,(y,T,M)=>`e${T||"+"}${M.padStart(2,"0")}`)}`}${e+t===1?" no non-informative pairs: pa+pd=0":""}`,v="Power for McNemar's Exact Test"+(p?", including power to reject in the wrong direction":"")+(l==="central"&&r.tsmethod!==void 0?`, using (tsmethod=${l})`:l==="minlike"?`, using (tsmethod=${l})`:l==="blaker"?`, using (tsmethod=${l})`:"");return J("powerPaired2x2",{power:b,npairs:n,pb:e,pc:t,sigLevel:o,alternative:s,nullOddsRatio:a,note:E,method:v,warnings:[]})}function aM(e,t,n,r,o,s={}){z(s,UN,"powerCT");let i=s.alpha??Hr;Qr(i);for(let[l,f]of[[e,"nE"],[t,"nC"]])Qe(l,f,An,`'${f}' must be positive and finite`);for(let[l,f]of[[n,"pE"],[r,"pC"]])Qe(l,f,qt,`'${f}' must be in [0, 1]`);Qe(o,"RR",An,"'RR' must be positive and finite");let a=e/t,c=e*n+t*r;return tr(Math.sqrt(a*c)*Math.abs(o-1)/(a*o+1)-un(1-i/2))}function iM(e,t,n,r,o,s={}){z(s,BN,"ssizeCT");let i=s.alpha??Hr;Qr(i),Qe(e,"power",f=>qt(f)&&f>0&&f<1,"'power' must be strictly between 0 and 1"),Qe(t,"k",An,"'k' must be positive and finite"),Qe(n,"pE",qt,"'pE' must be in [0, 1]"),Qe(r,"pC",qt,"'pC' must be in [0, 1]"),Qe(o,"RR",An,"'RR' must be positive and finite");let a=un(1-i/2)+un(e),c=1/t*((t*o+1)/(o-1))**2*(a*a),l=t*n+r;return{nE:Math.ceil(c*t/l),nC:Math.ceil(c/l)}}function cM(e,t,n,r,o,s={}){z(s,HN,"powerEpi");let i=s.alpha??Hr;Qr(i),Qe(e,"n",An,"'n' must be positive and finite"),Qe(t,"theta",An,"'theta' must be positive and finite"),Qe(n,"p",qt,"'p' must be in [0, 1]"),Qe(r,"psi",qt,"'psi' must be in [0, 1]"),Qe(o,"rho2",qt,"'rho2' must be in [0, 1]");let a=e*Math.log(t)**2*n*(1-n)*r*(1-o);return tr(Math.sqrt(a)-un(1-i/2))}function lM(e,t,n,r,o,s={}){z(s,GN,"ssizeEpi");let i=s.alpha??Hr;Qr(i),Qe(e,"power",qt,"'power' must be in [0, 1]"),Qe(t,"theta",An,"'theta' must be positive and finite"),Qe(n,"p",qt,"'p' must be in [0, 1]"),Qe(r,"psi",qt,"'psi' must be in [0, 1]"),Qe(o,"rho2",qt,"'rho2' must be in [0, 1]");let a=Math.log(t)**2*n*(1-n)*r*(1-o);return Math.ceil((un(1-i/2)+un(e))**2/a)}function fM(e,t,n,r,o={}){z(o,QN,"numDEpi");let s=o.alpha??Hr;Qr(s),Qe(e,"power",a=>qt(a)&&a>0&&a<1,"'power' must be strictly between 0 and 1"),Qe(t,"theta",An,"'theta' must be positive and finite"),Qe(n,"p",qt,"'p' must be in [0, 1]"),Qe(r,"rho2",qt,"'rho2' must be in [0, 1]");let i=Math.log(t)**2*n*(1-n)*(1-r);return Math.ceil((un(1-s/2)+un(e))**2/i)}function op(e){let t=new Array(e.length),n=0,r=0;for(let o=0;o<e.length;o++){let s=e[o]-r,i=n+s;r=i-n-s,n=i,t[o]=n}return t}function mM(e){let t=new Array(e.length),n=0,r=0;for(let o=e.length-1;o>=0;o--){let s=e[o]-r,i=n+s;r=i-n-s,n=i,t[o]=n}return t}function sp(e,t,n,r,o,s){let i=0,a=0;a=r.slice(0,e+1).reduce((c,l)=>c+l,0);for(let c=e+1;c<n.length;c++){let l=t-c,f=l<0?1:l+1>=o.length?0:o[l+1],m=l<0?0:s[Math.min(l,s.length-1)];i+=n[c]*f,a+=r[c]*m}return[i,a]}function uM(e,t,n,r,o={}){z(o,XN,"ph2simon");let s=o.nmax??100;for(let[E,v]of[[e,"pu"],[t,"pa"],[n,"ep1"],[r,"ep2"]])Qe(E,v,qt,`'${v}' must be in [0, 1]`);Qe(s,"nmax",E=>Number.isInteger(E)&&E>=1&&E<=1e3,"nmax cannot exceed 1000");let i=Array.from({length:s+1},(E,v)=>Co(v,e)),a=Array.from({length:s+1},(E,v)=>Co(v,t)),c=i.map(op),l=a.map(op),f=i.map(mM),m=[];for(let E=2;E<=s;E++){let v;for(let y=1;y<E&&(v===void 0||y<v.enP0);y++){let T=E-y,M=i[y],R=a[y],x=l[T],A=c[y];for(let S=0;S<y;S++){let I=A[S],C=y+T*(1-I),_=S,F=E-1,O=-1;for(;_<=F;){let D=Math.floor((_+F)/2),[,k]=sp(S,D,M,R,f[T],x);k<=r?(O=D,_=D+1):F=D-1}if(O>=0){let[D]=sp(S,O,M,R,f[T],x);D>n&&(O=-1)}O>=0&&(v===void 0||C<v.enP0)&&(v={r1:S,n1:y,r:O,n:E,enP0:C,petP0:I})}}v&&m.push(v)}if(m.length===0)throw new Error(` No feasible solution found.
|
|
20
|
+
Increase maximum sample size. Current nmax value = ${s}.`);let u=m.filter((E,v)=>!m.some((y,T)=>T!==v&&y.n<=E.n&&y.enP0<=E.enP0&&(y.n<E.n||y.enP0<E.enP0))),h=[],p=(E,v)=>(E.enP0-v.enP0)/(v.n-E.n+(E.enP0-v.enP0));for(let E of u)for(h.push(E);h.length>=3;){let v=h[h.length-3],y=h[h.length-2],T=h[h.length-1];if(p(v,y)>p(y,T))break;h.splice(h.length-2,1)}let d=h.map((E,v)=>{let y=v===h.length-1?0:p(E,h[v+1]),T=v===0?1:p(h[v-1],E);return{...E,qLo:y,qHi:T}}),g=d[0],w=d.reduce((E,v)=>v.enP0<E.enP0?v:E),N=d,b=[];return w.n>=s-1&&b.push(` Optimal sample size too close to nmax.
|
|
21
21
|
Try increasing nmax (current value = ${s})
|
|
22
|
-
`),H("ph2simon",{out:m,minimax:g,optimal:w,admissible:N,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:v})}var Lv=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),ih=["method","mods","data","intercept","test","level","control"],ch=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],Cv=["type","level"],Ov=["newmods","predtype","level"],lh=["model","predictor","retFit","level"],yn=e=>e.length===1?e[0]:e,Mn=(e,t)=>e.reduce((n,r,o)=>n+r*t[o],0),Ho=(e,t)=>e.map(n=>Mn(n,t)),hh=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),ho=(e,t,n,r=1e-14)=>{let o=e(t),s=e(n);if(o===0)return t;if(s===0)return n;if(!(o*s<0))return NaN;for(let i=0;i<180;i++){let c=t+(n-t)/2,a=e(c);if(!Number.isFinite(a)){n=c;continue}if(Math.abs(n-t)<=r*Math.max(1,Math.abs(c))||a===0)return c;o*a<=0?(n=c,s=a):(t=c,o=a)}return t+(n-t)/2},fh=(e,t)=>{let n=Math.max(t,1e-8),r=e(n);for(let o=0;o<80&&r>0;o++)n*=2,r=e(n);return r<=0?ho(e,0,n):NaN};function Gt(e,t,n,r){let o=e.length,s=n[0].length,i=t.map(F=>F+r),c=i.reduce((F,C)=>Math.min(F,C),1/0),a=i.map(F=>c/F),l=a.reduce((F,C)=>F+C,0),f=Array.from({length:s},(F,C)=>Array.from({length:s},(D,A)=>n.reduce((O,q,P)=>O+a[P]*q[C]*q[A],0))),m=Array.from({length:s},(F,C)=>n.reduce((D,A,O)=>D+a[O]*A[C]*e[O],0)),u;try{u=rt(f,hh(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=Ho(u,m),p=Ho(n,h),d=e.map((F,C)=>F-p[C]),g=e.every(F=>F===e[0])?0:d.reduce((F,C,D)=>F+a[D]*C*C,0),w=g/c,N=Array.from({length:o},(F,C)=>Array.from({length:o},(D,A)=>(C===A?a[C]:0)-a[C]*Mn(n[C],Ho(u,n[A]).map(O=>O*a[A])))),v=N.reduce((F,C,D)=>F+C[D],0),E=v/c,b=N.reduce((F,C,D)=>F+C.reduce((A,O,q)=>A+O*N[q][D],0),0),M=b/c/c,x=N.map(F=>Mn(F,e)),y=.5*(Mn(x,x)-c*v),R=.5*(d.reduce((F,C,D)=>F+C*C*a[D]*a[D],0)-c*a.reduce((F,C)=>F+C,0)),T=a.reduce((F,C)=>F+C*C,0),S=.5*T,k=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(Fv(f))),L=-.5*(i.reduce((F,C)=>F+Math.log(C),0)+k-s*Math.log(c)+w),_=u.map(F=>F.map(C=>C*c)),I=u.map((F,C)=>Math.sqrt(Math.max(0,F[C]))*Math.sqrt(c));return{beta:h,cov:_,se:I,fitted:p,residuals:d,qe:w,qeScaled:g,weights:a,minVariance:c,P:N,traceP:E,tracePNorm:v,traceP2:M,traceP2Norm:b,score:y,mlScore:R,mlInfo:S,logLik:L,sumWeightsNorm:l,sumWeightSquaredNorm:T}}function Fv(e){let t=e.length,n=e.map(o=>o.slice()),r=1;for(let o=0;o<t;o++){let s=o;for(let c=o+1;c<t;c++)Math.abs(n[c][o])>Math.abs(n[s][o])&&(s=c);if(n[s][o]===0)return 0;s!==o&&([n[s],n[o]]=[n[o],n[s]],r=-r);let i=n[o][o];r*=i;for(let c=o+1;c<t;c++){let a=n[c][o]/i;for(let l=o+1;l<t;l++)n[c][l]-=a*n[o][l]}}return r}function Ec(e,t){return e.reduce((n,r,o)=>n+r.reduce((s,i,c)=>s+i*t[c]*e[c][o]*t[o],0),0)}function Dv(e,t,n,r,o,s,i){let c=e.length,a=c-n[0].length,l=t.map(m=>m+o);if(r==="EE"||r==="FE")return NaN;if(r==="REML")return s.traceP2Norm>0?s.minVariance*Math.sqrt(2/s.traceP2Norm):NaN;if(r==="ML")return s.sumWeightSquaredNorm>0?s.minVariance*Math.sqrt(2/s.sumWeightSquaredNorm):NaN;if(r==="DL"||r==="HS"||r==="HSk"){let m=Math.sqrt(2*Ec(i.P,l)),u=r==="DL"?i.tracePNorm:r==="HS"?i.sumWeightsNorm:i.sumWeightsNorm*(c-1)/c;return u>0?m/u:NaN}if(r==="HE"){let m=Gt(e,Array(c).fill(1),n,0);return a>0?Math.sqrt(2*Ec(m.P,l))/a:NaN}if(r==="SJ"){let m=Gt(e,Array(c).fill(1),n,0),u=m.residuals.reduce((p,d)=>p+d*d,0)/c;if(!(u>0)||a<=0)return NaN;let h=Gt(e,t,n,u);return u/a*Math.sqrt(2*Ec(h.P,l))/h.minVariance}let f=s.residuals.reduce((m,u,h)=>m+s.weights[h]*s.weights[h]*u*u,0);return f>0&&a>0?Math.sqrt(2*a)*s.minVariance*s.minVariance/f:NaN}function mh(e,t,n,r,o){let s=e.length,i=n[0].length,c=s-i,a=Gt(e,t,n,0);if(r==="EE"||r==="FE")return 0;if(r==="DL")return Math.max(0,(a.qeScaled-c*a.minVariance)/a.tracePNorm);if(r==="HS")return Math.max(0,(a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);if(r==="HSk")return Math.max(0,(s/(s-1)*a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);let l=Gt(e,Array(s).fill(1),n,0),f=l.residuals.reduce((w,N)=>w+N*N,0),m=Math.max(0,(f-t.reduce((w,N,v)=>w+l.P[v][v]*N,0))/Math.max(1,c));if(r==="HE")return m;if(r==="SJ"){let w=f/s,N=Gt(e,t,n,w),v=t.map(E=>w/(E+w));return Math.max(0,v.reduce((E,b,M)=>E+b*N.residuals[M]*N.residuals[M],0)/Math.max(1,c))}let u=w=>Gt(e,t,n,w).qe-c;if(r==="EB"){let w=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,ho(u,0,w,1e-14))}if(r==="PM"){let w=o.tau2Max??Math.max(100,10*f/s);for(;u(w)>0&&w<Number.MAX_VALUE/4;)w*=2;let N=o.tol??1e-14;return Math.max(0,ho(u,0,w,N))}let h=Math.max(0,o.tau2Init??m),p=o.maxiter??100,d=o.threshold??1e-5,g=o.stepadj??1;if(p<=1)return h;for(let w=0;w<p;w++){let N=Gt(e,t,n,h),v=r==="REML"?N.score:N.mlScore,E=r==="REML"?.5*N.traceP2Norm:N.mlInfo;if(!(E>0)||!Number.isFinite(E))return h;let b=Math.max(0,h+g*v/E);if(!Number.isFinite(b))return h;if(Math.abs(b-h)<d)return b;h=b}return h}function uh(e){if(e.length===0||e[0].length===0)return[];let t=[],n=[];for(let r=0;r<e[0].length;r++){let o=e.map(a=>a[r]),s=Math.sqrt(Mn(o,o)),i=o.slice();for(let a of n){let l=Mn(i,a);i=i.map((f,m)=>f-l*a[m])}let c=Math.sqrt(Mn(i,i));s>0&&c>1e-10*s&&(t.push(r),n.push(i.map(a=>a/c)))}return t}function Pv(e,t,n,r){let o,s=[],i=!1,c=0;if(typeof t=="string"){if(r===void 0)throw new TypeError("data is required when mods is a formula");let f=t.includes("~")?t:`~ ${t}`,m=gr(Xn(f),r,{intercept:n});if(o=m.x,s=m.colNames.map(h=>h==="(Intercept)"?"intrcpt":h),m.frame.rows.length!==e)throw new RangeError("variable lengths differ between yi and mods");if(o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let u=uh(o);return{x:o.map(h=>u.map(p=>h[p])),names:u.map(h=>s[h]??""),warnings:u.length<o[0].length?["Redundant predictors dropped from the model."]:[]}}else if(t===void 0)o=Array.from({length:e},()=>[]);else if(Array.isArray(t))if(t.length===0)o=Array.from({length:e},()=>[]);else if(typeof t[0]=="number"){let f=Array.from(t,Number);if(f.length!==e)throw new RangeError("variable lengths differ (found for 'mods')");o=f.map(m=>[m]),c=1}else{let f=t;if(f.length===e)o=Array.from(f,m=>Array.from(m,Number)),c=o[0]?.length??0;else if(f.every(m=>m.length===e))c=f.length,o=Array.from({length:e},(m,u)=>f.map(h=>Number(h[u])));else throw new RangeError("variable lengths differ (found for 'mods')")}else{let f=t;if(f.rows!==void 0){let m=f.rows[0];if(m!=null&&typeof m=="object"&&!Array.isArray(m)){let u=typeof f.colNames=="string"?[f.colNames]:f.colNames===void 0||f.colNames===null?Object.keys(m):Array.from(f.colNames);o=f.rows.map(h=>u.map(p=>Number(h[p]))),s=u.map(h=>h==="(Intercept)"?"intrcpt":h),i=!0,c=u.length}else o=f.rows.map(u=>Array.from(u,Number)),c=o[0]?.length??0}else if(f.columns!==void 0)c=f.columns.length,o=Array.from({length:e},(m,u)=>f.columns.map(h=>Number(h[u])));else throw new TypeError("mods matrix must have rows or columns");if(o.length!==e)throw new RangeError("variable lengths differ (found for 'mods')");f.colNames!==void 0&&f.colNames!==null&&(s=(typeof f.colNames=="string"?[f.colNames]:Array.from(f.colNames)).map(m=>m==="(Intercept)"?"intrcpt":m),i=!0)}if(o.some(f=>f.length!==o[0].length))throw new TypeError("all rows of the moderator matrix must have the same length");if(i||(s=Array.from({length:c},(f,m)=>c>1?`.${m+1}`:"")),n&&(o=o.map(f=>[1,...f]),s=["intrcpt",...s]),o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let a=uh(o);if(a.length<o[0].length&&!i&&c>0){for(let f=0;f<a.length;f++)if(a[f]!==0||!n){s[a[f]]="";break}}return{x:o.map(f=>a.map(m=>f[m])),names:a.map(f=>s[f]??""),warnings:a.length<o[0].length?["Redundant predictors dropped from the model."]:[]}}function Rc(e){let t=[676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,15056327351493116e-23];if(e<.5)return Math.log(Math.PI)-Math.log(Math.sin(Math.PI*e))-Rc(1-e);let n=e-1,r=.9999999999998099;for(let s=0;s<t.length;s++)r+=t[s]/(n+s+1);let o=n+7.5;return .9189385332046727+(n+.5)*Math.log(o)-o+Math.log(r)}function ph(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function xc(e,t,n,r,o){let s=ph(e,t,n,r);if(s===0)return NaN;let i=e/s,c=t/s,a=n/s,l=r/s,f=Ce(Ht(i,l),Ht(c,a)),m=$t(i,c),u=$t(a,l),h=o==="RR"?_e([a,0],m):o==="OR"?Ht(c,a):_e(m,u),p=lt(f,h);return p[0]+p[1]}function Vv(e){let t=e/2;if(t>1e3){let n=1/t,r=n*n,o=r*n,s=r*r,i=s*n,c=o*o,a=c*n,l=s*s,f=-(3/8)*n-1/8*r-3/64*o-1/64*s-3/640*i-1/384*c-33/14336*a-1/2048*l;return Math.exp(f)}return Math.exp(Rc(t)-.5*Math.log(t)-Rc(t-.5))}function $v(e,t){V(t,Lv,"escalc");let n=[],r=f=>t[f]===void 0?void 0:Array.from(t[f]),s={RR:["ai","bi","ci","di"],OR:["ai","bi","ci","di"],RD:["ai","bi","ci","di"],MD:["m1i","sd1i","n1i","m2i","sd2i","n2i"],SMD:["m1i","sd1i","n1i","m2i","sd2i","n2i"],ZCOR:["ri","ni"]}[e].map(f=>r(f)),i=s.slice(0,4).some(f=>f.some(m=>m===0));if(s.some(f=>f===void 0))throw new TypeError(`missing required arguments for measure ${e}`);let c=s[0].length;if(s.some(f=>f.length!==c))throw new RangeError("variable lengths differ");let a=[],l=[];for(let f=0;f<c;f++){let m=NaN,u=NaN;if(e==="ZCOR"){let h=s[0][f],p=s[1][f];if(Math.abs(h)>1)throw new RangeError("One or more correlations are > 1 or < -1.");p>3?(m=Math.atanh(h),u=1/(p-3)):(n.push("Cannot estimate the sampling variance when ni <= 3."),m=Math.atanh(h),u=NaN)}else if(e==="MD"||e==="SMD"){let h=s[0][f],p=s[1][f],d=s[2][f],g=s[3][f],w=s[4][f],N=s[5][f];if(!(d>1&&N>1&&p>=0&&w>=0))throw new RangeError("invalid means, standard deviations, or sample sizes");if(e==="MD")m=h-g,u=p*p/d+w*w/N;else{let v=d+N-2,E=((d-1)*p*p+(N-1)*w*w)/v,b=(h-g)/Math.sqrt(E);m=Vv(v)*b,u=(d+N)/(d*N)+m*m/(2*(d+N))}}else{let h=s[0][f],p=s[1][f],d=s[2][f],g=s[3][f],w=h,N=p,v=d,E=g,b=t.add??.5,M=t.to??"only0",x=[w,N,v,E].some(C=>C===0),y=M==="all"||M==="only0"&&x||M==="if0all"&&i;if(e!=="RD"&&y&&(w+=b,N+=b,v+=b,E+=b),e==="RD"&&y&&(w+=b,N+=b,v+=b,E+=b),t.drop00&&(h===0&&d===0||p===0&&g===0)){a.push(NaN),l.push(NaN);continue}let R=ph(w,N,v,E),T=w/R,S=N/R,k=v/R,L=E/R,_=T+S,I=k+L,F=1/R;e==="RR"?(m=Math.log1p(xc(w,N,v,E,"RR")),u=F*(S/(T*_)+L/(k*I))):e==="OR"?(m=Math.log1p(xc(w,N,v,E,"OR")),u=F*(1/T+1/S+1/k+1/L)):(m=xc(w,N,v,E,"RD"),u=F*(T*S/(_*_*_)+k*L/(I*I*I)))}a.push(m),l.push(u)}return H("escalc",{yi:a,vi:l,measure:e,warnings:n})}function dh(e,t,n={}){at(t)&&V(t,ih,"rma"),at(t)&&V(Ft(t,"control"),ch,"rma","control"),V(n,ih,"rma"),V(Ft(n,"control"),ch,"rma","control");let r=typeof e=="object"&&"yi"in e,o=Array.from(r?e.yi:e),s=(r?t:n)??{},i=Array.from(r?e.vi:t??[]);if(o.length===0||i.length!==o.length)throw new RangeError("yi and vi must have the same positive length");if(o.some(B=>!Number.isFinite(B)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(i.some(B=>!Number.isFinite(B)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(s.method==="DL"&&i.some(B=>B<=0))throw new Error("DL estimator cannot be used when there are non-positive sampling variances in the data.");if(i.some(B=>B<0))throw new RangeError("sampling variances must be non-negative");if(i.some(B=>B===0)&&!["EE","FE"].includes(s.method??"REML"))throw new RangeError("random-effects estimators require positive sampling variances");let c=s.method??"REML",a=s.test??"z",l=s.level??95,f=s.intercept??!0,m=Pv(o.length,s.mods,f,s.data),u=m.x,h=u[0].length,p=o.length,d=p-h,g=m.names[0]==="intrcpt"||!f&&u.every(B=>B[0]===1);if(h===0)throw new RangeError("the model must contain at least one coefficient");if(p<h)throw new Error("Number of parameters to be estimated is larger than the number of observations.");if(!(l>0&&l<100))throw new RangeError("level must be between 0 and 100");if(!["EE","FE","DL","HE","HS","HSk","SJ","ML","REML","EB","PM"].includes(c))throw new RangeError(`unknown method '${c}'`);if(!["z","t","knha"].includes(a))throw new RangeError(`unknown test '${a}'`);if(c==="DL"&&!Number.isFinite(Gt(o,i,u,0).qe))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");let w=Gt(o,i,u,0),N=mh(o,i,u,c,s.control??{}),v=Gt(o,i,u,N),E=s.mods===void 0?N:mh(o,i,Array.from({length:p},()=>[1]),c,s.control??{}),b=a==="knha"&&d>0?Math.max(1,v.qe/d):1,M=v.cov.map(B=>B.map(J=>J*b)),x=v.se.map(B=>B*Math.sqrt(b)),y=v.beta.map((B,J)=>B/x[J]),R=a==="z"?oe(1-(1-l/100)/2):rn(1-(1-l/100)/2,d),T=y.map(B=>2*(a==="z"?ie(-Math.abs(B)):dn(-Math.abs(B),d))),S=v.beta.map((B,J)=>B-R*x[J]),k=v.beta.map((B,J)=>B+R*x[J]),L=g?1:0,_=h===1?1:Math.max(1,h-L),I=0,F=1;if(h===1)I=y[0]*y[0],F=a==="z"?Mt(I,1,{lowerTail:!1}):xn(I,1,d,{lowerTail:!1});else{let B=v.beta.slice(L),J=M.slice(L).map(ce=>ce.slice(L));try{I=Mn(B,rt(J,B))}catch{I=NaN}F=a==="z"?Mt(I,_,{lowerTail:!1}):xn(I/_,_,d,{lowerTail:!1})}let C=w.tracePNorm,D=d>0?d*w.minVariance/C:0,A=c==="EE"||c==="FE"?w.qe>0?100*Math.max(0,(w.qe-d)/w.qe):0:N===0?0:100*N/(N+D),O=c==="EE"||c==="FE"?d>0?Math.max(1,w.qe/d):1:N===0?1:(N+D)/D,q=s.mods===void 0||h===1?null:E>0?100*Math.max(0,(E-N)/E):0,P=Dv(o,i,u,c,N,v,w);return H("rma",{beta:{dim:[h,1],rowNames:h===1?m.names[0]??"intrcpt":m.names,colNames:null,rows:v.beta.map(B=>[B])},se:yn(x),zval:yn(y),pval:yn(T),"ci.lb":yn(S),"ci.ub":yn(k),tau2:N,"se.tau2":P,QE:w.qe,QEp:d===0?1:Mt(w.qe,d,{lowerTail:!1}),QM:I,QMp:F,R2:q,p:h,m:_,I2:A,H2:O,k:p,yi:o,vi:i,mods:u,intercept:g,method:c,test:a,level:l,warnings:m.warnings,covariance:M,tau2Null:E})}function zv(e,t={}){V(t,Cv,"confintRma");let n=t.type??"QP",r=t.level??e.level,o=1-r/100;if(n!=="QP"&&n!=="PL")throw new RangeError("type must be 'QP' or 'PL'");let s=Gt(e.yi,e.vi,e.mods,e.tau2),i=e.k-e.mods[0].length,c=0,a=NaN;if(n==="QP"){let g=Gt(e.yi,e.vi,e.mods,0),w=Cn(1-o/2,i),N=Cn(o/2,i),v=g.qe,E=b=>Gt(e.yi,e.vi,e.mods,b).qe;c=v<=w?0:fh(b=>E(b)-w,Math.max(e.tau2,1e-8)),a=v<=N?NaN:fh(b=>E(b)-N,Math.max(e.tau2,1e-8))}else{let g=Cn(r/100,1),w=v=>Gt(e.yi,e.vi,e.mods,v).logLik,N=v=>2*(s.logLik-w(v))-g;if(e.tau2>0){c=N(0)<=0?0:ho(N,0,e.tau2);let v=Math.max(e.tau2*2,1e-8),E=N(v);for(let b=0;b<80&&E<0;b++)v*=2,E=N(v);a=E>=0?ho(N,e.tau2,v):NaN}else{let v=1e-8,E=N(v);for(let b=0;b<80&&E<0;b++)v*=2,E=N(v);a=E>=0?ho(N,0,v):NaN}}let l=Gt(e.yi,e.vi,e.mods,0),f=i>0?i*l.minVariance/l.tracePNorm:0,m=g=>[g,Math.sqrt(g),100*g/(g+f),(g+f)/f],u=m(e.tau2),h=m(c),p=m(a),d=["tau^2","tau","I^2(%)","H^2"];return{random:{dim:[4,3],rowNames:d,colNames:["estimate","ci.lb","ci.ub"],rows:d.map((g,w)=>({estimate:u[w],"ci.lb":h[w],"ci.ub":p[w]}))},digits:{est:"4",se:"4",test:"4",pval:"4",ci:"4",var:"4",sevar:"4",fit:"4",het:"4"},"ci.null":!1,"lb.sign":"","ub.sign":"","tau2.min":"0"}}function jv(e,t={}){V(t,Ov,"predictRma");let n=t.level??e.level,r=1-n/100,o=e.mods[0].length,s;if(t.newmods===void 0)s=o-(e.intercept?1:0)===0?[e.intercept?[1]:[]]:e.mods.map(h=>h.slice());else{let u=t.newmods;if(u.length>0&&typeof u[0]=="number"?s=Array.from(u,h=>e.intercept?[1,h]:[h]):s=Array.from(u,h=>Array.from(h)),s.some(h=>h.length===o-(e.intercept?1:0))&&(s=s.map(h=>e.intercept?[1,...h]:h)),s.some(h=>h.length!==o))throw new RangeError("newmods has the wrong number of columns")}let i=e.beta.rows.map(u=>u[0]),c=s.map(u=>Mn(u,i)),a=s.map(u=>Math.sqrt(Math.max(0,Mn(u,Ho(e.covariance,u))))),l=e.test==="z"?oe(1-r/2):rn(1-r/2,e.k-o),f=t.predtype==="riley"?rn(1-r/2,e.k-o-1):l,m=s.map((u,h)=>Math.sqrt(a[h]**2+e.tau2));return{pred:yn(c),se:yn(a),"ci.lb":yn(c.map((u,h)=>u-l*a[h])),"ci.ub":yn(c.map((u,h)=>u+l*a[h])),"pi.lb":yn(c.map((u,h)=>u-f*m[h])),"pi.ub":yn(c.map((u,h)=>u+f*m[h]))}}function Kv(e,t,n={}){at(t)&&V(t,lh,"regtest"),V(n,lh,"regtest");let r=typeof e=="object"&&"beta"in e,o=(r?t:n)??{},s=o.model??"rma",i=o.predictor??"sei",c=o.level??95,a=Array.from(r?e.yi:e),l=Array.from(r?e.vi:t??[]);if(a.length!==l.length||a.length<2)throw new RangeError("yi and vi must have equal lengths and at least two studies");let f=l.map(b=>Math.sqrt(b)),m=i==="sei"?f:i==="vi"?l:l.map((b,M)=>1/(M+1)),u,h,p,d,g,w;if(s==="lm"){let b=m.map((T,S)=>[1,1/T]),M=a.map((T,S)=>T/f[S]),x=Wv(b,M);u=x.beta[1];let y=Math.sqrt(x.cov[0][0]*x.sigma2),R=Math.sqrt(x.cov[1][1]*x.sigma2);p=x.beta[0]/y,h=R,d=a.length-2,g=2*dn(-Math.abs(p),d)}else{w=dh(a,l,{method:"REML",mods:m,intercept:!0,test:"z"}),u=w.beta.rows[0][0],h=Array.isArray(w.se)?w.se[0]:w.se;let b=Array.isArray(w.zval)?w.zval[1]:NaN,M=Array.isArray(w.pval)?w.pval[1]:NaN;p=b,d=NaN,g=M}let N=s==="lm"?rn(1-(1-c/100)/2,d):oe(1-(1-c/100)/2),v=[];s==="lm"&&(!Number.isFinite(p)||!Number.isFinite(g)||!Number.isFinite(h))&&v.push("NaNs produced");let E={model:s,predictor:i,zval:p,pval:g,dfs:d,est:u,"ci.lb":u-N*h,"ci.ub":u+N*h,warnings:v};return H("regtest",o.retFit&&w!==void 0?{...E,fit:w}:E)}function Wv(e,t){let n=Array.from({length:2},(c,a)=>Array.from({length:2},(l,f)=>e.reduce((m,u,h)=>m+u[a]*u[f],0))),r=rt(n,hh(2)),o=Ho(r,Array.from({length:2},(c,a)=>e.reduce((l,f,m)=>l+f[a]*t[m],0))),s=t.map((c,a)=>c-Mn(e[a],o)),i=t.length>2?s.reduce((c,a)=>c+a*a,0)/(t.length-2):NaN;return{beta:o,cov:r,sigma2:i}}var Yv=["type","opposite","twoSided"];function Tc(e,t){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(!Number.isSafeInteger(t)||t<3)return NaN;if(e<=0)return 1;if(e>=(t-1)/Math.sqrt(t))return 0;let n=t-1,r=Ht(n,n),o=zt(Ht(e,e),t),s=Ce(r,o);if(s[0]+s[1]<=0)return 0;let i=lt(s,r),c=lt(o,r),a=Pc((t-2)/2,.5,i,c,!0,!1);return Math.min(1,t*a/2)}function gh(e,t){let n=Tc(e,t);return Number.isNaN(n)?NaN:1-n}function Bv(e,t,n=10){if(n!==10)throw new RangeError(`Grubbs type ${n} is not supported in rStats.js 1.9.0 (type 10 only)`);return ha(e,r=>gh(r,t))}function Uv(e){let t=0,n=(e-1)/Math.sqrt(e);for(let r=0;r<80;r++){let o=t+(n-t)/2;if(o===t||o===n)break;Tc(o,e)>=1?t=o:n=o}return n}function Gv(e,t){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(!Number.isSafeInteger(t)||t<3||e<0||e>1)return NaN;if(e===0)return Uv(t);if(e===1)return(t-1)/Math.sqrt(t);let n=0,r=(t-1)/Math.sqrt(t);for(let o=0;o<80;o++){let s=n+(r-n)/2;gh(s,t)<e?n=s:r=s}return n+(r-n)/2}function Hv(e,t,n=10){if(n!==10)throw new RangeError(`Grubbs type ${n} is not supported in rStats.js 1.9.0 (type 10 only)`);return ha(e,r=>Gv(r,t))}function Xv(e,t={}){V(t,Yv,"grubbsTest");let n=t.type??10;if(n!==10)throw new RangeError(`Grubbs type ${n} is not supported in rStats.js 1.9.0 (type 10 only)`);let r=Array.from(e).filter(N=>!Number.isNaN(N));if(r.some(N=>!Number.isFinite(N)))throw new RangeError("x must contain finite values");let o=r.length;if(o<2)throw new RangeError("sample size must be at least 2");let s=t.opposite??!1,i=t.twoSided??!1,c=r[0];for(let N=1;N<r.length;N++)(s?r[N]<c:r[N]>c)&&(c=r[N]);let a=ts(r),l=r.indexOf(c),f=Math.abs(a[l]),m=r.slice();m.splice(l,1);let u=Et([r,m]),h=(o-2)*u.variance[1]/((o-1)*u.variance[0]),p=Tc(f,o),d=1-p,g=Number.isNaN(f)?NaN:Math.min(1,i?2*Math.min(d,p):p);return{method:"Grubbs test for one outlier",statistic:[f,h],statisticName:["G","U"],pValue:g,alternative:`${s?"lowest":"highest"} value ${String(c)} is an outlier`,warnings:o===2?["NaNs produced"]:[]}}export{Ki as Anova,qb as ICC,Mg as TukeyHSD,S1 as adTest,Fg as add1,ii as anova,im as ansariTest,Ng as aov,t1 as aovEz,ci as aovSummary,eb as auc,Lf as bartlettTest,Xa as binomCI,nd as binomDiffCI,pd as binomDiffEquivalenceTest,od as binomRatioCI,qp as binomTest,ub as blandAltman,Qd as boschlooTest,ag as brunnerMunzelTest,Yh as chisqTest,sb as ciAuc,c1 as clogit,s0 as cochranQTest,uw as coefci,fw as coeftest,nu as cohenES,Rd as cohensD,Mh as confint,zv as confintRma,nb as coords,yh as corTest,u1 as coxZph,Hr as coxph,P1 as cronbachAlpha,rv as crr,Zb as cuminc,A1 as cvmTest,lb as diagnosticTests,Og as drop1,q0 as dscfAllPairsTest,C0 as dunnTest,A0 as dunnettTest,vv as epiSsequb,Nv as epiSsequc,yv as epiSsninfb,Mv as epiSsninfc,Ev as epiSssupb,xv as epiSssupc,Cd as epsilonSquared,Ow as esH,Fw as esW1,Dw as esW2,$v as escalc,_d as etaSquared,Cg as extractAIC,Cb as factanal,So as fisherTest,Ff as flignerTest,gi as friedmanTest,Ph as gTest,_0 as gamesHowellTest,F0 as glhtDunnett,er as glm,gw as glmAnova,Rg as glmConfint,Sh as glmCooksDistance,Rh as glmHatvalues,Ah as glmPredict,xh as glmResiduals,Th as glmRstandard,ya as glmSummary,jh as goodmanKruskalGamma,Xv as grubbsTest,Td as hedgesG,D1 as icc,R1 as jonckheereTest,O1 as kappa2,F1 as kappamFleiss,Vs as kruskalTest,gm as ksTest,T0 as kwAllPairsDunnTest,S0 as kwManyOneDunnTest,Wh as lblTest,d1 as leveneTest,k1 as lillieTest,wr as lm,K0 as mantelhaenTest,k0 as manyOneUTest,Mw as mauchlyTest,e0 as mcnemarTest,xb as mcreg,Kr as modelMatrix,Df as moodTest,qv as numDEpi,ld as oddsRatio,zb as omega,Ld as omegaSquared,Hf as onewayTest,Yr as pAdjust,o0 as pageTest,gg as pairwisePropTest,pg as pairwiseTTest,dg as pairwiseWilcoxTest,$1 as partialR,I1 as pearsonTest,Bv as pgrubbs,_v as ph2simon,Fp as poissonTest,Bb as polychor,Gb as polyserial,Nh as powerAnovaTest,Tv as powerCT,Av as powerEpi,Rv as powerPaired2x2,vh as powerPropTest,bh as powerTTest,Eh as predict,ov as predictCrr,jv as predictRma,Ga as propTest,b1 as propTrendTest,Uw as pwr2p2nTest,Ww as pwr2pTest,Xw as pwrAnovaTest,Hw as pwrChisqTest,Qw as pwrF2Test,Bw as pwrNormTest,Yw as pwrPTest,Gw as pwrRTest,Kw as pwrT2nTest,jw as pwrTTest,Hv as qgrubbs,Of as quadeTest,Kv as regtest,dh as rma,s1 as rmst2,Z1 as roc,ib as rocTest,wh as setWarningHandler,q1 as sfTest,P0 as shapiroTest,Sv as ssizeCT,kv as ssizeEpi,Pg as step,Vg as stepAIC,zh as stuartMaxwellTest,Jg as survdiff,ar as survfit,rw as survfitCoxph,qh as survreg,Ih as survregPredict,_h as survregResiduals,jg as svyglm,jd as tTOST,vn as tTest,I0 as tamhaneT2Test,ev as timepoints,ud as tost,Kd as tsumTOST,L0 as tukeyTest,_f as varTest,kh as vcovCL,xa as vcovHC,Ao as wilcoxTest,Eg as zTest};
|
|
22
|
+
`),J("ph2simon",{out:m,minimax:g,optimal:w,admissible:N,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:b})}var hM=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),ap=["method","mods","data","intercept","test","level","control"],ip=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],pM=["type","level"],dM=["newmods","predtype","level"],cp=["model","predictor","retFit","level"],kn=e=>e.length===1?e[0]:e,qn=(e,t)=>e.reduce((n,r,o)=>n+r*t[o],0),Oo=(e,t)=>e.map(n=>qn(n,t)),hp=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),Fo=(e,t,n,r=1e-14)=>{let o=e(t),s=e(n);if(o===0)return t;if(s===0)return n;if(!(o*s<0))return NaN;for(let i=0;i<2200;i++){let a=t+(n-t)/2,c=e(a);if(!Number.isFinite(c)){n=a;continue}if(Math.abs(n-t)<=r*Math.abs(a)||c===0||a===t||a===n)return a;o*c<=0?(n=a,s=c):(t=a,o=c)}return t+(n-t)/2},lp=(e,t)=>{let n=t,r=e(n);for(let o=0;o<2100&&r>0&&Number.isFinite(n);o++)n*=2,r=e(n);return r<=0?Fo(e,0,n):NaN};function Ut(e,t,n,r){let o=n[0].length,s=Array.from({length:o},(v,y)=>y).find(v=>n.every(y=>y[v]===1))??-1,i=Array.from({length:o},(v,y)=>y===s||s<0?0:ri(n.map(T=>T[y]))),a=Array.from({length:o},(v,y)=>{if(y===s)return 1;let T=Xo(n.map(M=>M[y]-i[y]));return Math.abs(Math.log2(T))>200?T:1});if(i.every(v=>v===0)&&a.every(v=>v===1))return fp(e,t,n,r);let c=n.map(v=>v.map((y,T)=>(y-i[T])/a[T])),l=fp(e,t,c,r),f=Array.from({length:o},(v,y)=>Array.from({length:o},(T,M)=>y===M?y===s?1:1/a[M]:y===s?-i[M]/a[M]:0)),m=v=>f.map(y=>Array.from({length:o},(T,M)=>y.reduce((R,x,A)=>R+x*Array.from({length:o},(S,I)=>v[A][I]*f[M][I]).reduce((S,I)=>S+I,0),0))),u=f.map(v=>v.reduce((y,T,M)=>y+T*l.beta[M],0)),h=(v,y)=>v.reduce((T,M,R)=>T+M*y.reduce((x,A,S)=>x+l.cov[R][S]*A,0),0),p=f.map(v=>v.reduce((y,T)=>Math.max(y,Math.abs(T)),0)),d=f.map((v,y)=>v.map(T=>T/p[y])),g=d.map(v=>Math.sqrt(Math.max(0,h(v,v)))),w=g.map((v,y)=>v*p[y]),N=d.map((v,y)=>d.map((T,M)=>h(v.map(R=>R/g[y]),T.map(R=>R/g[M])))),b=N.map((v,y)=>v.map((T,M)=>T*w[y]*w[M])),E=l.logLik-a.reduce((v,y)=>v+Math.log(y),0);return{...l,beta:u,cov:b,se:w,corr:N,logLik:E}}function fp(e,t,n,r){let o=e.length,s=n[0].length,i=t.map(V=>V+r),a=i.reduce((V,P)=>Math.min(V,P),1/0),c=i.map(V=>a/V),l=c.reduce((V,P)=>V+P,0),f=Array.from({length:s},(V,P)=>Array.from({length:s},(Q,K)=>n.reduce((X,Y,se)=>X+c[se]*Y[P]*Y[K],0))),m=Array.from({length:s},(V,P)=>n.reduce((Q,K,X)=>Q+c[X]*K[P]*e[X],0)),u;try{u=ot(f,hp(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=Oo(u,m),p=Oo(n,h),d=e.map((V,P)=>V-p[P]),g=e.every(V=>V===e[0])?0:d.reduce((V,P,Q)=>V+c[Q]*P*P,0),w=g/a,N=n.map(V=>qn(V,Oo(u,V))),b=Array.from({length:s},(V,P)=>Array.from({length:s},(Q,K)=>n.reduce((X,Y,se)=>X+c[se]*c[se]*Y[P]*Y[K],0))),E=u.map(V=>Array.from({length:s},(P,Q)=>V.reduce((K,X,Y)=>K+X*b[Y][Q],0))),v=0;for(let V=0;V<s;V++)for(let P=0;P<s;P++)v+=E[V][P]*E[P][V];let y=c.reduce((V,P,Q)=>V+P-P*P*N[Q],0),T=y/a,M=c.reduce((V,P,Q)=>V+P*P-2*P*P*P*N[Q],0)+v,R=M/a/a,x=d.map((V,P)=>c[P]*V),A=null,S=()=>A??=Array.from({length:o},(V,P)=>{let Q=Oo(u,n[P]);return Array.from({length:o},(K,X)=>(P===X?c[P]:0)-c[P]*c[X]*qn(n[X],Q))}),I=.5*(qn(x,x)-a*y),C=.5*(d.reduce((V,P,Q)=>V+P*P*c[Q]*c[Q],0)-a*c.reduce((V,P)=>V+P,0)),_=c.reduce((V,P)=>V+P*P,0),F=.5*_,O=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(gM(f))),D=-.5*(i.reduce((V,P)=>V+Math.log(P),0)+O-s*Math.log(a)+w),k=u.map(V=>V.map(P=>P*a)),L=u.map((V,P)=>Math.sqrt(Math.max(0,V[P]))*Math.sqrt(a)),q=u.map((V,P)=>Math.sqrt(Math.max(0,V[P]))),$=u.map((V,P)=>V.map((Q,K)=>Q/(q[P]*q[K])));return{beta:h,cov:k,se:L,corr:$,fitted:p,residuals:d,qe:w,qeScaled:g,weights:c,minVariance:a,get P(){return S()},traceP:T,tracePNorm:y,traceP2:R,traceP2Norm:M,score:I,mlScore:C,mlInfo:F,logLik:D,sumWeightsNorm:l,sumWeightSquaredNorm:_}}function gM(e){let t=e.length,n=e.map(o=>o.slice()),r=1;for(let o=0;o<t;o++){let s=o;for(let a=o+1;a<t;a++)Math.abs(n[a][o])>Math.abs(n[s][o])&&(s=a);if(n[s][o]===0)return 0;s!==o&&([n[s],n[o]]=[n[o],n[s]],r=-r);let i=n[o][o];r*=i;for(let a=o+1;a<t;a++){let c=n[a][o]/i;for(let l=o+1;l<t;l++)n[a][l]-=c*n[o][l]}}return r}function al(e,t){return e.reduce((n,r,o)=>n+r.reduce((s,i,a)=>s+i*t[a]*e[a][o]*t[o],0),0)}function wM(e,t,n,r,o,s,i){let a=e.length,c=a-n[0].length,l=t.map(m=>m+o);if(r==="EE"||r==="FE")return NaN;if(r==="REML")return s.traceP2Norm>0?s.minVariance*Math.sqrt(2/s.traceP2Norm):NaN;if(r==="ML")return s.sumWeightSquaredNorm>0?s.minVariance*Math.sqrt(2/s.sumWeightSquaredNorm):NaN;if(r==="DL"||r==="HS"||r==="HSk"){let m=Math.sqrt(2*al(i.P,l)),u=r==="DL"?i.tracePNorm:r==="HS"?i.sumWeightsNorm:i.sumWeightsNorm*(a-1)/a;return u>0?m/u:NaN}if(r==="HE"){let m=Ut(e,Array(a).fill(1),n,0);return c>0?Math.sqrt(2*al(m.P,l))/c:NaN}if(r==="SJ"){let m=Ut(e,Array(a).fill(1),n,0),u=m.residuals.reduce((p,d)=>p+d*d,0)/a;if(!(u>0)||c<=0)return NaN;let h=Ut(e,t,n,u);return u/c*Math.sqrt(2*al(h.P,l))/h.minVariance}let f=s.residuals.reduce((m,u,h)=>m+s.weights[h]*s.weights[h]*u*u,0);return f>0&&c>0?Math.sqrt(2*c)*s.minVariance*s.minVariance/f:NaN}function mp(e,t,n,r,o){let s=e.length,i=n[0].length,a=s-i,c=Ut(e,t,n,0);if(r==="EE"||r==="FE")return 0;if(r==="DL")return Math.max(0,(c.qeScaled-a*c.minVariance)/c.tracePNorm);if(r==="HS")return Math.max(0,(c.qeScaled-s*c.minVariance)/c.sumWeightsNorm);if(r==="HSk")return Math.max(0,(s/(s-1)*c.qeScaled-s*c.minVariance)/c.sumWeightsNorm);let l=Ut(e,Array(s).fill(1),n,0),f=l.residuals.reduce((w,N)=>w+N*N,0),m=Math.max(0,(f-t.reduce((w,N,b)=>w+l.P[b][b]*N,0))/Math.max(1,a));if(r==="HE")return m;if(r==="SJ"){let w=f/s,N=Ut(e,t,n,w),b=t.map(E=>w/(E+w));return Math.max(0,b.reduce((E,v,y)=>E+v*N.residuals[y]*N.residuals[y],0)/Math.max(1,a))}let u=w=>Ut(e,t,n,w).qe-a;if((r==="EB"||r==="PM")&&u(0)<=0)return 0;if(r==="EB"){let w=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,Fo(u,0,w,1e-14))}if(r==="PM"){let w=o.tau2Max??Math.max(100,10*f/s);for(;u(w)>0&&w<Number.MAX_VALUE/4;)w*=2;let N=o.tol??1e-14;return Math.max(0,Fo(u,0,w,N))}let h=Math.max(0,o.tau2Init??m),p=o.maxiter??100,d=o.threshold??1e-5,g=o.stepadj??1;if(p<=1)return h;for(let w=0;w<p;w++){let N=Ut(e,t,n,h),b=r==="REML"?N.score:N.mlScore,E=r==="REML"?.5*N.traceP2Norm:N.mlInfo;if(!(E>0)||!Number.isFinite(E))return h;let v=Math.max(0,h+g*b/E);if(!Number.isFinite(v))return h;if(Math.abs(v-h)<d)return v;h=v}return h}function up(e){if(e.length===0||e[0].length===0)return[];let t=[],n=[],r=e[0].some((o,s)=>e.every(i=>i[s]===1));for(let o=0;o<e[0].length;o++){let s=e.map(h=>h[o]),i=s.every(h=>h===1),a=r&&!i?ri(s):0,c=s.reduce((h,p)=>Math.max(h,Math.abs(p-a)),0),l=c>0&&Number.isFinite(c)?s.map(h=>(h-a)/c):s,f=Math.sqrt(qn(l,l)),m=l.slice();for(let h of n){let p=qn(m,h);m=m.map((d,g)=>d-p*h[g])}let u=Math.sqrt(qn(m,m));f>0&&u>1e-10*f&&(t.push(o),n.push(m.map(h=>h/u)))}return t}function bM(e,t,n,r){let o,s=[],i=!1,a=0;if(typeof t=="string"){if(r===void 0)throw new TypeError("data is required when mods is a formula");let f=t.includes("~")?t:`~ ${t}`,m=_r(mr(f),r,{intercept:n});if(o=m.x,s=m.colNames.map(h=>h==="(Intercept)"?"intrcpt":h),m.frame.rows.length!==e)throw new RangeError("variable lengths differ between yi and mods");if(o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let u=up(o);return{x:o.map(h=>u.map(p=>h[p])),names:u.map(h=>s[h]??""),warnings:u.length<o[0].length?["Redundant predictors dropped from the model."]:[]}}else if(t===void 0)o=Array.from({length:e},()=>[]);else if(Array.isArray(t))if(t.length===0)o=Array.from({length:e},()=>[]);else if(typeof t[0]=="number"){let f=Array.from(t,Number);if(f.length!==e)throw new RangeError("variable lengths differ (found for 'mods')");o=f.map(m=>[m]),a=1}else{let f=t;if(f.length===e)o=Array.from(f,m=>Array.from(m,Number)),a=o[0]?.length??0;else if(f.every(m=>m.length===e))a=f.length,o=Array.from({length:e},(m,u)=>f.map(h=>Number(h[u])));else throw new RangeError("variable lengths differ (found for 'mods')")}else{let f=t;if(f.rows!==void 0){let m=f.rows[0];if(m!=null&&typeof m=="object"&&!Array.isArray(m)){let u=typeof f.colNames=="string"?[f.colNames]:f.colNames===void 0||f.colNames===null?Object.keys(m):Array.from(f.colNames);o=f.rows.map(h=>u.map(p=>Number(h[p]))),s=u.map(h=>h==="(Intercept)"?"intrcpt":h),i=!0,a=u.length}else o=f.rows.map(u=>Array.from(u,Number)),a=o[0]?.length??0}else if(f.columns!==void 0)a=f.columns.length,o=Array.from({length:e},(m,u)=>f.columns.map(h=>Number(h[u])));else throw new TypeError("mods matrix must have rows or columns");if(o.length!==e)throw new RangeError("variable lengths differ (found for 'mods')");f.colNames!==void 0&&f.colNames!==null&&(s=(typeof f.colNames=="string"?[f.colNames]:Array.from(f.colNames)).map(m=>m==="(Intercept)"?"intrcpt":m),i=!0)}if(o.some(f=>f.length!==o[0].length))throw new TypeError("all rows of the moderator matrix must have the same length");if(i||(s=Array.from({length:a},(f,m)=>a>1?`.${m+1}`:"")),n&&(o=o.map(f=>[1,...f]),s=["intrcpt",...s]),o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let c=up(o);if(c.length<o[0].length&&!i&&a>0){for(let f=0;f<c.length;f++)if(c[f]!==0||!n){s[c[f]]="";break}}return{x:o.map(f=>c.map(m=>f[m])),names:c.map(f=>s[f]??""),warnings:c.length<o[0].length?["Redundant predictors dropped from the model."]:[]}}function cl(e){let t=[676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,15056327351493116e-23];if(e<.5)return Math.log(Math.PI)-Math.log(Math.sin(Math.PI*e))-cl(1-e);let n=e-1,r=.9999999999998099;for(let s=0;s<t.length;s++)r+=t[s]/(n+s+1);let o=n+7.5;return .9189385332046727+(n+.5)*Math.log(o)-o+Math.log(r)}function pp(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function il(e,t,n,r,o){let s=pp(e,t,n,r);if(s===0)return NaN;let i=e/s,a=t/s,c=n/s,l=r/s,f=Le(Xt(i,l),Xt(a,c)),m=zt(i,a),u=zt(c,l),h=o==="RR"?_e([c,0],m):o==="OR"?Xt(a,c):_e(m,u),p=ut(f,h);return p[0]+p[1]}function vM(e){let t=e/2;if(t>1e3){let n=1/t,r=n*n,o=r*n,s=r*r,i=s*n,a=o*o,c=a*n,l=s*s,f=-(3/8)*n-1/8*r-3/64*o-1/64*s-3/640*i-1/384*a-33/14336*c-1/2048*l;return Math.exp(f)}return Math.exp(cl(t)-.5*Math.log(t)-cl(t-.5))}function NM(e,t){z(t,hM,"escalc");let n=[],r=f=>t[f]===void 0?void 0:Array.from(t[f]),s={RR:["ai","bi","ci","di"],OR:["ai","bi","ci","di"],RD:["ai","bi","ci","di"],MD:["m1i","sd1i","n1i","m2i","sd2i","n2i"],SMD:["m1i","sd1i","n1i","m2i","sd2i","n2i"],ZCOR:["ri","ni"]}[e].map(f=>r(f)),i=s.slice(0,4).some(f=>f.some(m=>m===0));if(s.some(f=>f===void 0))throw new TypeError(`missing required arguments for measure ${e}`);let a=s[0].length;if(s.some(f=>f.length!==a))throw new RangeError("variable lengths differ");let c=[],l=[];for(let f=0;f<a;f++){let m=NaN,u=NaN;if(e==="ZCOR"){let h=s[0][f],p=s[1][f];if(Math.abs(h)>1)throw new RangeError("One or more correlations are > 1 or < -1.");p>3?(m=Math.atanh(h),u=1/(p-3)):(n.push("Cannot estimate the sampling variance when ni <= 3."),m=Math.atanh(h),u=NaN)}else if(e==="MD"||e==="SMD"){let h=s[0][f],p=s[1][f],d=s[2][f],g=s[3][f],w=s[4][f],N=s[5][f];if(!(d>1&&N>1&&p>=0&&w>=0))throw new RangeError("invalid means, standard deviations, or sample sizes");if(e==="MD")m=h-g,u=p*p/d+w*w/N;else{let b=d+N-2,E=((d-1)*p*p+(N-1)*w*w)/b,v=(h-g)/Math.sqrt(E);m=vM(b)*v,u=(d+N)/(d*N)+m*m/(2*(d+N))}}else{let h=s[0][f],p=s[1][f],d=s[2][f],g=s[3][f],w=h,N=p,b=d,E=g,v=t.add??.5,y=t.to??"only0",T=[w,N,b,E].some(O=>O===0),M=y==="all"||y==="only0"&&T||y==="if0all"&&i;if(e!=="RD"&&M&&(w+=v,N+=v,b+=v,E+=v),e==="RD"&&M&&(w+=v,N+=v,b+=v,E+=v),t.drop00&&(h===0&&d===0||p===0&&g===0)){c.push(NaN),l.push(NaN);continue}let R=pp(w,N,b,E),x=w/R,A=N/R,S=b/R,I=E/R,C=x+A,_=S+I,F=1/R;e==="RR"?(m=Math.log1p(il(w,N,b,E,"RR")),u=F*(A/(x*C)+I/(S*_))):e==="OR"?(m=Math.log1p(il(w,N,b,E,"OR")),u=F*(1/x+1/A+1/S+1/I)):(m=il(w,N,b,E,"RD"),u=F*(x*A/(C*C*C)+S*I/(_*_*_)))}c.push(m),l.push(u)}return J("escalc",{yi:c,vi:l,measure:e,warnings:n})}function hn(e,t){let n=Xo([...e,...t]);return Math.abs(Math.log2(n))>200?n:1}function ll(e,t,n,r){if(t===1)return e;let o=s=>Array.isArray(s)?s.map(i=>i*t):s*t;return{...e,beta:{...e.beta,rows:e.beta.rows.map(s=>s.map(i=>i*t))},se:o(e.se),"ci.lb":o(e["ci.lb"]),"ci.ub":o(e["ci.ub"]),tau2:e.tau2*t*t,"se.tau2":e["se.tau2"]*t*t,tau2Null:e.tau2Null*t*t,covariance:e.covariance.map(s=>s.map(i=>i*t*t)),yi:[...n],vi:[...r]}}function In(e,t,n={}){lt(t)&&z(t,ap,"rma"),lt(t)&&z(_t(t,"control"),ip,"rma","control"),z(n,ap,"rma"),z(_t(n,"control"),ip,"rma","control");let r=typeof e=="object"&&"yi"in e,o=Array.from(r?e.yi:e),s=(r?t:n)??{},i=Array.from(r?e.vi:t??[]);if(o.length===0||i.length!==o.length)throw new RangeError("yi and vi must have the same positive length");if(o.some(P=>!Number.isFinite(P)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(i.some(P=>!Number.isFinite(P)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(s.method==="DL"&&i.some(P=>P<=0))throw new Error("DL estimator cannot be used when there are non-positive sampling variances in the data.");if(i.some(P=>P<0))throw new RangeError("sampling variances must be non-negative");if(i.some(P=>P===0)&&!["EE","FE"].includes(s.method??"REML"))throw new RangeError("random-effects estimators require positive sampling variances");let a=s.method??"REML",c=s.test??"z",l=s.level??95,f=s.intercept??!0,m=bM(o.length,s.mods,f,s.data),u=m.x,h=u[0].length,p=o.length,d=p-h,g=m.names[0]==="intrcpt"||!f&&u.every(P=>P[0]===1);if(h===0)throw new RangeError("the model must contain at least one coefficient");if(p<h)throw new Error("Number of parameters to be estimated is larger than the number of observations.");if(!(l>0&&l<100))throw new RangeError("level must be between 0 and 100");if(!["EE","FE","DL","HE","HS","HSk","SJ","ML","REML","EB","PM"].includes(a))throw new RangeError(`unknown method '${a}'`);if(!["z","t","knha"].includes(c))throw new RangeError(`unknown test '${c}'`);let w=hn(o,i.map(P=>Math.sqrt(P)));if(w!==1&&i.every(P=>P===0||P/w/w>0))return ll(In(o.map(P=>P/w),i.map(P=>P/w/w),s),w,o,i);if(a==="DL"&&!Number.isFinite(Ut(o,i,u,0).qe))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");let N=Ut(o,i,u,0),b=mp(o,i,u,a,s.control??{}),E=Ut(o,i,u,b),v=s.mods===void 0?b:mp(o,i,Array.from({length:p},()=>[1]),a,s.control??{}),y=c==="knha"&&d>0?Math.max(1,E.qe/d):1,T=E.cov.map(P=>P.map(Q=>Q*y)),M=E.se.map(P=>P*Math.sqrt(y)),R=E.beta.map((P,Q)=>P/M[Q]),x=c==="z"?re(1-(1-l/100)/2):an(1-(1-l/100)/2,d),A=R.map(P=>2*(c==="z"?oe(-Math.abs(P)):yn(-Math.abs(P),d))),S=E.beta.map((P,Q)=>P-x*M[Q]),I=E.beta.map((P,Q)=>P+x*M[Q]),C=g?1:0,_=h===1?1:Math.max(1,h-C),F=0,O=1;if(h===1)F=R[0]*R[0],O=c==="z"?Ze(F,1,{lowerTail:!1}):Cn(F,1,d,{lowerTail:!1});else{let P=R.slice(C),Q=E.corr.slice(C).map(K=>K.slice(C));try{F=qn(P,ot(Q,P))}catch{F=NaN}O=c==="z"?Ze(F,_,{lowerTail:!1}):Cn(F/_,_,d,{lowerTail:!1})}let D=N.tracePNorm,k=d>0?d*N.minVariance/D:0,L=a==="EE"||a==="FE"?N.qe>0?100*Math.max(0,(N.qe-d)/N.qe):0:b===0?0:100*b/(b+k),q=a==="EE"||a==="FE"?d>0?Math.max(1,N.qe/d):1:b===0?1:(b+k)/k,$=s.mods===void 0||h===1?null:v>0?100*Math.max(0,(v-b)/v):0,V=wM(o,i,u,a,b,E,N);return J("rma",{beta:{dim:[h,1],rowNames:h===1?m.names[0]??"intrcpt":m.names,colNames:null,rows:E.beta.map(P=>[P])},se:kn(M),zval:kn(R),pval:kn(A),"ci.lb":kn(S),"ci.ub":kn(I),tau2:b,"se.tau2":V,QE:N.qe,QEp:d===0?1:Ze(N.qe,d,{lowerTail:!1}),QM:F,QMp:O,R2:$,p:h,m:_,I2:L,H2:q,k:p,yi:o,vi:i,mods:u,intercept:g,method:a,test:c,level:l,warnings:m.warnings,covariance:T,tau2Null:v})}function fl(e,t={}){z(t,pM,"confintRma");let n=t.type??"QP",r=t.level??e.level,o=1-r/100;if(n!=="QP"&&n!=="PL")throw new RangeError("type must be 'QP' or 'PL'");let s=Ut(e.yi,e.vi,e.mods,e.tau2),i=e.k-e.mods[0].length,a=0,c=NaN;if(n==="QP"){let g=Ut(e.yi,e.vi,e.mods,0),w=jn(1-o/2,i),N=jn(o/2,i),b=g.qe,E=y=>Ut(e.yi,e.vi,e.mods,y).qe,v=e.tau2>0?e.tau2:g.minVariance;a=b<=w?0:lp(y=>E(y)-w,v),c=b<=N?NaN:lp(y=>E(y)-N,v)}else{let g=jn(r/100,1),w=b=>Ut(e.yi,e.vi,e.mods,b).logLik,N=b=>2*(s.logLik-w(b))-g;if(e.tau2>0){a=N(0)<=0?0:Fo(N,0,e.tau2);let b=e.tau2*2,E=N(b);for(let v=0;v<2100&&E<0&&Number.isFinite(b);v++)b*=2,E=N(b);c=E>=0?Fo(N,e.tau2,b):NaN}else{let b=Ut(e.yi,e.vi,e.mods,0).minVariance,E=N(b);for(let v=0;v<2100&&E<0&&Number.isFinite(b);v++)b*=2,E=N(b);c=E>=0?Fo(N,0,b):NaN}}let l=Ut(e.yi,e.vi,e.mods,0),f=i>0?i*l.minVariance/l.tracePNorm:0,m=g=>[g,Math.sqrt(g),100*g/(g+f),(g+f)/f],u=m(e.tau2),h=m(a),p=m(c),d=["tau^2","tau","I^2(%)","H^2"];return{random:{dim:[4,3],rowNames:d,colNames:["estimate","ci.lb","ci.ub"],rows:d.map((g,w)=>({estimate:u[w],"ci.lb":h[w],"ci.ub":p[w]}))},digits:{est:"4",se:"4",test:"4",pval:"4",ci:"4",var:"4",sevar:"4",fit:"4",het:"4"},"ci.null":!1,"lb.sign":"","ub.sign":"","tau2.min":"0"}}function MM(e,t={}){z(t,dM,"predictRma");let n=t.level??e.level,r=1-n/100,o=e.mods[0].length,s;if(t.newmods===void 0)s=o-(e.intercept?1:0)===0?[e.intercept?[1]:[]]:e.mods.map(h=>h.slice());else{let u=t.newmods;if(u.length>0&&typeof u[0]=="number"?s=Array.from(u,h=>e.intercept?[1,h]:[h]):s=Array.from(u,h=>Array.from(h)),s.some(h=>h.length===o-(e.intercept?1:0))&&(s=s.map(h=>e.intercept?[1,...h]:h)),s.some(h=>h.length!==o))throw new RangeError("newmods has the wrong number of columns")}let i=e.beta.rows.map(u=>u[0]),a=s.map(u=>qn(u,i)),c=s.map(u=>Math.sqrt(Math.max(0,qn(u,Oo(e.covariance,u))))),l=e.test==="z"?re(1-r/2):an(1-r/2,e.k-o),f=t.predtype==="riley"?an(1-r/2,e.k-o-1):l,m=s.map((u,h)=>Math.sqrt(c[h]**2+e.tau2));return{pred:kn(a),se:kn(c),"ci.lb":kn(a.map((u,h)=>u-l*c[h])),"ci.ub":kn(a.map((u,h)=>u+l*c[h])),"pi.lb":kn(a.map((u,h)=>u-f*m[h])),"pi.ub":kn(a.map((u,h)=>u+f*m[h]))}}function yM(e,t,n={}){lt(t)&&z(t,cp,"regtest"),z(n,cp,"regtest");let r=typeof e=="object"&&"beta"in e,o=(r?t:n)??{},s=o.model??"rma",i=o.predictor??"sei",a=o.level??95,c=Array.from(r?e.yi:e),l=Array.from(r?e.vi:t??[]);if(c.length!==l.length||c.length<2)throw new RangeError("yi and vi must have equal lengths and at least two studies");let f=l.map(v=>Math.sqrt(v)),m=i==="sei"?f:i==="vi"?l:l.map((v,y)=>1/(y+1)),u,h,p,d,g,w;if(s==="lm"){let v=m.map((x,A)=>[1,1/x]),y=c.map((x,A)=>x/f[A]),T=EM(v,y);u=T.beta[1];let M=Math.sqrt(T.cov[0][0]*T.sigma2),R=Math.sqrt(T.cov[1][1]*T.sigma2);p=T.beta[0]/M,h=R,d=c.length-2,g=2*yn(-Math.abs(p),d)}else{w=In(c,l,{method:"REML",mods:m,intercept:!0,test:"z"}),u=w.beta.rows[0][0],h=Array.isArray(w.se)?w.se[0]:w.se;let v=Array.isArray(w.zval)?w.zval[1]:NaN,y=Array.isArray(w.pval)?w.pval[1]:NaN;p=v,d=NaN,g=y}let N=s==="lm"?an(1-(1-a/100)/2,d):re(1-(1-a/100)/2),b=[];s==="lm"&&(!Number.isFinite(p)||!Number.isFinite(g)||!Number.isFinite(h))&&b.push("NaNs produced");let E={model:s,predictor:i,zval:p,pval:g,dfs:d,est:u,"ci.lb":u-N*h,"ci.ub":u+N*h,warnings:b};return J("regtest",o.retFit&&w!==void 0?{...E,fit:w}:E)}function EM(e,t){let n=Array.from({length:2},(a,c)=>Array.from({length:2},(l,f)=>e.reduce((m,u,h)=>m+u[c]*u[f],0))),r=ot(n,hp(2)),o=Oo(r,Array.from({length:2},(a,c)=>e.reduce((l,f,m)=>l+f[c]*t[m],0))),s=t.map((a,c)=>a-qn(e[c],o)),i=t.length>2?s.reduce((a,c)=>a+c*c,0)/(t.length-2):NaN;return{beta:o,cov:r,sigma2:i}}var TM=["type","opposite","twoSided"];function ml(e,t){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(!Number.isSafeInteger(t)||t<3)return NaN;if(e<=0)return 1;if(e>=(t-1)/Math.sqrt(t))return 0;let n=t-1,r=Xt(n,n),o=Kt(Xt(e,e),t),s=Le(r,o);if(s[0]+s[1]<=0)return 0;let i=ut(s,r),a=ut(o,r),c=Sl((t-2)/2,.5,i,a,!0,!1);return Math.min(1,t*c/2)}function dp(e,t){let n=ml(e,t);return Number.isNaN(n)?NaN:1-n}function RM(e,t,n=10){if(n!==10)throw new RangeError(`Grubbs type ${n} is not supported in rStats.js 1.9.0 (type 10 only)`);return Ya(e,r=>dp(r,t))}function xM(e){let t=0,n=(e-1)/Math.sqrt(e);for(let r=0;r<80;r++){let o=t+(n-t)/2;if(o===t||o===n)break;ml(o,e)>=1?t=o:n=o}return n}function SM(e,t){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(!Number.isSafeInteger(t)||t<3||e<0||e>1)return NaN;if(e===0)return xM(t);if(e===1)return(t-1)/Math.sqrt(t);let n=0,r=(t-1)/Math.sqrt(t);for(let o=0;o<80;o++){let s=n+(r-n)/2;dp(s,t)<e?n=s:r=s}return n+(r-n)/2}function AM(e,t,n=10){if(n!==10)throw new RangeError(`Grubbs type ${n} is not supported in rStats.js 1.9.0 (type 10 only)`);return Ya(e,r=>SM(r,t))}function kM(e,t={}){z(t,TM,"grubbsTest");let n=t.type??10;if(n!==10)throw new RangeError(`Grubbs type ${n} is not supported in rStats.js 1.9.0 (type 10 only)`);let r=Array.from(e).filter(N=>!Number.isNaN(N));if(r.some(N=>!Number.isFinite(N)))throw new RangeError("x must contain finite values");let o=r.length;if(o<2)throw new RangeError("sample size must be at least 2");let s=t.opposite??!1,i=t.twoSided??!1,a=r[0];for(let N=1;N<r.length;N++)(s?r[N]<a:r[N]>a)&&(a=r[N]);let c=qs(r),l=r.indexOf(a),f=Math.abs(c[l]),m=r.slice();m.splice(l,1);let u=Rt([r,m]),h=(o-2)*u.variance[1]/((o-1)*u.variance[0]),p=ml(f,o),d=1-p,g=Number.isNaN(f)?NaN:Math.min(1,i?2*Math.min(d,p):p);return{method:"Grubbs test for one outlier",statistic:[f,h],statisticName:["G","U"],pValue:g,alternative:`${s?"lowest":"highest"} value ${String(a)} is an outlier`,warnings:o===2?["NaNs produced"]:[]}}var qM=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict"],vn=e=>e.reduce((t,n)=>t+n,0);function gp(e,t){let n=Xo(t),r=t.map(d=>d/n),o=e/n,s=Math.max(0,...r),i=r.filter(d=>d>1e-14*s);if(i.length===0)return o>=0?1:0;if(o<=0)return 0;let a=i.length,c=Math.min(...i),l=Math.max(...i),f=2*c*l/(c+l),m=(l-c)/(l+c),u=m<1e-12?1:Math.ceil(Math.log(1e-17)/Math.log(m))+10;if(u<=2e4){let d=o/f,g=i.map(T=>1-f/T),w=[Math.exp(.5*vn(i.map(T=>Math.log(f/T))))],N=[0];for(let T=1;T<=u;T++)N.push(.5*vn(g.map(M=>M**T)));let b=Ze(d,a),E=a/2*Math.log(d/2)-d/2-Ko(a/2+1),v=w[0]*b,y=w[0];for(let T=1;T<=u;T++){let M=0;for(let x=0;x<T;x++)M+=N[T-x]*w[x];M/=T,w.push(M);let R=a+2*(T-1);if(b-=Math.exp(E),E+=Math.log(d/2)-Math.log(R/2+1),v+=M*b,y+=M,Math.abs(1-y)<1e-16&&Math.abs(M)<1e-18)break}return Math.min(1,Math.max(0,v))}let p=Ul(d=>{if(d===0)return .5*(vn(i)-o);let g=0,w=0;for(let N of i)g+=Math.atan(N*d),w+=Math.log1p(N*N*d*d);return Math.sin(.5*g-.5*o*d)/(d*Math.exp(.25*w))},0,1/0,{relTol:1e-10,subdivisions:5e3,stopOnError:!1});return Math.min(1,Math.max(0,.5-p.value/Math.PI))}function wp(e,t,n,r){let o=e.length,s=vn(n),i=vn(n.map((m,u)=>m*e[u]))/s,a=vn(n.map((m,u)=>m*(e[u]-i)**2)),c=m=>{let u=t.map(p=>Math.sqrt(p+m)),h=Array.from({length:o},(p,d)=>Array.from({length:o},(g,w)=>u[d]*((d===w?n[d]:0)-n[d]*n[w]/s)*u[w]));return gp(a,Wo(h).values.map(p=>Math.max(0,p)))},l=1-r,f=m=>{if(c(0)<=m)return 0;let u=Math.max(...t);for(let p=0;p<2100&&c(u)>m&&Number.isFinite(u);p++)u*=2;let h=0;for(let p=0;p<2200&&u-h>1e-15*u;p++){let d=(h+u)/2;if(d===h||d===u)break;c(d)>m?h=d:u=d}return(h+u)/2};return[f(1-l/2),f(l/2)]}function Po(e,t,n){let r=t-1,o=t>1?Math.max(1,Math.sqrt(e/r)):NaN,s=t>1?Math.max(0,(e-r)/e):NaN,i=NaN,a=NaN;if(t>2){let c=e>t?.5*(Math.log(e)-Math.log(r))/(Math.sqrt(2*e)-Math.sqrt(2*t-3)):Math.sqrt(1/(2*(t-2))*(1-1/(3*(t-2)**2))),l=re(.5+n/2),f=Math.log(Math.max(o,1));i=Math.max(1,Math.exp(f-l*c)),a=Math.max(1,Math.exp(f+l*c))}return{H:o,lH:i,uH:a,I2:s,lI2:Math.max(0,1-1/(i*i)),uI2:Math.max(0,1-1/(a*a))}}function Kn(e,t,n={}){let r=Math.max(...t.map(Math.abs)),o=r>0&&Number.isFinite(r)&&(r>2**200||r<2**-200)?2**Math.round(Math.log2(r)):1;if(o===1)return ul(e,t,n);let s=ul(e.map(l=>l/o),t.map(l=>l/o),{...n,...n.fixedTau2!==void 0?{fixedTau2:n.fixedTau2/o/o}:{}}),i=["TE.common","seTE.common","lower.common","upper.common","TE.random","seTE.random","lower.random","upper.random","seTE.predict","lower.predict","upper.predict","tau","lower.tau","upper.tau"],a=["tau2","lower.tau2","upper.tau2"],c={...s,TE:e.slice(),seTE:t.slice(),"w.common":s["w.common"].map(l=>l/o/o),"w.random":s["w.random"].map(l=>l/o/o)};for(let l of i)c[l]=s[l]*o;for(let l of a)c[l]=s[l]*o*o;return c}function ul(e,t,n={}){if(e.length===0){let P=ul([0],[1],n);for(let Q of Object.keys(P))typeof P[Q]=="number"&&(P[Q]=NaN);return{...P,TE:[],seTE:[],k:0,"w.common":[],"w.random":[],"df.Q":NaN,"df.predict":NaN}}let r=e.length,o=t.map(P=>P*P),s=n.methodTau??"REML",i=n.methodRandomCi??"classic",a=n.methodTauCi??(s==="DL"?"J":"QP"),c=n.level??.95,l=n.levelMa??c,f=n.levelPredict??c,m=re(.5+l/2),u=o.map(P=>1/P),h=vn(u),p=vn(u.map((P,Q)=>P*e[Q]))/h,d=Math.sqrt(1/h),g=n.fixedQ??vn(u.map((P,Q)=>P*(e[Q]-p)**2)),w=r-1,N=0;n.fixedTau2!==void 0?N=n.fixedTau2:r>1&&(N=In(e,o,{method:s}).tau2);let b=o.map(P=>1/(P+N)),E=vn(b),v=vn(b.map((P,Q)=>P*e[Q]))/E,y=Math.sqrt(1/E),T,M,R,x,A=null;if(i==="HK"&&r>1){let P=vn(b.map((K,X)=>K*(e[X]-v)**2))/(r-1);y=Math.sqrt(P/E),A=r-1;let Q=it(.5+l/2,A,!0,!1);T=v-Q*y,M=v+Q*y,R=v/y,x=2*at(-Math.abs(R),A,!0,!1)}else T=v-m*y,M=v+m*y,R=v/y,x=2*oe(-Math.abs(R));let S=p/d,I=Math.sqrt(N+1/E),C=r-1,_=C>0?it(.5+f/2,C,!0,!1):NaN,F=NaN,O=NaN;if(r>1)if(a==="QP"){let P=In(e,o,{method:s,level:100*c}),Q=fl(P,{type:"QP",level:100*c}).random.rows[0];F=Q["ci.lb"],O=Number.isFinite(Q["ci.ub"])?Q["ci.ub"]:0}else[F,O]=wp(e,o,a==="J"?t.map(P=>1/P):u,c);let{H:D,lH:k,uH:L,I2:q,lI2:$,uI2:V}=Po(g,r,c);return{TE:e.slice(),seTE:t.slice(),k:r,"TE.common":p,"seTE.common":d,"lower.common":p-m*d,"upper.common":p+m*d,"statistic.common":S,"pval.common":2*oe(-Math.abs(S)),"TE.random":v,"seTE.random":y,"lower.random":T,"upper.random":M,"statistic.random":R,"pval.random":x,"df.random":A,"seTE.predict":I,"lower.predict":v-_*I,"upper.predict":v+_*I,"df.predict":C,Q:g,"df.Q":w,"pval.Q":r>1?Ze(g,w,{lowerTail:!1}):NaN,tau2:r>1?N:NaN,"lower.tau2":F,"upper.tau2":O,tau:r>1?Math.sqrt(N):NaN,"lower.tau":Math.sqrt(F),"upper.tau":Math.sqrt(O),H:D,"lower.H":k,"upper.H":L,I2:q,"lower.I2":$,"upper.I2":V,"w.common":u,"w.random":b,methodTau:s,methodRandomCi:i,methodTauCi:a}}function IM(e,t,n={}){if(z(n,qM,"metagen"),e.length!==t.length)throw new RangeError("metagen: TE and seTE must have the same length");if(e.length===0)throw new RangeError("metagen: no studies");if(e.map((o,s)=>Number.isFinite(o)&&Number.isFinite(t[s])&&t[s]>0).some(o=>!o))throw new RangeError("metagen: TE must be finite and seTE finite and positive");return Kn(e,t,n)}var _M=new Set(["methodTau","sm","method","incr","methodIncr","allstudies","MHexact","QCochrane","methodRandomCi","methodTauCi","level","levelMa","levelPredict"]),Gt=e=>e.reduce((t,n)=>t+n,0);function CM(e,t,n,r,o={}){z(o,_M,"metabin");let s=e.length;if(![t,n,r].every(S=>S.length===s))throw new RangeError("metabin: e1, n1, e2 and n2 must have the same length");for(let S=0;S<s;S++)if(!(e[S]>=0&&e[S]<=t[S]&&n[S]>=0&&n[S]<=r[S]&&t[S]>0&&r[S]>0))throw new RangeError("metabin: events must lie in 0..n and n > 0");let i=o.sm??"RR",a=o.method??"MH",c=o.incr??.5,l=o.methodIncr??"only0";if(a==="Peto"&&i!=="OR")throw new RangeError("metabin: Peto's method is for sm = 'OR'");let f=e.map(Number),m=n.map(Number),u=f.map((S,I)=>t[I]-S),h=m.map((S,I)=>r[I]-S),p=f.map((S,I)=>f[I]===0||u[I]===0||m[I]===0||h[I]===0),d=f.map((S,I)=>f[I]===0&&m[I]===0||u[I]===0&&h[I]===0),g=f.map((S,I)=>i==="RD"||o.allstudies||!d[I]),w=p.some((S,I)=>S&&g[I]),N=f.map((S,I)=>l==="all"||l==="if0all"&&w||p[I]?c:0),b=new Array(s).fill(NaN),E=new Array(s).fill(NaN);for(let S=0;S<s;S++){if(!g[S])continue;let I=N[S],C=f[S]+I,_=u[S]+I,F=m[S]+I,O=h[S]+I;if(a==="Peto"){let D=t[S]+r[S],k=f[S]+m[S],L=t[S]*k/D,q=t[S]*r[S]*k*(D-k)/(D*D*(D-1));b[S]=(f[S]-L)/q,E[S]=1/Math.sqrt(q)}else if(i==="OR")b[S]=Math.log(C*O/(_*F)),E[S]=Math.sqrt(1/C+1/_+1/F+1/O);else if(i==="RR"){let D=t[S]+I,k=r[S]+I;b[S]=Math.log(C/D)-Math.log(F/k),E[S]=Math.sqrt(1/C-1/D+1/F-1/k)}else b[S]=f[S]/t[S]-m[S]/r[S],E[S]=Math.sqrt(C*_/(C+_)**3+F*O/(F+O)**3)}let v=b.map((S,I)=>g[I]&&Number.isFinite(S)&&Number.isFinite(E[I])&&E[I]>0?I:-1).filter(S=>S>=0),y=Kn(v.map(S=>b[S]),v.map(S=>E[S]),o),T={...y,TE:b,seTE:E,k:v.length},M=S=>{let I=new Array(s).fill(0);return v.forEach((C,_)=>{I[C]=S[_]}),I};T["w.random"]=M(y["w.random"]);let R=o.levelMa??o.level??.95,x=re(.5+R/2),A=(S,I,C)=>{let _=S/I;Object.assign(T,{"TE.common":S,"seTE.common":I,"lower.common":S-x*I,"upper.common":S+x*I,"statistic.common":_,"pval.common":2*oe(-Math.abs(_)),"w.common":C})};if(a==="Peto")A(y["TE.common"],y["seTE.common"],M(y["w.common"]));else if(a==="MH"){let S=f.map((V,P)=>o.MHexact?0:N[P]),I=v.map(V=>f[V]+S[V]),C=v.map(V=>u[V]+S[V]),_=v.map(V=>m[V]+S[V]),F=v.map(V=>h[V]+S[V]),O=I.map((V,P)=>V+C[P]),D=_.map((V,P)=>V+F[P]),k=O.map((V,P)=>V+D[P]),L,q,$;if(i==="OR"){let V=I.map((se,H)=>se*F[H]/k[H]),P=C.map((se,H)=>se*_[H]/k[H]),Q=I.map((se,H)=>(se+F[H])/k[H]),K=C.map((se,H)=>(se+_[H])/k[H]),X=Gt(V),Y=Gt(P);L=Math.log(X/Y),q=Math.sqrt(Gt(Q.map((se,H)=>se*V[H]))/(2*X*X)+Gt(Q.map((se,H)=>se*P[H]+K[H]*V[H]))/(2*X*Y)+Gt(K.map((se,H)=>se*P[H]))/(2*Y*Y)),$=P}else if(i==="RR"){let V=I.map((Q,K)=>Q*D[K]/k[K]),P=_.map((Q,K)=>Q*O[K]/k[K]);L=Math.log(Gt(V)/Gt(P)),q=Math.sqrt(Gt(k.map((Q,K)=>(O[K]*D[K]*(I[K]+_[K])-I[K]*_[K]*Q)/(Q*Q)))/(Gt(V)*Gt(P))),$=P}else{let V=O.map((P,Q)=>P*D[Q]/k[Q]);L=Gt(V.map((P,Q)=>P*b[v[Q]]))/Gt(V),q=Math.sqrt(Gt(k.map((P,Q)=>(I[Q]*C[Q]*D[Q]**3+_[Q]*F[Q]*O[Q]**3)/(O[Q]*D[Q]*P*P)))/Gt(V)**2),$=V}if(A(L,q,M($)),(o.methodTau??"REML")==="DL"&&(o.QCochrane??!0)){let V=v.map(H=>b[H]),P=v.map(H=>1/E[H]**2),Q=Gt(V.map((H,j)=>P[j]*(H-L)**2)),K=v.length-1,X=Gt(P),Y=Math.max(0,(Q-K)/(X-Gt(P.map(H=>H*H))/X)),se=Kn(V,v.map(H=>E[H]),{...o,methodTau:"DL",fixedTau2:Y,fixedQ:Q});Object.assign(T,LM(se),{Q,"pval.Q":Ze(Q,K,{lowerTail:!1}),"w.random":M(se["w.random"])})}}else A(y["TE.common"],y["seTE.common"],M(y["w.common"]));return T}function LM(e){let t=["TE.random","seTE.random","lower.random","upper.random","statistic.random","pval.random","df.random","seTE.predict","lower.predict","upper.predict","tau2","lower.tau2","upper.tau2","tau","lower.tau","upper.tau","H","lower.H","upper.H","I2","lower.I2","upper.I2"],n={};for(let r of t)n[r]=e[r];return n}var OM=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","sm"],FM=new Set(["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","sm","method","incr"]),PM=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","sm"],DM=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","sm"],VM=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","sm","incr"],$M=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","sm","incr"],zM=["methodRandomCi","level","levelMa","levelPredict"],nr=e=>e.reduce((t,n)=>t+n,0),Xr=(e,...t)=>{let n=t[0].length;if(t.some(r=>r.length!==n))throw new RangeError(`${e}: the inputs must have the same length`);if(n===0)throw new RangeError(`${e}: no studies`);return n},rr=(e,t)=>{if(!e)throw new RangeError(t)},Do=(e,t,n)=>rr(e.every(r=>Number.isFinite(r)&&r>0),`${t}: ${n} must be finite and positive`),bp=(e,t,n)=>{Do(t,n,"n"),rr(e.every((r,o)=>Number.isFinite(r)&&r>=0&&r<=t[o]),`${n}: events must lie in 0..n`)};function Vo(e,t,n){let r=e.map((i,a)=>Number.isFinite(i)&&Number.isFinite(t[a])&&t[a]>0?a:-1).filter(i=>i>=0);if(r.length===0)throw new RangeError("meta: no study with a finite estimate and standard error");let o=Kn(r.map(i=>e[i]),r.map(i=>t[i]),n),s=i=>{let a=new Array(e.length).fill(0);return r.forEach((c,l)=>{a[c]=i[l]}),a};return{...o,TE:e,seTE:t,k:r.length,"w.common":s(o["w.common"]),"w.random":s(o["w.random"])}}function KM(e,t,n,r,o,s,i={}){z(i,OM,"metacont");let a=Xr("metacont",e,t,n,r,o,s),c=i.sm??"MD";Do(e,"metacont","n1"),Do(r,"metacont","n2"),rr([...n,...s].every(m=>Number.isFinite(m)&&m>=0),"metacont: sd must be finite and non-negative"),rr([...t,...o].every(Number.isFinite),"metacont: means must be finite");let l=[],f=[];for(let m=0;m<a;m++){let u=e[m],h=r[m],p=n[m],d=s[m];if(c==="MD")l.push(t[m]-o[m]),f.push(Math.hypot(p/Math.sqrt(u),d/Math.sqrt(h)));else if(c==="ROM")l.push(Math.log(t[m]/o[m])),f.push(Math.hypot(p/(Math.sqrt(u)*t[m]),d/(Math.sqrt(h)*o[m])));else{let g=u+h,w=Math.hypot(p*Math.sqrt(u-1),d*Math.sqrt(h-1))/Math.sqrt(g-2),N=g-2,b=Math.exp(Ko(N/2)-.5*Math.log(N/2)-Ko((N-1)/2)),E=b*(t[m]-o[m])/w;l.push(E),f.push(Math.sqrt(1/u+1/h+(1-(g-4)/((g-2)*b*b))*E*E))}}return Vo(l,f,i)}function YM(e,t,n,r,o={}){z(o,FM,"metainc");let s=Xr("metainc",e,t,n,r),i=o.sm??"IRR",a=o.method??"MH",c=o.incr??.5;Do([...t,...r],"metainc","time"),rr([...e,...n].every(u=>Number.isFinite(u)&&u>=0),"metainc: events must be finite and non-negative");let l=[],f=[];for(let u=0;u<s;u++){let h=e[u]===0||n[u]===0?c:0,p=e[u]+h,d=n[u]+h;i==="IRR"?(l.push(Math.log(p/t[u]/(d/r[u]))),f.push(Math.sqrt(1/p+1/d))):(l.push(e[u]/t[u]-n[u]/r[u]),f.push(Math.hypot(Math.sqrt(p)/t[u],Math.sqrt(d)/r[u])))}let m=Vo(l,f,o);if(a==="MH"){let u=t.map((b,E)=>b+r[E]),h,p,d;if(i==="IRR"){let b=e.map((v,y)=>v*(r[y]/u[y])),E=n.map((v,y)=>v*(t[y]/u[y]));h=Math.log(nr(b)/nr(E)),p=Math.sqrt(nr(u.map((v,y)=>t[y]/v*(r[y]/v)*(e[y]+n[y])))/(nr(b)*nr(E))),d=E}else{let b=t.map((T,M)=>T*(r[M]/u[M]));h=nr(e.map((T,M)=>T*(r[M]/u[M])-n[M]*(t[M]/u[M])))/nr(b);let E=Math.max(...b),v=b.map(T=>T/E),y=Math.max(...t,...r);p=Math.sqrt(nr(v.map((T,M)=>T*T*(e[M]/t[M]/(t[M]/y)+n[M]/r[M]/(r[M]/y)))))/Math.sqrt(y)/nr(v),d=b}let g=o.levelMa??o.level??.95,w=re(.5+g/2),N=h/p;Object.assign(m,{"TE.common":h,"seTE.common":p,"lower.common":h-w*p,"upper.common":h+w*p,"statistic.common":N,"pval.common":2*oe(-Math.abs(N)),"w.common":d})}return m}function WM(e,t,n={}){z(n,PM,"metacor"),Xr("metacor",e,t);let r=n.sm??"ZCOR";return rr(e.every(o=>Number.isFinite(o)&&o>=-1&&o<=1),"metacor: cor must lie in [-1, 1]"),rr(t.every(o=>Number.isFinite(o)&&o>(r==="ZCOR"?3:1)),`metacor: n must exceed ${r==="ZCOR"?3:1}`),Vo(e.map(o=>r==="ZCOR"?Math.atanh(o):o),e.map((o,s)=>r==="ZCOR"?1/Math.sqrt(t[s]-3):(1-o*o)/Math.sqrt(t[s]-1)),n)}function jM(e,t,n,r={}){z(r,DM,"metamean"),Xr("metamean",e,t,n);let o=r.sm??"MRAW";return Do(e,"metamean","n"),rr(n.every(s=>Number.isFinite(s)&&s>=0),"metamean: sd must be finite and non-negative"),Vo(t.map(s=>o==="MRAW"?s:Math.log(s)),n.map((s,i)=>o==="MRAW"?s/Math.sqrt(e[i]):s/(Math.sqrt(e[i])*t[i])),r)}function BM(e,t,n={}){z(n,VM,"metarate"),Xr("metarate",e,t);let r=n.sm??"IRLN",o=n.incr??.5;Do(t,"metarate","time"),rr(e.every(a=>Number.isFinite(a)&&a>=0),"metarate: events must be finite and non-negative");let s=[],i=[];return e.forEach((a,c)=>{let l=t[c],f=(r==="IR"||r==="IRLN")&&a===0?a+o:a;r==="IRLN"?(s.push(Math.log(f/l)),i.push(Math.sqrt(1/f))):r==="IR"?(s.push(f/l),i.push(Math.sqrt(f)/l)):r==="IRS"?(s.push(Math.sqrt(f/l)),i.push(Math.sqrt(1/(4*l)))):(s.push(.5*(Math.sqrt(f/l)+Math.sqrt((f+1)/l))),i.push(Math.sqrt(1/(4*l))))}),Vo(s,i,n)}function vp(e,t,n={}){z(n,$M,"metaprop"),Xr("metaprop",e,t);let r=n.sm??"PLOGIT",o=n.incr??.5;bp(e,t,"metaprop");let s=[],i=[];return e.forEach((a,c)=>{let l=t[c],f=a===0||a===l;if(r==="PLOGIT"){let m=f?a+o:a,u=f?l-a+o:l-a;s.push(Math.log(m/u)),i.push(Math.sqrt(1/m+1/u))}else if(r==="PFT")s.push(.5*(Math.asin(Math.sqrt(a/(l+1)))+Math.asin(Math.sqrt((a+1)/(l+1))))),i.push(Math.sqrt(1/(4*l+2)));else if(r==="PAS")s.push(Math.asin(Math.sqrt(a/l))),i.push(Math.sqrt(1/(4*l)));else if(r==="PRAW"){let m=f?a+o:a,u=f?l+2*o:l,h=m/u;s.push(a/l),i.push(Math.sqrt(h*(1-h)/u))}else{let m=a===0?a+o:a,u=f?l+o:l;s.push(Math.log(m/(a===0?u:l))),i.push(Math.sqrt(1/m-1/u))}}),Vo(s,i,n)}function UM(e,t,n={}){z(n,zM,"metapropGlmm");let r=Xr("metapropGlmm",e,t);if(bp(e,t,"metapropGlmm"),r<2)throw new RangeError("metapropGlmm: at least two studies");let o=e.map((S,I)=>`s${String(I+1).padStart(String(r).length,"0")}`),s={p:e.map((S,I)=>S/t[I]),w:t.slice(),study:o},i=lf("p ~ 1 + (1 | study)",s,{family:"binomial",weights:"w",nAGQ:7}),a=i.theta[0]**2,c=i.fixef[0],l=Math.sqrt(i.vcov[0][0]),f=xn("p ~ 1",s,{family:"binomial",weights:"w"}),m=f.coefficients[0],u=Math.sqrt(ao(f).covUnscaled[0][0]),h=xn("p ~ study",s,{family:"binomial",weights:"w"}),p=ao(h).covUnscaled,d=h.coefficients.slice(1),g=p.slice(1).map(S=>S.slice(1)),w=GM(d,HM(g,d)),N=f.deviance,b=r-1,E=n.levelMa??n.level??.95,v=re(.5+E/2),y=n.levelPredict??n.level??.95,T=Math.sqrt(a+l*l),M=it(.5+y/2,b,!0,!1),R=Po(w,r,n.level??.95),x=vp(e,t,{sm:"PLOGIT"}),A=m/u;return{TE:x.TE,seTE:x.seTE,k:r,"TE.common":m,"seTE.common":u,"lower.common":m-v*u,"upper.common":m+v*u,"statistic.common":A,"pval.common":2*oe(-Math.abs(A)),"TE.random":c,"seTE.random":l,"lower.random":c-v*l,"upper.random":c+v*l,"statistic.random":c/l,"pval.random":2*oe(-Math.abs(c/l)),"df.random":null,"seTE.predict":T,"lower.predict":c-M*T,"upper.predict":c+M*T,"df.predict":b,Q:[w,N],"df.Q":[b,b],"pval.Q":[Ze(w,b,{lowerTail:!1}),Ze(N,b,{lowerTail:!1})],tau2:a,"lower.tau2":NaN,"upper.tau2":NaN,tau:Math.sqrt(a),"lower.tau":NaN,"upper.tau":NaN,H:R.H,"lower.H":R.lH,"upper.H":R.uH,I2:R.I2,"lower.I2":R.lI2,"upper.I2":R.uI2,"w.common":new Array(r).fill(NaN),"w.random":new Array(r).fill(NaN),methodTau:"ML",methodRandomCi:n.methodRandomCi??"classic",methodTauCi:"QP"}}var GM=(e,t)=>e.reduce((n,r,o)=>n+r*t[o],0);function HM(e,t){let n=t.length,r=Array.from({length:n},()=>new Array(n).fill(0));for(let i=0;i<n;i++)for(let a=0;a<=i;a++){let c=e[i][a];for(let l=0;l<a;l++)c-=r[i][l]*r[a][l];r[i][a]=i===a?Math.sqrt(c):c/r[a][a]}let o=new Array(n);for(let i=0;i<n;i++){let a=t[i];for(let c=0;c<i;c++)a-=r[i][c]*o[c];o[i]=a/r[i][i]}let s=new Array(n);for(let i=n-1;i>=0;i--){let a=o[i];for(let c=i+1;c<n;c++)a-=r[c][i]*s[c];s[i]=a/r[i][i]}return s}var QM=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","tauCommon"],XM=["method","counts"],ZM=["e1","n1","e2","n2"],JM=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","pooled"],e2=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","pooled"],t2=["methodTau","methodRandomCi","methodTauCi","level","levelMa","levelPredict","side","maxiter"],en=e=>e.reduce((t,n)=>t+n,0),Sr=e=>e.TE.map((t,n)=>Number.isFinite(t)&&Number.isFinite(e.seTE[n])&&e.seTE[n]>0?n:-1).filter(t=>t>=0),Np=(e,t)=>{let n=t.map(o=>1/(o*o)),r=en(n.map((o,s)=>o*e[s]))/en(n);return en(n.map((o,s)=>o*(e[s]-r)**2))};function n2(e,t,n,r={}){if(z(r,QM,"metaSubgroup"),t.length!==e.TE.length)throw new RangeError("metaSubgroup: one subgroup per study");let o=new Set(Sr(e)),s=[];for(let[g,w]of t.entries())o.has(g)&&!s.includes(String(w))&&s.push(String(w));let i;if(r.tauCommon){let g=[...o].sort((b,E)=>b-E),w=g.map(b=>s.slice(1).map(E=>String(t[b])===E?1:0)),N=hn(g.map(b=>e.TE[b]),g.map(b=>e.seTE[b]));i=In(g.map(b=>e.TE[b]/N),g.map(b=>(e.seTE[b]/N)**2),{method:r.methodTau??"REML",mods:w}).tau2*N*N}let a=s.map(g=>{let w=n(t.map((E,v)=>String(E)===g&&o.has(v)?v:-1).filter(E=>E>=0));if(i===void 0)return w;let N=Sr(w),b=Kn(N.map(E=>w.TE[E]),N.map(E=>w.seTE[E]),{...r,fixedTau2:i});return{...w,"TE.random":b["TE.random"],"seTE.random":b["seTE.random"],tau2:i}}),c=a.map(g=>g["TE.common"]),l=a.map(g=>g["seTE.common"]),f=a.map(g=>g["TE.random"]),m=a.map(g=>g["seTE.random"]),u=Np(c,l),h=Np(f,m),p=s.length-1,d={tau2:e.tau2,te:e["TE.random"],se:e["seTE.random"]};return{levels:s,"TE.common.w":c,"seTE.common.w":l,"TE.random.w":f,"seTE.random.w":m,"tau2.w":a.map(g=>g.tau2),"Q.w":a.map(g=>g.Q),"k.w":a.map(g=>g.k),"Q.w.common":en(a.map(g=>g.Q)),"Q.b.common":u,"Q.b.random":h,"df.Q.b":p,"df.Q.w":en(a.map(g=>g.k-1)),"pval.Q.b.common":Ze(u,p,{lowerTail:!1}),"pval.Q.b.random":Ze(h,p,{lowerTail:!1}),tau2:d.tau2,"TE.random":d.te,"seTE.random":d.se,...i!==void 0?{"Q.w.random":en(a.map(g=>g.Q))}:{}}}function hl(e,t,n,r){let o=Xn(e,t,r?{weights:r}:{});return{est:o.coefficients[n],se:o.coefTable.stdError[n],t:o.coefTable.tValue[n],df:o.dfResidual}}function r2(e,t={}){z(t,XM,"metabias"),z(_t(t,"counts"),ZM,"metabias","counts");let n=t.method??"Egger",r=Sr(e),o=r.length;if(o<3)throw new RangeError("metabias: at least three studies");let s=hn(r.map(g=>e.TE[g]),r.map(g=>e.seTE[g])),i=r.map(g=>e.TE[g]/s),a=r.map(g=>e.seTE[g]/s);if(n==="Egger"){let g=hl(a.map(w=>[1/w]),i.map((w,N)=>w/a[N]),0);return{statistic:g.t,df:g.df,pValue:2*at(-Math.abs(g.t),g.df,!0,!1),estimate:[g.est,g.se],method:"Linear regression test of funnel plot asymmetry"}}if(n==="Begg"){let g=a.map(M=>1/(M*M)),w=en(g.map((M,R)=>M*i[R]))/en(g),N=1/en(g),b=i.map((M,R)=>(M-w)/Math.sqrt(a[R]**2-N)),E=a.map(M=>M*M),v=0;for(let M=0;M<o;M++)for(let R=M+1;R<o;R++)v+=Math.sign(b[M]-b[R])*Math.sign(E[M]-E[R]);let y=Math.sqrt(o*(o-1)*(2*o+5)/18),T=v/y;return{statistic:T,df:null,pValue:2*oe(-Math.abs(T)),estimate:[v,y],method:"Rank correlation test of funnel plot asymmetry"}}if(n==="Thompson"){let g=In(i,a.map(E=>E*E),{method:"REML",mods:a.map(E=>[E])}),w=g.beta.rows[1][0],N=g.se[1],b=w/N;return{statistic:b,df:o-2,pValue:2*at(-Math.abs(b),o-2,!0,!1),estimate:[w,N],method:"Linear regression test of funnel plot asymmetry (methods of moment)"}}let c=t.counts;if(!c)throw new RangeError(`metabias: method '${n}' needs the 2 x 2 counts`);let l=r.map(g=>c.e1[g]),f=r.map(g=>c.n1[g]-c.e1[g]),m=r.map(g=>c.e2[g]),u=r.map(g=>c.n2[g]-c.e2[g]);if(n==="Harbord"){let g=l.map((y,T)=>y+f[T]+m[T]+u[T]),w=l.map((y,T)=>y+f[T]),N=l.map((y,T)=>y+m[T]),b=l.map((y,T)=>y-w[T]*N[T]/g[T]),E=g.map((y,T)=>w[T]*(y-w[T])*N[T]*(y-N[T])/(y*y*(y-1))),v=hl(E.map(y=>[Math.sqrt(y)]),b.map((y,T)=>y/Math.sqrt(E[T])),0);return{statistic:v.t,df:v.df,pValue:2*at(-Math.abs(v.t),v.df,!0,!1),estimate:[v.est,v.se],method:"Linear regression test of funnel plot asymmetry (efficient score)"}}let h=l.map((g,w)=>g+f[w]+m[w]+u[w]),p=l.map((g,w)=>1/(1/(g+m[w])+1/(f[w]+u[w]))),d=hl(h.map(g=>[1/g]),i,1,p);return{statistic:d.t,df:d.df,pValue:2*at(-Math.abs(d.t),d.df,!0,!1),estimate:[d.est,d.se],method:"Linear regression test of funnel plot asymmetry (based on sample size)"}}function o2(e,t){let n=Sr(e),r=n.map(a=>e.TE[a]),o=n.map(a=>e.seTE[a]),s=hn(r,o),i=In(r.map(a=>a/s),o.map(a=>(a/s)**2),{method:e.methodTau,mods:n.map(a=>[...t[a]]),...e.methodRandomCi==="HK"?{test:"knha"}:{}});return ll(i,s,r,o.map(a=>a*a))}function Mp(e,t,n,r){let o={TE:[],seTE:[],lower:[],upper:[],pval:[],tau2:[],I2:[]},s={methodTau:e.methodTau,methodRandomCi:e.methodRandomCi,...r};for(let i of t){let a=Kn(i.map(l=>e.TE[l]),i.map(l=>e.seTE[l]),s),c=n==="common"?"common":"random";o.TE.push(a[`TE.${c}`]),o.seTE.push(a[`seTE.${c}`]),o.lower.push(a[`lower.${c}`]),o.upper.push(a[`upper.${c}`]),o.pval.push(a[`pval.${c}`]),o.tau2.push(i.length>1?a.tau2:NaN),o.I2.push(a.I2)}return o}function s2(e,t={}){z(t,JM,"metainf");let n=Sr(e);return Mp(e,n.map(r=>n.filter(o=>o!==r)),t.pooled??"common",t)}function a2(e,t={}){z(t,e2,"metacum");let n=Sr(e);return Mp(e,n.map((r,o)=>n.slice(0,o+1)),t.pooled??"common",t)}function i2(e){let t=Sr(e),n=[],r=[],o=hn(t.map(c=>e.TE[c]),t.map(c=>e.seTE[c])),s=e.TE.map(c=>c/o),i=e.seTE.map(c=>c/o),a=e["TE.common"]/o;for(let c of t){n.push((s[c]-a)**2/i[c]**2);let l=t.filter(u=>u!==c),f=l.map(u=>1/i[u]**2),m=en(f.map((u,h)=>u*s[l[h]]))/en(f);r.push((a-m)**2*en(f))}return{x:n,y:r}}function c2(e,t={}){z(t,t2,"trimfill");let n=Sr(e),r=hn(n.map(T=>e.TE[T]),n.map(T=>e.seTE[T])),o=n.map(T=>e.TE[T]/r),s=n.map(T=>e.seTE[T]/r),i=n.length,a=t.side;a||(a=In(o,s.map(M=>M*M),{method:"FE",mods:s.map(M=>[M])}).beta.rows[1][0]<0?"left":"right");let c=a==="right"?1:-1,l=o.map(T=>c*T),f=l.map((T,M)=>M).sort((T,M)=>l[T]-l[M]),m=f.map(T=>l[T]),u=f.map(T=>s[T]),h=T=>{let M=u.slice(0,T).map(R=>1/(R*R));return en(M.map((R,x)=>R*m[x]))/en(M)},p=0,d=-1,g=h(i),w=t.maxiter??100;for(let T=0;T<w&&p!==d;T++){d=p,g=h(i-p);let M=m.map(S=>S-g),R=l2(M.map(Math.abs)),A=(4*en(M.map((S,I)=>S>0?R[I]:0))-i*(i+1))/(2*i-1);p=Math.max(0,Math.round(A))}g=h(i-p);let N=m.slice(i-p).map(T=>2*g-T),b=u.slice(i-p),E=[...o,...N.map(T=>c*T)].map(T=>T*r),v=[...s,...b].map(T=>T*r),y=Kn(E,v,{methodTau:t.methodTau??e.methodTau,...t});return{k0:p,side:a,TE:E,seTE:v,filled:E.map((T,M)=>M>=i),meta:y}}function l2(e){let t=e.map((r,o)=>o).sort((r,o)=>e[r]-e[o]),n=new Array(e.length);for(let r=0;r<t.length;){let o=r;for(;o+1<t.length&&e[t[o+1]]===e[t[r]];)o++;for(let s=r;s<=o;s++)n[t[s]]=(r+o)/2+1;r=o+1}return n}var f2=["referenceGroup","level","levelPredict","tau2"],m2=["random","smallValues"],u2=["random"],h2=["treat","studlab","n","event","mean","sd"],p2=["sm","incr"],d2=["inactive","sep"],g2=["inactive","sep"],w2=["random","digits","bracket","separator"],Es=(e,t)=>Array.from({length:e},()=>new Array(t).fill(0)),$o=(e,t)=>e.map(n=>t[0].map((r,o)=>n.reduce((s,i,a)=>s+i*t[a][o],0))),yp=e=>e[0].map((t,n)=>e.map(r=>r[n])),yt=e=>e.reduce((t,n)=>t+n,0);function pl(e){let t=e.length,n=e.map((a,c)=>a.map((l,f)=>(l+e[f][c])/2)),{values:r,vectors:o}=Wo(n),s=Math.max(...r.map(Math.abs)),i=Es(t,t);return r.forEach((a,c)=>{if(Math.abs(a)>1e-10*s)for(let l=0;l<t;l++)for(let f=0;f<t;f++)i[l][f]+=o[l][c]*o[f][c]/a}),i}function Ep(e,t,n,r){let o=new Array(e.length).fill(0);for(let s of[...new Set(r)]){let i=r.map((u,h)=>u===s?h:-1).filter(u=>u>=0);if(i.length===1){o[i[0]]=1/e[i[0]];continue}let a=[...new Set(i.flatMap(u=>[t[u],n[u]]))],c=a.length,l=Es(c,c);for(let u of i){let h=a.indexOf(t[u]),p=a.indexOf(n[u]);l[h][p]=e[u],l[p][h]=e[u]}let f=Array.from({length:c},(u,h)=>Array.from({length:c},(p,d)=>(h===d?1:0)-1/c)),m=pl($o($o(f,l),f).map(u=>u.map(h=>-h/2)));for(let u of i)o[u]=-m[a.indexOf(t[u])][a.indexOf(n[u])]}return o}function $a(e,t,n,r,o,s){let i=e.length,a=s.length,c=Es(i,a);for(let d=0;d<i;d++)c[d][s.indexOf(n[d])]=1,c[d][s.indexOf(r[d])]=-1;let l=Ep(t,n,r,o),f=Es(a,a);for(let d=0;d<i;d++)for(let g=0;g<a;g++)for(let w=0;w<a;w++)f[g][w]+=c[d][g]*l[d]*c[d][w];let m=pl(f),u=Array.from({length:a},(d,g)=>yt(c.map((w,N)=>w[g]*l[N]*e[N]))),h=m.map(d=>yt(d.map((g,w)=>g*u[w]))),p=yt(e.map((d,g)=>l[g]*(d-yt(c[g].map((w,N)=>w*h[N])))**2));return{theta:h,Lp:m,Q:p,w:l,B:c}}var Nn=(e,t)=>({rows:[...e],cols:[...e],values:e.map((n,r)=>e.map((o,s)=>t(r,s)))});function Tp(e,t,n,r,o,s={}){z(s,f2,"netmeta");let i=e.length;if(![t,n,r,o].every(k=>k.length===i))throw new RangeError("netmeta: the inputs must have the same length");for(let k=0;k<i;k++){if(!(Number.isFinite(e[k])&&t[k]>0))throw new RangeError("netmeta: TE must be finite and seTE positive");if(n[k]===r[k])throw new RangeError("netmeta: treat1 and treat2 must differ")}let a=[...new Set([...n,...r])].sort(),c=a.length;if(Rp(n,r,o)["n.subnets"]>1)throw new RangeError("netmeta: the network is not connected (see netconnection)");let l=hn(e,t);if(l!==1){let k=Tp(e.map(q=>q/l),t.map(q=>q/l),n,r,o,{...s,...s.tau2!==void 0?{tau2:s.tau2/l/l}:{}}),L=q=>({...q,values:q.values.map($=>$.map(V=>V*l))});return{...k,TE:[...e],seTE:[...t],"TE.common":L(k["TE.common"]),"seTE.common":L(k["seTE.common"]),"lower.common":L(k["lower.common"]),"upper.common":L(k["upper.common"]),"TE.random":L(k["TE.random"]),"seTE.random":L(k["seTE.random"]),"lower.random":L(k["lower.random"]),"upper.random":L(k["upper.random"]),"lower.predict":L(k["lower.predict"]),"upper.predict":L(k["upper.predict"]),"TE.direct.common":L(k["TE.direct.common"]),"seTE.direct.common":L(k["seTE.direct.common"]),"TE.direct.random":L(k["TE.direct.random"]),"seTE.direct.random":L(k["seTE.direct.random"]),tau2:k.tau2*l*l,"w.common":k["w.common"].map(q=>q/l/l),"w.random":k["w.random"].map(q=>q/l/l)}}let f=t.map(k=>k*k),m=[...new Set(o)],u=m.length,h=$a(e,f,n,r,o,a),p=m.map(k=>new Set(o.flatMap((L,q)=>L===k?[n[q],r[q]]:[])).size),d=yt(p.map(k=>k-1))-(c-1),g=s.tau2??0;if(s.tau2===void 0&&d>0){let k=h.B,L=h.w,q=$o(k,h.Lp),V=$o(q,yp(k)).map(X=>X.map((Y,se)=>Y*L[se])).map((X,Y)=>X.map((se,H)=>(Y===H?1:0)-se)),P=Es(i,i);for(let X of m){let Y=o.map((se,H)=>se===X?H:-1).filter(se=>se>=0);for(let se of Y)for(let H of Y)P[se][H]=.5*yt(k[se].map((j,te)=>j*k[H][te]))}let Q=$o($o(V,P),yp(V)),K=yt(Q.map((X,Y)=>L[Y]*X[Y]));g=Math.max(0,(h.Q-d)/K)}let w=$a(e,f.map(k=>k+g),n,r,o,a),N=s.level??.95,b=re(.5+N/2),E=s.levelPredict??N,v=k=>(L,q)=>k.theta[L]-k.theta[q],y=k=>(L,q)=>L===q?0:Math.sqrt(Math.max(0,k.Lp[L][L]+k.Lp[q][q]-2*k.Lp[L][q])),T=d>0?it(.5+E/2,d,!0,!1):NaN,M=k=>{let L=Nn(a,()=>NaN),q=Nn(a,()=>NaN);for(let $=0;$<c;$++)for(let V=0;V<c;V++){if($===V)continue;let P=e.map((X,Y)=>n[Y]===a[$]&&r[Y]===a[V]?1:n[Y]===a[V]&&r[Y]===a[$]?-1:0),Q=P.map((X,Y)=>X===0?0:1/(f[Y]+k)),K=yt(Q);K>0&&(L.values[$][V]=yt(Q.map((X,Y)=>X*P[Y]*e[Y]))/K,q.values[$][V]=Math.sqrt(1/K))}return{te:L,se:q}},R=M(0),x=M(g),A=k=>[...new Set(o.flatMap((L,q)=>L===k?[n[q],r[q]]:[]))].sort().join(":"),S=0,I=0;for(let k of[...new Set(m.map(A))]){let L=o.map((P,Q)=>A(P)===k?Q:-1).filter(P=>P>=0),q=[...new Set(L.flatMap(P=>[n[P],r[P]]))].sort(),$=$a(L.map(P=>e[P]),L.map(P=>f[P]),L.map(P=>n[P]),L.map(P=>r[P]),L.map(P=>o[P]),q),V=new Set(L.map(P=>o[P])).size;S+=$.Q,I+=(V-1)*(q.length-1)}let C=Po(h.Q,d+1,N),_=v(h),F=y(h),O=v(w),D=y(w);return{trts:a,studlab:[...o],treat1:[...n],treat2:[...r],TE:[...e],seTE:[...t],k:u,n:c,m:i,"TE.common":Nn(a,_),"seTE.common":Nn(a,F),"lower.common":Nn(a,(k,L)=>_(k,L)-b*F(k,L)),"upper.common":Nn(a,(k,L)=>_(k,L)+b*F(k,L)),"TE.random":Nn(a,O),"seTE.random":Nn(a,D),"lower.random":Nn(a,(k,L)=>O(k,L)-b*D(k,L)),"upper.random":Nn(a,(k,L)=>O(k,L)+b*D(k,L)),"lower.predict":Nn(a,(k,L)=>k===L?0:O(k,L)-T*Math.sqrt(D(k,L)**2+g)),"upper.predict":Nn(a,(k,L)=>k===L?0:O(k,L)+T*Math.sqrt(D(k,L)**2+g)),"TE.direct.common":R.te,"seTE.direct.common":R.se,"TE.direct.random":x.te,"seTE.direct.random":x.se,Q:h.Q,"df.Q":d,"pval.Q":d>0?Ze(h.Q,d,{lowerTail:!1}):NaN,tau2:s.tau2===void 0&&d<=0?NaN:g,I2:C.I2,"lower.I2":C.lI2,"upper.I2":C.uI2,"Q.heterogeneity":S,"df.Q.heterogeneity":I,"Q.inconsistency":h.Q-S,"df.Q.inconsistency":d-I,"w.common":h.w,"w.random":w.w,referenceGroup:s.referenceGroup??null,level:N}}function b2(e,t={}){z(t,m2,"netrank");let n=t.random===!1?e["TE.common"]:e["TE.random"],r=t.random===!1?e["seTE.common"]:e["seTE.random"],o=(t.smallValues??"desirable")==="desirable"?1:-1,s=e.trts.length,i={};return e.trts.forEach((a,c)=>{let l=0;for(let f=0;f<s;f++)f!==c&&(l+=oe(o*n.values[f][c]/r.values[c][f]));i[a]=l/(s-1)}),i}function v2(e){let t=e.trts.length,n=re(.5+e.level/2),r=[],o=e.referenceGroup===null?-1:e.trts.indexOf(e.referenceGroup);for(let h=0;h<t;h++)for(let p=h+1;p<t;p++)r.push(h===o?[p,h]:[h,p]);r.sort((h,p)=>(h[1]===o?0:1)-(p[1]===o?0:1));let s=(h,p,d)=>({comparison:d,TE:h,seTE:p,lower:h.map((g,w)=>g-n*p[w]),upper:h.map((g,w)=>g+n*p[w]),z:h.map((g,w)=>g/p[w]),p:h.map((g,w)=>2*oe(-Math.abs(g/p[w])))}),i=r.map(([h,p])=>`${e.trts[h]}:${e.trts[p]}`),a=hn(e.TE,e.seTE),c=h=>a===1?h:h.map(p=>p.map(d=>d/a)),l=h=>a===1?h:{...h,TE:h.TE.map(p=>p*a),seTE:h.seTE.map(p=>p*a),lower:h.lower.map(p=>p*a),upper:h.upper.map(p=>p*a)},f=h=>{let p=c(e[`TE.${h}`].values),d=c(e[`seTE.${h}`].values),g=c(e[`TE.direct.${h}`].values),w=c(e[`seTE.direct.${h}`].values),N=[],b=[],E=[],v=[],y=[],T=[],M=[];for(let[R,x]of r){let A=d[R][x]**2,S=w[R][x]**2;if(N.push(g[R][x]),b.push(w[R][x]),!Number.isFinite(S)){E.push(p[R][x]),v.push(d[R][x]),y.push(NaN),T.push(NaN),M.push(0);continue}let I=A/S;if(M.push(I),I>=1-1e-12){E.push(NaN),v.push(NaN),y.push(NaN),T.push(NaN);continue}let C=1/(1/A-1/S),_=C*(p[R][x]/A-g[R][x]/S);E.push(_),v.push(Math.sqrt(C)),y.push(g[R][x]-_),T.push(Math.sqrt(S+C))}return{direct:l(s(N,b,i)),indirect:l(s(E,v,i)),compare:l(s(y,T,i)),prop:M}},m=f("random"),u=f("common");return{"direct.random":m.direct,"indirect.random":m.indirect,"compare.random":m.compare,"direct.common":u.direct,"indirect.common":u.indirect,"compare.common":u.compare,"prop.random":m.prop,"prop.common":u.prop}}function Rp(e,t,n){let r=[...new Set([...e,...t])].sort(),o=r.length,s=Array.from({length:o},(c,l)=>Array.from({length:o},(f,m)=>l===m?0:1/0));e.forEach((c,l)=>{let f=r.indexOf(c),m=r.indexOf(t[l]);s[f][m]=1,s[m][f]=1});for(let c=0;c<o;c++)for(let l=0;l<o;l++)for(let f=0;f<o;f++)s[l][c]+s[c][f]<s[l][f]&&(s[l][f]=s[l][c]+s[c][f]);let i={},a=0;for(let c=0;c<o;c++)if(i[r[c]]===void 0){a++;for(let l=0;l<o;l++)Number.isFinite(s[c][l])&&(i[r[l]]=a)}return{"n.subnets":a,k:new Set(n??e.map((c,l)=>String(l))).size,n:o,m:e.length,D:{rows:r,cols:r,values:s},subnet:i}}var N2=e=>{let t=new Map;for(let n of new Set(e.studlab))t.set(n,[...new Set(e.studlab.flatMap((r,o)=>r===n?[e.treat1[o],e.treat2[o]]:[]))].sort().join(":"));return t};function M2(e){let t=hn(e.TE,e.seTE),n=e.TE.map(m=>m/t),r=N2(e),o=e.seTE.map(m=>m/t*(m/t)),s=[...new Set(r.values())].sort((m,u)=>m.split(":").length-u.split(":").length||(m<u?-1:m>u?1:0)),i=[],a=[];for(let m of s){let u=e.studlab.map((d,g)=>r.get(d)===m?g:-1).filter(d=>d>=0),h=m.split(":"),p=$a(u.map(d=>n[d]),u.map(d=>o[d]),u.map(d=>e.treat1[d]),u.map(d=>e.treat2[d]),u.map(d=>e.studlab[d]),h);i.push(p.Q),a.push((new Set(u.map(d=>e.studlab[d])).size-1)*(h.length-1))}let c=(m,u)=>u>0?Ze(m,u,{lowerTail:!1}):NaN,l=yt(i),f=yt(a);return{"Q.decomp":{Q:[e.Q,l,e.Q-l],df:[e["df.Q"],f,e["df.Q"]-f],pval:[c(e.Q,e["df.Q"]),c(l,f),c(e.Q-l,e["df.Q"]-f)]},"Q.het.design":{design:s,Q:i,df:a,pval:i.map((m,u)=>c(m,a[u]))}}}function y2(e,t={}){z(t,u2,"netmeasures");let n=t.random===!1?"common":"random",r=e.trts.length,o=e[`seTE.direct.${n}`].values,s={},i=hn(e.TE,e.seTE);for(let a=0;a<r;a++)for(let c=a+1;c<r;c++){let l=(e[`seTE.${n}`].values[a][c]/i)**2,f=(o[a][c]/i)**2;s[`${e.trts[a]}:${e.trts[c]}`]=Number.isFinite(f)?l/f:0}return{proportion:s}}function E2(e,t={}){z(e,h2,"pairwise"),z(t,p2,"pairwise");let n=t.sm??(e.event?"OR":"MD"),r=t.incr??.5,o={TE:[],seTE:[],treat1:[],treat2:[],studlab:[]};for(let s of[...new Set(e.studlab)]){let i=e.studlab.map((c,l)=>c===s?l:-1).filter(c=>c>=0),a=e.event?i.some(c=>e.event[c]===0||e.event[c]===e.n[c]):!1;for(let c=0;c<i.length;c++)for(let l=c+1;l<i.length;l++){let f=i[c],m=i[l],u,h;if(n==="MD")u=e.mean[f]-e.mean[m],h=Math.sqrt(e.sd[f]**2/e.n[f]+e.sd[m]**2/e.n[m]);else{let p=a?r:0,d=e.event[f]+p,g=e.event[m]+p;if(n==="OR"){let w=e.n[f]+2*p,N=e.n[m]+2*p;u=Math.log(d/(w-d))-Math.log(g/(N-g)),h=Math.sqrt(1/d+1/(w-d)+1/g+1/(N-g))}else if(n==="RR"){let w=e.n[f]+p,N=e.n[m]+p;u=Math.log(d/w)-Math.log(g/N),h=Math.sqrt(1/d-1/w+1/g-1/N)}else{let w=e.n[f]+2*p,N=e.n[m]+2*p;u=e.event[f]/e.n[f]-e.event[m]/e.n[m],h=Math.sqrt(d*(w-d)/w**3+g*(N-g)/N**3)}}o.TE.push(u),o.seTE.push(h),o.treat1.push(e.treat[f]),o.treat2.push(e.treat[m]),o.studlab.push(s)}}return o}function dl(e,t,n,r,o,s={}){z(s,d2,"discomb");let i=hn(e,t);if(i!==1){let T=dl(e.map(R=>R/i),t.map(R=>R/i),n,r,o,s),M=R=>R.map(x=>x*i);return{...T,"Comp.common":M(T["Comp.common"]),"seComp.common":M(T["seComp.common"]),"Comp.random":M(T["Comp.random"]),"seComp.random":M(T["seComp.random"]),"Comb.common":M(T["Comb.common"]),"seComb.common":M(T["seComb.common"]),"Comb.random":M(T["Comb.random"]),"seComb.random":M(T["seComb.random"]),tau2:T.tau2*i*i}}let a=s.sep??"+",c=[...new Set([...n,...r])].sort(),l=[...new Set(c.flatMap(T=>T.split(a).map(M=>M.trim())))].filter(T=>T!==s.inactive).sort(),f=c.map(T=>{let M=T.split(a).map(R=>R.trim());return l.map(R=>M.includes(R)?1:0)}),m=e.length,u=e.map((T,M)=>l.map((R,x)=>f[c.indexOf(n[M])][x]-f[c.indexOf(r[M])][x])),h=t.map(T=>T*T),p=T=>{let M=Ep(h.map(C=>C+T),n,r,o),R=l.map((C,_)=>l.map((F,O)=>yt(u.map((D,k)=>D[_]*M[k]*D[O])))),x=pl(R),A=l.map((C,_)=>yt(u.map((F,O)=>F[_]*M[O]*e[O]))),S=x.map(C=>yt(C.map((_,F)=>_*A[F]))),I=yt(e.map((C,_)=>M[_]*(C-yt(u[_].map((F,O)=>F*S[O])))**2));return{beta:S,se:x.map((C,_)=>Math.sqrt(C[_])),Q:I,A:x,w:M}},d=p(0),g=R2(u),w=m-g,N=0;if(w>0){let M=u.map(x=>u.map((A,S)=>yt(x.map((I,C)=>I*yt(d.A[C].map((_,F)=>_*A[F]))))*d.w[S])).map((x,A)=>x.map((S,I)=>(A===I?1:0)-S)),R=yt(M.map((x,A)=>d.w[A]*yt(x.map(S=>S*S))));N=Math.max(0,(d.Q-w)/R)}let b=p(N),E=T=>({e:f.map(M=>yt(M.map((R,x)=>R*T.beta[x]))),s:f.map(M=>Math.sqrt(Math.max(0,yt(M.map((R,x)=>R*yt(T.A[x].map((A,S)=>A*M[S])))))))}),v=E(d),y=E(b);return{comps:l,"Comp.common":d.beta,"seComp.common":d.se,"Comp.random":b.beta,"seComp.random":b.se,trts:c,"Comb.common":v.e,"seComb.common":v.s,"Comb.random":y.e,"seComb.random":y.s,"Q.additive":d.Q,"df.Q.additive":w,tau2:N}}function T2(e,t={}){return z(t,g2,"netcomb"),dl(e.TE,e.seTE,e.treat1,e.treat2,e.studlab,t)}function R2(e){let t=e[0].length,n=Array.from({length:t},(s,i)=>Array.from({length:t},(a,c)=>yt(e.map(l=>l[i]*l[c])))),{values:r}=Wo(n),o=Math.max(...r.map(Math.abs));return r.filter(s=>Math.abs(s)>1e-10*o).length}function x2(e,t={}){z(t,w2,"netleague");let n=t.random===!1?"common":"random",r=t.digits??2,o=t.separator??" to ",[s,i]=(t.bracket??"(")==="("?["(",")"]:["[","]"],a=re(.5+e.level/2),c=e.trts.length,l=e[`TE.${n}`].values,f=e[`TE.direct.${n}`].values,m=e[`seTE.direct.${n}`].values,u=e[`lower.${n}`].values,h=e[`upper.${n}`].values,p=e.trts.map((b,E)=>e.trts.map((v,y)=>E===y?{est:NaN,lo:NaN,up:NaN}:E>y?{est:l[y][E],lo:u[y][E],up:h[y][E]}:{est:f[E][y],lo:f[E][y]-a*m[E][y],up:f[E][y]+a*m[E][y]})),d=b=>b.toFixed(r),g=p.flat().flatMap(b=>Number.isFinite(b.est)?[d(b.lo),d(b.up)]:[]),w=Math.max(0,...g.map(b=>b.length)),N=b=>b.padStart(w," ");return p.map((b,E)=>b.map((v,y)=>E===y?e.trts[E]:Number.isFinite(v.est)?`${d(v.est)} ${s}${N(d(v.lo))}${o}${N(d(v.up))}${i}`:"."))}function S2(e){let t=e.length,n=(r,o)=>r!==o&&e[r].every((s,i)=>s>=e[o][i])&&e[r].some((s,i)=>s>e[o][i]);return Array.from({length:t},(r,o)=>Array.from({length:t},(s,i)=>{if(!n(o,i))return 0;for(let a=0;a<t;a++)if(a!==o&&a!==i&&n(o,a)&&n(a,i))return 0;return 1}))}export{Mc as Anova,fN as ICC,Iw as TukeyHSD,Ob as adTest,jw as add1,Di as anova,Jm as ansariTest,kw as aov,lb as aovEz,Vi as aovSummary,cv as auc,Rm as bartlettTest,i2 as baujat,Si as binomCI,m0 as binomDiffCI,E0 as binomDiffEquivalenceTest,h0 as binomRatioCI,Vd as binomTest,Bv as blandAltman,ag as boschlooTest,dw as brunnerMunzelTest,ed as chisqTest,hv as ciAuc,gb as clogit,pg as cochranQTest,N1 as coefci,b1 as coeftest,Gu as cohenES,L0 as cohensD,Ip as confint,fl as confintRma,fv as coords,qp as corTest,Nb as coxZph,ho as coxph,Bb as cronbachAlpha,PN as crr,_N as cuminc,Fb as cvmTest,M2 as decompDesign,Yv as diagnosticTests,dl as discomb,Ww as drop1,Vg as dscfAllPairsTest,Yg as dunnTest,Pg as dunnettTest,JN as epiSsequb,eM as epiSsequc,tM as epiSsninfb,nM as epiSsninfc,rM as epiSssupb,oM as epiSssupc,Y0 as epsilonSquared,Y1 as esH,W1 as esW1,j1 as esW2,NM as escalc,z0 as etaSquared,Yw as extractAIC,pN as factanal,ts as fisherTest,Am as flignerTest,Ui as friedmanTest,Up as gTest,zg as gamesHowellTest,wp as genqInterval,jg as glhtDunnett,xn as glm,T1 as glmAnova,Lw as glmConfint,Fp as glmCooksDistance,Lp as glmHatvalues,Pp as glmPredict,Cp as glmResiduals,Op as glmRstandard,ao as glmSummary,Xp as goodmanKruskalGamma,kM as grubbsTest,O0 as hedgesG,Po as heterogeneityIntervals,jb as icc,Cb as jonckheereTest,Yb as kappa2,Wb as kappamFleiss,ha as kruskalTest,cu as ksTest,Og as kwAllPairsDunnTest,Fg as kwManyOneDunnTest,Jp as lblTest,Eb as leveneTest,Pb as lillieTest,Xn as lm,Zg as mantelhaenTest,Dg as manyOneUTest,q1 as mauchlyTest,lg as mcnemarTest,oN as mcreg,n2 as metaSubgroup,r2 as metabias,CM as metabin,KM as metacont,WM as metacor,a2 as metacum,IM as metagen,YM as metainc,s2 as metainf,jM as metamean,vp as metaprop,UM as metapropGlmm,BM as metarate,o2 as metareg,so as modelMatrix,km as moodTest,T2 as netcomb,Rp as netconnection,x2 as netleague,y2 as netmeasures,Tp as netmeta,S2 as netposet,b2 as netrank,v2 as netsplit,fM as numDEpi,b0 as oddsRatio,MN as omega,K0 as omegaSquared,zm as onewayTest,lo as pAdjust,gp as pWeightedChisq,hg as pageTest,E2 as pairwise,Rw as pairwisePropTest,Ew as pairwiseTTest,Tw as pairwiseWilcoxTest,Gb as partialR,Vb as pearsonTest,RM as pgrubbs,uM as ph2simon,jd as poissonTest,xN as polychor,AN as polyserial,kp as powerAnovaTest,aM as powerCT,cM as powerEpi,sM as powerPaired2x2,Ap as powerPropTest,Sp as powerTTest,_p as predict,DN as predictCrr,MM as predictRma,Ri as propTest,xb as propTrendTest,tb as pwr2p2nTest,Z1 as pwr2pTest,ob as pwrAnovaTest,rb as pwrChisqTest,sb as pwrF2Test,eb as pwrNormTest,J1 as pwrPTest,nb as pwrRTest,X1 as pwrT2nTest,Q1 as pwrTTest,AM as qgrubbs,Sm as quadeTest,yM as regtest,In as rma,hb as rmst2,av as roc,dv as rocTest,Dv as seSpPPVNPV,xp as setWarningHandler,Db as sfTest,Ug as shapiroTest,iM as ssizeCT,lM as ssizeEpi,Uw as step,Gw as stepAIC,Qp as stuartMaxwellTest,c1 as survdiff,Nr as survfit,vc as survfitCoxph,zv as survivalROC,Vp as survreg,$p as survregPredict,zp as survregResiduals,Xw as svyglm,X0 as tTOST,Sn as tTest,$g as tamhaneT2Test,qv as timeROC,Vv as timeROCAucCurve,$v as timeROCAucDiff,kh as timeROCCompare,Iv as timeROCConfint,LN as timepoints,M0 as tost,c2 as trimfill,Z0 as tsumTOST,Kg as tukeyTest,Tm as varTest,Dp as vcovCL,ei as vcovHC,ns as wilcoxTest,_w as zTest};
|