@numero-uno-jpn/rstats 1.5.0 → 1.7.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 +34 -0
- package/README.ja.md +17 -10
- package/README.md +18 -10
- package/dist/chunks/chunk-56AMJQ27.js +2 -0
- package/dist/chunks/{chunk-TP4PRPVQ.js → chunk-76FLTZVF.js} +2 -2
- package/dist/chunks/chunk-AIEPW6DX.js +2 -0
- package/dist/chunks/chunk-B6KJ4RWG.js +2 -0
- package/dist/chunks/{chunk-73QCE2CI.js → chunk-B7XZMIQO.js} +2 -2
- package/dist/chunks/chunk-BJMPSDJ7.js +2 -0
- package/dist/chunks/chunk-CB7STL5Z.js +2 -0
- package/dist/chunks/{chunk-MYRCEQ2R.js → chunk-CUT3IHMY.js} +1 -1
- package/dist/chunks/{chunk-MJV5G7FG.js → chunk-CZUOG2U5.js} +2 -2
- package/dist/chunks/{chunk-LVB22NKV.js → chunk-FQRNZEH2.js} +2 -2
- package/dist/chunks/{chunk-2IYY74LX.js → chunk-HORF36FD.js} +2 -2
- package/dist/chunks/{chunk-GDMNF4P3.js → chunk-HSJ4OT3H.js} +1 -1
- package/dist/chunks/{chunk-E5M5RAVB.js → chunk-J7ZVV4DN.js} +2 -2
- package/dist/chunks/chunk-JXEXOD7K.js +2 -0
- package/dist/chunks/chunk-KSH43GX4.js +2 -0
- package/dist/chunks/chunk-LMRCFKVK.js +2 -0
- package/dist/chunks/{chunk-CX4XK7WC.js → chunk-MOJ2KIQE.js} +2 -2
- package/dist/chunks/chunk-PCTRVBEJ.js +3 -0
- package/dist/chunks/{chunk-F2MURX2Y.js → chunk-PLQQ4VFN.js} +2 -2
- package/dist/chunks/{chunk-L7PDJNJ3.js → chunk-QWDK3ZC2.js} +2 -2
- package/dist/chunks/{chunk-F7SQG6BV.js → chunk-R5BMFVIH.js} +2 -2
- package/dist/chunks/chunk-R7F7ELS7.js +3 -0
- package/dist/chunks/{chunk-2MYUAEYE.js → chunk-RIP2RKAW.js} +2 -2
- package/dist/chunks/chunk-RX6MZO2Y.js +2 -0
- package/dist/chunks/{chunk-VAWD57ZB.js → chunk-SDT3YHAG.js} +2 -2
- package/dist/chunks/{chunk-FCF6FBJP.js → chunk-THSV22QF.js} +2 -2
- package/dist/chunks/{chunk-AIXJAVDS.js → chunk-TY3IFCLI.js} +2 -2
- package/dist/chunks/{chunk-BJOZZ5J6.js → chunk-V2Y2W7LE.js} +2 -2
- package/dist/chunks/{chunk-UJM7AJII.js → chunk-VALULDEX.js} +1 -1
- package/dist/chunks/chunk-VQOU7U7R.js +2 -0
- package/dist/chunks/{chunk-DYCWYHYV.js → chunk-YKNRDPIB.js} +1 -1
- package/dist/chunks/chunk-YVJ4LDDE.js +2 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/mixed.d.ts +2 -0
- package/dist/entries/mixed.js +2 -0
- package/dist/entries/regression.d.ts +3 -0
- package/dist/entries/regression.js +6 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +5 -5
- package/dist/models/mixedConfint.d.ts +42 -0
- package/dist/models/mixedConfint.js +573 -0
- package/dist/models/mixedSimulate.d.ts +36 -0
- package/dist/models/mixedSimulate.js +232 -0
- package/dist/models/nls.d.ts +286 -0
- package/dist/models/nls.js +1007 -0
- package/dist/models/nlsFormula.d.ts +37 -0
- package/dist/models/nlsFormula.js +295 -0
- package/dist/models/polr.d.ts +341 -0
- package/dist/models/polr.js +1028 -0
- package/dist/models/rlm.d.ts +282 -0
- package/dist/models/rlm.js +766 -0
- package/dist/numerics.js +2 -2
- package/dist/regression.js +3 -0
- package/dist/rstats.js +1 -1
- package/dist/special.js +2 -2
- package/dist/tests.js +14 -14
- package/llms.txt +8 -2
- package/package.json +6 -1
- package/dist/chunks/chunk-3BEE6BHN.js +0 -2
- package/dist/chunks/chunk-FMLFYM7N.js +0 -2
- package/dist/chunks/chunk-JBJTXMRJ.js +0 -2
- package/dist/chunks/chunk-MXOGMN4P.js +0 -2
- package/dist/chunks/chunk-Q32WODL4.js +0 -2
- package/dist/chunks/chunk-RZBX6VYO.js +0 -3
- package/dist/chunks/chunk-WBEYJ5W5.js +0 -3
- package/dist/chunks/chunk-YK2EJU6T.js +0 -2
package/dist/tests.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
/*! rStats.js 1.
|
|
2
|
-
import{B as As,C as Ss,D as Ts,b as _t,c as Fn,d as Nn,f as cn,k as jr,l as xl,p as Rl,q as Kr,r as Al,v as Rs,w as Sl,x as ko,z as Ll}from"./chunks/chunk-JBJTXMRJ.js";import{a as Ha,e as Ya}from"./chunks/chunk-VAWD57ZB.js";import{a as qs,b as er,c as Lo,d as Pl,e as Dl,f as Ua,g as Ze,h as fn,i as Mn,j as Lt,k as Wl,l as Np,n as Fs,o as Co,p as Ps}from"./chunks/chunk-F2MURX2Y.js";import"./chunks/chunk-E5M5RAVB.js";import"./chunks/chunk-TP4PRPVQ.js";import{a as Vl,c as $l,d as zl,e as yr,g as Oo}from"./chunks/chunk-RZBX6VYO.js";import{b as Ka}from"./chunks/chunk-GDMNF4P3.js";import{b as Tl,c as kl,d as ql,e as Il,f as _l,i as qo}from"./chunks/chunk-FMLFYM7N.js";import{a as vt,b as Ur,c as Br}from"./chunks/chunk-FCF6FBJP.js";import{a as Ml,d as xs}from"./chunks/chunk-MJV5G7FG.js";import{a as _s,b as Ls,c as Cs,d as zn,e as Gr,f as Xr,g as Hl,h as Yl}from"./chunks/chunk-WBEYJ5W5.js";import{A as Pn,g as bs,h as vs,j as ys,k as Wr,p as dt,q as pn,s as Tn,t as Yn,v as Gn,z as dn}from"./chunks/chunk-YK2EJU6T.js";import{C as Ul,E as Bl,H as jt,I as Gl,c as jl,h as Kl,j as At,k as Ba,l as Is,m as Hr,n as Yr,t as Os,y as nt}from"./chunks/chunk-MXOGMN4P.js";import{a as Jn,b as Ol,g as ks}from"./chunks/chunk-BJOZZ5J6.js";import{A as El,B as an,C as Wt,i as yt,l as ws,x as ja,y as ft,z as bt}from"./chunks/chunk-F7SQG6BV.js";import{a as wn,c as Fl}from"./chunks/chunk-AIXJAVDS.js";import{a as yp,c as X,d as rt,e as mt,f as Io}from"./chunks/chunk-3BEE6BHN.js";import{a as _o,b as $n,c as Cl,d as kt,e as ut}from"./chunks/chunk-MYRCEQ2R.js";import{a as gt,b as tn,c as ln}from"./chunks/chunk-UJM7AJII.js";import"./chunks/chunk-73QCE2CI.js";import{b as dl,c as gl}from"./chunks/chunk-2MYUAEYE.js";import{A as ul,B as hl,D as pl,M as wl,q as Gt,r as se,s as le}from"./chunks/chunk-CX4XK7WC.js";import{A as bl,C as vl,D as Pt,E as Ns,F as yl,G as Ms,H as Nl,I as De,J as Nt,K as yn,L as Dt,M as Zn,N as Vt,O as Es,P as Se,Q as Wa,b as Ft,c as en,d as he,e as fl,f as Pe,g as _e,h as Tt,i as at,j as lt,k as ml,l as K,m as sn,p as To,q as vr,r as Dn,s as gn,t as Vn,u as D,v as Le,w as Xn,x as it,y as st,z as Qn}from"./chunks/chunk-DYCWYHYV.js";var Xl=["alternative","mu","paired","varEqual","confLevel"];function En(e,t,n={}){if(typeof e!="string"&&vt(t)&&K(t,Xl,"tTest"),K(n,Xl,"tTest"),typeof e=="string")return Mp(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.varEqual??!1,a=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(fn(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[T,L]=Zn(Array.from(e),Array.from(r));({d:l,k:m}=Ns(T,L))}else{let T=Se(r===void 0?[Dt(Array.from(e))]:[Dt(Array.from(e)),Dt(Array.from(r))]);m=T.k,l=T.groups[0],f=T.groups[1]}let u=D(s,-m),h,d,w,g,b,N,v,M=l.length,p=f!==void 0&&c;if(M<1||!p&&M<2)throw new Error("not enough 'x' observations");let y=it(l),x=M>1?st(l):NaN;if(f===void 0){if(d=M-1,w=Math.sqrt(x/M),w===0||w<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/w,N=i?"Paired t-test":"One Sample t-test",g=y,b=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let T=f.length;if(T<1||!c&&T<2)throw new Error("not enough 'y' observations");if(c&&M+T<3)throw new Error("not enough observations");let L=it(f),C=T>1?st(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,g=[y,L],b=["mean of x","mean of y"],v="difference in means",c){d=M+T-2;let q=0;M>1&&(q+=(M-1)*x),T>1&&(q+=(T-1)*C),q/=d,w=Math.sqrt(q*(1/M+1/T))}else{let q=x/M,O=C/T;w=Math.sqrt(Math.sqrt(q)**2+Math.sqrt(O)**2),d=Ms([q,O],[M-1,T-1])}if(w===0||w<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(L)))throw new Error("data are essentially constant");h=(y-L-u)/w}let E=typeof g=="number"?g:g[0]-g[1],R=(T,L)=>{let C=u+(h+L*T)*w;return Number.isFinite(C)&&Math.max(Math.abs(u),Math.abs(h*w))<=16*(Math.abs(E)+T*w)?C:E+L*T*w},A,S;if(o==="less")A=ft(h,d,!0,!1),S=[-1/0,R(bt(a,d,!0,!1),1)];else if(o==="greater")A=ft(h,d,!1,!1),S=[R(bt(a,d,!0,!1),-1),1/0];else{A=2*ft(-Math.abs(h),d,!0,!1);let T=1-a,L=T/2<1e-8?bt(T/2,d,!1,!1):bt(1-T/2,d,!0,!1);S=[R(L,-1),R(L,1)]}return S=[D(S[0],m),D(S[1],m)],g=typeof g=="number"?D(g,m):g.map(T=>D(T,m)),w=D(w,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:d,parameterName:"df",pValue:A,confInt:S,confLevel:a,estimate:g,estimateNames:b,nullValue:s,nullValueName:v,stdErr:w,stdError:w,warnings:[]}}function Mp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=qs(e,t,!0);if(r.kind==="one")return En(r.x,n);if(r.kind==="paired")return En(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...En(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${s}`],nullValueName:`difference in means between group ${o} and group ${s}`}}var Ep=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),xp=["n","p1","p2","sigLevel","power","alternative","strict","tol"],Rp=["groups","n","betweenVar","withinVar","sigLevel","power"],Zl=Number.EPSILON**.25;function Te(e,t){if(e===void 0)return t[0];let n=t.find(o=>o===e);if(n!==void 0)return n;let r=t.filter(o=>o.startsWith(e));if(e.length===0||r.length!==1)throw new RangeError(`'arg' should be one of ${t.map(o=>`\u201C${o}\u201D`).join(", ")}`);return r[0]}var Ne=e=>e==null;function mn(e,t,n){if(!Ne(e)&&!t(e))throw new RangeError(n)}var Fo=e=>e>=0&&e<=1,Ds=e=>e>0&&e<1/0;function nn(e,t){if(e.filter(Ne).length!==1)throw new RangeError(t)}function Ye(e,t,n,r={}){return zl(e,t,n,r).root}var Nr=(e,t,n,r)=>an(e,t,{ncp:n,lowerTail:r});function Ga(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Xa="'sig.level' must be numeric in [0, 1]";function Ap(e={}){K(e,Ep,"powerTTest");let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,s=e.sigLevel===void 0?.05:e.sigLevel;nn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Ga(s,Xa),mn(r,Fo,"'power' must be a probability"),mn(o,Ds,"'sd' must be a positive finite number"),mn(t,w=>w>=2,"number of observations in each group must be at least 2"),mn(n,w=>!Number.isNaN(w),"'delta' must not be NaN"),mn(e.tol,Ds,"'tol' must be a positive finite number");let i=Te(e.type,["two.sample","one.sample","paired"]),c=Te(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Zl,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!Ne(n)&&(n=Math.abs(n));let u=(w,g,b,N)=>{let v=Math.max(1e-7,w-1)*f,M=Wt(N/m,v,{lowerTail:!1}),p=Math.sqrt(w/f)*g/b;Number.isFinite(p)||(p=Math.sqrt(w/f)*(g/b));let y=Nr(M,v,p,!1);return a&&m===2?y+Nr(-M,v,p,!0):y};if(Ne(r))r=u(t,n,o,s);else{let w=r;if(Ne(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=Ye(g=>u(g,n,o,s)-w,2,1e7,{tol:l,extendInt:"upX"})}else if(Ne(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let g=Math.abs(n);o=Ye(b=>u(t,n,b,s)-w,g*1e-7,g*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else Ne(n)?n=Ye(g=>u(t,g,o,s)-w,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ye(g=>u(t,n,o,g)-w,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let h=i==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":i==="two.sample"?"n is number in *each* group":void 0,d={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[i]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:s,power:r,alternative:c,...h===void 0?{}:{note:h},method:d,warnings:[]}}function Sp(e={}){K(e,xp,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;nn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Ga(s,Xa),mn(o,Fo,"'power' must be a probability"),mn(n,Fo,"'p1' must be a probability"),mn(r,Fo,"'p2' must be a probability"),mn(t,u=>u>0,"number of observations in each group must be positive"),mn(e.tol,Ds,"'tol' must be a positive finite number");let i=Te(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Zl,l=i==="two.sided"?2:1,f=(u,h,d,w)=>{let g=le(w/l,{lowerTail:!1});if(c&&l===2){let b=(h+d)/2,N=h*(1-h),v=d*(1-d),M=b*(1-b),p=Math.abs(h-d),y=Math.sqrt(N+v);return se((Math.sqrt(u)*p-g*Math.sqrt(2*M))/y)+se((Math.sqrt(u)*p+g*Math.sqrt(2*M))/y,{lowerTail:!1})}return se((Math.sqrt(u)*Math.abs(h-d)-g*Math.sqrt((h+d)*(1-(h+d)/2)))/Math.sqrt(h*(1-h)+d*(1-d)))},m=[];if(Ne(o))o=f(t,n,r,s);else{let u=o;if(Ne(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=Ye(h=>f(h,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(Ne(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=Ye(h=>f(t,h,r,s)-u,0,r,{tol:a,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")}else if(Ne(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=Ye(h=>f(t,n,h,s)-u,n,1,{tol:a,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")}else s=Ye(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return X("powerPropTest",{n:t,p1:n,p2:r,sigLevel:s,power:o,alternative:i,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var Ql=1e16,Vs=(e,t)=>e>Ql&&e<1/0&&t>Ql;function $s(e,t,n,r){let o=le(r,{lowerTail:!1}),s=n/e,i=Math.sqrt(2/e+2/t),c=Math.hypot(Math.sqrt(2*(1+2*s)/e),Math.sqrt(2/t)*(1+s));return se((o*i-s)/c,{lowerTail:!1})}function Tp(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,i,c,a]=[o(e),o(t),o(n),o(r)];return D(D(e,-s)*D(t,-i)*D(n,-c)/D(r,-a),s+i+c-a)}function kp(e={}){K(e,Rp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!Ne(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Ne(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Ga(i,Xa),mn(s,Fo,"'power' must be a probability"),mn(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),mn(o,Ds,"'withinVar' must be a positive finite number");let c=(a,l,f,m,u)=>{let h=(a-1)*l*f/m;h<1/0||(h=(a-1)*l*(f/m)),!(h<1/0)&&Number.isFinite(a)&&Number.isFinite(l)&&(h=Tp(a-1,l,f,m));let d=a-1,w=(l-1)*a;return h===1/0?1:Vs(d,w)?$s(d,w,h,u):Tn(Yn(u,d,w,{lowerTail:!1}),d,w,{ncp:h,lowerTail:!1})};if(Ne(s))s=c(t,n,r,o,i);else{let a=s;if(Ne(t))t=Ye(l=>c(l,n,r,o,i)-a,2,100);else if(Ne(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=Ye(l=>c(t,l,r,o,i)-a,2,1e5)}else if(Ne(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=Ye(l=>c(t,n,r,l,i)-a,r*1e-7,r*1e7)}else Ne(r)?r=Ye(l=>c(t,n,l,o,i)-a,o*1e-7,o*1e7):i=Ye(l=>c(t,n,r,o,l)-a,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:i,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var qp=["correct","p","rescaleP"],Ip=["confLevel"],_p=["alternative","rowScores","colScores"];function Xt(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Za(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 Ja=e=>e.length>0&&typeof e[0]!="number",ei=(e,t)=>Number.isNaN(e)?NaN:yt(e,t/2,2,!1,!1);function Jl(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 Po(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Jl(o,t[r][s]))):e.map((n,r)=>Jl(n,t[r]))}var Lp=["none","williams","yates"],tf="Log likelihood ratio (G-test)";function Cp(e,t={}){K(t,qp,"gTest");let n=Te(t.correct,Lp);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(Ja(e)){let o=Za(e);return ef(o.flat()),o.length===1?X("gTest",Qa(o[0],n,t)):o[0].length===1?X("gTest",Qa(o.map(s=>s[0]),n,t)):X("gTest",Op(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 ef(r),X("gTest",Qa(r,n,t))}function ef(e){if(e.some(t=>!(t>=0&&t<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");if(!e.some(t=>t>0))throw new RangeError("at least one entry of 'x' must be positive")}function nf(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function rf(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,hl(o,s))}return n[0]+n[1]}function Op(e,t){let n=e.length,r=e[0].length;if(t==="yates"&&(n!==2||r!==2))throw new RangeError("Yates' correction requires a 2 x 2 matrix");let o=Xt(e.flat()),s=nf(o[0],n*r),i=s===0?e:e.map(v=>v.map(M=>D(M,-s))),c=s===0?o:Xt(i.flat()),a=i.map(v=>Xt(v)),l=i[0].map((v,M)=>Xt(i.map(p=>p[M]))),f=a.map(v=>l.map(M=>_e(v,at(M,c)))),m=i;if(t==="yates"){let[[v,M],[p,y]]=i,x=v*y-M*p>0?-.5:.5,E=D(.5,-s),R=x<0?-E:E;m=[[v+R,M-R],[p-R,y+R]]}let u=rf(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],d=1;if(t==="williams"){if(a.some(y=>y[0]===0)||l.some(y=>y[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],M=a.reduce((y,x)=>y+1/(x[0]+x[1]),0),p=l.reduce((y,x)=>y+1/(x[0]+x[1]),0);d=1+D((v*M-1)*(v*p-1)/(6*v*(n-1)*(r-1)),-s)}let w=D(2*u,s)/d,g=(n-1)*(r-1),b=f.map(v=>v.map(M=>D(M[0]+M[1],s)));return{method:`${tf} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:w,statisticName:"G",parameter:g,parameterName:"X-squared df",pValue:ei(w,g),observed:s===0?m:m.map(v=>v.map(M=>D(M,s))),expected:b,ftResiduals:Po(e,b),warnings:h}}function Qa(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(v=>typeof v!="number"||Number.isNaN(v)))throw new RangeError("'p' must not contain missing values");if(s.some(v=>v<0))throw new RangeError("probabilities must be non-negative");if(s.some(v=>v===1/0))throw new RangeError("probabilities must be finite");let i=Xt(s);if(o){if(!(i[0]>0&&i[0]<1/0))throw new RangeError("the probabilities to rescale must have a positive finite sum")}else if(Math.abs(i[0]-1+i[1])>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let c=Xt(e),a=nf(c[0],r),l=a===0?e:e.map(v=>D(v,-a)),f=a===0?c:Xt(l),m=s.map(v=>o?at(Tt(f,v),i):Tt(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=D(.5,-a),M=l[0]-u[0];M>D(.25,-a)?h=[l[0]-v,l[1]+v]:-M>D(.25,-a)&&(h=[l[0]+v,l[1]-v])}let d=rf(h,m);if(!o&&!Number.isNaN(d)){let v=f[0]+f[1];d-=v*(i[0]-1+i[1])}let w=Number.isNaN(d)?["NaNs produced"]:[],g=t==="williams"?1+(r+1)/(6*D(f[0]+f[1],a)):1,b=D(2*d,a)/g,N=u.map(v=>D(v,a));return{method:`${tf} goodness of fit test`,statistic:b,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:ei(b,r-1),observed:a===0?h:h.map(v=>D(v,a)),expected:N,ftResiduals:Po(e,N),warnings:w}}function Fp(e){if(typeof e!="object"||e===null||!Ja(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Za(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 Pp(e){let t=Fp(e),n=Xt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(p=>p.map(y=>D(y,-r))),s=o.length,i=o.map(p=>Xt(p)),c=o.map((p,y)=>Xt(o.map(x=>x[y]))),a=o.map((p,y)=>o[y].some((x,E)=>E!==y&&x>0)||o.some((x,E)=>E!==y&&x[y]>0)).flatMap((p,y)=>p?[y]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((p,y)=>y),m=p=>f[p]===p?p:f[p]=m(f[p]);for(let p=0;p<l;p++)for(let y=p+1;y<l;y++)o[a[p]][a[y]]+o[a[y]][a[p]]>0&&(f[m(p)]=m(y));if(a.some((p,y)=>m(y)!==m(0)))throw new RangeError("the categories fall into groups without counts between them: the covariance matrix is singular");let u=l-1,h=a.slice(0,u).map(p=>{let y=he(i[p],[-c[p][0],-c[p][1]]);return y[0]+y[1]}),d=a.slice(0,u).map(p=>a.slice(0,u).map(y=>{if(p!==y)return-(o[p][y]+o[y][p]);let x=0;for(let E=0;E<s;E++)E!==p&&(x+=o[p][E]+o[E][p]);return x})),w=d.map(p=>p.map(()=>0)),g=[];for(let p=0;p<u;p++){let y=d[p][p];for(let x=0;x<p;x++)y-=w[p][x]*w[p][x]*g[x];g.push(y);for(let x=p+1;x<u;x++){let E=d[x][p];for(let R=0;R<p;R++)E-=w[x][R]*w[p][R]*g[R];w[x][p]=E/y}}let b=0,N=[];for(let p=0;p<u;p++){let y=h[p];for(let x=0;x<p;x++)y-=w[p][x]*N[x];N.push(y),b+=y*y/g[p]}let v=Xt(a.flatMap(p=>a.map(y=>t[p][y]))),M=l-1;return b=D(b,r),X("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:b,statisticName:"chi-squared",parameter:M,parameterName:"df",pValue:ei(b,M),N:v[0]+v[1],warnings:[]})}function Dp(e,t={}){K(t,Ip,"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=of(e),o=Xt(r.flat())[0],s=0;(o>2**250||o>0&&o<2**-250)&&(s=2*Math.round((Math.log2(o)-250)/2));let i=s===0?r:r.map(p=>p.map(y=>D(y,-s))),{pc:c,pd:a}=Vp(i),l=[0,0],f=[0,0];i.forEach((p,y)=>p.forEach((x,E)=>{l=he(l,Tt(c[y][E],x)),f=he(f,Tt(a[y][E],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=he(l,f),u=at(Pe(l,f),m)[0],h=_e(m,m),d=i.map((p,y)=>p.map((x,E)=>2*at(Pe(_e(f,c[y][E]),_e(l,a[y][E])),h)[0])),w=Math.max(...d.flat().map(Math.abs)),g=w>0&&w<1/0?Math.floor(Math.log2(w)):0,b=0;i.forEach((p,y)=>p.forEach((x,E)=>{let R=D(d[y][E],-g);b+=x*R*R}));let N=D(Math.sqrt(b),g-s/2),v=(1-n)/2,M=v<1e-8?le(v,{lowerTail:!1}):le(1-v);return X("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-M*N),Math.min(1,u+M*N)],confLevel:n,ase:N,warnings:[]})}function of(e){if(typeof e!="object"||e===null||!Ja(e))throw new TypeError("'table' must be an array of rows");let t=Za(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 Vp(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(d=>{let w=[],g=r;for(let b=0;b<n;b++)w.push(g),g=he(g,[d[b],0]);return w}),s=e.map(d=>{let w=new Array(n),g=r;for(let b=n-1;b>=0;b--)w[b]=g,g=he(g,[d[b],0]);return w}),i=d=>{let w=[],g=new Array(n).fill(r);for(let b=0;b<t;b++)w.push(g),g=g.map((N,v)=>he(N,d[b][v]));return w},c=d=>{let w=new Array(t),g=new Array(n).fill(r);for(let b=t-1;b>=0;b--)w[b]=g,g=g.map((N,v)=>he(N,d[b][v]));return w},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((d,w)=>d.map((g,b)=>he(g,l[w][b]))),h=f.map((d,w)=>d.map((g,b)=>he(g,m[w][b])));return{pc:u,pd:h}}function $p(e,t={}){K(t,_p,"lblTest");let n=Ze(t.alternative),r=of(e),o=r.length,s=r[0].length,i=(R,A,S)=>{if(R===void 0)return Array.from({length:A},(L,C)=>C+1);let T=Array.from(R);if(T.length!==A)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(T.some(L=>typeof L!="number"||!Number.isFinite(L)))throw new RangeError(`'${S}' must be finite numbers`);return T},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Xt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(R=>R.map(A=>D(A,-f))),u=m.map(R=>Xt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,A)=>Xt(m.map(S=>S[A]))).map(R=>R[0]+R[1]),d=u.reduce((R,A)=>R+A,0);if(!(f>0||d>1))throw new RangeError("the table must have a total above 1");let w=u.reduce((R,A,S)=>R+A*c[S],0)/d,g=h.reduce((R,A,S)=>R+A*a[S],0)/d,b=c.map(R=>R-w),N=a.map(R=>R-g),v=u.reduce((R,A,S)=>R+A*b[S]*b[S],0),M=h.reduce((R,A,S)=>R+A*N[S]*N[S],0),p=0;if(m.forEach((R,A)=>R.forEach((S,T)=>p+=S*b[A]*N[T])),!(v>0&&M>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let y=p/Math.sqrt(v)/Math.sqrt(M),x=f===0?Math.sqrt(d-1)*y:D(Math.sqrt(d-D(1,-f))*y,f/2),E=n==="less"?se(x):n==="greater"?se(x,{lowerTail:!1}):2*se(-Math.abs(x));return X("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:E,warnings:[]})}var sf=["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 zp(e,t,n){return vt(t)&&K(t,sf,"chisqTest"),K(n,sf,"chisqTest"),Array.isArray(t)?X("chisqTest",Wp(e,t,n??{})):X("chisqTest",jp(e,t??{}))}var lf="Chi-squared approximation may be incorrect";function Wp(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 d=t[h];!r(u)&&!r(d)&&(o.push(u),s.push(d))});let i=[...new Set(o)].sort(wn),c=[...new Set(s)].sort(wn);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])]++),{...pf(f,n),rowLevels:i,colLevels:c}}function jp(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?ti(r[0],t):o===1?ti(r.map(s=>s[0]),t):pf(r,t)}return ti(Array.from(e),t)}function ff(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 mf(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 uf(e,t){return Number.isNaN(e)?NaN:yt(e,t/2,2,!1,!1)}function hf(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 ti(e,t){if(t.nMinus1??!1)throw new RangeError("'nMinus1' requires a contingency table with at least two rows and columns");let n=e.length;if(e.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=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(w=>w<0))throw new RangeError("probabilities must be non-negative");if(o.some(w=>w===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let w=bn(o),g=w<1/0?0:Math.ceil(Math.log2(n)),b=g===0?o:o.map(v=>D(v,-g)),N=g===0?w:bn(b);o=b.map(v=>v/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(w=>D(w,-s)),c=s===0?r:bn(i),a=o.map(w=>c*w),l=s===0?a:a.map(w=>D(w,s));hf(l);let f=o.map((w,g)=>a[g]*(1-w)),m=D(bn(i.map((w,g)=>Vt(w-a[g],a[g]))),s),u="Chi-squared test for given probabilities",h=n-1,d;if(t.simulatePValue??!1){let w=ff(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=_o(w),h=NaN,d=mf(Kp(r,o,l,w),m,w)}else d=uf(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:d,observed:e,expected:l,residuals:i.map((w,g)=>D((w-a[g])/Math.sqrt(a[g]),s/2)),stdres:i.map((w,g)=>D((w-a[g])/Math.sqrt(f[g]),s/2)),ftResiduals:Po(e,l),warnings:Number.isFinite(h)&&l.some(w=>w<5)?[lf]:[]}}function Kp(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=Ll(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 Up(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>As)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=Ss(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){Ts(e,t,c,a);let m=0;for(let u=0;u<s;u++)for(let h=0;h<o;h++){let d=n[h][u];m+=(a[h+u*o]-d)**2/d}l[f]=m}return l}function pf(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=bn(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=>bn(v)),l=e[0].map((v,M)=>bn(e.map(p=>p[M]))),f=a.map(v=>l.map(M=>Es(v,M,c)));hf(f.flat());let m=e.map((v,M)=>v.map((p,y)=>p-f[M][y])),u="Pearson's Chi-squared test",h,d=(o-1)*(s-1),w,g=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),b=!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(p=>p.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let v=ff(t);u+=_o(v),h=bn(m.flatMap((p,y)=>p.map((x,E)=>Vt(x,f[y][E])))),d=NaN;let M=Up(a.map(p=>Math.trunc(p)),l.map(p=>Math.trunc(p)),f,v);w=mf(M,h,v)}else if(b>0)u+=" with Yates' continuity correction",h=bn(m.flatMap((v,M)=>v.map((p,y)=>Vt(Math.abs(p)-b,f[M][y]))));else if(h=bn(m.flatMap((v,M)=>v.map((p,y)=>Vt(p,f[M][y])))),n){u="Pearson's 'N-1' Chi-squared test";let v=h*(c-1);h=Number.isFinite(v)?v/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:d,parameterName:"df",pValue:w??uf(h,d),observed:e,expected:f,residuals:m.map((v,M)=>v.map((p,y)=>p/Math.sqrt(f[M][y]))),stdres:Bp(e,c,a,l,f,m),ftResiduals:Po(e,f),warnings:Number.isFinite(d)&&f.some(v=>v.some(M=>M<5))?[...N,lf]:N}}function Bp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((d,w)=>d.map((g,b)=>g/Math.sqrt(o[w][b]*(1-n[w]/t)*(1-r[b]/t))));let i=e.map(d=>af(d)),c=e[0].map((d,w)=>af(e.map(g=>g[w]))),a=Yp(i),l=cf(i),f=cf(c),m=l.map(d=>at(d,a)),u=c.map(d=>at(d,a)),h=f.map(d=>at(d,a));return e.map((d,w)=>d.map((g,b)=>{let N=_e(i[w],u[b]),v=Pe([g,0],N),M=v[0]+v[1],p=_e(_e(N,m[w]),h[b]),y=p[0]+p[1];if(y>=Hp||M===0)return M/Math.sqrt(y);let x=N[0]+N[1],E=m[w][0]+m[w][1],R=h[b][0]+h[b][1];return M/Math.sqrt(x)/Math.sqrt(E)/Math.sqrt(R)}))}var Hp=2**-1022;function af(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Yp(e){let t=[0,0];for(let n of e)t=he(t,n);return t}function cf(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 Gp=Math.pow(Number.EPSILON,.25);function Kt(e,t,n,r={}){let o={tol:r.tol??Gp,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),yr(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 Xp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function $o(e,t={}){K(t,Xp,"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(gl));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Qp(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 X("fisherTest",{...td(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 X("fisherTest",{method:a+_o(f),alternative:l,pValue:Zp(s,f),warnings:i})}return X("fisherTest",{method:a,alternative:l,pValue:nd(s),warnings:i})}function Qp(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: ${Cl(a)}`:null}function Zp(e,t){let n=e.filter(g=>g.some(b=>b>0)),r=e[0].map((g,b)=>b).filter(g=>e.some(b=>b[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(b=>g[b])),s=o.map(g=>g.reduce((b,N)=>b+N,0)),i=o[0].map((g,b)=>o.reduce((N,v)=>N+v[b],0)),c=s.length,a=i.length,l=[0,0];for(let g of o.flat())l=he(l,[-xs(g),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((g,b)=>g+b,0);if(m>As)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Ss(m),h=new Int32Array(c*a),d=0,w=Math.trunc(t);for(let g=0;g<w;g++){Ts(s,i,u,h);let b=0;for(let N=0;N<h.length;N++)b-=u[h[N]];b<=f&&d++}return Math.max(0,Math.min(1,(1+d)/(w+1)))}var df=1+1e-7,Jp=16,ed=2**24;function Do(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 Qr=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%Jp===0||!(s>0&&s<1/0)?(o=Nn(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=Do(o,0,n-1);for(let c=0;c<n;c++)o[c]!==0&&(o[c]=o[c]/i);return o}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)n[o]!==0&&(r=he(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?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?Do(o,s,o.length-1):Do(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*Nn(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?Do(o,s+1,o.length-1):Do(o,0,s-1))+.5*o[s]}};function ri(e,t,n){let r=e.density(n),o=r[t-e.lo]*df,s=[0,0];for(let i of r)i<=o&&(s=he(s,[i,0]));return s[0]}function gf(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=w=>Nn(w,t,n,r,!0),c=i(e)+Math.log(df),a=Math.min(s,Math.max(o,Math.floor((r+1)*(t+1)/(t+n+2))));for(;a<s&&i(a+1)>i(a);)a++;for(;a>o&&i(a-1)>i(a);)a--;let l=(w,g)=>{let b=w,N=g+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?b=v:N=v}return b},f=(w,g)=>{let b=w-1,N=g+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?N=v:b=v}return N},m,u;e<=a?(m=l(e,a),u=f(a+1,s)):(u=f(a,e),m=a-1<o||i(o)>c?o-1:l(o,a-1));let h=m<o?0:cn(m,t,n,r,!0,!1),d=u>s?0:cn(u-1,t,n,r,!1,!1);return h+d}function Vo(e,t){return tr(e,t)}function td(e,t,n){let r=Ze(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[i,c],[a,l]]=e,f=i,m=Math.min(i,l)+Math.min(c,a)+1;if(m>ed)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Qr(i+a,c+l,i+c),{lo:h,hi:d}=u,w=t.midp??!1,g;w?(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===d?1:0:g=ri(u,f,o);let b;if(f===h)b=0;else if(f===d)b=1/0;else{let y=u.mean(1);b=y===f?1:Vo(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:g,estimate:b,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=y=>{if(f===d)return 1/0;if(w){if(f===h&&y>=.5)return 0;let E=u.midTail(f,1,!1);return E===y?1:Vo(R=>u.midTail(f,R,!1)-y,E<y)}let x=u.tail(f,1,!1);return x===y?1:Vo(E=>u.tail(f,E,!1)-y,x<y)},M=y=>{if(f===h)return 0;if(w){if(f===d&&y>=.5)return 1/0;let E=u.midTail(f,1,!0);return E===y?1:Vo(R=>u.midTail(f,R,!0)-y,E>y)}let x=u.tail(f,1,!0);return x===y?1:Vo(E=>u.tail(f,E,!0)-y,x>y)},p;if(r==="less")p=[0,v(1-s)];else if(r==="greater")p=[M(1-s),1/0];else{let y=(1-s)/2;p=[M(y),v(y)]}return{...N,confInt:p,confLevel:s}}var zs=[[0,0]];function kn(e){for(let t=zs.length;t<=e;t++)zs.push(he(zs[t-1],ml([t,0])));return zs[e]}function Ws(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 ni=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 nd(e){let t=e.filter(v=>v.some(M=>M>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,M)=>t.some(p=>p[M]>0));if(t=t.map(v=>v.filter((M,p)=>n[p])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,M)=>t.map(p=>p[M])));let r=t.length,o=t.map(v=>v.reduce((M,p)=>M+p,0)),s=t[0].map((v,M)=>t.reduce((p,y)=>p+y[M],0)).sort((v,M)=>M-v),i=s.length,c=o.reduce((v,M)=>v+M,0),a=fl(kn(c));for(let v of o)a=he(a,kn(v));for(let v of s)a=he(a,kn(v));let l=[0,0];for(let v of t.flat())l=Pe(l,kn(v));let f=he(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,d=Array.from({length:i},()=>new Map),w=(v,M)=>{let p=M.join(","),y=d[v].get(p);if(y!==void 0)return y;let x=he(a,kn(M.reduce((L,C)=>L+C,0)));for(let L of M)x=Pe(x,kn(L));for(let L=v;L<i;L++)x=Pe(x,kn(s[L]));let E={R:M,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(d[v].set(p,E),v===i-1){let L=[0,0];for(let C of M)L=Pe(L,kn(C));return E.max=E.min=L[0],E.edges.push({to:E,hi:L[0],lo:L[1],mult:1}),E}let R=new Map,A=new Array(r+1).fill(0);for(let L=r-1;L>=0;L--)A[L]=A[L+1]+M[L];let S=new Array(r).fill(0),T=(L,C,q)=>{if(L===r-1){S[L]=C;let F=Pe(q,kn(C)),I=M.map((U,Q)=>U-S[Q]).sort((U,Q)=>U-Q),P=w(v+1,I),k=`${I.join(",")}|${F[0]}`,V=R.get(k);V===void 0?R.set(k,{to:P,hi:F[0],lo:F[1],mult:1}):V.mult++;return}let O=Math.max(0,C-A[L+1]),_=Math.min(M[L],C);for(let F=O;F<=_;F++)S[L]=F,T(L+1,C-F,Pe(q,kn(F)))};if(T(0,s[v],[0,0]),E.edges=[...R.values()],E.max=gt(E.edges.map(L=>L.hi+L.to.max)),E.min=ln(E.edges.map(L=>L.hi+L.to.min)),v===i-2){let L=E.edges.map(P=>{let k=P.to.edges[0];return{v:he([P.hi,P.lo],[k.hi,k.lo]),mult:P.mult}});L.sort((P,k)=>P.v[0]-k.v[0]||P.v[1]-k.v[1]);let C=L.length,q=L[C-1].v[0],O=new Float64Array(C),_=new Float64Array(C),F=new Float64Array(C),I=[0,0];L.forEach(({v:P,mult:k},V)=>{O[V]=P[0],_[V]=P[1];let U=Pe(P,[q,0]);I=he(I,[k*Math.exp(U[0])*(1+U[1]),0]),F[V]=I[0]}),E.dist={hi:O,lo:_,cum:F,vmax:q,kv:he(a,[q,0])}}return E},g=w(0,[...o].sort((v,M)=>v-M)),b=new ni,N=(v,M,p,y)=>{if(M+v.max<=u-m){let[T,L]=Ws(v.KZ[0],v.KZ[1],M,p);return b.add(T,L,y),!0}if(M+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[E,R]=Ws(u,h,-M,-p),A=0,S=x.hi.length;for(;A<S;){let T=A+S>>>1,L=x.hi[T];L<E||L===E&&x.lo[T]<=R?A=T+1:S=T}if(A>0){let[T,L]=Ws(x.kv[0],x.kv[1],M,p);b.add(T,L,y*x.cum[A-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 M of d[v].values()){let{pastHi:p,pastLo:y,count:x}=M;if(x.length!==0){for(let{to:E,hi:R,lo:A,mult:S}of M.edges)for(let T=0;T<x.length;T++){let[L,C]=Ws(p[T],y[T],R,A),q=x[T]*S;if(N(E,L,C,q))continue;let O=E.index.get(L);O===void 0?(E.index.set(L,E.count.length),E.pastHi.push(L),E.pastLo.push(C),E.count.push(q)):E.count[O]+=q}M.pastHi=M.pastLo=M.count=[],M.index.clear()}}return Math.min(1,b.value())}function js(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function si(e){return[...e].sort((t,n)=>t-n).join(",")}function oi(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 ai(e){let[,t]=js(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function ii(e){let[t,n]=js(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 oi(t,0,o)}function ci(e,t){let[,n]=js(e),r=n.length,o=Math.min(t,r-t),s=wf(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 wf(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function li(e,t){let[n,r]=js(e),o=r.length,s=Math.min(t,o-t),i=wf(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 d=r[h-1];for(let w=Math.min(h,s);w>=Math.max(1,s-(o-h));w--){let g=c[w],b=c[w-1],N=w/h,v=(h-w)/h,M=i[w],p=i[h]-i[h-w],y=i[w-1]+d,x=Math.max(M,y);for(let E=p;E>=x;E--)g[E-M]=v*g[E-M]+N*b[E-y];for(let E=x-1;E>=M;E--)g[E-M]=v*g[E-M]}}let a=c[s],l=t*(t+1)/2;if(s===t)return oi(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 oi(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 Mr(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 bf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function zo(e,t,n={}){if(typeof e!="string"&&vt(t)&&K(t,bf,"wilcoxTest"),K(n,bf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let w=qs(e,t,!0);return w.kind==="one"?zo(w.x,n):zo(w.x,w.y,w.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=od(n.correct??!0),l=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(l&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(i&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[w,g]=Zn(Array.from(e),Array.from(r));u=Dt(w.map((b,N)=>b-g[N]))}else u=Dt(Array.from(e)),r!==void 0&&(h=Dt(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let d={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new mt};return X("wilcoxTest",h===void 0?ld(u,c,i,d):wd(u,h,c,d))}function od(e){if(typeof e=="boolean")return e?0:-1;if(!(Number.isInteger(e)&&e>=0&&e<=3))throw new RangeError("'correct' must be an integer between 0 and 3");return e}function Ks(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var Nf=10*Number.EPSILON;function Mf(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?se(e):r(se(e)-n*Gt(e)),s=()=>n===0?se(e,{lowerTail:!1}):r(se(e,{lowerTail:!1})+n*Gt(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let c=s();return c>=1/64?2*(1-i):2*c}function sd(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function ad(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 Ef(e,o,s,n)}function id(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 Ef(e,i,c,r)}function Ef(e,t,n,r){let[o,s,i]=sd(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function ui(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:it([e[n-1],e[n]])}var hi=(e,t)=>e-t,fi=5e9,cd=40;function mi(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function Us(e,t){if(e.size>16777216||e.steps>fi)throw mi(t);return e.steps}function xf(e){let t=e.length,n=Nt(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 yn(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 ld(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(p=>p-s);i.some((p,y)=>!Number.isFinite(p)&&Number.isFinite(e[y]))&&(i=e.map(p=>p/2-s/2));let a=i.some(p=>p===0),l=t??i.length<50,f=(p,y,x)=>({method:p,alternative:o,statistic:y,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let p=Nt(i.map(Math.abs)),y=yn(p).length>0,x=0;for(let L=0;L<i.length;L++)i[L]>0&&(x+=p[L]);let E="Wilcoxon signed rank exact test";if(!y&&!a){let L=i.length,C=L*(L+1)/2,q;if(o==="less")q=jr(x,L,!0,!1);else if(o==="greater")q=jr(x-1,L,!1,!1);else{let _=x>C/2?jr(x-1,L,!1,!1):jr(x,L,!0,!1);q=Math.min(1,2*_)}let O=f(E,x,q);return r.confInt?{...O,...fd(e,r)}:O}let R=p.filter((L,C)=>i[C]!==0);Us(ai(R),"distribution");let A=ii(R),S;if(o==="less")S=nr(A,x);else if(o==="greater")S=Mr(A,x-.25);else{let L=0;for(let C of R)L+=C/2;S=Math.min(1,2*(x>L?Mr(A,x-.25):nr(A,x)))}let T=f(E,x,S);return r.confInt?{...T,...ud(e,r)}:T}let m=a?i.filter(p=>p!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,d,w]=xf(m),b=yn(Nt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(d-Ks(d,o,b>=0))/w,v=Mf(N,o,b>=1?ad(N,u,b):0),M=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...M,...hd(e,r)}:M}function pi(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function vf(e,t,n){let r={confInt:pi(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Rf(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Af(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=tn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(hi)}function Sf(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+Nf&&l++;let f=ui(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:pi(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function fd(e,t){let n=e.length;return Sf(Af(e),r=>jr(r,n,!0,!1),r=>xl(r,n,!0,!1),t,"(pseudo)median")}function Tf(e){let t=e.length,n=Float64Array.from(e).sort(),r=new Array(t);for(let s=0;s<t;s++){let i=e[s],c=0,a=t;for(;c<a;){let f=c+a>>>1;n[f]<i?c=f+1:a=f}let l=c;for(a=t;l<a;){let f=l+a>>>1;n[f]<=i?l=f+1:a=f}r[s]=(c+1+l)/2}let o="";for(let s=0;s<t;){let i=s;for(;i+1<t&&n[i+1]===n[s];)i++;i>s&&(o+=`${s}:${i-s+1},`),s=i+1}return{ranks:r,key:o}}function kf(e,t,n,r,o,s,i,c,a,l){let{alternative:f,confLevel:m}=a,u=e.length,d=(f==="two.sided"?(1-m)/2:1-m)+Nf,w=f!=="less",g=f!=="greater",b=q=>tn(e[q-1],e[q]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+le(Math.min(d,1))*i.sd+.1*i.sd+3))),v=8,M=q=>q===0?e[0]-1:b(q),p=q=>q===0?e[u-1]+1:b(u-q),y=[new Map,new Map];{let q=new Map,O=0,_=cd*c;for(let[F,I]of[[0,M],[1,p]])if(!(F===0?!w:!g))for(let P=0;P<=N;P++){if(O+=_,O>fi)throw mi("interval");let k;try{k=t(I(P))}catch{break}if(y[F].set(P,[k.stat,k.key]),q.has(k.key)){q.delete(k.key),q.set(k.key,!0);continue}if(O+=r(k.scores),O>fi)throw mi("interval");q.set(k.key,!0),q.size>v&&q.delete(q.keys().next().value)}}let x=new Map,E=(q,O)=>{let _=q===0?M(O):p(O),F=y[q].get(O),I;if(F===void 0){let U=t(_);F=[U.stat,U.key],I=U.scores}let[P,k]=F,V=x.get(k);return V!==void 0?x.delete(k):(V=n(I??t(_).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(k,V),[P,V]},R=q=>{let[O,_]=E(0,q);return o(_,O)},A=q=>{let[O,_]=E(1,q);return s(_,O)},S=-1/0,T=0;if(w){let q=R(0);if(!(q>d)){let O=q,_=1;for(;_<=u-1;_++){let F=R(_);if(F>d)break;O=F}S=e[_-1],T=O}}let L=1/0,C=0;if(g){let q=A(0);if(!(q>d)){let O=q,_=u-1;for(;_>=1;_--){let F=A(u-_);if(F>d)break;O=F}L=e[_],C=O}}return{confInt:[S,L],confLevel:1-(T+C),estimate:ui(e),estimateNames:l}}function md(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 ud(e,t){let n=e.length,r=s=>{let i=md(e,s),{ranks:c,key:a}=Tf(i.map(Math.abs)),l=0;for(let f=0;f<n;f++)i[f]>0&&(l+=c[f]);return{stat:l,scores:c,key:a}},o=n*(n+1)/2;return kf(Af(e),r,ii,s=>Us(ai(s),"interval"),(s,i)=>Mr(s,i-.25),nr,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function hd(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return vf(n,NaN);let c=ln(e),a=gt(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"),vf(n,tn(c,a),"midrange");let l=(v,M)=>{let[,p,y]=xf(e.map(x=>x-v).filter(x=>x!==0));return(p-Ks(p,n,M))/y},f=l(c,s),m=l(a,s),u=v=>-le(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>le(v/2)):!(f<u(v/2))&&!(m>le(v/2)),d=1-r,w=!1;for(;!h(d)&&d<1;)d*=2,w=!0;w&&t.warnings.add("requested conf.level not achievable"),Rf(o);let g=v=>Kt(M=>l(M,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),b;if(d>=1){let v=ui([...e].sort(hi));b=pi(n,v,v)}else n==="greater"?b=[g(u(d)),1/0]:n==="less"?b=[-1/0,g(le(d))]:b=[g(u(d/2)),g(le(d/2))];let N=Kt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:b,confLevel:w?1-Math.min(1,d):r,estimate:N,estimateNames:"(pseudo)median"}}function qf(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 yn(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 pd(e,t){let[n,r,o]=qf(Nt([...e,...t]),e.length);return[n,r,o]}function dd(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 gd(e,t,n){return If(e,t,n).ranks}function If(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=Nt([...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:si(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=dd(e.map(s=>s/2-n/2),t);return{ranks:o,key:si(o)}}return Tf([...r,...t])}function wd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=gd(e,t,s),[f,m,u,h]=qf(l,i),d,w;if(a)if(w="Wilcoxon rank sum exact test",!h)d=yf(()=>o==="less"?Kr(f,i,c,!0,!1):o==="greater"?Kr(f-1,i,c,!1,!1):Math.min(2*Kr(f,i,c,!0,!1),2*Kr(f-1,i,c,!1,!1),1));else{Us(ci(l,i),"distribution");let N=li(l,i);o==="less"?d=nr(N,f):o==="greater"?d=Mr(N,f-.25):d=Math.min(2*nr(N,f),2*Mr(N,f-.25),1)}else{w=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(p=>p===l[0]),M=(m-Ks(m,o,N>=0))/u;d=v?1:Mf(M,o,N>=1?id(M,i,c,N):0)}let g={method:w,alternative:o,statistic:f,statisticName:"W",pValue:d,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return g;let b=a?h?vd(e,t,r):yf(()=>bd(e,t,r)):yd(e,t,r);return{...g,...b}}function yf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function _f(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(hi)}function bd(e,t,n){let r=e.length,o=t.length;return Sf(_f(e,t,!1),s=>Kr(s,r,o,!0,!1),s=>Al(s,r,o,!0,!1),n,"difference in location")}function vd(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=If(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 kf(_f(e,t,!0),s,i=>li(i,r),i=>Us(ci(i,r),"interval"),(i,c)=>Mr(i,c-.25),nr,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function yd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=ln(e)-gt(t),a=gt(e)-ln(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,d)=>{let[,w,g]=pd(e.map(b=>b-h),t);return(w-Ks(w,r,d))/g},f=(h,d)=>{let w=l(c,d)-h,g=l(a,d)-h;if(Number.isNaN(w)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return w<=0?c:g>=0?a:(Rf(s),Kt(b=>l(b,d)-h,c,a,{tol:s,fLower:w,fUpper:g}))},m=1-o,u;return r==="greater"?u=[f(-le(m),i),1/0]:r==="less"?u=[-1/0,f(le(m),i)]:u=[f(-le(m/2),i),f(le(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Nd=["method","n"],Lf=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Md(e){if(Lf.includes(e))return e;let t=Lf.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Zr(e,t={}){K(t,Nd,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Md(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=Of(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Bs(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 Of(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=Bs(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=Bs(e,!0),i=1/0;return s.forEach((c,a)=>{let l=r-a;i=Math.min(i,(n-l+1)*e[c]),o[c]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=Dn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)gn(m,1/h,u);s=Vn(m)}else{let h=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*h)+1/(120*h*h)}}let i=s*n,c=Number.isFinite(i)?1:1024,a=Number.isFinite(i)?i:s*(n/1024),l=Bs(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?Of(e,"hochberg",n):Ed(e,n)}}var Cf=2**16;function Ed(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Cf)throw new RangeError(`n must be at most ${Cf} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Bs(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 Ff=["p","alternative","confLevel"],xd=1+1e-7;function di(e,t,n){let r=e-1;return e===0||e-r===1&&Ft(t,-r)[1]===0?Pn(r,t,n,!1,!1):Pn(e,t,n,!1,!1)+dn(e,t,n,!1)}function Rd(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)*xd,s=a=>dn(a,t,n,!1)<=o;if(e<r){let a=bs(s,Math.ceil(r),t),l=a>t?0:di(a,t,n);return Math.min(1,Pn(e,t,n,!0,!1)+l)}let i=vs(s,0,Math.floor(r)),c=i<0?0:Pn(i,t,n,!0,!1);return Math.min(1,di(e,t,n)+c)}function Ad(e,t,n={}){K(t,Ff,"binomTest"),K(n,Ff,"binomTest");let r,o;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(n=t),!Mn(e[0])||!Mn(e[1]))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1]),o===0||o===1/0)throw new RangeError("'n' must be a positive integer >= 'x'")}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Mn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Mn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return gi(r,o,n)}function gi(e,t,n={}){let r=n.p??.5;if(!(r>=0&&r<=1))throw new RangeError("'p' must be a single number between 0 and 1");let o=Ze(n.alternative),s=n.confLevel??.95;Lt(s);let i;o==="less"?i=Pn(e,t,r,!0,!1):o==="greater"?i=di(e,t,r):i=Rd(e,t,r);let c=f=>e===0?0:Gn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Gn(f,e+1,t-e,!1,!1):Gn(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,a(1-s)];else if(o==="greater")l=[c(1-s),1];else{let f=(1-s)/2;l=[c(f),a(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:i,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var Sd=["T","r","alternative","confLevel"],Td=1+1e-7;function kd(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=ys(e,t,!1)*Td,r=i=>ys(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=bs(r,Math.max(e+1,o),1/0);return Math.min(1,Wr(e,t,!0,!1)+wi(i,t))}let s=vs(r,0,Math.min(e-1,o));return Math.min(1,Wr(s,t,!0,!1)+wi(e,t))}var Wo=2**-1022;function wi(e,t){return e-(e-1)===1?Wr(e-1,t,!1,!1):Wr(e,t,!1,!1)+ys(e,t,!1)}function Er(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function qd(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Wo&&(s!==0||e===0||t===0))return s;let i=e*t,c=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(i)&&Number.isFinite(c))return i/c;let[a,l]=Er(e),[f,m]=Er(t),[u,h]=Er(n),[d,w]=Er(r),g=f*d,b=h-m-w,N=b>=0?u/(u+D(g,-b)):u/(D(u,b)+g);return D(a*f*N,l+m+Math.min(b,0))}function Id(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Wo&&(o===0||o>=Wo)?i:e===0?0:e/t*(r/n)}function _d(e,t={}){K(t,Sd,"poissonTest");let n=typeof e=="number"?[e]:Array.from(e);if(n.length<1||n.length>2)throw new RangeError("incorrect length of 'x'");if(!n.every(Mn))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,i=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(b=>Number.isFinite(b)&&b>0))throw new RangeError("'T' must be positive");let c=t.r??1;if(!(Number.isFinite(c)&&c>=0))throw new RangeError("'r' must be a single positive number");let a=Ze(t.alternative),l=t.confLevel??.95;if(o===2?Lt(l):fn(l),o===2){let[b,N]=r,[v,M]=i,p=b+N;if(p===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*v,x=y+M,E=Number.isFinite(x)&&x>=Wo&&(y===0||y>=Wo)?y/x:1/(1+M/v/c),R=gi(Math.round(b),Math.round(b)+Math.round(N),{p:E,alternative:a,confLevel:l}),A=M/v,S=T=>{if(T===0)return 0;let L=T/(1-T);if(Number.isFinite(A)||L===1/0)return L*A;let[C,q]=Er(L),[O,_]=Er(v),[F,I]=Er(M);return D(C*F/O,q+I-_)};return{method:"Comparison of Poisson rates",alternative:a,statistic:b,statisticName:"count1",parameter:qd(p,c,v,M),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:Id(b,N,v,M),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Wr(f,u,!0,!1):a==="greater"?h=wi(f,u):h=kd(f,u);let d=b=>f===0?0:ws(b,f,1,!0,!1),w=b=>b<1e-8?ws(b,f+1,1,!1,!1):ws(1-b,f+1,1,!0,!1),g;if(a==="less")g=[0,w(1-l)];else if(a==="greater")g=[d(1-l),1/0];else{let b=(1-l)/2;g=[d(b),w(b)]}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 Pf=["p","alternative","confLevel","correct"],Ld="Chi-squared approximation may be incorrect";function bi(e,t,n={}){vt(t)&&K(t,Pf,"propTest"),K(n,Pf,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(T=>T.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(T=>T[0]),o=S.map(T=>T[0]+T[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,T)=>S>o[T]))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(T=>S[T]),l.some(T=>!(T>0&&T<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;Lt(m);let u=Ze(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,T)=>S/o[T]),d=a===2?h[0]-h[1]:0,w=a===2?1/o[0]+1/o[1]:0,g=(s.correct??!0)&&a<=2?.5:0,b=!1;if(g>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=g&&([g,b]=[S,!0])}else if(g>0&&a===2&&!f){let S=Math.abs(d)/w;S<=g&&([g,b]=[S,!0])}let N=Cd(r,o),v=o.some((S,T)=>{let L=l?.[T]??N;return S*L<5||S*(1-L)<5}),M=0;if(!b)for(let S=0;S<a;S++){let T=l?.[S]??N,L=o[S]*T,C=o[S]*(1-T);M+=Vt(Math.abs(r[S]-L)-g,L),M+=Vt(Math.abs(o[S]-r[S]-C)-g,C)}let p=l!==void 0?a:a-1,y;if(u==="two.sided")y=yt(M,p/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:d);y=se(S*Math.sqrt(M),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,T=u==="two.sided"&&S<1e-8?le(S,{lowerTail:!1}):le(u==="two.sided"?(1+m)/2:m);if(a===1){let L=o[0],C=T*T,q=(P,k)=>(P+C/(2*L)+k*T*Math.sqrt(P*(1-P)/L+C/(4*L*L)))/(1+C/L),O=h[0]+g/L,_=h[0]-g/L,F=Math.min(1,Math.max(0,O>=1?1:q(O,1))),I=Math.min(1,Math.max(0,_<=0?0:q(_,-1)));x=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[0,F]}else{let L=Math.sqrt(h.reduce((_,F,I)=>_+F*(1-F)/o[I],0)),C=T*L+g*w,q=Math.max(-1,d-C),O=Math.min(1,d+C);x=u==="two.sided"?[q,O]:u==="greater"?[q,1]:[-1,O]}}let E=a===1?"p":c.map(S=>`prop ${S+1}`),A={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:M,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:y,estimate:a===1?h[0]:h,estimateNames:E,warnings:v?[Ld]:[]};return l!==void 0&&(A.nullValue=a===1?l[0]:l,A.nullValueName=E),x!==void 0&&(A.confInt=x,A.confLevel=m),X("propTest",A)}function Cd(e,t){let n=t.reduce((o,s)=>o+s,0);if(n<1/0)return e.reduce((o,s)=>o+s,0)/n;let r=Math.ceil(Math.log2(t.length));return e.reduce((o,s)=>o+D(s,-r),0)/t.reduce((o,s)=>o+D(s,-r),0)}var Od=["method","confLevel","sides"],Fd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],Pd=["two.sided","left","right"];function jo(e,t){let n=Te(t,Pd),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{...vi(s,o),sides:n}}function vi(e,t=1-e){let n=e/2<1e-8,r=n?le(e/2,{lowerTail:!1}):le(1-e/2),o=n?Fn(e,1,{lowerTail:!1}):Fn(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Jr(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!Mn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function Wn(e,t,n,r){let o=Jr(e,n),s=Jr(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 Ys(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 Hs(e,t,n,r){return r?e/2<1e-8?Gn(e/2,t,n,!1,!1):Gn(1-e/2,t,n,!0,!1):Gn(e/2,t,n,!0,!1)}function Df(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*dn(e,t,m,!1)+Pn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*dn(e,t,m,!1)+Pn(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 d=i*s(h);if(d<=0)return Math.exp(d===0?h:Kt(w=>i*s(w),h,f,{tol:2**-60,fLower:d,fUpper:m}));f=h,m=d,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 yi(e,t,n={}){K(n,Od,"binomCI");let r=Te(n.method,Fd),o=jo(n.confLevel,n.sides),[s,i]=Wn(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]=Ys(s,i,a);break;case"wilsoncc":{let h=a*a,d=2*(i+h),w=2*s+h-1,g=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:w-g<w/2?(2*s-1)/(2*i)*(2*s-1)/(w+g):(w-g)/d,u=l===1?1:(2*s+h+1+a*Math.sqrt(h+2-1/i+4*l*(i*f-1)))/d;break}case"agresti-coull":{let h=a*a,d=i+h,w=(s+h/2)/d,g=a*Math.sqrt(w*(1-w)/d);[m,u]=[w-g,w+g];break}case"jeffreys":m=s===0?0:Hs(c,s+.5,i-s+.5,!1),u=s===i?1:Hs(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:Hs(c,s,i-s+1,!1),u=s===i?1:Hs(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):Df(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):Df(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),X("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Ko(e){return e===1/0?1:e/(1+e)}function Uo(e,t){return e===0?0:t>0?e*e/t:1/0}function Dd(e,t,n,r){return e/t+n/r>1}function Vd(e,t,n,r){return Xs(e,t,n,r,0)}function Ei(e,t,n,r,o){if(Dd(e,t,n,r)){let[d,w,g,b]=Ei(t-e,t,r-n,r,-o);return[w,d,b,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 d=b=>{let N=0;return i>0&&(N+=i/b),i<c&&(N-=(c-i)/(1-b)),a>0&&(N+=a/(b+f)),a<l&&(N-=(l-a)/(m-b)),N},w=b=>{let N=0;return i>0&&(N-=i/(b*b)),i<c&&(N-=(c-i)/((1-b)*(1-b))),a>0&&(N-=a/((b+f)*(b+f))),a<l&&(N-=(l-a)/((m-b)*(m-b))),N};if(d(0)<=0)u=0;else if(d(m)>=0)u=m;else{let b=0,N=m,v=$d(e,t,n,r,o);u=s?v:v-o,u>b&&u<N||(u=N/2);for(let M=0;M<2e3;M++){let p=d(u);if(p===0)break;p>0?b=u:N=u;let y=u-p/w(u);if(y>b&&y<N||(y=b===0?N/2:N>2*b?Math.sqrt(b*N):b+(N-b)/2),!(y>b&&y<N))break;let x=Math.abs(y-u);if(u=y,x<=2**-53*u)break}}}let h=[u,1-u,Math.min(1,u+f),Math.max(0,m-u)];return s?h:[h[2],h[3],h[0],h[1]]}function $d(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),d=Math.sign(h)*Math.sqrt(Math.max(0,u*u-f/(3*a)));if(d===0)return-u;let w=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(d*d*d)))))/3;return 2*d*Math.cos(w)-u}function xi(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]=Ei(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 zd(e,t,n,r,o,s,i){let c=Pe(Pe(lt([e,0],t),lt([n,0],r)),[o,0]),[a,l,f,m]=Ei(e,t,n,r,o),u=he(lt(en(a,l),t),lt(en(f,m),r));return s==="mn"&&(u=lt(Tt(u,t+r),t+r-1)),Pe(_e(c,c),Tt(u,i))[0]}function Xs(e,t,n,r,o){return Pe(Pe(lt([e,0],t),lt([n,0],r)),[o,0])[0]}function Wd(e,t,n,r,o,s){return Uo(Xs(e,t,n,r,o),xi(e,t,n,r,o,s))}var Gs=new Float64Array(1),Ni=new BigInt64Array(Gs.buffer);function Vf(e){return Gs[0]=Math.abs(e),e<0?-Ni[0]:Ni[0]}function rr(e){return Ni[0]=e<0n?-e:e,e<0n?-Gs[0]:Gs[0]}function Mi(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=Vf(i?r:n),a=Vf(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,d=e(rr(h));if(d===0)return rr(h);d<0==l<0?(a=h,l=d):(f=h,m=d)}return Math.abs(m)<Math.abs(l)?rr(f):rr(a)}function zf(e,t,n,r,o,s){let i=Vd(e,t,n,r);if(o===0)return[i,i];let c=Ko(o),a=d=>(d===i?0:Ko(Wd(e,t,n,r,d,s)))-c,l=(d,w,g,b)=>{if(g===0)return d;if(b===0)return w;let N=Kt(a,d,w,{tol:1e-300,fLower:g,fUpper:b});return s==="wald"?N:Mi(v=>zd(e,t,n,r,v,s,o),N,d,w,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 $f(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 jd(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 Kd(e,t,n,r,o){let[s,i]=jd(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,i,1-i];let c=s<=.5&&i<=.5?0:o*(1-i)<=1-s?2:3,a=c===0?i:c===2?1-i:1-s,l=y=>c===0?[y,0]:c===2?Ft(1,-y):lt(Ft(1,-y),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?lt(Ft(o,-1),o)[0]:0:c===3&&o<1?Ft(1,-o)[0]:0,h=y=>{let x=l(y),E=Tt(x,o);return[E[0],Pe([1,0],E)[0],x[0],Pe([1,0],x)[0]]},d=e+n,w=c<2?1:-1,g=y=>{let[,x,E,R]=h(y),A=0,S=0;return d>0&&(A+=E>0?d/E:1/0,S-=d/(E*E)),e<t&&(A-=x>0?o*(t-e)/x:1/0,S-=o*(t-e)/x*(o/x)),n<r&&(A-=R>0?(r-n)/R:1/0,S-=(r-n)/(R*R)),[w*A,S*f]},b;if(!(m>u))b=u;else if(!(g(u)[0]>0))b=u;else if(!(g(m)[0]<0))b=m;else{let y=u,x=m;b=a,b>y&&b<x||(b=y+(x-y)/2);for(let E=0;E<2e3;E++){let[R,A]=g(b);if(R===0)break;R>0?y=b:x=b;let S=b-R/A;if(S>y&&S<x||(S=y<=0?x/2:x>2*y?Math.sqrt(y*x):y+(x-y)/2),!(S>y&&S<x))break;let T=Math.abs(S-b);if(b=S,T<=2**-53*b)break}}let[N,v,M,p]=h(b);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,M)),Math.min(1,Math.max(0,p))]}function Ud(e,t,n,r,o){let[s,i,c,a]=Kd(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return Uo(l(e,t,s,i),t*s*i)+Uo(l(n,r,c,a),r*c*a)}function Wf(e,t,n,r,o){let s=e/t/(n/r);return Kf(s,o,i=>Ud(e,t,n,r,i))}function Bd(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 Hd(e,t,n,r,o){let[s,i]=Bd(e,t,n,r,o),c=e-t*s;return Uo(c,t*s*(1-s))+Uo(c,r*i*(1-i))}function jf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return Kf(s,o,i=>Hd(e,t,n,r,i))}function Kf(e,t,n){if(t===0)return[e,e];let r=Ko(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Mi(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?Mi(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Ko(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:Ko(n(Math.exp(l))))-r;return[s($f(a,c,-1)),i($f(a,c,1))]}var Yd=["method","confLevel","sides"],Gd=["method","confLevel","sides"],Xd=["method","confLevel"],Qd=["ac","wald","score","mn","mee"];function Ri(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),d=u-h,w=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,d-w),Math.min(1,d+w)];break}case"score":{let[u,h]=Ys(e,t,i),[d,w]=Ys(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+w*(1-w)/r),m=l+i*Math.sqrt(h*(1-h)/t+d*(1-d)/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 Zd(e,t,n,r,o={}){K(o,Yd,"binomDiffCI");let s=Te(o.method,Qd),i=jo(o.confLevel,o.sides),[c,a]=Wn(e,t,"x1","n1"),[l,f]=Wn(n,r,"x2","n2"),m=c/a-l/f,[u,h]=Ri(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),X("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Jd=["katz.log","koopman"];function Qs(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 e0(e,t,n,r,o={}){K(o,Gd,"binomRatioCI");let s=Te(o.method,Jd),i=jo(o.confLevel,o.sides),[c,a]=Wn(e,t,"x1","n1"),[l,f]=Wn(n,r,"x2","n2"),m=i.kappa,u=c===0?0:c/a/(l/f),h,d;return c===0&&l===0?[h,d]=[0,1/0]:s==="katz.log"?c===0?[h,d]=[0,Qs(.5,a,l,f,m)[1]]:l===0?[h,d]=[Qs(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,d]=Qs(a-.5,a,f-.5,f,m):[h,d]=Qs(c,a,l,f,m):[h,d]=Wf(c,a,l,f,i.chisq),i.sides==="left"?d=1/0:i.sides==="right"&&(h=0),X("binomRatioCI",{estimate:u,confInt:[h,d],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var t0=["midp","fisher","wald","score"],n0={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"},r0=2**23,o0=2**20;function Uf(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 s0(e,t={}){K(t,Xd,"oddsRatio");let n=Te(t.method,t0);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}=jo(r,void 0),c=e.map((S,T)=>Array.from(S,(L,C)=>Jr(L,`table[${T}][${C}]`))),[[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),d=a+Math.min(l,f),w=d-h+1;if(n==="midp"||n==="fisher"){if(w>r0)throw new RangeError(`the exact distribution of the 2 x 2 table has ${w} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(a,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let g,b,N;try{g=cn(a-1,a+f,l+m,a+l,!0,!1)+.5*Nn(a,a+f,l+m,a+l,!1),b=cn(a,a+f,l+m,a+l,!1,!1)+.5*Nn(a,a+f,l+m,a+l,!1),N=w<=o0?ri(new Qr(a+f,l+m,a+l),a,1):gf(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,b),M=[a+l,f+m],p=[a+f,l+m],y=0,x=!1;for(let S=0;S<2;S++)for(let T=0;T<2;T++){let L=M[T]*p[S]/u;L<5&&(x=!0);let C=Math.abs(c[T][S]-L);y+=C*C/L}let E=yt(y,.5,2,!1,!1),R,A;switch(n){case"wald":{R=a*m/(l*f);let S=s*Math.sqrt(1/a+1/l+1/f+1/m);A=[Math.exp(Math.log(R)-S),Math.exp(Math.log(R)+S)];break}case"fisher":{let S=rt(()=>$o(c,{confLevel:r}));R=S.estimate,A=S.confInt;break}case"score":{R=a*m/(l*f),A=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:jf(a,a+l,f,f+m,i);break}case"midp":{let S=new Qr(a+f,l+m,a+l),T=O=>{if(O===1)return[g,b];let _=S.density(O),F=a-h,I=.5*_[F];return[Uf(_,0,F-1)+I,Uf(_,F+1,_.length-1)+I]},L=(O,_)=>{let F=O(1);return F===0?1:tr(O,_?F>0:F<0)};R=a===h?0:a===d?1/0:L(O=>T(O)[0]-.5,!1);let C=a===h?0:L(O=>T(O)[1]-o/2,!0),q=a===d?1/0:L(O=>T(O)[0]-o/2,!1);A=[C,q];break}}return X("oddsRatio",{estimate:R,confInt:A,confLevel:r,method:n0[n],pValue:{midp:v,fisher:N,chiSquare:E},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var a0=["epsilon","paired","varEqual","confLevel","alpha"],i0=["margin","method","alpha"];function Bf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function c0(e,t,n={}){K(n,a0,"tost");let r=n.epsilon??1;if(!(typeof r=="number"&&r>0&&r<1/0))throw new RangeError("'epsilon' must be a positive number");let o,s;if(n.alpha!==void 0)Bf(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,Lt(o),!(o>.5))throw new RangeError("'confLevel' must be greater than 0.5");s=1-o}if(t===void 0)throw new TypeError("'y' is required");let i=n.paired??!1,c=n.varEqual??!1,a=rt(()=>En(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=ft((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?bt(s,f,!1,!1):bt(o,f,!0,!1),d=[m-h*l,m+h*l];return X("tost",{method:a.method.replace("t-test","TOST"),parameter:f,parameterName:"df",pValue:u,result:d[0]>-r&&d[1]<r?"rejected":"not rejected",confInt:d,confLevel:o,estimate:a.estimate,estimateNames:i?"mean of the differences":a.estimateNames,epsilon:r,seDiff:l,warnings:[]})}var l0=["mn","mee","wald"];function f0(e,t,n,r,o={}){K(o,i0,"binomDiffEquivalenceTest");let s=Te(o.method,l0),i=o.margin;if(i===void 0)throw new TypeError("'margin' is required");if(!(typeof i=="number"&&i>0&&i<1))throw new RangeError("'margin' must be a single number in (0, 1)");let c=o.alpha??.05;Bf(c,"alpha");let[a,l]=Wn(e,t,"x1","n1"),[f,m]=Wn(n,r,"x2","n2"),u=a/l-f/m,h=M=>Xs(a,l,f,m,M)/Math.sqrt(xi(a,l,f,m,M,s)),d=h(-i),w=h(i),g=[se(d,{lowerTail:!1}),se(w)],b=Math.max(g[0],g[1]),N=1-2*c,v=Ri(a,l,f,m,s,vi(2*c,N));return X("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[d,w],statisticName:"z",pValues:g,pValue:b,result:g[0]<c&&g[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var Ut=new WeakMap;function Js(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Se(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function ea(e,t){return e.map(n=>{if(n.rss>=m0&&n.rss<1/0)return D(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=D(o,-t);r+=i*c*c}),r})}var m0=2**-1022;function Si(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,D(n,-t)]])}function ta(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function xr(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+D(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=ta(e),s=r(o)),!(D(t,-o)>=1e-24*s)}function Hf(e){let t=Js([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+=D(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:ea([e],t)[0],k:t}}var Yf=(e,t,n)=>dt(e,t,n,!1,!1),Ti="essentially perfect fit for \u20181\u2019: summary may be unreliable";function ki(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 qi=new WeakMap;function u0(e){let t=Hf(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*D(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function Ai(e){let t=u0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let w=Si(e,o);xr(w,c)&&(c=0,i=i.map(g=>xr(w,g.ss)?{...g,ss:0}:g))}let a=c/r,l=w=>D(w,2*o),f=i.map(({df:w,ss:g})=>{let b=g/w/a;return[w,l(g),l(g/w),b,Yf(b,w,r)]});f.push([r,l(c),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],h={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},d=i.map(({df:w,ss:g})=>[w,g]);return d.push([r,c]),Ut.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:d}),h}function h0(e){let t=e.map(f=>f.dfResidual),n=Js(e),r=ea(e,n),o=f=>D(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*D(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],[d,w]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],g=N=>N.filter((v,M)=>(d.weights?.[M]??1)!==0),b=u===0||!Cs(g(d.x),g(w.x))?NaN:h/u/i;return b<0&&(b=NaN),[t[m],o(r[m]),u,o(h),b,Number.isNaN(b)?NaN:Yf(b,Math.abs(u),t[s])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((f,m)=>String(m+1)),table:l,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function Zs(...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=Ai(e),a={...c,warnings:Zs(e.warnings,c.warnings)},l=Ut.get(c);return l!==void 0&&Ut.set(a,l),X("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?Ai(e):h0(o);return i.warnings=Zs(...n.map(c=>c.warnings),s,i.warnings),X("anova",i,...n)}function _i(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Zs(i.warnings,qi.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Ut.get(i);return Array.isArray(l)&&Ut.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),X("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=Ai(t),r=Hf(t),o=ki(t.fittedValues.map(i=>D(i,-r.k)),r.rss,r.rdf),s=Zs(t.warnings,n.warnings,o?[Ti]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Ut.get(n);return c!==void 0&&Ut.set(i,c),X("aovSummary",i,e)}return X("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 p0=["pooledSd","mu","paired","ci","alternative","adjust"],d0=["pooledSd","mu","paired","ci","alternative"],g0=["generalized","partial","ci","alternative"],w0=["partial","ci","alternative"],b0=["partial","ci","alternative"];function Oi(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var v0="Missing values detected. NAs dropped.",y0=`'y' is numeric but has only 2 unique values.
|
|
3
|
-
If this is a grouping variable, convert it to a factor.`,Zf={tol:0,maxiter:5e3};function Rr(e){return e===1/0?1:Math.exp(wl(.5,(e-1)/2)-.5*Math.log(e/2))}function Gf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return yr(o=>an(e,t,{ncp:o})-n,e-r,e+r,{...Zf,extendInt:"downX"}).root}function Jf(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;Oi(c);let a=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(Dt(f)).size===2&&m.push(y0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(v0);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,d,w,g,b,N;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:N}=Se([Dt(l)]));else{let[L,C]=Zn(l,f);({d:S,k:N}=Ns(L,C))}let T=S.length;if(T<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");h=it(S),d=Math.sqrt(st(S)),w=d/Math.sqrt(T),g=1/T,b=T-1}else{let S=Pt([Dt(l),Dt(f)]);N=S.k;let[T,L]=S.n;if(T<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 C=Math.sqrt(S.variance[0]),q=Math.sqrt(S.variance[1]);o?(d=Math.sqrt(((T-1)*C**2+(L-1)*q**2)/(T+L-2)),w=d*Math.sqrt(1/T+1/L),g=1/T+1/L,b=T+L-2):(d=Math.sqrt((C**2+q**2)/2),g=2*(L*C**2+T*q**2)/(T*L*(C**2+q**2)),w=Math.sqrt(Math.sqrt(C**2/T)**2+Math.sqrt(q**2/L)**2),b=Ms([C**2/T,q**2/L],[T-1,L-1]))}let v=r?Rr(b):1,M=D(s,-N),p=(h-M)/d*v,y=r?"Hedges_g":"Cohens_d";if(c===null)return{name:y,value:p,warnings:m};let x=a==="two.sided"?c:2*c-1,E=(h-M)/w,R=Gf(E,b,(1+x)/2)*Math.sqrt(g)*v,A=Gf(E,b,(1-x)/2)*Math.sqrt(g)*v;return a==="less"&&(R=-1/0),a==="greater"&&(A=1/0),{name:y,value:p,ci:c,ciLow:R,ciHigh:A,warnings:m}}function N0(e,t,n={}){return K(n,p0,"cohensD"),X("cohensD",Jf(e,t,n,n.adjust??!1))}function M0(e,t,n={}){return K(n,d0,"hedgesG"),X("hedgesG",Jf(e,t,n,!0))}var E0=e=>Array.isArray(e.effects),Li=e=>e.multistratum===!0,em=e=>Array.isArray(e.strata),tm=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Ci=e=>({test:t=>t.split(":").includes(e)});function x0(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let s=o.stratum.replace(/^Error: /,""),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 R0(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],d=e.within.filter(b=>Ci(b).test(m)),w=o(d.length===0?"Subject":`Subject:${d.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[a],ss:h[n]};w.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 nm(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>Ci(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>Ci(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function Xf(e,t,n,r){let o=i=>Tn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:yr(o,0,Math.max(10,e*t*4),{...Zf,extendInt:"downX"}).root:0}function rm(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=Xf(c,t,n,(1+s)/2),l=Xf(c,t,n,(1-s)/2);c<=Yn((1-s)/2,t,n)&&(l=0),c<=Yn((1+s)/2,t,n)&&(a=0);let f=a===1/0?1:a/(a+n),m=l===1/0?1:l/(l+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}function A0(e){let t=Se([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=D(n.ss,-t)}var Qf="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Fi(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Qf);if(Li(e)||em(e)||tm(e))return S0(e,t,n);let r=E0(e)?rt(()=>Ii(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Qf);let o=Ut.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;Oi(i);let c=Te(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,A)=>({name:R,df:s.table[A][a],ss:s.table[A][l]})).filter(R=>R.name!=="(Intercept)");A0(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,d=m.ss,w=d/h,g=f.reduce((R,A)=>R+A.ss,0),b=f.reduce((R,A)=>R+A.df,0)+1,N=t==="Eta2"?n.generalized??!1:!1,M=u.length===1?"plain":N!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:p,ssObserved:y}=nm(N,u),x=R=>{let{ss:A,df:S}=R;if(t==="Eta2")return M==="partial"?A/(A+d):M==="plain"?A/g:A/(A+d+y-(p(R.name)?A:0));if(t==="Omega2"){let L=M==="partial"?(A-S*w)/(A+(b-S)*w):(A-S*w)/(g+w);return Math.max(0,L)}let T=M==="partial"?(A-S*w)/(A+d):(A-S*w)/g;return Math.max(0,T)},E=`${t}${M==="plain"?"":`_${M}`}`;return{colNames:i===null?[E]:[E,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>i===null?[x(R)]:rm(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function S0(e,t,n){let r=n.ci===void 0?.95:n.ci;Oi(r);let o=Te(n.alternative??"greater",["two.sided","less","greater"]),s=tm(e),i,c,a;if(s){let p=Ut.get(e),y=R0(p!==void 0&&!Array.isArray(p)?{...e,univariate:p}:e);({strata:i,subject:c}=y),a=y.order.map(x=>({term:x,stratum:y.strata.find(E=>E.terms.includes(x))}))}else{let p=Li(e)?rt(()=>_i(e)):e,y=Ut.get(p);({strata:i,subject:c}=x0(Array.isArray(y)?{...p,strata:y.map((x,E)=>({...p.strata[E],...x}))}:p)),a=i.flatMap(x=>x.terms.map(E=>({term:E,stratum:x})))}let l=Se([[...i.flatMap(p=>p.terms.map(y=>y.ss)),...i.map(p=>p.sse)]]).k;for(let p of i){p.sse=D(p.sse,-l);for(let y of p.terms)y.ss=D(y.ss,-l)}let f=i.flatMap(p=>p.terms),m=i.reduce((p,y)=>p+y.sse,0),u=f.reduce((p,y)=>p+y.ss,0)+m,h=c.sse/c.dfe,d=t==="Eta2"?n.generalized??!1:!1,w=d!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:b}=nm(d,f),N=(p,y)=>{let{ss:x,df:E}=p,R=y.sse/y.dfe;if(t==="Eta2")return w==="partial"?x/(x+y.sse):w==="plain"?x/u:x/(x+m+b-(g(p.name)?x:0));if(t==="Omega2"){let S=y===c?0:1,T=w==="partial"?(x-E*R)/(x+S*y.sse+c.sse+h):(x-E*R)/(u+h);return Math.max(0,T)}let A=w==="partial"?(x-E*R)/(x+y.sse):(x-E*R)/u;return Math.max(0,A)},v=`${t}${w==="plain"?"":`_${w}`}`,M={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(p=>p.term.name),table:a.map(({term:p,stratum:y})=>{let x=N(p,y);return r===null?[x]:rm(x,p.df,y.dfe,r,o)}),warnings:[]};return s||(M.groups=a.map(p=>p.stratum.name),M.warnings=[...Li(e)||em(e)?e.warnings:[]]),M}function T0(e,t={}){return K(t,g0,"etaSquared"),X("etaSquared",Fi(e,"Eta2",t),e)}function k0(e,t={}){return K(t,w0,"omegaSquared"),X("omegaSquared",Fi(e,"Omega2",t),e)}function q0(e,t={}){return K(t,b0,"epsilonSquared"),X("epsilonSquared",Fi(e,"Epsilon2",t),e)}var I0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),_0=["m1","sd1","n1","m2","sd2","n2","r12"],L0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),C0="Equivalence interval does not include zero.",O0="Warning: setting bound type to SMD produces biased results!",F0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function om(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 yr(s=>an(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function P0(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(E,R,A)=>(1-(A-2)/(A*E*E))*R*R,i,c,a,l,f,m,u,h,d,w,g,b,N=t.biasCorrection;if(e.design==="two"){let{n1:E,n2:R,s1:A,s2:S}=e,T=A*A,L=S*S,C=2*E*R/(E+R);if(t.glass!==void 0){let q=t.glass==="glass1",[O,_,F,I]=q?[A,E,S,R]:[S,R,A,E];c=O,a=_-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Rr(a):1,f=l*o/c,d=1/R+1/E;let P=F/O;m=Math.sqrt(P*P/(I-1)+1/(_-1)+f*f/(2*(_-1))),u=o/(O*Math.sqrt(1/_+P*P/I)),h=E+R-2,w=Math.sqrt(2/C)}else if(t.varEqual)c=Math.sqrt(((E-1)*T+(R-1)*L)/(E+R-2)),a=E+R-2,i=N?"Hedges's g":"Cohen's d",l=N?Rr(a):1,f=l*o/c,d=1/E+1/R,m=Math.sqrt(1/E+1/R+s(l,f,a)),u=o/(c*Math.sqrt(d)),h=a,w=Math.sqrt(2*(R*T+E*L)/(E*R*(T+L)));else{c=Math.sqrt((T+L)/2),a=(E-1)*(R-1)*(T+L)**2/((R-1)*T*T+(E-1)*L*L),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Rr(a):1,f=l*o/c,d=2*(R*T+E*L)/(E*R*(T+L));let q=c**4;m=Math.sqrt(f*f*(T*T/(E-1)+L*L/(R-1))/(8*q)+(T/(E-1)+L/(R-1))/(c*c)),u=o/Math.sqrt(T/E+L/R),h=a,w=Math.sqrt(2/C)}g=a,b=(q,O)=>Math.sqrt(g/(g-2)*(2/C)*(1+q*q*C/2)-q*q/(O*O))}else if(e.design==="paired"){let{n:E,s1:R,s2:A,sdif:S,oneMinusR:T}=e,L=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?R:A:c=t.rmCorrection?S/Math.sqrt(2*T):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=E-1,g=2*E-2,l=N?Rr(r==="goulet"?g:a):1,f=l*o/c,d=1/E,u=o*Math.sqrt(E)/c,h=a,w=Math.sqrt(2*T/E),b=(C,q)=>{let O=r==="goulet"?g:a,_=Math.sqrt(O/(O-2)*(2*T/E)*(1+C*C*E/(2*T))-C*C/(q*q));return t.rmCorrection?_:_*Math.sqrt(2*T)},t.glass!==void 0){let C=S/c;m=Math.sqrt(C*C/(E-1)+f*f/(2*(E-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/E+s(l,f,a)):m=b(f,l)}else{let{n:E,s:R}=e;c=R,a=E-1,g=a,i=N?"Hedges's g":"Cohen's d",l=N?Rr(a):1,f=l*o/c,d=1/E,m=Math.sqrt(1/E+f*f/(2*E)),u=o*Math.sqrt(E)/R,h=a,w=1/Math.sqrt(E),b=(A,S)=>Math.sqrt(a/(a-2)*(1/E)*(1+A*A*E)-A*A/(S*S))}let v,M,p,y=a;if(r==="nct"){p=m,y=h;let E=om(u,h,n,!0),R=om(u,h,n,!1);v=E*Math.sqrt(d)*l,M=R*Math.sqrt(d)*l}else if(r==="goulet"){p=b(f,l),y=g;let E=f/w;v=Wt(n,g,{ncp:E})*w,M=Wt(n,g,{ncp:E,lowerTail:!1})*w}else{p=m;let E=r==="t"?bt(n,a,!1,!1):le(n,{lowerTail:!1});v=f-E*p,M=f+E*p}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:p,confInt:x?[-M,-v]:[v,M],df:y,J:l}}function sm(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 am(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 im(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 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 cm(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,d=P0(c,l),w=D(d.denom,a),g=l.smdBounds?[f[0]*w,f[1]*w]:f,b=F=>{if(F===0)return[0,n];let I=Math.floor(Math.log2(Math.abs(F))),P=Le(F,-I);return l.smdBounds?[P*d.denom,I+a]:[P,I]},N=l.smdBounds?f:[f[0]/w,f[1]/w];l.smdBounds&&m.push(O0),g[0]<=0&&g[1]>=0||m.push(C0);let v=F=>D(F/r,n-o),M=F=>{let[I,P]=b(F),k=Math.max(n,P);return Le((Le(t,n-k)-Le(I,P-k))/r,k-o)},p=v(t),y=M(f[0]),x=M(f[1]),E=D(r,o),R=h==="EQU",A={t:p,se:E,df:s,pValue:2*ft(-Math.abs(p),s,!0,!1)},S={t:y,se:E,df:s,pValue:ft(y,s,!R,!1)},T={t:x,se:E,df:s,pValue:ft(x,s,R,!1)},L=R?Math.max(S.pValue,T.pValue):Math.min(S.pValue,T.pValue);!R&&g[0]<=0&&g[1]>=0&&L<=A.pValue&&m.push(F0);let C=bt(u,s,!1,!1),q=Math.max(n,o),[O,_]=[D(t,n-q),D(r,o-q)];return X(e,{tTest:A,lower:S,upper:T,pValue:L,bounds:{raw:g,smd:N},raw:{estimate:D(t,n),se:E,confInt:[D(O-C*_,q),D(O+C*_,q)]},smd:{label:d.label,estimate:d.estimate,se:d.se,confInt:d.confInt,df:d.df,J:d.J},confLevel:1-2*u,method:i,hypothesis:h,warnings:m})}function D0(e,t,n={}){K(n,I0,"tTOST");let r=im(n),o=am(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");sm(e,"x"),a||sm(t,"y");let l=rt(()=>a?En(e,{mu:c}):En(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,d;if(a){let w=Se([Dt(Array.from(e))]);u=w.k;let g=w.groups[0],b=st(g);h=it(g)-D(c,-u),d=Math.sqrt(b/g.length),m={design:"one",n:g.length,delta:h,s:Math.sqrt(b)}}else if(s){let[w,g]=Zn(Array.from(e),Array.from(t)),b=Se([w,g]);u=b.k;let[N,v]=b.groups,M=N.map((A,S)=>A-v[S]),p=Math.sqrt(st(N)),y=Math.sqrt(st(v)),x=st(M),E=Math.sqrt(x),R=(E-(p-y))*(E+(p-y))/(2*p*y);h=it(M)-D(c,-u),d=Math.sqrt(x/M.length),m={design:"paired",n:M.length,delta:it(N)-it(v)-D(c,-u),s1:p,s2:y,sdif:E,oneMinusR:R}}else{let w=Se([Dt(Array.from(e)),Dt(Array.from(t))]);u=w.k;let[g,b]=w.groups;if(g.length<2||b.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,v,M,p]=[g.length,b.length,st(g),st(b)];h=it(g)-it(b)-D(c,-u),d=Math.sqrt(i?((N-1)*M+(v-1)*p)/(N+v-2)*(1/N+1/v):Math.sqrt(M/N)**2+Math.sqrt(p/v)**2),m={design:"two",n1:N,n2:v,delta:h,s1:Math.sqrt(M),s2:Math.sqrt(p)}}return cm("tTOST",h,u,d,u,f,l.method,m,u,r,o,[])}function V0(e,t={}){K(e,_0,"tsumTOST"),K(t,L0,"tsumTOST");let n=im(t),r=am(t),{m1:o,sd1:s,n1:i,m2:c,sd2:a,n2:l,r12:f}=e,m=(q,O)=>{if(typeof q!="number"||!Number.isFinite(q))throw new RangeError(`'${O}' must be a finite number`);return q},u=(q,O)=>{let _=m(q,O);if(!(Number.isInteger(_)&&_>=2))throw new RangeError(`'${O}' must be an integer of at least 2`);return _},h=(q,O)=>{let _=m(q,O);if(!(_>0))throw new RangeError(`'${O}' must be positive`);return _};m(o,"m1"),h(s,"sd1"),u(i,"n1");let d=c===void 0&&a===void 0&&l===void 0;if(n.paired&&d)throw new TypeError("'m2', 'sd2' and 'n2' are required when paired is true");d||(m(c,"m2"),h(a,"sd2"),u(l,"n2"));let w=[],{groups:g,k:b}=Se([[o,...d?[]:[c],n.mu]]),{groups:N,k:v}=Se([[s,...d?[]:[a]]]),[M,p=NaN]=d?[g[0][0]]:[g[0][0],g[0][1]],y=g[0][g[0].length-1],[x,E=NaN]=N[0],R=q=>D(q,b-v),A,S,T,L,C;if(d)A=M-y,S=x/Math.sqrt(i),T=i-1,L="One Sample t-test",C={design:"one",n:i,delta:R(A),s:x};else if(n.paired){let q=m(f,"r12");if(!(q>-1&&q<1))throw new RangeError("'r12' must be in (-1, 1)");let O=i;i!==l&&(w.push("Unequal number of pairs; using smallest n"),O=Math.min(i,l));let _=Math.sqrt(x*x+E*E-2*q*x*E);if(!(_>0))throw new RangeError("the standard deviation of the differences is 0");A=M-p-y,S=_/Math.sqrt(O),T=O-1,L="Paired t-test",C={design:"paired",n:O,delta:R(A),s1:x,s2:E,sdif:_,oneMinusR:1-q}}else{let q=l,O=x*x,_=E*E;if(A=M-p-y,n.varEqual){let F=((i-1)*O+(q-1)*_)/(i+q-2);S=Math.sqrt(F*(1/i+1/q)),T=i+q-2,L=" Two Sample t-test"}else{let F=O/i,I=_/q;S=Math.sqrt(F+I),T=(F+I)**2/(F*F/(i-1)+I*I/(q-1)),L="Welch Two Sample t-test"}C={design:"two",n1:i,n2:q,delta:R(A),s1:x,s2:E}}return cm("tsumTOST",A,b,S,v,T,L,C,v,n,r,w)}var $0=["alternative","tsmethod"],z0=["square","central"],W0=1e3,j0=32,K0=.9,lm=1+1e-7,U0=2**24,B0=34e7;function H0(e,t={}){if(K(t,$0,"boschlooTest"),e.length!==2||e.some(y=>y.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ze(t.alternative),r=Te(t.tsmethod,z0),o=e.map((y,x)=>Array.from(y,(E,R)=>Jr(E,`table[${x}][${R}]`))),[[s,i],[c,a]]=o,l=s+i,f=c+a;if(l===0||f===0)throw new RangeError("each row (group) of 'table' must have at least one observation");if((l+1)*(f+1)>U0)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(W0,Math.ceil(j0*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>B0)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let d=s/l-c/f,w;n!=="two.sided"?w=n:r==="square"?w="square":w=d>0?"greater":"less";let g=s+c,b;w==="less"?b=cn(s,l,f,g,!0,!1):w==="greater"?b=cn(s-1,l,f,g,!1,!1):b=rt(()=>$o(o)).pValue;let N=Y0(l,f,s,c,w),{value:v,at:M}=G0(N,l,f,u),p=n==="two.sided"&&r==="central"?Math.min(1,2*v):v;return X("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:b,statisticName:"test statistic",pValue:p,estimate:d,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:M,warnings:[]})}function Y0(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let b=2;b<=s;b++)i[b]=xs(b);let c=t+1,a=new Float64Array((e+1)*c),l=Math.min(e,t)+1,f=new Float64Array(l),m=new Float64Array(l),u=new Float64Array(l);for(let b=0;b<=s;b++){let N=Math.max(0,b-t),M=Math.min(b,e)-N+1,p=-1/0;for(let E=0;E<M;E++){let R=N+E,A=-i[R]-i[e-R]-i[b-R]-i[t-b+R];f[E]=A,A>p&&(p=A)}let y=0;for(let E=0;E<M;E++)f[E]=Math.exp(f[E]-p),y+=f[E];for(let E=0;E<M;E++)f[E]=f[E]/y;let x=0;for(let E=0;E<M;E++)m[E]=x+=f[E];x=0;for(let E=M-1;E>=0;E--)u[E]=x+=f[E];if(o==="less")for(let E=0;E<M;E++)a[(N+E)*c+b-N-E]=m[E];else if(o==="greater")for(let E=0;E<M;E++)a[(N+E)*c+b-N-E]=u[E];else{let E=0;for(let R=1;R<M;R++)f[R]>f[E]&&(E=R);for(let R=0;R<M;R++){let A=f[R]*lm,S=-1,T=E;for(;T-S>1;){let O=S+T>>1;f[O]<=A?S=O:T=O}let L=E,C=M;for(;C-L>1;){let O=L+C>>1;f[O]<=A?C=O:L=O}let q=f[E]<=A?1:(S>=0?m[S]:0)+(C<M?u[C]:0);a[(N+R)*c+b-N-R]=Math.min(1,q)}}}let h=a[n*c+r]*lm,d=new Int32Array(e+2),w=[],g=[];for(let b=0;b<=e;b++){d[b]=w.length;let N=0;for(;N<=t;){if(a[b*c+N]<=h){let v=N;for(;N+1<=t&&a[b*c+N+1]<=h;)N++;w.push(v),g.push(N)}N++}}return d[e+1]=w.length,{starts:d,from:Int32Array.from(w),to:Int32Array.from(g)}}function fm(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=Ft(1,-t),s=at([t,0],o),i=at(o,[t,0]),c=s[0],a=i[0],l=r;for(let h=r+1;h<=e;h++){let d=n[h-1]*((e-h+1)/h*c);if(d===0)break;n[h]=d,l=h}let f=r;for(let h=r-1;h>=0;h--){let d=n[h+1]*((h+1)/(e-h)*a);if(d===0)break;n[h]=d,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 G0(e,t,n,r){let{starts:o,from:s,to:i}=e,c=new Float64Array(t+1),a=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=v=>{let[M,p]=fm(t,v,c),[y,x]=fm(n,v,a),E=0;for(let A=y;A<=x;A++)l[A]=E+=a[A];E=0;for(let A=x;A>=y;A--)f[A]=E+=a[A];let R=0;for(let A=M;A<=p;A++){let S=c[A];if(S===0)continue;let T=0;for(let L=o[A];L<o[A+1];L++){let C=s[L],q=i[L];if(!(q<y||C>x))if(C===0)T+=l[Math.min(q,x)];else if(q===n)T+=f[Math.max(C,y)];else for(let O=Math.max(C,y);O<=Math.min(q,x);O++)T+=a[O]}R+=S*T}return Math.min(1,R)},u=Math.PI/2,h=v=>v<=0?0:v>=u?1:Math.sin(v)**2,d=new Float64Array(r+2),w=0;for(let v=0;v<=r+1;v++)d[v]=m(h(u*v/(r+1))),d[v]>d[w]&&(w=v);let g=d[w],b=h(u*w/(r+1));if(g>=1)return{value:1,at:b};let N=K0*g;for(let v=0;v<=r+1;v++){let M=d[v];if(M<N||v>0&&!(M>d[v-1])||v<=r&&!(M>=d[v+1]))continue;let p=u*Math.max(0,v-1)/(r+1),y=u*Math.min(r+1,v+1)/(r+1),E=Vl(S=>-m(h(S)),p,y,1e-14).x,R=m(h(E)),A=1e-4/Math.sqrt(2*(t+n));for(let S=0;S<2&&(E-A>p&&E+A<y);S++,A*=.01){let T=m(h(E-A)),L=m(h(E+A)),C=T-2*R+L;if(!(C<0))break;let q=A*(T-L)/(2*C);if(!(Math.abs(q)<A))break;let O=m(h(E+q));O>R*(1+2**-50)&&(E+=q,R=O)}R>g&&(g=R,b=h(E))}return{value:g,at:b}}function vn(e){let t=kt(e);return t.some(n=>typeof n=="string")?t.map(n=>typeof n=="number"&&!Number.isNaN(n)?ut(n):n):t}function Ar(e,t){return typeof e=="number"&&typeof t=="number"?e-t:wn(String(e),String(t))}function or(e,t,n="drop"){if(t===void 0){let c=e;if(c.length>0&&typeof c[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let a=c.map(m=>Array.from(m).filter(u=>!Number.isNaN(u)));if(n==="error"&&a.some(m=>m.length===0))throw new RangeError("all groups must contain data");let l=[],f=[];return a.forEach((m,u)=>{m.length!==0&&(l.push(String(u+1)),f.push(m))}),{labels:l,groups:f}}let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=vn(t),s=new Map;for(let c=0;c<r.length;c++){let a=r[c],l=o[c];if(Number.isNaN(a)||typeof l=="number"&&Number.isNaN(l))continue;let f=s.get(l);f===void 0&&s.set(l,f=[]),f.push(a)}let i=Array.from(s.keys()).sort(Ar);return{labels:i.map(ut),groups:i.map(c=>s.get(c))}}function Sr(e,t,n="drop"){return or(e,t,n).groups}function na(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:tn(t[n/2-1],t[n/2])}function Pi(e){if(e.length===0)throw new RangeError("'y' must have at least one row");let t=e[0].length;if(e.some(n=>n.length!==t))throw new RangeError("all rows of 'y' must have the same length");return e.filter(n=>n.every(r=>!Number.isNaN(r))).map(n=>Array.from(n))}var mm=["correct"];function X0(e,t,n={}){vt(t)&&K(t,mm,"mcnemarTest"),K(n,mm,"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=Q0(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=um(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+=um(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:yt(i,a/2,2,!1,!1),warnings:[]}}function um(e,t,n){let r=t+n;return r<1/0?Vt(e,r):Vt(D(e,-520),D(t,-1040)+D(n,-1040))}function Q0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=vn(e),o=vn(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(Ar),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 Z0=["ratio","alternative","confLevel"],Di=2**-1022;function hm(e,t,n={}){if(K(n,Z0,"varTest"),typeof e=="string"){let v=er(e,t);return hm(v.x,v.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=n.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let i=Ze(n.alternative),c=n.confLevel??.95;Lt(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]=Pt([r,o]).variance,u=0;if(!(f>=Di&&m>=Di)){let v=Pt([r]),M=Pt([o]);f=v.variance[0],m=M.variance[0],u=2*(v.k-M.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:Le(f/m,u),d=h/s,w=dt(d,a,l,!0,!1),g=dt(d,a,l,!1,!1),b,N;if(i==="less")b=w,N=[0,h/pn(1-c,a,l,!0,!1)];else if(i==="greater")b=g,N=[h/pn(c,a,l,!0,!1),1/0];else{b=Math.min(1,2*Math.min(w,g));let v=(1-c)/2,M=v<1e-8?pn(v,a,l,!1,!1):pn(1-v,a,l,!0,!1);N=[h/M,h/pn(v,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:d,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:b,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function pm(e,t){if(typeof e=="string"){let d=Lo(e,t);return pm(Pl(d.values,d.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(d=>Array.from(d).filter(Number.isFinite));if(o.some(d=>d.length<2))throw new RangeError("there must be at least 2 observations in each group");let s=o.map(d=>d.length-1),i=Pt(o).variance,c=0,a=0,l=0,f=0;for(let d=0;d<r;d++)c+=s[d],a+=s[d]*i[d],l+=s[d]*Math.log(i[d]),f+=1/s[d];a/=c;let m=c*Math.log(a)-l;if(i.some(d=>d<Di)){let d=o.map(M=>{let p=Pt([M]);return{w:p.variance[0],e:p.k}}),w=Math.max(...d.map(M=>M.e)),g=d.map(M=>Math.log(M.w)+2*(M.e-w)*Math.LN2),b=g.map((M,p)=>Math.log(s[p])+M),N=Math.max(...b),v=N+Math.log(b.reduce((M,p)=>M+Math.exp(p-N),0))-Math.log(c);m=c*v-g.reduce((M,p,y)=>M+s[y]*p,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:yt(u,h/2,2,!1,!1),warnings:[]}}var J0=["alternative"];function Vi(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:yt(n,r/2,2,!1,!1),warnings:[]}}function dm(e){let t=0;for(let n of yn(e))t+=n*n*n-n;return t}function ra(e,t){if(typeof e=="string"){let l=Lo(e,t);return ra(l.values,l.group.codes)}let n=Sr(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=Nt(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-dm(o)/(s*s*s-s)),Vi("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function $i(e,t){if(typeof e=="string")return $i(Ua(e,t));let n=Pi(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=Nt(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=dm(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 Vi("Friedman rank sum test","Friedman chi-squared",a,o-1)}function eg(e){if(e.some(t=>t.some(n=>!Number.isNaN(n)&&n!==0&&n!==1)))throw new RangeError("the responses must be 0 or 1 (or NaN)");return{...$i(e),method:"Cochran's Q test",statisticName:"Q"}}function gm(e,t){if(typeof e=="string")return gm(Ua(e,t));let n=Pi(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(w=>gt(w)-ln(w));s.some(w=>w===1/0)&&n.every(w=>w.every(Number.isFinite))&&(s=Se(n).groups.map(w=>gt(w)-ln(w)));let i=Nt(s),c=new Array(o).fill(0),a=0;n.forEach((w,g)=>{let b=Nt(w);for(let N=0;N<o;N++){let v=i[g]*(b[N]-(o+1)/2);a+=v*v,c[N]+=v}});let l=0;for(let w of c)l+=w*w;l/=r;let f=o-1,m=(r-1)*(o-1),u,h,d=[f,m];if(a===l){u=NaN,d=[NaN,NaN];let w=0;for(let g=2;g<=o;g++)w+=Math.log(g);h=Math.exp((1-r)*w)}else u=(r-1)*l/(a-l),h=dt(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:d,parameterName:["num df","denom df"],pValue:h,warnings:[]}}function wm(e,t){if(typeof e=="string"){let h=Lo(e,t);return wm(h.values,h.group.codes)}let n=Sr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=h=>h.flatMap(d=>{let w=na(d);return d.map(g=>Math.abs(g-w))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(h=>h===1/0)&&n.every(h=>h.every(Number.isFinite))&&(s=o(Se(n).groups));let i=s.length,a=Nt(s).map(h=>le((1+h/(i+1))/2)),l=0;for(let h of a)l+=h;let f=l/i,m=0,u=0;for(let h of n){let d=0;for(let w=0;w<h.length;w++)d+=a[u+w];u+=h.length,m+=h.length*(d/h.length-f)**2}return m/=st(a),Vi("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function bm(e,t,n={}){if(K(n,J0,"moodTest"),typeof e=="string"){let M=er(e,t);return bm(M.x,M.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ze(n.alternative),i=r.length,c=o.length,a=i+c;if(a<3)throw new RangeError("not enough observations");if(i===0||c===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:a},(M,p)=>p).sort((M,p)=>l[M]-l[p]),m=new Array(a),u=0;for(;u<a;){let M=u;for(;M+1<a&&l[f[M+1]]===l[f[u]];)M++;let p=0;for(let y=u;y<=M;y++)p+=(y+1-(a+1)/2)**2;p/=M-u+1;for(let y=u;y<=M;y++)m[f[y]]=p;u=M+1}let h=0;for(let M=0;M<i;M++)h+=m[M];let d=0;for(let M of m)d+=M;d/=a;let w=0;for(let M of m)w+=(M-d)**2;let g=i*d,b=i*c/(a*(a-1))*w,N=(h-g)/Math.sqrt(b),v=s==="less"?se(N):s==="greater"?se(N,{lowerTail:!1}):2*se(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:v,warnings:[]}}function Tr(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var tg=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],ng=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],rg=[.544,-.39978,.025054,-6714e-7],og=[1.3822,-.77857,.062767,-.0020322],sg=[-1.5861,-.31082,-.083751,.0038915],ag=[-.4803,-.082676,.0030302],ig=[-2.273,.459];function cg(e){if(!(e>=3)||e!==Math.floor(e))throw new RangeError("n must be an integer of at least 3");let t=new Float64Array(e);if(e===3)return t[0]=-Math.SQRT1_2,t[2]=Math.SQRT1_2,t;let n=Math.floor(e/2),r=new Float64Array(n),o=0;for(let m=0;m<n;m++)r[m]=-le((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),i=1/Math.sqrt(e),c=r[0]/s+Tr(tg,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Tr(ng,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 vm(e){let t=Se([e]).groups[0],n=t.length,r=cg(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 lg=1.90985931710274,fg=1.0471975511966;function ym(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=lg*(Math.asin(Math.sqrt(1-e))-fg);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Tr(ig,t);if(n>=i)return 1e-99;let c=Tr(rg,t),a=Math.exp(Tr(og,t));return se(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Tr(sg,r),s=Math.exp(Tr(ag,r));return se(n,{mean:o,sd:s,lowerTail:!1})}var xm=Math.log(2*Math.PI),Nm=2e-14,Mm=500;function mg(e){return Math.exp(-.5*e*e-.5*xm)}function ug(e){return se(e,{logP:!0})}function Rm(e,t=-1/0,n=!1){let r=t===-1/0?Ka(e,0,2,0,Nm,Mm):Ka(e,t,1,0,Nm,Mm),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 Em(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Rm(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=ug(l);continue}let f=(r[a]*i-e)/o[a],m=se(f)+se(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?se(l)-se(f):se(f,{lowerTail:!1})-se(l,{lowerTail:!1})))}return mg(i)*-Math.expm1(c)})}function kr(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,Em(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*xm-ul(o);return Math.min(1,Rm(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*Em(e*Math.exp(c),t,r)},-1/0,!0))}var hg=["pAdjustMethod"],pg=["pAdjustMethod","alternative"],dg=["alternative"],gg=["pAdjustMethod","alternative"],wg=["method","altp"],bg=["alternative"],Am="Ties are present. z-quantiles were corrected for ties.";function jn(e,t){let{labels:n,groups:r}=or(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(i=>i.length);return{labels:n,groups:r,n:s,N:s.reduce((i,c)=>i+c,0),k:o}}function zi(e){let t=e.groups.flat(),n=Nt(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 yn(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function Bo(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 vg(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=Zr(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 sa(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 Wi(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 oa=e=>se(e,{lowerTail:!1}),ji=(e,t,n)=>Rs(Math.abs(e),t,n,{lowerTail:!1});function Ki(e){let t=Pt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Ho(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Ho(e,t){if(e===0)throw new RangeError("no residual degrees of freedom");if(t===0)throw new RangeError("the pooled within-group variance is 0")}function yg(e,t,n={}){K(n,hg,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=jn(e,t),{R:s,C:i}=zi(o),c=o.N*(o.N+1)/12-i,a=Bo(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=vg(a.map(f=>f.map(m=>2*oa(m))),r);return X("kwAllPairsDunnTest",sa(o,"Dunn's all-pairs test",a,l,r,i!==0?[Am]:[]))}function Ng(e,t,n={}){K(n,pg,"kwManyOneDunnTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=jn(e,t),{R:i,C:c}=zi(s),a=s.N*(s.N+1)/12-c,l=s.n.slice(1).map((h,d)=>d+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=Sm(f,m,r,o);return X("kwManyOneDunnTest",Wi(s,"Dunn's many-to-one test",f,u,o,c!==0?[Am]:[]))}function Sm(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?kr(o,t,1/0,!0):kr(n==="greater"?o:-o,t,1/0,!1)):Zr(e.map(o=>n==="two.sided"?2*oa(Math.abs(o)):oa(n==="greater"?o:-o)),r)}function Mg(e,t,n={}){K(n,dg,"dunnettTest");let r=Ze(n.alternative),o=jn(e,t),{m:s,s2:i,df:c}=Ki(o),a=Math.sqrt(i),l=o.n.slice(1).map((h,d)=>d+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*kr(Math.abs(h),m,c,!1)):kr(r==="greater"?h:-h,m,c,!1));return X("dunnettTest",Wi(o,`Dunnett's-test for multiple
|
|
4
|
-
comparisons with one control`,f,u,"single-step"))}function
|
|
1
|
+
/*! rStats.js 1.7.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 Ts,a as Nn,c as cn,h as jr,i as xl,m as Rl,n as Kr,o as Al,s as Rs,t as Sl,u as ko,w as Ll,y as As,z as Ss}from"./chunks/chunk-YVJ4LDDE.js";import{a as Ya,e as Ga}from"./chunks/chunk-SDT3YHAG.js";import{a as qs,b as er,c as Lo,d as Dl,e as Pl,f as Ba,g as Ze,h as fn,i as Mn,j as Lt,k as Wl,l as yp,n as Os,o as Co,p as Ds}from"./chunks/chunk-PLQQ4VFN.js";import"./chunks/chunk-76FLTZVF.js";import{a as vt,b as Br,c as Ur}from"./chunks/chunk-THSV22QF.js";import{a as Ml,d as xs}from"./chunks/chunk-CZUOG2U5.js";import{a as _s,b as Ls,c as Cs,d as Qr,e as Yl,f as Gl}from"./chunks/chunk-R7F7ELS7.js";import{d as bs,e as vs,g as ys,h as Wr,m as dn,n as Dn}from"./chunks/chunk-LMRCFKVK.js";import{a as Fs,f as nt,k as jt,l as Xl}from"./chunks/chunk-VQOU7U7R.js";import"./chunks/chunk-J7ZVV4DN.js";import{a as gt,b as tn,c as ln}from"./chunks/chunk-VALULDEX.js";import"./chunks/chunk-B7XZMIQO.js";import{a as Xr,b as Ha,c as Hl}from"./chunks/chunk-KSH43GX4.js";import{b as _t,c as On}from"./chunks/chunk-BJMPSDJ7.js";import{a as Vl,c as $l,d as zl,e as yr,g as Fo}from"./chunks/chunk-PCTRVBEJ.js";import{b as Ka}from"./chunks/chunk-HSJ4OT3H.js";import{a as zn,b as Gr}from"./chunks/chunk-56AMJQ27.js";import{d as dt,e as pn,g as Tn,h as Yn,j as Gn}from"./chunks/chunk-B6KJ4RWG.js";import{c as jl,h as Kl,j as At,m as Ua,n as Is,o as Hr,p as Yr,v as Bl,x as Ul}from"./chunks/chunk-AIEPW6DX.js";import{a as Jn,b as Fl,g as ks}from"./chunks/chunk-V2Y2W7LE.js";import{A as El,B as an,C as Wt,i as yt,l as ws,x as ja,y as ft,z as bt}from"./chunks/chunk-R5BMFVIH.js";import{a as wn,c as Ol}from"./chunks/chunk-TY3IFCLI.js";import{b as Tl,c as kl,d as ql,e as Il,f as _l,i as qo,l as vp,n as X,o as rt,p as mt,q as Io}from"./chunks/chunk-JXEXOD7K.js";import{a as _o,b as $n,c as Cl,d as kt,e as ut}from"./chunks/chunk-CUT3IHMY.js";import{b as dl,c as gl}from"./chunks/chunk-RIP2RKAW.js";import{A as ul,B as hl,D as pl,M as wl,q as Gt,r as se,s as le}from"./chunks/chunk-MOJ2KIQE.js";import{A as bl,C as vl,D as Dt,E as Ns,F as yl,G as Ms,H as Nl,I as Pe,J as Nt,K as yn,L as Pt,M as Zn,N as Vt,O as Es,P as Se,Q as Wa,b as Ot,c as en,d as he,e as fl,f as De,g as _e,h as Tt,i as at,j as lt,k as ml,l as K,m as sn,p as To,q as vr,r as Pn,s as gn,t as Vn,u as P,v as Le,w as Xn,x as it,y as st,z as Qn}from"./chunks/chunk-YKNRDPIB.js";var Ql=["alternative","mu","paired","varEqual","confLevel"];function En(e,t,n={}){if(typeof e!="string"&&vt(t)&&K(t,Ql,"tTest"),K(n,Ql,"tTest"),typeof e=="string")return Np(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.varEqual??!1,a=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(fn(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[T,L]=Zn(Array.from(e),Array.from(r));({d:l,k:m}=Ns(T,L))}else{let T=Se(r===void 0?[Pt(Array.from(e))]:[Pt(Array.from(e)),Pt(Array.from(r))]);m=T.k,l=T.groups[0],f=T.groups[1]}let u=P(s,-m),h,d,w,g,b,N,v,M=l.length,p=f!==void 0&&c;if(M<1||!p&&M<2)throw new Error("not enough 'x' observations");let y=it(l),x=M>1?st(l):NaN;if(f===void 0){if(d=M-1,w=Math.sqrt(x/M),w===0||w<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/w,N=i?"Paired t-test":"One Sample t-test",g=y,b=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let T=f.length;if(T<1||!c&&T<2)throw new Error("not enough 'y' observations");if(c&&M+T<3)throw new Error("not enough observations");let L=it(f),C=T>1?st(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,g=[y,L],b=["mean of x","mean of y"],v="difference in means",c){d=M+T-2;let q=0;M>1&&(q+=(M-1)*x),T>1&&(q+=(T-1)*C),q/=d,w=Math.sqrt(q*(1/M+1/T))}else{let q=x/M,F=C/T;w=Math.sqrt(Math.sqrt(q)**2+Math.sqrt(F)**2),d=Ms([q,F],[M-1,T-1])}if(w===0||w<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(L)))throw new Error("data are essentially constant");h=(y-L-u)/w}let E=typeof g=="number"?g:g[0]-g[1],R=(T,L)=>{let C=u+(h+L*T)*w;return Number.isFinite(C)&&Math.max(Math.abs(u),Math.abs(h*w))<=16*(Math.abs(E)+T*w)?C:E+L*T*w},A,S;if(o==="less")A=ft(h,d,!0,!1),S=[-1/0,R(bt(a,d,!0,!1),1)];else if(o==="greater")A=ft(h,d,!1,!1),S=[R(bt(a,d,!0,!1),-1),1/0];else{A=2*ft(-Math.abs(h),d,!0,!1);let T=1-a,L=T/2<1e-8?bt(T/2,d,!1,!1):bt(1-T/2,d,!0,!1);S=[R(L,-1),R(L,1)]}return S=[P(S[0],m),P(S[1],m)],g=typeof g=="number"?P(g,m):g.map(T=>P(T,m)),w=P(w,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:d,parameterName:"df",pValue:A,confInt:S,confLevel:a,estimate:g,estimateNames:b,nullValue:s,nullValueName:v,stdErr:w,stdError:w,warnings:[]}}function Np(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=qs(e,t,!0);if(r.kind==="one")return En(r.x,n);if(r.kind==="paired")return En(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...En(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${s}`],nullValueName:`difference in means between group ${o} and group ${s}`}}var Mp=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),Ep=["n","p1","p2","sigLevel","power","alternative","strict","tol"],xp=["groups","n","betweenVar","withinVar","sigLevel","power"],Jl=Number.EPSILON**.25;function Te(e,t){if(e===void 0)return t[0];let n=t.find(o=>o===e);if(n!==void 0)return n;let r=t.filter(o=>o.startsWith(e));if(e.length===0||r.length!==1)throw new RangeError(`'arg' should be one of ${t.map(o=>`\u201C${o}\u201D`).join(", ")}`);return r[0]}var Ne=e=>e==null;function mn(e,t,n){if(!Ne(e)&&!t(e))throw new RangeError(n)}var Oo=e=>e>=0&&e<=1,Ps=e=>e>0&&e<1/0;function nn(e,t){if(e.filter(Ne).length!==1)throw new RangeError(t)}function Ye(e,t,n,r={}){return zl(e,t,n,r).root}var Nr=(e,t,n,r)=>an(e,t,{ncp:n,lowerTail:r});function Xa(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Qa="'sig.level' must be numeric in [0, 1]";function Rp(e={}){K(e,Mp,"powerTTest");let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,s=e.sigLevel===void 0?.05:e.sigLevel;nn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Xa(s,Qa),mn(r,Oo,"'power' must be a probability"),mn(o,Ps,"'sd' must be a positive finite number"),mn(t,w=>w>=2,"number of observations in each group must be at least 2"),mn(n,w=>!Number.isNaN(w),"'delta' must not be NaN"),mn(e.tol,Ps,"'tol' must be a positive finite number");let i=Te(e.type,["two.sample","one.sample","paired"]),c=Te(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Jl,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!Ne(n)&&(n=Math.abs(n));let u=(w,g,b,N)=>{let v=Math.max(1e-7,w-1)*f,M=Wt(N/m,v,{lowerTail:!1}),p=Math.sqrt(w/f)*g/b;Number.isFinite(p)||(p=Math.sqrt(w/f)*(g/b));let y=Nr(M,v,p,!1);return a&&m===2?y+Nr(-M,v,p,!0):y};if(Ne(r))r=u(t,n,o,s);else{let w=r;if(Ne(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=Ye(g=>u(g,n,o,s)-w,2,1e7,{tol:l,extendInt:"upX"})}else if(Ne(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let g=Math.abs(n);o=Ye(b=>u(t,n,b,s)-w,g*1e-7,g*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else Ne(n)?n=Ye(g=>u(t,g,o,s)-w,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ye(g=>u(t,n,o,g)-w,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let h=i==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":i==="two.sample"?"n is number in *each* group":void 0,d={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[i]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:s,power:r,alternative:c,...h===void 0?{}:{note:h},method:d,warnings:[]}}function Ap(e={}){K(e,Ep,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;nn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Xa(s,Qa),mn(o,Oo,"'power' must be a probability"),mn(n,Oo,"'p1' must be a probability"),mn(r,Oo,"'p2' must be a probability"),mn(t,u=>u>0,"number of observations in each group must be positive"),mn(e.tol,Ps,"'tol' must be a positive finite number");let i=Te(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Jl,l=i==="two.sided"?2:1,f=(u,h,d,w)=>{let g=le(w/l,{lowerTail:!1});if(c&&l===2){let b=(h+d)/2,N=h*(1-h),v=d*(1-d),M=b*(1-b),p=Math.abs(h-d),y=Math.sqrt(N+v);return se((Math.sqrt(u)*p-g*Math.sqrt(2*M))/y)+se((Math.sqrt(u)*p+g*Math.sqrt(2*M))/y,{lowerTail:!1})}return se((Math.sqrt(u)*Math.abs(h-d)-g*Math.sqrt((h+d)*(1-(h+d)/2)))/Math.sqrt(h*(1-h)+d*(1-d)))},m=[];if(Ne(o))o=f(t,n,r,s);else{let u=o;if(Ne(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=Ye(h=>f(h,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(Ne(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=Ye(h=>f(t,h,r,s)-u,0,r,{tol:a,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")}else if(Ne(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=Ye(h=>f(t,n,h,s)-u,n,1,{tol:a,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")}else s=Ye(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return X("powerPropTest",{n:t,p1:n,p2:r,sigLevel:s,power:o,alternative:i,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var Zl=1e16,Vs=(e,t)=>e>Zl&&e<1/0&&t>Zl;function $s(e,t,n,r){let o=le(r,{lowerTail:!1}),s=n/e,i=Math.sqrt(2/e+2/t),c=Math.hypot(Math.sqrt(2*(1+2*s)/e),Math.sqrt(2/t)*(1+s));return se((o*i-s)/c,{lowerTail:!1})}function Sp(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,i,c,a]=[o(e),o(t),o(n),o(r)];return P(P(e,-s)*P(t,-i)*P(n,-c)/P(r,-a),s+i+c-a)}function Tp(e={}){K(e,xp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!Ne(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Ne(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Xa(i,Qa),mn(s,Oo,"'power' must be a probability"),mn(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),mn(o,Ps,"'withinVar' must be a positive finite number");let c=(a,l,f,m,u)=>{let h=(a-1)*l*f/m;h<1/0||(h=(a-1)*l*(f/m)),!(h<1/0)&&Number.isFinite(a)&&Number.isFinite(l)&&(h=Sp(a-1,l,f,m));let d=a-1,w=(l-1)*a;return h===1/0?1:Vs(d,w)?$s(d,w,h,u):Tn(Yn(u,d,w,{lowerTail:!1}),d,w,{ncp:h,lowerTail:!1})};if(Ne(s))s=c(t,n,r,o,i);else{let a=s;if(Ne(t))t=Ye(l=>c(l,n,r,o,i)-a,2,100);else if(Ne(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=Ye(l=>c(t,l,r,o,i)-a,2,1e5)}else if(Ne(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=Ye(l=>c(t,n,r,l,i)-a,r*1e-7,r*1e7)}else Ne(r)?r=Ye(l=>c(t,n,l,o,i)-a,o*1e-7,o*1e7):i=Ye(l=>c(t,n,r,o,l)-a,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:i,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var kp=["correct","p","rescaleP"],qp=["confLevel"],Ip=["alternative","rowScores","colScores"];function Xt(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Ja(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 ei=e=>e.length>0&&typeof e[0]!="number",ti=(e,t)=>Number.isNaN(e)?NaN:yt(e,t/2,2,!1,!1);function ef(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 Do(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>ef(o,t[r][s]))):e.map((n,r)=>ef(n,t[r]))}var _p=["none","williams","yates"],nf="Log likelihood ratio (G-test)";function Lp(e,t={}){K(t,kp,"gTest");let n=Te(t.correct,_p);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(ei(e)){let o=Ja(e);return tf(o.flat()),o.length===1?X("gTest",Za(o[0],n,t)):o[0].length===1?X("gTest",Za(o.map(s=>s[0]),n,t)):X("gTest",Cp(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 tf(r),X("gTest",Za(r,n,t))}function tf(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 rf(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function of(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,hl(o,s))}return n[0]+n[1]}function Cp(e,t){let n=e.length,r=e[0].length;if(t==="yates"&&(n!==2||r!==2))throw new RangeError("Yates' correction requires a 2 x 2 matrix");let o=Xt(e.flat()),s=rf(o[0],n*r),i=s===0?e:e.map(v=>v.map(M=>P(M,-s))),c=s===0?o:Xt(i.flat()),a=i.map(v=>Xt(v)),l=i[0].map((v,M)=>Xt(i.map(p=>p[M]))),f=a.map(v=>l.map(M=>_e(v,at(M,c)))),m=i;if(t==="yates"){let[[v,M],[p,y]]=i,x=v*y-M*p>0?-.5:.5,E=P(.5,-s),R=x<0?-E:E;m=[[v+R,M-R],[p-R,y+R]]}let u=of(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],d=1;if(t==="williams"){if(a.some(y=>y[0]===0)||l.some(y=>y[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],M=a.reduce((y,x)=>y+1/(x[0]+x[1]),0),p=l.reduce((y,x)=>y+1/(x[0]+x[1]),0);d=1+P((v*M-1)*(v*p-1)/(6*v*(n-1)*(r-1)),-s)}let w=P(2*u,s)/d,g=(n-1)*(r-1),b=f.map(v=>v.map(M=>P(M[0]+M[1],s)));return{method:`${nf} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:w,statisticName:"G",parameter:g,parameterName:"X-squared df",pValue:ti(w,g),observed:s===0?m:m.map(v=>v.map(M=>P(M,s))),expected:b,ftResiduals:Do(e,b),warnings:h}}function Za(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(v=>typeof v!="number"||Number.isNaN(v)))throw new RangeError("'p' must not contain missing values");if(s.some(v=>v<0))throw new RangeError("probabilities must be non-negative");if(s.some(v=>v===1/0))throw new RangeError("probabilities must be finite");let i=Xt(s);if(o){if(!(i[0]>0&&i[0]<1/0))throw new RangeError("the probabilities to rescale must have a positive finite sum")}else if(Math.abs(i[0]-1+i[1])>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let c=Xt(e),a=rf(c[0],r),l=a===0?e:e.map(v=>P(v,-a)),f=a===0?c:Xt(l),m=s.map(v=>o?at(Tt(f,v),i):Tt(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=P(.5,-a),M=l[0]-u[0];M>P(.25,-a)?h=[l[0]-v,l[1]+v]:-M>P(.25,-a)&&(h=[l[0]+v,l[1]-v])}let d=of(h,m);if(!o&&!Number.isNaN(d)){let v=f[0]+f[1];d-=v*(i[0]-1+i[1])}let w=Number.isNaN(d)?["NaNs produced"]:[],g=t==="williams"?1+(r+1)/(6*P(f[0]+f[1],a)):1,b=P(2*d,a)/g,N=u.map(v=>P(v,a));return{method:`${nf} goodness of fit test`,statistic:b,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:ti(b,r-1),observed:a===0?h:h.map(v=>P(v,a)),expected:N,ftResiduals:Do(e,N),warnings:w}}function Fp(e){if(typeof e!="object"||e===null||!ei(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Ja(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 Op(e){let t=Fp(e),n=Xt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(p=>p.map(y=>P(y,-r))),s=o.length,i=o.map(p=>Xt(p)),c=o.map((p,y)=>Xt(o.map(x=>x[y]))),a=o.map((p,y)=>o[y].some((x,E)=>E!==y&&x>0)||o.some((x,E)=>E!==y&&x[y]>0)).flatMap((p,y)=>p?[y]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((p,y)=>y),m=p=>f[p]===p?p:f[p]=m(f[p]);for(let p=0;p<l;p++)for(let y=p+1;y<l;y++)o[a[p]][a[y]]+o[a[y]][a[p]]>0&&(f[m(p)]=m(y));if(a.some((p,y)=>m(y)!==m(0)))throw new RangeError("the categories fall into groups without counts between them: the covariance matrix is singular");let u=l-1,h=a.slice(0,u).map(p=>{let y=he(i[p],[-c[p][0],-c[p][1]]);return y[0]+y[1]}),d=a.slice(0,u).map(p=>a.slice(0,u).map(y=>{if(p!==y)return-(o[p][y]+o[y][p]);let x=0;for(let E=0;E<s;E++)E!==p&&(x+=o[p][E]+o[E][p]);return x})),w=d.map(p=>p.map(()=>0)),g=[];for(let p=0;p<u;p++){let y=d[p][p];for(let x=0;x<p;x++)y-=w[p][x]*w[p][x]*g[x];g.push(y);for(let x=p+1;x<u;x++){let E=d[x][p];for(let R=0;R<p;R++)E-=w[x][R]*w[p][R]*g[R];w[x][p]=E/y}}let b=0,N=[];for(let p=0;p<u;p++){let y=h[p];for(let x=0;x<p;x++)y-=w[p][x]*N[x];N.push(y),b+=y*y/g[p]}let v=Xt(a.flatMap(p=>a.map(y=>t[p][y]))),M=l-1;return b=P(b,r),X("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:b,statisticName:"chi-squared",parameter:M,parameterName:"df",pValue:ti(b,M),N:v[0]+v[1],warnings:[]})}function Dp(e,t={}){K(t,qp,"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=sf(e),o=Xt(r.flat())[0],s=0;(o>2**250||o>0&&o<2**-250)&&(s=2*Math.round((Math.log2(o)-250)/2));let i=s===0?r:r.map(p=>p.map(y=>P(y,-s))),{pc:c,pd:a}=Pp(i),l=[0,0],f=[0,0];i.forEach((p,y)=>p.forEach((x,E)=>{l=he(l,Tt(c[y][E],x)),f=he(f,Tt(a[y][E],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=he(l,f),u=at(De(l,f),m)[0],h=_e(m,m),d=i.map((p,y)=>p.map((x,E)=>2*at(De(_e(f,c[y][E]),_e(l,a[y][E])),h)[0])),w=Math.max(...d.flat().map(Math.abs)),g=w>0&&w<1/0?Math.floor(Math.log2(w)):0,b=0;i.forEach((p,y)=>p.forEach((x,E)=>{let R=P(d[y][E],-g);b+=x*R*R}));let N=P(Math.sqrt(b),g-s/2),v=(1-n)/2,M=v<1e-8?le(v,{lowerTail:!1}):le(1-v);return X("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-M*N),Math.min(1,u+M*N)],confLevel:n,ase:N,warnings:[]})}function sf(e){if(typeof e!="object"||e===null||!ei(e))throw new TypeError("'table' must be an array of rows");let t=Ja(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 Pp(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(d=>{let w=[],g=r;for(let b=0;b<n;b++)w.push(g),g=he(g,[d[b],0]);return w}),s=e.map(d=>{let w=new Array(n),g=r;for(let b=n-1;b>=0;b--)w[b]=g,g=he(g,[d[b],0]);return w}),i=d=>{let w=[],g=new Array(n).fill(r);for(let b=0;b<t;b++)w.push(g),g=g.map((N,v)=>he(N,d[b][v]));return w},c=d=>{let w=new Array(t),g=new Array(n).fill(r);for(let b=t-1;b>=0;b--)w[b]=g,g=g.map((N,v)=>he(N,d[b][v]));return w},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((d,w)=>d.map((g,b)=>he(g,l[w][b]))),h=f.map((d,w)=>d.map((g,b)=>he(g,m[w][b])));return{pc:u,pd:h}}function Vp(e,t={}){K(t,Ip,"lblTest");let n=Ze(t.alternative),r=sf(e),o=r.length,s=r[0].length,i=(R,A,S)=>{if(R===void 0)return Array.from({length:A},(L,C)=>C+1);let T=Array.from(R);if(T.length!==A)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(T.some(L=>typeof L!="number"||!Number.isFinite(L)))throw new RangeError(`'${S}' must be finite numbers`);return T},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Xt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(R=>R.map(A=>P(A,-f))),u=m.map(R=>Xt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,A)=>Xt(m.map(S=>S[A]))).map(R=>R[0]+R[1]),d=u.reduce((R,A)=>R+A,0);if(!(f>0||d>1))throw new RangeError("the table must have a total above 1");let w=u.reduce((R,A,S)=>R+A*c[S],0)/d,g=h.reduce((R,A,S)=>R+A*a[S],0)/d,b=c.map(R=>R-w),N=a.map(R=>R-g),v=u.reduce((R,A,S)=>R+A*b[S]*b[S],0),M=h.reduce((R,A,S)=>R+A*N[S]*N[S],0),p=0;if(m.forEach((R,A)=>R.forEach((S,T)=>p+=S*b[A]*N[T])),!(v>0&&M>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let y=p/Math.sqrt(v)/Math.sqrt(M),x=f===0?Math.sqrt(d-1)*y:P(Math.sqrt(d-P(1,-f))*y,f/2),E=n==="less"?se(x):n==="greater"?se(x,{lowerTail:!1}):2*se(-Math.abs(x));return X("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:E,warnings:[]})}var af=["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 $p(e,t,n){return vt(t)&&K(t,af,"chisqTest"),K(n,af,"chisqTest"),Array.isArray(t)?X("chisqTest",zp(e,t,n??{})):X("chisqTest",Wp(e,t??{}))}var ff="Chi-squared approximation may be incorrect";function zp(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 d=t[h];!r(u)&&!r(d)&&(o.push(u),s.push(d))});let i=[...new Set(o)].sort(wn),c=[...new Set(s)].sort(wn);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])]++),{...df(f,n),rowLevels:i,colLevels:c}}function Wp(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?ni(r[0],t):o===1?ni(r.map(s=>s[0]),t):df(r,t)}return ni(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 uf(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 hf(e,t){return Number.isNaN(e)?NaN:yt(e,t/2,2,!1,!1)}function pf(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 ni(e,t){if(t.nMinus1??!1)throw new RangeError("'nMinus1' requires a contingency table with at least two rows and columns");let n=e.length;if(e.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=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(w=>w<0))throw new RangeError("probabilities must be non-negative");if(o.some(w=>w===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let w=bn(o),g=w<1/0?0:Math.ceil(Math.log2(n)),b=g===0?o:o.map(v=>P(v,-g)),N=g===0?w:bn(b);o=b.map(v=>v/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(w=>P(w,-s)),c=s===0?r:bn(i),a=o.map(w=>c*w),l=s===0?a:a.map(w=>P(w,s));pf(l);let f=o.map((w,g)=>a[g]*(1-w)),m=P(bn(i.map((w,g)=>Vt(w-a[g],a[g]))),s),u="Chi-squared test for given probabilities",h=n-1,d;if(t.simulatePValue??!1){let w=mf(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=_o(w),h=NaN,d=uf(jp(r,o,l,w),m,w)}else d=hf(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:d,observed:e,expected:l,residuals:i.map((w,g)=>P((w-a[g])/Math.sqrt(a[g]),s/2)),stdres:i.map((w,g)=>P((w-a[g])/Math.sqrt(f[g]),s/2)),ftResiduals:Do(e,l),warnings:Number.isFinite(h)&&l.some(w=>w<5)?[ff]:[]}}function jp(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=Ll(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 Kp(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>As)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=Ss(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){Ts(e,t,c,a);let m=0;for(let u=0;u<s;u++)for(let h=0;h<o;h++){let d=n[h][u];m+=(a[h+u*o]-d)**2/d}l[f]=m}return l}function df(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=bn(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=>bn(v)),l=e[0].map((v,M)=>bn(e.map(p=>p[M]))),f=a.map(v=>l.map(M=>Es(v,M,c)));pf(f.flat());let m=e.map((v,M)=>v.map((p,y)=>p-f[M][y])),u="Pearson's Chi-squared test",h,d=(o-1)*(s-1),w,g=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),b=!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(p=>p.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let v=mf(t);u+=_o(v),h=bn(m.flatMap((p,y)=>p.map((x,E)=>Vt(x,f[y][E])))),d=NaN;let M=Kp(a.map(p=>Math.trunc(p)),l.map(p=>Math.trunc(p)),f,v);w=uf(M,h,v)}else if(b>0)u+=" with Yates' continuity correction",h=bn(m.flatMap((v,M)=>v.map((p,y)=>Vt(Math.abs(p)-b,f[M][y]))));else if(h=bn(m.flatMap((v,M)=>v.map((p,y)=>Vt(p,f[M][y])))),n){u="Pearson's 'N-1' Chi-squared test";let v=h*(c-1);h=Number.isFinite(v)?v/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:d,parameterName:"df",pValue:w??hf(h,d),observed:e,expected:f,residuals:m.map((v,M)=>v.map((p,y)=>p/Math.sqrt(f[M][y]))),stdres:Bp(e,c,a,l,f,m),ftResiduals:Do(e,f),warnings:Number.isFinite(d)&&f.some(v=>v.some(M=>M<5))?[...N,ff]:N}}function Bp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((d,w)=>d.map((g,b)=>g/Math.sqrt(o[w][b]*(1-n[w]/t)*(1-r[b]/t))));let i=e.map(d=>cf(d)),c=e[0].map((d,w)=>cf(e.map(g=>g[w]))),a=Hp(i),l=lf(i),f=lf(c),m=l.map(d=>at(d,a)),u=c.map(d=>at(d,a)),h=f.map(d=>at(d,a));return e.map((d,w)=>d.map((g,b)=>{let N=_e(i[w],u[b]),v=De([g,0],N),M=v[0]+v[1],p=_e(_e(N,m[w]),h[b]),y=p[0]+p[1];if(y>=Up||M===0)return M/Math.sqrt(y);let x=N[0]+N[1],E=m[w][0]+m[w][1],R=h[b][0]+h[b][1];return M/Math.sqrt(x)/Math.sqrt(E)/Math.sqrt(R)}))}var Up=2**-1022;function cf(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Hp(e){let t=[0,0];for(let n of e)t=he(t,n);return t}function lf(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 Yp=Math.pow(Number.EPSILON,.25);function Kt(e,t,n,r={}){let o={tol:r.tol??Yp,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),yr(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 Gp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function $o(e,t={}){K(t,Gp,"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(gl));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Xp(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 X("fisherTest",{...ed(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 X("fisherTest",{method:a+_o(f),alternative:l,pValue:Qp(s,f),warnings:i})}return X("fisherTest",{method:a,alternative:l,pValue:td(s),warnings:i})}function Xp(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: ${Cl(a)}`:null}function Qp(e,t){let n=e.filter(g=>g.some(b=>b>0)),r=e[0].map((g,b)=>b).filter(g=>e.some(b=>b[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(b=>g[b])),s=o.map(g=>g.reduce((b,N)=>b+N,0)),i=o[0].map((g,b)=>o.reduce((N,v)=>N+v[b],0)),c=s.length,a=i.length,l=[0,0];for(let g of o.flat())l=he(l,[-xs(g),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((g,b)=>g+b,0);if(m>As)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Ss(m),h=new Int32Array(c*a),d=0,w=Math.trunc(t);for(let g=0;g<w;g++){Ts(s,i,u,h);let b=0;for(let N=0;N<h.length;N++)b-=u[h[N]];b<=f&&d++}return Math.max(0,Math.min(1,(1+d)/(w+1)))}var gf=1+1e-7,Zp=16,Jp=2**24;function Po(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 Zr=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let i=this.lo;i<=this.hi;i++){let c=i-this.lo;c%Zp===0||!(s>0&&s<1/0)?(o=Nn(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=Po(o,0,n-1);for(let c=0;c<n;c++)o[c]!==0&&(o[c]=o[c]/i);return o}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)n[o]!==0&&(r=he(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?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?Po(o,s,o.length-1):Po(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*Nn(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?Po(o,s+1,o.length-1):Po(o,0,s-1))+.5*o[s]}};function oi(e,t,n){let r=e.density(n),o=r[t-e.lo]*gf,s=[0,0];for(let i of r)i<=o&&(s=he(s,[i,0]));return s[0]}function wf(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=w=>Nn(w,t,n,r,!0),c=i(e)+Math.log(gf),a=Math.min(s,Math.max(o,Math.floor((r+1)*(t+1)/(t+n+2))));for(;a<s&&i(a+1)>i(a);)a++;for(;a>o&&i(a-1)>i(a);)a--;let l=(w,g)=>{let b=w,N=g+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?b=v:N=v}return b},f=(w,g)=>{let b=w-1,N=g+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?N=v:b=v}return N},m,u;e<=a?(m=l(e,a),u=f(a+1,s)):(u=f(a,e),m=a-1<o||i(o)>c?o-1:l(o,a-1));let h=m<o?0:cn(m,t,n,r,!0,!1),d=u>s?0:cn(u-1,t,n,r,!1,!1);return h+d}function Vo(e,t){return tr(e,t)}function ed(e,t,n){let r=Ze(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[i,c],[a,l]]=e,f=i,m=Math.min(i,l)+Math.min(c,a)+1;if(m>Jp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Zr(i+a,c+l,i+c),{lo:h,hi:d}=u,w=t.midp??!1,g;w?(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===d?1:0:g=oi(u,f,o);let b;if(f===h)b=0;else if(f===d)b=1/0;else{let y=u.mean(1);b=y===f?1:Vo(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:g,estimate:b,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=y=>{if(f===d)return 1/0;if(w){if(f===h&&y>=.5)return 0;let E=u.midTail(f,1,!1);return E===y?1:Vo(R=>u.midTail(f,R,!1)-y,E<y)}let x=u.tail(f,1,!1);return x===y?1:Vo(E=>u.tail(f,E,!1)-y,x<y)},M=y=>{if(f===h)return 0;if(w){if(f===d&&y>=.5)return 1/0;let E=u.midTail(f,1,!0);return E===y?1:Vo(R=>u.midTail(f,R,!0)-y,E>y)}let x=u.tail(f,1,!0);return x===y?1:Vo(E=>u.tail(f,E,!0)-y,x>y)},p;if(r==="less")p=[0,v(1-s)];else if(r==="greater")p=[M(1-s),1/0];else{let y=(1-s)/2;p=[M(y),v(y)]}return{...N,confInt:p,confLevel:s}}var zs=[[0,0]];function kn(e){for(let t=zs.length;t<=e;t++)zs.push(he(zs[t-1],ml([t,0])));return zs[e]}function Ws(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 ri=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 td(e){let t=e.filter(v=>v.some(M=>M>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,M)=>t.some(p=>p[M]>0));if(t=t.map(v=>v.filter((M,p)=>n[p])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,M)=>t.map(p=>p[M])));let r=t.length,o=t.map(v=>v.reduce((M,p)=>M+p,0)),s=t[0].map((v,M)=>t.reduce((p,y)=>p+y[M],0)).sort((v,M)=>M-v),i=s.length,c=o.reduce((v,M)=>v+M,0),a=fl(kn(c));for(let v of o)a=he(a,kn(v));for(let v of s)a=he(a,kn(v));let l=[0,0];for(let v of t.flat())l=De(l,kn(v));let f=he(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,d=Array.from({length:i},()=>new Map),w=(v,M)=>{let p=M.join(","),y=d[v].get(p);if(y!==void 0)return y;let x=he(a,kn(M.reduce((L,C)=>L+C,0)));for(let L of M)x=De(x,kn(L));for(let L=v;L<i;L++)x=De(x,kn(s[L]));let E={R:M,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(d[v].set(p,E),v===i-1){let L=[0,0];for(let C of M)L=De(L,kn(C));return E.max=E.min=L[0],E.edges.push({to:E,hi:L[0],lo:L[1],mult:1}),E}let R=new Map,A=new Array(r+1).fill(0);for(let L=r-1;L>=0;L--)A[L]=A[L+1]+M[L];let S=new Array(r).fill(0),T=(L,C,q)=>{if(L===r-1){S[L]=C;let O=De(q,kn(C)),I=M.map((B,Q)=>B-S[Q]).sort((B,Q)=>B-Q),D=w(v+1,I),k=`${I.join(",")}|${O[0]}`,V=R.get(k);V===void 0?R.set(k,{to:D,hi:O[0],lo:O[1],mult:1}):V.mult++;return}let F=Math.max(0,C-A[L+1]),_=Math.min(M[L],C);for(let O=F;O<=_;O++)S[L]=O,T(L+1,C-O,De(q,kn(O)))};if(T(0,s[v],[0,0]),E.edges=[...R.values()],E.max=gt(E.edges.map(L=>L.hi+L.to.max)),E.min=ln(E.edges.map(L=>L.hi+L.to.min)),v===i-2){let L=E.edges.map(D=>{let k=D.to.edges[0];return{v:he([D.hi,D.lo],[k.hi,k.lo]),mult:D.mult}});L.sort((D,k)=>D.v[0]-k.v[0]||D.v[1]-k.v[1]);let C=L.length,q=L[C-1].v[0],F=new Float64Array(C),_=new Float64Array(C),O=new Float64Array(C),I=[0,0];L.forEach(({v:D,mult:k},V)=>{F[V]=D[0],_[V]=D[1];let B=De(D,[q,0]);I=he(I,[k*Math.exp(B[0])*(1+B[1]),0]),O[V]=I[0]}),E.dist={hi:F,lo:_,cum:O,vmax:q,kv:he(a,[q,0])}}return E},g=w(0,[...o].sort((v,M)=>v-M)),b=new ri,N=(v,M,p,y)=>{if(M+v.max<=u-m){let[T,L]=Ws(v.KZ[0],v.KZ[1],M,p);return b.add(T,L,y),!0}if(M+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[E,R]=Ws(u,h,-M,-p),A=0,S=x.hi.length;for(;A<S;){let T=A+S>>>1,L=x.hi[T];L<E||L===E&&x.lo[T]<=R?A=T+1:S=T}if(A>0){let[T,L]=Ws(x.kv[0],x.kv[1],M,p);b.add(T,L,y*x.cum[A-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 M of d[v].values()){let{pastHi:p,pastLo:y,count:x}=M;if(x.length!==0){for(let{to:E,hi:R,lo:A,mult:S}of M.edges)for(let T=0;T<x.length;T++){let[L,C]=Ws(p[T],y[T],R,A),q=x[T]*S;if(N(E,L,C,q))continue;let F=E.index.get(L);F===void 0?(E.index.set(L,E.count.length),E.pastHi.push(L),E.pastLo.push(C),E.count.push(q)):E.count[F]+=q}M.pastHi=M.pastLo=M.count=[],M.index.clear()}}return Math.min(1,b.value())}function js(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function ai(e){return[...e].sort((t,n)=>t-n).join(",")}function si(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 ii(e){let[,t]=js(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function ci(e){let[t,n]=js(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 si(t,0,o)}function li(e,t){let[,n]=js(e),r=n.length,o=Math.min(t,r-t),s=bf(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 bf(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function fi(e,t){let[n,r]=js(e),o=r.length,s=Math.min(t,o-t),i=bf(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 d=r[h-1];for(let w=Math.min(h,s);w>=Math.max(1,s-(o-h));w--){let g=c[w],b=c[w-1],N=w/h,v=(h-w)/h,M=i[w],p=i[h]-i[h-w],y=i[w-1]+d,x=Math.max(M,y);for(let E=p;E>=x;E--)g[E-M]=v*g[E-M]+N*b[E-y];for(let E=x-1;E>=M;E--)g[E-M]=v*g[E-M]}}let a=c[s],l=t*(t+1)/2;if(s===t)return si(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 si(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 Mr(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 vf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function zo(e,t,n={}){if(typeof e!="string"&&vt(t)&&K(t,vf,"wilcoxTest"),K(n,vf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let w=qs(e,t,!0);return w.kind==="one"?zo(w.x,n):zo(w.x,w.y,w.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=rd(n.correct??!0),l=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(l&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(i&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[w,g]=Zn(Array.from(e),Array.from(r));u=Pt(w.map((b,N)=>b-g[N]))}else u=Pt(Array.from(e)),r!==void 0&&(h=Pt(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let d={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new mt};return X("wilcoxTest",h===void 0?cd(u,c,i,d):gd(u,h,c,d))}function rd(e){if(typeof e=="boolean")return e?0:-1;if(!(Number.isInteger(e)&&e>=0&&e<=3))throw new RangeError("'correct' must be an integer between 0 and 3");return e}function Ks(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var Mf=10*Number.EPSILON;function Ef(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?se(e):r(se(e)-n*Gt(e)),s=()=>n===0?se(e,{lowerTail:!1}):r(se(e,{lowerTail:!1})+n*Gt(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let c=s();return c>=1/64?2*(1-i):2*c}function od(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function sd(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 xf(e,o,s,n)}function ad(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 xf(e,i,c,r)}function xf(e,t,n,r){let[o,s,i]=od(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function hi(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:it([e[n-1],e[n]])}var pi=(e,t)=>e-t,mi=5e9,id=40;function ui(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function Bs(e,t){if(e.size>16777216||e.steps>mi)throw ui(t);return e.steps}function Rf(e){let t=e.length,n=Nt(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 yn(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 cd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(p=>p-s);i.some((p,y)=>!Number.isFinite(p)&&Number.isFinite(e[y]))&&(i=e.map(p=>p/2-s/2));let a=i.some(p=>p===0),l=t??i.length<50,f=(p,y,x)=>({method:p,alternative:o,statistic:y,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let p=Nt(i.map(Math.abs)),y=yn(p).length>0,x=0;for(let L=0;L<i.length;L++)i[L]>0&&(x+=p[L]);let E="Wilcoxon signed rank exact test";if(!y&&!a){let L=i.length,C=L*(L+1)/2,q;if(o==="less")q=jr(x,L,!0,!1);else if(o==="greater")q=jr(x-1,L,!1,!1);else{let _=x>C/2?jr(x-1,L,!1,!1):jr(x,L,!0,!1);q=Math.min(1,2*_)}let F=f(E,x,q);return r.confInt?{...F,...ld(e,r)}:F}let R=p.filter((L,C)=>i[C]!==0);Bs(ii(R),"distribution");let A=ci(R),S;if(o==="less")S=nr(A,x);else if(o==="greater")S=Mr(A,x-.25);else{let L=0;for(let C of R)L+=C/2;S=Math.min(1,2*(x>L?Mr(A,x-.25):nr(A,x)))}let T=f(E,x,S);return r.confInt?{...T,...md(e,r)}:T}let m=a?i.filter(p=>p!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,d,w]=Rf(m),b=yn(Nt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(d-Ks(d,o,b>=0))/w,v=Ef(N,o,b>=1?sd(N,u,b):0),M=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...M,...ud(e,r)}:M}function di(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function yf(e,t,n){let r={confInt:di(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Af(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Sf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=tn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(pi)}function Tf(e,t,n,r,o){let{alternative:s,confLevel:i}=r,c=e.length,a=s==="two.sided"?(1-i)/2:1-i,l=n(a);t(l)<=a+Mf&&l++;let f=hi(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:di(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function ld(e,t){let n=e.length;return Tf(Sf(e),r=>jr(r,n,!0,!1),r=>xl(r,n,!0,!1),t,"(pseudo)median")}function kf(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 qf(e,t,n,r,o,s,i,c,a,l){let{alternative:f,confLevel:m}=a,u=e.length,d=(f==="two.sided"?(1-m)/2:1-m)+Mf,w=f!=="less",g=f!=="greater",b=q=>tn(e[q-1],e[q]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+le(Math.min(d,1))*i.sd+.1*i.sd+3))),v=8,M=q=>q===0?e[0]-1:b(q),p=q=>q===0?e[u-1]+1:b(u-q),y=[new Map,new Map];{let q=new Map,F=0,_=id*c;for(let[O,I]of[[0,M],[1,p]])if(!(O===0?!w:!g))for(let D=0;D<=N;D++){if(F+=_,F>mi)throw ui("interval");let k;try{k=t(I(D))}catch{break}if(y[O].set(D,[k.stat,k.key]),q.has(k.key)){q.delete(k.key),q.set(k.key,!0);continue}if(F+=r(k.scores),F>mi)throw ui("interval");q.set(k.key,!0),q.size>v&&q.delete(q.keys().next().value)}}let x=new Map,E=(q,F)=>{let _=q===0?M(F):p(F),O=y[q].get(F),I;if(O===void 0){let B=t(_);O=[B.stat,B.key],I=B.scores}let[D,k]=O,V=x.get(k);return V!==void 0?x.delete(k):(V=n(I??t(_).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(k,V),[D,V]},R=q=>{let[F,_]=E(0,q);return o(_,F)},A=q=>{let[F,_]=E(1,q);return s(_,F)},S=-1/0,T=0;if(w){let q=R(0);if(!(q>d)){let F=q,_=1;for(;_<=u-1;_++){let O=R(_);if(O>d)break;F=O}S=e[_-1],T=F}}let L=1/0,C=0;if(g){let q=A(0);if(!(q>d)){let F=q,_=u-1;for(;_>=1;_--){let O=A(u-_);if(O>d)break;F=O}L=e[_],C=F}}return{confInt:[S,L],confLevel:1-(T+C),estimate:hi(e),estimateNames:l}}function fd(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 md(e,t){let n=e.length,r=s=>{let i=fd(e,s),{ranks:c,key:a}=kf(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 qf(Sf(e),r,ci,s=>Bs(ii(s),"interval"),(s,i)=>Mr(s,i-.25),nr,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function ud(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return yf(n,NaN);let c=ln(e),a=gt(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"),yf(n,tn(c,a),"midrange");let l=(v,M)=>{let[,p,y]=Rf(e.map(x=>x-v).filter(x=>x!==0));return(p-Ks(p,n,M))/y},f=l(c,s),m=l(a,s),u=v=>-le(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>le(v/2)):!(f<u(v/2))&&!(m>le(v/2)),d=1-r,w=!1;for(;!h(d)&&d<1;)d*=2,w=!0;w&&t.warnings.add("requested conf.level not achievable"),Af(o);let g=v=>Kt(M=>l(M,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),b;if(d>=1){let v=hi([...e].sort(pi));b=di(n,v,v)}else n==="greater"?b=[g(u(d)),1/0]:n==="less"?b=[-1/0,g(le(d))]:b=[g(u(d/2)),g(le(d/2))];let N=Kt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:b,confLevel:w?1-Math.min(1,d):r,estimate:N,estimateNames:"(pseudo)median"}}function If(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 yn(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 hd(e,t){let[n,r,o]=If(Nt([...e,...t]),e.length);return[n,r,o]}function pd(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 dd(e,t,n){return _f(e,t,n).ranks}function _f(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=Nt([...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:ai(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=pd(e.map(s=>s/2-n/2),t);return{ranks:o,key:ai(o)}}return kf([...r,...t])}function gd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=dd(e,t,s),[f,m,u,h]=If(l,i),d,w;if(a)if(w="Wilcoxon rank sum exact test",!h)d=Nf(()=>o==="less"?Kr(f,i,c,!0,!1):o==="greater"?Kr(f-1,i,c,!1,!1):Math.min(2*Kr(f,i,c,!0,!1),2*Kr(f-1,i,c,!1,!1),1));else{Bs(li(l,i),"distribution");let N=fi(l,i);o==="less"?d=nr(N,f):o==="greater"?d=Mr(N,f-.25):d=Math.min(2*nr(N,f),2*Mr(N,f-.25),1)}else{w=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(p=>p===l[0]),M=(m-Ks(m,o,N>=0))/u;d=v?1:Ef(M,o,N>=1?ad(M,i,c,N):0)}let g={method:w,alternative:o,statistic:f,statisticName:"W",pValue:d,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return g;let b=a?h?bd(e,t,r):Nf(()=>wd(e,t,r)):vd(e,t,r);return{...g,...b}}function Nf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Lf(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(pi)}function wd(e,t,n){let r=e.length,o=t.length;return Tf(Lf(e,t,!1),s=>Kr(s,r,o,!0,!1),s=>Al(s,r,o,!0,!1),n,"difference in location")}function bd(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=_f(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 qf(Lf(e,t,!0),s,i=>fi(i,r),i=>Bs(li(i,r),"interval"),(i,c)=>Mr(i,c-.25),nr,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function vd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=ln(e)-gt(t),a=gt(e)-ln(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,d)=>{let[,w,g]=hd(e.map(b=>b-h),t);return(w-Ks(w,r,d))/g},f=(h,d)=>{let w=l(c,d)-h,g=l(a,d)-h;if(Number.isNaN(w)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return w<=0?c:g>=0?a:(Af(s),Kt(b=>l(b,d)-h,c,a,{tol:s,fLower:w,fUpper:g}))},m=1-o,u;return r==="greater"?u=[f(-le(m),i),1/0]:r==="less"?u=[-1/0,f(le(m),i)]:u=[f(-le(m/2),i),f(le(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var yd=["method","n"],Cf=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Nd(e){if(Cf.includes(e))return e;let t=Cf.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Jr(e,t={}){K(t,yd,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Nd(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=Of(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Us(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 Of(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=Us(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=Us(e,!0),i=1/0;return s.forEach((c,a)=>{let l=r-a;i=Math.min(i,(n-l+1)*e[c]),o[c]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=Pn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)gn(m,1/h,u);s=Vn(m)}else{let h=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*h)+1/(120*h*h)}}let i=s*n,c=Number.isFinite(i)?1:1024,a=Number.isFinite(i)?i:s*(n/1024),l=Us(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?Of(e,"hochberg",n):Md(e,n)}}var Ff=2**16;function Md(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Ff)throw new RangeError(`n must be at most ${Ff} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Us(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 Df=["p","alternative","confLevel"],Ed=1+1e-7;function gi(e,t,n){let r=e-1;return e===0||e-r===1&&Ot(t,-r)[1]===0?Dn(r,t,n,!1,!1):Dn(e,t,n,!1,!1)+dn(e,t,n,!1)}function xd(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)*Ed,s=a=>dn(a,t,n,!1)<=o;if(e<r){let a=bs(s,Math.ceil(r),t),l=a>t?0:gi(a,t,n);return Math.min(1,Dn(e,t,n,!0,!1)+l)}let i=vs(s,0,Math.floor(r)),c=i<0?0:Dn(i,t,n,!0,!1);return Math.min(1,gi(e,t,n)+c)}function Rd(e,t,n={}){K(t,Df,"binomTest"),K(n,Df,"binomTest");let r,o;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(n=t),!Mn(e[0])||!Mn(e[1]))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1]),o===0||o===1/0)throw new RangeError("'n' must be a positive integer >= 'x'")}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Mn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Mn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return wi(r,o,n)}function wi(e,t,n={}){let r=n.p??.5;if(!(r>=0&&r<=1))throw new RangeError("'p' must be a single number between 0 and 1");let o=Ze(n.alternative),s=n.confLevel??.95;Lt(s);let i;o==="less"?i=Dn(e,t,r,!0,!1):o==="greater"?i=gi(e,t,r):i=xd(e,t,r);let c=f=>e===0?0:Gn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Gn(f,e+1,t-e,!1,!1):Gn(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,a(1-s)];else if(o==="greater")l=[c(1-s),1];else{let f=(1-s)/2;l=[c(f),a(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:i,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var Ad=["T","r","alternative","confLevel"],Sd=1+1e-7;function Td(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=ys(e,t,!1)*Sd,r=i=>ys(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=bs(r,Math.max(e+1,o),1/0);return Math.min(1,Wr(e,t,!0,!1)+bi(i,t))}let s=vs(r,0,Math.min(e-1,o));return Math.min(1,Wr(s,t,!0,!1)+bi(e,t))}var Wo=2**-1022;function bi(e,t){return e-(e-1)===1?Wr(e-1,t,!1,!1):Wr(e,t,!1,!1)+ys(e,t,!1)}function Er(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function kd(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Wo&&(s!==0||e===0||t===0))return s;let i=e*t,c=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(i)&&Number.isFinite(c))return i/c;let[a,l]=Er(e),[f,m]=Er(t),[u,h]=Er(n),[d,w]=Er(r),g=f*d,b=h-m-w,N=b>=0?u/(u+P(g,-b)):u/(P(u,b)+g);return P(a*f*N,l+m+Math.min(b,0))}function qd(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Wo&&(o===0||o>=Wo)?i:e===0?0:e/t*(r/n)}function Id(e,t={}){K(t,Ad,"poissonTest");let n=typeof e=="number"?[e]:Array.from(e);if(n.length<1||n.length>2)throw new RangeError("incorrect length of 'x'");if(!n.every(Mn))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,i=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(b=>Number.isFinite(b)&&b>0))throw new RangeError("'T' must be positive");let c=t.r??1;if(!(Number.isFinite(c)&&c>=0))throw new RangeError("'r' must be a single positive number");let a=Ze(t.alternative),l=t.confLevel??.95;if(o===2?Lt(l):fn(l),o===2){let[b,N]=r,[v,M]=i,p=b+N;if(p===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*v,x=y+M,E=Number.isFinite(x)&&x>=Wo&&(y===0||y>=Wo)?y/x:1/(1+M/v/c),R=wi(Math.round(b),Math.round(b)+Math.round(N),{p:E,alternative:a,confLevel:l}),A=M/v,S=T=>{if(T===0)return 0;let L=T/(1-T);if(Number.isFinite(A)||L===1/0)return L*A;let[C,q]=Er(L),[F,_]=Er(v),[O,I]=Er(M);return P(C*O/F,q+I-_)};return{method:"Comparison of Poisson rates",alternative:a,statistic:b,statisticName:"count1",parameter:kd(p,c,v,M),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:qd(b,N,v,M),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Wr(f,u,!0,!1):a==="greater"?h=bi(f,u):h=Td(f,u);let d=b=>f===0?0:ws(b,f,1,!0,!1),w=b=>b<1e-8?ws(b,f+1,1,!1,!1):ws(1-b,f+1,1,!0,!1),g;if(a==="less")g=[0,w(1-l)];else if(a==="greater")g=[d(1-l),1/0];else{let b=(1-l)/2;g=[d(b),w(b)]}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 Pf=["p","alternative","confLevel","correct"],_d="Chi-squared approximation may be incorrect";function vi(e,t,n={}){vt(t)&&K(t,Pf,"propTest"),K(n,Pf,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(T=>T.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(T=>T[0]),o=S.map(T=>T[0]+T[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,T)=>S>o[T]))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(T=>S[T]),l.some(T=>!(T>0&&T<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;Lt(m);let u=Ze(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,T)=>S/o[T]),d=a===2?h[0]-h[1]:0,w=a===2?1/o[0]+1/o[1]:0,g=(s.correct??!0)&&a<=2?.5:0,b=!1;if(g>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=g&&([g,b]=[S,!0])}else if(g>0&&a===2&&!f){let S=Math.abs(d)/w;S<=g&&([g,b]=[S,!0])}let N=Ld(r,o),v=o.some((S,T)=>{let L=l?.[T]??N;return S*L<5||S*(1-L)<5}),M=0;if(!b)for(let S=0;S<a;S++){let T=l?.[S]??N,L=o[S]*T,C=o[S]*(1-T);M+=Vt(Math.abs(r[S]-L)-g,L),M+=Vt(Math.abs(o[S]-r[S]-C)-g,C)}let p=l!==void 0?a:a-1,y;if(u==="two.sided")y=yt(M,p/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:d);y=se(S*Math.sqrt(M),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,T=u==="two.sided"&&S<1e-8?le(S,{lowerTail:!1}):le(u==="two.sided"?(1+m)/2:m);if(a===1){let L=o[0],C=T*T,q=(D,k)=>(D+C/(2*L)+k*T*Math.sqrt(D*(1-D)/L+C/(4*L*L)))/(1+C/L),F=h[0]+g/L,_=h[0]-g/L,O=Math.min(1,Math.max(0,F>=1?1:q(F,1))),I=Math.min(1,Math.max(0,_<=0?0:q(_,-1)));x=u==="two.sided"?[I,O]:u==="greater"?[I,1]:[0,O]}else{let L=Math.sqrt(h.reduce((_,O,I)=>_+O*(1-O)/o[I],0)),C=T*L+g*w,q=Math.max(-1,d-C),F=Math.min(1,d+C);x=u==="two.sided"?[q,F]:u==="greater"?[q,1]:[-1,F]}}let E=a===1?"p":c.map(S=>`prop ${S+1}`),A={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:M,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:y,estimate:a===1?h[0]:h,estimateNames:E,warnings:v?[_d]:[]};return l!==void 0&&(A.nullValue=a===1?l[0]:l,A.nullValueName=E),x!==void 0&&(A.confInt=x,A.confLevel=m),X("propTest",A)}function Ld(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+P(s,-r),0)/t.reduce((o,s)=>o+P(s,-r),0)}var Cd=["method","confLevel","sides"],Fd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],Od=["two.sided","left","right"];function jo(e,t){let n=Te(t,Od),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{...yi(s,o),sides:n}}function yi(e,t=1-e){let n=e/2<1e-8,r=n?le(e/2,{lowerTail:!1}):le(1-e/2),o=n?On(e,1,{lowerTail:!1}):On(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function eo(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!Mn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function Wn(e,t,n,r){let o=eo(e,n),s=eo(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 Ys(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 Hs(e,t,n,r){return r?e/2<1e-8?Gn(e/2,t,n,!1,!1):Gn(1-e/2,t,n,!0,!1):Gn(e/2,t,n,!0,!1)}function Vf(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*dn(e,t,m,!1)+Dn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*dn(e,t,m,!1)+Dn(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let d=i*s(h);if(d<=0)return Math.exp(d===0?h:Kt(w=>i*s(w),h,f,{tol:2**-60,fLower:d,fUpper:m}));f=h,m=d,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 Ni(e,t,n={}){K(n,Cd,"binomCI");let r=Te(n.method,Fd),o=jo(n.confLevel,n.sides),[s,i]=Wn(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]=Ys(s,i,a);break;case"wilsoncc":{let h=a*a,d=2*(i+h),w=2*s+h-1,g=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:w-g<w/2?(2*s-1)/(2*i)*(2*s-1)/(w+g):(w-g)/d,u=l===1?1:(2*s+h+1+a*Math.sqrt(h+2-1/i+4*l*(i*f-1)))/d;break}case"agresti-coull":{let h=a*a,d=i+h,w=(s+h/2)/d,g=a*Math.sqrt(w*(1-w)/d);[m,u]=[w-g,w+g];break}case"jeffreys":m=s===0?0:Hs(c,s+.5,i-s+.5,!1),u=s===i?1:Hs(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:Hs(c,s,i-s+1,!1),u=s===i?1:Hs(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):Vf(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):Vf(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),X("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Ko(e){return e===1/0?1:e/(1+e)}function Bo(e,t){return e===0?0:t>0?e*e/t:1/0}function Dd(e,t,n,r){return e/t+n/r>1}function Pd(e,t,n,r){return Xs(e,t,n,r,0)}function xi(e,t,n,r,o){if(Dd(e,t,n,r)){let[d,w,g,b]=xi(t-e,t,r-n,r,-o);return[w,d,b,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 d=b=>{let N=0;return i>0&&(N+=i/b),i<c&&(N-=(c-i)/(1-b)),a>0&&(N+=a/(b+f)),a<l&&(N-=(l-a)/(m-b)),N},w=b=>{let N=0;return i>0&&(N-=i/(b*b)),i<c&&(N-=(c-i)/((1-b)*(1-b))),a>0&&(N-=a/((b+f)*(b+f))),a<l&&(N-=(l-a)/((m-b)*(m-b))),N};if(d(0)<=0)u=0;else if(d(m)>=0)u=m;else{let b=0,N=m,v=Vd(e,t,n,r,o);u=s?v:v-o,u>b&&u<N||(u=N/2);for(let M=0;M<2e3;M++){let p=d(u);if(p===0)break;p>0?b=u:N=u;let y=u-p/w(u);if(y>b&&y<N||(y=b===0?N/2:N>2*b?Math.sqrt(b*N):b+(N-b)/2),!(y>b&&y<N))break;let x=Math.abs(y-u);if(u=y,x<=2**-53*u)break}}}let h=[u,1-u,Math.min(1,u+f),Math.max(0,m-u)];return s?h:[h[2],h[3],h[0],h[1]]}function Vd(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),d=Math.sign(h)*Math.sqrt(Math.max(0,u*u-f/(3*a)));if(d===0)return-u;let w=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(d*d*d)))))/3;return 2*d*Math.cos(w)-u}function Ri(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]=xi(e,t,n,r,o),f=i*c/t+a*l/r;if(s==="mee")return f;let m=t+r;return f*m/(m-1)}function $d(e,t,n,r,o,s,i){let c=De(De(lt([e,0],t),lt([n,0],r)),[o,0]),[a,l,f,m]=xi(e,t,n,r,o),u=he(lt(en(a,l),t),lt(en(f,m),r));return s==="mn"&&(u=lt(Tt(u,t+r),t+r-1)),De(_e(c,c),Tt(u,i))[0]}function Xs(e,t,n,r,o){return De(De(lt([e,0],t),lt([n,0],r)),[o,0])[0]}function zd(e,t,n,r,o,s){return Bo(Xs(e,t,n,r,o),Ri(e,t,n,r,o,s))}var Gs=new Float64Array(1),Mi=new BigInt64Array(Gs.buffer);function $f(e){return Gs[0]=Math.abs(e),e<0?-Mi[0]:Mi[0]}function rr(e){return Mi[0]=e<0n?-e:e,e<0n?-Gs[0]:Gs[0]}function Ei(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=$f(i?r:n),a=$f(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,d=e(rr(h));if(d===0)return rr(h);d<0==l<0?(a=h,l=d):(f=h,m=d)}return Math.abs(m)<Math.abs(l)?rr(f):rr(a)}function Wf(e,t,n,r,o,s){let i=Pd(e,t,n,r);if(o===0)return[i,i];let c=Ko(o),a=d=>(d===i?0:Ko(zd(e,t,n,r,d,s)))-c,l=(d,w,g,b)=>{if(g===0)return d;if(b===0)return w;let N=Kt(a,d,w,{tol:1e-300,fLower:g,fUpper:b});return s==="wald"?N:Ei(v=>$d(e,t,n,r,v,s,o),N,d,w,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 zf(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 Wd(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 jd(e,t,n,r,o){let[s,i]=Wd(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,i,1-i];let c=s<=.5&&i<=.5?0:o*(1-i)<=1-s?2:3,a=c===0?i:c===2?1-i:1-s,l=y=>c===0?[y,0]:c===2?Ot(1,-y):lt(Ot(1,-y),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?lt(Ot(o,-1),o)[0]:0:c===3&&o<1?Ot(1,-o)[0]:0,h=y=>{let x=l(y),E=Tt(x,o);return[E[0],De([1,0],E)[0],x[0],De([1,0],x)[0]]},d=e+n,w=c<2?1:-1,g=y=>{let[,x,E,R]=h(y),A=0,S=0;return d>0&&(A+=E>0?d/E:1/0,S-=d/(E*E)),e<t&&(A-=x>0?o*(t-e)/x:1/0,S-=o*(t-e)/x*(o/x)),n<r&&(A-=R>0?(r-n)/R:1/0,S-=(r-n)/(R*R)),[w*A,S*f]},b;if(!(m>u))b=u;else if(!(g(u)[0]>0))b=u;else if(!(g(m)[0]<0))b=m;else{let y=u,x=m;b=a,b>y&&b<x||(b=y+(x-y)/2);for(let E=0;E<2e3;E++){let[R,A]=g(b);if(R===0)break;R>0?y=b:x=b;let S=b-R/A;if(S>y&&S<x||(S=y<=0?x/2:x>2*y?Math.sqrt(y*x):y+(x-y)/2),!(S>y&&S<x))break;let T=Math.abs(S-b);if(b=S,T<=2**-53*b)break}}let[N,v,M,p]=h(b);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,M)),Math.min(1,Math.max(0,p))]}function Kd(e,t,n,r,o){let[s,i,c,a]=jd(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return Bo(l(e,t,s,i),t*s*i)+Bo(l(n,r,c,a),r*c*a)}function jf(e,t,n,r,o){let s=e/t/(n/r);return Bf(s,o,i=>Kd(e,t,n,r,i))}function Bd(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 Ud(e,t,n,r,o){let[s,i]=Bd(e,t,n,r,o),c=e-t*s;return Bo(c,t*s*(1-s))+Bo(c,r*i*(1-i))}function Kf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return Bf(s,o,i=>Ud(e,t,n,r,i))}function Bf(e,t,n){if(t===0)return[e,e];let r=Ko(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Ei(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?Ei(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Ko(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:Ko(n(Math.exp(l))))-r;return[s(zf(a,c,-1)),i(zf(a,c,1))]}var Hd=["method","confLevel","sides"],Yd=["method","confLevel","sides"],Gd=["method","confLevel"],Xd=["ac","wald","score","mn","mee"];function Ai(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),d=u-h,w=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,d-w),Math.min(1,d+w)];break}case"score":{let[u,h]=Ys(e,t,i),[d,w]=Ys(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+w*(1-w)/r),m=l+i*Math.sqrt(h*(1-h)/t+d*(1-d)/r);break}case"mn":case"mee":[f,m]=Wf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function Qd(e,t,n,r,o={}){K(o,Hd,"binomDiffCI");let s=Te(o.method,Xd),i=jo(o.confLevel,o.sides),[c,a]=Wn(e,t,"x1","n1"),[l,f]=Wn(n,r,"x2","n2"),m=c/a-l/f,[u,h]=Ai(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),X("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Zd=["katz.log","koopman"];function Qs(e,t,n,r,o){let s=e/t/(n/r),i=o*Math.sqrt(1/e-1/t+1/n-1/r);return[s*Math.exp(-i),s*Math.exp(i)]}function Jd(e,t,n,r,o={}){K(o,Yd,"binomRatioCI");let s=Te(o.method,Zd),i=jo(o.confLevel,o.sides),[c,a]=Wn(e,t,"x1","n1"),[l,f]=Wn(n,r,"x2","n2"),m=i.kappa,u=c===0?0:c/a/(l/f),h,d;return c===0&&l===0?[h,d]=[0,1/0]:s==="katz.log"?c===0?[h,d]=[0,Qs(.5,a,l,f,m)[1]]:l===0?[h,d]=[Qs(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,d]=Qs(a-.5,a,f-.5,f,m):[h,d]=Qs(c,a,l,f,m):[h,d]=jf(c,a,l,f,i.chisq),i.sides==="left"?d=1/0:i.sides==="right"&&(h=0),X("binomRatioCI",{estimate:u,confInt:[h,d],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var e0=["midp","fisher","wald","score"],t0={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"},n0=2**23,r0=2**20;function Uf(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 o0(e,t={}){K(t,Gd,"oddsRatio");let n=Te(t.method,e0);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}=jo(r,void 0),c=e.map((S,T)=>Array.from(S,(L,C)=>eo(L,`table[${T}][${C}]`))),[[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),d=a+Math.min(l,f),w=d-h+1;if(n==="midp"||n==="fisher"){if(w>n0)throw new RangeError(`the exact distribution of the 2 x 2 table has ${w} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(a,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let g,b,N;try{g=cn(a-1,a+f,l+m,a+l,!0,!1)+.5*Nn(a,a+f,l+m,a+l,!1),b=cn(a,a+f,l+m,a+l,!1,!1)+.5*Nn(a,a+f,l+m,a+l,!1),N=w<=r0?oi(new Zr(a+f,l+m,a+l),a,1):wf(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,b),M=[a+l,f+m],p=[a+f,l+m],y=0,x=!1;for(let S=0;S<2;S++)for(let T=0;T<2;T++){let L=M[T]*p[S]/u;L<5&&(x=!0);let C=Math.abs(c[T][S]-L);y+=C*C/L}let E=yt(y,.5,2,!1,!1),R,A;switch(n){case"wald":{R=a*m/(l*f);let S=s*Math.sqrt(1/a+1/l+1/f+1/m);A=[Math.exp(Math.log(R)-S),Math.exp(Math.log(R)+S)];break}case"fisher":{let S=rt(()=>$o(c,{confLevel:r}));R=S.estimate,A=S.confInt;break}case"score":{R=a*m/(l*f),A=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:Kf(a,a+l,f,f+m,i);break}case"midp":{let S=new Zr(a+f,l+m,a+l),T=F=>{if(F===1)return[g,b];let _=S.density(F),O=a-h,I=.5*_[O];return[Uf(_,0,O-1)+I,Uf(_,O+1,_.length-1)+I]},L=(F,_)=>{let O=F(1);return O===0?1:tr(F,_?O>0:O<0)};R=a===h?0:a===d?1/0:L(F=>T(F)[0]-.5,!1);let C=a===h?0:L(F=>T(F)[1]-o/2,!0),q=a===d?1/0:L(F=>T(F)[0]-o/2,!1);A=[C,q];break}}return X("oddsRatio",{estimate:R,confInt:A,confLevel:r,method:t0[n],pValue:{midp:v,fisher:N,chiSquare:E},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var s0=["epsilon","paired","varEqual","confLevel","alpha"],a0=["margin","method","alpha"];function Hf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function i0(e,t,n={}){K(n,s0,"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)Hf(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,Lt(o),!(o>.5))throw new RangeError("'confLevel' must be greater than 0.5");s=1-o}if(t===void 0)throw new TypeError("'y' is required");let i=n.paired??!1,c=n.varEqual??!1,a=rt(()=>En(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=ft((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?bt(s,f,!1,!1):bt(o,f,!0,!1),d=[m-h*l,m+h*l];return X("tost",{method:a.method.replace("t-test","TOST"),parameter:f,parameterName:"df",pValue:u,result:d[0]>-r&&d[1]<r?"rejected":"not rejected",confInt:d,confLevel:o,estimate:a.estimate,estimateNames:i?"mean of the differences":a.estimateNames,epsilon:r,seDiff:l,warnings:[]})}var c0=["mn","mee","wald"];function l0(e,t,n,r,o={}){K(o,a0,"binomDiffEquivalenceTest");let s=Te(o.method,c0),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;Hf(c,"alpha");let[a,l]=Wn(e,t,"x1","n1"),[f,m]=Wn(n,r,"x2","n2"),u=a/l-f/m,h=M=>Xs(a,l,f,m,M)/Math.sqrt(Ri(a,l,f,m,M,s)),d=h(-i),w=h(i),g=[se(d,{lowerTail:!1}),se(w)],b=Math.max(g[0],g[1]),N=1-2*c,v=Ai(a,l,f,m,s,yi(2*c,N));return X("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[d,w],statisticName:"z",pValues:g,pValue:b,result:g[0]<c&&g[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var Bt=new WeakMap;function Js(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Se(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function ea(e,t){return e.map(n=>{if(n.rss>=f0&&n.rss<1/0)return P(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=P(o,-t);r+=i*c*c}),r})}var f0=2**-1022;function Ti(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,P(n,-t)]])}function ta(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function xr(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+P(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=ta(e),s=r(o)),!(P(t,-o)>=1e-24*s)}function Yf(e){let t=Js([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+=P(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:ea([e],t)[0],k:t}}var Gf=(e,t,n)=>dt(e,t,n,!1,!1),ki="essentially perfect fit for \u20181\u2019: summary may be unreliable";function qi(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 Ii=new WeakMap;function m0(e){let t=Yf(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*P(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 Si(e){let t=m0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let w=Ti(e,o);xr(w,c)&&(c=0,i=i.map(g=>xr(w,g.ss)?{...g,ss:0}:g))}let a=c/r,l=w=>P(w,2*o),f=i.map(({df:w,ss:g})=>{let b=g/w/a;return[w,l(g),l(g/w),b,Gf(b,w,r)]});f.push([r,l(c),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],h={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},d=i.map(({df:w,ss:g})=>[w,g]);return d.push([r,c]),Bt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:d}),h}function u0(e){let t=e.map(f=>f.dfResidual),n=Js(e),r=ea(e,n),o=f=>P(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*P(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],[d,w]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],g=N=>N.filter((v,M)=>(d.weights?.[M]??1)!==0),b=u===0||!Cs(g(d.x),g(w.x))?NaN:h/u/i;return b<0&&(b=NaN),[t[m],o(r[m]),u,o(h),b,Number.isNaN(b)?NaN:Gf(b,Math.abs(u),t[s])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((f,m)=>String(m+1)),table:l,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function Zs(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function _i(e,...t){let n=[e,...t];for(let c of n)if(typeof c!="object"||c===null||!Array.isArray(c.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1){let c=Si(e),a={...c,warnings:Zs(e.warnings,c.warnings)},l=Bt.get(c);return l!==void 0&&Bt.set(a,l),X("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?Si(e):u0(o);return i.warnings=Zs(...n.map(c=>c.warnings),s,i.warnings),X("anova",i,...n)}function Li(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Zs(i.warnings,Ii.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Bt.get(i);return Array.isArray(l)&&Bt.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),X("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=Si(t),r=Yf(t),o=qi(t.fittedValues.map(i=>P(i,-r.k)),r.rss,r.rdf),s=Zs(t.warnings,n.warnings,o?[ki]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Bt.get(n);return c!==void 0&&Bt.set(i,c),X("aovSummary",i,e)}return X("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 h0=["pooledSd","mu","paired","ci","alternative","adjust"],p0=["pooledSd","mu","paired","ci","alternative"],d0=["generalized","partial","ci","alternative"],g0=["partial","ci","alternative"],w0=["partial","ci","alternative"];function Oi(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var b0="Missing values detected. NAs dropped.",v0=`'y' is numeric but has only 2 unique values.
|
|
3
|
+
If this is a grouping variable, convert it to a factor.`,Jf={tol:0,maxiter:5e3};function Rr(e){return e===1/0?1:Math.exp(wl(.5,(e-1)/2)-.5*Math.log(e/2))}function Xf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return yr(o=>an(e,t,{ncp:o})-n,e-r,e+r,{...Jf,extendInt:"downX"}).root}function em(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;Oi(c);let a=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(Pt(f)).size===2&&m.push(v0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(b0);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,d,w,g,b,N;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:N}=Se([Pt(l)]));else{let[L,C]=Zn(l,f);({d:S,k:N}=Ns(L,C))}let T=S.length;if(T<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");h=it(S),d=Math.sqrt(st(S)),w=d/Math.sqrt(T),g=1/T,b=T-1}else{let S=Dt([Pt(l),Pt(f)]);N=S.k;let[T,L]=S.n;if(T<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 C=Math.sqrt(S.variance[0]),q=Math.sqrt(S.variance[1]);o?(d=Math.sqrt(((T-1)*C**2+(L-1)*q**2)/(T+L-2)),w=d*Math.sqrt(1/T+1/L),g=1/T+1/L,b=T+L-2):(d=Math.sqrt((C**2+q**2)/2),g=2*(L*C**2+T*q**2)/(T*L*(C**2+q**2)),w=Math.sqrt(Math.sqrt(C**2/T)**2+Math.sqrt(q**2/L)**2),b=Ms([C**2/T,q**2/L],[T-1,L-1]))}let v=r?Rr(b):1,M=P(s,-N),p=(h-M)/d*v,y=r?"Hedges_g":"Cohens_d";if(c===null)return{name:y,value:p,warnings:m};let x=a==="two.sided"?c:2*c-1,E=(h-M)/w,R=Xf(E,b,(1+x)/2)*Math.sqrt(g)*v,A=Xf(E,b,(1-x)/2)*Math.sqrt(g)*v;return a==="less"&&(R=-1/0),a==="greater"&&(A=1/0),{name:y,value:p,ci:c,ciLow:R,ciHigh:A,warnings:m}}function y0(e,t,n={}){return K(n,h0,"cohensD"),X("cohensD",em(e,t,n,n.adjust??!1))}function N0(e,t,n={}){return K(n,p0,"hedgesG"),X("hedgesG",em(e,t,n,!0))}var M0=e=>Array.isArray(e.effects),Ci=e=>e.multistratum===!0,tm=e=>Array.isArray(e.strata),nm=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Fi=e=>({test:t=>t.split(":").includes(e)});function E0(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 x0(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],d=e.within.filter(b=>Fi(b).test(m)),w=o(d.length===0?"Subject":`Subject:${d.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[a],ss:h[n]};w.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 rm(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 Qf(e,t,n,r){let o=i=>Tn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:yr(o,0,Math.max(10,e*t*4),{...Jf,extendInt:"downX"}).root:0}function om(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=Qf(c,t,n,(1+s)/2),l=Qf(c,t,n,(1-s)/2);c<=Yn((1-s)/2,t,n)&&(l=0),c<=Yn((1+s)/2,t,n)&&(a=0);let f=a===1/0?1:a/(a+n),m=l===1/0?1:l/(l+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}function R0(e){let t=Se([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=P(n.ss,-t)}var Zf="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Di(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Zf);if(Ci(e)||tm(e)||nm(e))return A0(e,t,n);let r=M0(e)?rt(()=>_i(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Zf);let o=Bt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;Oi(i);let c=Te(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,A)=>({name:R,df:s.table[A][a],ss:s.table[A][l]})).filter(R=>R.name!=="(Intercept)");R0(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,d=m.ss,w=d/h,g=f.reduce((R,A)=>R+A.ss,0),b=f.reduce((R,A)=>R+A.df,0)+1,N=t==="Eta2"?n.generalized??!1:!1,M=u.length===1?"plain":N!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:p,ssObserved:y}=rm(N,u),x=R=>{let{ss:A,df:S}=R;if(t==="Eta2")return M==="partial"?A/(A+d):M==="plain"?A/g:A/(A+d+y-(p(R.name)?A:0));if(t==="Omega2"){let L=M==="partial"?(A-S*w)/(A+(b-S)*w):(A-S*w)/(g+w);return Math.max(0,L)}let T=M==="partial"?(A-S*w)/(A+d):(A-S*w)/g;return Math.max(0,T)},E=`${t}${M==="plain"?"":`_${M}`}`;return{colNames:i===null?[E]:[E,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>i===null?[x(R)]:om(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function A0(e,t,n){let r=n.ci===void 0?.95:n.ci;Oi(r);let o=Te(n.alternative??"greater",["two.sided","less","greater"]),s=nm(e),i,c,a;if(s){let p=Bt.get(e),y=x0(p!==void 0&&!Array.isArray(p)?{...e,univariate:p}:e);({strata:i,subject:c}=y),a=y.order.map(x=>({term:x,stratum:y.strata.find(E=>E.terms.includes(x))}))}else{let p=Ci(e)?rt(()=>Li(e)):e,y=Bt.get(p);({strata:i,subject:c}=E0(Array.isArray(y)?{...p,strata:y.map((x,E)=>({...p.strata[E],...x}))}:p)),a=i.flatMap(x=>x.terms.map(E=>({term:E,stratum:x})))}let l=Se([[...i.flatMap(p=>p.terms.map(y=>y.ss)),...i.map(p=>p.sse)]]).k;for(let p of i){p.sse=P(p.sse,-l);for(let y of p.terms)y.ss=P(y.ss,-l)}let f=i.flatMap(p=>p.terms),m=i.reduce((p,y)=>p+y.sse,0),u=f.reduce((p,y)=>p+y.ss,0)+m,h=c.sse/c.dfe,d=t==="Eta2"?n.generalized??!1:!1,w=d!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:b}=rm(d,f),N=(p,y)=>{let{ss:x,df:E}=p,R=y.sse/y.dfe;if(t==="Eta2")return w==="partial"?x/(x+y.sse):w==="plain"?x/u:x/(x+m+b-(g(p.name)?x:0));if(t==="Omega2"){let S=y===c?0:1,T=w==="partial"?(x-E*R)/(x+S*y.sse+c.sse+h):(x-E*R)/(u+h);return Math.max(0,T)}let A=w==="partial"?(x-E*R)/(x+y.sse):(x-E*R)/u;return Math.max(0,A)},v=`${t}${w==="plain"?"":`_${w}`}`,M={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(p=>p.term.name),table:a.map(({term:p,stratum:y})=>{let x=N(p,y);return r===null?[x]:om(x,p.df,y.dfe,r,o)}),warnings:[]};return s||(M.groups=a.map(p=>p.stratum.name),M.warnings=[...Ci(e)||tm(e)?e.warnings:[]]),M}function S0(e,t={}){return K(t,d0,"etaSquared"),X("etaSquared",Di(e,"Eta2",t),e)}function T0(e,t={}){return K(t,g0,"omegaSquared"),X("omegaSquared",Di(e,"Omega2",t),e)}function k0(e,t={}){return K(t,w0,"epsilonSquared"),X("epsilonSquared",Di(e,"Epsilon2",t),e)}var q0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),I0=["m1","sd1","n1","m2","sd2","n2","r12"],_0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),L0="Equivalence interval does not include zero.",C0="Warning: setting bound type to SMD produces biased results!",F0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function sm(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 yr(s=>an(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function O0(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(E,R,A)=>(1-(A-2)/(A*E*E))*R*R,i,c,a,l,f,m,u,h,d,w,g,b,N=t.biasCorrection;if(e.design==="two"){let{n1:E,n2:R,s1:A,s2:S}=e,T=A*A,L=S*S,C=2*E*R/(E+R);if(t.glass!==void 0){let q=t.glass==="glass1",[F,_,O,I]=q?[A,E,S,R]:[S,R,A,E];c=F,a=_-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Rr(a):1,f=l*o/c,d=1/R+1/E;let D=O/F;m=Math.sqrt(D*D/(I-1)+1/(_-1)+f*f/(2*(_-1))),u=o/(F*Math.sqrt(1/_+D*D/I)),h=E+R-2,w=Math.sqrt(2/C)}else if(t.varEqual)c=Math.sqrt(((E-1)*T+(R-1)*L)/(E+R-2)),a=E+R-2,i=N?"Hedges's g":"Cohen's d",l=N?Rr(a):1,f=l*o/c,d=1/E+1/R,m=Math.sqrt(1/E+1/R+s(l,f,a)),u=o/(c*Math.sqrt(d)),h=a,w=Math.sqrt(2*(R*T+E*L)/(E*R*(T+L)));else{c=Math.sqrt((T+L)/2),a=(E-1)*(R-1)*(T+L)**2/((R-1)*T*T+(E-1)*L*L),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Rr(a):1,f=l*o/c,d=2*(R*T+E*L)/(E*R*(T+L));let q=c**4;m=Math.sqrt(f*f*(T*T/(E-1)+L*L/(R-1))/(8*q)+(T/(E-1)+L/(R-1))/(c*c)),u=o/Math.sqrt(T/E+L/R),h=a,w=Math.sqrt(2/C)}g=a,b=(q,F)=>Math.sqrt(g/(g-2)*(2/C)*(1+q*q*C/2)-q*q/(F*F))}else if(e.design==="paired"){let{n:E,s1:R,s2:A,sdif:S,oneMinusR:T}=e,L=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?R:A:c=t.rmCorrection?S/Math.sqrt(2*T):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=E-1,g=2*E-2,l=N?Rr(r==="goulet"?g:a):1,f=l*o/c,d=1/E,u=o*Math.sqrt(E)/c,h=a,w=Math.sqrt(2*T/E),b=(C,q)=>{let F=r==="goulet"?g:a,_=Math.sqrt(F/(F-2)*(2*T/E)*(1+C*C*E/(2*T))-C*C/(q*q));return t.rmCorrection?_:_*Math.sqrt(2*T)},t.glass!==void 0){let C=S/c;m=Math.sqrt(C*C/(E-1)+f*f/(2*(E-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/E+s(l,f,a)):m=b(f,l)}else{let{n:E,s:R}=e;c=R,a=E-1,g=a,i=N?"Hedges's g":"Cohen's d",l=N?Rr(a):1,f=l*o/c,d=1/E,m=Math.sqrt(1/E+f*f/(2*E)),u=o*Math.sqrt(E)/R,h=a,w=1/Math.sqrt(E),b=(A,S)=>Math.sqrt(a/(a-2)*(1/E)*(1+A*A*E)-A*A/(S*S))}let v,M,p,y=a;if(r==="nct"){p=m,y=h;let E=sm(u,h,n,!0),R=sm(u,h,n,!1);v=E*Math.sqrt(d)*l,M=R*Math.sqrt(d)*l}else if(r==="goulet"){p=b(f,l),y=g;let E=f/w;v=Wt(n,g,{ncp:E})*w,M=Wt(n,g,{ncp:E,lowerTail:!1})*w}else{p=m;let E=r==="t"?bt(n,a,!1,!1):le(n,{lowerTail:!1});v=f-E*p,M=f+E*p}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:p,confInt:x?[-M,-v]:[v,M],df:y,J:l}}function am(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 im(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 cm(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 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 lm(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,d=O0(c,l),w=P(d.denom,a),g=l.smdBounds?[f[0]*w,f[1]*w]:f,b=O=>{if(O===0)return[0,n];let I=Math.floor(Math.log2(Math.abs(O))),D=Le(O,-I);return l.smdBounds?[D*d.denom,I+a]:[D,I]},N=l.smdBounds?f:[f[0]/w,f[1]/w];l.smdBounds&&m.push(C0),g[0]<=0&&g[1]>=0||m.push(L0);let v=O=>P(O/r,n-o),M=O=>{let[I,D]=b(O),k=Math.max(n,D);return Le((Le(t,n-k)-Le(I,D-k))/r,k-o)},p=v(t),y=M(f[0]),x=M(f[1]),E=P(r,o),R=h==="EQU",A={t:p,se:E,df:s,pValue:2*ft(-Math.abs(p),s,!0,!1)},S={t:y,se:E,df:s,pValue:ft(y,s,!R,!1)},T={t:x,se:E,df:s,pValue:ft(x,s,R,!1)},L=R?Math.max(S.pValue,T.pValue):Math.min(S.pValue,T.pValue);!R&&g[0]<=0&&g[1]>=0&&L<=A.pValue&&m.push(F0);let C=bt(u,s,!1,!1),q=Math.max(n,o),[F,_]=[P(t,n-q),P(r,o-q)];return X(e,{tTest:A,lower:S,upper:T,pValue:L,bounds:{raw:g,smd:N},raw:{estimate:P(t,n),se:E,confInt:[P(F-C*_,q),P(F+C*_,q)]},smd:{label:d.label,estimate:d.estimate,se:d.se,confInt:d.confInt,df:d.df,J:d.J},confLevel:1-2*u,method:i,hypothesis:h,warnings:m})}function D0(e,t,n={}){K(n,q0,"tTOST");let r=cm(n),o=im(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");am(e,"x"),a||am(t,"y");let l=rt(()=>a?En(e,{mu:c}):En(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,d;if(a){let w=Se([Pt(Array.from(e))]);u=w.k;let g=w.groups[0],b=st(g);h=it(g)-P(c,-u),d=Math.sqrt(b/g.length),m={design:"one",n:g.length,delta:h,s:Math.sqrt(b)}}else if(s){let[w,g]=Zn(Array.from(e),Array.from(t)),b=Se([w,g]);u=b.k;let[N,v]=b.groups,M=N.map((A,S)=>A-v[S]),p=Math.sqrt(st(N)),y=Math.sqrt(st(v)),x=st(M),E=Math.sqrt(x),R=(E-(p-y))*(E+(p-y))/(2*p*y);h=it(M)-P(c,-u),d=Math.sqrt(x/M.length),m={design:"paired",n:M.length,delta:it(N)-it(v)-P(c,-u),s1:p,s2:y,sdif:E,oneMinusR:R}}else{let w=Se([Pt(Array.from(e)),Pt(Array.from(t))]);u=w.k;let[g,b]=w.groups;if(g.length<2||b.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,v,M,p]=[g.length,b.length,st(g),st(b)];h=it(g)-it(b)-P(c,-u),d=Math.sqrt(i?((N-1)*M+(v-1)*p)/(N+v-2)*(1/N+1/v):Math.sqrt(M/N)**2+Math.sqrt(p/v)**2),m={design:"two",n1:N,n2:v,delta:h,s1:Math.sqrt(M),s2:Math.sqrt(p)}}return lm("tTOST",h,u,d,u,f,l.method,m,u,r,o,[])}function P0(e,t={}){K(e,I0,"tsumTOST"),K(t,_0,"tsumTOST");let n=cm(t),r=im(t),{m1:o,sd1:s,n1:i,m2:c,sd2:a,n2:l,r12:f}=e,m=(q,F)=>{if(typeof q!="number"||!Number.isFinite(q))throw new RangeError(`'${F}' must be a finite number`);return q},u=(q,F)=>{let _=m(q,F);if(!(Number.isInteger(_)&&_>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return _},h=(q,F)=>{let _=m(q,F);if(!(_>0))throw new RangeError(`'${F}' must be positive`);return _};m(o,"m1"),h(s,"sd1"),u(i,"n1");let d=c===void 0&&a===void 0&&l===void 0;if(n.paired&&d)throw new TypeError("'m2', 'sd2' and 'n2' are required when paired is true");d||(m(c,"m2"),h(a,"sd2"),u(l,"n2"));let w=[],{groups:g,k:b}=Se([[o,...d?[]:[c],n.mu]]),{groups:N,k:v}=Se([[s,...d?[]:[a]]]),[M,p=NaN]=d?[g[0][0]]:[g[0][0],g[0][1]],y=g[0][g[0].length-1],[x,E=NaN]=N[0],R=q=>P(q,b-v),A,S,T,L,C;if(d)A=M-y,S=x/Math.sqrt(i),T=i-1,L="One Sample t-test",C={design:"one",n:i,delta:R(A),s:x};else if(n.paired){let q=m(f,"r12");if(!(q>-1&&q<1))throw new RangeError("'r12' must be in (-1, 1)");let F=i;i!==l&&(w.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let _=Math.sqrt(x*x+E*E-2*q*x*E);if(!(_>0))throw new RangeError("the standard deviation of the differences is 0");A=M-p-y,S=_/Math.sqrt(F),T=F-1,L="Paired t-test",C={design:"paired",n:F,delta:R(A),s1:x,s2:E,sdif:_,oneMinusR:1-q}}else{let q=l,F=x*x,_=E*E;if(A=M-p-y,n.varEqual){let O=((i-1)*F+(q-1)*_)/(i+q-2);S=Math.sqrt(O*(1/i+1/q)),T=i+q-2,L=" Two Sample t-test"}else{let O=F/i,I=_/q;S=Math.sqrt(O+I),T=(O+I)**2/(O*O/(i-1)+I*I/(q-1)),L="Welch Two Sample t-test"}C={design:"two",n1:i,n2:q,delta:R(A),s1:x,s2:E}}return lm("tsumTOST",A,b,S,v,T,L,C,v,n,r,w)}var V0=["alternative","tsmethod"],$0=["square","central"],z0=1e3,W0=32,j0=.9,fm=1+1e-7,K0=2**24,B0=34e7;function U0(e,t={}){if(K(t,V0,"boschlooTest"),e.length!==2||e.some(y=>y.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ze(t.alternative),r=Te(t.tsmethod,$0),o=e.map((y,x)=>Array.from(y,(E,R)=>eo(E,`table[${x}][${R}]`))),[[s,i],[c,a]]=o,l=s+i,f=c+a;if(l===0||f===0)throw new RangeError("each row (group) of 'table' must have at least one observation");if((l+1)*(f+1)>K0)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(z0,Math.ceil(W0*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>B0)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let d=s/l-c/f,w;n!=="two.sided"?w=n:r==="square"?w="square":w=d>0?"greater":"less";let g=s+c,b;w==="less"?b=cn(s,l,f,g,!0,!1):w==="greater"?b=cn(s-1,l,f,g,!1,!1):b=rt(()=>$o(o)).pValue;let N=H0(l,f,s,c,w),{value:v,at:M}=Y0(N,l,f,u),p=n==="two.sided"&&r==="central"?Math.min(1,2*v):v;return X("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:b,statisticName:"test statistic",pValue:p,estimate:d,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:M,warnings:[]})}function H0(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let b=2;b<=s;b++)i[b]=xs(b);let c=t+1,a=new Float64Array((e+1)*c),l=Math.min(e,t)+1,f=new Float64Array(l),m=new Float64Array(l),u=new Float64Array(l);for(let b=0;b<=s;b++){let N=Math.max(0,b-t),M=Math.min(b,e)-N+1,p=-1/0;for(let E=0;E<M;E++){let R=N+E,A=-i[R]-i[e-R]-i[b-R]-i[t-b+R];f[E]=A,A>p&&(p=A)}let y=0;for(let E=0;E<M;E++)f[E]=Math.exp(f[E]-p),y+=f[E];for(let E=0;E<M;E++)f[E]=f[E]/y;let x=0;for(let E=0;E<M;E++)m[E]=x+=f[E];x=0;for(let E=M-1;E>=0;E--)u[E]=x+=f[E];if(o==="less")for(let E=0;E<M;E++)a[(N+E)*c+b-N-E]=m[E];else if(o==="greater")for(let E=0;E<M;E++)a[(N+E)*c+b-N-E]=u[E];else{let E=0;for(let R=1;R<M;R++)f[R]>f[E]&&(E=R);for(let R=0;R<M;R++){let A=f[R]*fm,S=-1,T=E;for(;T-S>1;){let F=S+T>>1;f[F]<=A?S=F:T=F}let L=E,C=M;for(;C-L>1;){let F=L+C>>1;f[F]<=A?C=F:L=F}let q=f[E]<=A?1:(S>=0?m[S]:0)+(C<M?u[C]:0);a[(N+R)*c+b-N-R]=Math.min(1,q)}}}let h=a[n*c+r]*fm,d=new Int32Array(e+2),w=[],g=[];for(let b=0;b<=e;b++){d[b]=w.length;let N=0;for(;N<=t;){if(a[b*c+N]<=h){let v=N;for(;N+1<=t&&a[b*c+N+1]<=h;)N++;w.push(v),g.push(N)}N++}}return d[e+1]=w.length,{starts:d,from:Int32Array.from(w),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=Ot(1,-t),s=at([t,0],o),i=at(o,[t,0]),c=s[0],a=i[0],l=r;for(let h=r+1;h<=e;h++){let d=n[h-1]*((e-h+1)/h*c);if(d===0)break;n[h]=d,l=h}let f=r;for(let h=r-1;h>=0;h--){let d=n[h+1]*((h+1)/(e-h)*a);if(d===0)break;n[h]=d,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 Y0(e,t,n,r){let{starts:o,from:s,to:i}=e,c=new Float64Array(t+1),a=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=v=>{let[M,p]=mm(t,v,c),[y,x]=mm(n,v,a),E=0;for(let A=y;A<=x;A++)l[A]=E+=a[A];E=0;for(let A=x;A>=y;A--)f[A]=E+=a[A];let R=0;for(let A=M;A<=p;A++){let S=c[A];if(S===0)continue;let T=0;for(let L=o[A];L<o[A+1];L++){let C=s[L],q=i[L];if(!(q<y||C>x))if(C===0)T+=l[Math.min(q,x)];else if(q===n)T+=f[Math.max(C,y)];else for(let F=Math.max(C,y);F<=Math.min(q,x);F++)T+=a[F]}R+=S*T}return Math.min(1,R)},u=Math.PI/2,h=v=>v<=0?0:v>=u?1:Math.sin(v)**2,d=new Float64Array(r+2),w=0;for(let v=0;v<=r+1;v++)d[v]=m(h(u*v/(r+1))),d[v]>d[w]&&(w=v);let g=d[w],b=h(u*w/(r+1));if(g>=1)return{value:1,at:b};let N=j0*g;for(let v=0;v<=r+1;v++){let M=d[v];if(M<N||v>0&&!(M>d[v-1])||v<=r&&!(M>=d[v+1]))continue;let p=u*Math.max(0,v-1)/(r+1),y=u*Math.min(r+1,v+1)/(r+1),E=Vl(S=>-m(h(S)),p,y,1e-14).x,R=m(h(E)),A=1e-4/Math.sqrt(2*(t+n));for(let S=0;S<2&&(E-A>p&&E+A<y);S++,A*=.01){let T=m(h(E-A)),L=m(h(E+A)),C=T-2*R+L;if(!(C<0))break;let q=A*(T-L)/(2*C);if(!(Math.abs(q)<A))break;let F=m(h(E+q));F>R*(1+2**-50)&&(E+=q,R=F)}R>g&&(g=R,b=h(E))}return{value:g,at:b}}function vn(e){let t=kt(e);return t.some(n=>typeof n=="string")?t.map(n=>typeof n=="number"&&!Number.isNaN(n)?ut(n):n):t}function Ar(e,t){return typeof e=="number"&&typeof t=="number"?e-t:wn(String(e),String(t))}function or(e,t,n="drop"){if(t===void 0){let c=e;if(c.length>0&&typeof c[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let a=c.map(m=>Array.from(m).filter(u=>!Number.isNaN(u)));if(n==="error"&&a.some(m=>m.length===0))throw new RangeError("all groups must contain data");let l=[],f=[];return a.forEach((m,u)=>{m.length!==0&&(l.push(String(u+1)),f.push(m))}),{labels:l,groups:f}}let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=vn(t),s=new Map;for(let c=0;c<r.length;c++){let a=r[c],l=o[c];if(Number.isNaN(a)||typeof l=="number"&&Number.isNaN(l))continue;let f=s.get(l);f===void 0&&s.set(l,f=[]),f.push(a)}let i=Array.from(s.keys()).sort(Ar);return{labels:i.map(ut),groups:i.map(c=>s.get(c))}}function Sr(e,t,n="drop"){return or(e,t,n).groups}function na(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:tn(t[n/2-1],t[n/2])}function Pi(e){if(e.length===0)throw new RangeError("'y' must have at least one row");let t=e[0].length;if(e.some(n=>n.length!==t))throw new RangeError("all rows of 'y' must have the same length");return e.filter(n=>n.every(r=>!Number.isNaN(r))).map(n=>Array.from(n))}var um=["correct"];function G0(e,t,n={}){vt(t)&&K(t,um,"mcnemarTest"),K(n,um,"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=X0(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=hm(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+=hm(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:yt(i,a/2,2,!1,!1),warnings:[]}}function hm(e,t,n){let r=t+n;return r<1/0?Vt(e,r):Vt(P(e,-520),P(t,-1040)+P(n,-1040))}function X0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=vn(e),o=vn(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(Ar),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 Q0=["ratio","alternative","confLevel"],Vi=2**-1022;function pm(e,t,n={}){if(K(n,Q0,"varTest"),typeof e=="string"){let v=er(e,t);return pm(v.x,v.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=n.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let i=Ze(n.alternative),c=n.confLevel??.95;Lt(c);let a=r.length-1,l=o.length-1;if(a<1)throw new Error("not enough 'x' observations");if(l<1)throw new Error("not enough 'y' observations");let[f,m]=Dt([r,o]).variance,u=0;if(!(f>=Vi&&m>=Vi)){let v=Dt([r]),M=Dt([o]);f=v.variance[0],m=M.variance[0],u=2*(v.k-M.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:Le(f/m,u),d=h/s,w=dt(d,a,l,!0,!1),g=dt(d,a,l,!1,!1),b,N;if(i==="less")b=w,N=[0,h/pn(1-c,a,l,!0,!1)];else if(i==="greater")b=g,N=[h/pn(c,a,l,!0,!1),1/0];else{b=Math.min(1,2*Math.min(w,g));let v=(1-c)/2,M=v<1e-8?pn(v,a,l,!1,!1):pn(1-v,a,l,!0,!1);N=[h/M,h/pn(v,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:d,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:b,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function dm(e,t){if(typeof e=="string"){let d=Lo(e,t);return dm(Dl(d.values,d.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(d=>Array.from(d).filter(Number.isFinite));if(o.some(d=>d.length<2))throw new RangeError("there must be at least 2 observations in each group");let s=o.map(d=>d.length-1),i=Dt(o).variance,c=0,a=0,l=0,f=0;for(let d=0;d<r;d++)c+=s[d],a+=s[d]*i[d],l+=s[d]*Math.log(i[d]),f+=1/s[d];a/=c;let m=c*Math.log(a)-l;if(i.some(d=>d<Vi)){let d=o.map(M=>{let p=Dt([M]);return{w:p.variance[0],e:p.k}}),w=Math.max(...d.map(M=>M.e)),g=d.map(M=>Math.log(M.w)+2*(M.e-w)*Math.LN2),b=g.map((M,p)=>Math.log(s[p])+M),N=Math.max(...b),v=N+Math.log(b.reduce((M,p)=>M+Math.exp(p-N),0))-Math.log(c);m=c*v-g.reduce((M,p,y)=>M+s[y]*p,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:yt(u,h/2,2,!1,!1),warnings:[]}}var Z0=["alternative"];function $i(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:yt(n,r/2,2,!1,!1),warnings:[]}}function gm(e){let t=0;for(let n of yn(e))t+=n*n*n-n;return t}function ra(e,t){if(typeof e=="string"){let l=Lo(e,t);return ra(l.values,l.group.codes)}let n=Sr(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=Nt(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-gm(o)/(s*s*s-s)),$i("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function zi(e,t){if(typeof e=="string")return zi(Ba(e,t));let n=Pi(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=Nt(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=gm(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 $i("Friedman rank sum test","Friedman chi-squared",a,o-1)}function J0(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{...zi(e),method:"Cochran's Q test",statisticName:"Q"}}function wm(e,t){if(typeof e=="string")return wm(Ba(e,t));let n=Pi(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(w=>gt(w)-ln(w));s.some(w=>w===1/0)&&n.every(w=>w.every(Number.isFinite))&&(s=Se(n).groups.map(w=>gt(w)-ln(w)));let i=Nt(s),c=new Array(o).fill(0),a=0;n.forEach((w,g)=>{let b=Nt(w);for(let N=0;N<o;N++){let v=i[g]*(b[N]-(o+1)/2);a+=v*v,c[N]+=v}});let l=0;for(let w of c)l+=w*w;l/=r;let f=o-1,m=(r-1)*(o-1),u,h,d=[f,m];if(a===l){u=NaN,d=[NaN,NaN];let w=0;for(let g=2;g<=o;g++)w+=Math.log(g);h=Math.exp((1-r)*w)}else u=(r-1)*l/(a-l),h=dt(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:d,parameterName:["num df","denom df"],pValue:h,warnings:[]}}function bm(e,t){if(typeof e=="string"){let h=Lo(e,t);return bm(h.values,h.group.codes)}let n=Sr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=h=>h.flatMap(d=>{let w=na(d);return d.map(g=>Math.abs(g-w))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(h=>h===1/0)&&n.every(h=>h.every(Number.isFinite))&&(s=o(Se(n).groups));let i=s.length,a=Nt(s).map(h=>le((1+h/(i+1))/2)),l=0;for(let h of a)l+=h;let f=l/i,m=0,u=0;for(let h of n){let d=0;for(let w=0;w<h.length;w++)d+=a[u+w];u+=h.length,m+=h.length*(d/h.length-f)**2}return m/=st(a),$i("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function vm(e,t,n={}){if(K(n,Z0,"moodTest"),typeof e=="string"){let M=er(e,t);return vm(M.x,M.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ze(n.alternative),i=r.length,c=o.length,a=i+c;if(a<3)throw new RangeError("not enough observations");if(i===0||c===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:a},(M,p)=>p).sort((M,p)=>l[M]-l[p]),m=new Array(a),u=0;for(;u<a;){let M=u;for(;M+1<a&&l[f[M+1]]===l[f[u]];)M++;let p=0;for(let y=u;y<=M;y++)p+=(y+1-(a+1)/2)**2;p/=M-u+1;for(let y=u;y<=M;y++)m[f[y]]=p;u=M+1}let h=0;for(let M=0;M<i;M++)h+=m[M];let d=0;for(let M of m)d+=M;d/=a;let w=0;for(let M of m)w+=(M-d)**2;let g=i*d,b=i*c/(a*(a-1))*w,N=(h-g)/Math.sqrt(b),v=s==="less"?se(N):s==="greater"?se(N,{lowerTail:!1}):2*se(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:v,warnings:[]}}function Tr(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var eg=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],tg=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],ng=[.544,-.39978,.025054,-6714e-7],rg=[1.3822,-.77857,.062767,-.0020322],og=[-1.5861,-.31082,-.083751,.0038915],sg=[-.4803,-.082676,.0030302],ag=[-2.273,.459];function ig(e){if(!(e>=3)||e!==Math.floor(e))throw new RangeError("n must be an integer of at least 3");let t=new Float64Array(e);if(e===3)return t[0]=-Math.SQRT1_2,t[2]=Math.SQRT1_2,t;let n=Math.floor(e/2),r=new Float64Array(n),o=0;for(let m=0;m<n;m++)r[m]=-le((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),i=1/Math.sqrt(e),c=r[0]/s+Tr(eg,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Tr(tg,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 ym(e){let t=Se([e]).groups[0],n=t.length,r=ig(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 cg=1.90985931710274,lg=1.0471975511966;function Nm(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=cg*(Math.asin(Math.sqrt(1-e))-lg);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Tr(ag,t);if(n>=i)return 1e-99;let c=Tr(ng,t),a=Math.exp(Tr(rg,t));return se(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Tr(og,r),s=Math.exp(Tr(sg,r));return se(n,{mean:o,sd:s,lowerTail:!1})}var Rm=Math.log(2*Math.PI),Mm=2e-14,Em=500;function fg(e){return Math.exp(-.5*e*e-.5*Rm)}function mg(e){return se(e,{logP:!0})}function Am(e,t=-1/0,n=!1){let r=t===-1/0?Ka(e,0,2,0,Mm,Em):Ka(e,t,1,0,Mm,Em),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 xm(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Am(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=mg(l);continue}let f=(r[a]*i-e)/o[a],m=se(f)+se(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?se(l)-se(f):se(f,{lowerTail:!1})-se(l,{lowerTail:!1})))}return fg(i)*-Math.expm1(c)})}function kr(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,xm(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Rm-ul(o);return Math.min(1,Am(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*xm(e*Math.exp(c),t,r)},-1/0,!0))}var ug=["pAdjustMethod"],hg=["pAdjustMethod","alternative"],pg=["alternative"],dg=["pAdjustMethod","alternative"],gg=["method","altp"],wg=["alternative"],Sm="Ties are present. z-quantiles were corrected for ties.";function jn(e,t){let{labels:n,groups:r}=or(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(i=>i.length);return{labels:n,groups:r,n:s,N:s.reduce((i,c)=>i+c,0),k:o}}function Wi(e){let t=e.groups.flat(),n=Nt(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 yn(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function Uo(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 bg(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=Jr(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 sa(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 ji(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 oa=e=>se(e,{lowerTail:!1}),Ki=(e,t,n)=>Rs(Math.abs(e),t,n,{lowerTail:!1});function Bi(e){let t=Dt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Ho(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Ho(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 vg(e,t,n={}){K(n,ug,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=jn(e,t),{R:s,C:i}=Wi(o),c=o.N*(o.N+1)/12-i,a=Uo(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=bg(a.map(f=>f.map(m=>2*oa(m))),r);return X("kwAllPairsDunnTest",sa(o,"Dunn's all-pairs test",a,l,r,i!==0?[Sm]:[]))}function yg(e,t,n={}){K(n,hg,"kwManyOneDunnTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=jn(e,t),{R:i,C:c}=Wi(s),a=s.N*(s.N+1)/12-c,l=s.n.slice(1).map((h,d)=>d+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=Tm(f,m,r,o);return X("kwManyOneDunnTest",ji(s,"Dunn's many-to-one test",f,u,o,c!==0?[Sm]:[]))}function Tm(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?kr(o,t,1/0,!0):kr(n==="greater"?o:-o,t,1/0,!1)):Jr(e.map(o=>n==="two.sided"?2*oa(Math.abs(o)):oa(n==="greater"?o:-o)),r)}function Ng(e,t,n={}){K(n,pg,"dunnettTest");let r=Ze(n.alternative),o=jn(e,t),{m:s,s2:i,df:c}=Bi(o),a=Math.sqrt(i),l=o.n.slice(1).map((h,d)=>d+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*kr(Math.abs(h),m,c,!1)):kr(r==="greater"?h:-h,m,c,!1));return X("dunnettTest",ji(o,`Dunnett's-test for multiple
|
|
4
|
+
comparisons with one control`,f,u,"single-step"))}function Mg(e,t,n={}){K(n,dg,"manyOneUTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=jn(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
|
|
5
5
|
|
|
6
|
-
k = 3 groups are required.`);let i=s.n[0],c=s.n.slice(1).map((m,u)=>u+1),a=c.map(m=>{let u=s.n[m],h=[...s.groups[0],...s.groups[m]],d=Nt(h),w=0;for(let x=0;x<i;x++)w+=d[x];let g=i+u,b=g*(g+1)/2-w,N=i*u+i*(i+1)/2-w,v=i*u+u*(u+1)/2-b,M=0;for(let x of yn(h))M+=x*x*x-x;let p=i*u/(g*(g-1))*((g*g*g-g)/12-M/12),y=(Math.min(N,v)-i*u/2)/Math.sqrt(p);return w<b?-y:y}),l=c.map(m=>s.n[m]/(s.n[m]+i)),f=
|
|
7
|
-
for multiple comparisons with one control`,a,f,o))}function xg(e,t){let n=jn(e,t),r=Bo(n.k,(s,i)=>{let c=n.n[s],a=n.n[i],l=[...n.groups[s],...n.groups[i]],f=Nt(l),m=0;for(let b=0;b<c;b++)m+=f[b];let u=m-c*(c+1)/2,h=Math.min(u,c*a-u),d=c+a,w=0;for(let b of yn(l))w+=b*b*b-b;let g=c*a/(d*(d-1))*((d*d*d-d)/12-w/12);return Math.SQRT2*(h-c*a/2)/Math.sqrt(g)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:ji(i,n.k,1/0)));return X("dscfAllPairsTest",sa(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function Rg(e,t){let n=jn(e,t),r=Pt(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),i=Bo(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),c=Bo(n.k,(a,l)=>{let f=Nl([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return ji(i[a-1][l],n.k,f)});return X("gamesHowellTest",sa(n,"Games-Howell test",i,c,"none"))}function Ag(e,t){let n=jn(e,t),{m:r,s2:o,df:s}=Ki(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let i=Bo(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:ji(l,n.k,s)));return X("tukeyTest",sa(n,"Tukey's test",i,c,"single-step"))}function Sg(e,t,n={}){K(n,wg,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=jn(e,t),i=ra(e,t).statistic,{R:c,C:a}=zi(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let d=1;d<s.k;d++)for(let w=0;w<d;w++)f.push((c[w]-c[d])/Math.sqrt(l*(1/s.n[w]+1/s.n[d]))),m.push(`${s.labels[w]} - ${s.labels[d]}`);let u=f.map(d=>(o?2:1)*oa(Math.abs(d))),h=Tg(u,r);return{chi2:i,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function Tg(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 kg(e,t,n={}){K(n,bg,"glhtDunnett");let r=Ze(n.alternative),o=jn(e,t),{m:s,s2:i,df:c,k:a}=Ki(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]),d=m.map(N=>D(N,a)),w=u.map(N=>D(N,a)),g=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),b=h.map(N=>r==="two.sided"?kr(N,g,c,!0):kr(r==="greater"?N:-N,g,c,!1));return{names:f.map(N=>`${o.labels[N]} - ${o.labels[0]}`),estimate:d,se:w,t:h,p:b,stdError:w,tValue:h,pValue:b,df:c,warnings:[]}}var Tm=["varEqual"],qg=2**-1022;function qm(e,t,n={}){if(typeof e!="string"&&vt(t)&&K(t,Tm,"onewayTest"),K(n,Tm,"onewayTest"),typeof e=="string"){let g=Dl(e,t);return qm(g.values,g.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=Sr(e,r),s=Pt(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,b)=>g+b,0),h,d,w;if(c){let g=s.grandMean,b=0,N=0;for(let v=0;v<a;v++)b+=l[v]*(f[v]-g)**2,N+=(l[v]-1)*m[v];d=u-a,Ho(d,s.ssWithin),h=b/(a-1)/(N/d)}else if(m.some(g=>!(g>=qg))&&(w=_g(o))!==void 0){let g=w.map((E,R)=>km(l[R]/E.variance,-2*E.k)),b=Math.max(...g.map(E=>E.e)),N=g.map(E=>Le(E.m,E.e-b)),v=N.reduce((E,R)=>E+R,0),M=0;for(let E=0;E<a;E++){let R=N.reduce((A,S,T)=>T===E?A:A+S,0);M+=(R/v)**2/(l[E]-1)}M/=a*a-1;let p=[];for(let E=0;E<a;E++)for(let R=E+1;R<a;R++){let[A,S]=w[E].k>=w[R].k?[w[E],w[R]]:[w[R],w[E]],T=A.mean-Le(S.mean,S.k-A.k);if(T===0)continue;let L=km(T,A.k);p.push({m:g[E].m*g[R].m*L.m*L.m/v,e:g[E].e+g[R].e+2*L.e-b})}let y=Math.max(...p.map(E=>E.e)),x=p.reduce((E,R)=>E+Le(R.m,R.e-y),0);h=(p.length===0?0:Le(x,y))/((a-1)*(1+2*(a-2)*M)),d=1/(3*M)}else if(m.some(g=>g===0)){let g=m.flatMap((M,p)=>M===0?[p]:[]);if(g.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let b=g[0],N=0,v=0;for(let M=0;M<a;M++)M!==b&&(N+=1/(l[M]-1),v+=l[M]/m[M]*(f[M]-f[b])**2);N/=a*a-1,h=v/((a-1)*(1+2*(a-2)*N)),d=1/(3*N)}else{let g=l.map((p,y)=>p/m[y]),b=g.reduce((p,y)=>p+y,0),N=0,v=0;for(let p=0;p<a;p++)N+=(1-g[p]/b)**2/(l[p]-1),v+=g[p]*f[p];N/=a*a-1,v/=b;let M=0;for(let p=0;p<a;p++)M+=g[p]*(f[p]-v)**2;h=M/((a-1)*(1+2*(a-2)*N)),d=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,d],parameterName:["num df","denom df"],pValue:dt(h,a-1,d,!1,!1),warnings:[]}}function Ig(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=vm(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:ym(r,n),warnings:[]}}function _g(e){let t=[];for(let n of e){let r=Pt([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function km(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=Le(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function Ui(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 Lg([[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]=Nn(f,o,s,i,!1);return{lo:c,d:l}}function Cg(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 aa=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=Lg(o);n+=s.lo,r=Cg(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=Ui(r,0,n);for(let c=0;c<n;c++)r[c]=r[c]/i;return r}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)r=he(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(t,n,r){let o=this.density(n),s=t-this.lo;return r?Ui(o,s,o.length):Ui(o,0,s+1)}};function Bi(e,t){return tr(e,t)}function Im(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Bi(r=>e.mean(r)-t,n>t)}function Hi(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Bi(o=>n-e.cdf(t,o,!1),r<n)}function Yi(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Bi(o=>e.cdf(t,o,!0)-n,r>n)}var Og=["alternative","correct","exact","confLevel"];function Fg(e,t={}){K(t,Og,"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(Gi);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=Ze(t.alternative),c=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(Lt(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?Vg(f,i,l):Pg(f,s,i,c,l)}return $g(e,s,r,o)}function Gi(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Cm(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 Om(e,t){return e.map(n=>n.map(r=>r.map(o=>D(o,-t))))}function _m(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,d]],w)=>{let g=t[w],b=m+u,N=h+d,v=m+h,M=u+d;r+=m;let p=b*v/g;o+=Number.isFinite(p)?p:b/g*v;let y=b*N*v*M/(g*g*(g-n));s+=Number.isFinite(y)?y:b/g*(N/g)*(v/(g-n))*M;let x=(m+d)/g,E=(u+h)/g,R=m*d/g,A=u*h/g,S=Number.isFinite(R)?R:m/g*d,T=Number.isFinite(A)?A:u/g*h;i+=S,c+=T,a+=x*S,l+=x*T+E*S,f+=E*T}),{sa:r,se:o,sv:s,sR:i,sS:c,sPR:a,sPSQR:l,sQS:f}}function Pg(e,t,n,r,o){let s=_m(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=Cm(e);let x=Om(e,i);s=_m(x,x.map(Gi),D(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:d}=s,w=c-a,g=D(.5,-i),b=r&&Math.abs(w)>=g?g:0,N=D(Vt(Math.abs(w)-b,l),i),v=n==="two.sided"?yt(N,.5,2,!1,!1):se(Math.sign(w)*Math.sqrt(N),{lowerTail:n==="less"}),M=f/m,p=D(Math.sqrt(u/(2*f*f)+h/(2*f*m)+d/(2*m*m)),-i/2),y;if(n==="less")y=[0,M*Math.exp(le(o)*p)];else if(n==="greater")y=[M*Math.exp(le(o,{lowerTail:!1})*p),1/0];else{let x=(1-o)/2,E=x<1e-8?le(x,{lowerTail:!1}):le((1+o)/2);y=[M*Math.exp(-E*p),M*Math.exp(E*p)]}return{method:`Mantel-Haenszel chi-squared test ${b!==0?"with":"without"} continuity correction`,alternative:n,statistic:N,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:v,confInt:y,confLevel:o,estimate:M,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var Dg=2**24;function Vg(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>Dg)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new aa(e),s=e.reduce((a,l)=>a+l[0][0],0),i;if(t==="two.sided"){let a=o.density(1),l=a[s-o.lo]*(1+1e-7),f=[0,0];for(let m of a)m<=l&&(f=he(f,[m,0]));i=Math.min(1,f[0])}else i=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,Hi(o,s,1-n)];else if(t==="greater")c=[Yi(o,s,1-n),1/0];else{let a=(1-n)/2;c=[Yi(o,s,a),Hi(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:Im(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function $g(e,t,n,r){let{diff:o,V:s}=Lm(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=Cm(e);let f=Om(e,i);({diff:o,V:s}=Lm(f,f.map(Gi),n,r,D(1,-i)))}let c=o.length,a=nt(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*a[f];return l=D(l,i),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:yt(l,c/2,2,!1,!1),warnings:[]}}var zg=2**-1022;function Lm(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(d=>d.reduce((w,g)=>w+g,0)),u=Array.from({length:r},(d,w)=>a.reduce((g,b)=>g+b[w],0)),h=1/(f*f*(f-o));for(let d=0;d<r-1;d++)for(let w=0;w<n-1;w++){let g=w+(n-1)*d,b=m[w]*u[d]/f;i[g]+=a[w][d]-(Number.isFinite(b)?b:Es(m[w],u[d],f));for(let N=0;N<r-1;N++)for(let v=0;v<n-1;v++){let M=(w===v?f*m[w]:0)-m[w]*m[v],p=(d===N?f*u[d]:0)-u[d]*u[N],y=h*M*p;c[g][v+(n-1)*N]+=Number.isFinite(y)&&h>=zg?y:m[w]*((w===v?1:0)-m[v]/f)*(((d===N?1:0)-u[N]/f)*(u[d]/(f-o)))}}}),{diff:i,V:c}}var Fm=new Map,Wg=2**24,jg=2**32;function Dm(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>Wg||n*r>jg)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function Kg(e,t){let n=`${e},${t}`,r=Fm.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,d=s[u],w=Array.from({length:e+1},()=>new Float64Array(i+1));for(let g=0;g<=e;g++){let b=c[g],N=e-g;if(N>h)continue;let v=N/h;for(let M=0;M<=i;M++){let p=b[M];p!==0&&(N>0&&(w[g+1][M+d]+=p*v),N<h&&(w[g][M]+=p*(1-v)))}}c=w}let a=c[e],l=0;for(;l<a.length&&a[l]===0;)l++;let f=a.length-1;for(;f>l&&a[f]===0;)f--;let m={min:l,probs:a.slice(l,f+1)};return Fm.set(n,m),m}function Xi(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}=Kg(t,n),i=dl(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 Pm=new Map;function Ug(e,t){let n=`${e},${t}`,r=Pm.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 d=0;d<=e;d++){if(e-d>o-m)continue;let w=i[d];for(let g=0;g<=s;g++){let b=w[g];b!==0&&(d<e&&(h[d+1][g+u]+=b),e-d<o-m&&(h[d][g]+=b))}}i=h}let c=i[e],a=0;for(;c[a]===0;)a++;let l=c.length-1;for(;c[l]===0;)l--;let f={min:a,counts:c.slice(a,l+1)};return Pm.set(n,f),f}function ia(e,t,n){let{min:r,counts:o}=Ug(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=Ml(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 Bg=["alternative","exact","confInt","confLevel"];function Wm(e,t,n={}){if(K(n,Bg,"ansariTest"),typeof e=="string"){let p=er(e,t);return Wm(p.x,p.y,n)}let r=Array.from(e).filter(p=>!Number.isNaN(p)),o=Array.from(t).filter(p=>!Number.isNaN(p)),s=r.length,i=o.length;if(s<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let c=Ze(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&Lt(l);let f=s+i,m=Nt(r.concat(o)),u=m.map(p=>Math.min(p,f-p+1)),h=0;for(let p=0;p<s;p++)h+=u[p];let d=new Set(m).size<f,w=n.exact??(s<50&&i<50&&!d),g=n.exact??(s<50&&i<50),b=new mt,N,v;if(w&&!d){Dm(s,i);let p=x=>Xi(x-1,s,i,!1),y=x=>Xi(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?p(h):y(h)))}else c==="less"?N=p(h):N=y(h);a&&(v=Xg(r,o,c,l,b))}else{let p=jm(h,u,d,s,i);N=c==="two.sided"?2*Math.min(se(p),se(p,{lowerTail:!1})):c==="less"?se(p,{lowerTail:!1}):se(p),a&&(v=Qg(r,o,c,l,g?new mt:b)),g&&d&&(b.add("cannot compute exact p-value with ties"),a&&b.add("cannot compute exact confidence interval with ties"))}let M={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:N,warnings:b.list};return v!==void 0&&(M.confInt=v.confInt,M.confLevel=l,M.estimate=v.estimate,M.estimateNames="ratio of scales"),M.nullValue=1,M.nullValueName="ratio of scales",X("ansariTest",M)}function jm(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 Km(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)<Hg))?Yg(e.map(f=>Vm(f,n)).concat(t.map(f=>Vm(f,1)))):Gg(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 Hg=2**-1022;function Vm(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]=$m(Math.abs(e)),[s,i]=$m(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function $m(e){let t=Math.floor(Math.log2(e)),n=D(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function 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 Yg(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 Gg(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return Nt(e);let n=Nt(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function ca(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Xg(e,t,n,r,o){let s=e.length,i=t.length,c=[];for(let b of e)for(let N of t)b/N>=0&&c.push(b/N);c.sort((b,N)=>b-N);let a=c.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:ca(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((b,N)=>Km(e,t,b+(N<a-1?(c[N+1]-b)/2:b*1.01)).ab),f=(b,N,v)=>{let M=1/0;for(let y of l)(N?y-b>0:y-b>=0)&&y-b<M&&(M=y-b);let p=v?-1/0:1/0;return l.forEach((y,x)=>{y-b===M&&(p=v?Math.max(p,c[x]):Math.min(p,c[x]))}),p},m=b=>{let N=ia(b/2,s,i),v=ia(1-b/2,s,i);if(!l.some(y=>y-N>=0)||!l.some(y=>y-v>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let M=f(N,!1,!1),p=f(v,!0,!0);return M>p?[f(v,!1,!1),f(N,!0,!0)]:[M,p]},u=ca(n,1-r,m),h=ia(.5,s,i),d=-1/0,w=1/0;l.forEach((b,N)=>{b-h<=0?d=Math.max(d,c[N]):w=Math.min(w,c[N])});let g=d===-1/0||w===1/0?NaN:(w+d)/2;return g===1/0&&(g=w/2+d/2),{confInt:u,estimate:g}}function Qg(e,t,n,r,o){let s=e.length,i=t.length,c=(b,N)=>{let{ab:v,a:M,ties:p}=Km(e,t,b);return jm(v,M,p,s,i)-N},a=[],l=(b,N)=>{b.length===0||N.length===0||a.push(ln(b)/gt(N),gt(b)/ln(N))};if(l(e.filter(b=>b>0),t.filter(b=>b>0)),l(e.filter(b=>b<=0),t.filter(b=>b<0)),a.some(b=>!Number.isFinite(b))){o.add("cannot compute asymptotic confidence set or estimator");return}if(a.length===0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),{confInt:ca(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=ln(a),m=gt(a),u=b=>Kt(N=>c(N,b),f,m,{tol:1e-4}),h=b=>{let N=le(b/2),v=le(b/2,{lowerTail:!1});if(c(f,N)>0||c(m,v)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let M=u(N),p=u(v);return M<=p?[M,p]:[p,M]},d=ca(n,1-r,h),w=c(f,0)>0||c(m,0)<0;w&&o.add("cannot compute estimate, returning NA");let g=w?NaN:u(0);return{confInt:d,estimate:g}}var Zg=["alternative","confLevel"];function Jg(e,t,n={}){K(n,Zg,"brunnerMunzelTest");let r=Array.from(e).filter(E=>!Number.isNaN(E)),o=Array.from(t).filter(E=>!Number.isNaN(E)),s=r.length,i=o.length;if(s<2)throw new RangeError("not enough 'x' observations");if(i<2)throw new RangeError("not enough 'y' observations");let c=Ze(n.alternative),a=n.confLevel??.95;fn(a);let l=Nt(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=E=>E.reduce((R,A)=>R+A,0)/E.length,h=u(f),d=u(m),w=(E,R,A)=>{let S=(E.length+1)/2,T=0;for(let L=0;L<E.length;L++)T+=(E[L]-R[L]-A+S)**2;return T/(E.length-1)},g=w(f,Nt(r),h),b=w(m,Nt(o),d),N=(d-(i+1)/2)/s,v=s*g+i*b,M=s*i*(d-h)/(s+i)/Math.sqrt(v),p=v**2/((s*g)**2/(s-1)+(i*b)**2/(i-1)),y;c==="greater"?y=ft(M,p,!0,!1):c==="less"?y=ft(M,p,!1,!1):y=2*ft(-Math.abs(M),p,!0,!1);let x=bt((1-a)/2,p,!1,!1)*Math.sqrt(g/(s*i**2)+b/(i*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:M,statisticName:"Brunner-Munzel Test Statistic",parameter:p,parameterName:"df",pValue:y,confInt:[N-x,N+x],confLevel:a,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var ew=[1.2337005501361697,7831619385924639e-32];function tw(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let d=0;d<t;d++){let w=f[h*t+d];if(w!==0)for(let g=0;g<t;g++)u[h*t+g]+=w*m[d*t+g]}return u},o=f=>{let m=0;for(let d of f)m=Math.max(m,Math.abs(d));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 d=0;d<f.length;d++)f[d]*=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 nw=2**33;function Bm(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e)||t<=1/(2*e)||t>=1||e*t<1)return;let n=2*Math.floor(e*t)+1,r=e.toString(2),o=Math.max(1,r.length-1+(r.split("1").length-2));if(n**3*o>nw)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 Hm(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]=en(e,t),u=2*f-1+2*m,h=1,d=0;for(let w=1;w<=e;w++)h*=w/e*u,h<2**-500&&(h*=2**500,d-=500);for(;d<-1e3;)h*=2**-1e3,d+=1e3;return h*2**d}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 d=2;d<=u;d++)h*=d;s[f*r+m]/=h}}let[i,c]=tw(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 Qi(e,t=!0){if(Number.isNaN(e))return NaN;if(e<=0)return t?0:1;if(e===1/0)return t?1:0;if(e<1){let o=at(ew,en(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 Zi(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]=en(e,t),o=Math.floor(e-n-r),s=0;for(let i=0;i<=o;i++){let c=he(Ft(e-i,-n),[-r,0]);if(c[0]<=0)break;let a=he(Ft(i,n),[r,0]),l=at(a,[e,0]),f=at(c,[e,0]);s+=pl(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var Um=2**28;function Ym(e,t){let[n,r]=en(e,t),o=Math.floor(n);o===n&&r<0&&(o-=1);let s=[];for(let h=0;h<=e;h++){let d=(h-n-r)/e;d>0&&d<1&&s.push({t:d,up:!0,i:h});let w=(h+n+r)/e;w>0&&w<1&&s.push({t:w,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>Um)return NaN;s.sort((h,d)=>h.t-d.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 d=(h.t-f)/(1-f);if(d>0){let w=d/(1-d);a.fill(0);for(let g=l;g<i;g++){let b=c[g];if(b===0)continue;let N=e-g,v=Math.min(N,Math.floor((N+1)*d)),M=i-g,p=dn(v,N,d,!1);for(let y=v;y<=N&&p!==0;y++)y>v&&(p=p*(N-y+1)*w/y),y>=M?m+=b*p:a[g+y]+=b*p,u++;p=v>0?dn(v-1,N,d,!1):0;for(let y=v-1;y>=0&&p!==0;y--)y<v-1&&(p=p*(y+1)/((N-y)*w)),y>=M?m+=b*p:a[g+y]+=b*p,u++}if(u>2*Um)return NaN;[c,a]=[a,c]}if(f=h.t,h.up)i=h.i+1;else{for(let w=l;w<=h.i;w++)m+=c[w],c[w]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function Gm(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,d)=>h-d);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 d=u*n-h*t;return(o==="two.sided"?Math.abs(d):o==="greater"?d:-d)>=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 d=u+h,w=(u>0?u/d*l[h]:0)+(h>0?h/d*f[h-1]:0);(i===void 0?d<s:i[d])&&a(u,h)?(w>0&&(m+=w*Nn(u,t,n,d,!1)),f[h]=0):f[h]=w}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var rw=["alternative","exact"];function Xm(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 Qm(e,t,n={}){if(K(n,rw,"ksTest"),typeof e=="string"){let N=er(e,t);return Qm(N.x,N.y,n)}let r=Array.from(e).filter(N=>!Number.isNaN(N)).sort((N,v)=>N-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=Ze(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,v=-1/0;for(let E=0;E<o;E++){let R=t(r[E]);if(!(R>=0&&R<=1))throw new RangeError(`the distribution function returned ${R} at ${r[E]}, not a value in [0, 1]`);N=Math.max(N,(E+1)/o-R),v=Math.max(v,R-E/o)}let M=s==="two.sided"?Math.max(N,v):s==="greater"?N:v,p=new Set(r).size<o,y=n.exact??(o<100&&!p),x;if(y&&s==="two.sided"&&M<.5&&Bm(o,M),s==="two.sided")if(!y)x=Qi(Math.sqrt(o)*M,!1);else{let E=2*Zi(o,M);if(M>=.5)x=Math.min(1,E);else{let R=E<.01?Ym(o,M):NaN;x=Number.isNaN(R)?1-Hm(o,M):R}}else x=y?Zi(o,M):Math.exp(-2*o*M*M);return X("ksTest",{method:`${y?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Xm(s,!0),exact:y,statistic:M,statisticName:i,pValue:Math.min(1,Math.max(0,x)),warnings:p?["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 d=s==="two.sided"?Math.max(u,h):s==="greater"?u:h,w=new Set(l.map(N=>N[0])).size<l.length,g=n.exact??o*a<1e4,b;if(g)b=Gm(d,o,a,w?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*a/(o+a);b=s==="two.sided"?Qi(Math.sqrt(N)*d,!1):Math.exp(-2*N*d*d)}return X("ksTest",{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Xm(s,!1),exact:g,statistic:d,statisticName:i,pValue:Math.min(1,Math.max(0,b)),warnings:w&&!g?["p-value will be approximate in the presence of ties"]:[]})}var ow=["poolSd","paired","pAdjustMethod","alternative"],sw=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],aw=["pAdjustMethod","correct"];function Zm(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=vn(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(Ar);return{levels:i,groups:i.map(c=>o.get(c))}}function Ji(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=Zr(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 ec(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(ut),colNames:t.slice(0,-1).map(ut),pAdjustMethod:r,warnings:o}}function iw(e,t,n={}){K(n,ow,"pairwiseTTest");let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let s=Ze(n.alternative),i=n.pAdjustMethod??"holm",{levels:c,groups:a}=Zm(e,t,!r),l=c.length;if(l<2)throw new RangeError("there must be at least two groups");let f,m;if(o){m="t tests with pooled SD";let u=a.map(b=>b.length),h=Pt(a),d=h.mean,w=0;for(let b of u)b>1&&(w+=b-1);Ho(w,h.ssWithin);let g=Math.sqrt(h.ssWithin/w);f=(b,N)=>{let v=(d[b]-d[N])/(g*Math.sqrt(1/u[b]+1/u[N]));return s==="less"?ft(v,w,!0,!1):s==="greater"?ft(v,w,!1,!1):2*ft(-Math.abs(v),w,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>rt(()=>En(a[u],a[h],{paired:r,alternative:s})).pValue;return ec(m,c,Ji(l,f,i),i)}function cw(e,t,n={}){K(n,sw,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=Zm(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=Ji(l,(h,d)=>{let w=rt(()=>zo(a[h],a[d],{...s,...o===void 0?{}:{alternative:o}}));f=w.method;for(let g of w.warnings)m.add(g);return w.pValue},i);return X("pairwiseWilcoxTest",ec(f,c,u,i,m.list))}function lw(e,t,n={}){if(K(n,aw,"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=Ji(s,(m,u)=>{let h=rt(()=>bi([r[m],r[u]],[o[m],o[u]],{correct:c}));for(let d of h.warnings)a.add(d);return h.pValue},i),f=Array.from({length:s},(m,u)=>u+1);return X("pairwisePropTest",ec("Pairwise comparison of proportions",f,l,i,a.list))}var Yo=[0,0];function la(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=Pe(e,en(t,t));return Ft(t,n[0]/(2*t))}function fa(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:la([e[r],0]))}function eo(e,t){return Kn(e,t,[]).fit}function Kn(e,t,n){let r=t.length,o=Math.min(e.length,r),s=e.map(f=>f.slice()),i=t.slice(),c=n.map(f=>f.slice()),a=[];for(let f=0;f<o;f++){let m=s[f],u=Yo;for(let b=f;b<r;b++)u=he(u,_e(m[b],m[b]));if(u[0]===0){a.push(Yo);continue}let h=la(u),d=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=he(m[f],d);let w=_e(d,m[f]),g=b=>{let N=Yo;for(let M=f;M<r;M++)N=he(N,_e(m[M],b[M]));let v=at(N,w);for(let M=f;M<r;M++)b[M]=Pe(b[M],_e(v,m[M]))};for(let b=f+1;b<e.length;b++)g(s[b]);g(i);for(let b of c)g(b);a.push([-d[0],-d[1]])}let l=new Array(o).fill(Yo);for(let f=o-1;f>=0;f--){let m=i[f];for(let u=f+1;u<o;u++)m=Pe(m,_e(s[u][f],l[u]));l[f]=at(m,a[f])}return{fit:{qty:i,coef:l},qtv:c}}function to(e,t=0,n=e.length){let r=Yo;for(let o=t;o<n;o++)r=he(r,_e(e[o],e[o]));return r}var fw=1e-14;function mw(e,t,n,r,o,s,i,c){let a=y=>D(y,2*i),l=t.length,f=n.flatMap((y,x)=>to(y)[0]>fw*s[x]?[x]:[]),m=0,u=[],h=t.slice(),d=new Array(l).fill(0);if(f.length>0){let y=At(Array.from({length:l},(E,R)=>f.map(A=>n[A][R][0])));m=y.rank,u=y.pivot.slice(0,m).map(E=>f[E]);let x=eo(u.map(E=>n[E]),t);h=x.qty,u.forEach((E,R)=>{for(let A=0;A<l;A++)d[A]=d[A]+n[E][A][0]*x.coef[R][0]})}let w=[];u.forEach((y,x)=>{let E=r[y],R=w.find(A=>A.term===E);R===void 0&&w.push(R={term:E,df:0,ss:[0,0]}),R.df++,R.ss=he(R.ss,_e(h[x],h[x]))});let g=l-m,b=to(h,m)[0],N=ki(d,b,g),v=w.filter(y=>y.term!==0);g>0&&xr(c,b)&&(b=0,v=v.map(y=>xr(c,y.ss[0])?{...y,ss:[0,0]}:y));let M=v.map(y=>o[y.term-1]),p=y=>{if(g>0){let x=b/g,E=v.map(({df:R,ss:A})=>{let S=A[0]/R/x;return[R,y(A[0]),y(A[0]/R),S,dt(S,R,g,!1,!1)]});return E.push([g,y(b),y(x),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...M,"Residuals"],table:E}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:M,table:v.map(({df:x,ss:E})=>[x,y(E[0]),y(E[0]/x)])}};return[p(a),p(y=>y),N]}function Jm(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=ks({response:t.response,rhs:i},Object.keys(o)).variables,a=zn(t,o,{...s,dropUnusedLevels:!0,contrastVariables:c},i),l=a.y;if(l===void 0)throw new TypeError("the formula has no response");if(l.some(q=>!Number.isFinite(q)))throw new RangeError("NA/NaN/Inf in 'y'");let f=zn({response:t.response,rhs:r},o,{...s.levels===void 0?{}:{levels:s.levels},allContrasts:"contr.helmert",intercept:a.terms.intercept,extraVariables:a.terms.variables,dropUnusedLevels:!0}),m=l.length,u=[...a.warnings,...f.warnings.filter(q=>!a.warnings.includes(q))],h=f.colNames.length,d=f.colNames.map((q,O)=>Se([f.x.map(_=>_[O])]).groups[0]),w=f.x.map((q,O)=>d.map(_=>_[O])),g=h>0?At(w):void 0,b=g?.rank??0;b<h&&u.push("Error() model is singular");let N=g?.pivot.slice(0,b)??[],v=q=>q.map(O=>[O,0]),M=a.colNames.map((q,O)=>v(Se([a.x.map(_=>_[O])]).groups[0])),{groups:p,k:y}=Se([l]),{fit:x,qtv:E}=Kn(N.map(q=>v(d[q])),v(p[0]),M),R=p[0].map(q=>[1,q]),A=M.map(q=>to(q)[0]),S=[];N.forEach((q,O)=>{let _=f.assign[q],F=_===0?"(Intercept)":f.terms.termLabels[_-1],I=S.find(P=>P.name===F);I===void 0&&S.push(I={name:F,idx:[]}),I.idx.push(O)}),m>b&&S.push({name:"Within",idx:Array.from({length:m-b},(q,O)=>b+O)});let T=S.map(q=>mw(q.name,q.idx.map(O=>x.qty[O]),E.map(O=>q.idx.map(_=>O[_])),a.assign,a.terms.termLabels,A,y,R)),L=T.map(q=>q[0]),C={formula:e,multistratum:!0,strata:L,rows:a.frame.rows,warnings:u};return Ut.set(C,T.map(q=>q[1])),T.some((q,O)=>q[2]&&S[O].name!=="(Intercept)")&&qi.set(C,[Ti]),C}function qr(e){let t=e.length,n=new Array(t).fill(0);for(let o=0;o<t;o++){let s=0;for(let i=0;i<=o;i++)s=Math.max(s,Math.abs(e[i][o]));s>0&&Number.isFinite(s)&&(s<2**-400||s>2**400)&&(n[o]=Math.floor(Math.log2(s)))}let r=n.every(o=>o===0)?e.map(o=>o.slice()):e.map(o=>o.map((s,i)=>D(s,-n[i])));return{inv:Bl(r),e:n}}var eu=new WeakMap;function no(e,t){return t!==void 0&&eu.set(e,t),e}function tc(e,t){return eu.get(e)??(t.length===0?0:t[t.length-1]+1)}function tu(e,t){for(let n of t)if(Object.hasOwn(e,n))return e[n].length;return Object.values(e)[0]?.length}var uw=["weights","contrasts","levels","intercept"],hw=["level"],pw=["interval","level"],nc=2**-1022;function rc(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=su(t.x,r,t.frame.weights,t.terms.intercept,!0),s=o.result,i=[...t.warnings];if(!n){let a=o.yScale??0,l=o.fitRows.map(h=>D(s.fittedValues[h],-a)),f=l.reduce((h,d)=>h+d,0)/l.length,m=l.length>1?l.reduce((h,d)=>h+(d-f)**2,0)/(l.length-1):NaN,u=(o.rssScaled??o.rss)/s.dfResidual;Number.isFinite(u)&&u<1e-30*(f*f+m)&&i.push("essentially perfect fit: summary may be unreliable")}let c={...s,formula:e,response:t.terms.variables[0],coefNames:t.colNames,assign:t.assign,termLabels:t.terms.termLabels,order:t.terms.order,variables:t.terms.variables,factors:t.terms.factors,rows:t.frame.rows,x:t.x,y:r,effects:o.effects,rss:o.rss,nObs:o.fitRows.length,aov:n,warnings:i};return t.frame.weights!==void 0&&(c.weights=t.frame.weights),c}function Go(e,t,n={}){if(K(n,uw,"lm"),typeof e=="string"){let r=zn(Jn(e),t,{...n,dropUnusedLevels:!0});return X("lm",no(rc(e,r,!1),tu(t,r.terms.variables)))}return dw(e,t,n)}function dw(e,t,n){let r=n.intercept??!0,o=e.length>0&&typeof e[0]!="number"?e.map(u=>Array.from(u)):Array.from(e,u=>[u]),s=Array.from(t);if(o.some(u=>u.length!==o[0].length))throw new TypeError("all rows of the matrix must have the same length");if(o.length!==s.length)throw new RangeError("variable lengths differ (found for 'x')");let i=n.weights===void 0?void 0:Array.from(n.weights);if(i!==void 0&&i.length!==s.length)throw new RangeError("variable lengths differ (found for '(weights)')");let c=s.map((u,h)=>!Number.isNaN(u)&&o[h].every(d=>!Number.isNaN(d))&&(i===void 0||!Number.isNaN(i[h]))),a=i?.filter((u,h)=>c[h]);if(a?.some(u=>u<0))throw new RangeError("missing or negative weights not allowed");let l=o.filter((u,h)=>c[h]).map(u=>r?[1,...u]:u),f=s.filter((u,h)=>c[h]),m=su(l,f,a,r).result;return m.rows=c.flatMap((u,h)=>u?[h]:[]),no(m,s.length)}function gw(e,t,n,r,o,s,i){let c=s.rank,a=s.pivot.slice(0,c),l=n?.map(p=>D(p,-r)),f=fa(l===void 0?void 0:o.map(p=>l[p]),o.length).map((p,y)=>{if(n===void 0||l===void 0)return p;let x=n[o[y]];if(x===0||l[o[y]]>=nc)return p;let E=la([x,0]);return[D(E[0],-r/2),D(E[1],-r/2)]}),m=a.map(p=>{let y=o.map((E,R)=>Tt(f[R],e[E][p])),x=Kl(y.map(E=>E[0]));return{c:x===0?y:y.map(E=>[D(E[0],-x),D(E[1],-x)]),e:x}}),{qty:u,coef:h}=eo(m.map(({c:p})=>p),o.map((p,y)=>Tt(f[y],t[p]))),d=h.map((p,y)=>m[y].e===0?p:[D(p[0],-m[y].e),D(p[1],-m[y].e)]),w=e[0]?.map(()=>NaN)??[];a.forEach((p,y)=>w[p]=d[y][0]);let g=e.map(p=>{let y=[0,0];return a.forEach((x,E)=>y=he(y,Tt(d[E],p[x]))),y}),b=to(u,c)[0],N=o.map(p=>l?.[p]??1),v=[0,0];if(i){let p=[0,0],y=[0,0];o.forEach((x,E)=>{p=he(p,[N[E],0]),y=he(y,Tt(g[x],N[E]))}),v=at(y,p)}let M=[0,0];return o.forEach((p,y)=>{let x=Pe(g[p],v);M=he(M,Tt(_e(x,x),N[y]))}),{coefficients:w,fittedValues:g.map(p=>p[0]),residuals:g.map((p,y)=>Pe([t[y],0],p)[0]),effects:u.map(p=>p[0]),rss:b,mss:M[0]}}function su(e,t,n,r,o=!1){let s=Se([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return nu(ru(e,t,n,r,o),e,t,n);let i=ru(e,t.map(l=>D(l,-s)),n,r,o),c=i.result,a=l=>D(l,s);return c.coefficients=c.coefficients.map(a),c.residuals=c.residuals.map(a),c.fittedValues=c.fittedValues.map(a),c.sigma=a(c.sigma),c.coefTable.estimate=c.coefTable.estimate.map(a),c.coefTable.stdError=c.coefTable.stdError.map(a),nu({...i,effects:i.effects.map(a),rss:D(i.rss,2*s),yScale:s,rssScaled:i.rss},e,t,n)}function nu(e,t,n,r){if(r===void 0)return e;let o=e.result,s=o.coefficients.flatMap((i,c)=>Number.isNaN(i)?[]:[c]);return r.forEach((i,c)=>{if(i!==0)return;let a=!1;if(!Number.isFinite(o.fittedValues[c])){let l=t[c],f=s.reduce((m,u)=>m+l[u]*o.coefficients[u],0);o.fittedValues[c]=To(f,0)?vr(s.map(m=>l[m]),s.map(m=>o.coefficients[m]),f):f,a=!0}(a||!Number.isFinite(o.residuals[c]))&&(o.residuals[c]=n[c]-o.fittedValues[c])}),e}var oc=new WeakMap;function sc(e){return oc.get(e)??0}function ww(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function ru(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map(($,Y)=>n===void 0||n[Y]!==0),i=e.filter(($,Y)=>s[Y]),c=t.filter(($,Y)=>s[Y]),a=n?.filter(($,Y)=>s[Y]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(i.some($=>$.some(Y=>!Number.isFinite(Y))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some($=>!Number.isFinite($)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some($=>!Number.isFinite($)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap(($,Y)=>s[Y]?[Y]:[]),m=a===void 0?1:gt(a),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,h=0,d=a?.map(Math.sqrt),w=d===void 0?i:i.map(($,Y)=>$.map(W=>W*d[Y])),g=At(w);a!==void 0&&Number.isFinite(m)&&u+2>0&&!ww(g)&&(h=u+2,d=a.map($=>D(Math.sqrt($),-h/2)),w=i.map(($,Y)=>$.map(W=>W*d[Y])),g=At(w),oc.set(g,h));let b=d===void 0?c:c.map(($,Y)=>$*d[Y]),N=g.rank,v=$=>D($,-u),M=o?gw(e,t,n,u,f,g,r):void 0,p=M?.coefficients??Hr(g,b),y=M?.residuals??[],x=M?.fittedValues??[],E=l-N,R=a?.map(v)??new Array(l).fill(1),A=M?.rss??0;if(M===void 0){let $=Yr(g,b),Y=d===void 0?$:$.map((B,j)=>B/d[j]),W=0;t.forEach((B,j)=>{if(s[j])y.push(Y[W]),x.push(B-Y[W]),W++;else{let te=e[j].reduce((fe,Re,Ce)=>Number.isNaN(p[Ce])?fe:fe+Re*p[Ce],0);x.push(te),y.push(B-te)}});for(let B=0;B<l;B++)A+=R[B]*Y[B]**2}let S=x.filter(($,Y)=>s[Y]),T=0;if(a!==void 0&&a.some($=>$!==0&&D($,-u)<nc)){let $=f.flatMap((Y,W)=>{let B=y[Y];return B===0?[]:[{w:a[W],r:B,a:Math.floor(Math.log2(Math.abs(B)))}]});if($.length>0&&$.every(Y=>Number.isFinite(Y.r))){let Y=Math.max(...$.map(j=>Math.log2(j.w)+2*j.a)),W=2*Math.floor(Math.floor(Y)/2),B=0;for(let j of $)B+=D(j.w,2*j.a-W)*D(j.r,-j.a)**2;A=B,T=W-u}}let L=A/E,C=D(Math.sqrt(L),(u+T)/2),q=h===0?0:(h-u)/2,O=g.qr.slice(0,N).map($=>$.slice(0,N).map(Y=>h===0?D(Y,-u/2):Y)),{inv:_,e:F}=N>0?qr(O):{inv:[],e:[]},P=g.pivot.slice(0,N).map(($,Y)=>[$,Y]).sort(($,Y)=>$[0]-Y[0]),k=P.map(([$])=>p[$]),V=P.map(([,$])=>D(Math.sqrt(_[$][$]*L),T/2-F[$]-q)),U=k.map(($,Y)=>$/V[Y]),Q=U.map($=>2*ft(-Math.abs($),E,!0,!1)),re={coefficients:p,residuals:y,fittedValues:x,rank:N,dfResidual:E,coefTable:{estimate:k,stdError:V,tValue:U,pValue:Q},sigma:C,rSquared:0,adjRSquared:0,intercept:r,qr:g,x:e,rows:e.map(($,Y)=>Y)};n!==void 0&&(re.weights=n);let z=r?1:0,J=N-z,Z={result:re,fitRows:f,effects:(M?.effects??[]).map($=>D($,u/2)),rss:D(A,u+T)};if(J===0)return Z;let ke=0;if(M!==void 0)ke=M.mss;else if(r){let $=R.reduce((W,B)=>W+B,0),Y=0;for(let W=0;W<l;W++)Y+=R[W]*S[W]/$;for(let W=0;W<l;W++)ke+=R[W]*(S[W]-Y)**2}else for(let $=0;$<l;$++)ke+=R[$]*S[$]**2;re.rSquared=ke/(ke+D(A,T)),re.adjRSquared=1-(1-re.rSquared)*((l-z)/E);let ne=D(ke/J/L,-T);return re.fStatistic={value:ne,numdf:J,dendf:E},re.fPValue=dt(ne,J,E,!1,!1),Z}function au(e,t){let n=(1-e)/2;return n<1e-8?bt(n,t,!1,!1):bt((1+e)/2,t,!0,!1)}function bw(e,t={}){K(t,hw,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=au(n,e.dfResidual),{estimate:o,stdError:s}=e.coefTable,i=e.coefficients.map(()=>NaN),c=e.coefficients.map(()=>NaN),a=0;return e.coefficients.forEach((l,f)=>{Number.isNaN(l)||(i[f]=o[a]-r*s[a],c[f]=o[a]+r*s[a],a++)}),{lower:i,upper:c}}function ou(e,t,n){if(t===0||!Number.isFinite(t))return e*t;let r=Math.floor(Math.log2(e)),o=Math.floor(Math.log2(Math.abs(t)));return D(D(e,-r)*D(t,-o),r+o+n)}function vw(e,t,n={}){K(n,pw,"predict");let o=(t.length>0&&typeof t[0]!="number"?t.map(v=>Array.from(v)):Array.from(t,v=>[v])).map(v=>e.intercept?[1,...v]:v);if(o.some(v=>v.length!==e.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let s=e.rank,i=e.qr.pivot.slice(0,s),c=e.qr.qr.slice(0,s).map(v=>v.slice(0,s)),a=-(oc.get(e.qr)??0)/2,l=e.coefficients.flatMap((v,M)=>Number.isNaN(v)?[]:[M]),f=o.map(v=>{let M=v.reduce((p,y,x)=>Number.isNaN(e.coefficients[x])?p:p+y*e.coefficients[x],0);return To(M,0)?vr(l.map(p=>v[p]),l.map(p=>e.coefficients[p]),M):M}),m=v=>{let M=new Array(s).fill(0);for(let p=0;p<s;p++){let y=v[p];for(let x=0;x<p;x++)y-=c[x][p]*M[x];M[p]=y/c[p][p]}return M},u=o.map(v=>{let M=i.map(x=>v[x]),p=Wa(m(M));if(p>=nc&&p<1/0||!M.every(Number.isFinite)||M.every(x=>x===0))return a===0||!(p>0&&p<1/0)?p*e.sigma:ou(p,e.sigma,a);let y=Math.floor(Math.log2(gt(M.map(Math.abs))));for(let x of[0,600]){let E=Wa(m(M.map(R=>D(R,-(y+x)))));if(E<1/0)return E===0?E*e.sigma:ou(E,e.sigma,y+x+a)}return p*e.sigma}),h=n.interval??"none";if(h!=="none"&&h!=="confidence"&&h!=="prediction")throw new RangeError(`invalid 'interval': ${String(h)}`);let d=Ls(e.qr,e.rank,o),w=d.length>0?{nonEstimable:d,warnings:[_s]}:{};if(h==="none")return X("predict",{fit:f,seFit:u,...w},e);let g=n.level??.95;if(!(g>0&&g<1))throw new RangeError("'level' must be between 0 and 1");let b=au(g,e.dfResidual),N=u.map(v=>b*(h==="prediction"?Math.hypot(v,e.sigma):v));return X("predict",{fit:f,seFit:u,lwr:f.map((v,M)=>v-N[M]),upr:f.map((v,M)=>v+N[M]),...w},e)}var yw=["weights","contrasts","levels"],iu=["confLevel"],cu=["sigmaX","sigmaY","mu","alternative","confLevel"];function Nw(e,t,n={}){return K(n,yw,"aov"),typeof e=="string"?X("aov",lu(e,t,n)):X("aov",Mw(e,t))}function lu(e,t,n){let r=Jn(e,!0),{treatment:o,errors:s}=Ol(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?Jm(e,r,o,s[0],t,n):rc(e,zn(r,t,{...n,dropUnusedLevels:!0},o),!0)}function Mw(e,t){let{labels:n,groups:r}=or(e,t);if(r.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=r.flat(),s=r.flatMap((i,c)=>i.map(()=>n[c]));return lu("y ~ g",{y:o,g:s},{levels:{g:n}})}function fu(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}=Pt(e),i=t-1,c=n-t,a=r/i,l=o/c,f=a/l,m=u=>D(u,2*s);return{df:[i,c],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:c>0?dt(f,i,c,!1,!1):NaN}}function Ew(e,t,n={}){vt(t)&&K(t,iu,"TukeyHSD"),K(n,iu,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=or(e,r),i=s.length;if(i<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let c=n.confLevel??.95;fn(c);let l=s.reduce((b,N)=>b+N.length,0)-i;if(l===0)throw new RangeError("no residual degrees of freedom");if(l<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let{ssWithin:f,mean:m,k:u}=Pt(s),h=f/l,d=Sl(c,i,l),w=[],g={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:w,pAdjusted:w,warnings:[]};for(let b=0;b<i-1;b++)for(let N=b+1;N<i;N++){let v=m[N]-m[b],M=Math.sqrt(h/2*(1/s[N].length+1/s[b].length));g.names.push(`${o[N]}-${o[b]}`),g.i.push(N+1),g.j.push(b+1),g.diff.push(D(v,u)),g.lwr.push(D(v-d*M,u)),g.upr.push(D(v+d*M,u)),g.pAdj.push(Rs(Math.abs(v)/M,i,l,{lowerTail:!1}))}return g}function xw(e,t,n={}){vt(t)&&K(t,cu,"zTest"),K(n,cu,"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 A of[o,s])if(A!==void 0&&!(A>0&&A<1/0))throw new RangeError("sigmaX and sigmaY must be positive finite numbers");if(Number.isNaN(i))throw new RangeError("mu must be a number");let c=Ze(n.alternative),a=n.confLevel??.95;if(!(a>=0&&a<=1))throw new RangeError("confLevel must be between 0 and 1");let l=Array.from(e).filter(A=>!Number.isNaN(A)),f=r===void 0?void 0:Array.from(r).filter(A=>!Number.isNaN(A));if(l.length<3)throw new RangeError("x needs at least 3 non-missing values");if(f!==void 0&&f.length<3)throw new RangeError("y needs at least 3 non-missing values");let m=Se([l,f??[],[o,s??0]]),u=m.k,[h,d,[w,g]]=m.groups,b=Xn(h)[0],N=f===void 0?NaN:Xn(d)[0],v=f===void 0?w/Math.sqrt(l.length):Math.sqrt(w**2/l.length+g**2/f.length),M=f===void 0?b:b-N,p=(M-D(i,-u))/v,y,x;if(c==="less")y=se(p),x=[-1/0,M+le(a)*v];else if(c==="greater")y=se(p,{lowerTail:!1}),x=[M-le(a)*v,1/0];else{y=2*se(-Math.abs(p));let A=(1-a)/2,S=(A<1e-8?le(A,{lowerTail:!1}):le(1-(1-a)/2))*v;x=[M-S,M+S]}x=[D(x[0],u),D(x[1],u)];let E=f!==void 0,R=(A,S)=>{if(!(u>0&&A.some((L,C)=>L!==0&&Math.abs(S[C])<2**-1022)))return D(Xn(S)[0],u);let T=Math.max(Se([A]).k,0);return D(Xn(T===0?A:A.map(L=>D(L,-T)))[0],T)};return{method:E?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:p,statisticName:"z",pValue:y,confInt:x,confLevel:a,estimate:E?[R(l,h),R(f,d)]:R(l,h),estimateNames:E?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:E?"difference in means":"mean",warnings:[]}}var Rw=["family","link","weights","offset","start","epsilon","maxit","intercept"],Aw=["family","link","variance"],mu=["power"],Sw=["dispersion"],Tw=["type"],kw=["type","dispersion"],qw=["dispersion"],Iw=["newdata","newoffset","type","dispersion"],ro=2**-52;function _w(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function Xo(e,t,n){return e.map((r,o)=>{let s=0;for(let i=0;i<r.length;i++)s+=r[i]*t[i];return s+n[o]})}function sr(e,t,n,r){let o=0;for(let s=0;s<t.length;s++)r[s]!==0&&(o+=e.devContribution(t[s],n[s],r[s]));return o}var pu=2**-1022,ma=e=>e===0||Math.abs(e)>=pu&&Math.abs(e)<1/0;function uu(e,t,n,r){let o=e.variance(r),s=n**2,i=Math.sqrt(t*s/o);if(i>0&&i<1/0&&ma(s)&&s!==0&&ma(o)&&o!==0&&ma(t*s))return i;if(e.muPower!==void 0&&e.varPower!==void 0&&r>0){let{c,a}=e.muPower;return Math.sqrt(t)*Math.abs(c)*r**((2*a-e.varPower)/2)}return i}function ac(e,t,n,r){let o=(t-n)/r;if(Math.abs(r)>=pu&&Math.abs(r)<1/0)return o;if(e.muPower!==void 0&&n>0){let{c:s,a:i}=e.muPower,c=n**(i/2);return(t-n)/s/c/c}return o}function Lw(e,t,n,r){let o=e.variance(n),s=(t-n)*Math.sqrt(r)/Math.sqrt(o);if(Number.isFinite(s)&&ma(o)&&o!==0||e.varPower===void 0||!(n>0))return s;let i=n**(e.varPower/4);return(t-n)/i*Math.sqrt(r)/i}var Cw=.1;function du(e,t){let{x:n,p:r,y:o,w:s,offset:i,fam:c,epsilon:a,maxit:l}=e,f=o.length;if(o.some((m,u)=>s[u]>0&&!Number.isFinite(m)))throw new RangeError("NA/NaN/Inf in 'y'");if(n.some((m,u)=>s[u]>0&&m.some(h=>!Number.isFinite(h))))throw new RangeError("NA/NaN/Inf in 'x'");return Io(t,()=>{let m=(L,C)=>L.every(q=>c.etaOk(q))&&C.every(q=>c.muOk(q));if(r===0){let L=i.slice();if(!L.every(_=>c.etaOk(_)))throw new Error("invalid linear predictor values in empty model");let C=L.map(_=>c.linkinv(_));if(!C.every(_=>c.muOk(_)))throw new Error("invalid fitted means in empty model");let q=L.map(_=>c.muEta(_)),O=C.map((_,F)=>uu(c,s[F],q[F],_)**2);return{coefficients:[],eta:L,mu:C,deviance:sr(c,o,C,s),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:s.map(_=>_>0),workingWeights:O,workingResiduals:o.map((_,F)=>ac(c,_,C[F],q[F]))}}let u,h;if(e.start!==void 0){if(e.start.length!==r)throw new RangeError(`length of 'start' should equal ${r} and correspond to initial coefs for ${_w(e.columnNames)}`);h=e.start.slice(),u=Xo(n,h,i)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(L=>c.link(L));let d=u.map(L=>c.linkinv(L));if(!m(u,d))throw new Error("cannot find valid starting values: please specify some");let w=sr(c,o,d,s),g=w,b=!1,N=!1,v=0,M=new Array(r).fill(0),p,y=s.map(()=>!1),x=[],E=[],R=Math.min(1e-7,a/1e3);for(v=1;v<=l;v++){let L=u.map(P=>c.muEta(P));for(let P=0;P<f;P++){if(!(s[P]>0))continue;let k=c.variance(d[P]);if(Number.isNaN(k))throw new Error("NAs in V(mu)");if(k===0&&!(c.varPower!==void 0&&d[P]>0))throw new Error("0s in V(mu)");if(Number.isNaN(L[P]))throw new Error("NAs in d(mu)/d(eta)")}y=s.map((P,k)=>P>0&&L[k]!==0);let C=[];for(let P=0;P<f;P++)y[P]&&C.push(P);if(C.length===0){t.add(`no observations informative at iteration ${v}`),b=!1;break}x=new Array(f).fill(0);let q=[],O=[];for(let P of C){let k=uu(c,s[P],L[P],d[P]);x[P]=k,q.push((u[P]-i[P]+ac(c,o[P],d[P],L[P]))*k),O.push(n[P].map(V=>V*k))}p=At(O,{tol:R});let _=Hr(p,q);if(_.some((P,k)=>p.pivot.indexOf(k)<p.rank&&!Number.isFinite(P))){t.add(`non-finite coefficients at iteration ${v}`),b=!1;break}M=_.map(P=>Number.isNaN(P)?0:P),N=!1;let F=u;if(u=Xo(n,M,i),d=u.map(P=>c.linkinv(P)),g=sr(c,o,d,s),!Number.isFinite(g)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated due to divergence");let P=0;for(;!Number.isFinite(g);){if(P>=l)throw new Error("inner loop 1; cannot correct step size");P++,M=M.map((k,V)=>(k+h[V])/2),u=Xo(n,M,i),d=u.map(k=>c.linkinv(k)),g=sr(c,o,d,s)}N=!0}if(!m(u,d)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated: out of bounds");let P=0;for(;!m(u,d);){if(P>=l)throw new Error("inner loop 2; cannot correct step size");P++,M=M.map((k,V)=>(k+h[V])/2),u=Xo(n,M,i),d=u.map(k=>c.linkinv(k))}N=!0,g=sr(c,o,d,s)}let I=Math.max(a,1e-10);if(h!==void 0&&v>1&&(g-w)/(.1+Math.abs(g))>=I){t.add("step size truncated due to increasing deviance");let P=0;for(;(g-w)/(.1+Math.abs(g))>=I&&P<l;)P++,M=M.map((k,V)=>(k+h[V])/2),u=Xo(n,M,i),d=u.map(k=>c.linkinv(k)),g=sr(c,o,d,s);if((g-w)/(.1+Math.abs(g))>=I){t.add("inner loop 3; cannot correct step size"),b=!1;break}}if(e.untilCoefficients){let P=gt(M.map(Math.abs));if(h!==void 0&&M.every((k,V)=>Math.abs(k-h[V])<=8*ro*P)){b=!0;break}if(E.some(k=>k.every((V,U)=>V===M[U]))){b=!0;break}E.push(M.slice()),E.length>16&&E.shift()}else if(Math.abs(g-w)/(Math.abs(g)+.1)<a){b=!0;let P=0;for(let k=0;k<f;k++)y[k]&&(P=Math.max(P,Math.abs(u[k]-F[k])));P>Cw&&t.add("glm.fit: the linear predictor was still changing at convergence: the data may be separated (no finite maximum likelihood estimates)");break}w=g,h=M.slice()}v>l&&(v=l);let A=r,S=M.slice();if(p!==void 0){A=p.rank;for(let L=p.rank;L<r;L++)S[p.pivot[L]]=NaN}let T=u.map(L=>c.muEta(L));return{coefficients:S,eta:u,mu:d,deviance:g,iter:v,converged:b,boundary:N,rank:A,qr:p,used:y,workingWeights:x.map(L=>L*L),workingResiduals:o.map((L,C)=>ac(c,L,d[C],T[C]))}})}function gu(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function wu(e,t,n,r){r||(e.converged||n.add("glm.fit: algorithm did not converge"),e.boundary&&n.add("glm.fit: algorithm stopped at boundary value"),t.family==="binomial"&&e.mu.some(o=>o>1-10*ro||o<10*ro)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*ro)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function oo(e,t,n,r={}){let o=Xr(e.familySpec),s="start"in r?r.start:e.start,i=Hl(o,e.yGiven,e.priorGiven,s!==void 0||r.etastart!==void 0,n);if(!i.weights.some(f=>f>0))throw new RangeError("all weights are zero");let c=r.extraOffset===void 0?e.offset:e.offset.map((f,m)=>f+r.extraOffset[m]),a=e.x.map(f=>t.map(m=>f[m])),l=du({x:a,p:t.length,y:i.y,w:i.weights,offset:c,fam:o,epsilon:r.epsilon??e.epsilon,maxit:r.maxit??e.maxit,columnNames:t.map(f=>e.columnNames[f]),start:s,etastart:r.etastart,mustart:i.mustart,untilCoefficients:r.untilCoefficients},n);return wu(l,o,n,t.length===0),{fit:l,fam:o,y:i.y,w:i.weights,size:i.size,offset:c}}function ic(e,t,n={}){K(n,Rw,"glm"),K(sn(n,"family"),Aw,"glm","family"),K(sn(n,"family.link"),mu,"glm","family.link"),K(sn(n,"link"),mu,"glm","link");let r=n.intercept??!0,o=gu(e);if(o.some(E=>E.length!==o[0].length))throw new TypeError("all rows of x must have the same length");let s=t.length>0&&typeof t[0]!="number",i=s?t.map(E=>Array.from(E)):Array.from(t),c=i.length;if(o.length!==c)throw new RangeError("variable lengths differ (found for 'x1')");let a=n.weights===void 0?void 0:Array.from(n.weights);if(a!==void 0&&a.length!==c)throw new RangeError("variable lengths differ (found for '(weights)')");let l=n.offset===void 0?void 0:Array.from(n.offset);if(l!==void 0&&l.length!==c)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((E,R)=>{let A=i[R];return!(Array.isArray(A)?A.some(T=>Number.isNaN(T)):Number.isNaN(A))&&E.every(T=>!Number.isNaN(T))&&!(a!==void 0&&Number.isNaN(a[R]))&&!(l!==void 0&&Number.isNaN(l[R]))}),m=o.filter((E,R)=>f[R]).map(E=>r?[1,...E]:E),u=i.filter((E,R)=>f[R]),h=u.length;if(h===0)throw new RangeError("0 (non-NA) cases");let d=a===void 0?new Array(h).fill(1):a.filter((E,R)=>f[R]);if(d.some(E=>E<0))throw new RangeError("negative weights not allowed");if(d.some(E=>!Number.isFinite(E)))throw new RangeError("non-finite weights not allowed");let w=l===void 0?new Array(h).fill(0):l.filter((E,R)=>f[R]);if(w.some(E=>!Number.isFinite(E)))throw new RangeError("the offset must be finite");let g=o.length>0?o[0].length:0,b=[...r?["(Intercept)"]:[],...Array.from({length:g},(E,R)=>`x${R+1}`)],N=b.length,v=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(v.link=n.link);let M={x:m,columnNames:b,yGiven:u,priorGiven:d,offset:w,hasOffset:l!==void 0,intercept:r,familySpec:v,epsilon:n.epsilon??1e-8,maxit:Math.floor(n.maxit??25),start:n.start===void 0?void 0:Array.from(n.start),rows:f.flatMap((E,R)=>E?[R]:[])};if(!(M.epsilon>0&&M.epsilon<1/0))throw new RangeError("value of 'epsilon' must be > 0 and finite");if(!(M.maxit>0&&M.maxit<1/0))throw new RangeError("maximum number of iterations must be > 0");let p=!s&&Ow(v)?Se([u]).k:0;if(p===0)return X("glm",no(hu(M),c));let y=E=>D(E,-p),x=hu({...M,yGiven:u.map(y),offset:w.map(y),start:M.start?.map(y)});return X("glm",no(Fw(x,p,{...M,yScale:p}),c))}function hu(e){let{x:t,offset:n,intercept:r}=e,o=t.length,s=e.columnNames.length,i=new mt,c=Array.from({length:s},(g,b)=>b),{fit:a,fam:l,y:f,w:m,size:u}=oo(e,c,i),h;if(r&&e.hasOffset){let g=new mt,b=du({x:t.map(()=>[1]),p:1,y:f,w:m,offset:n,fam:l,epsilon:e.epsilon,maxit:e.maxit,columnNames:["(Intercept)"],mustart:a.mu},g);wu(b,l,g,!1),g.list.forEach(N=>i.add(N)),h=b.deviance,b.converged||i.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let g=Math.floor(Math.log2(gt(m))),b=0,N=0;for(let M=0;M<o;M++){if(!(m[M]>0))continue;let p=D(m[M],-g);b+=p,N+=p*f[M]}let v=N/b;if(!Number.isFinite(v)){let M=0;for(let x=0;x<o;x++)m[x]>0&&(M=Math.max(M,Math.abs(f[x])));let p=Math.floor(Math.log2(M)),y=0;for(let x=0;x<o;x++)m[x]>0&&(y+=D(m[x],-g)*D(f[x],-p));v=D(y/b,p)}h=Io(i,()=>sr(l,f,f.map(()=>v),m))}else h=Io(i,()=>sr(l,f,n.map(g=>l.linkinv(g)),m));let d=o-m.filter(g=>g===0).length,w=Yl(l,f,a.mu,m,u,a.deviance,i)+2*a.rank;return bu(a.used,{coefficients:a.coefficients,fittedValues:a.mu,linearPredictors:a.eta,deviance:a.deviance,nullDeviance:h,aic:w,rank:a.rank,dfResidual:d-a.rank,dfNull:d-(r?1:0),iter:a.iter,converged:a.converged,boundary:a.boundary,y:f,priorWeights:m,weights:a.workingWeights,residuals:a.workingResiduals,family:l.family,link:l.name,warnings:i.list,qr:a.qr,model:e})}function Ow(e){return(e.link??"identity")!=="identity"?!1:e.family==="gaussian"||e.family==="quasi"&&(e.variance??"constant")==="constant"}function Fw(e,t,n){let r=s=>D(s,t),o={...e,coefficients:e.coefficients.map(r),fittedValues:e.fittedValues.map(r),linearPredictors:e.linearPredictors.map(r),deviance:D(e.deviance,2*t),nullDeviance:D(e.nullDeviance,2*t),aic:e.aic+2*e.priorWeights.filter(s=>s>0).length*t*Math.LN2,y:e.y.map(r),residuals:e.residuals.map(r),model:n};return Object.defineProperty(o,"scaled",{value:e,enumerable:!1}),bu(ua.get(e),o)}var ua=new WeakMap;function bu(e,t){return e!==void 0&&ua.set(t,e),t}function Qt(e){let t=e.scaled;return t===void 0?{fit:e,k:0}:{fit:t,k:e.model.yScale??0}}function ar(e){return Xr(e.model.familySpec)}function Qo(e,t){let n=Qt(e),r=Zo(n.fit,t);return ar(e).fixedDispersion?r:D(r,2*n.k)}function Zo(e,t){if(ar(e).fixedDispersion)return 1;if(e.dfResidual<=0)return NaN;let n=0;for(let r=0;r<e.weights.length;r++)e.weights[r]>0&&(n+=e.weights[r]*e.residuals[r]**2);return e.weights.some(r=>r===0)&&t?.add("observations with zero weight not used for calculating dispersion"),n===1/0?Pw(e):n/e.dfResidual}function Pw(e){let t=[];for(let s=0;s<e.weights.length;s++)e.weights[s]>0&&t.push(s);if(!t.every(s=>Number.isFinite(e.weights[s])&&Number.isFinite(e.residuals[s])))return 1/0;let n=Se([t.map(s=>e.residuals[s])]),r=Se([t.map((s,i)=>Math.sqrt(e.weights[s])*Math.abs(n.groups[0][i]))]),o=0;for(let s of r.groups[0])o+=s*s;return D(o/e.dfResidual,2*(n.k+r.k))}function Dw(e){return e.qr===void 0||e.rank===0?{inv:[],e:[]}:qr(e.qr.qr.slice(0,e.rank).map(t=>t.slice(0,e.rank)))}function Vw(e){if(!(e>=0&&e<1/0))throw new RangeError("'dispersion' must be a non-negative finite number");return e}function so(e,t){let n=D(Vw(e),-2*t);if(n===0?e===0:n>=2**-1022&&n<1/0)return{m:n,s:0};let r=Math.floor(Math.floor(Math.log2(e))/2);return{m:D(e,-2*r),s:r-t}}function cc(e,t={}){K(t,Sw,"glmSummary");let{fit:n,k:r}=Qt(e),o=new mt,s=ar(n),i=t.dispersion===void 0&&!s.fixedDispersion,c=t.dispersion===void 0?void 0:so(t.dispersion,r),a=c===void 0?Zo(n,o):c.m,l=c?.s??0,{inv:f,e:m}=Dw(n),u=f.map((p,y)=>p.map((x,E)=>D(x,-m[y]-m[E]))),d=(n.qr===void 0?[]:n.qr.pivot.slice(0,n.rank)).map(p=>n.coefficients[p]),w=f.map((p,y)=>D(Math.sqrt(p[y]*a),-m[y])),g=l===0?d.map((p,y)=>p/w[y]):d.map((p,y)=>Le(p/Math.sqrt(f[y][y]*a),m[y]-l)),b=!i,N=g.map(p=>b?2*se(-Math.abs(p)):n.dfResidual>0?2*ft(-Math.abs(p),n.dfResidual,!0,!1):NaN);i&&n.dfResidual===0&&(w.fill(NaN),g.fill(NaN));let v=p=>D(p,r),M=p=>D(p,2*r);return X("glmSummary",{dispersion:l===0?M(a):t.dispersion,estimate:d.map(v),stdError:l===0?w.map(v):f.map((p,y)=>Le(Math.sqrt(p[y]*a),l+r-m[y])),statistic:g,pValue:N,statisticName:b?"z value":"t value",covUnscaled:u,covScaled:l===0?u.map(p=>p.map(y=>M(y*a))):f.map((p,y)=>p.map((x,E)=>Le(x*a,2*(l+r)-m[y]-m[E]))),dfResidual:n.dfResidual,warnings:o.list},e)}function $w(e,...t){K(Br(t),Tw,"glmResiduals");let n=Ur(["type"],t).type??"deviance",r=Qt(e);return lc(r.fit,n).map(o=>D(o,r.k))}function lc(e,t){let n=ar(e),{y:r,fittedValues:o,priorWeights:s}=e;switch(t){case"response":return r.map((i,c)=>i-o[c]);case"working":return e.residuals.slice();case"pearson":return r.map((i,c)=>Lw(n,i,o[c],s[c]));case"deviance":return r.map((i,c)=>s[c]===0?0:Math.sign(i-o[c])*Math.sqrt(Math.max(n.devContribution(i,o[c],s[c]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function Jo(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let i=n.qr.length,c=Array.from({length:i},(l,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=Is(n,c).map(l=>l.reduce((f,m)=>f+m*m,0))}let o=0,s=ua.get(e)??e.weights.map(i=>i>0);return e.priorWeights.forEach((i,c)=>{let a=s[c]?r[o++]??0:0;i>0&&t.push(a>1-10*ro?1:a)}),t}function vu(e){let t=[],n=0,r=ua.get(e)??e.weights.map(o=>o>0);return e.priorWeights.forEach((o,s)=>{let i=r[s]?n++:-1;o>0&&t.push(i)}),t}function zw(e){let t=e.slice().sort((r,o)=>r-o),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function Ww(e,t={}){K(t,kw,"glmRstandard");let n=t.type??"pearson";if(n!=="pearson"&&n!=="deviance")throw new RangeError(`unknown type "${String(n)}" for glmRstandard`);let{fit:r,k:o}=Qt(e),s=Jo(r),{m:i,s:c}=t.dispersion===void 0?{m:Zo(r),s:0}:so(t.dispersion,o),a=Math.sqrt(i),l=lc(r,n).filter((m,u)=>r.priorWeights[u]>0),f=r.rank>0?100*ro*zw(l.map(Math.abs)):0;return l.map((m,u)=>{let h=Le((Math.abs(m)<f?0:m)/(a*Math.sqrt(1-s[u])),-c);return Number.isFinite(h)?h:NaN})}function jw(e,t={}){K(t,qw,"glmCooksDistance");let{fit:n,k:r}=Qt(e),o=Jo(n),{m:s,s:i}=t.dispersion===void 0?{m:Zo(n),s:0}:so(t.dispersion,r);return lc(n,"pearson").filter((a,l)=>n.priorWeights[l]>0).map((a,l)=>{let f=o[l]/(s*n.rank),m=(a/(1-o[l]))**2*f;return(!Number.isFinite(m)||f<2**-1022)&&(m=Kw(a,o[l],s,n.rank)),i!==0&&(m=Le(m,-2*i)),Number.isFinite(m)?m:NaN})}function Kw(e,t,n,r){if(!Number.isFinite(e)||!(n>0&&Number.isFinite(n)))return NaN;let o=e===0?0:Math.floor(Math.log2(Math.abs(e))),s=Math.floor(Math.log2(n)),i=D(e,-o)/(1-t);return D(i*i*(t/(D(n,-s)*r)),2*o-s)}function Uw(e,t={}){K(t,Iw,"glmPredict");let{fit:n,k:r}=Qt(e),o=N=>D(N,r),s=ar(n),i,c;if(t.newdata===void 0)i=n.model.x,c=n.linearPredictors.slice();else{if(i=gu(t.newdata).map(M=>n.model.intercept?[1,...M]:M),i.some(M=>M.length!==n.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let N=t.newoffset===void 0?i.map(()=>0):Array.from(t.newoffset,M=>D(M,-r));if(N.length!==i.length)throw new RangeError("newoffset must have one value per row of newdata");let v=n.coefficients.flatMap((M,p)=>Number.isNaN(M)?[]:[p]);c=i.map((M,p)=>{let y=M.reduce((E,R,A)=>Number.isNaN(n.coefficients[A])?E:E+R*n.coefficients[A],0);return(To(y,0)?vr(v.map(E=>M[E]),v.map(E=>n.coefficients[E]),y):y)+N[p]})}let{m:a,s:l}=t.dispersion===void 0?{m:Zo(n),s:0}:so(t.dispersion,r),f=N=>D(N,r+l),m=n.rank,u=n.qr===void 0?[]:n.qr.pivot.slice(0,m),h=n.qr?.qr,d=i.map(N=>{let v=new Array(m).fill(0),M=0;for(let p=0;p<m;p++){let y=N[u[p]];for(let x=0;x<p;x++)y-=h[x][p]*v[x];v[p]=y/h[p][p],M+=v[p]*v[p]}if(!(M>=2**-1022&&M<1/0)){let p=v.reduce((y,x)=>Math.max(y,Math.abs(x)),0);if(p>0&&Number.isFinite(p)){let y=Math.sqrt(v.reduce((E,R)=>E+(R/p)**2,0)),x=p*y*Math.sqrt(a);return Number.isFinite(x)?x:p*(y*Math.sqrt(a))}}return Math.sqrt(M)*Math.sqrt(a)}),w=t.type??"link";if(w!=="link"&&w!=="response")throw new RangeError(`invalid 'type': ${String(w)}`);let g=t.newdata===void 0?[]:Ls(n.qr,m,i),b=g.length>0?{nonEstimable:g,warnings:[_s]}:{};return w==="link"?X("glmPredict",{fit:c.map(o),seFit:d.map(f),...b},e):X("glmPredict",Io(new mt,()=>({fit:c.map(N=>o(s.linkinv(N))),seFit:d.map((N,v)=>f(N*Math.abs(s.muEta(c[v])))),...b})),e)}var Bw=["parm","level","test"],yu={epsilon:1e-300,maxit:100,untilCoefficients:!0};function fc(e,t,n,r,o=1e-11){let s=[];for(let w=0;w<t.length;w++)n[w]>0&&s.push(w);let i=0,c=0;for(let w of s)i+=n[w],c+=n[w]*t[w];if(r&&!(Number.isFinite(i)&&Number.isFinite(c))){let w=0;for(let g of s)w=Math.max(w,n[g]);if(Number.isFinite(w)){let g=w>1?Math.floor(Math.log2(w)):0;i=0,c=0;for(let b of s){let N=D(n[b],-g);i+=N,c+=N*t[b]}}}let a=r?c/i:0,l=0;for(let w of s)l+=n[w]*(t[w]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(w=>Math.sqrt(n[w])),u=At(s.map((w,g)=>e[w].map(b=>b*m[g])),{tol:o}),h=Yr(u,s.map((w,g)=>t[w]*m[g])),d=0;for(let w of h)d+=w*w;if(!Number.isFinite(l)){let w=s.map((b,N)=>m[N]*(t[b]-a)),g=0;for(let b of w)g=Math.max(g,Math.abs(b));for(let b of h)g=Math.max(g,Math.abs(b));if(Number.isFinite(g)){let b=g>1?Math.floor(Math.log2(g)):0,N=0;for(let M of w)N+=D(M,-b)**2;let v=0;for(let M of h)v+=D(M,-b)**2;return D(D(N-v,b),b)}}return l-d}function Hw(e,t={}){K(t,Bw,"glmConfint");let{fit:n,k:r}=Qt(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},(C,q)=>q);for(let C of c)if(!Number.isInteger(C)||C<0||C>=i)throw new RangeError(`invalid coefficient index ${C}`);let a=n.model,l=n.coefficients.map((C,q)=>q).filter(C=>!Number.isNaN(n.coefficients[C])),f=new mt,m=oo(a,l,f,{...yu,start:a.start===void 0?void 0:l.map(C=>a.start[C])}),u=new Array(i).fill(NaN);if(l.forEach((C,q)=>u[C]=m.fit.coefficients[q]),!m.fit.converged)return{lower:c.map(()=>NaN),upper:c.map(()=>NaN)};let h=!1,d={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},w=rt(()=>cc(d)),g=w.dispersion,b=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((C,q)=>b[l[C]]=w.stdError[q]);let v=a.x.map(C=>l.map(q=>C[q])),M=m.fit.eta,p=(C,q)=>{let O=l.filter(I=>I!==C),_=oo(a,O,new mt,{...yu,start:void 0,etastart:M,extraOffset:a.x.map(I=>I[C]*q)}).fit;if(!_.converged)return h=!0,NaN;let F=s==="LRT"?_.deviance-m.fit.deviance:fc(v,_.workingResiduals,_.workingWeights,!1);return Math.sign(q-u[C])*Math.sqrt(Math.max(F/g,0))},y=(1-o)/2,x=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),E=2**-9,R=x<E,A=R?[-x,x]:[le(y),y<1e-8?le(y,{lowerTail:!1}):le((1+o)/2)],S=c.map(()=>NaN),T=c.map(()=>NaN),L=(C,q,O)=>{let _=O===0?-1:1;h=!1;let F=NaN,I=NaN;for(let z=0;z<=30;z++){let J=u[C]+_*2**z*b[C],Z=p(C,J);if(Math.abs(Z)>=Math.abs(q)){F=J,I=Z;break}}if(Number.isNaN(F)||h)return NaN;let P=z=>z===u[C]?-q:p(C,z)-q,[k,V]=O===0?[F,u[C]]:[u[C],F],[U,Q]=O===0?[I-q,-q]:[-q,I-q],re;try{re=Kt(P,k,V,{tol:1e-14*b[C],maxIter:500,fLower:U,fUpper:Q})}catch(z){if(!h)throw z;re=NaN}return h?NaN:re};return c.forEach((C,q)=>{if(Number.isNaN(u[C]))return;if(!R){S[q]=L(C,A[0],0),T[q]=L(C,A[1],1);return}let O=[-2*E,-E,0,E,2*E],_=[L(C,-2*E,0),L(C,-E,0),u[C],L(C,E,1),L(C,2*E,1)].map(I=>I-u[C]),F=I=>{let P=0;for(let k=0;k<5;k++){let V=1;for(let U=0;U<5;U++)U!==k&&(V*=(I-O[U])/(O[k]-O[U]));P+=V*_[k]}return u[C]+P};S[q]=F(A[0]),T[q]=F(A[1])}),{lower:S.map(C=>D(C,r)),upper:T.map(C=>D(C,r))}}var Yw=["type","omega","sandwich"],Gw=["cluster","type","cadjust","multi0","fix","sandwich"],ha=2**-52;function hc(e,t){if(t===0)return e;let n=dc.get(e)??{mant:e,exp:e.map(()=>0)};return Ir(n.mant,n.exp.map(r=>r+t))}var pa=e=>Math.floor(Math.log2(Math.abs(e)));function un(e){if(e===0||!Number.isFinite(e))return[e,0];let t=pa(e);return[D(e,-t),t]}var es=(e,t)=>[e[0]*t[0],e[1]+t[1]];function ir(e,t=!1){let n=-1/0;for(let[o,s]of e)o!==0&&Number.isFinite(o)&&(n=Math.max(n,s+pa(o)));let r=n===-1/0?0:n;return t&&(r=2*Math.floor(r/2)),{v:e.map(([o,s])=>Le(o,s-r)),s:r}}var cr=(e,t)=>Math.abs(t)<=2046?D(e,t):Le(e,t),dc=new WeakMap;function Ir(e,t){let n=e.map((r,o)=>r.map((s,i)=>cr(s,t[o]+t[i])));return dc.set(n,{mant:e,exp:t}),n}function gc(e,t,n){let r=dc.get(e),o=r?.mant[t]?.[t];if(!(r===void 0||o===void 0||!Object.is(cr(o,2*r.exp[t]),n)))return cr(Math.sqrt(o),r.exp[t])}function lr(e){return typeof e=="object"&&e!==null&&"family"in e&&"model"in e}function da(e){if(!(typeof e=="object"&&e!==null&&Array.isArray(e.coefficients)&&"qr"in e&&(lr(e)||"sigma"in e&&"x"in e&&"rows"in e)))throw new TypeError("'fit' must be the result of lm or glm")}function xu(e,t){return e.pivot.slice(0,t).map((n,r)=>[n,r]).sort((n,r)=>n[0]-r[0])}function Ru(e,t){let{inv:n,e:r}=qr(e.qr.slice(0,t).map(s=>s.slice(0,t))),o=xu(e,t);return{inv:o.map(([,s])=>o.map(([,i])=>n[s][i])),e:o.map(([,s])=>r[s]),cols:o.map(([s])=>s)}}function Nu(e,t,n){let{inv:r,e:o,cols:s}=Ru(e,t);return{A:r.map((i,c)=>i.map((a,l)=>D(a,-o[c]-o[l]-n))),cols:s}}function Mu(e,t){let n=e.qr.slice(0,t).map(i=>i.slice(0,t)),r=n.map((i,c)=>{let a=0;for(let l=0;l<=c;l++)a=Math.max(a,Math.abs(n[l][c]));return a>0&&Number.isFinite(a)?pa(a):0}),{inv:o}=qr(n.map(i=>i.map((c,a)=>D(c,-r[a])))),s=xu(e,t);return{B:s.map(([,i])=>s.map(([,c])=>o[i][c])),e:s.map(([,i])=>r[i])}}function Au(e,t){let n=e.qr.length;return Is(e,Array.from({length:n},(r,o)=>Array.from({length:t},(s,i)=>o===i?1:0)))}function Xw(e,t){let n=e.qr.length;return t===0?new Array(n).fill(0):Au(e,t).map(o=>{let s=o.reduce((i,c)=>i+c*c,0);return s>1-10*ha?1:s})}function Eu(e,t,n,r){return n.map((o,s)=>{if(o>=1)return 0;let i=r(s);if(o<=.5||e===void 0||i<0)return 1-o;let c=e.qr.length,a=new Array(c).fill(0);a[i]=1;let l=Ba(e,a),f=0;for(let m=t;m<c;m++)f+=l[m]*l[m];return f})}function ga(e){if(lr(e))return e.model.columnNames;let t=e.coefNames;if(t!==void 0)return t;let n=e.coefficients.length;return e.intercept?["(Intercept)",...Array.from({length:n-1},(r,o)=>`x${o+1}`)]:Array.from({length:n},(r,o)=>`x${o+1}`)}function wc(e){let t=ga(e);if(e.qr===void 0||e.rank===0)return{vcov:[],names:[]};let{inv:n,e:r,cols:o}=Ru(e.qr,e.rank),s=lr(e)?0:sc(e.qr),i,c;if(lr(e)){let l=Qt(e),f=ar(e).fixedDispersion?1:Qo(l.fit),m=f===0||!Number.isFinite(f)?0:2*Math.floor(pa(f)/2);i=D(f,-m),c=m+2*l.k}else{let[l,f]=un(e.sigma);i=l*l,c=2*f}let a=n.map(l=>l.map(f=>f*i));return{vcov:Ir(a,r.map(l=>(c-s)/2-l)),names:o.map(l=>t[l])}}function Su(e,t=!1){let n=ga(e);if(lr(e)){let w=t?Qt(e):{fit:e,k:0},g=w.fit,b=ar(g),N=g.priorWeights,v=N.map(_=>_>0),M=N.flatMap((_,F)=>_>0?[F]:[]),{A:p,cols:y}=g.qr===void 0||g.rank===0?{A:[],cols:[]}:Nu(g.qr,g.rank,0),x=M.map(_=>y.map(F=>g.model.x[_][F])),E=M.map(_=>g.weights[_]),R=M.map(_=>g.weights[_]*g.residuals[_]),A=b.family==="binomial"||b.family==="poisson",S=A?1:R.reduce((_,F)=>_+F*F,0)/E.reduce((_,F)=>_+F,0),T=g.model.rows,L=Jo(g),C=vu(g),q=()=>{let _=Qt(g),F=M.map(z=>_.fit.residuals[z]),I=E.map(Math.sqrt),{B:P,e:k}=g.qr===void 0||g.rank===0?{B:[],e:[]}:Mu(g.qr,g.rank),V=x.map((z,J)=>z.map((Z,ke)=>D(I[J]*Z,-k[ke]))),{v:U,s:Q}=ir(I.map((z,J)=>z===0?[0,0]:es(un(z),un(F[J])))),re=()=>{let z=[1,0];if(!A){let J=ir(E.map((ke,ne)=>ke===0?[0,0]:es(un(ke),un(F[ne])))),Z=ir(E.map(un));z=[J.v.reduce((ke,ne)=>ke+ne*ne,0)/Z.v.reduce((ke,ne)=>ke+ne,0),2*J.s-Z.s+2*_.k]}return{T:x.map(J=>J.map((Z,ke)=>cr(Z,-k[ke]))),factor:[z[0]*z[0],2*z[1]]}};return{T:V,B:P,e:k,u:U,s:Q+_.k,omegaPart:re}},O;if(_r(S)&&R.every(Number.isFinite))O=R.map(_=>_/S);else{let _=Qt(g),F=ir(E.map((I,P)=>I===0?[0,0]:es(un(I),un(_.fit.residuals[M[P]]))));if(A)O=F.v.map(I=>cr(I,F.s+_.k));else{let I=ir(E.map(un)),P=F.v.reduce((k,V)=>k+V*V,0)/I.v.reduce((k,V)=>k+V,0);O=F.v.map(k=>cr(k/P,I.s-F.s-_.k))}}return{glm:!0,names:y.map(_=>n[_]),X:x,W:E,res:O,phiS:S,A:p,h:L,omh:Eu(g.qr,g.rank,L,_=>C[_]),qr:g.qr,rank:g.rank,qrRow:_=>C[_],inputRows:M.map(_=>T[_]),nInput:tc(e,T),nonZero:v,dispersion:()=>Qo(g),scaled:q,k:w.k}}let r=e,o=r.weights,s=r.residuals.map((w,g)=>o===void 0||o[g]!==0),i=s.flatMap((w,g)=>w?[g]:[]),c=sc(r.qr),{A:a,cols:l}=r.rank===0?{A:[],cols:[]}:Nu(r.qr,r.rank,c),f=i.map(w=>l.map(g=>r.x[w][g])),m=i.map(w=>o?.[w]??1),u=i.map((w,g)=>m[g]*r.residuals[w]),h=Xw(r.qr,r.rank),d=()=>{let w=m.map(y=>c===0?Math.sqrt(y):D(Math.sqrt(y),-c/2)),{B:g,e:b}=r.rank===0?{B:[],e:[]}:Mu(r.qr,r.rank),N=f.map((y,x)=>y.map((E,R)=>D(w[x]*E,-b[R]))),{v,s:M}=ir(w.map((y,x)=>es(un(y),un(r.residuals[i[x]]))));return{T:N,B:g,e:b,u:v,s:M,omegaPart:()=>({T:f.map(y=>y.map((x,E)=>cr(x,-b[E]))),factor:[1,-2*c]})}};return{glm:!1,names:l.map(w=>n[w]),X:f,W:m,res:u,phiS:1,A:a,h,omh:Eu(r.qr,r.rank,h,w=>w),qr:r.qr,rank:r.rank,qrRow:w=>w,inputRows:i.map(w=>r.rows[w]),nInput:tc(r,r.rows),nonZero:s,dispersion:()=>r.sigma*r.sigma,scaled:d,k:0}}var bc=2**-969,_r=e=>Math.abs(e)>=bc&&Math.abs(e)<1/0;function Tu(e,t,n,r){if(!(e.phiS>=bc&&e.phiS<1/0)||!_r(e.X.length*e.phiS*e.phiS))return!1;for(let o=0;o<n.length;o++)if(!_r(e.A[o][o])||!r&&(!_r(t[o][o])||!_r(n[o][o]))||!n[o].every(Number.isFinite))return!1;return!0}function ts(e,t,n){let r=e.length,s=e.map(i=>Array.from({length:r},(c,a)=>i.reduce((l,f,m)=>l+f*t[m][a],0))).map(i=>Array.from({length:r},(c,a)=>n*i.reduce((l,f,m)=>l+f*e[m][a],0)));for(let i=0;i<r;i++)for(let c=0;c<i;c++)s[i][c]=s[c][i];return s}function ao(e,t,n){let r=e[0]?.length??0,o=Array.from({length:r},()=>new Array(r).fill(0));e.forEach((s,i)=>{let c=t[i];for(let a=0;a<r;a++)for(let l=a;l<r;l++)o[a][l]+=c*s[a]*s[l]});for(let s=0;s<r;s++)for(let i=s;i<r;i++)o[s][i]=o[i][s]=o[s][i]/n;return o}function mc(e){let t=e.slice(0,10).map(n=>String(n+1));return e.length>10?`${t.join(", ")}, ...`:t.join(", ")}var Qw=["HC3","const","HC","HC0","HC1","HC2","HC4","HC4m","HC5"];function Zw(e,t={}){K(t,Yw,"vcovHC"),da(e);let n=Te(t.type,Qw),r=n==="HC"?"HC0":n,o=t.sandwich??!0;if(typeof o!="boolean")throw new TypeError("'sandwich' must be true or false");let s=Su(e,t.omega===void 0),i=s.X.length,c=s.names.length,a=[],{res:l,h:f,omh:m}=s,u,h;if(t.omega!==void 0){let w;if(typeof t.omega=="function"?w=t.omega(l.slice(),f.slice(),i-c):(w=t.omega,w.length!==i&&w.length===s.nonZero.length&&(w=w.filter((g,b)=>s.nonZero[b]))),!Array.isArray(w)&&!ArrayBuffer.isView(w))throw new TypeError("'omega' must give an array of numbers");if(w.length!==i)throw new RangeError(`'omega' must have one value per observation (${i})`);if(u=Array.from(w,Number),u.some(g=>!Number.isFinite(g)))throw new RangeError("'omega' must be finite")}else if(r==="const"){let w=s.dispersion();u=s.W.map(g=>w*g/(s.phiS*s.phiS))}else{let w=f.flatMap((N,v)=>N>1-Math.sqrt(ha)?[s.inputRows[v]]:[]);if(w.length>0&&a.push(r==="HC0"||r==="HC1"?`${r} covariances become (close to) singular if hat values are (close to) 1 as for observation(s) ${mc(w)}`:`${r} covariances are numerically unstable for hat values close to 1 (and undefined if exactly 1) as for observation(s) ${mc(w)}`),r!=="HC0"&&r!=="HC1"){let N=f.flatMap((v,M)=>v>=1?[s.inputRows[M]]:[]);if(N.length>0)throw new RangeError(`${r} covariances are undefined for hat values of 1 as for observation(s) ${mc(N)}`)}if(r==="HC1"&&i<=c)throw new RangeError("HC1 covariances need more observations than coefficients");let g=Math.round(f.reduce((N,v)=>N+v,0)),b=f.reduce((N,v)=>Math.max(N,v),0);h=N=>{switch(r){case"HC0":return N;case"HC1":return N.map(v=>v*i/(i-c));case"HC2":return N.map((v,M)=>v/m[M]);case"HC3":return N.map((v,M)=>v/m[M]**2);case"HC4":return N.map((v,M)=>v/m[M]**Math.min(4,i*f[M]/g));case"HC4m":return N.map((v,M)=>v/m[M]**(Math.min(1,i*f[M]/g)+Math.min(1.5,i*f[M]/g)));default:return N.map((v,M)=>v/Math.sqrt(m[M]**Math.min(i*f[M]/g,Math.max(4,i*.7*b/g))))}},u=h(l.map(N=>N*N))}let d;if(o&&r==="const"&&t.omega===void 0)d=wc(e).vcov;else if(o){let w=ao(s.X,u,i);if(d=ts(s.A,w,i*s.phiS*s.phiS),!Tu(s,w,d,(h===void 0?u:l).every(g=>g===0))){let g=s.scaled();if(h!==void 0){let b=ts(g.B,ao(g.T,h(g.u.map(N=>N*N)),i),i);d=Ir(b,g.e.map(N=>g.s-N))}else{let{T:b,factor:N}=g.omegaPart(),v=ir(u.map(M=>es(un(M),N)),!0);d=Ir(ts(g.B,ao(b,v.v,i),i),g.e.map(M=>v.s/2-M))}}d=hc(d,s.k)}else{let w=ao(s.X,u,i);if(Iu(w,(h===void 0?u:l).every(g=>g===0)))d=w;else{let g=s.scaled();if(h!==void 0){let{mant:b,exp:N}=_u(g,ao(g.T,h(g.u.map(v=>v*v)),i));d=Ir(b,N)}else{let b=ir(u.map(un),!0);d=Ir(ao(g.omegaPart().T,b.v,i),g.e.map(N=>b.s/2+N))}}d=hc(d,-s.k)}return X("vcovHC",{vcov:d,names:s.names,warnings:a},e)}var Jw=["HC","HC0","HC1","HC2","HC3"];function e1(e){if(e==null||typeof e=="number"&&Number.isNaN(e))throw new RangeError("cannot handle NAs in 'cluster': either refit the model without the NA observations in 'cluster' or impute the NAs");if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="number")return ut(e);if(typeof e=="string")return e;throw new TypeError("cluster labels must be numbers, strings or booleans")}function t1(e){let t=new Map;return{g:e.map(r=>{let o=t.get(r);return o===void 0&&(o=t.size,t.set(r,o)),o}),G:t.size}}function n1(e){let t=[],n=(r,o,s)=>{if(s.length===r){t.push(s.slice());return}for(let i=o;i<e;i++)s.push(i),n(r,i+1,s),s.pop()};for(let r=1;r<=e;r++)n(r,0,[]);return t}var ku=ha**(1/1.3);function qu(e,t){let{values:n,vectors:r}=jt(e,{symmetric:!0});if(n.some(i=>!(i>=ku)))return;let o=r,s=n.map(i=>i**t);return e.map((i,c)=>e.map((a,l)=>o[c].reduce((f,m,u)=>f+m*s[u]*o[l][u],0)))}function r1(e){return{A:e.A,xs:t=>e.X[t].map(n=>n*Math.sqrt(e.W[t])),rs:t=>{let n=Math.sqrt(e.W[t]);return n===0?0:e.res[t]/n},single:(t,n,r)=>e.X[t].map(o=>n*e.res[t]*r*o),psi:t=>e.X[t].map(n=>n*e.res[t])}}function o1(e){return{A:e.B,xs:t=>e.T[t],rs:t=>e.u[t],single:(t,n,r)=>e.T[t].map(o=>n*e.u[t]*r*o),psi:t=>e.T[t].map(n=>n*e.u[t])}}function s1(e,t,n,r,o){let s=e.names.length,i=Array.from({length:r},()=>[]);n.forEach((f,m)=>i[f].push(m));let c=Math.sqrt((r-1)/r),a=r===n.length,l;return i.map((f,m)=>{if(f.length===1){let N=f[0];if(a?e.h[N]>=1:!(e.omh[N]>=ku))throw uc(m);return t.single(N,c,e.omh[N]**o)}let u=f.map(t.xs),h=f.map(t.rs);if(e.qr!==void 0&&e.rank>0){l??=Au(e.qr,e.rank);let N=a1(e,l,f,h,o);if(N===null)throw uc(m);if(N!==void 0)return Array.from({length:s},(v,M)=>c*u.reduce((p,y,x)=>p+y[M]*N[x],0))}let d=u.map(N=>Array.from({length:s},(v,M)=>N.reduce((p,y,x)=>p+y*t.A[x][M],0))),w=u.map((N,v)=>u.map((M,p)=>(v===p?1:0)-d[v].reduce((y,x,E)=>y+x*M[E],0)));for(let N=0;N<w.length;N++)for(let v=0;v<N;v++)w[N][v]=w[v][N]=(w[N][v]+w[v][N])/2;let g=qu(w,o);if(g===void 0)throw uc(m);let b=g.map(N=>N.reduce((v,M,p)=>v+M*h[p],0));return Array.from({length:s},(N,v)=>c*u.reduce((M,p,y)=>M+p[v]*b[y],0))})}function a1(e,t,n,r,o){let s=e.qr,i=e.rank,c=new Array(i).fill(0),a=n.map(p=>{let y=e.qrRow(p);return y<0?c:t[y]}),l=Array.from({length:i},(p,y)=>Array.from({length:i},(x,E)=>a.reduce((R,A)=>R+A[y]*A[E],0))),{values:f,vectors:m}=jt(l,{symmetric:!0});if(!f.some(p=>p>.5))return;let u=m,h=r.slice(),d=[];f.forEach((p,y)=>{let x=a.map(E=>E.reduce((R,A,S)=>R+A*u[S][y],0));if(p>.5)d.push(x.map(E=>E/Math.sqrt(p)));else if(p>0){let R=Math.expm1(o*Math.log1p(-p))/p*x.reduce((A,S,T)=>A+S*r[T],0);x.forEach((A,S)=>h[S]+=R*A)}});let w=s.qr.length,g=d.map(p=>{let y=new Array(w).fill(0);return n.forEach((x,E)=>{let R=e.qrRow(x);R>=0&&(y[R]=p[E])}),Ba(s,y).slice(i)}),b=g.map(p=>g.map(y=>p.reduce((x,E,R)=>x+E*y[R],0))),N=qu(b,o);if(N===void 0)return null;let v=d.map(p=>p.reduce((y,x,E)=>y+x*r[E],0)),M=N.map((p,y)=>p.reduce((x,E,R)=>x+E*v[R],0)-v[y]);return d.forEach((p,y)=>p.forEach((x,E)=>h[E]+=M[y]*x)),h}function uc(e){return new RangeError(`clustered HC2/HC3 covariances are undefined: the block I - H_gg of cluster ${e+1} (in order of appearance) is singular, as with cluster fixed effects`)}function i1(e,t){let n=Array.from({length:t},()=>new Array(t).fill(0));for(let r of e)for(let o=0;o<t;o++)for(let s=0;s<t;s++)n[o][s]+=r[o]*r[s];return n}function pc(e){if(e.length===0)return e;let{values:t,vectors:n}=jt(e,{symmetric:!0});if(t.every(s=>s>=0))return e;let r=n,o=t.map(s=>Math.max(s,0));return e.map((s,i)=>e.map((c,a)=>r[i].reduce((l,f,m)=>l+f*o[m]*r[a][m],0)))}function c1(e,t){let n=e.length;if(n===0)return{mant:e,exp:t};let r=e.map((a,l)=>a[l]!==0?Math.sqrt(Math.abs(a[l])):1),o=e.map((a,l)=>a.map((f,m)=>f/r[l]/r[m])),{values:s}=jt(o,{symmetric:!0}),i=s.reduce((a,l)=>Math.max(a,Math.abs(l)),0);if(s.every(a=>a>=-16*n*ha*i))return{mant:e,exp:t};let c=Math.max(...t);return{mant:pc(e.map((a,l)=>a.map((f,m)=>cr(f,t[l]+t[m]-2*c)))),exp:t.map(()=>c)}}function Iu(e,t){return t||e.every((n,r)=>_r(n[r])&&n.every(Number.isFinite))}function _u(e,t){let[n,r]=e.omegaPart().factor;return{mant:t.map(o=>o.map(s=>s/n)),exp:e.e.map(o=>e.s+o-r/2)}}function l1(e,t={}){K(t,Gw,"vcovCL"),da(e);let n=Su(e,!0),r=n.X.length,o=n.names.length,s=t.type===void 0?n.glm?"HC0":"HC1":Te(t.type,Jw),i=s==="HC"?"HC0":s,c=t.cadjust??!0,a=t.multi0??!1,l=t.fix??!1,f=t.sandwich??!0;for(let[v,M]of[["cadjust",c],["multi0",a],["fix",l],["sandwich",f]])if(typeof M!="boolean")throw new TypeError(`'${v}' must be true or false`);let m,u=t.cluster;if(u===void 0)m=[n.inputRows.map(String)];else{let v=y=>Array.isArray(y)||ArrayBuffer.isView(y);if(!v(u))throw new TypeError("'cluster' must be an array of labels or an array of such arrays");let p=u.length>0&&Array.from(u).every(v)?Array.from(u):[u];if(p.length===0)throw new RangeError("'cluster' has no dimension");m=p.map(y=>{if(y.length!==n.nInput)throw new RangeError("number of observations in 'cluster' and 'estfun()' do not match");return n.inputRows.map(x=>e1(y[x]))})}if(i==="HC1"&&r<=o)throw new RangeError("HC1 covariances need more observations than coefficients");let h=m.length,d=n1(h).map((v,M)=>{let p=a&&h>1&&M===2**h-2,y=p?n.inputRows.map(String):n.inputRows.map((A,S)=>v.length===1?m[v[0]][S]:JSON.stringify(v.map(T=>m[T][S]))),{g:x,G:E}=t1(y);if(E<2)throw new RangeError("at least two clusters are needed");let R=p?i==="HC1"?(r-o)/(r-1):1:c?E/(E-1):1;return{g:x,G:E,c:R,sign:v.length%2===1?1:-1}}),w=v=>{let M=Array.from({length:o},()=>new Array(o).fill(0));for(let{g:p,G:y,c:x,sign:E}of d){let R;i==="HC2"||i==="HC3"?R=s1(n,v,p,y,i==="HC2"?-.5:-1):y===r?R=n.X.map((S,T)=>v.psi(T)):(R=Array.from({length:y},()=>new Array(o).fill(0)),n.X.forEach((S,T)=>{let L=R[p[T]],C=v.psi(T);for(let q=0;q<o;q++)L[q]+=C[q]}));let A=i1(R,o);M=M.map((S,T)=>S.map((L,C)=>L+E*x*A[T][C]/r))}return i==="HC1"&&(M=M.map(p=>p.map(y=>y*(r-1)/(r-o)))),M},g=n.res.every(v=>v===0),b=n.phiS>=bc&&n.phiS<1/0&&n.A.every((v,M)=>_r(v[M]))&&n.res.every(Number.isFinite),N;if(b){let v=w(r1(n));if(!f)Iu(v,g)&&(N=l?pc(v):v);else{let M=ts(n.A,v,r*n.phiS*n.phiS);Tu(n,v,M,g)&&(N=l?pc(M):M)}}if(N===void 0){let v=n.scaled(),M=w(o1(v)),{mant:p,exp:y}=f?{mant:ts(v.B,M,r),exp:v.e.map(E=>v.s-E)}:_u(v,M),x=l?c1(p,y):{mant:p,exp:y};N=Ir(x.mant,x.exp)}return N=hc(N,f?n.k:-n.k),X("vcovCL",{vcov:N,names:n.names,warnings:[]},e)}var f1=["vcov","df"],m1=["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 Cu(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 Ou=e=>new Set(e).size===e.length;function Fu(e,t){da(e);let n=e.coefficients,r=ga(e),o=n.length,s=n.map(m=>Number.isNaN(m)),i,c,a=[],l;if(t===void 0){let m=wc(e),u=new Map(m.names.map((d,w)=>[d,w])),h=r.map((d,w)=>s[w]?void 0:u.get(d));i=h.map(d=>h.map(w=>d===void 0||w===void 0?NaN:m.vcov[d][w])),c=r,l=h.map(d=>d===void 0?NaN:gc(m.vcov,d,m.vcov[d][d])??Math.sqrt(m.vcov[d][d]))}else{let m=typeof t=="function"?rt(()=>t(e)):t,u=Lu(m)?m.vcov:m;if(Lu(m)){if(i=Cu(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=Cu(m);l=i.map((h,d)=>{let w=h[d],g=typeof u=="object"&&u!==null?gc(u,d,w):void 0;if(g!==void 0&&!Number.isNaN(g))return g;if(!Number.isFinite(w))throw new RangeError(`the variance of coefficient ${d+1} in 'vcov' is not a finite number`);if(w<0)throw new RangeError(`the variance of coefficient ${d+1} in 'vcov' is negative`);return Math.sqrt(w)})}if(c!==void 0&&Ou(c)&&Ou(r)){let m=new Map(c.map((h,d)=>[h,d])),u=r.flatMap((h,d)=>m.has(h)?[d]:[]);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 Pu(e,t){if(t===void 0)return lr(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 Du=e=>Number.isFinite(e)&&e>0;function u1(e){return lr(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function h1(e,t={}){K(t,f1,"coeftest");let{est:n,se:r,names:o,warnings:s}=Fu(e,t.vcov),i=Pu(e,t.df),c=Du(i),a=n.map((f,m)=>f/r[m]),l=a.map(f=>Number.isNaN(f)?NaN:c?2*ft(-Math.abs(f),i,!0,!1):2*se(-Math.abs(f)));return X("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:u1(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function p1(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 d1(e,t={}){K(t,m1,"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=Fu(e,t.vcov),o=Pu(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]=Du(o)?[bt(c,o,!0,!1),c<1e-8?bt(c,o,!1,!1):bt(1-c,o,!0,!1)]:[le(c),c<1e-8?le(c,{lowerTail:!1}):le(1-c)],f=(m,u,h)=>m+h*u;return X("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:p1(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var g1=["test","dispersion"],w1=(e,t)=>yt(e,t/2,2,!1,!1);function b1(e,t,n){let{fit:r,w:o}=oo(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 vc=(e,t)=>fc(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function v1(e,t={}){K(t,g1,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Qt),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:so(t.dispersion,o),c=i?.s??0,a=y1(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return X("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,d)=>f[d]?D(h,2*o):h));if(c!==0&&a.colNames.includes("Cp")){let u=a.colNames.indexOf("Cp"),h=a.colNames.indexOf("Resid. Dev"),d=a.colNames.indexOf("Resid. Df"),w=s[0].model.x.length;for(let g of m)g[u]=g[h]+2*(w-g[d])*t.dispersion}return X("glmAnova",{...a,table:m},...n)}function y1(e,t,n,r=0){let o=new mt,s=e[0],i=Xr(s.model.familySpec);if(e.some(E=>{let R=Xr(E.model.familySpec);return R.family!==i.family||R.name!==i.name||R.varianceName!==i.varianceName}))throw new RangeError("models were not all fitted with the same family and link");let c=i.fixedDispersion,a=n.test===void 0?n.dispersion!==void 0||c?"Chisq":"F":n.test;if(a!==!1&&!["Rao","LRT","Chisq","F","Cp"].includes(a))throw new RangeError(`invalid 'test': ${String(a)}`);let l=[],f,m,u=[],h=new Set,d;if(t){let E=e[0].model.x.length;if(e.some(A=>A.model.x.length!==E))throw new RangeError("models were not all fitted to the same size of dataset");let R=(A,S)=>A.length===S.length&&A.every((T,L)=>JSON.stringify(T)===JSON.stringify(S[L]));if(e.some(A=>!R(A.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(A=>!R(A.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((A,S)=>l.push({name:String(S+1),dfRes:A.dfResidual,dev:A.deviance})),f=l.map((A,S)=>S===0?NaN:l[S-1].dfRes-A.dfRes),m=l.map((A,S)=>S===0?NaN:l[S-1].dev-A.dev),e.forEach((A,S)=>{if(S===0||f[S]===0)return;let[T,L]=f[S]>0?[e[S-1],A]:[A,e[S-1]],C=_=>_.filter((F,I)=>T.priorWeights[I]>0&&L.priorWeights[I]>0),q=T.model.offset.map((_,F)=>_-L.model.offset[F]),O=q.some((_,F)=>_!==0&&T.priorWeights[F]>0)?T.model.x.map((_,F)=>[..._,q[F]]):T.model.x;Cs(C(O),C(L.model.x))||h.add(S)}),a==="Rao"){let A=e.map(S=>({dfRes:S.dfResidual,dev:S.deviance,fit:Vu(S),x:S.model.x,intercept:S.model.intercept}));u=l.map((S,T)=>T===0?NaN:f[T]<0?-vc(A[T],A[T-1]):vc(A[T-1],A[T]))}d=e.reduce((A,S)=>S.dfResidual<A.dfResidual?S:A)}else{let E=s,R=E.model,A=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,S=R.intercept?[0]:[],T=C=>[...S,...Array.from({length:C},(q,O)=>O+S.length)],L=[];for(let C=0;C<=A;C++)C===A?L.push({dfRes:E.dfResidual,dev:E.deviance,fit:Vu(E),x:R.x,intercept:R.intercept}):C>0||a==="Rao"?L.push(b1(R,T(C),o)):L.push(void 0);l.push({name:"NULL",dfRes:E.dfNull,dev:E.nullDeviance});for(let C=1;C<=A;C++)l.push({name:`x${C}`,dfRes:L[C].dfRes,dev:L[C].dev});f=l.map((C,q)=>q===0?NaN:l[q-1].dfRes-C.dfRes),m=l.map((C,q)=>q===0?NaN:Math.max(l[q-1].dev-C.dev,0)),a==="Rao"&&(u=l.map((C,q)=>q===0?NaN:vc(L[q-1],L[q]))),d=E}e.forEach((E,R)=>{E.converged||o.add(`model ${R+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let w=new mt,g=n.dispersion??Qo(d,w);w.list.forEach(E=>o.add(E));let b=c?1/0:d.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=l.map((E,R)=>t?[E.dfRes,E.dev,f[R],m[R]]:[f[R],m[R],E.dfRes,E.dev]),M=[...N],p=E=>l.map((R,A)=>{if(A===0||f[A]===0||h.has(A))return NaN;let S=Le(E[A]/g*Math.sign(f[A]),-2*r);return S<0||Number.isNaN(S)?NaN:w1(S,Math.abs(f[A]))}),y=[];if(a==="Chisq"||a==="LRT")M.push("Pr(>Chi)"),y=[p(m)];else if(a==="Rao")M.push("Rao","Pr(>Chi)"),y=[u.map((E,R)=>h.has(R)?NaN:E),p(u)];else if(a==="F"){if(c&&o.add(`using F test with a '${i.family}' family is inappropriate`),b!==0){let E=l.map((R,A)=>{let S=Le(m[A]/f[A]/g,-2*r);return A===0||f[A]===0||h.has(A)||S<0?NaN:S});M.push("F","Pr(>F)"),y=[E,E.map((R,A)=>Number.isNaN(R)?NaN:dt(R,Math.abs(f[A]),b,!1,!1))]}}else if(a==="Cp"){let E=d.model.x.length;M.push("Cp"),y=[l.map(R=>R.dev+2*(E-R.dfRes)*g)]}let x=v.map((E,R)=>[...E,...y.map(A=>A[R])]);return{colNames:M,rowNames:l.map(E=>E.name),table:x,warnings:o.list}}function Vu(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 N1=["type"];function $u(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let i=e.y.flatMap((d,w)=>e.weights===void 0||e.weights[w]!==0?[w]:[]),c=i.map(d=>e.weights===void 0?1:Math.sqrt(e.weights[d])),a=At(i.map((d,w)=>s.map(g=>e.x[d][g]*c[w]))),l=fa(e.weights===void 0?void 0:i.map(d=>D(e.weights[d],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=eo(f.map(d=>i.map((w,g)=>Tt(l[g],e.x[w][s[d]]))),i.map((d,w)=>Tt(l[w],D(e.y[d],-r)))),u=[0,0],h=0;return f.forEach((d,w)=>{d>=t.length&&(u=he(u,_e(m[w],m[w])),h++)}),{ss:u[0],df:h}}function yc(e,t={}){if(K(t,N1,"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=Js([e]),i=ea([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=Si(e,s),a=y=>{let x=c.map(([A,S])=>[D(A,-y),S]),E=x.reduce((A,[S])=>A+S,0),R=x.reduce((A,[S,T])=>A+S*T,0)/E;return Number.isFinite(E)&&Number.isFinite(R)?x.reduce((A,[S,T])=>A+S*(T-R)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=ta(c),f=a(l)),xr(c,i,l)||i===0)throw new Error("residual sum of squares is 0 (within rounding error)");let m=[...e.warnings],u=e.termLabels.length;r===2&&u===0&&e.intercept&&(m.push("the model contains only an intercept: Type III test substituted"),r=3);let h=y=>e.assign.flatMap((x,E)=>y.has(x)?[E]:[]),d=[...new Set(e.assign)],w=[],g=[],b=y=>{w.length=0;let x=[];if(r===3){if(e.coefficients.some(E=>Number.isNaN(E)))throw new Error("there are aliased coefficients in the model");for(let E of d){let R=h(new Set([E])),A=h(new Set(d.filter(S=>S!==E)));w.push(E===0?"(Intercept)":e.termLabels[E-1]),x.push({ss:$u(e,A,R,s,y).ss,df:R.length})}}else{let E=R=>e.factors.flatMap((A,S)=>A[R-1]>0?[S]:[]);for(let R of d){if(R===0)continue;let A=E(R),S=d.filter(C=>C!==0&&C!==R&&A.every(q=>e.factors[q][C-1]>0)),T=h(new Set(d.filter(C=>C!==R&&!S.includes(C)))),L=$u(e,T,h(new Set([R])),s,y);w.push(e.termLabels[R-1]),x.push(L)}}return x};g=b(l),l===0&&e.weights!==void 0&&g.some(y=>!Number.isFinite(y.ss))&&(l=ta(c),g=b(l));let N=y=>D(y,2*s+l),v=D(i,-l),M=v/o,p=g.map(({ss:y,df:x})=>{let E=y/x/M;return[N(y),x,E,dt(E,x,o,!1,!1)]});return p.push([N(v),o,NaN,NaN]),X("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...w,"Residuals"],table:p,warnings:m},e)}var wa=[0,0];var Nc=e=>e.map(t=>t.map(n=>n[0]));function zu(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function Lr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(wa);return r.forEach((s,i)=>{let c=t[i];for(let a=0;a<n;a++)o[a]=he(o[a],_e(s,c[a]))}),o})}function Cr(e){let t=wa;return e.forEach((n,r)=>t=he(t,n[r])),t}function Wu(e){let t=e.length,n=e.map(i=>i.slice()),r=Array.from({length:t},(i,c)=>c),o=1,s=!1;for(let i=0;i<t;i++){let c=i;for(let a=i+1;a<t;a++)Math.abs(n[a][i][0])>Math.abs(n[c][i][0])&&(c=a);if(n[c][i][0]===0){s=!0;continue}c!==i&&([n[i],n[c]]=[n[c],n[i]],[r[i],r[c]]=[r[c],r[i]],o=-o);for(let a=i+1;a<t;a++){let l=at(n[a][i],n[i][i]);n[a][i]=l;for(let f=i+1;f<t;f++)n[a][f]=Pe(n[a][f],_e(l,n[i][f]))}}return{a:n,perm:r,sign:o,singular:s}}function ju(e){let{a:t,sign:n,singular:r}=Wu(e);if(r)return wa;let o=[n,0];return t.forEach((s,i)=>o=_e(o,s[i])),o}function Ku(e){let t=e.length,{a:n,perm:r,singular:o}=Wu(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(wa));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]=Pe(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]=Pe(c[a],_e(n[a][l],c[l]));c[a]=at(c[a],n[a][a])}for(let a=0;a<t;a++)s[a][i]=c[a]}return s}var M1=["group","idata","M","X"],Uu=(e,t)=>yt(e,t/2,2,!1,!1);function Mc(e){let t=e.length,n=lt(Cr(e),t);return ju(e.map(r=>r.map(o=>at(o,n))))[0]}function Ec(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=Uu(i,c),l=Uu(i,c+4);return a+s*(l-a)}var E1=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function x1(e,t,n){let r=e.length,o=b=>b.length===0?0:At(Array.from({length:r},(N,v)=>b.map(M=>M[v]))).rank,s=n.length===0?void 0:At(Array.from({length:r},(b,N)=>n.map(v=>v[N]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(b=>n[b]),c=i.length,a=[...i,...t],l=At(Array.from({length:r},(b,N)=>a.map(v=>v[N])));if(l.rank>o(t))throw new Error("X does not define a subspace of M");let f=l.pivot.slice(0,l.rank).map(b=>a[b]),m=b=>b.map(N=>[N,0]),u=new Array(r).fill([0,0]),h=Kn(f.map(m),u,e.map((b,N)=>e.map(v=>v[N]))).qtv,d=Array.from({length:r},(b,N)=>h.map(v=>v[N])),w=Kn(f.map(m),u,d).qtv,g=Array.from({length:l.rank-c},(b,N)=>c+N);return g.map(b=>g.map(N=>w[b][N]))}function R1(e,t={}){K(t,M1,"mauchlyTest");let n=e.map(p=>Array.from(p)),r=n[0]?.length??0;if(r===0||n.some(p=>p.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=p=>p==null||typeof p=="number"&&Number.isNaN(p),i=n.map((p,y)=>!p.some(s)&&(o===void 0||!s(o[y])));if(n.some((p,y)=>i[y]&&p.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=Se(n.filter((p,y)=>i[y])).groups,a=c.length,l=o?.filter((p,y)=>i[y]).map(ut)??c.map(()=>""),f=[...new Set(l)].sort(wn),m=c.map(p=>p.map(()=>[0,0]));for(let p of f){let y=l.flatMap((x,E)=>x===p?[E]:[]);for(let x=0;x<r;x++){let E=[0,0];for(let A of y)E=he(E,[c[A][x],0]);let R=lt(E,y.length);for(let A of y)m[A][x]=Pe([c[A][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},(p,y)=>Array.from({length:r},(x,E)=>{let R=[0,0];for(let A of m)R=he(R,_e(A[y],A[E]));return R})),d=t.idata??{index:Array.from({length:r},(p,y)=>y+1)},w=p=>{let y=rt(()=>Gr(p,d));if(y.x.length!==r)throw new RangeError("idata must have one row per column of y");return E1(y.x,y.colNames.length)},g=t.M===void 0?Array.from({length:r},(p,y)=>Array.from({length:r},(x,E)=>E===y?1:0)):w(t.M),b=w(t.X??"~0"),N=x1(h,g,b),v=N.length;if(v===0)throw new RangeError("X spans M: the test has no dimensions");if(v===1)throw new RangeError("one tested dimension: sphericity holds trivially and the test has no degrees of freedom");if(u<v)throw new RangeError(`too few residual degrees of freedom for the test: ${u} for ${v} dimension(s)`);let M=Mc(N);return{statistic:M,statisticName:"W",pValue:Ec(M,v,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var A1=["n","d","type","sigLevel","power","alternative"],S1=["n1","n2","d","sigLevel","power","alternative"],T1=["h","n","sigLevel","power","alternative"],k1=["h","n1","n2","sigLevel","power","alternative"],q1=["h","n","sigLevel","power","alternative"],I1=["d","n","sigLevel","power","alternative"],_1=["n","r","sigLevel","power","alternative"],L1=["w","N","df","sigLevel","power"],C1=["k","n","f","sigLevel","power"],O1=["u","v","f2","sigLevel","power"],F1=["size"],P1={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 Hu(e="p",...t){K(Br(t),F1,"cohenES");let n=Ur(["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:P1[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function D1(e,t){if(!ba(e)||!ba(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 V1(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(ba))throw new RangeError("'P0' and 'P1' must be probabilities in [0, 1]");let n=0;for(let r=0;r<e.length;r++)(e[r]!==0||t[r]!==0)&&(n+=Vt(t[r]-e[r],e[r]));return Math.sqrt(n)}function $1(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(ba)))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>=z1)o+=Vt(e[i][s]-c,c);else{let a=Math.sqrt(t[i]),l=Math.sqrt(r[s]),f=e[i][s]/a/l-a*l;o+=f*f}}return Math.sqrt(o)}var z1=2**-1022,ba=e=>e>=0&&e<=1,W1="\u2018sig.level\u2019 must be numeric in [0, 1]",j1="\u2018power\u2019 must be numeric in [0, 1]";function fr(e,t){let n=r=>!Ne(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(W1);if(n(t))throw new RangeError(j1)}function mr(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?Hu(t,e).effectSize:e}var ns=["two.sided","less","greater"];function va(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Yu(e,t,n,r){if(r==="less")return Nr(Wt(n,e),e,t,!0);if(r==="greater")return Nr(Wt(n,e,{lowerTail:!1}),e,t,!1);let o=Wt(n/2,e,{lowerTail:!1});return Nr(o,e,t,!1)+Nr(-o,e,t,!0)}var Bu=e=>e<1e-8?le(e,{lowerTail:!1}):le(1-e);function Gu(e,t,n){return n==="greater"?se(Bu(t)-e,{lowerTail:!1}):n==="less"?se(le(t)-e):se(Bu(t/2)-e,{lowerTail:!1})+se(le(t/2)-e)}function K1(e){let t=1,n=0;for(let r of e){if(r===0)return 0;let o=Math.floor(Math.log2(Math.abs(r)));t*=D(r,-o),n+=o}return D(t,n)}function U1(e={}){K(e,A1,"pwrTTest");let{n:t,power:n}=e,r=mr(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),fr(o,n),!Ne(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,ns),c=s==="two.sample"?2:1;i==="two.sided"&&!Ne(r)&&(r=Math.abs(r));let a=(m,u,h)=>Yu((m-1)*c,Math.sqrt(m/c)*u,h,i);if(Ne(n))n=a(t,r,o);else{let m=n;Ne(t)?t=Ye(u=>a(u,r,o)-m,2+1e-10,1e9):Ne(r)?r=Ye(u=>a(t,u,o)-m,...va(i,[1e-7,10])):o=Ye(u=>a(t,r,u)-m,1e-10,1-1e-10)}let l=s==="paired"?"n is number of *pairs*":s==="two.sample"?"n is number in *each* group":void 0,f={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[s]+" t test power calculation";return{n:t,d:r,sigLevel:o,power:n,alternative:i,...l===void 0?{}:{note:l},method:f,warnings:[]}}function B1(e={}){K(e,S1,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=mr(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),fr(s,r),!Ne(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!Ne(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Te(e.alternative,ns);i==="two.sided"&&!Ne(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>Yu(a+l-2,f/Math.sqrt(1/a+1/l),m,i);if(Ne(r))r=c(t,n,o,s);else{let a=r;Ne(t)?t=Ye(l=>c(l,n,o,s)-a,2+1e-10,1e9):Ne(n)?n=Ye(l=>c(t,l,o,s)-a,2+1e-10,1e9):Ne(o)?o=Ye(l=>c(t,n,l,s)-a,...va(i,[1e-7,10])):s=Ye(l=>c(t,n,o,l)-a,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:s,power:r,alternative:i,method:"t test power calculation",warnings:[]}}function xc(e,t,n,r,o,s){let{n:i,power:c}=t,a=mr(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(nn([a,i,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),fr(l,c),!Ne(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=Te(t.alternative,ns);f==="two.sided"&&!Ne(a)&&(a=Math.abs(a));let m=(u,h,d)=>Gu(u*r(h),d,f);if(Ne(c))c=m(a,i,l);else{let u=c;Ne(a)?a=Ye(h=>m(h,i,l)-u,...va(f,[s,10])):Ne(i)?i=Ye(h=>m(a,h,l)-u,o,1e9):l=Ye(h=>m(a,i,h)-u,1e-10,1-1e-10)}return{es:a,n:i,sigLevel:l,power:c,alternative:f}}function H1(e={}){K(e,T1,"pwr2pTest");let t=xc(e.h,e,"h",n=>Math.sqrt(n/2),2+1e-10,1e-10);return{h:t.es,n:t.n,sigLevel:t.sigLevel,power:t.power,alternative:t.alternative,method:"Difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"same sample sizes",warnings:[]}}function Y1(e={}){K(e,q1,"pwrPTest");let t=xc(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 G1(e={}){K(e,I1,"pwrNormTest");let t=xc(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 X1(e={}){K(e,k1,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=mr(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),fr(s,r),!Ne(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!Ne(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Te(e.alternative,ns);i==="two.sided"&&!Ne(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)=>Gu(l*Math.sqrt(c(f,m)),u,i);if(Ne(r))r=a(o,t,n,s);else{let l=r;Ne(o)?o=Ye(f=>a(f,t,n,s)-l,...va(i,[1e-10,10])):Ne(t)?t=Ye(f=>a(o,f,n,s)-l,2+1e-10,1e9):Ne(n)?n=Ye(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=Ye(f=>a(o,t,n,f)-l,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:s,power:r,alternative:i,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes",warnings:[]}}function Q1(e={}){K(e,_1,"pwrRTest");let{n:t,power:n}=e,r=mr(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),fr(o,n),!Ne(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!Ne(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=Te(e.alternative,ns);s==="two.sided"&&!Ne(r)&&(r=Math.abs(r));let i=(a,l,f,m)=>{let u=Wt(m?f/2:f,a-2,{lowerTail:!1}),h=u**2+a-2,d=h===1/0&&Number.isFinite(u)?1/Math.sqrt(1+(a-2)/u/u):Math.sqrt(u**2/h),w=Math.atanh(l)+l/(2*(a-1)),g=Math.atanh(d),b=se((w-g)*Math.sqrt(a-3));return m?b+se((-w-g)*Math.sqrt(a-3)):b},c=(a,l,f)=>s==="two.sided"?i(a,l,f,!0):i(a,s==="less"?-l:l,f,!1);if(Ne(n))n=c(t,r,o),s==="less"&&(r=-r);else{let a=n;if(Ne(t))t=Ye(l=>c(l,r,o)-a,4+1e-10,1e9);else if(Ne(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=Ye(m=>c(t,m,o)-a,l,f)}else o=Ye(l=>c(t,r,l)-a,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:s,method:"approximate correlation power calculation (arctangh transformation)",warnings:[]}}function Z1(e){K(e,L1,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=mr(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([o,t,n,s],"exactly one of w, N, power, and sig.level must be NULL"),typeof r!="number")throw new TypeError('argument "df" is missing, with no default');if(!(r>0&&r<1/0))throw new RangeError("'df' must be a positive finite number");if(fr(s,n),!Ne(o)&&!(o>=0))throw new RangeError("w must be positive");if(!Ne(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:_t(Fn(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(Ne(n))n=i(o,t,s);else{let c=n;Ne(o)?o=Ye(a=>i(a,t,s)-c,1e-10,1e9):Ne(t)?t=Ye(a=>i(o,a,s)-c,1+1e-10,1e9):s=Ye(a=>i(o,t,a)-c,1e-10,1-1e-10)}return{w:o,N:t,df:r,sigLevel:s,power:n,method:"Chi squared power calculation",note:"N is the number of observations",warnings:[]}}function J1(e={}){K(e,C1,"pwrAnovaTest");let{k:t,n,power:r}=e,o=mr(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),fr(s,r),!Ne(o)&&!(o>=0))throw new RangeError("f must be positive");if(!Ne(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Ne(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=K1([c,a,l,l])),h===1/0?1:Vs(m,u)?$s(m,u,h,f):Tn(Yn(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(Ne(r))r=i(t,n,o,s);else{let c=r;Ne(t)?t=Ye(a=>i(a,n,o,s)-c,2+1e-10,100):Ne(n)?n=Ye(a=>i(t,a,o,s)-c,2+1e-10,1e9):Ne(o)?o=Ye(a=>i(t,n,a,s)-c,1e-7,1e7):s=Ye(a=>i(t,n,o,a)-c,1e-10,1-1e-10)}return{k:t,n,f:o,sigLevel:s,power:r,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}function eb(e={}){K(e,O1,"pwrF2Test");let{u:t,v:n,power:r}=e,o=mr(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),fr(s,r),!Ne(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!Ne(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!Ne(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:Vs(c,a)?$s(c,a,m,f):Tn(Yn(f,c,a,{lowerTail:!1}),c,a,{ncp:m,lowerTail:!1})};if(Ne(r))r=i(t,n,o,s);else{let c=r;Ne(t)?t=Ye(a=>i(a,n,o,s)-c,1+1e-10,100):Ne(n)?n=Ye(a=>i(t,a,o,s)-c,1+1e-10,1e9):Ne(o)?o=Ye(a=>i(t,n,a,s)-c,1e-7,1e7):s=Ye(a=>i(t,n,o,a)-c,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:s,power:r,method:"Multiple regression power calculation",warnings:[]}}var tb=["id","dv","between","within","type","correction","es","observed"],ya=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],nb=1e-12,Xu=1e-24;function Qu(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 rs=e=>typeof e=="boolean"?e?"TRUE":"FALSE":ut(e);function rb(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(rs):[...new Set(t.map(rs))].sort(wn)}var Zu=(e,t)=>t.split(":").includes(e);function Ju(e,t,n,r,o){let s=l=>r.some(f=>Zu(f,l));for(let l of r)if(!e.some(f=>Zu(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 ob(e,t){K(t,tb,"aovEz");let n=ya(t.between),r=ya(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[W,B]of[["between",n],["within",r]]){let j=B.filter((te,fe)=>B.indexOf(te)!==fe);if(j.length>0)throw new RangeError(`duplicated factor names in '${W}': ${[...new Set(j)].join(", ")}`)}let o=n.filter(W=>r.includes(W));if(o.length>0)throw new RangeError(`factors in both 'between' and 'within': ${o.join(", ")}`);let s=t.type??3;if(s!==2&&s!==3)throw new RangeError(`invalid type (${String(s)}): use 2 or 3`);let i=t.correction??"GG";if(!["GG","HF","none"].includes(i))throw new RangeError(`invalid 'correction': ${String(i)}`);let c=ya(t.es??"ges");for(let W of c)if(W!=="ges"&&W!=="pes")throw new RangeError(`invalid 'es': ${String(W)}`);let a=ya(t.observed),l=[],f=W=>W==null||typeof W=="number"&&Number.isNaN(W),m=Qu(e,t.dv),u=[t.id,...n,...r].map(W=>Qu(e,W));for(let W of u)if(W.length!==m.length)throw new RangeError("the columns must have the same length");let h=m.map((W,B)=>u.every(j=>!f(j[B]))),d=W=>W.filter((B,j)=>h[j]),w=kt(d(u[0])).map(rs),g=d(m).map(W=>{if(f(W))return NaN;if(typeof W!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(W))throw new RangeError(`the response '${t.dv}' must be finite`);return W}),b=n.map((W,B)=>kt(d(u[1+B]))),N=r.map((W,B)=>kt(d(u[1+n.length+B]))),v=w.length;for(let W of[g,...b,...N])if(W.length!==v)throw new RangeError("the columns must have the same length");let M=b.map(W=>rb(W)),p=b.map(W=>W.map(rs)),y=N.map(W=>W.map(rs)),x=y.map(W=>[...new Set(W)]);M.forEach((W,B)=>{if(W.length<2)throw new Error(`Factor "${n[B]}" consists of one level only. Remove factor from model?`)}),x.forEach((W,B)=>{if(W.length<2)throw new Error(`Factor "${r[B]}" consists of one level only. Remove factor from model?`)});let E=[...new Set(w)],R=new Map,A=new Set;if(w.forEach((W,B)=>{let j=JSON.stringify(p.map(fe=>fe[B])),te=R.get(W);te===void 0?R.set(W,j):te!==j&&A.add(W)}),A.size>0)throw new Error(`Following ids are in more than one between subjects condition:
|
|
8
|
-
${E.filter(W=>A.has(W)).join(", ")}`);let S=[[]];for(let W of x)S=S.flatMap(
|
|
9
|
-
${B}`)}let Q=E.filter(W=>!_.includes(W)),re=new Map;w.forEach((W,B)=>re.has(W)||re.set(W,B));let z=Q.map(W=>{let B=U.get(W);return B.s.map((j,te)=>lt(j,B.n[te]))}),J={};n.forEach((W,B)=>J[W]=Q.map(j=>p[B][re.get(j)]));let Z=Object.fromEntries(n.map(W=>[W,"contr.sum"])),ke=Object.fromEntries(n.map((W,B)=>[W,M[B]])),ne="Rank deficient model matrix; insufficient data to estimate full model.";if(V===1)return X("aovEz",$());return X("aovEz",Y());function $(){let W="y";for(;n.includes(W);)W+="_";let B={...J,[W]:z.map(me=>me[0][0])},j=rt(()=>Go(`${W} ~ ${n.length>0?n.join(" * "):"1"}`,B,{contrasts:Z,levels:ke}));if(j.rank<j.coefNames.length)throw new Error(ne);if(j.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${z.length} subject(s) for ${j.rank} parameter(s)`);let te=rt(()=>yc(j,{type:s}));l.push(...te.warnings.filter(me=>!l.includes(me)));let fe=j.dfResidual,Re=te.rowNames.flatMap((me,qe)=>{if(me==="(Intercept)"||me==="Residuals")return[];let[de,Oe,xe,Ve]=te.table[qe];return[{name:me,ss:de,numDf:Oe,errSS:j.rss,denDf:fe,f:xe,p:Ve}]}),Ce=te.colNames.indexOf("Sum Sq"),be={anova:Ju(Re,new Map([[0,j.rss]]),c,a,k),univariate:{colNames:te.colNames,rowNames:te.rowNames,table:te.table.map(me=>me.map((qe,de)=>de===Ce?k(qe):qe))},within:r,warnings:l};return Ut.set(be,{colNames:te.colNames,rowNames:te.rowNames,table:te.table}),be}function Y(){let W=Q.length,B=z.reduce((Me,ye)=>ye.reduce((Ee,ee)=>Ee+ee[0]*ee[0],Me),0),j=n.length>0?rt(()=>Gr(`~ ${n.join(" * ")}`,J,{contrasts:Z,levels:ke})):{colNames:["(Intercept)"],assign:[0],x:Q.map(()=>[1]),termLabels:[],factors:[]},te=j.colNames.length;if(W===0||At(j.x).rank<te)throw new Error(ne);let fe=W-te;if(fe<=0)throw new RangeError(`no residual degrees of freedom: ${W} subject(s) for ${te} between-subject parameter(s)`);let Re=[0,...j.termLabels.map((Me,ye)=>ye+1)],Ce=["(Intercept)",...j.termLabels],be=Me=>j.assign.flatMap((ye,Ee)=>Me.includes(ye)?[Ee]:[]),me=Me=>j.factors.flatMap((ye,Ee)=>ye[Me-1]>0?[Ee]:[]),qe=Me=>j.x.map(ye=>[ye[Me],0]),de=new Array(W).fill([0,0]),Oe=(Me,ye,Ee)=>{let{qtv:ee}=Kn([...Me,...ye].map(qe),de,Ee);return ee.map(ve=>ve.slice(Me.length,Me.length+ye.length))},xe=Me=>{let{qtv:ye}=Kn(j.colNames.map((Ee,ee)=>qe(ee)),de,Me);return ye.map(Ee=>Ee.slice(te))},Ve=Object.fromEntries(r.map((Me,ye)=>[Me,S.map(Ee=>Ee[ye])])),Fe=rt(()=>Gr(`~ ${r.join(" * ")}`,Ve,{contrasts:Object.fromEntries(r.map(Me=>[Me,"contr.sum"])),levels:Object.fromEntries(r.map((Me,ye)=>[Me,x[ye]]))})),Ue=[0,...Fe.termLabels.map((Me,ye)=>ye+1)],ze=["(Intercept)",...Fe.termLabels],et=Me=>Me.map(ye=>Me.map(Ee=>{let ee=[0,0];return ye.forEach((ve,ie)=>ee=he(ee,_e(ve,Ee[ie]))),ee})),Be=[],Xe=[],pe=[],oe=[],we=[],ge=[],ce=[],tt=new Map,G=!1;for(let Me of Ue){let ye=Fe.assign.flatMap((We,je)=>We===Me?[je]:[]),Ee=ye.length,ee=ye.map(We=>z.map(je=>je.reduce((xt,Rt,Sn)=>he(xt,Tt(Rt,Fe.x[Sn][We])),[0,0]))),ve=ye.map(We=>ye.map(je=>[Fe.x.reduce((xt,Rt)=>xt+Rt[We]*Rt[je],0),0])),ie=Ku(ve),Ae=et(xe(ee)),Ke=Lr(Ae,ie),$e=Cr(Ke)[0],ot=NaN,ht=NaN,Ct=NaN,Bt=NaN,Hn=!1;if(Ee>=2){let We=ze[Me],je=Ul(Nc(ie)).map(Ht=>Ht.map(on=>[on,0])),xt=Nc(Lr(Lr(je,Ae),zu(je))),Rt=jt(xt,{symmetric:!0,onlyValues:!0}).values,Sn=Rt.reduce((Ht,on)=>Math.max(Ht,on),0),$t=Math.max(Sn*nb,B*Xu),It=Cr(Ke),pt=Cr(Lr(Ke,Ke));Sn>B*Xu&&pt[0]>0&&Number.isFinite(pt[0])?(Hn=!0,ot=at(_e(It,It),Tt(pt,Ee))[0],fe-Ee*ot>0?ht=((fe+1)*Ee*ot-2)/(Ee*(fe-Ee*ot)):l.push(`The Huynh-Feldt epsilon of ${We} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${We} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),Hn&&Rt.every(Ht=>Ht>$t)?(Ct=Mc(Lr(ie,Ae)),Bt=Ec(Ct,Ee,fe,V)):Hn&&l.push(`The error SSP matrix of ${We} is singular: no Mauchly test (the epsilons are still defined)`)}for(let We=0;We<Re.length;We++){let je=Re[We],xt;if(s===3)xt=Oe(be(Re.filter(Ot=>Ot!==je)),be([je]),ee);else if(je===0)xt=Oe([],be([0]),ee);else{let Ot=me(je),Jt=Re.filter(H=>H!==0&&H!==je&&Ot.every(ae=>j.factors[ae][H-1]>0));xt=Oe(be(Re.filter(H=>H!==je&&!Jt.includes(H))),be([je]),ee)}let Rt=Cr(Lr(et(xt),ie))[0],$t=(je===0?1:be([je]).length)*Ee,It=fe*Ee,pt=Rt/$t/($e/It),Ht=dt(pt,$t,It,!1,!1),on=Me===0?Ce[We]:je===0?ze[Me]:`${Ce[We]}:${ze[Me]}`;Be.push([k(Rt),$t,k($e),It,pt,Ht]),Xe.push([Rt,$t,$e,It,pt,Ht]),pe.push(on),tt.set(Me,$e);let Zt={name:on,ss:Rt,numDf:$t,errSS:$e,denDf:It,f:pt,p:Ht};if(Hn){let Ot=Math.min(1,ht),Jt=dt(pt,ot*$t,ot*It,!1,!1),H=dt(pt,Ot*$t,Ot*It,!1,!1);oe.push([ot,Jt,ht,H]),we.push([Ct,Bt]),ge.push(on),i==="GG"?Zt={...Zt,numDf:ot*$t,denDf:ot*It,p:Jt}:i==="HF"&&(ht>1&&(G=!0),Zt=Number.isNaN(ht)?{...Zt,numDf:ot*$t,denDf:ot*It,p:Jt}:{...Zt,numDf:Ot*$t,denDf:Ot*It,p:H})}Me===0&&je===0||ce.push(Zt)}}G&&l.push("HF eps > 1 treated as 1");let ue={anova:Ju(ce,tt,c,a,k),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:pe,table:Be},within:r,warnings:l};return oe.length>0&&(ue.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:ge,table:oe},ue.sphericity={colNames:["Test statistic","p-value"],rowNames:[...ge],table:we}),Ut.set(ue,{colNames:ue.univariate.colNames,rowNames:pe,table:Xe}),ue}}function io(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(s=>Number.isFinite(s)))).sort((s,i)=>s-i);if(n.length<2)return e.slice();let r=it(n.map(Math.abs)),o=[n[0]];for(let s=0;s+1<n.length;s++){let i=n[s+1]-n[s];i<=t||i/r<=t||o.push(n[s+1])}return e.map(s=>{if(!Number.isFinite(s))return s;let i=0,c=o.length-1;for(;i<c;){let a=Math.floor((i+c+1)/2);o[a]<=s?i=a:c=a-1}return o[i]})}var sb=["rho"],ab=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),Ac=e=>e==null||typeof e=="number"&&Number.isNaN(e);function ib(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&&Ac(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=kt(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=io(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=${ut(a)}`)}}function cb(e,t,n,r={}){K(r,sb,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=ib(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,d=1;for(let v=0;v<s.length;){let M=s[v].time,p=v,y=new Array(c).fill(0),x=new Array(c).fill(0),E=0;for(;p<s.length&&s[p].time===M;){let R=s[p];x[R.group]++,R.event&&(y[R.group]++,E++),p++}if(E>0){let R=o===0?1:d**o;for(let A=0;A<c;A++)l[A]+=R*y[A],f[A]+=R*E*u[A]/h;if(h>1){let A=R*R*E*(h-E)/(h*(h-1));for(let S=0;S<c;S++)for(let T=0;T<c;T++)m[S][T]+=A*u[S]*((S===T?1:0)-u[T]/h)}d*=1-E/h}for(let R=0;R<c;R++)u[R]-=x[R];h-=p-v,v=p}let w=f.map((v,M)=>v>0?M:-1).filter(v=>v>=0),g=0,b=Math.max(w.length-1,0);if(w.length>=2){let v=w.slice(1),M=v.map(y=>l[y]-f[y]),p=v.map(y=>v.map(x=>m[y][x]));try{let y=nt(p,M);g=M.reduce((x,E,R)=>x+E*y[R],0)}catch{({chisq:g,df:b}=lb(p,M))}}let N=b===0?NaN:_t(g,b,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:g,df:b,pvalue:N,pValue:N,warnings:[]}}function lb(e,t){let n=jt(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 Rc(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 tn(e[r],e[s])}return e[r]}function ur(e,t,n={}){K(n,ab,"survfit");let{group:r,start:o,id:s,weights:i,ctype:c=1,stype:a=1,robust:l=!1,rmean:f="common",confType:m="log",confInt:u=.95}=n;if(typeof m!="string")throw new TypeError("confType must be a string");let h=["log","log-log","plain","none","logit","arcsin"],d=typeof m=="string"?h.filter(k=>k.startsWith(m)):[],w=h.includes(m)?m:d.length===1?d[0]:m;if(typeof c!="number")throw new TypeError("ctype must be a number");if(typeof a!="number")throw new TypeError("stype must be a number");if(c!==1&&c!==2)throw new RangeError("ctype must be 1 or 2");if(a!==1&&a!==2)throw new RangeError("stype must be 1 or 2");if(!h.some(k=>k===w))throw new RangeError("'arg' should be one of \u201Clog\u201D, \u201Clog-log\u201D, \u201Cplain\u201D, \u201Cnone\u201D, \u201Clogit\u201D, \u201Carcsin\u201D");if(typeof u!="number")throw new TypeError("confInt must be a number");if(!(u>0&&u<1))throw new RangeError("confidence intervals must be between 0 and 1");if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(r!==void 0&&r.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(o!==void 0&&o.length!==e.length)throw new RangeError("'start' must have the same length as 'time'");if(s!==void 0&&s.length!==e.length)throw new RangeError("'id' must have the same length as 'time'");if(s!==void 0&&o===void 0)throw new RangeError("'id' requires 'start'");if(i!==void 0&&!Array.isArray(i))throw new TypeError("'weights' must be an array");if(i!==void 0&&i.length!==e.length)throw new RangeError("'weights' must have the same length as 'time'");if(typeof l!="boolean")throw new TypeError("'robust' must be a boolean");if(typeof f!="number"&&typeof f!="string")throw new TypeError("rmean must be a number or string");if(typeof f=="number"&&!Number.isFinite(f))throw new RangeError("'rmean' must be finite");if(typeof f!="number"&&f!=="none"&&f!=="common"&&f!=="individual")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Ccommon\u201D, \u201Cindividual\u201D");let g=[];for(let k=0;k<e.length;k++){let V=i?.[k];if(i&&V!=null&&typeof V!="number")throw new TypeError("weights must contain numbers");if(i&&(V==null||Number.isNaN(V)))continue;let U=V??1;if(!Number.isFinite(U))throw new RangeError("weights must be finite");if(U<0)throw new RangeError("weights must be non-negative");if(!Number.isSafeInteger(U))throw new RangeError("weights must be non-negative safe integers");if(e[k]!=null&&typeof e[k]!="number")throw new TypeError("'time' must contain numbers");if(t[k]!=null&&typeof t[k]!="number")throw new TypeError("'status' must contain numbers");if(o!==void 0&&o[k]!=null&&typeof o[k]!="number")throw new TypeError("'start' must contain numbers");if(!(e[k]==null||t[k]==null||Number.isNaN(e[k])||Number.isNaN(t[k])||o!==void 0&&(o[k]==null||Number.isNaN(o[k]))||r!==void 0&&Ac(r[k])||s!==void 0&&Ac(s[k]))){if(o!==void 0&&!(o[k]<e[k]))throw new RangeError("stop time must be > start time");if(t[k]!==0&&t[k]!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[k]===-1/0||e[k]===1/0&&t[k]===1)throw new RangeError("event times must be finite");g.push(k)}}if(g.length===0)throw new RangeError("no observations without missing values");let b=r===void 0?[]:kt(r),N=s===void 0?[]:Array.from(s),v=r===void 0?["all"]:Array.from(new Set(g.map(k=>b[k]))).sort((k,V)=>typeof k=="number"&&typeof V=="number"?k-V:String(k)<String(V)?-1:String(k)>String(V)?1:0).map(k=>`group=${ut(k)}`),M=new Map;r!==void 0&&Array.from(new Set(g.map(k=>b[k]))).sort((k,V)=>typeof k=="number"&&typeof V=="number"?k-V:String(k)<String(V)?-1:String(k)>String(V)?1:0).forEach((k,V)=>M.set(k,V));let p=new Map;s!==void 0&&Array.from(new Set(g.map(k=>N[k]))).forEach((k,V)=>p.set(k,V));let y=g.flatMap(k=>o===void 0?[e[k]]:[o[k],e[k]]),x=io(y),E=new Map,R=new Map;g.forEach((k,V)=>{if(o===void 0)R.set(k,x[V]);else if(E.set(k,x[2*V]),R.set(k,x[2*V+1]),!(E.get(k)<R.get(k)))throw new Error("aeqSurv exception, an interval has effective length 0")});let A=g.map(k=>({time:R.get(k),start:o===void 0?-1/0:E.get(k),event:t[k]===1,group:r===void 0?0:M.get(b[k]),id:s===void 0?k:p.get(N[k]),weight:i?.[k]??1})).sort((k,V)=>k.time-V.time),S=s===void 0?void 0:new Map;if(S)for(let k of A){let V=S.get(k.id)??[];V.push(k),S.set(k.id,V)}let T=v,L=A.map(k=>k.time),C=Math.min(...L),q=Math.min(0,C);if(typeof f=="number"&&f<=q)throw new RangeError("'rmean' must be greater than the integration origin");let O=(1-u)/2,_=O<1e-8?le(O,{lowerTail:!1}):le(1-O),F=[],I={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:F,stdError:F,lower:[],upper:[],cumhaz:[],stdChaz:[],logse:!l||c===2,n:[],records:[],nMax:[],nStart:[],events:[],rmean:[],seRmean:[],median:[],medianRows:T,table:{colNames:[],rowNames:T,table:[]},warnings:[]};i!==void 0&&(I.counts={nRisk:[],nEvent:[],nCensor:[]});let P=[];for(let k=0;k<T.length;k++){let V=A.filter(pe=>pe.group===k);if(!V.some(pe=>pe.weight>0))throw new Error("a group has no positive weight");let U=I.time.length,Q=V.length,re=Math.max(...V.map(pe=>pe.time)),z=0,J=0,Z=1,ke=0,ne=0,$=0;if(o===void 0){let pe=0;for(let oe of V)if(pe+=oe.weight,!Number.isSafeInteger(pe))throw new RangeError("total case weight per group must be a safe integer");z=pe,J=V.length}let Y=[],W=[],B=[],j=s===void 0?void 0:new Map;if(j)for(let pe of V){let oe=j.get(pe.id)??[];oe.push(pe),j.set(pe.id,oe)}for(let pe=0;pe<V.length;){let oe=V[pe].time,we=pe,ge=0,ce=0,tt=0,G=J;for(;we<V.length&&V[we].time===oe;)we++;let ue=V.slice(pe,we);if(o!==void 0){let We=V.filter(je=>je.start<oe&&je.time>=oe);if(z=We.reduce((je,xt)=>je+xt.weight,0),J=We.length,G=J,!Number.isSafeInteger(z))throw new RangeError("total case weight at risk must be a safe integer")}let Me=ue.filter(We=>We.event),ye=ue.filter(We=>!We.event&&(S===void 0||!(S.get(We.id)??[]).some(je=>je.start===oe&&je.time>oe)));for(let We of Me)ge+=We.weight,ce++;for(let We of ye)tt+=We.weight;let Ee=Me.length,ee=ye.length;if(s!==void 0&&Me.length===0&&ye.length===0){pe=we;continue}let ve=0,ie=0;if(ge>0){if(c===1)ve=ge/z,ie=ge/(z*z);else for(let We=0;We<ce;We++){let je=ge/ce,xt=z-We*je;ve+=je/xt,ie+=je/(xt*xt)}ke+=ve,$+=ie,a===1?(Z*=1-ge/z,ne+=ge<z?ge/(z*(z-ge)):1/0):Z=Math.exp(-ke),B.push({t:oe,d:ge,n:z,e:ce,S:Z})}let Ae=Math.sqrt(a===1?ne:$),Ke=0,$e=0;if(l){let We=0,je=0,xt=j===void 0?V:Array.from(j.values(),Rt=>Rt[0]);for(let Rt of xt){let Sn=j?.get(Rt.id)??[Rt],$t=0,It=0;for(let pt of B){if(pt.t>oe)break;let Ht=Sn.some(Ot=>Ot.start<pt.t&&Ot.time>=pt.t),on=Sn.some(Ot=>Ot.time===pt.t&&Ot.event),Zt=pt.d/pt.n;if(c===1)It+=(Number(on)-Number(Ht)*Zt)/pt.n;else{let Ot=0,Jt=0;for(let H=0;H<pt.e;H++){let ae=pt.d/pt.e,Ie=pt.n-H*ae;Ot+=(Ie+H*ae)/(pt.e*Ie*Ie),Jt-=ae/(Ie*Ie)}It+=Ot*Number(on)+Jt*Number(Ht)}a===1&&Zt<1&&($t-=Z*(Number(on)-Number(Ht)*Zt)/((1-Zt)*pt.n))}We+=(Rt.weight*$t)**2,je+=(Rt.weight*It)**2}Ke=Math.sqrt(We),$e=Math.sqrt(je)}let ot=l?c===2?a===1?Ke:$e:a===1?Z===0?NaN:Ke/Z:$e:Ae,ht=l?c===2?Z*(a===1?Ke:$e):a===1?Ke:Z*$e:Z*Ae,Ct=NaN,Bt=NaN;if(w!=="none")if(w==="log")Ct=Z===0?ht===0?0:NaN:Z*Math.exp(-_*ot),Bt=Z===0?ht===0?0:NaN:Math.min(1,Z*Math.exp(_*ot));else if(w==="log-log")if(Z===0||Z===1)Ct=ht===0?Z:NaN,Bt=Ct;else{let We=Math.log(-Math.log(Z));Ct=Math.exp(-Math.exp(We-_*ot/Math.log(Z))),Bt=Math.exp(-Math.exp(We+_*ot/Math.log(Z)))}else if(w==="plain")Z===0&&ht===1/0?(Ct=NaN,Bt=NaN):(Ct=Math.max(0,Z-_*ht),Bt=Math.min(1,Z+_*ht));else if(w==="logit")if(Z===0||Z===1)Ct=ht===0?Z:NaN,Bt=Ct;else{let We=Math.log(Z/(1-Z)),je=_*ht/(Z*(1-Z));Ct=1-1/(1+Math.exp(We-je)),Bt=1-1/(1+Math.exp(We+je))}else if(Z>0&&Z<1){let We=Math.asin(Math.sqrt(Z)),je=_*ht/(2*Math.sqrt(Z*(1-Z)));Ct=Math.sin(Math.max(0,We-je))**2,Bt=Math.sin(Math.min(Math.PI/2,We+je))**2}else Ct=NaN,Bt=NaN;let Hn=z;I.strata.push(T[k]),I.time.push(oe),I.nRisk.push(Hn),I.nEvent.push(ge),I.nCensor.push(tt),I.surv.push(Z),I.cumhaz.push(ke),I.stdChaz.push(l?$e:Math.sqrt($)),I.stdErr.push(l?c===2?Z*(a===1?Ke:$e):a===1?Ke:Z*$e:Z*Ae),I.lower.push(Ct),I.upper.push(Bt),I.counts&&(I.counts.nRisk.push(G),I.counts.nEvent.push(Ee),I.counts.nCensor.push(ee)),o===void 0&&(z-=ge+tt,J-=we-pe),pe=we}let te=I.time.slice(U),fe=I.surv.slice(U),Re=I.lower.slice(U),Ce=I.upper.slice(U);I.median.push([Rc(te,fe),w==="none"?NaN:Rc(te,Re),w==="none"?NaN:Rc(te,Ce)]),I.n.push(Q),I.records.push(Q);let be=I.nRisk.slice(U);I.nMax.push(Math.max(...be)),I.nStart.push(be[0]??0),I.events.push(V.reduce((pe,oe)=>pe+(oe.event?oe.weight:0),0));let me=f==="none"?NaN:f==="common"?Math.max(...L):f==="individual"?re:f;if(f!=="none"&&!Number.isFinite(me))throw new RangeError("'rmean' must be finite");let qe=V.filter(pe=>Number.isFinite(pe.time)).map(pe=>Math.abs(pe.time)),de=Number.isFinite(me)?Math.max(Math.abs(me),Math.abs(q),...qe):Math.max(Math.abs(q),...qe),Oe=de===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(de)))),xe=de===0?1:2**Oe,Ve=Number.isFinite(me)&&!Number.isFinite(me-q),Fe=pe=>{let oe=1;for(let we of B)if(we.t<=pe)oe=we.S;else break;return oe},Ue=pe=>{let oe=1;for(let we of B)if(we.t/xe<=pe)oe=we.S;else break;return oe},ze=Dn();if(Ve){let pe=Array.from(new Set([q/xe,...B.map(oe=>oe.t/xe).filter(oe=>oe>q/xe&&oe<me/xe),me/xe])).sort((oe,we)=>oe-we);for(let oe=0;oe<pe.length-1;oe++){let we=pe[oe],ge=pe[oe+1];gn(ze,Ue(we)*(ge-we),pe.length+1)}}else{let pe=Array.from(new Set([q,...B.map(oe=>oe.t).filter(oe=>oe>q&&oe<me),me])).sort((oe,we)=>oe-we);for(let oe=0;oe<pe.length-1;oe++){let we=pe[oe],ge=pe[oe+1];gn(ze,Fe(we)*(ge-we),pe.length+1)}}let et=Vn(ze)*(Ve?xe:1),Be=Dn();if(Number.isFinite(me))for(let pe of B){if(pe.t>me||pe.n<=pe.d)continue;let oe=Dn();if(Ve){let ge=Array.from(new Set([pe.t/xe,...B.map(ce=>ce.t/xe).filter(ce=>ce>pe.t/xe&&ce<me/xe),me/xe])).sort((ce,tt)=>ce-tt);for(let ce=0;ce<ge.length-1;ce++)gn(oe,Ue(ge[ce])*(ge[ce+1]-ge[ce]),ge.length+1)}else{let ge=Array.from(new Set([pe.t,...B.map(ce=>ce.t).filter(ce=>ce>pe.t&&ce<me),me])).sort((ce,tt)=>ce-tt);for(let ce=0;ce<ge.length-1;ce++)gn(oe,Fe(ge[ce])*((ge[ce+1]-ge[ce])/xe),ge.length+1)}let we=Vn(oe);gn(Be,we*we*pe.d/(pe.n*(pe.n-pe.d)),B.length+1)}let Xe=Vn(Be);I.rmean.push(f==="none"?NaN:et),I.seRmean.push(f==="none"?NaN:Math.sqrt(Xe)*xe),P.push(f==="none"?w==="none"?[Q,I.nMax[k],I.nStart[k],I.events[k],I.median[k][0]]:[Q,I.nMax[k],I.nStart[k],I.events[k],I.median[k][0],I.median[k][1],I.median[k][2]]:w==="none"?[Q,I.nMax[k],I.nStart[k],I.events[k],et,Math.sqrt(Xe)*xe,I.median[k][0]]:[Q,I.nMax[k],I.nStart[k],I.events[k],et,Math.sqrt(Xe)*xe,I.median[k][0],I.median[k][1],I.median[k][2]])}return I.table={colNames:[...f==="none"?["records","n.max","n.start","events","median"]:["records","n.max","n.start","events","rmean","se(rmean)","median"],...w==="none"?[]:[`${u}LCL`,`${u}UCL`]],rowNames:T,table:P},X("survfit",I)}var fb=["tau","covariates","covariateNames","confLevel"];function os(e){return 2*se(-Math.abs(e))}function nh(e,t,n){return[e-n*t,e+n*t]}function eh(e,t,n){return{estimate:e,se:t,confInt:nh(e,t,n)}}function mb(e,t,n,r){return{estimate:e,confInt:nh(e,t,n),pValue:os(r)}}function th(e,t){let n=ur(e.map(l=>l.time),e.map(l=>l.status),{rmean:t}),r=n.rmean[0],o=n.seRmean[0],s=0,i=1,c=Dn();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];gn(c,(1-i)*(f-s),n.time.length+1),i=n.surv[l],s=f}gn(c,(1-i)*(t-s),n.time.length+1);let a=Vn(c);return{rmst:{estimate:r,se:o},rmtl:{estimate:a,se:o}}}function Sc(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,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:os(c/a)}}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=ur(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 Tc(e,t,n,r,o,s){let i=e.map(C=>[C.arm,...C.x]),c=i[0].map((C,q)=>{let O=i.reduce((F,I)=>Math.max(F,Math.abs(I[q])),0);if(O===0)return 1;let _=Math.floor(Math.log2(O));return _>500||_<-500?2**_:1}),a=i.map(C=>C.map((q,O)=>q/c[O])),l=e.map((C,q)=>[1,...a[q]]),f=[1,...c],m=ub(e),u=r?ic(a,n,{family:"poisson",weights:m}):Go(a,n,{weights:m});if(r&&!u.converged)throw new Error("the adjusted RMST Poisson model did not converge");let d=u.coefficients.map((C,q)=>C/f[q]),w=u.fittedValues;if(d.some(C=>!Number.isFinite(C))||w.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,q)=>C.map(O=>m[q]*O*(n[q]-w[q]))),b=g.map(C=>C.slice());for(let C of[0,1]){let q=e.flatMap((_,F)=>_.arm===C?[F]:[]),O=ur(q.map(_=>e[_].time),q.map(_=>e[_].status===0?1:0),{rmean:"none"});for(let _=0;_<O.time.length;_++){let F=O.time[_],I=O.nEvent[_];if(!(F<t)||I===0)continue;let P=O.nRisk[_];if(!(P>0))continue;let k=new Array(l[0].length).fill(0);for(let U=0;U<k.length;U++){let Q=Dn();for(let re of q)e[re].time>=F&&gn(Q,g[re][U],q.length);k[U]=Vn(Q)}let V=I/P;for(let U of q){let Q=e[U],re=Q.time===F&&Q.status===0,z=Q.time>=F;for(let J=0;J<k.length;J++)b[U][J]+=k[J]/P*(Number(re)-(z?V:0))}}}let N=l.map((C,q)=>C.map(O=>O*Math.sqrt(r?w[q]:1))),v=Ha(N),M=Ha(b),p=nt(v),x=Ya(Ya(p,M),p).map((C,q)=>C.map((O,_)=>O/f[q]/f[_])),E=x.map((C,q)=>Math.sqrt(Math.max(0,C[q]))),R=d.map((C,q)=>C/E[q]),A=R.map(os),S=d.map((C,q)=>C-s*E[q]),T=d.map((C,q)=>C+s*E[q]),L=r?[S.map(Math.exp),T.map(Math.exp)]:[S,T];return{coef:d,cov:x,table:{names:["intercept","arm",...o],estimate:d,se:E,z:R,pValue:A,confInt:L}}}function hb(e,t,n,r={}){if(K(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((z,J)=>Number.isFinite(J)?Math.max(z,Math.abs(J)):z,0),i=s>2**500?2**Math.floor(Math.log2(s)):1,a=e.every(z=>!Number.isFinite(z)||z===0||z/i!==0)&&(r.tau===void 0||r.tau===0||r.tau/i!==0)?i:1,l=z=>z/a,f=Array.from(t),m=new Set;for(let z=0;z<f.length;z++){let J=n[z]===!0?1:n[z]===!1?0:n[z];(J===0||J===1)&&f[z]===2&&m.add(J)}let u=r.covariates?.[0],h=r.covariates!==void 0&&typeof u=="number";if(r.covariates!==void 0&&r.covariates.length>0&&!h&&!Array.isArray(u))throw new TypeError("'covariates' must be a numeric vector or a matrix of numeric rows");let d=r.covariates===void 0?void 0:h?r.covariates.map(z=>[z]):r.covariates.map(z=>Array.from(z));if(d&&d.length!==e.length)throw new RangeError("'covariates' must have one row per observation");if(d&&d.some(z=>z.length!==d[0].length))throw new RangeError("'covariates' must have the same number of columns in every row");if(d&&d.some(z=>z.some(J=>typeof J!="number")))throw new TypeError("'covariates' must contain numeric values");if(d&&d.some(z=>z.some(J=>!Number.isFinite(J)&&!Number.isNaN(J))))throw new RangeError("'covariates' must be finite");let w=[],g=[],b=[],N=[];for(let z=0;z<e.length;z++){if(e[z]==null||Number.isNaN(e[z])||f[z]==null||Number.isNaN(f[z])||n[z]==null||Number.isNaN(n[z]))throw new Error("missing value where TRUE/FALSE needed");if(typeof e[z]!="number")throw new TypeError("'time' must contain numbers");if(!Number.isFinite(e[z]))throw new RangeError("'time' must be finite");if(typeof n[z]!="number"&&typeof n[z]!="boolean")throw new TypeError("'arm' must contain numbers or booleans");let J=n[z]===!0?1:n[z]===!1?0:n[z];if(J!==0&&J!==1){b.push(l(e[z]));continue}if(typeof f[z]!="number")throw new TypeError("'status' must contain numbers");let Z=f[z],ke=Z===1?1:0,ne=d?d[z]:[];if(ne.some(Y=>!Number.isFinite(Y)))continue;g.push({time:l(e[z]),status:ke,arm:J,x:ne,index:z});let $=Z;if(m.has(J))if(Z===2)$=1;else if(Z===1)$=0;else{N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}else if($!==0&&$!==1){N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}w.push({time:l(e[z]),status:$,arm:J,x:ne,index:z})}if(g.some(z=>z.time<0))throw new Error("negative follow-up times are not supported");if(!w.some(z=>z.arm===0)||!w.some(z=>z.arm===1))throw new Error("both arms 0 and 1 are required");let v=[0,1].map(z=>Math.max(...g.filter(J=>J.arm===z).map(J=>J.time))),M=[0,1].map(z=>{let J=v[z];return g.some(Z=>Z.arm===z&&Z.time===J&&Z.status===0)}),p=M.some(Boolean)?Math.min(...v.filter((z,J)=>M[J])):Math.max(...v),y=r.tau===void 0,x=r.tau===void 0?p:l(r.tau),E=r.tau===void 0?x*a:r.tau;if(b.some(z=>z<=x))throw new RangeError("'arm' must contain only 0 and 1 through tau");if(!(x>0))throw new RangeError("'tau' must be greater than zero");if(x>p)throw new RangeError(`'tau' must be at most ${p*a}: the minimum of the largest times allowed by both arms`);if(x<Math.min(...g.map(z=>z.time)))throw new RangeError("'tau' must not be below the first observed time");let R=(1-o)/2,A=R<1e-8?le(R,{lowerTail:!1}):le(1-R),S=w.filter(z=>z.arm===1),T=w.filter(z=>z.arm===0),L=th(S,x),C=th(T,x),q=z=>({estimate:z.estimate*a,se:z.se*a}),O={rmst:q(L.rmst),rmtl:q(L.rmtl)},_={rmst:q(C.rmst),rmtl:q(C.rmtl)},F=eh(O.rmst.estimate,O.rmst.se,A),I=eh(_.rmst.estimate,_.rmst.se,A),P={estimate:O.rmtl.estimate,se:O.rmtl.se,confInt:[E-F.confInt[1],E-F.confInt[0]]},k={estimate:_.rmtl.estimate,se:_.rmtl.se,confInt:[E-I.confInt[1],E-I.confInt[0]]},V=Sc(O.rmst,_.rmst,"difference",A),U=Sc(O.rmst,_.rmst,"ratio",A),Q=Sc(O.rmtl,_.rmtl,"rmtlRatio",A),re={tau:E,tauDefault:y,confLevel:o,arm1:{rmst:F,rmtl:P},arm0:{rmst:I,rmtl:k},difference:V,ratio:U,rmtlRatio:Q,warnings:N};if(d){let z=r.covariateNames===void 0?h?["covariates"]:Array.from({length:d[0].length},()=>""):Array.from(r.covariateNames);if(z.some(be=>typeof be!="string"))throw new TypeError("'covariateNames' must contain strings");if(z.length!==d[0].length)throw new RangeError("'covariateNames' must have one name per covariate column");let J=w.map(be=>({...be,time:Math.min(be.time,x),status:be.time>=x?1:be.status})),Z=J.map(be=>be.time),ke=J.map(be=>x-be.time),ne=Tc(J,x,Z,!1,z,A),$=!(O.rmst.estimate>0&&_.rmst.estimate>0),Y=!(O.rmtl.estimate>0&&_.rmtl.estimate>0),W=$?void 0:Tc(J,x,Z,!0,z,A),B=Y?void 0:Tc(J,x,ke,!0,z,A),j=(be,me)=>{let qe=be.coef[1],de=Math.sqrt(Math.max(0,be.cov[1][1]));return me?{estimate:Math.exp(qe),confInt:[Math.exp(qe-A*de),Math.exp(qe+A*de)],pValue:os(qe/de)}:{estimate:qe*a,confInt:[(qe-A*de)*a,(qe+A*de)*a],pValue:os(qe/de)}},te=be=>({...be,estimate:be.estimate.map(me=>me*a),se:be.se.map(me=>me*a),confInt:[be.confInt[0].map(me=>me*a),be.confInt[1].map(me=>me*a)]}),fe=be=>({...be,estimate:be.estimate.map((me,qe)=>qe===0?me+Math.log(a):me),confInt:[be.confInt[0].map((me,qe)=>qe===0?me+Math.log(a):me),be.confInt[1].map((me,qe)=>qe===0?me+Math.log(a):me)]}),Re={names:["intercept","arm",...z],estimate:Array(ne.coef.length).fill(NaN),se:Array(ne.coef.length).fill(NaN),z:Array(ne.coef.length).fill(NaN),pValue:Array(ne.coef.length).fill(NaN),confInt:[Array(ne.coef.length).fill(NaN),Array(ne.coef.length).fill(NaN)]},Ce={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};re.adjusted={difference:j(ne,!1),ratio:$?Ce:j(W,!0),rmtlRatio:Y?Ce:j(B,!0)},re.coefficients={difference:te(ne.table),ratio:W?fe(W.table):Re,rmtlRatio:B?fe(B.table):Re},delete re.arm1,delete re.arm0}return X("rmst2",re)}var pb=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),db=["eps","iterMax","tolerChol","tolerInf","timefix"],kc=e=>e==null||typeof e=="number"&&Number.isNaN(e),gb=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,wb=2**64,bb=2**-1022,rh=700;function Na(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),d=u(),w=h(),g=0,b=0,N=u(),v=h(),M=u(),p=h(),y=0,x=0,E=R=>{let A=Math.exp(x-R);b*=A,y*=A;for(let S=0;S<m;S++){N[S]*=A,M[S]*=A;let T=v[S],L=p[S];for(let C=0;C<=S;C++)T[C]*=A,L[C]*=A}x=R};for(let R=f-1;R>=0;){c[R]&&(x=0,b=0,N=u(),v=h());let A=r[R],S=!1;if(l!==void 0){let q=R;for(;q>0&&r[q-1]===A&&!c[q-1];)q--;for(let O=q;O<=R;O++)o[O]&&(S=!0)}if(l!==void 0&&S){let q=R;for(;q>0&&!c[q-1];)q--;let O=R;for(;!c[O];)O++;let _=I=>{let P=0;for(let k=0;k<m;k++)P+=t[k]*i[I][k];return P},F=-1/0;for(let I=q;I<=O;I++)l[I]<A&&r[I]>=A&&(F=Math.max(F,_(I)));x=Number.isFinite(F)?F:0,b=0,N=u(),v=h();for(let I=q;I<=O;I++){if(!(l[I]<A&&r[I]>=A)||r[I]===A&&o[I])continue;let P=Math.exp(_(I)-x)*s[I];b+=P;for(let k=0;k<m;k++){N[k]+=P*i[I][k];for(let V=0;V<=k;V++)v[k][V]+=P*i[I][k]*i[I][V]}}}let T=0,L=0;y=0;let C=R;do{let q=i[C],O=0;for(let I=0;I<m;I++)O+=t[I]*q[I];let _=s[C];Number.isFinite(O)&&(O-x>rh?E(O):O-x<-rh&&b===0&&y===0&&(x=O));let F=Math.exp(O-x)*_;if(o[C]){T++,L+=_,y+=F,g+=_*O;for(let I=0;I<m;I++){d[I]+=_*q[I],M[I]+=F*q[I];let P=p[I];for(let k=0;k<=I;k++)P[k]+=F*q[I]*q[k]}}else if(l===void 0){b+=F;for(let I=0;I<m;I++){N[I]+=F*q[I];let P=v[I];for(let k=0;k<=I;k++)P[k]+=F*q[I]*q[k]}}C--}while(C>=0&&r[C]===A&&!c[C]);if(T>0){let q=n?T:1,O=n?T:1,_=L/q;for(let F=0;F<q;F++){b+=n?y/O:y,g-=_*(Math.log(b)+x+a);for(let I=0;I<m;I++){N[I]+=n?M[I]/O:M[I];let P=N[I]/b;d[I]-=_*P;let k=v[I],V=p[I],U=w[I];for(let Q=0;Q<=I;Q++){k[Q]+=n?V[Q]/O:V[Q];let re=k[Q]-P*N[Q],z=_*re;U[Q]+=z<1/0&&z>-1/0&&(Math.abs(z)>=bb||re===0)||!Number.isFinite(_)?z/b:_*(re/b)}}}M.fill(0);for(let F of p)F.fill(0)}R=C}for(let R=0;R<m;R++)for(let A=0;A<R;A++)w[A][R]=w[R][A];return{loglik:g,score:d,info:w}}function oh(e,t,n){let{time:r,event:o,x:s,stratumEnd:i}=e,c=Se([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 d=0;d<l;d++)h+=t[d]*s[u][d];return h};for(let u=0;u<a;){let h=u;for(;!i[h];)h++;let d=[];for(let R=u;R<=h;R++)d.push(m(R));let w=[];for(let R=u;R<=h;R++)(R===u||r[R]!==r[R-1])&&w.push(R);let g=R=>R+1<w.length?w[R+1]:h+1,b=new Array(w.length),N=new Array(w.length),v=new Array(w.length),M=-1/0,p=0,y=new Array(l).fill(0);for(let R=w.length-1;R>=0;R--){let A=Number.isFinite(M)?M:0;for(let T=w[R];T<g(R);T++)M=Math.max(M,d[T-u]);let S=Number.isFinite(M)?M:0;if(R<w.length-1&&S!==A){let T=Math.exp(A-S);p*=T;for(let L=0;L<l;L++)y[L]*=T}for(let T=w[R];T<g(R);T++){let L=c[T]*Math.exp(d[T-u]-S);p+=L;for(let C=0;C<l;C++)y[C]+=L*s[T][C]}b[R]=p,N[R]=y.slice(),v[R]=S}let x=0,E=new Array(l).fill(0);for(let R=0;R<w.length;R++){let A=v[R];if(R>0&&A!==v[R-1]){let I=Math.exp(A-v[R-1]);x*=I;for(let P=0;P<l;P++)E[P]*=I}let S=w[R],T=g(R),L=[];for(let I=S;I<T;I++)L.push(Math.exp(d[I-u]-A));let C=0,q=0,O=0,_=new Array(l).fill(0);for(let I=S;I<T;I++){if(!o[I])continue;let P=c[I]*L[I-S];C++,q+=c[I],O+=P;for(let k=0;k<l;k++)_[k]+=P*s[I][k]}for(let I=S;I<T;I++){let P=L[I-S],k=s[I],V=f[I];for(let U=0;U<l;U++)V[U]=-P*(k[U]*x-E[U])}if(C===0)continue;let F=n?C:1;for(let I=0;I<F;I++){let P=I/F,k=b[R]-P*O,V=N[R].map((Q,re)=>(Q-P*_[re])/k),U=q/F/k;for(let Q=S;Q<T;Q++){let re=L[Q-S],z=s[Q],J=f[Q];if(o[Q])for(let Z=0;Z<l;Z++){let ke=z[Z]-V[Z];J[Z]+=ke/F-(1-P)*re*ke*U}else for(let Z=0;Z<l;Z++)J[Z]-=re*(z[Z]-V[Z])*U}x+=U;for(let Q=0;Q<l;Q++)E[Q]+=U*V[Q]}}u=h+1}return f}function qc(e,t,n){let r=e.start,{time:o,event:s,x:i,stratumEnd:c}=e,a=Se([e.weight]).groups[0],l=o.length,f=t.length,m=Array.from({length:l},()=>new Array(f).fill(0)),u=o.map((h,d)=>{let w=0;for(let g=0;g<f;g++)w+=t[g]*i[d][g];return w});for(let h=0;h<l;){let d=h;for(;!c[d];)d++;for(let w=h;w<=d;){let g=o[w],b=w;for(;b<d&&o[b+1]===g;)b++;let N=[],v=[],M=-1/0;for(let p=w;p<=b;p++)s[p]&&v.push(p);if(v.length>0)for(let p=h;p<=d;p++)r[p]<g&&o[p]>=g&&(N.push(p),M=Math.max(M,u[p]));if(v.length>0&&N.length>0){Number.isFinite(M)||(M=0);let p=0,y=new Array(f).fill(0),x=0,E=new Array(f).fill(0),R=0;for(let S of N){let T=Math.exp(u[S]-M),L=a[S]*T;p+=L;for(let C=0;C<f;C++)y[C]+=L*i[S][C];if(s[S]&&o[S]===g){R+=a[S],x+=L;for(let C=0;C<f;C++)E[C]+=L*i[S][C]}}let A=n?v.length:1;for(let S=0;S<A;S++){let T=S/A,L=p-T*x,C=R/A/L,q=y.map((O,_)=>(O-T*E[_])/L);for(let O of N){let _=Math.exp(u[O]-M),F=s[O]&&o[O]===g;for(let I=0;I<f;I++){let P=i[O][I]-q[I];F?m[O][I]+=P/A-(1-T)*_*P*C:m[O][I]-=_*P*C}}}}w=b+1}h=d+1}return m}function ss(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 Ma(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 Ea(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 as=(e,t)=>({statistic:e,df:t,pValue:_t(e,t,{lowerTail:!1})});function vb(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=as(0,0),i=new Array(t).fill(0),c=o(),a=o();return{coefficients:o(),var:Array.from({length:t},()=>new Array(t).fill(0)),loglik:[0,0],score:0,wald:0,iter:0,means:Array.from({length:t},(l,f)=>it(e.map(m=>m[f]))),n:r,nevent:0,linearPredictors:new Array(r).fill(0),se:i,z:c,p:a,stdError:i,zValue:c,pValue:a,lower:o(),upper:o(),logtest:s,sctest:{...s},waldtest:{...s},rsq:{rsq:0,maxrsq:0},usedRobust:!1,warnings:n}}function yb(e,t,n,r={}){return K(r,pb,"coxph"),K(sn(r,"control"),db,"coxph","control"),X("coxph",Nb(e,t,n,r).result)}function Nb(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:d,id:w}=r,{eps:g=1e-9,iterMax:b=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 M=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(b)&&b>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(m>0&&m<1))throw new RangeError("'confLevel' must be between 0 and 1");let p=e.length;if(t.length!==p||n.length!==p)throw new RangeError("'time', 'status' and 'x' must have the same length");if(i!==void 0&&i.length!==p)throw new RangeError("'weights' must have the same length as 'time'");if(c!==void 0&&c.length!==p)throw new RangeError("'strata' must have the same length as 'time'");if(h!==void 0&&h.length!==p)throw new RangeError("'cluster' must have the same length as 'time'");if(d!==void 0&&d.length!==p)throw new RangeError("'start' must have the same length as 'time'");if(w!==void 0&&w.length!==p)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=p===0?0:n[0].length;if(y<1)throw new RangeError("'x' must have at least one column");if(n.some(H=>H.length!==y))throw new RangeError("the rows of 'x' must have the same length");if(a!==void 0&&a.length!==y)throw new RangeError("'init' must have one value per column of 'x'");if(a?.some(H=>!Number.isFinite(H)))throw new RangeError("'init' must contain finite values");let x=new mt,E=[];for(let H=0;H<p;H++){if(Number.isNaN(e[H])||Number.isNaN(t[H])||n[H].some(Ie=>Number.isNaN(Ie))||d!==void 0&&Number.isNaN(d[H])||i!==void 0&&Number.isNaN(i[H])||c!==void 0&&kc(c[H])||h!==void 0&&kc(h[H])||w!==void 0&&kc(w[H]))continue;if(d!==void 0&&!(d[H]<e[H]))throw new RangeError("stop time must be > start time");let ae=t[H];if(ae!==0&&ae!==1){if(d===void 0)throw new RangeError("'status' must be 0 (censored) or 1 (event)");x.add("Invalid status value, converted to NA");continue}if(e[H]===-1/0||e[H]===1/0&&ae===1)throw new RangeError("event times must be finite");E.push(H)}let R=E.length;if(R===0)throw new RangeError(d===void 0?"no observations without missing values":"No (non-missing) observations");if(E.some(H=>n[H].some(ae=>!Number.isFinite(ae))))throw new RangeError("'x' contains infinite values");let A=E.map(H=>i===void 0?1:i[H]);if(A.some(H=>!(H>0)))throw new RangeError("Invalid weights, must be >0");if(A.some(H=>H===1/0))throw new RangeError("'weights' must be finite");let S=w===void 0?void 0:kt(w),T=new Map;if(S!==void 0)for(let H of E){if(t[H]!==1)continue;let ae=S[H];T.set(ae,(T.get(ae)??0)+1)}let L=d!==void 0&&[...T.values()].some(H=>H>1),C=u??(h!==void 0||A.some(H=>!Number.isInteger(H))||L);if(d!==void 0&&u===!0&&h===void 0&&w===void 0)throw new RangeError("one of cluster or id is needed");h!==void 0&&!C&&x.add("cluster specified with robust=FALSE, cluster ignored");let q=0;for(let H of E)q+=t[H];let O=E.map(H=>e[H]),_=d===void 0?void 0:E.map(H=>d[H]),F,I;if(_===void 0)F=M?io(O):O;else if(!M)F=O,I=_;else{let H=io([..._,...O]);if(I=H.slice(0,R),F=H.slice(R),I.some((ae,Ie)=>ae>=F[Ie]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(q===0){let H=vb(E.map(ae=>n[ae]),y,x.list);return S!==void 0&&(H.nId=new Set(E.map(ae=>S[ae])).size),{result:H}}let P=new Array(R).fill(0),k,V=[];if(c!==void 0){let H=kt(c);V=Array.from(new Set(E.map(Ie=>H[Ie]))).sort(gb);let ae=new Map;V.forEach((Ie,Ge)=>ae.set(Ie,Ge)),k=E.map(Ie=>H[Ie]),P=E.map(Ie=>ae.get(H[Ie]))}let U=Array.from({length:R},(H,ae)=>ae).sort((H,ae)=>P[H]-P[ae]||F[H]-F[ae]||H-ae),Q=U.map(H=>A[H]),{groups:re,k:z}=gt(Q)>=wb?Se([Q]):{groups:[Q],k:0},J=re[0],Z=0;for(let H of A)Z+=z===0?H:D(H,-z);let ke=U.map(H=>n[E[H]].slice()),ne=new Array(y).fill(0),$=new Array(y).fill(1),Y=new Array(y).fill(0),W=new Set(f);for(let H=0;H<y;H++){if(ke.every(He=>W.has(He[H])))continue;let ae=ke.map(He=>He[H]),Ie=He=>{let zt=0;for(let On=0;On<R;On++)zt+=(d===void 0?J[On]:1)*He[On];let Cn=d===void 0?Z:R,wr=zt/Cn,$r=0,$a=He.map((On,gs)=>($r+=(d===void 0?J[gs]:1)*Math.abs(On-wr),On-wr)),zr=$r===0?1:Cn/$r;return{m:wr,s:zr,d:$a,ok:Number.isFinite(wr)&&zr>=2**-1022&&zr<1/0}},Ge=Ie(ae);if(!Ge.ok){let{groups:He,k:zt}=Se([ae]);Ge=Ie(He[0]),Y[H]=zt}for(let He=0;He<R;He++)ke[He][H]=Ge.d[He]*Ge.s;ne[H]=D(Ge.m,Y[H]),$[H]=Ge.s}let B={time:U.map(H=>F[H]),...I===void 0?{}:{start:U.map(H=>I[H])},event:U.map(H=>t[E[H]]===1),weight:J,x:ke,stratumEnd:U.map((H,ae)=>ae===R-1||P[U[ae+1]]!==P[H]),logUnit:z*Math.LN2},j=s==="efron",te=a===void 0?new Array(y).fill(0):a.map((H,ae)=>D(H/$[ae],Y[ae])),fe=Na(B,te,j),Re=[fe.loglik,fe.loglik],Ce=fe.info.map(H=>H.slice()),be=ss(Ce,N),me=Ma(Ce,fe.score),qe=0;for(let H=0;H<y;H++)qe+=fe.score[H]*me[H];let de=0,Oe,xe=!1,Ve=b===0;if(b===0||!Number.isFinite(fe.loglik))Oe=Ea(Ce);else{let H=fe.loglik,ae=te.map((Ge,He)=>Ge+me[He]),Ie=0;for(Ve=!1,de=1;de<=b;de++){fe=Na(B,ae,j),Ce=fe.info.map(He=>He.slice()),be=ss(Ce,N);let Ge=Number.isFinite(fe.loglik)&&fe.score.every(Number.isFinite)&&fe.info.every(He=>He.every(Number.isFinite));if(Ge&&(fe.loglik===H||Math.abs(1-H/fe.loglik)<=g)){Ve=!0,Re[1]=fe.loglik,te=ae;break}!Ge||fe.loglik<H?(Ie++,ae=ae.map((He,zt)=>(He+Ie*te[zt])/(Ie+1))):(Ie=0,H=fe.loglik,Re[1]=H,me=Ma(Ce,fe.score),te=ae,ae=ae.map((He,zt)=>He+me[zt]))}Ve?Oe=Ea(Ce):(de=d===void 0?b+1:b,xe=!0,d!==void 0?(fe=Na(B,te,j),Re[1]=fe.loglik,Ce=fe.info.map(Ge=>Ge.slice()),be=ss(Ce,N),Oe=Ea(Ce)):b>1?(fe=Na(B,te,j),Re[1]=fe.loglik,Oe=fe.info.map((Ge,He)=>Ge.map((zt,Cn)=>He===Cn?Ge[He]>0?1/Ge[He]:0:-0))):Oe=Ea(Ce))}let Fe=$.map(H=>Math.floor(Math.log2(H))),Ue=$.map((H,ae)=>D(H,-Fe[ae])),ze=Fe.map((H,ae)=>H-Y[ae]),et=te.map((H,ae)=>H*Ue[ae]),Be=fe.score.map((H,ae)=>H/Ue[ae]),Xe=Oe.map((H,ae)=>H.map((Ie,Ge)=>Ie*Ue[ae]*Ue[Ge])),pe=et.map((H,ae)=>D(H,ze[ae])),oe=Xe.map((H,ae)=>H.map((Ie,Ge)=>D(Ie,ze[ae]+ze[Ge]-z))),we=pe.slice();if(b>0&&Math.abs(be)<y)for(let H=0;H<y;H++)Oe[H][H]===0&&(we[H]=NaN);let ge=d===void 0?pe:pe.map((H,ae)=>Number.isNaN(we[ae])?0:H),ce=0;for(let H=0;H<y;H++)ce+=ge[H]*ne[H];let tt=E.map(H=>{let ae=0;for(let Ie=0;Ie<y;Ie++)ae+=n[H][Ie]*ge[Ie];return ae-ce});if(b>1){let H=Xe.map((ae,Ie)=>{let Ge=0;for(let He=0;He<y;He++)Ge+=Be[He]*Xe[He][Ie];return Math.abs(Ge)});if(xe)(d===void 0||b>1)&&x.add("Ran out of iterations and did not converge"),d===void 0&&(gt(tt)>500||H.some(ae=>!Number.isFinite(ae)))&&x.add("one or more coefficients may be infinite");else if(d===void 0){let ae=[];for(let Ie=0;Ie<y;Ie++){let Ge=H[Ie];(!Number.isFinite(Be[Ie])||D(Ge,ze[Ie])>g&&Ge>v*Math.abs(et[Ie]))&&ae.push(Ie+1)}ae.length>0&&x.add("Loglik converged before variable "+ae.join(",")+" ; coefficient may be infinite. ")}}let G=we.map((H,ae)=>Number.isNaN(H)?-1:ae).filter(H=>H>=0),ue=G.length,Me,ye=0,Ee=0,ee;if(C&&ue>0){let H=U.map((ct,Qe)=>Qe),ae=R,Ie=h??(d===void 0?void 0:w);if(Ie!==void 0){let ct=kt(Ie),Qe=new Map;H=U.map(wt=>{let Mt=ct[E[wt]],Et=Qe.get(Mt);return Et===void 0&&(Et=Qe.size,Qe.set(Mt,Et)),Et}),ae=Qe.size}if(ae<2)throw new RangeError(d===void 0?"at least two clusters are needed for the robust variance":"(subscript) logical subscript too long");let Ge=ct=>ct.map(Qe=>Qe.map((wt,Mt)=>wt/Ue[Mt])),He=Ge(d===void 0?oh(B,te,j):qc(B,te,j)),zt=He.map(ct=>Xe.map((Qe,wt)=>{let Mt=0;for(let Et=0;Et<y;Et++)Mt+=ct[Et]*Xe[Et][wt];return Mt})),Cn;if(z===0)Cn=zt.map((ct,Qe)=>ct.map(wt=>J[Qe]*wt));else{let ct=wt=>wt===0||!Number.isFinite(wt)?0:Math.floor(Math.log2(Math.abs(wt))),Qe=-1/0;zt.forEach((wt,Mt)=>wt.forEach(Et=>Et!==0&&Number.isFinite(Et)&&(Qe=Math.max(Qe,ct(J[Mt])+ct(Et))))),ye=Qe===-1/0?0:-Qe,Cn=zt.map((wt,Mt)=>{let Et=ct(J[Mt]);return wt.map(br=>{let ll=ct(br);return D(D(J[Mt],-Et)*D(br,-ll),Et+ll+ye)})})}let wr=ct=>{let Qe=Array.from({length:ae},()=>new Array(y).fill(0));return ct.forEach((wt,Mt)=>{let Et=Qe[H[Mt]];for(let br=0;br<y;br++)Et[br]+=wt[br]}),Qe},$r=ct=>{let Qe=Array.from({length:y},()=>new Array(y).fill(0));for(let wt of ct)for(let Mt=0;Mt<y;Mt++)for(let Et=0;Et<y;Et++)Qe[Mt][Et]+=wt[Mt]*wt[Et];return Qe};Me=$r(wr(Cn));let $a=Se([Q]).groups[0],zr=a===void 0?new Array(y).fill(0):a.map((ct,Qe)=>D(ct/$[Qe],Y[Qe])),On=d===void 0?oh(B,zr,j):qc(B,zr,j),gs=wr(Ge(On).map((ct,Qe)=>ct.map(wt=>$a[Qe]*wt))),So=new Array(y).fill(0);for(let ct of gs)for(let Qe=0;Qe<y;Qe++)So[Qe]+=ct[Qe];let za=$r(gs);if(y===1)Ee=So[0]*So[0]/za[0][0];else{ss(za,N);let ct=Ma(za,So);for(let Qe=0;Qe<y;Qe++)Ee+=So[Qe]*ct[Qe]}let il=new Array(R),cl=new Array(R);U.forEach((ct,Qe)=>{il[ct]=He[Qe].map((wt,Mt)=>D(wt,-ze[Mt])),cl[ct]=Cn[Qe].map((wt,Mt)=>D(wt,ze[Mt]-ye))}),ee={scoreResiduals:il,dfbeta:cl}}let ve=Me!==void 0,ie=Me??Xe,Ae=Me===void 0?-z:-2*ye,Ke=G.map(H=>et[H]-(a===void 0?0:D(a[H],-ze[H]))),$e=0;if(ue===1)$e=Ke[0]*Ke[0]/ie[G[0]][G[0]];else if(ue>1){let H=G.map(Ie=>G.map(Ge=>ie[Ie][Ge]));ss(H,N);let ae=Ma(H,Ke);for(let Ie=0;Ie<ue;Ie++)$e+=Ke[Ie]*ae[Ie]}$e=D($e,-Ae);let ot=(1-m)/2,ht=ot<1e-8?le(ot,{lowerTail:!1}):le((1+m)/2),Ct=(H,ae)=>{let Ie=Math.floor(ae/2),Ge=H.map((He,zt)=>Math.sqrt(D(He[zt],ae-2*Ie)));return{unit:Ge,h:Ie,se:Ge.map((He,zt)=>D(He,ze[zt]+Ie))}},Bt=Ct(Xe,-z).se,{unit:Hn,h:We,se:je}=Ct(ie,Ae),xt=we.map((H,ae)=>D((Number.isNaN(H)?H:et[ae])/Hn[ae],-We)),Rt=xt.map(H=>_t(H*H,1,{lowerTail:!1})),Sn=we.map((H,ae)=>Math.exp(H-ht*je[ae])),$t=we.map((H,ae)=>Math.exp(H+ht*je[ae])),It=D(-2*(Re[0]-Re[1]),z),pt=[D(Re[0],z),D(Re[1],z)],Ht=D(qe,z),on=Me?.map((H,ae)=>H.map((Ie,Ge)=>D(Ie,ze[ae]+ze[Ge]-2*ye))),Zt={coefficients:we,var:on??oe,loglik:pt,score:Ht,wald:$e,iter:de,means:ne,n:R,nevent:q,...S===void 0?{}:{nId:new Set(E.map(H=>S[H])).size},linearPredictors:tt,se:Bt,z:xt,p:Rt,stdError:Bt,zValue:xt,pValue:Rt,lower:Sn,upper:$t,logtest:as(It,ue),sctest:as(Ht,ue),waldtest:as($e,ue),rsq:{rsq:1-Math.exp(-It/R),maxrsq:1-Math.exp(2*pt[0]/R)},usedRobust:ve,...ve?{naiveVar:oe,robustSe:je,rscore:Ee,robscore:as(Ee,ue)}:{},warnings:x.list},Ot=fe.score.map((H,ae)=>D(H/$[ae],Y[ae]+z));Object.defineProperty(Zt,"coxZphData",{enumerable:!1,value:{time:F.slice(),...I===void 0?{}:{start:I.slice()},status:E.map(H=>t[H]===1),weights:A.slice(),...k===void 0?{}:{strata:k.slice()},x:E.map(H=>n[H].slice()),sortedToInput:U.slice(),sortedTime:B.time.slice(),...B.start===void 0?{}:{sortedStart:B.start.slice()},sortedEvent:B.event.slice(),sortedWeight:B.weight.slice(),sortedX:B.x.map(H=>H.slice()),stratumIndex:U.map(H=>P[H]),stratumEnd:B.stratumEnd.slice(),betaScaled:te.slice(),inverseCovariateScale:Ue.map((H,ae)=>D(1/H,-ze[ae])),weightExp:z,ties:s}});let Jt=ee?.scoreResiduals;if(d!==void 0&&o&&Jt===void 0){let H=qc(B,te,j);Jt=new Array(R),U.forEach((ae,Ie)=>{Jt[ae]=H[Ie].map((Ge,He)=>D(Ge/Ue[He],-ze[He]))})}return{result:Zt,...ee,...Jt===void 0?{}:{scoreResiduals:Jt},first:Ot,converged:Ve}}var sh=["transform","terms","collapseTerms","global"],Un=e=>new Array(e).fill(0),hr=e=>Array.from({length:e},()=>Un(e));function Mb(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 ah(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 Eb(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=ah(i,a)-ah(s,a);l.deaths>0&&(c*=1-l.deaths/f)}return n}function xb(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=Eb(e);else if(t==="rank")o=Mb(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 Rb(e,t={}){K(t,sh,"coxZph"),K(t,sh,"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(($,Y)=>Number.isNaN($)?-1:Y).filter($=>$>=0),c=i.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let a=[];for(let $=0;$<s.sortedTime.length;){let Y=$;for(;!s.stratumEnd[Y];)Y++;for(let W=$;W<=Y;){let B=s.sortedTime[W],j=W;for(;j<Y&&s.sortedTime[j+1]===B;)j++;let te=[];for(let fe=W;fe<=j;fe++)s.sortedEvent[fe]&&te.push(fe);s.start!==void 0&&te.reverse();for(let fe of te)a.push(s.sortedToInput[fe]);W=j+1}$=Y+1}if(a.length===0)throw new Error("the proportional-hazards test requires events");let l=a.map($=>s.time[$]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=xb(s,n,a,f),u=m[a[0]];if(a.every($=>m[$]===u))throw new Error("the transform is constant at the event times");let h=a.reduce(($,Y)=>Math.max($,Math.abs(m[Y])),0),d=h===0?0:Math.floor(Math.log2(h)),w=m.map($=>Number.isFinite($)?D($,-d):$),g=a.reduce(($,Y)=>$+w[Y],0)/a.length,b=w.map($=>$-g);if(a.every($=>b[$]===0))throw new Error("the transform is constant at the event times");let N=i.map($=>s.betaScaled[$]),v=hr(c),M=hr(c),p=hr(c),y=Un(c),x=[];for(let $=0;$<s.sortedTime.length;){let Y=$;for(;!s.stratumEnd[Y];)Y++;for(let W=$;W<=Y;){let B=s.sortedTime[W],j=W;for(;j<Y&&s.sortedTime[j+1]===B;)j++;let te=[];for(let fe=W;fe<=j;fe++)s.sortedEvent[fe]&&te.push(fe);if(te.length>0){let fe=[],Re=-1/0;for(let oe=$;oe<=Y;oe++){if(!((s.sortedStart===void 0||s.sortedStart[oe]<B)&&s.sortedTime[oe]>=B))continue;fe.push(oe);let ge=0;for(let ce=0;ce<c;ce++)ge+=N[ce]*s.sortedX[oe][i[ce]];Re=Math.max(Re,ge)}if(fe.length===0||!Number.isFinite(Re))throw new Error("empty Cox proportional-hazards risk set");let Ce=Un(c),be=hr(c),me=Un(c),qe=hr(c),de=0,Oe=0;for(let oe of fe){let we=0;for(let ce=0;ce<c;ce++)we+=N[ce]*s.sortedX[oe][i[ce]];let ge=s.sortedWeight[oe]*Math.exp(we-Re);de+=ge;for(let ce=0;ce<c;ce++){let tt=s.sortedX[oe][i[ce]];Ce[ce]+=ge*tt;for(let G=0;G<c;G++)be[ce][G]+=ge*tt*s.sortedX[oe][i[G]]}if(s.sortedEvent[oe]&&s.sortedTime[oe]===B){Oe+=ge;for(let ce=0;ce<c;ce++){let tt=s.sortedX[oe][i[ce]];me[ce]+=ge*tt;for(let G=0;G<c;G++)qe[ce][G]+=ge*tt*s.sortedX[oe][i[G]]}}}let xe=0,Ve=Un(c);for(let oe of te){let we=s.sortedWeight[oe];xe+=we;for(let ge=0;ge<c;ge++)Ve[ge]+=we*s.sortedX[oe][i[ge]]}let Fe=te.length,Ue=s.ties==="efron"&&Fe>1?Fe:1,ze=Un(c),et=hr(c);for(let oe=0;oe<Ue;oe++){let we=oe/Ue,ge=de-we*Oe,ce=Ce.map((G,ue)=>(G-we*me[ue])/ge);for(let G=0;G<c;G++)ze[G]+=ce[G]/Ue;let tt=xe/Ue;for(let G=0;G<c;G++)for(let ue=0;ue<c;ue++)et[G][ue]+=tt*((be[G][ue]-we*qe[G][ue])/ge-ce[G]*ce[ue])}let Be=Ve.map((oe,we)=>oe-xe*ze[we]),Xe=s.sortedToInput[te[0]],pe=b[Xe];for(let oe=0;oe<c;oe++){y[oe]+=pe*Be[oe];for(let we=0;we<c;we++)v[oe][we]+=et[oe][we],M[oe][we]+=pe*et[oe][we],p[oe][we]+=pe*pe*et[oe][we]}s.start!==void 0&&te.reverse();for(let oe of te){let we=s.sortedToInput[oe],ge=i.map(ce=>s.sortedX[oe][ce]);for(let ce=0;ce<c;ce++)ge[ce]-=ze[ce];x.push({input:we,time:B,stratum:s.stratumIndex[oe],g:m[we],residual:ge})}}W=j+1}$=Y+1}let E;try{E=nt(v,M)}catch{throw new Error("the proportional-hazards information is singular")}let R=hr(c);for(let $=0;$<c;$++)for(let Y=0;Y<c;Y++){let W=0;for(let B=0;B<c;B++)W+=M[B][$]*E[B][Y];R[$][Y]=p[$][Y]-W}for(let $=0;$<c;$++)for(let Y=0;Y<$;Y++){let W=(R[$][Y]+R[Y][$])/2;R[$][Y]=W,R[Y][$]=W}let A=t.terms===!1||t.terms===void 0?i.map($=>[$]):t.terms,S=new Set,T=[],L=[];if(A.forEach(($,Y)=>{if(!Array.isArray($)||$.length===0)throw new RangeError("each terms group must be a non-empty array");let W=[];for(let B of $){if(!Number.isInteger(B)||B<0||B>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(S.has(B))throw new RangeError("terms groups must not overlap");S.add(B),i.includes(B)&&W.push(B)}W.length>0&&(T.push(W),L.push(Y))}),i.forEach($=>{S.has($)||(T.push([$]),L.push(A.length))}),T.length===0)throw new Error("the proportional-hazards test has no estimable terms");let C=[];T.forEach(($,Y)=>{if(o&&$.length>1)C.push({col:-1,group:Y,collapsed:!0});else for(let W of $)C.push({col:W,group:Y,collapsed:!1})});let q=C.length,O=Array.from({length:c},()=>Un(q)),_=[],F=[];C.forEach(($,Y)=>{let W=T[$.group],B=L[$.group];if($.collapsed){for(let j of W){let te=i.indexOf(j);O[te][Y]=N[te]}_.push(1),F.push(`g${B+1}`)}else{let j=i.indexOf($.col);O[j][Y]=s.inverseCovariateScale[$.col],_.push(e.coefficients[$.col]),F.push(W.length>1?`g${B+1}x${$.col+1}`:`x${$.col+1}`)}});let I=Array.from({length:q},($,Y)=>{let W=0;for(let B=0;B<c;B++)W=Math.max(W,Math.abs(O[B][Y]));if(!(W>0&&Number.isFinite(W)))throw new Error("the proportional-hazards residual variance is singular");return Math.floor(Math.log2(W))}),P=O.map($=>$.map((Y,W)=>D(Y,-I[W]))),k=Array.from({length:c},()=>Un(q));for(let $=0;$<c;$++)for(let Y=0;Y<q;Y++)for(let W=0;W<c;W++)k[$][Y]+=v[$][W]*P[W][Y];let V=hr(q);for(let $=0;$<q;$++)for(let Y=0;Y<q;Y++)for(let W=0;W<c;W++)V[$][Y]+=P[W][$]*k[W][Y];for(let $=0;$<q;$++)for(let Y=0;Y<$;Y++){let W=(V[$][Y]+V[Y][$])/2;V[$][Y]=W,V[Y][$]=W}let U;try{U=nt(V)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let $=0;$<q;$++)for(let Y=0;Y<$;Y++){let W=(U[$][Y]+U[Y][$])/2;U[$][Y]=W,U[Y][$]=W}let Q=U.map(($,Y)=>$.map((W,B)=>a.length*Le(W,-I[Y]-I[B]-s.weightExp))),re=x.map($=>{let Y=Un(q);for(let W=0;W<q;W++)for(let B=0;B<c;B++)Y[W]+=P[B][W]*$.residual[B];return _.map((W,B)=>{let j=0;for(let te=0;te<q;te++)j+=Y[te]*U[te][B];return W+a.length*Le(j,-I[B]-s.weightExp)})}),z=[],J=[],Z=[],ke=C.map($=>$.collapsed?T[$.group].map(Y=>i.indexOf(Y)):[i.indexOf($.col)]);for(let $ of ke){let Y=$.map(te=>$.map(fe=>R[te][fe])),W=$.map(te=>y[te]),B;try{B=nt(Y,W)}catch{throw new Error("the proportional-hazards term information is singular")}let j=D(Math.max(0,W.reduce((te,fe,Re)=>te+fe*B[Re],0)),s.weightExp);z.push(j),J.push($.length),Z.push(_t(j,$.length,{lowerTail:!1}))}if(r){let $;try{$=nt(R,y)}catch{throw new Error("the global proportional-hazards information is singular")}let Y=D(Math.max(0,y.reduce((W,B,j)=>W+B*$[j],0)),s.weightExp);z.push(Y),J.push(c),Z.push(_t(Y,c,{lowerTail:!1})),F.push("GLOBAL")}let ne={rowNames:F,chisq:z,df:J,p:Z,pValue:Z,x:x.map($=>$.g),time:x.map($=>$.time),...s.strata===void 0?{}:{strata:x.map($=>ut(s.strata[$.input]))},y:re,var:Q,transform:n,warnings:f};return X("coxZph",ne,e)}var Ab=["center"],Sb=2**-1022;function Tb(e,t,n={}){K(n,Ab,"leveneTest");let r=Te(n.center??"median",["median","mean"]),{groups:o}=or(e,t);if(o.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let s=l=>{let f=r==="median"?na(l):it(l);return l.map(m=>Math.abs(m-f))},i=Se(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<Sb)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let h=Se([u]);return{d:s(h.groups[0]),k:h.k}}),f=Math.max(...l.map(u=>u.d.some(h=>h>0)?Math.floor(Math.log2(Math.max(...u.d)))+u.k:-1/0)),m=Number.isFinite(f)?f:0;i=l.map(u=>u.d.map(h=>D(h,u.k-m)))}let a=fu(Sr(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 kb=["score"],qb=["alternative","exact","variant","levels"];function Ib(e,t,...n){K(Br(n),kb,"propTrendTest");let{score:r}=Ur(["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:d,sxy:w,sxx:g}=Ic(f,m,h,u);if(!(g>=2**-1022&&g<1/0&&Math.abs(w)<1/0)){let N=Se([u.map(v=>c[v])]);N.k!==0&&(h=c.slice(),u.forEach((v,M)=>h[v]=N.groups[0][M]),{sw:d,sxy:w,sxx:g}=Ic(f,m,h,u))}let b=Vt(w,g);if(!(a<1/0)||l>0&&l<1&&!(d<1/0&&Math.abs(w)<1/0&&g<1/0)){let{groups:[N,v],k:M}=Se([o,s]),p=N.reduce((x,E)=>x+E,0)/v.reduce((x,E)=>x+E,0),y=Ic(v,m,h,u);b=D(y.sxy/p*(y.sxy/y.sxx/(1-p)),M)}return{method:"Chi-squared Test for Trend in Proportions",statistic:b,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:yt(b,.5,2,!1,!1),warnings:[]}}function Ic(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 _b=2**24,Lb=2**32,Cb=`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 Fb(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=kt(t));let c;if(n!==void 0)c=n.map(ut);else{let f=[...new Set(i.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(ut):[...new Set(f.map(ut))].sort(wn)}let a=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of i){let m=l.get(ut(t[f]));if(m===void 0)throw new RangeError(`the group '${ut(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function Pb(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 Db(e,t,n={}){K(n,qb,"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=Fb(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 M=v+1;M<s.length;M++)for(let p of N)for(let y of s[M])l+=p<y?1:p===y?.5:0});let u=[],h=!a&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&a&&(h=!1,u.push(Ob))):h||u.push(Cb);let d,w;if(h){let N=1,v=0;for(let[p,y]of f)p===0||y===0||(v+=N*(p*y+1),N+=p*y);if(N>_b||v>Lb)throw new RangeError(`the exact distribution of JT has ${N} values and needs ${v} steps, too many: use exact: false`);let M=[1];for(let[p,y]of f){if(p===0||y===0)continue;let x=Array.from({length:p*y+1},(R,A)=>Rl(A,p,y,!1)),E=new Array(M.length+x.length-1).fill(0);M.forEach((R,A)=>x.forEach((S,T)=>E[A+T]+=R*S)),M=E}d=0,w=0,M.forEach((p,y)=>{y<=l&&(d+=p),y>=l&&(w+=p)})}else{let N=f.reduce((p,[y,x])=>p+y*x,0)/2,v=a?Pb(s.map(p=>p.length),i):f.reduce((p,[y,x])=>p+y*x*(y+x+1)/12,0),M=(l-N)/Math.sqrt(v);d=se(M),w=1-d,w<1e-8&&(w=se(M,{lowerTail:!1}))}let b=r==="increasing"?w:r==="decreasing"?d:Math.min(1,2*Math.min(w,d,o==="clinfun"?.5:1));return X("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:b,warnings:u})}var Vb=["nClasses","adjust"];function is(e){return Dt(Array.from(e)).sort((t,n)=>t-n)}function ih(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function xa(e){ih(e);let t=vl(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function $b(e){let t=is(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=xa(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(se(r[a-1],{logP:!0})+se(r[n-a],{lowerTail:!1,logP:!0}));let s=-n-o/n,i=(1+.75/n+2.25/n**2)*s,c;return i<.2?c=1-Math.exp(-13.436+101.14*i-223.73*i**2):i<.34?c=1-Math.exp(-8.318+42.796*i-59.938*i**2):i<.6?c=Math.exp(.9177-4.279*i-1.38*i**2):i<10?c=Math.exp(1.2937-5.709*i+.0186*i**2):c=37e-25,{method:"Anderson-Darling normality test",statistic:s,statisticName:"A",pValue:c,warnings:[]}}function zb(e){let t=is(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=xa(t),o=1/(12*n);r.forEach((a,l)=>{o+=(se(a)-(2*(l+1)-1)/(2*n))**2});let s=(1+.5/n)*o,i=[],c;return s<.0275?c=1-Math.exp(-13.953+775.5*s-12542.61*s**2):s<.051?c=1-Math.exp(-5.903+179.546*s-1515.29*s**2):s<.092?c=Math.exp(.886-31.62*s+10.897*s**2):s<1.1?c=Math.exp(1.111-34.242*s+12.832*s**2):(c=737e-12,i.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),X("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function Wb(e){let t=is(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=xa(t).map(f=>se(f)),o=-1/0,s=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),s=Math.max(s,f-m/n)});let i=Math.max(o,s),c=n<=100?i:i*(n/100)**.49,a=n<=100?n:100,l=Math.exp(-7.01256*c**2*(a+2.78019)+2.99587*c*Math.sqrt(a+2.78019)-.122119+.974598/Math.sqrt(a)+1.67997/a);if(l>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*i;f<=.302?l=1:f<=.5?l=2.76773-19.828315*f+80.709644*f**2-138.55152*f**3+81.218052*f**4:f<=.9?l=-4.901232+40.662806*f-97.490286*f**2+94.029866*f**3-32.355711*f**4:f<=1.31?l=6.198765-19.558097*f+23.186922*f**2-12.234627*f**3+2.423045*f**4:l=0}return{method:"Lilliefors (Kolmogorov-Smirnov) normality test",statistic:i,statisticName:"D",pValue:l,warnings:[]}}function jb(e){let t=is(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(ih(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>le((u+1-3/8)/(n+1/4))),o=Se([t]).groups[0],s=Math.min(bl(o,r)**2/(st(o)*st(r)),1),i=Math.log(n),c=Math.log(i),a=-1.2725+1.0521*(c-i),l=1.0308-.26758*(c+2/i),f=se((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function Kb(e,t={}){K(t,Vb,"pearsonTest");let n=is(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),s=t.adjust??!0;if(!(Number.isInteger(o)&&o-1-(s?2:0)>0))throw new RangeError("'nClasses' must be an integer leaving positive degrees of freedom");if(o>2147483647)throw new RangeError("attempt to make a table with >= 2^31 elements");let i=[];for(let f of xa(n)){let m=Math.min(o,Math.floor(1+o*se(f)));m>=1&&i.push(m)}i.sort((f,m)=>f-m);let c=r/o,a=0;for(let f=1,m=0;f<=o;f++){let u=0;for(;m<i.length&&i[m]===f;)u++,m++;a=a+(u-c)**2/c}let l=o-1-(s?2:0);return{method:"Pearson chi-square normality test",statistic:a,statisticName:"P",pValue:yt(a,l/2,2,!1,!1),warnings:[]}}var Ub=["weight"],Bb=["exact"],Hb=["model","type","unit","r0","confLevel"];function cs(e,t){let n=e[0]?.length??0;if(e.some(r=>r.length!==n))throw new TypeError(`'${t}' must be a matrix (rows of equal length)`);return Array.from({length:n},(r,o)=>e.map(s=>s[o]))}function Ra(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`);if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError(`'${t}' contains infinite values`)}var lh=e=>2*se(-Math.abs(e));function Yb(e,t={}){K(t,Ub,"kappa2");let n=Te(t.weight,["unweighted","equal","squared"]);if(e.some(x=>x.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(x=>!x.some(Number.isNaN)),o=r.length;if(o===0)throw new RangeError("no complete rating pairs");if(o<2)throw new RangeError("at least 2 complete rating pairs are needed");let s=r.map(x=>$n(x[0])),i=r.map(x=>$n(x[1])),c=[...new Set([...s,...i])].sort((x,E)=>x<E?-1:x>E?1:0),a=c.length,l=new Map(c.map((x,E)=>[x,E])),f=Array.from({length:a},()=>new Array(a).fill(0));for(let x=0;x<o;x++)f[l.get(s[x])][l.get(i[x])]++;let m=x=>n==="unweighted"?x===0?1:0:n==="equal"?1-x/(a-1):1-x*x/((a-1)*(a-1)),u=Array.from({length:a},(x,E)=>Array.from({length:a},(R,A)=>m(Math.abs(E-A)))),h=f.map(x=>De(x)/o),d=Array.from({length:a},(x,E)=>De(f.map(R=>R[E]))/o),w=x=>{let E=[];for(let R=0;R<a;R++)for(let A=0;A<a;A++)E.push(x(R,A));return De(E)},g=w((x,E)=>f[x][E]*u[x][E])/o,b=w((x,E)=>h[x]*d[E]*u[x][E]),N=(g-b)/(1-b),v=Array.from({length:a},(x,E)=>De(d.map((R,A)=>R*u[E][A]))),M=Array.from({length:a},(x,E)=>De(h.map((R,A)=>R*u[A][E]))),p=(w((x,E)=>h[x]*d[E]*(u[x][E]-(v[x]+M[E]))**2)-b*b)/(o*(1-b)**2),y=N/Math.sqrt(p);return{value:N,statistic:y,pValue:lh(y),warnings:[]}}function Gb(e,t={}){K(t,Bb,"kappamFleiss");let n=t.exact??!1,r=cs(e,"ratings");Ra(e,"ratings");let o=e.length,s=r.length;if(o<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let i=e.flat(),c=kt(i),a=[...new Set(c)].sort((p,y)=>p-y),l=new Map(a.map((p,y)=>[p,y])),f=new Map(i.map((p,y)=>[p,l.get(c[y])])),m=a.length,u=e.map(p=>{let y=new Array(m).fill(0);for(let x of p)y[f.get(x)]++;return y}),h=De(u.map(p=>(De(p.map(y=>y*y))-s)/(s*(s-1))))/o,d=Array.from({length:m},(p,y)=>De(u.map(x=>x[y]))/(o*s)),w=De(d.map(p=>p*p));if(n){let p=r.map(x=>{let E=new Array(m).fill(0);for(let R of x)E[f.get(R)]++;return E.map(R=>R/o)}),y=Array.from({length:m},(x,E)=>st(p.map(R=>R[E]))*(s-1)/s);w-=De(y)/(s-1)}let g=(h-w)/(1-w);if(n)return{value:g,warnings:[]};let b=De(d.map(p=>p*(1-p))),N=De(d.map(p=>p*(1-p)*(1-p-p))),v=2/(b*b*o*s*(s-1))*(b*b-N),M=g/Math.sqrt(v);return{value:g,statistic:M,pValue:lh(M),warnings:[]}}function Xb(e,t={}){K(t,Hb,"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;Lt(i),cs(e,"ratings"),Ra(e,"ratings");let c=Se(e).groups,a=cs(c,"ratings"),l=c.length,f=a.length;if(l<2||f<2)throw new RangeError("at least 2 subjects and 2 raters are needed");if(!(s>=-1&&s<1))throw new RangeError("'r0' must be in [-1, 1)");let m=1-i,u=st(c.map(P=>it(P)))*f,h=st(a.map(P=>it(P)))*l,d=it(c.map(P=>st(P))),w=st(c.flat())*(l*f-1),g=w-u*(l-1)-h*(f-1),b=g>2**-20*w?g/((l-1)*(f-1)):ev(c)/((l-1)*(f-1)),N=(P,k,V)=>dt(P,k,V,!1,!1),v=(P,k)=>m/2<1e-8?pn(m/2,P,k,!1,!1):pn(1-m/2,P,k,!0,!1),M=l-1;if(n==="oneway"||r==="consistency"){let P=n==="oneway"?d:b,k=n==="oneway"?l*(f-1):(l-1)*(f-1),V=u/P,U=V/v(M,k),Q=V*v(k,M),[re,z,J]=o==="single"?[(u-P)/(u+(f-1)*P),V*(1-s)/(1+(f-1)*s),Z=>(Z-1)/(Z+f-1)]:[(u-P)/u,V*(1-s),Z=>1-1/Z];return{value:re,Fvalue:z,fValue:z,df1:M,df2:k,pValue:N(z,M,k),lbound:J(U),ubound:J(Q),warnings:[]}}let p=o==="single"?f:1,y=P=>[p*P/(l*(1-P)),1+p*P*(l-1)/(l*(1-P))],x=(P,k)=>yl([P*h,k*b],[f-1,(l-1)*(f-1)]),E=o==="single"?(u-b)/(u+(f-1)*b+f/l*(h-b)):(u-b)/(u+(h-b)/l),[R,A]=y(s),S=u/(R*h+A*b),T=x(R,A),[L,C]=[f*E/(l*(1-E)),1+f*E*(l-1)/(l*(1-E))],q=x(L,C),O=v(M,q),_=v(q,M),[F,I]=o==="single"?[l*(u-O*b)/(O*(f*h+(f*l-f-l)*b)+l*u),l*(_*u-b)/(f*h+(f*l-f-l)*b+l*_*u)]:[l*(u-O*b)/(O*(h-b)+l*u),l*(_*u-b)/(h-b+l*_*u)];return{value:E,Fvalue:S,fValue:S,df1:M,df2:T,pValue:N(S,M,T),lbound:F,ubound:I,warnings:[]}}function ch(e,t){let n=t.length,r=De(t.flatMap(s=>t.map(i=>e[s][i])));return(1-De(t.map(s=>e[s][s]))/r)*n/(n-1)}function Qb(e){let t=cs(e,"x").length;if(Ra(e,"x"),t<2)throw new RangeError("'x' needs at least two items");if(e.length<2)throw new RangeError("'x' needs at least two subjects");let n=Fs(Se(e).groups),r=Co(e),o=Array.from({length:t},(c,a)=>a),s=De(r.flat()),i;try{let c=nt(r);i=o.map(a=>1-1/c[a][a])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let a=Se(e).groups;i=o.map(l=>{let f=a.map(h=>h[l]),m=Yr(At(a.map(h=>[1,...h.filter((d,w)=>w!==l)])),f),u=it(f);return 1-De(m.map(h=>h*h))/De(f.map(h=>(h-u)**2))})}return X("cronbachAlpha",{rawAlpha:ch(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-De(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>ch(n,o.filter(a=>a!==c))),warnings:Zb(e,r)})}function Zb(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}=jt(r.map(c=>r.map(a=>t[c][a])),{symmetric:!0}),s=o.map(c=>c[0]),i=De(s)<0?-1:1;s.some(c=>i*c<0)&&n.push(`Some items were negatively correlated with the first principal component and probably
|
|
6
|
+
k = 3 groups are required.`);let i=s.n[0],c=s.n.slice(1).map((m,u)=>u+1),a=c.map(m=>{let u=s.n[m],h=[...s.groups[0],...s.groups[m]],d=Nt(h),w=0;for(let x=0;x<i;x++)w+=d[x];let g=i+u,b=g*(g+1)/2-w,N=i*u+i*(i+1)/2-w,v=i*u+u*(u+1)/2-b,M=0;for(let x of yn(h))M+=x*x*x-x;let p=i*u/(g*(g-1))*((g*g*g-g)/12-M/12),y=(Math.min(N,v)-i*u/2)/Math.sqrt(p);return w<b?-y:y}),l=c.map(m=>s.n[m]/(s.n[m]+i)),f=Tm(a,l,r,o);return X("manyOneUTest",ji(s,`Wilcoxon, Mann, Whittney U-test
|
|
7
|
+
for multiple comparisons with one control`,a,f,o))}function Eg(e,t){let n=jn(e,t),r=Uo(n.k,(s,i)=>{let c=n.n[s],a=n.n[i],l=[...n.groups[s],...n.groups[i]],f=Nt(l),m=0;for(let b=0;b<c;b++)m+=f[b];let u=m-c*(c+1)/2,h=Math.min(u,c*a-u),d=c+a,w=0;for(let b of yn(l))w+=b*b*b-b;let g=c*a/(d*(d-1))*((d*d*d-d)/12-w/12);return Math.SQRT2*(h-c*a/2)/Math.sqrt(g)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:Ki(i,n.k,1/0)));return X("dscfAllPairsTest",sa(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function xg(e,t){let n=jn(e,t),r=Dt(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),i=Uo(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),c=Uo(n.k,(a,l)=>{let f=Nl([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return Ki(i[a-1][l],n.k,f)});return X("gamesHowellTest",sa(n,"Games-Howell test",i,c,"none"))}function Rg(e,t){let n=jn(e,t),{m:r,s2:o,df:s}=Bi(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let i=Uo(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:Ki(l,n.k,s)));return X("tukeyTest",sa(n,"Tukey's test",i,c,"single-step"))}function Ag(e,t,n={}){K(n,gg,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=jn(e,t),i=ra(e,t).statistic,{R:c,C:a}=Wi(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let d=1;d<s.k;d++)for(let w=0;w<d;w++)f.push((c[w]-c[d])/Math.sqrt(l*(1/s.n[w]+1/s.n[d]))),m.push(`${s.labels[w]} - ${s.labels[d]}`);let u=f.map(d=>(o?2:1)*oa(Math.abs(d))),h=Sg(u,r);return{chi2:i,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function Sg(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 Tg(e,t,n={}){K(n,wg,"glhtDunnett");let r=Ze(n.alternative),o=jn(e,t),{m:s,s2:i,df:c,k:a}=Bi(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]),d=m.map(N=>P(N,a)),w=u.map(N=>P(N,a)),g=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),b=h.map(N=>r==="two.sided"?kr(N,g,c,!0):kr(r==="greater"?N:-N,g,c,!1));return{names:f.map(N=>`${o.labels[N]} - ${o.labels[0]}`),estimate:d,se:w,t:h,p:b,stdError:w,tValue:h,pValue:b,df:c,warnings:[]}}var km=["varEqual"],kg=2**-1022;function Im(e,t,n={}){if(typeof e!="string"&&vt(t)&&K(t,km,"onewayTest"),K(n,km,"onewayTest"),typeof e=="string"){let g=Pl(e,t);return Im(g.values,g.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=Sr(e,r),s=Dt(o),i=s.groups,c=n.varEqual??!1,a=i.length;if(a<2)throw new RangeError("not enough groups");let l=i.map(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,b)=>g+b,0),h,d,w;if(c){let g=s.grandMean,b=0,N=0;for(let v=0;v<a;v++)b+=l[v]*(f[v]-g)**2,N+=(l[v]-1)*m[v];d=u-a,Ho(d,s.ssWithin),h=b/(a-1)/(N/d)}else if(m.some(g=>!(g>=kg))&&(w=Ig(o))!==void 0){let g=w.map((E,R)=>qm(l[R]/E.variance,-2*E.k)),b=Math.max(...g.map(E=>E.e)),N=g.map(E=>Le(E.m,E.e-b)),v=N.reduce((E,R)=>E+R,0),M=0;for(let E=0;E<a;E++){let R=N.reduce((A,S,T)=>T===E?A:A+S,0);M+=(R/v)**2/(l[E]-1)}M/=a*a-1;let p=[];for(let E=0;E<a;E++)for(let R=E+1;R<a;R++){let[A,S]=w[E].k>=w[R].k?[w[E],w[R]]:[w[R],w[E]],T=A.mean-Le(S.mean,S.k-A.k);if(T===0)continue;let L=qm(T,A.k);p.push({m:g[E].m*g[R].m*L.m*L.m/v,e:g[E].e+g[R].e+2*L.e-b})}let y=Math.max(...p.map(E=>E.e)),x=p.reduce((E,R)=>E+Le(R.m,R.e-y),0);h=(p.length===0?0:Le(x,y))/((a-1)*(1+2*(a-2)*M)),d=1/(3*M)}else if(m.some(g=>g===0)){let g=m.flatMap((M,p)=>M===0?[p]:[]);if(g.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let b=g[0],N=0,v=0;for(let M=0;M<a;M++)M!==b&&(N+=1/(l[M]-1),v+=l[M]/m[M]*(f[M]-f[b])**2);N/=a*a-1,h=v/((a-1)*(1+2*(a-2)*N)),d=1/(3*N)}else{let g=l.map((p,y)=>p/m[y]),b=g.reduce((p,y)=>p+y,0),N=0,v=0;for(let p=0;p<a;p++)N+=(1-g[p]/b)**2/(l[p]-1),v+=g[p]*f[p];N/=a*a-1,v/=b;let M=0;for(let p=0;p<a;p++)M+=g[p]*(f[p]-v)**2;h=M/((a-1)*(1+2*(a-2)*N)),d=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,d],parameterName:["num df","denom df"],pValue:dt(h,a-1,d,!1,!1),warnings:[]}}function qg(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=ym(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:Nm(r,n),warnings:[]}}function Ig(e){let t=[];for(let n of e){let r=Dt([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function qm(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=Le(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function Ui(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 _g([[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]=Nn(f,o,s,i,!1);return{lo:c,d:l}}function Lg(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 aa=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=_g(o);n+=s.lo,r=Lg(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=Ui(r,0,n);for(let c=0;c<n;c++)r[c]=r[c]/i;return r}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)r=he(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(t,n,r){let o=this.density(n),s=t-this.lo;return r?Ui(o,s,o.length):Ui(o,0,s+1)}};function Hi(e,t){return tr(e,t)}function _m(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Hi(r=>e.mean(r)-t,n>t)}function Yi(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Hi(o=>n-e.cdf(t,o,!1),r<n)}function Gi(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Hi(o=>e.cdf(t,o,!0)-n,r>n)}var Cg=["alternative","correct","exact","confLevel"];function Fg(e,t={}){K(t,Cg,"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(Xi);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=Ze(t.alternative),c=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(Lt(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?Pg(f,i,l):Og(f,s,i,c,l)}return Vg(e,s,r,o)}function Xi(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Fm(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 Om(e,t){return e.map(n=>n.map(r=>r.map(o=>P(o,-t))))}function Lm(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,d]],w)=>{let g=t[w],b=m+u,N=h+d,v=m+h,M=u+d;r+=m;let p=b*v/g;o+=Number.isFinite(p)?p:b/g*v;let y=b*N*v*M/(g*g*(g-n));s+=Number.isFinite(y)?y:b/g*(N/g)*(v/(g-n))*M;let x=(m+d)/g,E=(u+h)/g,R=m*d/g,A=u*h/g,S=Number.isFinite(R)?R:m/g*d,T=Number.isFinite(A)?A:u/g*h;i+=S,c+=T,a+=x*S,l+=x*T+E*S,f+=E*T}),{sa:r,se:o,sv:s,sR:i,sS:c,sPR:a,sPSQR:l,sQS:f}}function Og(e,t,n,r,o){let s=Lm(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=Fm(e);let x=Om(e,i);s=Lm(x,x.map(Xi),P(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:d}=s,w=c-a,g=P(.5,-i),b=r&&Math.abs(w)>=g?g:0,N=P(Vt(Math.abs(w)-b,l),i),v=n==="two.sided"?yt(N,.5,2,!1,!1):se(Math.sign(w)*Math.sqrt(N),{lowerTail:n==="less"}),M=f/m,p=P(Math.sqrt(u/(2*f*f)+h/(2*f*m)+d/(2*m*m)),-i/2),y;if(n==="less")y=[0,M*Math.exp(le(o)*p)];else if(n==="greater")y=[M*Math.exp(le(o,{lowerTail:!1})*p),1/0];else{let x=(1-o)/2,E=x<1e-8?le(x,{lowerTail:!1}):le((1+o)/2);y=[M*Math.exp(-E*p),M*Math.exp(E*p)]}return{method:`Mantel-Haenszel chi-squared test ${b!==0?"with":"without"} continuity correction`,alternative:n,statistic:N,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:v,confInt:y,confLevel:o,estimate:M,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var Dg=2**24;function Pg(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>Dg)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new aa(e),s=e.reduce((a,l)=>a+l[0][0],0),i;if(t==="two.sided"){let a=o.density(1),l=a[s-o.lo]*(1+1e-7),f=[0,0];for(let m of a)m<=l&&(f=he(f,[m,0]));i=Math.min(1,f[0])}else i=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,Yi(o,s,1-n)];else if(t==="greater")c=[Gi(o,s,1-n),1/0];else{let a=(1-n)/2;c=[Gi(o,s,a),Yi(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:_m(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function Vg(e,t,n,r){let{diff:o,V:s}=Cm(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=Fm(e);let f=Om(e,i);({diff:o,V:s}=Cm(f,f.map(Xi),n,r,P(1,-i)))}let c=o.length,a=nt(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*a[f];return l=P(l,i),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:yt(l,c/2,2,!1,!1),warnings:[]}}var $g=2**-1022;function Cm(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(d=>d.reduce((w,g)=>w+g,0)),u=Array.from({length:r},(d,w)=>a.reduce((g,b)=>g+b[w],0)),h=1/(f*f*(f-o));for(let d=0;d<r-1;d++)for(let w=0;w<n-1;w++){let g=w+(n-1)*d,b=m[w]*u[d]/f;i[g]+=a[w][d]-(Number.isFinite(b)?b:Es(m[w],u[d],f));for(let N=0;N<r-1;N++)for(let v=0;v<n-1;v++){let M=(w===v?f*m[w]:0)-m[w]*m[v],p=(d===N?f*u[d]:0)-u[d]*u[N],y=h*M*p;c[g][v+(n-1)*N]+=Number.isFinite(y)&&h>=$g?y:m[w]*((w===v?1:0)-m[v]/f)*(((d===N?1:0)-u[N]/f)*(u[d]/(f-o)))}}}),{diff:i,V:c}}var Dm=new Map,zg=2**24,Wg=2**32;function Vm(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>zg||n*r>Wg)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function jg(e,t){let n=`${e},${t}`,r=Dm.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,d=s[u],w=Array.from({length:e+1},()=>new Float64Array(i+1));for(let g=0;g<=e;g++){let b=c[g],N=e-g;if(N>h)continue;let v=N/h;for(let M=0;M<=i;M++){let p=b[M];p!==0&&(N>0&&(w[g+1][M+d]+=p*v),N<h&&(w[g][M]+=p*(1-v)))}}c=w}let a=c[e],l=0;for(;l<a.length&&a[l]===0;)l++;let f=a.length-1;for(;f>l&&a[f]===0;)f--;let m={min:l,probs:a.slice(l,f+1)};return Dm.set(n,m),m}function Qi(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}=jg(t,n),i=dl(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 Pm=new Map;function Kg(e,t){let n=`${e},${t}`,r=Pm.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 d=0;d<=e;d++){if(e-d>o-m)continue;let w=i[d];for(let g=0;g<=s;g++){let b=w[g];b!==0&&(d<e&&(h[d+1][g+u]+=b),e-d<o-m&&(h[d][g]+=b))}}i=h}let c=i[e],a=0;for(;c[a]===0;)a++;let l=c.length-1;for(;c[l]===0;)l--;let f={min:a,counts:c.slice(a,l+1)};return Pm.set(n,f),f}function ia(e,t,n){let{min:r,counts:o}=Kg(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=Ml(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 Bg=["alternative","exact","confInt","confLevel"];function jm(e,t,n={}){if(K(n,Bg,"ansariTest"),typeof e=="string"){let p=er(e,t);return jm(p.x,p.y,n)}let r=Array.from(e).filter(p=>!Number.isNaN(p)),o=Array.from(t).filter(p=>!Number.isNaN(p)),s=r.length,i=o.length;if(s<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let c=Ze(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&Lt(l);let f=s+i,m=Nt(r.concat(o)),u=m.map(p=>Math.min(p,f-p+1)),h=0;for(let p=0;p<s;p++)h+=u[p];let d=new Set(m).size<f,w=n.exact??(s<50&&i<50&&!d),g=n.exact??(s<50&&i<50),b=new mt,N,v;if(w&&!d){Vm(s,i);let p=x=>Qi(x-1,s,i,!1),y=x=>Qi(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?p(h):y(h)))}else c==="less"?N=p(h):N=y(h);a&&(v=Gg(r,o,c,l,b))}else{let p=Km(h,u,d,s,i);N=c==="two.sided"?2*Math.min(se(p),se(p,{lowerTail:!1})):c==="less"?se(p,{lowerTail:!1}):se(p),a&&(v=Xg(r,o,c,l,g?new mt:b)),g&&d&&(b.add("cannot compute exact p-value with ties"),a&&b.add("cannot compute exact confidence interval with ties"))}let M={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:N,warnings:b.list};return v!==void 0&&(M.confInt=v.confInt,M.confLevel=l,M.estimate=v.estimate,M.estimateNames="ratio of scales"),M.nullValue=1,M.nullValueName="ratio of scales",X("ansariTest",M)}function Km(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 Bm(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)<Ug))?Hg(e.map(f=>$m(f,n)).concat(t.map(f=>$m(f,1)))):Yg(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 Ug=2**-1022;function $m(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]=zm(Math.abs(e)),[s,i]=zm(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function zm(e){let t=Math.floor(Math.log2(e)),n=P(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Wm(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 Hg(e){let t=e.map((r,o)=>o).sort((r,o)=>Wm(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Wm(e[t[r]],e[t[o]])===0;)o++;for(let s=r;s<o;s++)n[t[s]]=(r+1+o)/2;r=o}return n}function Yg(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return Nt(e);let n=Nt(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function ca(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Gg(e,t,n,r,o){let s=e.length,i=t.length,c=[];for(let b of e)for(let N of t)b/N>=0&&c.push(b/N);c.sort((b,N)=>b-N);let a=c.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:ca(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((b,N)=>Bm(e,t,b+(N<a-1?(c[N+1]-b)/2:b*1.01)).ab),f=(b,N,v)=>{let M=1/0;for(let y of l)(N?y-b>0:y-b>=0)&&y-b<M&&(M=y-b);let p=v?-1/0:1/0;return l.forEach((y,x)=>{y-b===M&&(p=v?Math.max(p,c[x]):Math.min(p,c[x]))}),p},m=b=>{let N=ia(b/2,s,i),v=ia(1-b/2,s,i);if(!l.some(y=>y-N>=0)||!l.some(y=>y-v>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let M=f(N,!1,!1),p=f(v,!0,!0);return M>p?[f(v,!1,!1),f(N,!0,!0)]:[M,p]},u=ca(n,1-r,m),h=ia(.5,s,i),d=-1/0,w=1/0;l.forEach((b,N)=>{b-h<=0?d=Math.max(d,c[N]):w=Math.min(w,c[N])});let g=d===-1/0||w===1/0?NaN:(w+d)/2;return g===1/0&&(g=w/2+d/2),{confInt:u,estimate:g}}function Xg(e,t,n,r,o){let s=e.length,i=t.length,c=(b,N)=>{let{ab:v,a:M,ties:p}=Bm(e,t,b);return Km(v,M,p,s,i)-N},a=[],l=(b,N)=>{b.length===0||N.length===0||a.push(ln(b)/gt(N),gt(b)/ln(N))};if(l(e.filter(b=>b>0),t.filter(b=>b>0)),l(e.filter(b=>b<=0),t.filter(b=>b<0)),a.some(b=>!Number.isFinite(b))){o.add("cannot compute asymptotic confidence set or estimator");return}if(a.length===0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),{confInt:ca(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=ln(a),m=gt(a),u=b=>Kt(N=>c(N,b),f,m,{tol:1e-4}),h=b=>{let N=le(b/2),v=le(b/2,{lowerTail:!1});if(c(f,N)>0||c(m,v)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let M=u(N),p=u(v);return M<=p?[M,p]:[p,M]},d=ca(n,1-r,h),w=c(f,0)>0||c(m,0)<0;w&&o.add("cannot compute estimate, returning NA");let g=w?NaN:u(0);return{confInt:d,estimate:g}}var Qg=["alternative","confLevel"];function Zg(e,t,n={}){K(n,Qg,"brunnerMunzelTest");let r=Array.from(e).filter(E=>!Number.isNaN(E)),o=Array.from(t).filter(E=>!Number.isNaN(E)),s=r.length,i=o.length;if(s<2)throw new RangeError("not enough 'x' observations");if(i<2)throw new RangeError("not enough 'y' observations");let c=Ze(n.alternative),a=n.confLevel??.95;fn(a);let l=Nt(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=E=>E.reduce((R,A)=>R+A,0)/E.length,h=u(f),d=u(m),w=(E,R,A)=>{let S=(E.length+1)/2,T=0;for(let L=0;L<E.length;L++)T+=(E[L]-R[L]-A+S)**2;return T/(E.length-1)},g=w(f,Nt(r),h),b=w(m,Nt(o),d),N=(d-(i+1)/2)/s,v=s*g+i*b,M=s*i*(d-h)/(s+i)/Math.sqrt(v),p=v**2/((s*g)**2/(s-1)+(i*b)**2/(i-1)),y;c==="greater"?y=ft(M,p,!0,!1):c==="less"?y=ft(M,p,!1,!1):y=2*ft(-Math.abs(M),p,!0,!1);let x=bt((1-a)/2,p,!1,!1)*Math.sqrt(g/(s*i**2)+b/(i*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:M,statisticName:"Brunner-Munzel Test Statistic",parameter:p,parameterName:"df",pValue:y,confInt:[N-x,N+x],confLevel:a,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var Jg=[1.2337005501361697,7831619385924639e-32];function ew(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let d=0;d<t;d++){let w=f[h*t+d];if(w!==0)for(let g=0;g<t;g++)u[h*t+g]+=w*m[d*t+g]}return u},o=f=>{let m=0;for(let d of f)m=Math.max(m,Math.abs(d));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 d=0;d<f.length;d++)f[d]*=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 tw=2**33;function Hm(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>tw)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 Ym(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]=en(e,t),u=2*f-1+2*m,h=1,d=0;for(let w=1;w<=e;w++)h*=w/e*u,h<2**-500&&(h*=2**500,d-=500);for(;d<-1e3;)h*=2**-1e3,d+=1e3;return h*2**d}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 d=2;d<=u;d++)h*=d;s[f*r+m]/=h}}let[i,c]=ew(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 Zi(e,t=!0){if(Number.isNaN(e))return NaN;if(e<=0)return t?0:1;if(e===1/0)return t?1:0;if(e<1){let o=at(Jg,en(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 Ji(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]=en(e,t),o=Math.floor(e-n-r),s=0;for(let i=0;i<=o;i++){let c=he(Ot(e-i,-n),[-r,0]);if(c[0]<=0)break;let a=he(Ot(i,n),[r,0]),l=at(a,[e,0]),f=at(c,[e,0]);s+=pl(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var Um=2**28;function Gm(e,t){let[n,r]=en(e,t),o=Math.floor(n);o===n&&r<0&&(o-=1);let s=[];for(let h=0;h<=e;h++){let d=(h-n-r)/e;d>0&&d<1&&s.push({t:d,up:!0,i:h});let w=(h+n+r)/e;w>0&&w<1&&s.push({t:w,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>Um)return NaN;s.sort((h,d)=>h.t-d.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 d=(h.t-f)/(1-f);if(d>0){let w=d/(1-d);a.fill(0);for(let g=l;g<i;g++){let b=c[g];if(b===0)continue;let N=e-g,v=Math.min(N,Math.floor((N+1)*d)),M=i-g,p=dn(v,N,d,!1);for(let y=v;y<=N&&p!==0;y++)y>v&&(p=p*(N-y+1)*w/y),y>=M?m+=b*p:a[g+y]+=b*p,u++;p=v>0?dn(v-1,N,d,!1):0;for(let y=v-1;y>=0&&p!==0;y--)y<v-1&&(p=p*(y+1)/((N-y)*w)),y>=M?m+=b*p:a[g+y]+=b*p,u++}if(u>2*Um)return NaN;[c,a]=[a,c]}if(f=h.t,h.up)i=h.i+1;else{for(let w=l;w<=h.i;w++)m+=c[w],c[w]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function Xm(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,d)=>h-d);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 d=u*n-h*t;return(o==="two.sided"?Math.abs(d):o==="greater"?d:-d)>=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 d=u+h,w=(u>0?u/d*l[h]:0)+(h>0?h/d*f[h-1]:0);(i===void 0?d<s:i[d])&&a(u,h)?(w>0&&(m+=w*Nn(u,t,n,d,!1)),f[h]=0):f[h]=w}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var nw=["alternative","exact"];function Qm(e,t){return e==="two.sided"?"two-sided":`the CDF of x lies ${e==="less"?"below":"above"} ${t?"the null hypothesis":"that of y"}`}function Zm(e,t,n={}){if(K(n,nw,"ksTest"),typeof e=="string"){let N=er(e,t);return Zm(N.x,N.y,n)}let r=Array.from(e).filter(N=>!Number.isNaN(N)).sort((N,v)=>N-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=Ze(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,v=-1/0;for(let E=0;E<o;E++){let R=t(r[E]);if(!(R>=0&&R<=1))throw new RangeError(`the distribution function returned ${R} at ${r[E]}, not a value in [0, 1]`);N=Math.max(N,(E+1)/o-R),v=Math.max(v,R-E/o)}let M=s==="two.sided"?Math.max(N,v):s==="greater"?N:v,p=new Set(r).size<o,y=n.exact??(o<100&&!p),x;if(y&&s==="two.sided"&&M<.5&&Hm(o,M),s==="two.sided")if(!y)x=Zi(Math.sqrt(o)*M,!1);else{let E=2*Ji(o,M);if(M>=.5)x=Math.min(1,E);else{let R=E<.01?Gm(o,M):NaN;x=Number.isNaN(R)?1-Ym(o,M):R}}else x=y?Ji(o,M):Math.exp(-2*o*M*M);return X("ksTest",{method:`${y?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Qm(s,!0),exact:y,statistic:M,statisticName:i,pValue:Math.min(1,Math.max(0,x)),warnings:p?["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 d=s==="two.sided"?Math.max(u,h):s==="greater"?u:h,w=new Set(l.map(N=>N[0])).size<l.length,g=n.exact??o*a<1e4,b;if(g)b=Xm(d,o,a,w?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*a/(o+a);b=s==="two.sided"?Zi(Math.sqrt(N)*d,!1):Math.exp(-2*N*d*d)}return X("ksTest",{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Qm(s,!1),exact:g,statistic:d,statisticName:i,pValue:Math.min(1,Math.max(0,b)),warnings:w&&!g?["p-value will be approximate in the presence of ties"]:[]})}var rw=["poolSd","paired","pAdjustMethod","alternative"],ow=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],sw=["pAdjustMethod","correct"];function Jm(e,t,n){let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=new Map,s=vn(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(Ar);return{levels:i,groups:i.map(c=>o.get(c))}}function ec(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=Jr(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 tc(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(ut),colNames:t.slice(0,-1).map(ut),pAdjustMethod:r,warnings:o}}function aw(e,t,n={}){K(n,rw,"pairwiseTTest");let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let s=Ze(n.alternative),i=n.pAdjustMethod??"holm",{levels:c,groups:a}=Jm(e,t,!r),l=c.length;if(l<2)throw new RangeError("there must be at least two groups");let f,m;if(o){m="t tests with pooled SD";let u=a.map(b=>b.length),h=Dt(a),d=h.mean,w=0;for(let b of u)b>1&&(w+=b-1);Ho(w,h.ssWithin);let g=Math.sqrt(h.ssWithin/w);f=(b,N)=>{let v=(d[b]-d[N])/(g*Math.sqrt(1/u[b]+1/u[N]));return s==="less"?ft(v,w,!0,!1):s==="greater"?ft(v,w,!1,!1):2*ft(-Math.abs(v),w,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>rt(()=>En(a[u],a[h],{paired:r,alternative:s})).pValue;return tc(m,c,ec(l,f,i),i)}function iw(e,t,n={}){K(n,ow,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=Jm(e,t,!(s.paired??!1)),l=c.length;if(l<2)throw new RangeError("there must be at least two groups");let f="",m=new mt,u=ec(l,(h,d)=>{let w=rt(()=>zo(a[h],a[d],{...s,...o===void 0?{}:{alternative:o}}));f=w.method;for(let g of w.warnings)m.add(g);return w.pValue},i);return X("pairwiseWilcoxTest",tc(f,c,u,i,m.list))}function cw(e,t,n={}){if(K(n,sw,"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=ec(s,(m,u)=>{let h=rt(()=>vi([r[m],r[u]],[o[m],o[u]],{correct:c}));for(let d of h.warnings)a.add(d);return h.pValue},i),f=Array.from({length:s},(m,u)=>u+1);return X("pairwisePropTest",tc("Pairwise comparison of proportions",f,l,i,a.list))}var Yo=[0,0];function la(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=De(e,en(t,t));return Ot(t,n[0]/(2*t))}function fa(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:la([e[r],0]))}function to(e,t){return Kn(e,t,[]).fit}function Kn(e,t,n){let r=t.length,o=Math.min(e.length,r),s=e.map(f=>f.slice()),i=t.slice(),c=n.map(f=>f.slice()),a=[];for(let f=0;f<o;f++){let m=s[f],u=Yo;for(let b=f;b<r;b++)u=he(u,_e(m[b],m[b]));if(u[0]===0){a.push(Yo);continue}let h=la(u),d=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=he(m[f],d);let w=_e(d,m[f]),g=b=>{let N=Yo;for(let M=f;M<r;M++)N=he(N,_e(m[M],b[M]));let v=at(N,w);for(let M=f;M<r;M++)b[M]=De(b[M],_e(v,m[M]))};for(let b=f+1;b<e.length;b++)g(s[b]);g(i);for(let b of c)g(b);a.push([-d[0],-d[1]])}let l=new Array(o).fill(Yo);for(let f=o-1;f>=0;f--){let m=i[f];for(let u=f+1;u<o;u++)m=De(m,_e(s[u][f],l[u]));l[f]=at(m,a[f])}return{fit:{qty:i,coef:l},qtv:c}}function no(e,t=0,n=e.length){let r=Yo;for(let o=t;o<n;o++)r=he(r,_e(e[o],e[o]));return r}var lw=1e-14;function fw(e,t,n,r,o,s,i,c){let a=y=>P(y,2*i),l=t.length,f=n.flatMap((y,x)=>no(y)[0]>lw*s[x]?[x]:[]),m=0,u=[],h=t.slice(),d=new Array(l).fill(0);if(f.length>0){let y=At(Array.from({length:l},(E,R)=>f.map(A=>n[A][R][0])));m=y.rank,u=y.pivot.slice(0,m).map(E=>f[E]);let x=to(u.map(E=>n[E]),t);h=x.qty,u.forEach((E,R)=>{for(let A=0;A<l;A++)d[A]=d[A]+n[E][A][0]*x.coef[R][0]})}let w=[];u.forEach((y,x)=>{let E=r[y],R=w.find(A=>A.term===E);R===void 0&&w.push(R={term:E,df:0,ss:[0,0]}),R.df++,R.ss=he(R.ss,_e(h[x],h[x]))});let g=l-m,b=no(h,m)[0],N=qi(d,b,g),v=w.filter(y=>y.term!==0);g>0&&xr(c,b)&&(b=0,v=v.map(y=>xr(c,y.ss[0])?{...y,ss:[0,0]}:y));let M=v.map(y=>o[y.term-1]),p=y=>{if(g>0){let x=b/g,E=v.map(({df:R,ss:A})=>{let S=A[0]/R/x;return[R,y(A[0]),y(A[0]/R),S,dt(S,R,g,!1,!1)]});return E.push([g,y(b),y(x),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...M,"Residuals"],table:E}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:M,table:v.map(({df:x,ss:E})=>[x,y(E[0]),y(E[0]/x)])}};return[p(a),p(y=>y),N]}function eu(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=ks({response:t.response,rhs:i},Object.keys(o)).variables,a=zn(t,o,{...s,dropUnusedLevels:!0,contrastVariables:c},i),l=a.y;if(l===void 0)throw new TypeError("the formula has no response");if(l.some(q=>!Number.isFinite(q)))throw new RangeError("NA/NaN/Inf in 'y'");let f=zn({response:t.response,rhs:r},o,{...s.levels===void 0?{}:{levels:s.levels},allContrasts:"contr.helmert",intercept:a.terms.intercept,extraVariables:a.terms.variables,dropUnusedLevels:!0}),m=l.length,u=[...a.warnings,...f.warnings.filter(q=>!a.warnings.includes(q))],h=f.colNames.length,d=f.colNames.map((q,F)=>Se([f.x.map(_=>_[F])]).groups[0]),w=f.x.map((q,F)=>d.map(_=>_[F])),g=h>0?At(w):void 0,b=g?.rank??0;b<h&&u.push("Error() model is singular");let N=g?.pivot.slice(0,b)??[],v=q=>q.map(F=>[F,0]),M=a.colNames.map((q,F)=>v(Se([a.x.map(_=>_[F])]).groups[0])),{groups:p,k:y}=Se([l]),{fit:x,qtv:E}=Kn(N.map(q=>v(d[q])),v(p[0]),M),R=p[0].map(q=>[1,q]),A=M.map(q=>no(q)[0]),S=[];N.forEach((q,F)=>{let _=f.assign[q],O=_===0?"(Intercept)":f.terms.termLabels[_-1],I=S.find(D=>D.name===O);I===void 0&&S.push(I={name:O,idx:[]}),I.idx.push(F)}),m>b&&S.push({name:"Within",idx:Array.from({length:m-b},(q,F)=>b+F)});let T=S.map(q=>fw(q.name,q.idx.map(F=>x.qty[F]),E.map(F=>q.idx.map(_=>F[_])),a.assign,a.terms.termLabels,A,y,R)),L=T.map(q=>q[0]),C={formula:e,multistratum:!0,strata:L,rows:a.frame.rows,warnings:u};return Bt.set(C,T.map(q=>q[1])),T.some((q,F)=>q[2]&&S[F].name!=="(Intercept)")&&Ii.set(C,[ki]),C}function qr(e){let t=e.length,n=new Array(t).fill(0);for(let o=0;o<t;o++){let s=0;for(let i=0;i<=o;i++)s=Math.max(s,Math.abs(e[i][o]));s>0&&Number.isFinite(s)&&(s<2**-400||s>2**400)&&(n[o]=Math.floor(Math.log2(s)))}let r=n.every(o=>o===0)?e.map(o=>o.slice()):e.map(o=>o.map((s,i)=>P(s,-n[i])));return{inv:Ul(r),e:n}}var mw=["weights","contrasts","levels","intercept"],uw=["level"],hw=["interval","level"],nc=2**-1022;function rc(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=ou(t.x,r,t.frame.weights,t.terms.intercept,!0),s=o.result,i=[...t.warnings];if(!n){let a=o.yScale??0,l=o.fitRows.map(h=>P(s.fittedValues[h],-a)),f=l.reduce((h,d)=>h+d,0)/l.length,m=l.length>1?l.reduce((h,d)=>h+(d-f)**2,0)/(l.length-1):NaN,u=(o.rssScaled??o.rss)/s.dfResidual;Number.isFinite(u)&&u<1e-30*(f*f+m)&&i.push("essentially perfect fit: summary may be unreliable")}let c={...s,formula:e,response:t.terms.variables[0],coefNames:t.colNames,assign:t.assign,termLabels:t.terms.termLabels,order:t.terms.order,variables:t.terms.variables,factors:t.terms.factors,rows:t.frame.rows,x:t.x,y:r,effects:o.effects,rss:o.rss,nObs:o.fitRows.length,aov:n,warnings:i};return t.frame.weights!==void 0&&(c.weights=t.frame.weights),c}function Go(e,t,n={}){if(K(n,mw,"lm"),typeof e=="string"){let r=zn(Jn(e),t,{...n,dropUnusedLevels:!0});return X("lm",Xr(rc(e,r,!1),Hl(t,r.terms.variables)))}return pw(e,t,n)}function pw(e,t,n){let r=n.intercept??!0,o=e.length>0&&typeof e[0]!="number"?e.map(u=>Array.from(u)):Array.from(e,u=>[u]),s=Array.from(t);if(o.some(u=>u.length!==o[0].length))throw new TypeError("all rows of the matrix must have the same length");if(o.length!==s.length)throw new RangeError("variable lengths differ (found for 'x')");let i=n.weights===void 0?void 0:Array.from(n.weights);if(i!==void 0&&i.length!==s.length)throw new RangeError("variable lengths differ (found for '(weights)')");let c=s.map((u,h)=>!Number.isNaN(u)&&o[h].every(d=>!Number.isNaN(d))&&(i===void 0||!Number.isNaN(i[h]))),a=i?.filter((u,h)=>c[h]);if(a?.some(u=>u<0))throw new RangeError("missing or negative weights not allowed");let l=o.filter((u,h)=>c[h]).map(u=>r?[1,...u]:u),f=s.filter((u,h)=>c[h]),m=ou(l,f,a,r).result;return m.rows=c.flatMap((u,h)=>u?[h]:[]),Xr(m,s.length)}function dw(e,t,n,r,o,s,i){let c=s.rank,a=s.pivot.slice(0,c),l=n?.map(p=>P(p,-r)),f=fa(l===void 0?void 0:o.map(p=>l[p]),o.length).map((p,y)=>{if(n===void 0||l===void 0)return p;let x=n[o[y]];if(x===0||l[o[y]]>=nc)return p;let E=la([x,0]);return[P(E[0],-r/2),P(E[1],-r/2)]}),m=a.map(p=>{let y=o.map((E,R)=>Tt(f[R],e[E][p])),x=Kl(y.map(E=>E[0]));return{c:x===0?y:y.map(E=>[P(E[0],-x),P(E[1],-x)]),e:x}}),{qty:u,coef:h}=to(m.map(({c:p})=>p),o.map((p,y)=>Tt(f[y],t[p]))),d=h.map((p,y)=>m[y].e===0?p:[P(p[0],-m[y].e),P(p[1],-m[y].e)]),w=e[0]?.map(()=>NaN)??[];a.forEach((p,y)=>w[p]=d[y][0]);let g=e.map(p=>{let y=[0,0];return a.forEach((x,E)=>y=he(y,Tt(d[E],p[x]))),y}),b=no(u,c)[0],N=o.map(p=>l?.[p]??1),v=[0,0];if(i){let p=[0,0],y=[0,0];o.forEach((x,E)=>{p=he(p,[N[E],0]),y=he(y,Tt(g[x],N[E]))}),v=at(y,p)}let M=[0,0];return o.forEach((p,y)=>{let x=De(g[p],v);M=he(M,Tt(_e(x,x),N[y]))}),{coefficients:w,fittedValues:g.map(p=>p[0]),residuals:g.map((p,y)=>De([t[y],0],p)[0]),effects:u.map(p=>p[0]),rss:b,mss:M[0]}}function ou(e,t,n,r,o=!1){let s=Se([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return tu(nu(e,t,n,r,o),e,t,n);let i=nu(e,t.map(l=>P(l,-s)),n,r,o),c=i.result,a=l=>P(l,s);return c.coefficients=c.coefficients.map(a),c.residuals=c.residuals.map(a),c.fittedValues=c.fittedValues.map(a),c.sigma=a(c.sigma),c.coefTable.estimate=c.coefTable.estimate.map(a),c.coefTable.stdError=c.coefTable.stdError.map(a),tu({...i,effects:i.effects.map(a),rss:P(i.rss,2*s),yScale:s,rssScaled:i.rss},e,t,n)}function tu(e,t,n,r){if(r===void 0)return e;let o=e.result,s=o.coefficients.flatMap((i,c)=>Number.isNaN(i)?[]:[c]);return r.forEach((i,c)=>{if(i!==0)return;let a=!1;if(!Number.isFinite(o.fittedValues[c])){let l=t[c],f=s.reduce((m,u)=>m+l[u]*o.coefficients[u],0);o.fittedValues[c]=To(f,0)?vr(s.map(m=>l[m]),s.map(m=>o.coefficients[m]),f):f,a=!0}(a||!Number.isFinite(o.residuals[c]))&&(o.residuals[c]=n[c]-o.fittedValues[c])}),e}var oc=new WeakMap;function sc(e){return oc.get(e)??0}function gw(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function nu(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map(($,Y)=>n===void 0||n[Y]!==0),i=e.filter(($,Y)=>s[Y]),c=t.filter(($,Y)=>s[Y]),a=n?.filter(($,Y)=>s[Y]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(i.some($=>$.some(Y=>!Number.isFinite(Y))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some($=>!Number.isFinite($)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some($=>!Number.isFinite($)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap(($,Y)=>s[Y]?[Y]:[]),m=a===void 0?1:gt(a),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,h=0,d=a?.map(Math.sqrt),w=d===void 0?i:i.map(($,Y)=>$.map(W=>W*d[Y])),g=At(w);a!==void 0&&Number.isFinite(m)&&u+2>0&&!gw(g)&&(h=u+2,d=a.map($=>P(Math.sqrt($),-h/2)),w=i.map(($,Y)=>$.map(W=>W*d[Y])),g=At(w),oc.set(g,h));let b=d===void 0?c:c.map(($,Y)=>$*d[Y]),N=g.rank,v=$=>P($,-u),M=o?dw(e,t,n,u,f,g,r):void 0,p=M?.coefficients??Hr(g,b),y=M?.residuals??[],x=M?.fittedValues??[],E=l-N,R=a?.map(v)??new Array(l).fill(1),A=M?.rss??0;if(M===void 0){let $=Yr(g,b),Y=d===void 0?$:$.map((U,j)=>U/d[j]),W=0;t.forEach((U,j)=>{if(s[j])y.push(Y[W]),x.push(U-Y[W]),W++;else{let te=e[j].reduce((fe,Re,Ce)=>Number.isNaN(p[Ce])?fe:fe+Re*p[Ce],0);x.push(te),y.push(U-te)}});for(let U=0;U<l;U++)A+=R[U]*Y[U]**2}let S=x.filter(($,Y)=>s[Y]),T=0;if(a!==void 0&&a.some($=>$!==0&&P($,-u)<nc)){let $=f.flatMap((Y,W)=>{let U=y[Y];return U===0?[]:[{w:a[W],r:U,a:Math.floor(Math.log2(Math.abs(U)))}]});if($.length>0&&$.every(Y=>Number.isFinite(Y.r))){let Y=Math.max(...$.map(j=>Math.log2(j.w)+2*j.a)),W=2*Math.floor(Math.floor(Y)/2),U=0;for(let j of $)U+=P(j.w,2*j.a-W)*P(j.r,-j.a)**2;A=U,T=W-u}}let L=A/E,C=P(Math.sqrt(L),(u+T)/2),q=h===0?0:(h-u)/2,F=g.qr.slice(0,N).map($=>$.slice(0,N).map(Y=>h===0?P(Y,-u/2):Y)),{inv:_,e:O}=N>0?qr(F):{inv:[],e:[]},D=g.pivot.slice(0,N).map(($,Y)=>[$,Y]).sort(($,Y)=>$[0]-Y[0]),k=D.map(([$])=>p[$]),V=D.map(([,$])=>P(Math.sqrt(_[$][$]*L),T/2-O[$]-q)),B=k.map(($,Y)=>$/V[Y]),Q=B.map($=>2*ft(-Math.abs($),E,!0,!1)),re={coefficients:p,residuals:y,fittedValues:x,rank:N,dfResidual:E,coefTable:{estimate:k,stdError:V,tValue:B,pValue:Q},sigma:C,rSquared:0,adjRSquared:0,intercept:r,qr:g,x:e,rows:e.map(($,Y)=>Y)};n!==void 0&&(re.weights=n);let z=r?1:0,J=N-z,Z={result:re,fitRows:f,effects:(M?.effects??[]).map($=>P($,u/2)),rss:P(A,u+T)};if(J===0)return Z;let ke=0;if(M!==void 0)ke=M.mss;else if(r){let $=R.reduce((W,U)=>W+U,0),Y=0;for(let W=0;W<l;W++)Y+=R[W]*S[W]/$;for(let W=0;W<l;W++)ke+=R[W]*(S[W]-Y)**2}else for(let $=0;$<l;$++)ke+=R[$]*S[$]**2;re.rSquared=ke/(ke+P(A,T)),re.adjRSquared=1-(1-re.rSquared)*((l-z)/E);let ne=P(ke/J/L,-T);return re.fStatistic={value:ne,numdf:J,dendf:E},re.fPValue=dt(ne,J,E,!1,!1),Z}function su(e,t){let n=(1-e)/2;return n<1e-8?bt(n,t,!1,!1):bt((1+e)/2,t,!0,!1)}function ww(e,t={}){K(t,uw,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=su(n,e.dfResidual),{estimate:o,stdError:s}=e.coefTable,i=e.coefficients.map(()=>NaN),c=e.coefficients.map(()=>NaN),a=0;return e.coefficients.forEach((l,f)=>{Number.isNaN(l)||(i[f]=o[a]-r*s[a],c[f]=o[a]+r*s[a],a++)}),{lower:i,upper:c}}function ru(e,t,n){if(t===0||!Number.isFinite(t))return e*t;let r=Math.floor(Math.log2(e)),o=Math.floor(Math.log2(Math.abs(t)));return P(P(e,-r)*P(t,-o),r+o+n)}function bw(e,t,n={}){K(n,hw,"predict");let o=(t.length>0&&typeof t[0]!="number"?t.map(v=>Array.from(v)):Array.from(t,v=>[v])).map(v=>e.intercept?[1,...v]:v);if(o.some(v=>v.length!==e.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let s=e.rank,i=e.qr.pivot.slice(0,s),c=e.qr.qr.slice(0,s).map(v=>v.slice(0,s)),a=-(oc.get(e.qr)??0)/2,l=e.coefficients.flatMap((v,M)=>Number.isNaN(v)?[]:[M]),f=o.map(v=>{let M=v.reduce((p,y,x)=>Number.isNaN(e.coefficients[x])?p:p+y*e.coefficients[x],0);return To(M,0)?vr(l.map(p=>v[p]),l.map(p=>e.coefficients[p]),M):M}),m=v=>{let M=new Array(s).fill(0);for(let p=0;p<s;p++){let y=v[p];for(let x=0;x<p;x++)y-=c[x][p]*M[x];M[p]=y/c[p][p]}return M},u=o.map(v=>{let M=i.map(x=>v[x]),p=Wa(m(M));if(p>=nc&&p<1/0||!M.every(Number.isFinite)||M.every(x=>x===0))return a===0||!(p>0&&p<1/0)?p*e.sigma:ru(p,e.sigma,a);let y=Math.floor(Math.log2(gt(M.map(Math.abs))));for(let x of[0,600]){let E=Wa(m(M.map(R=>P(R,-(y+x)))));if(E<1/0)return E===0?E*e.sigma:ru(E,e.sigma,y+x+a)}return p*e.sigma}),h=n.interval??"none";if(h!=="none"&&h!=="confidence"&&h!=="prediction")throw new RangeError(`invalid 'interval': ${String(h)}`);let d=Ls(e.qr,e.rank,o),w=d.length>0?{nonEstimable:d,warnings:[_s]}:{};if(h==="none")return X("predict",{fit:f,seFit:u,...w},e);let g=n.level??.95;if(!(g>0&&g<1))throw new RangeError("'level' must be between 0 and 1");let b=su(g,e.dfResidual),N=u.map(v=>b*(h==="prediction"?Math.hypot(v,e.sigma):v));return X("predict",{fit:f,seFit:u,lwr:f.map((v,M)=>v-N[M]),upr:f.map((v,M)=>v+N[M]),...w},e)}var vw=["weights","contrasts","levels"],au=["confLevel"],iu=["sigmaX","sigmaY","mu","alternative","confLevel"];function yw(e,t,n={}){return K(n,vw,"aov"),typeof e=="string"?X("aov",cu(e,t,n)):X("aov",Nw(e,t))}function cu(e,t,n){let r=Jn(e,!0),{treatment:o,errors:s}=Fl(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?eu(e,r,o,s[0],t,n):rc(e,zn(r,t,{...n,dropUnusedLevels:!0},o),!0)}function Nw(e,t){let{labels:n,groups:r}=or(e,t);if(r.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=r.flat(),s=r.flatMap((i,c)=>i.map(()=>n[c]));return cu("y ~ g",{y:o,g:s},{levels:{g:n}})}function lu(e){let t=e.length;if(t<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let n=e.reduce((u,h)=>u+h.length,0),{ssBetween:r,ssWithin:o,k:s}=Dt(e),i=t-1,c=n-t,a=r/i,l=o/c,f=a/l,m=u=>P(u,2*s);return{df:[i,c],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:c>0?dt(f,i,c,!1,!1):NaN}}function Mw(e,t,n={}){vt(t)&&K(t,au,"TukeyHSD"),K(n,au,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=or(e,r),i=s.length;if(i<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let c=n.confLevel??.95;fn(c);let l=s.reduce((b,N)=>b+N.length,0)-i;if(l===0)throw new RangeError("no residual degrees of freedom");if(l<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let{ssWithin:f,mean:m,k:u}=Dt(s),h=f/l,d=Sl(c,i,l),w=[],g={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:w,pAdjusted:w,warnings:[]};for(let b=0;b<i-1;b++)for(let N=b+1;N<i;N++){let v=m[N]-m[b],M=Math.sqrt(h/2*(1/s[N].length+1/s[b].length));g.names.push(`${o[N]}-${o[b]}`),g.i.push(N+1),g.j.push(b+1),g.diff.push(P(v,u)),g.lwr.push(P(v-d*M,u)),g.upr.push(P(v+d*M,u)),g.pAdj.push(Rs(Math.abs(v)/M,i,l,{lowerTail:!1}))}return g}function Ew(e,t,n={}){vt(t)&&K(t,iu,"zTest"),K(n,iu,"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 A of[o,s])if(A!==void 0&&!(A>0&&A<1/0))throw new RangeError("sigmaX and sigmaY must be positive finite numbers");if(Number.isNaN(i))throw new RangeError("mu must be a number");let c=Ze(n.alternative),a=n.confLevel??.95;if(!(a>=0&&a<=1))throw new RangeError("confLevel must be between 0 and 1");let l=Array.from(e).filter(A=>!Number.isNaN(A)),f=r===void 0?void 0:Array.from(r).filter(A=>!Number.isNaN(A));if(l.length<3)throw new RangeError("x needs at least 3 non-missing values");if(f!==void 0&&f.length<3)throw new RangeError("y needs at least 3 non-missing values");let m=Se([l,f??[],[o,s??0]]),u=m.k,[h,d,[w,g]]=m.groups,b=Xn(h)[0],N=f===void 0?NaN:Xn(d)[0],v=f===void 0?w/Math.sqrt(l.length):Math.sqrt(w**2/l.length+g**2/f.length),M=f===void 0?b:b-N,p=(M-P(i,-u))/v,y,x;if(c==="less")y=se(p),x=[-1/0,M+le(a)*v];else if(c==="greater")y=se(p,{lowerTail:!1}),x=[M-le(a)*v,1/0];else{y=2*se(-Math.abs(p));let A=(1-a)/2,S=(A<1e-8?le(A,{lowerTail:!1}):le(1-(1-a)/2))*v;x=[M-S,M+S]}x=[P(x[0],u),P(x[1],u)];let E=f!==void 0,R=(A,S)=>{if(!(u>0&&A.some((L,C)=>L!==0&&Math.abs(S[C])<2**-1022)))return P(Xn(S)[0],u);let T=Math.max(Se([A]).k,0);return P(Xn(T===0?A:A.map(L=>P(L,-T)))[0],T)};return{method:E?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:p,statisticName:"z",pValue:y,confInt:x,confLevel:a,estimate:E?[R(l,h),R(f,d)]:R(l,h),estimateNames:E?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:E?"difference in means":"mean",warnings:[]}}var xw=["family","link","weights","offset","start","epsilon","maxit","intercept"],Rw=["family","link","variance"],fu=["power"],Aw=["dispersion"],Sw=["type"],Tw=["type","dispersion"],kw=["dispersion"],qw=["newdata","newoffset","type","dispersion"],ro=2**-52;function Iw(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function Xo(e,t,n){return e.map((r,o)=>{let s=0;for(let i=0;i<r.length;i++)s+=r[i]*t[i];return s+n[o]})}function sr(e,t,n,r){let o=0;for(let s=0;s<t.length;s++)r[s]!==0&&(o+=e.devContribution(t[s],n[s],r[s]));return o}var hu=2**-1022,ma=e=>e===0||Math.abs(e)>=hu&&Math.abs(e)<1/0;function mu(e,t,n,r){let o=e.variance(r),s=n**2,i=Math.sqrt(t*s/o);if(i>0&&i<1/0&&ma(s)&&s!==0&&ma(o)&&o!==0&&ma(t*s))return i;if(e.muPower!==void 0&&e.varPower!==void 0&&r>0){let{c,a}=e.muPower;return Math.sqrt(t)*Math.abs(c)*r**((2*a-e.varPower)/2)}return i}function ac(e,t,n,r){let o=(t-n)/r;if(Math.abs(r)>=hu&&Math.abs(r)<1/0)return o;if(e.muPower!==void 0&&n>0){let{c:s,a:i}=e.muPower,c=n**(i/2);return(t-n)/s/c/c}return o}function _w(e,t,n,r){let o=e.variance(n),s=(t-n)*Math.sqrt(r)/Math.sqrt(o);if(Number.isFinite(s)&&ma(o)&&o!==0||e.varPower===void 0||!(n>0))return s;let i=n**(e.varPower/4);return(t-n)/i*Math.sqrt(r)/i}var Lw=.1;function pu(e,t){let{x:n,p:r,y:o,w:s,offset:i,fam:c,epsilon:a,maxit:l}=e,f=o.length;if(o.some((m,u)=>s[u]>0&&!Number.isFinite(m)))throw new RangeError("NA/NaN/Inf in 'y'");if(n.some((m,u)=>s[u]>0&&m.some(h=>!Number.isFinite(h))))throw new RangeError("NA/NaN/Inf in 'x'");return Io(t,()=>{let m=(L,C)=>L.every(q=>c.etaOk(q))&&C.every(q=>c.muOk(q));if(r===0){let L=i.slice();if(!L.every(_=>c.etaOk(_)))throw new Error("invalid linear predictor values in empty model");let C=L.map(_=>c.linkinv(_));if(!C.every(_=>c.muOk(_)))throw new Error("invalid fitted means in empty model");let q=L.map(_=>c.muEta(_)),F=C.map((_,O)=>mu(c,s[O],q[O],_)**2);return{coefficients:[],eta:L,mu:C,deviance:sr(c,o,C,s),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:s.map(_=>_>0),workingWeights:F,workingResiduals:o.map((_,O)=>ac(c,_,C[O],q[O]))}}let u,h;if(e.start!==void 0){if(e.start.length!==r)throw new RangeError(`length of 'start' should equal ${r} and correspond to initial coefs for ${Iw(e.columnNames)}`);h=e.start.slice(),u=Xo(n,h,i)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(L=>c.link(L));let d=u.map(L=>c.linkinv(L));if(!m(u,d))throw new Error("cannot find valid starting values: please specify some");let w=sr(c,o,d,s),g=w,b=!1,N=!1,v=0,M=new Array(r).fill(0),p,y=s.map(()=>!1),x=[],E=[],R=Math.min(1e-7,a/1e3);for(v=1;v<=l;v++){let L=u.map(D=>c.muEta(D));for(let D=0;D<f;D++){if(!(s[D]>0))continue;let k=c.variance(d[D]);if(Number.isNaN(k))throw new Error("NAs in V(mu)");if(k===0&&!(c.varPower!==void 0&&d[D]>0))throw new Error("0s in V(mu)");if(Number.isNaN(L[D]))throw new Error("NAs in d(mu)/d(eta)")}y=s.map((D,k)=>D>0&&L[k]!==0);let C=[];for(let D=0;D<f;D++)y[D]&&C.push(D);if(C.length===0){t.add(`no observations informative at iteration ${v}`),b=!1;break}x=new Array(f).fill(0);let q=[],F=[];for(let D of C){let k=mu(c,s[D],L[D],d[D]);x[D]=k,q.push((u[D]-i[D]+ac(c,o[D],d[D],L[D]))*k),F.push(n[D].map(V=>V*k))}p=At(F,{tol:R});let _=Hr(p,q);if(_.some((D,k)=>p.pivot.indexOf(k)<p.rank&&!Number.isFinite(D))){t.add(`non-finite coefficients at iteration ${v}`),b=!1;break}M=_.map(D=>Number.isNaN(D)?0:D),N=!1;let O=u;if(u=Xo(n,M,i),d=u.map(D=>c.linkinv(D)),g=sr(c,o,d,s),!Number.isFinite(g)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated due to divergence");let D=0;for(;!Number.isFinite(g);){if(D>=l)throw new Error("inner loop 1; cannot correct step size");D++,M=M.map((k,V)=>(k+h[V])/2),u=Xo(n,M,i),d=u.map(k=>c.linkinv(k)),g=sr(c,o,d,s)}N=!0}if(!m(u,d)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated: out of bounds");let D=0;for(;!m(u,d);){if(D>=l)throw new Error("inner loop 2; cannot correct step size");D++,M=M.map((k,V)=>(k+h[V])/2),u=Xo(n,M,i),d=u.map(k=>c.linkinv(k))}N=!0,g=sr(c,o,d,s)}let I=Math.max(a,1e-10);if(h!==void 0&&v>1&&(g-w)/(.1+Math.abs(g))>=I){t.add("step size truncated due to increasing deviance");let D=0;for(;(g-w)/(.1+Math.abs(g))>=I&&D<l;)D++,M=M.map((k,V)=>(k+h[V])/2),u=Xo(n,M,i),d=u.map(k=>c.linkinv(k)),g=sr(c,o,d,s);if((g-w)/(.1+Math.abs(g))>=I){t.add("inner loop 3; cannot correct step size"),b=!1;break}}if(e.untilCoefficients){let D=gt(M.map(Math.abs));if(h!==void 0&&M.every((k,V)=>Math.abs(k-h[V])<=8*ro*D)){b=!0;break}if(E.some(k=>k.every((V,B)=>V===M[B]))){b=!0;break}E.push(M.slice()),E.length>16&&E.shift()}else if(Math.abs(g-w)/(Math.abs(g)+.1)<a){b=!0;let D=0;for(let k=0;k<f;k++)y[k]&&(D=Math.max(D,Math.abs(u[k]-O[k])));D>Lw&&t.add("glm.fit: the linear predictor was still changing at convergence: the data may be separated (no finite maximum likelihood estimates)");break}w=g,h=M.slice()}v>l&&(v=l);let A=r,S=M.slice();if(p!==void 0){A=p.rank;for(let L=p.rank;L<r;L++)S[p.pivot[L]]=NaN}let T=u.map(L=>c.muEta(L));return{coefficients:S,eta:u,mu:d,deviance:g,iter:v,converged:b,boundary:N,rank:A,qr:p,used:y,workingWeights:x.map(L=>L*L),workingResiduals:o.map((L,C)=>ac(c,L,d[C],T[C]))}})}function du(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function gu(e,t,n,r){r||(e.converged||n.add("glm.fit: algorithm did not converge"),e.boundary&&n.add("glm.fit: algorithm stopped at boundary value"),t.family==="binomial"&&e.mu.some(o=>o>1-10*ro||o<10*ro)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*ro)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function oo(e,t,n,r={}){let o=Qr(e.familySpec),s="start"in r?r.start:e.start,i=Yl(o,e.yGiven,e.priorGiven,s!==void 0||r.etastart!==void 0,n);if(!i.weights.some(f=>f>0))throw new RangeError("all weights are zero");let c=r.extraOffset===void 0?e.offset:e.offset.map((f,m)=>f+r.extraOffset[m]),a=e.x.map(f=>t.map(m=>f[m])),l=pu({x:a,p:t.length,y:i.y,w:i.weights,offset:c,fam:o,epsilon:r.epsilon??e.epsilon,maxit:r.maxit??e.maxit,columnNames:t.map(f=>e.columnNames[f]),start:s,etastart:r.etastart,mustart:i.mustart,untilCoefficients:r.untilCoefficients},n);return gu(l,o,n,t.length===0),{fit:l,fam:o,y:i.y,w:i.weights,size:i.size,offset:c}}function ic(e,t,n={}){K(n,xw,"glm"),K(sn(n,"family"),Rw,"glm","family"),K(sn(n,"family.link"),fu,"glm","family.link"),K(sn(n,"link"),fu,"glm","link");let r=n.intercept??!0,o=du(e);if(o.some(E=>E.length!==o[0].length))throw new TypeError("all rows of x must have the same length");let s=t.length>0&&typeof t[0]!="number",i=s?t.map(E=>Array.from(E)):Array.from(t),c=i.length;if(o.length!==c)throw new RangeError("variable lengths differ (found for 'x1')");let a=n.weights===void 0?void 0:Array.from(n.weights);if(a!==void 0&&a.length!==c)throw new RangeError("variable lengths differ (found for '(weights)')");let l=n.offset===void 0?void 0:Array.from(n.offset);if(l!==void 0&&l.length!==c)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((E,R)=>{let A=i[R];return!(Array.isArray(A)?A.some(T=>Number.isNaN(T)):Number.isNaN(A))&&E.every(T=>!Number.isNaN(T))&&!(a!==void 0&&Number.isNaN(a[R]))&&!(l!==void 0&&Number.isNaN(l[R]))}),m=o.filter((E,R)=>f[R]).map(E=>r?[1,...E]:E),u=i.filter((E,R)=>f[R]),h=u.length;if(h===0)throw new RangeError("0 (non-NA) cases");let d=a===void 0?new Array(h).fill(1):a.filter((E,R)=>f[R]);if(d.some(E=>E<0))throw new RangeError("negative weights not allowed");if(d.some(E=>!Number.isFinite(E)))throw new RangeError("non-finite weights not allowed");let w=l===void 0?new Array(h).fill(0):l.filter((E,R)=>f[R]);if(w.some(E=>!Number.isFinite(E)))throw new RangeError("the offset must be finite");let g=o.length>0?o[0].length:0,b=[...r?["(Intercept)"]:[],...Array.from({length:g},(E,R)=>`x${R+1}`)],N=b.length,v=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(v.link=n.link);let M={x:m,columnNames:b,yGiven:u,priorGiven:d,offset:w,hasOffset:l!==void 0,intercept:r,familySpec:v,epsilon:n.epsilon??1e-8,maxit:Math.floor(n.maxit??25),start:n.start===void 0?void 0:Array.from(n.start),rows:f.flatMap((E,R)=>E?[R]:[])};if(!(M.epsilon>0&&M.epsilon<1/0))throw new RangeError("value of 'epsilon' must be > 0 and finite");if(!(M.maxit>0&&M.maxit<1/0))throw new RangeError("maximum number of iterations must be > 0");let p=!s&&Cw(v)?Se([u]).k:0;if(p===0)return X("glm",Xr(uu(M),c));let y=E=>P(E,-p),x=uu({...M,yGiven:u.map(y),offset:w.map(y),start:M.start?.map(y)});return X("glm",Xr(Fw(x,p,{...M,yScale:p}),c))}function uu(e){let{x:t,offset:n,intercept:r}=e,o=t.length,s=e.columnNames.length,i=new mt,c=Array.from({length:s},(g,b)=>b),{fit:a,fam:l,y:f,w:m,size:u}=oo(e,c,i),h;if(r&&e.hasOffset){let g=new mt,b=pu({x:t.map(()=>[1]),p:1,y:f,w:m,offset:n,fam:l,epsilon:e.epsilon,maxit:e.maxit,columnNames:["(Intercept)"],mustart:a.mu},g);gu(b,l,g,!1),g.list.forEach(N=>i.add(N)),h=b.deviance,b.converged||i.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let g=Math.floor(Math.log2(gt(m))),b=0,N=0;for(let M=0;M<o;M++){if(!(m[M]>0))continue;let p=P(m[M],-g);b+=p,N+=p*f[M]}let v=N/b;if(!Number.isFinite(v)){let M=0;for(let x=0;x<o;x++)m[x]>0&&(M=Math.max(M,Math.abs(f[x])));let p=Math.floor(Math.log2(M)),y=0;for(let x=0;x<o;x++)m[x]>0&&(y+=P(m[x],-g)*P(f[x],-p));v=P(y/b,p)}h=Io(i,()=>sr(l,f,f.map(()=>v),m))}else h=Io(i,()=>sr(l,f,n.map(g=>l.linkinv(g)),m));let d=o-m.filter(g=>g===0).length,w=Gl(l,f,a.mu,m,u,a.deviance,i)+2*a.rank;return wu(a.used,{coefficients:a.coefficients,fittedValues:a.mu,linearPredictors:a.eta,deviance:a.deviance,nullDeviance:h,aic:w,rank:a.rank,dfResidual:d-a.rank,dfNull:d-(r?1:0),iter:a.iter,converged:a.converged,boundary:a.boundary,y:f,priorWeights:m,weights:a.workingWeights,residuals:a.workingResiduals,family:l.family,link:l.name,warnings:i.list,qr:a.qr,model:e})}function Cw(e){return(e.link??"identity")!=="identity"?!1:e.family==="gaussian"||e.family==="quasi"&&(e.variance??"constant")==="constant"}function Fw(e,t,n){let r=s=>P(s,t),o={...e,coefficients:e.coefficients.map(r),fittedValues:e.fittedValues.map(r),linearPredictors:e.linearPredictors.map(r),deviance:P(e.deviance,2*t),nullDeviance:P(e.nullDeviance,2*t),aic:e.aic+2*e.priorWeights.filter(s=>s>0).length*t*Math.LN2,y:e.y.map(r),residuals:e.residuals.map(r),model:n};return Object.defineProperty(o,"scaled",{value:e,enumerable:!1}),wu(ua.get(e),o)}var ua=new WeakMap;function wu(e,t){return e!==void 0&&ua.set(t,e),t}function Qt(e){let t=e.scaled;return t===void 0?{fit:e,k:0}:{fit:t,k:e.model.yScale??0}}function ar(e){return Qr(e.model.familySpec)}function Qo(e,t){let n=Qt(e),r=Zo(n.fit,t);return ar(e).fixedDispersion?r:P(r,2*n.k)}function Zo(e,t){if(ar(e).fixedDispersion)return 1;if(e.dfResidual<=0)return NaN;let n=0;for(let r=0;r<e.weights.length;r++)e.weights[r]>0&&(n+=e.weights[r]*e.residuals[r]**2);return e.weights.some(r=>r===0)&&t?.add("observations with zero weight not used for calculating dispersion"),n===1/0?Ow(e):n/e.dfResidual}function Ow(e){let t=[];for(let s=0;s<e.weights.length;s++)e.weights[s]>0&&t.push(s);if(!t.every(s=>Number.isFinite(e.weights[s])&&Number.isFinite(e.residuals[s])))return 1/0;let n=Se([t.map(s=>e.residuals[s])]),r=Se([t.map((s,i)=>Math.sqrt(e.weights[s])*Math.abs(n.groups[0][i]))]),o=0;for(let s of r.groups[0])o+=s*s;return P(o/e.dfResidual,2*(n.k+r.k))}function Dw(e){return e.qr===void 0||e.rank===0?{inv:[],e:[]}:qr(e.qr.qr.slice(0,e.rank).map(t=>t.slice(0,e.rank)))}function Pw(e){if(!(e>=0&&e<1/0))throw new RangeError("'dispersion' must be a non-negative finite number");return e}function so(e,t){let n=P(Pw(e),-2*t);if(n===0?e===0:n>=2**-1022&&n<1/0)return{m:n,s:0};let r=Math.floor(Math.floor(Math.log2(e))/2);return{m:P(e,-2*r),s:r-t}}function cc(e,t={}){K(t,Aw,"glmSummary");let{fit:n,k:r}=Qt(e),o=new mt,s=ar(n),i=t.dispersion===void 0&&!s.fixedDispersion,c=t.dispersion===void 0?void 0:so(t.dispersion,r),a=c===void 0?Zo(n,o):c.m,l=c?.s??0,{inv:f,e:m}=Dw(n),u=f.map((p,y)=>p.map((x,E)=>P(x,-m[y]-m[E]))),d=(n.qr===void 0?[]:n.qr.pivot.slice(0,n.rank)).map(p=>n.coefficients[p]),w=f.map((p,y)=>P(Math.sqrt(p[y]*a),-m[y])),g=l===0?d.map((p,y)=>p/w[y]):d.map((p,y)=>Le(p/Math.sqrt(f[y][y]*a),m[y]-l)),b=!i,N=g.map(p=>b?2*se(-Math.abs(p)):n.dfResidual>0?2*ft(-Math.abs(p),n.dfResidual,!0,!1):NaN);i&&n.dfResidual===0&&(w.fill(NaN),g.fill(NaN));let v=p=>P(p,r),M=p=>P(p,2*r);return X("glmSummary",{dispersion:l===0?M(a):t.dispersion,estimate:d.map(v),stdError:l===0?w.map(v):f.map((p,y)=>Le(Math.sqrt(p[y]*a),l+r-m[y])),statistic:g,pValue:N,statisticName:b?"z value":"t value",covUnscaled:u,covScaled:l===0?u.map(p=>p.map(y=>M(y*a))):f.map((p,y)=>p.map((x,E)=>Le(x*a,2*(l+r)-m[y]-m[E]))),dfResidual:n.dfResidual,warnings:o.list},e)}function Vw(e,...t){K(Ur(t),Sw,"glmResiduals");let n=Br(["type"],t).type??"deviance",r=Qt(e);return lc(r.fit,n).map(o=>P(o,r.k))}function lc(e,t){let n=ar(e),{y:r,fittedValues:o,priorWeights:s}=e;switch(t){case"response":return r.map((i,c)=>i-o[c]);case"working":return e.residuals.slice();case"pearson":return r.map((i,c)=>_w(n,i,o[c],s[c]));case"deviance":return r.map((i,c)=>s[c]===0?0:Math.sign(i-o[c])*Math.sqrt(Math.max(n.devContribution(i,o[c],s[c]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function Jo(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let i=n.qr.length,c=Array.from({length:i},(l,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=Is(n,c).map(l=>l.reduce((f,m)=>f+m*m,0))}let o=0,s=ua.get(e)??e.weights.map(i=>i>0);return e.priorWeights.forEach((i,c)=>{let a=s[c]?r[o++]??0:0;i>0&&t.push(a>1-10*ro?1:a)}),t}function bu(e){let t=[],n=0,r=ua.get(e)??e.weights.map(o=>o>0);return e.priorWeights.forEach((o,s)=>{let i=r[s]?n++:-1;o>0&&t.push(i)}),t}function $w(e){let t=e.slice().sort((r,o)=>r-o),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function zw(e,t={}){K(t,Tw,"glmRstandard");let n=t.type??"pearson";if(n!=="pearson"&&n!=="deviance")throw new RangeError(`unknown type "${String(n)}" for glmRstandard`);let{fit:r,k:o}=Qt(e),s=Jo(r),{m:i,s:c}=t.dispersion===void 0?{m:Zo(r),s:0}:so(t.dispersion,o),a=Math.sqrt(i),l=lc(r,n).filter((m,u)=>r.priorWeights[u]>0),f=r.rank>0?100*ro*$w(l.map(Math.abs)):0;return l.map((m,u)=>{let h=Le((Math.abs(m)<f?0:m)/(a*Math.sqrt(1-s[u])),-c);return Number.isFinite(h)?h:NaN})}function Ww(e,t={}){K(t,kw,"glmCooksDistance");let{fit:n,k:r}=Qt(e),o=Jo(n),{m:s,s:i}=t.dispersion===void 0?{m:Zo(n),s:0}:so(t.dispersion,r);return lc(n,"pearson").filter((a,l)=>n.priorWeights[l]>0).map((a,l)=>{let f=o[l]/(s*n.rank),m=(a/(1-o[l]))**2*f;return(!Number.isFinite(m)||f<2**-1022)&&(m=jw(a,o[l],s,n.rank)),i!==0&&(m=Le(m,-2*i)),Number.isFinite(m)?m:NaN})}function jw(e,t,n,r){if(!Number.isFinite(e)||!(n>0&&Number.isFinite(n)))return NaN;let o=e===0?0:Math.floor(Math.log2(Math.abs(e))),s=Math.floor(Math.log2(n)),i=P(e,-o)/(1-t);return P(i*i*(t/(P(n,-s)*r)),2*o-s)}function Kw(e,t={}){K(t,qw,"glmPredict");let{fit:n,k:r}=Qt(e),o=N=>P(N,r),s=ar(n),i,c;if(t.newdata===void 0)i=n.model.x,c=n.linearPredictors.slice();else{if(i=du(t.newdata).map(M=>n.model.intercept?[1,...M]:M),i.some(M=>M.length!==n.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let N=t.newoffset===void 0?i.map(()=>0):Array.from(t.newoffset,M=>P(M,-r));if(N.length!==i.length)throw new RangeError("newoffset must have one value per row of newdata");let v=n.coefficients.flatMap((M,p)=>Number.isNaN(M)?[]:[p]);c=i.map((M,p)=>{let y=M.reduce((E,R,A)=>Number.isNaN(n.coefficients[A])?E:E+R*n.coefficients[A],0);return(To(y,0)?vr(v.map(E=>M[E]),v.map(E=>n.coefficients[E]),y):y)+N[p]})}let{m:a,s:l}=t.dispersion===void 0?{m:Zo(n),s:0}:so(t.dispersion,r),f=N=>P(N,r+l),m=n.rank,u=n.qr===void 0?[]:n.qr.pivot.slice(0,m),h=n.qr?.qr,d=i.map(N=>{let v=new Array(m).fill(0),M=0;for(let p=0;p<m;p++){let y=N[u[p]];for(let x=0;x<p;x++)y-=h[x][p]*v[x];v[p]=y/h[p][p],M+=v[p]*v[p]}if(!(M>=2**-1022&&M<1/0)){let p=v.reduce((y,x)=>Math.max(y,Math.abs(x)),0);if(p>0&&Number.isFinite(p)){let y=Math.sqrt(v.reduce((E,R)=>E+(R/p)**2,0)),x=p*y*Math.sqrt(a);return Number.isFinite(x)?x:p*(y*Math.sqrt(a))}}return Math.sqrt(M)*Math.sqrt(a)}),w=t.type??"link";if(w!=="link"&&w!=="response")throw new RangeError(`invalid 'type': ${String(w)}`);let g=t.newdata===void 0?[]:Ls(n.qr,m,i),b=g.length>0?{nonEstimable:g,warnings:[_s]}:{};return w==="link"?X("glmPredict",{fit:c.map(o),seFit:d.map(f),...b},e):X("glmPredict",Io(new mt,()=>({fit:c.map(N=>o(s.linkinv(N))),seFit:d.map((N,v)=>f(N*Math.abs(s.muEta(c[v])))),...b})),e)}var Bw=["parm","level","test"],vu={epsilon:1e-300,maxit:100,untilCoefficients:!0};function fc(e,t,n,r,o=1e-11){let s=[];for(let w=0;w<t.length;w++)n[w]>0&&s.push(w);let i=0,c=0;for(let w of s)i+=n[w],c+=n[w]*t[w];if(r&&!(Number.isFinite(i)&&Number.isFinite(c))){let w=0;for(let g of s)w=Math.max(w,n[g]);if(Number.isFinite(w)){let g=w>1?Math.floor(Math.log2(w)):0;i=0,c=0;for(let b of s){let N=P(n[b],-g);i+=N,c+=N*t[b]}}}let a=r?c/i:0,l=0;for(let w of s)l+=n[w]*(t[w]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(w=>Math.sqrt(n[w])),u=At(s.map((w,g)=>e[w].map(b=>b*m[g])),{tol:o}),h=Yr(u,s.map((w,g)=>t[w]*m[g])),d=0;for(let w of h)d+=w*w;if(!Number.isFinite(l)){let w=s.map((b,N)=>m[N]*(t[b]-a)),g=0;for(let b of w)g=Math.max(g,Math.abs(b));for(let b of h)g=Math.max(g,Math.abs(b));if(Number.isFinite(g)){let b=g>1?Math.floor(Math.log2(g)):0,N=0;for(let M of w)N+=P(M,-b)**2;let v=0;for(let M of h)v+=P(M,-b)**2;return P(P(N-v,b),b)}}return l-d}function Uw(e,t={}){K(t,Bw,"glmConfint");let{fit:n,k:r}=Qt(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},(C,q)=>q);for(let C of c)if(!Number.isInteger(C)||C<0||C>=i)throw new RangeError(`invalid coefficient index ${C}`);let a=n.model,l=n.coefficients.map((C,q)=>q).filter(C=>!Number.isNaN(n.coefficients[C])),f=new mt,m=oo(a,l,f,{...vu,start:a.start===void 0?void 0:l.map(C=>a.start[C])}),u=new Array(i).fill(NaN);if(l.forEach((C,q)=>u[C]=m.fit.coefficients[q]),!m.fit.converged)return{lower:c.map(()=>NaN),upper:c.map(()=>NaN)};let h=!1,d={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},w=rt(()=>cc(d)),g=w.dispersion,b=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((C,q)=>b[l[C]]=w.stdError[q]);let v=a.x.map(C=>l.map(q=>C[q])),M=m.fit.eta,p=(C,q)=>{let F=l.filter(I=>I!==C),_=oo(a,F,new mt,{...vu,start:void 0,etastart:M,extraOffset:a.x.map(I=>I[C]*q)}).fit;if(!_.converged)return h=!0,NaN;let O=s==="LRT"?_.deviance-m.fit.deviance:fc(v,_.workingResiduals,_.workingWeights,!1);return Math.sign(q-u[C])*Math.sqrt(Math.max(O/g,0))},y=(1-o)/2,x=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),E=2**-9,R=x<E,A=R?[-x,x]:[le(y),y<1e-8?le(y,{lowerTail:!1}):le((1+o)/2)],S=c.map(()=>NaN),T=c.map(()=>NaN),L=(C,q,F)=>{let _=F===0?-1:1;h=!1;let O=NaN,I=NaN;for(let z=0;z<=30;z++){let J=u[C]+_*2**z*b[C],Z=p(C,J);if(Math.abs(Z)>=Math.abs(q)){O=J,I=Z;break}}if(Number.isNaN(O)||h)return NaN;let D=z=>z===u[C]?-q:p(C,z)-q,[k,V]=F===0?[O,u[C]]:[u[C],O],[B,Q]=F===0?[I-q,-q]:[-q,I-q],re;try{re=Kt(D,k,V,{tol:1e-14*b[C],maxIter:500,fLower:B,fUpper:Q})}catch(z){if(!h)throw z;re=NaN}return h?NaN:re};return c.forEach((C,q)=>{if(Number.isNaN(u[C]))return;if(!R){S[q]=L(C,A[0],0),T[q]=L(C,A[1],1);return}let F=[-2*E,-E,0,E,2*E],_=[L(C,-2*E,0),L(C,-E,0),u[C],L(C,E,1),L(C,2*E,1)].map(I=>I-u[C]),O=I=>{let D=0;for(let k=0;k<5;k++){let V=1;for(let B=0;B<5;B++)B!==k&&(V*=(I-F[B])/(F[k]-F[B]));D+=V*_[k]}return u[C]+D};S[q]=O(A[0]),T[q]=O(A[1])}),{lower:S.map(C=>P(C,r)),upper:T.map(C=>P(C,r))}}var Hw=["type","omega","sandwich"],Yw=["cluster","type","cadjust","multi0","fix","sandwich"],ha=2**-52;function hc(e,t){if(t===0)return e;let n=dc.get(e)??{mant:e,exp:e.map(()=>0)};return Ir(n.mant,n.exp.map(r=>r+t))}var pa=e=>Math.floor(Math.log2(Math.abs(e)));function un(e){if(e===0||!Number.isFinite(e))return[e,0];let t=pa(e);return[P(e,-t),t]}var es=(e,t)=>[e[0]*t[0],e[1]+t[1]];function ir(e,t=!1){let n=-1/0;for(let[o,s]of e)o!==0&&Number.isFinite(o)&&(n=Math.max(n,s+pa(o)));let r=n===-1/0?0:n;return t&&(r=2*Math.floor(r/2)),{v:e.map(([o,s])=>Le(o,s-r)),s:r}}var cr=(e,t)=>Math.abs(t)<=2046?P(e,t):Le(e,t),dc=new WeakMap;function Ir(e,t){let n=e.map((r,o)=>r.map((s,i)=>cr(s,t[o]+t[i])));return dc.set(n,{mant:e,exp:t}),n}function gc(e,t,n){let r=dc.get(e),o=r?.mant[t]?.[t];if(!(r===void 0||o===void 0||!Object.is(cr(o,2*r.exp[t]),n)))return cr(Math.sqrt(o),r.exp[t])}function lr(e){return typeof e=="object"&&e!==null&&"family"in e&&"model"in e}function da(e){if(!(typeof e=="object"&&e!==null&&Array.isArray(e.coefficients)&&"qr"in e&&(lr(e)||"sigma"in e&&"x"in e&&"rows"in e)))throw new TypeError("'fit' must be the result of lm or glm")}function Eu(e,t){return e.pivot.slice(0,t).map((n,r)=>[n,r]).sort((n,r)=>n[0]-r[0])}function xu(e,t){let{inv:n,e:r}=qr(e.qr.slice(0,t).map(s=>s.slice(0,t))),o=Eu(e,t);return{inv:o.map(([,s])=>o.map(([,i])=>n[s][i])),e:o.map(([,s])=>r[s]),cols:o.map(([s])=>s)}}function yu(e,t,n){let{inv:r,e:o,cols:s}=xu(e,t);return{A:r.map((i,c)=>i.map((a,l)=>P(a,-o[c]-o[l]-n))),cols:s}}function Nu(e,t){let n=e.qr.slice(0,t).map(i=>i.slice(0,t)),r=n.map((i,c)=>{let a=0;for(let l=0;l<=c;l++)a=Math.max(a,Math.abs(n[l][c]));return a>0&&Number.isFinite(a)?pa(a):0}),{inv:o}=qr(n.map(i=>i.map((c,a)=>P(c,-r[a])))),s=Eu(e,t);return{B:s.map(([,i])=>s.map(([,c])=>o[i][c])),e:s.map(([,i])=>r[i])}}function Ru(e,t){let n=e.qr.length;return Is(e,Array.from({length:n},(r,o)=>Array.from({length:t},(s,i)=>o===i?1:0)))}function Gw(e,t){let n=e.qr.length;return t===0?new Array(n).fill(0):Ru(e,t).map(o=>{let s=o.reduce((i,c)=>i+c*c,0);return s>1-10*ha?1:s})}function Mu(e,t,n,r){return n.map((o,s)=>{if(o>=1)return 0;let i=r(s);if(o<=.5||e===void 0||i<0)return 1-o;let c=e.qr.length,a=new Array(c).fill(0);a[i]=1;let l=Ua(e,a),f=0;for(let m=t;m<c;m++)f+=l[m]*l[m];return f})}function ga(e){if(lr(e))return e.model.columnNames;let t=e.coefNames;if(t!==void 0)return t;let n=e.coefficients.length;return e.intercept?["(Intercept)",...Array.from({length:n-1},(r,o)=>`x${o+1}`)]:Array.from({length:n},(r,o)=>`x${o+1}`)}function wc(e){let t=ga(e);if(e.qr===void 0||e.rank===0)return{vcov:[],names:[]};let{inv:n,e:r,cols:o}=xu(e.qr,e.rank),s=lr(e)?0:sc(e.qr),i,c;if(lr(e)){let l=Qt(e),f=ar(e).fixedDispersion?1:Qo(l.fit),m=f===0||!Number.isFinite(f)?0:2*Math.floor(pa(f)/2);i=P(f,-m),c=m+2*l.k}else{let[l,f]=un(e.sigma);i=l*l,c=2*f}let a=n.map(l=>l.map(f=>f*i));return{vcov:Ir(a,r.map(l=>(c-s)/2-l)),names:o.map(l=>t[l])}}function Au(e,t=!1){let n=ga(e);if(lr(e)){let w=t?Qt(e):{fit:e,k:0},g=w.fit,b=ar(g),N=g.priorWeights,v=N.map(_=>_>0),M=N.flatMap((_,O)=>_>0?[O]:[]),{A:p,cols:y}=g.qr===void 0||g.rank===0?{A:[],cols:[]}:yu(g.qr,g.rank,0),x=M.map(_=>y.map(O=>g.model.x[_][O])),E=M.map(_=>g.weights[_]),R=M.map(_=>g.weights[_]*g.residuals[_]),A=b.family==="binomial"||b.family==="poisson",S=A?1:R.reduce((_,O)=>_+O*O,0)/E.reduce((_,O)=>_+O,0),T=g.model.rows,L=Jo(g),C=bu(g),q=()=>{let _=Qt(g),O=M.map(z=>_.fit.residuals[z]),I=E.map(Math.sqrt),{B:D,e:k}=g.qr===void 0||g.rank===0?{B:[],e:[]}:Nu(g.qr,g.rank),V=x.map((z,J)=>z.map((Z,ke)=>P(I[J]*Z,-k[ke]))),{v:B,s:Q}=ir(I.map((z,J)=>z===0?[0,0]:es(un(z),un(O[J])))),re=()=>{let z=[1,0];if(!A){let J=ir(E.map((ke,ne)=>ke===0?[0,0]:es(un(ke),un(O[ne])))),Z=ir(E.map(un));z=[J.v.reduce((ke,ne)=>ke+ne*ne,0)/Z.v.reduce((ke,ne)=>ke+ne,0),2*J.s-Z.s+2*_.k]}return{T:x.map(J=>J.map((Z,ke)=>cr(Z,-k[ke]))),factor:[z[0]*z[0],2*z[1]]}};return{T:V,B:D,e:k,u:B,s:Q+_.k,omegaPart:re}},F;if(_r(S)&&R.every(Number.isFinite))F=R.map(_=>_/S);else{let _=Qt(g),O=ir(E.map((I,D)=>I===0?[0,0]:es(un(I),un(_.fit.residuals[M[D]]))));if(A)F=O.v.map(I=>cr(I,O.s+_.k));else{let I=ir(E.map(un)),D=O.v.reduce((k,V)=>k+V*V,0)/I.v.reduce((k,V)=>k+V,0);F=O.v.map(k=>cr(k/D,I.s-O.s-_.k))}}return{glm:!0,names:y.map(_=>n[_]),X:x,W:E,res:F,phiS:S,A:p,h:L,omh:Mu(g.qr,g.rank,L,_=>C[_]),qr:g.qr,rank:g.rank,qrRow:_=>C[_],inputRows:M.map(_=>T[_]),nInput:Ha(e,T),nonZero:v,dispersion:()=>Qo(g),scaled:q,k:w.k}}let r=e,o=r.weights,s=r.residuals.map((w,g)=>o===void 0||o[g]!==0),i=s.flatMap((w,g)=>w?[g]:[]),c=sc(r.qr),{A:a,cols:l}=r.rank===0?{A:[],cols:[]}:yu(r.qr,r.rank,c),f=i.map(w=>l.map(g=>r.x[w][g])),m=i.map(w=>o?.[w]??1),u=i.map((w,g)=>m[g]*r.residuals[w]),h=Gw(r.qr,r.rank),d=()=>{let w=m.map(y=>c===0?Math.sqrt(y):P(Math.sqrt(y),-c/2)),{B:g,e:b}=r.rank===0?{B:[],e:[]}:Nu(r.qr,r.rank),N=f.map((y,x)=>y.map((E,R)=>P(w[x]*E,-b[R]))),{v,s:M}=ir(w.map((y,x)=>es(un(y),un(r.residuals[i[x]]))));return{T:N,B:g,e:b,u:v,s:M,omegaPart:()=>({T:f.map(y=>y.map((x,E)=>cr(x,-b[E]))),factor:[1,-2*c]})}};return{glm:!1,names:l.map(w=>n[w]),X:f,W:m,res:u,phiS:1,A:a,h,omh:Mu(r.qr,r.rank,h,w=>w),qr:r.qr,rank:r.rank,qrRow:w=>w,inputRows:i.map(w=>r.rows[w]),nInput:Ha(r,r.rows),nonZero:s,dispersion:()=>r.sigma*r.sigma,scaled:d,k:0}}var bc=2**-969,_r=e=>Math.abs(e)>=bc&&Math.abs(e)<1/0;function Su(e,t,n,r){if(!(e.phiS>=bc&&e.phiS<1/0)||!_r(e.X.length*e.phiS*e.phiS))return!1;for(let o=0;o<n.length;o++)if(!_r(e.A[o][o])||!r&&(!_r(t[o][o])||!_r(n[o][o]))||!n[o].every(Number.isFinite))return!1;return!0}function ts(e,t,n){let r=e.length,s=e.map(i=>Array.from({length:r},(c,a)=>i.reduce((l,f,m)=>l+f*t[m][a],0))).map(i=>Array.from({length:r},(c,a)=>n*i.reduce((l,f,m)=>l+f*e[m][a],0)));for(let i=0;i<r;i++)for(let c=0;c<i;c++)s[i][c]=s[c][i];return s}function ao(e,t,n){let r=e[0]?.length??0,o=Array.from({length:r},()=>new Array(r).fill(0));e.forEach((s,i)=>{let c=t[i];for(let a=0;a<r;a++)for(let l=a;l<r;l++)o[a][l]+=c*s[a]*s[l]});for(let s=0;s<r;s++)for(let i=s;i<r;i++)o[s][i]=o[i][s]=o[s][i]/n;return o}function mc(e){let t=e.slice(0,10).map(n=>String(n+1));return e.length>10?`${t.join(", ")}, ...`:t.join(", ")}var Xw=["HC3","const","HC","HC0","HC1","HC2","HC4","HC4m","HC5"];function Qw(e,t={}){K(t,Hw,"vcovHC"),da(e);let n=Te(t.type,Xw),r=n==="HC"?"HC0":n,o=t.sandwich??!0;if(typeof o!="boolean")throw new TypeError("'sandwich' must be true or false");let s=Au(e,t.omega===void 0),i=s.X.length,c=s.names.length,a=[],{res:l,h:f,omh:m}=s,u,h;if(t.omega!==void 0){let w;if(typeof t.omega=="function"?w=t.omega(l.slice(),f.slice(),i-c):(w=t.omega,w.length!==i&&w.length===s.nonZero.length&&(w=w.filter((g,b)=>s.nonZero[b]))),!Array.isArray(w)&&!ArrayBuffer.isView(w))throw new TypeError("'omega' must give an array of numbers");if(w.length!==i)throw new RangeError(`'omega' must have one value per observation (${i})`);if(u=Array.from(w,Number),u.some(g=>!Number.isFinite(g)))throw new RangeError("'omega' must be finite")}else if(r==="const"){let w=s.dispersion();u=s.W.map(g=>w*g/(s.phiS*s.phiS))}else{let w=f.flatMap((N,v)=>N>1-Math.sqrt(ha)?[s.inputRows[v]]:[]);if(w.length>0&&a.push(r==="HC0"||r==="HC1"?`${r} covariances become (close to) singular if hat values are (close to) 1 as for observation(s) ${mc(w)}`:`${r} covariances are numerically unstable for hat values close to 1 (and undefined if exactly 1) as for observation(s) ${mc(w)}`),r!=="HC0"&&r!=="HC1"){let N=f.flatMap((v,M)=>v>=1?[s.inputRows[M]]:[]);if(N.length>0)throw new RangeError(`${r} covariances are undefined for hat values of 1 as for observation(s) ${mc(N)}`)}if(r==="HC1"&&i<=c)throw new RangeError("HC1 covariances need more observations than coefficients");let g=Math.round(f.reduce((N,v)=>N+v,0)),b=f.reduce((N,v)=>Math.max(N,v),0);h=N=>{switch(r){case"HC0":return N;case"HC1":return N.map(v=>v*i/(i-c));case"HC2":return N.map((v,M)=>v/m[M]);case"HC3":return N.map((v,M)=>v/m[M]**2);case"HC4":return N.map((v,M)=>v/m[M]**Math.min(4,i*f[M]/g));case"HC4m":return N.map((v,M)=>v/m[M]**(Math.min(1,i*f[M]/g)+Math.min(1.5,i*f[M]/g)));default:return N.map((v,M)=>v/Math.sqrt(m[M]**Math.min(i*f[M]/g,Math.max(4,i*.7*b/g))))}},u=h(l.map(N=>N*N))}let d;if(o&&r==="const"&&t.omega===void 0)d=wc(e).vcov;else if(o){let w=ao(s.X,u,i);if(d=ts(s.A,w,i*s.phiS*s.phiS),!Su(s,w,d,(h===void 0?u:l).every(g=>g===0))){let g=s.scaled();if(h!==void 0){let b=ts(g.B,ao(g.T,h(g.u.map(N=>N*N)),i),i);d=Ir(b,g.e.map(N=>g.s-N))}else{let{T:b,factor:N}=g.omegaPart(),v=ir(u.map(M=>es(un(M),N)),!0);d=Ir(ts(g.B,ao(b,v.v,i),i),g.e.map(M=>v.s/2-M))}}d=hc(d,s.k)}else{let w=ao(s.X,u,i);if(qu(w,(h===void 0?u:l).every(g=>g===0)))d=w;else{let g=s.scaled();if(h!==void 0){let{mant:b,exp:N}=Iu(g,ao(g.T,h(g.u.map(v=>v*v)),i));d=Ir(b,N)}else{let b=ir(u.map(un),!0);d=Ir(ao(g.omegaPart().T,b.v,i),g.e.map(N=>b.s/2+N))}}d=hc(d,-s.k)}return X("vcovHC",{vcov:d,names:s.names,warnings:a},e)}var Zw=["HC","HC0","HC1","HC2","HC3"];function Jw(e){if(e==null||typeof e=="number"&&Number.isNaN(e))throw new RangeError("cannot handle NAs in 'cluster': either refit the model without the NA observations in 'cluster' or impute the NAs");if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="number")return ut(e);if(typeof e=="string")return e;throw new TypeError("cluster labels must be numbers, strings or booleans")}function e1(e){let t=new Map;return{g:e.map(r=>{let o=t.get(r);return o===void 0&&(o=t.size,t.set(r,o)),o}),G:t.size}}function t1(e){let t=[],n=(r,o,s)=>{if(s.length===r){t.push(s.slice());return}for(let i=o;i<e;i++)s.push(i),n(r,i+1,s),s.pop()};for(let r=1;r<=e;r++)n(r,0,[]);return t}var Tu=ha**(1/1.3);function ku(e,t){let{values:n,vectors:r}=jt(e,{symmetric:!0});if(n.some(i=>!(i>=Tu)))return;let o=r,s=n.map(i=>i**t);return e.map((i,c)=>e.map((a,l)=>o[c].reduce((f,m,u)=>f+m*s[u]*o[l][u],0)))}function n1(e){return{A:e.A,xs:t=>e.X[t].map(n=>n*Math.sqrt(e.W[t])),rs:t=>{let n=Math.sqrt(e.W[t]);return n===0?0:e.res[t]/n},single:(t,n,r)=>e.X[t].map(o=>n*e.res[t]*r*o),psi:t=>e.X[t].map(n=>n*e.res[t])}}function r1(e){return{A:e.B,xs:t=>e.T[t],rs:t=>e.u[t],single:(t,n,r)=>e.T[t].map(o=>n*e.u[t]*r*o),psi:t=>e.T[t].map(n=>n*e.u[t])}}function o1(e,t,n,r,o){let s=e.names.length,i=Array.from({length:r},()=>[]);n.forEach((f,m)=>i[f].push(m));let c=Math.sqrt((r-1)/r),a=r===n.length,l;return i.map((f,m)=>{if(f.length===1){let N=f[0];if(a?e.h[N]>=1:!(e.omh[N]>=Tu))throw uc(m);return t.single(N,c,e.omh[N]**o)}let u=f.map(t.xs),h=f.map(t.rs);if(e.qr!==void 0&&e.rank>0){l??=Ru(e.qr,e.rank);let N=s1(e,l,f,h,o);if(N===null)throw uc(m);if(N!==void 0)return Array.from({length:s},(v,M)=>c*u.reduce((p,y,x)=>p+y[M]*N[x],0))}let d=u.map(N=>Array.from({length:s},(v,M)=>N.reduce((p,y,x)=>p+y*t.A[x][M],0))),w=u.map((N,v)=>u.map((M,p)=>(v===p?1:0)-d[v].reduce((y,x,E)=>y+x*M[E],0)));for(let N=0;N<w.length;N++)for(let v=0;v<N;v++)w[N][v]=w[v][N]=(w[N][v]+w[v][N])/2;let g=ku(w,o);if(g===void 0)throw uc(m);let b=g.map(N=>N.reduce((v,M,p)=>v+M*h[p],0));return Array.from({length:s},(N,v)=>c*u.reduce((M,p,y)=>M+p[v]*b[y],0))})}function s1(e,t,n,r,o){let s=e.qr,i=e.rank,c=new Array(i).fill(0),a=n.map(p=>{let y=e.qrRow(p);return y<0?c:t[y]}),l=Array.from({length:i},(p,y)=>Array.from({length:i},(x,E)=>a.reduce((R,A)=>R+A[y]*A[E],0))),{values:f,vectors:m}=jt(l,{symmetric:!0});if(!f.some(p=>p>.5))return;let u=m,h=r.slice(),d=[];f.forEach((p,y)=>{let x=a.map(E=>E.reduce((R,A,S)=>R+A*u[S][y],0));if(p>.5)d.push(x.map(E=>E/Math.sqrt(p)));else if(p>0){let R=Math.expm1(o*Math.log1p(-p))/p*x.reduce((A,S,T)=>A+S*r[T],0);x.forEach((A,S)=>h[S]+=R*A)}});let w=s.qr.length,g=d.map(p=>{let y=new Array(w).fill(0);return n.forEach((x,E)=>{let R=e.qrRow(x);R>=0&&(y[R]=p[E])}),Ua(s,y).slice(i)}),b=g.map(p=>g.map(y=>p.reduce((x,E,R)=>x+E*y[R],0))),N=ku(b,o);if(N===void 0)return null;let v=d.map(p=>p.reduce((y,x,E)=>y+x*r[E],0)),M=N.map((p,y)=>p.reduce((x,E,R)=>x+E*v[R],0)-v[y]);return d.forEach((p,y)=>p.forEach((x,E)=>h[E]+=M[y]*x)),h}function uc(e){return new RangeError(`clustered HC2/HC3 covariances are undefined: the block I - H_gg of cluster ${e+1} (in order of appearance) is singular, as with cluster fixed effects`)}function a1(e,t){let n=Array.from({length:t},()=>new Array(t).fill(0));for(let r of e)for(let o=0;o<t;o++)for(let s=0;s<t;s++)n[o][s]+=r[o]*r[s];return n}function pc(e){if(e.length===0)return e;let{values:t,vectors:n}=jt(e,{symmetric:!0});if(t.every(s=>s>=0))return e;let r=n,o=t.map(s=>Math.max(s,0));return e.map((s,i)=>e.map((c,a)=>r[i].reduce((l,f,m)=>l+f*o[m]*r[a][m],0)))}function i1(e,t){let n=e.length;if(n===0)return{mant:e,exp:t};let r=e.map((a,l)=>a[l]!==0?Math.sqrt(Math.abs(a[l])):1),o=e.map((a,l)=>a.map((f,m)=>f/r[l]/r[m])),{values:s}=jt(o,{symmetric:!0}),i=s.reduce((a,l)=>Math.max(a,Math.abs(l)),0);if(s.every(a=>a>=-16*n*ha*i))return{mant:e,exp:t};let c=Math.max(...t);return{mant:pc(e.map((a,l)=>a.map((f,m)=>cr(f,t[l]+t[m]-2*c)))),exp:t.map(()=>c)}}function qu(e,t){return t||e.every((n,r)=>_r(n[r])&&n.every(Number.isFinite))}function Iu(e,t){let[n,r]=e.omegaPart().factor;return{mant:t.map(o=>o.map(s=>s/n)),exp:e.e.map(o=>e.s+o-r/2)}}function c1(e,t={}){K(t,Yw,"vcovCL"),da(e);let n=Au(e,!0),r=n.X.length,o=n.names.length,s=t.type===void 0?n.glm?"HC0":"HC1":Te(t.type,Zw),i=s==="HC"?"HC0":s,c=t.cadjust??!0,a=t.multi0??!1,l=t.fix??!1,f=t.sandwich??!0;for(let[v,M]of[["cadjust",c],["multi0",a],["fix",l],["sandwich",f]])if(typeof M!="boolean")throw new TypeError(`'${v}' must be true or false`);let m,u=t.cluster;if(u===void 0)m=[n.inputRows.map(String)];else{let v=y=>Array.isArray(y)||ArrayBuffer.isView(y);if(!v(u))throw new TypeError("'cluster' must be an array of labels or an array of such arrays");let p=u.length>0&&Array.from(u).every(v)?Array.from(u):[u];if(p.length===0)throw new RangeError("'cluster' has no dimension");m=p.map(y=>{if(y.length!==n.nInput)throw new RangeError("number of observations in 'cluster' and 'estfun()' do not match");return n.inputRows.map(x=>Jw(y[x]))})}if(i==="HC1"&&r<=o)throw new RangeError("HC1 covariances need more observations than coefficients");let h=m.length,d=t1(h).map((v,M)=>{let p=a&&h>1&&M===2**h-2,y=p?n.inputRows.map(String):n.inputRows.map((A,S)=>v.length===1?m[v[0]][S]:JSON.stringify(v.map(T=>m[T][S]))),{g:x,G:E}=e1(y);if(E<2)throw new RangeError("at least two clusters are needed");let R=p?i==="HC1"?(r-o)/(r-1):1:c?E/(E-1):1;return{g:x,G:E,c:R,sign:v.length%2===1?1:-1}}),w=v=>{let M=Array.from({length:o},()=>new Array(o).fill(0));for(let{g:p,G:y,c:x,sign:E}of d){let R;i==="HC2"||i==="HC3"?R=o1(n,v,p,y,i==="HC2"?-.5:-1):y===r?R=n.X.map((S,T)=>v.psi(T)):(R=Array.from({length:y},()=>new Array(o).fill(0)),n.X.forEach((S,T)=>{let L=R[p[T]],C=v.psi(T);for(let q=0;q<o;q++)L[q]+=C[q]}));let A=a1(R,o);M=M.map((S,T)=>S.map((L,C)=>L+E*x*A[T][C]/r))}return i==="HC1"&&(M=M.map(p=>p.map(y=>y*(r-1)/(r-o)))),M},g=n.res.every(v=>v===0),b=n.phiS>=bc&&n.phiS<1/0&&n.A.every((v,M)=>_r(v[M]))&&n.res.every(Number.isFinite),N;if(b){let v=w(n1(n));if(!f)qu(v,g)&&(N=l?pc(v):v);else{let M=ts(n.A,v,r*n.phiS*n.phiS);Su(n,v,M,g)&&(N=l?pc(M):M)}}if(N===void 0){let v=n.scaled(),M=w(r1(v)),{mant:p,exp:y}=f?{mant:ts(v.B,M,r),exp:v.e.map(E=>v.s-E)}:Iu(v,M),x=l?i1(p,y):{mant:p,exp:y};N=Ir(x.mant,x.exp)}return N=hc(N,f?n.k:-n.k),X("vcovCL",{vcov:N,names:n.names,warnings:[]},e)}var l1=["vcov","df"],f1=["parm","level","vcov","df"];function _u(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function Lu(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 Cu=e=>new Set(e).size===e.length;function Fu(e,t){da(e);let n=e.coefficients,r=ga(e),o=n.length,s=n.map(m=>Number.isNaN(m)),i,c,a=[],l;if(t===void 0){let m=wc(e),u=new Map(m.names.map((d,w)=>[d,w])),h=r.map((d,w)=>s[w]?void 0:u.get(d));i=h.map(d=>h.map(w=>d===void 0||w===void 0?NaN:m.vcov[d][w])),c=r,l=h.map(d=>d===void 0?NaN:gc(m.vcov,d,m.vcov[d][d])??Math.sqrt(m.vcov[d][d]))}else{let m=typeof t=="function"?rt(()=>t(e)):t,u=_u(m)?m.vcov:m;if(_u(m)){if(i=Lu(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=Lu(m);l=i.map((h,d)=>{let w=h[d],g=typeof u=="object"&&u!==null?gc(u,d,w):void 0;if(g!==void 0&&!Number.isNaN(g))return g;if(!Number.isFinite(w))throw new RangeError(`the variance of coefficient ${d+1} in 'vcov' is not a finite number`);if(w<0)throw new RangeError(`the variance of coefficient ${d+1} in 'vcov' is negative`);return Math.sqrt(w)})}if(c!==void 0&&Cu(c)&&Cu(r)){let m=new Map(c.map((h,d)=>[h,d])),u=r.flatMap((h,d)=>m.has(h)?[d]:[]);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 Ou(e,t){if(t===void 0)return lr(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 Du=e=>Number.isFinite(e)&&e>0;function m1(e){return lr(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function u1(e,t={}){K(t,l1,"coeftest");let{est:n,se:r,names:o,warnings:s}=Fu(e,t.vcov),i=Ou(e,t.df),c=Du(i),a=n.map((f,m)=>f/r[m]),l=a.map(f=>Number.isNaN(f)?NaN:c?2*ft(-Math.abs(f),i,!0,!1):2*se(-Math.abs(f)));return X("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:m1(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function h1(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 p1(e,t={}){K(t,f1,"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=Fu(e,t.vcov),o=Ou(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]=Du(o)?[bt(c,o,!0,!1),c<1e-8?bt(c,o,!1,!1):bt(1-c,o,!0,!1)]:[le(c),c<1e-8?le(c,{lowerTail:!1}):le(1-c)],f=(m,u,h)=>m+h*u;return X("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:h1(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var d1=["test","dispersion"],g1=(e,t)=>yt(e,t/2,2,!1,!1);function w1(e,t,n){let{fit:r,w:o}=oo(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 vc=(e,t)=>fc(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function b1(e,t={}){K(t,d1,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Qt),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:so(t.dispersion,o),c=i?.s??0,a=v1(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return X("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,d)=>f[d]?P(h,2*o):h));if(c!==0&&a.colNames.includes("Cp")){let u=a.colNames.indexOf("Cp"),h=a.colNames.indexOf("Resid. Dev"),d=a.colNames.indexOf("Resid. Df"),w=s[0].model.x.length;for(let g of m)g[u]=g[h]+2*(w-g[d])*t.dispersion}return X("glmAnova",{...a,table:m},...n)}function v1(e,t,n,r=0){let o=new mt,s=e[0],i=Qr(s.model.familySpec);if(e.some(E=>{let R=Qr(E.model.familySpec);return R.family!==i.family||R.name!==i.name||R.varianceName!==i.varianceName}))throw new RangeError("models were not all fitted with the same family and link");let c=i.fixedDispersion,a=n.test===void 0?n.dispersion!==void 0||c?"Chisq":"F":n.test;if(a!==!1&&!["Rao","LRT","Chisq","F","Cp"].includes(a))throw new RangeError(`invalid 'test': ${String(a)}`);let l=[],f,m,u=[],h=new Set,d;if(t){let E=e[0].model.x.length;if(e.some(A=>A.model.x.length!==E))throw new RangeError("models were not all fitted to the same size of dataset");let R=(A,S)=>A.length===S.length&&A.every((T,L)=>JSON.stringify(T)===JSON.stringify(S[L]));if(e.some(A=>!R(A.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(A=>!R(A.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((A,S)=>l.push({name:String(S+1),dfRes:A.dfResidual,dev:A.deviance})),f=l.map((A,S)=>S===0?NaN:l[S-1].dfRes-A.dfRes),m=l.map((A,S)=>S===0?NaN:l[S-1].dev-A.dev),e.forEach((A,S)=>{if(S===0||f[S]===0)return;let[T,L]=f[S]>0?[e[S-1],A]:[A,e[S-1]],C=_=>_.filter((O,I)=>T.priorWeights[I]>0&&L.priorWeights[I]>0),q=T.model.offset.map((_,O)=>_-L.model.offset[O]),F=q.some((_,O)=>_!==0&&T.priorWeights[O]>0)?T.model.x.map((_,O)=>[..._,q[O]]):T.model.x;Cs(C(F),C(L.model.x))||h.add(S)}),a==="Rao"){let A=e.map(S=>({dfRes:S.dfResidual,dev:S.deviance,fit:Pu(S),x:S.model.x,intercept:S.model.intercept}));u=l.map((S,T)=>T===0?NaN:f[T]<0?-vc(A[T],A[T-1]):vc(A[T-1],A[T]))}d=e.reduce((A,S)=>S.dfResidual<A.dfResidual?S:A)}else{let E=s,R=E.model,A=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,S=R.intercept?[0]:[],T=C=>[...S,...Array.from({length:C},(q,F)=>F+S.length)],L=[];for(let C=0;C<=A;C++)C===A?L.push({dfRes:E.dfResidual,dev:E.deviance,fit:Pu(E),x:R.x,intercept:R.intercept}):C>0||a==="Rao"?L.push(w1(R,T(C),o)):L.push(void 0);l.push({name:"NULL",dfRes:E.dfNull,dev:E.nullDeviance});for(let C=1;C<=A;C++)l.push({name:`x${C}`,dfRes:L[C].dfRes,dev:L[C].dev});f=l.map((C,q)=>q===0?NaN:l[q-1].dfRes-C.dfRes),m=l.map((C,q)=>q===0?NaN:Math.max(l[q-1].dev-C.dev,0)),a==="Rao"&&(u=l.map((C,q)=>q===0?NaN:vc(L[q-1],L[q]))),d=E}e.forEach((E,R)=>{E.converged||o.add(`model ${R+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let w=new mt,g=n.dispersion??Qo(d,w);w.list.forEach(E=>o.add(E));let b=c?1/0:d.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=l.map((E,R)=>t?[E.dfRes,E.dev,f[R],m[R]]:[f[R],m[R],E.dfRes,E.dev]),M=[...N],p=E=>l.map((R,A)=>{if(A===0||f[A]===0||h.has(A))return NaN;let S=Le(E[A]/g*Math.sign(f[A]),-2*r);return S<0||Number.isNaN(S)?NaN:g1(S,Math.abs(f[A]))}),y=[];if(a==="Chisq"||a==="LRT")M.push("Pr(>Chi)"),y=[p(m)];else if(a==="Rao")M.push("Rao","Pr(>Chi)"),y=[u.map((E,R)=>h.has(R)?NaN:E),p(u)];else if(a==="F"){if(c&&o.add(`using F test with a '${i.family}' family is inappropriate`),b!==0){let E=l.map((R,A)=>{let S=Le(m[A]/f[A]/g,-2*r);return A===0||f[A]===0||h.has(A)||S<0?NaN:S});M.push("F","Pr(>F)"),y=[E,E.map((R,A)=>Number.isNaN(R)?NaN:dt(R,Math.abs(f[A]),b,!1,!1))]}}else if(a==="Cp"){let E=d.model.x.length;M.push("Cp"),y=[l.map(R=>R.dev+2*(E-R.dfRes)*g)]}let x=v.map((E,R)=>[...E,...y.map(A=>A[R])]);return{colNames:M,rowNames:l.map(E=>E.name),table:x,warnings:o.list}}function Pu(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 y1=["type"];function Vu(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let i=e.y.flatMap((d,w)=>e.weights===void 0||e.weights[w]!==0?[w]:[]),c=i.map(d=>e.weights===void 0?1:Math.sqrt(e.weights[d])),a=At(i.map((d,w)=>s.map(g=>e.x[d][g]*c[w]))),l=fa(e.weights===void 0?void 0:i.map(d=>P(e.weights[d],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=to(f.map(d=>i.map((w,g)=>Tt(l[g],e.x[w][s[d]]))),i.map((d,w)=>Tt(l[w],P(e.y[d],-r)))),u=[0,0],h=0;return f.forEach((d,w)=>{d>=t.length&&(u=he(u,_e(m[w],m[w])),h++)}),{ss:u[0],df:h}}function yc(e,t={}){if(K(t,y1,"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=Js([e]),i=ea([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=Ti(e,s),a=y=>{let x=c.map(([A,S])=>[P(A,-y),S]),E=x.reduce((A,[S])=>A+S,0),R=x.reduce((A,[S,T])=>A+S*T,0)/E;return Number.isFinite(E)&&Number.isFinite(R)?x.reduce((A,[S,T])=>A+S*(T-R)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=ta(c),f=a(l)),xr(c,i,l)||i===0)throw new Error("residual sum of squares is 0 (within rounding error)");let m=[...e.warnings],u=e.termLabels.length;r===2&&u===0&&e.intercept&&(m.push("the model contains only an intercept: Type III test substituted"),r=3);let h=y=>e.assign.flatMap((x,E)=>y.has(x)?[E]:[]),d=[...new Set(e.assign)],w=[],g=[],b=y=>{w.length=0;let x=[];if(r===3){if(e.coefficients.some(E=>Number.isNaN(E)))throw new Error("there are aliased coefficients in the model");for(let E of d){let R=h(new Set([E])),A=h(new Set(d.filter(S=>S!==E)));w.push(E===0?"(Intercept)":e.termLabels[E-1]),x.push({ss:Vu(e,A,R,s,y).ss,df:R.length})}}else{let E=R=>e.factors.flatMap((A,S)=>A[R-1]>0?[S]:[]);for(let R of d){if(R===0)continue;let A=E(R),S=d.filter(C=>C!==0&&C!==R&&A.every(q=>e.factors[q][C-1]>0)),T=h(new Set(d.filter(C=>C!==R&&!S.includes(C)))),L=Vu(e,T,h(new Set([R])),s,y);w.push(e.termLabels[R-1]),x.push(L)}}return x};g=b(l),l===0&&e.weights!==void 0&&g.some(y=>!Number.isFinite(y.ss))&&(l=ta(c),g=b(l));let N=y=>P(y,2*s+l),v=P(i,-l),M=v/o,p=g.map(({ss:y,df:x})=>{let E=y/x/M;return[N(y),x,E,dt(E,x,o,!1,!1)]});return p.push([N(v),o,NaN,NaN]),X("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...w,"Residuals"],table:p,warnings:m},e)}var wa=[0,0];var Nc=e=>e.map(t=>t.map(n=>n[0]));function $u(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function Lr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(wa);return r.forEach((s,i)=>{let c=t[i];for(let a=0;a<n;a++)o[a]=he(o[a],_e(s,c[a]))}),o})}function Cr(e){let t=wa;return e.forEach((n,r)=>t=he(t,n[r])),t}function zu(e){let t=e.length,n=e.map(i=>i.slice()),r=Array.from({length:t},(i,c)=>c),o=1,s=!1;for(let i=0;i<t;i++){let c=i;for(let a=i+1;a<t;a++)Math.abs(n[a][i][0])>Math.abs(n[c][i][0])&&(c=a);if(n[c][i][0]===0){s=!0;continue}c!==i&&([n[i],n[c]]=[n[c],n[i]],[r[i],r[c]]=[r[c],r[i]],o=-o);for(let a=i+1;a<t;a++){let l=at(n[a][i],n[i][i]);n[a][i]=l;for(let f=i+1;f<t;f++)n[a][f]=De(n[a][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}=zu(e);if(r)return wa;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}=zu(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(wa));for(let i=0;i<t;i++){let c=r.map(a=>[a===i?1:0,0]);for(let a=0;a<t;a++)for(let l=0;l<a;l++)c[a]=De(c[a],_e(n[a][l],c[l]));for(let a=t-1;a>=0;a--){for(let l=a+1;l<t;l++)c[a]=De(c[a],_e(n[a][l],c[l]));c[a]=at(c[a],n[a][a])}for(let a=0;a<t;a++)s[a][i]=c[a]}return s}var N1=["group","idata","M","X"],Ku=(e,t)=>yt(e,t/2,2,!1,!1);function Mc(e){let t=e.length,n=lt(Cr(e),t);return Wu(e.map(r=>r.map(o=>at(o,n))))[0]}function Ec(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=Ku(i,c),l=Ku(i,c+4);return a+s*(l-a)}var M1=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function E1(e,t,n){let r=e.length,o=b=>b.length===0?0:At(Array.from({length:r},(N,v)=>b.map(M=>M[v]))).rank,s=n.length===0?void 0:At(Array.from({length:r},(b,N)=>n.map(v=>v[N]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(b=>n[b]),c=i.length,a=[...i,...t],l=At(Array.from({length:r},(b,N)=>a.map(v=>v[N])));if(l.rank>o(t))throw new Error("X does not define a subspace of M");let f=l.pivot.slice(0,l.rank).map(b=>a[b]),m=b=>b.map(N=>[N,0]),u=new Array(r).fill([0,0]),h=Kn(f.map(m),u,e.map((b,N)=>e.map(v=>v[N]))).qtv,d=Array.from({length:r},(b,N)=>h.map(v=>v[N])),w=Kn(f.map(m),u,d).qtv,g=Array.from({length:l.rank-c},(b,N)=>c+N);return g.map(b=>g.map(N=>w[b][N]))}function x1(e,t={}){K(t,N1,"mauchlyTest");let n=e.map(p=>Array.from(p)),r=n[0]?.length??0;if(r===0||n.some(p=>p.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=p=>p==null||typeof p=="number"&&Number.isNaN(p),i=n.map((p,y)=>!p.some(s)&&(o===void 0||!s(o[y])));if(n.some((p,y)=>i[y]&&p.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=Se(n.filter((p,y)=>i[y])).groups,a=c.length,l=o?.filter((p,y)=>i[y]).map(ut)??c.map(()=>""),f=[...new Set(l)].sort(wn),m=c.map(p=>p.map(()=>[0,0]));for(let p of f){let y=l.flatMap((x,E)=>x===p?[E]:[]);for(let x=0;x<r;x++){let E=[0,0];for(let A of y)E=he(E,[c[A][x],0]);let R=lt(E,y.length);for(let A of y)m[A][x]=De([c[A][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},(p,y)=>Array.from({length:r},(x,E)=>{let R=[0,0];for(let A of m)R=he(R,_e(A[y],A[E]));return R})),d=t.idata??{index:Array.from({length:r},(p,y)=>y+1)},w=p=>{let y=rt(()=>Gr(p,d));if(y.x.length!==r)throw new RangeError("idata must have one row per column of y");return M1(y.x,y.colNames.length)},g=t.M===void 0?Array.from({length:r},(p,y)=>Array.from({length:r},(x,E)=>E===y?1:0)):w(t.M),b=w(t.X??"~0"),N=E1(h,g,b),v=N.length;if(v===0)throw new RangeError("X spans M: the test has no dimensions");if(v===1)throw new RangeError("one tested dimension: sphericity holds trivially and the test has no degrees of freedom");if(u<v)throw new RangeError(`too few residual degrees of freedom for the test: ${u} for ${v} dimension(s)`);let M=Mc(N);return{statistic:M,statisticName:"W",pValue:Ec(M,v,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var R1=["n","d","type","sigLevel","power","alternative"],A1=["n1","n2","d","sigLevel","power","alternative"],S1=["h","n","sigLevel","power","alternative"],T1=["h","n1","n2","sigLevel","power","alternative"],k1=["h","n","sigLevel","power","alternative"],q1=["d","n","sigLevel","power","alternative"],I1=["n","r","sigLevel","power","alternative"],_1=["w","N","df","sigLevel","power"],L1=["k","n","f","sigLevel","power"],C1=["u","v","f2","sigLevel","power"],F1=["size"],O1={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 Uu(e="p",...t){K(Ur(t),F1,"cohenES");let n=Br(["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:O1[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function D1(e,t){if(!ba(e)||!ba(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 P1(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(ba))throw new RangeError("'P0' and 'P1' must be probabilities in [0, 1]");let n=0;for(let r=0;r<e.length;r++)(e[r]!==0||t[r]!==0)&&(n+=Vt(t[r]-e[r],e[r]));return Math.sqrt(n)}function V1(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(ba)))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>=$1)o+=Vt(e[i][s]-c,c);else{let a=Math.sqrt(t[i]),l=Math.sqrt(r[s]),f=e[i][s]/a/l-a*l;o+=f*f}}return Math.sqrt(o)}var $1=2**-1022,ba=e=>e>=0&&e<=1,z1="\u2018sig.level\u2019 must be numeric in [0, 1]",W1="\u2018power\u2019 must be numeric in [0, 1]";function fr(e,t){let n=r=>!Ne(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(z1);if(n(t))throw new RangeError(W1)}function mr(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?Uu(t,e).effectSize:e}var ns=["two.sided","less","greater"];function va(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Hu(e,t,n,r){if(r==="less")return Nr(Wt(n,e),e,t,!0);if(r==="greater")return Nr(Wt(n,e,{lowerTail:!1}),e,t,!1);let o=Wt(n/2,e,{lowerTail:!1});return Nr(o,e,t,!1)+Nr(-o,e,t,!0)}var Bu=e=>e<1e-8?le(e,{lowerTail:!1}):le(1-e);function Yu(e,t,n){return n==="greater"?se(Bu(t)-e,{lowerTail:!1}):n==="less"?se(le(t)-e):se(Bu(t/2)-e,{lowerTail:!1})+se(le(t/2)-e)}function j1(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*=P(r,-o),n+=o}return P(t,n)}function K1(e={}){K(e,R1,"pwrTTest");let{n:t,power:n}=e,r=mr(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),fr(o,n),!Ne(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,ns),c=s==="two.sample"?2:1;i==="two.sided"&&!Ne(r)&&(r=Math.abs(r));let a=(m,u,h)=>Hu((m-1)*c,Math.sqrt(m/c)*u,h,i);if(Ne(n))n=a(t,r,o);else{let m=n;Ne(t)?t=Ye(u=>a(u,r,o)-m,2+1e-10,1e9):Ne(r)?r=Ye(u=>a(t,u,o)-m,...va(i,[1e-7,10])):o=Ye(u=>a(t,r,u)-m,1e-10,1-1e-10)}let l=s==="paired"?"n is number of *pairs*":s==="two.sample"?"n is number in *each* group":void 0,f={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[s]+" t test power calculation";return{n:t,d:r,sigLevel:o,power:n,alternative:i,...l===void 0?{}:{note:l},method:f,warnings:[]}}function B1(e={}){K(e,A1,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=mr(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),fr(s,r),!Ne(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!Ne(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Te(e.alternative,ns);i==="two.sided"&&!Ne(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>Hu(a+l-2,f/Math.sqrt(1/a+1/l),m,i);if(Ne(r))r=c(t,n,o,s);else{let a=r;Ne(t)?t=Ye(l=>c(l,n,o,s)-a,2+1e-10,1e9):Ne(n)?n=Ye(l=>c(t,l,o,s)-a,2+1e-10,1e9):Ne(o)?o=Ye(l=>c(t,n,l,s)-a,...va(i,[1e-7,10])):s=Ye(l=>c(t,n,o,l)-a,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:s,power:r,alternative:i,method:"t test power calculation",warnings:[]}}function xc(e,t,n,r,o,s){let{n:i,power:c}=t,a=mr(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(nn([a,i,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),fr(l,c),!Ne(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=Te(t.alternative,ns);f==="two.sided"&&!Ne(a)&&(a=Math.abs(a));let m=(u,h,d)=>Yu(u*r(h),d,f);if(Ne(c))c=m(a,i,l);else{let u=c;Ne(a)?a=Ye(h=>m(h,i,l)-u,...va(f,[s,10])):Ne(i)?i=Ye(h=>m(a,h,l)-u,o,1e9):l=Ye(h=>m(a,i,h)-u,1e-10,1-1e-10)}return{es:a,n:i,sigLevel:l,power:c,alternative:f}}function U1(e={}){K(e,S1,"pwr2pTest");let t=xc(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 H1(e={}){K(e,k1,"pwrPTest");let t=xc(e.h,e,"h",n=>Math.sqrt(n),2+1e-10,1e-10);return{h:t.es,n:t.n,sigLevel:t.sigLevel,power:t.power,alternative:t.alternative,method:"proportion power calculation for binomial distribution (arcsine transformation)",warnings:[]}}function Y1(e={}){K(e,q1,"pwrNormTest");let t=xc(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 G1(e={}){K(e,T1,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=mr(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),fr(s,r),!Ne(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!Ne(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Te(e.alternative,ns);i==="two.sided"&&!Ne(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)=>Yu(l*Math.sqrt(c(f,m)),u,i);if(Ne(r))r=a(o,t,n,s);else{let l=r;Ne(o)?o=Ye(f=>a(f,t,n,s)-l,...va(i,[1e-10,10])):Ne(t)?t=Ye(f=>a(o,f,n,s)-l,2+1e-10,1e9):Ne(n)?n=Ye(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=Ye(f=>a(o,t,n,f)-l,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:s,power:r,alternative:i,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes",warnings:[]}}function X1(e={}){K(e,I1,"pwrRTest");let{n:t,power:n}=e,r=mr(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),fr(o,n),!Ne(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!Ne(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=Te(e.alternative,ns);s==="two.sided"&&!Ne(r)&&(r=Math.abs(r));let i=(a,l,f,m)=>{let u=Wt(m?f/2:f,a-2,{lowerTail:!1}),h=u**2+a-2,d=h===1/0&&Number.isFinite(u)?1/Math.sqrt(1+(a-2)/u/u):Math.sqrt(u**2/h),w=Math.atanh(l)+l/(2*(a-1)),g=Math.atanh(d),b=se((w-g)*Math.sqrt(a-3));return m?b+se((-w-g)*Math.sqrt(a-3)):b},c=(a,l,f)=>s==="two.sided"?i(a,l,f,!0):i(a,s==="less"?-l:l,f,!1);if(Ne(n))n=c(t,r,o),s==="less"&&(r=-r);else{let a=n;if(Ne(t))t=Ye(l=>c(l,r,o)-a,4+1e-10,1e9);else if(Ne(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=Ye(m=>c(t,m,o)-a,l,f)}else o=Ye(l=>c(t,r,l)-a,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:s,method:"approximate correlation power calculation (arctangh transformation)",warnings:[]}}function Q1(e){K(e,_1,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=mr(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([o,t,n,s],"exactly one of w, N, power, and sig.level must be NULL"),typeof r!="number")throw new TypeError('argument "df" is missing, with no default');if(!(r>0&&r<1/0))throw new RangeError("'df' must be a positive finite number");if(fr(s,n),!Ne(o)&&!(o>=0))throw new RangeError("w must be positive");if(!Ne(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:_t(On(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(Ne(n))n=i(o,t,s);else{let c=n;Ne(o)?o=Ye(a=>i(a,t,s)-c,1e-10,1e9):Ne(t)?t=Ye(a=>i(o,a,s)-c,1+1e-10,1e9):s=Ye(a=>i(o,t,a)-c,1e-10,1-1e-10)}return{w:o,N:t,df:r,sigLevel:s,power:n,method:"Chi squared power calculation",note:"N is the number of observations",warnings:[]}}function Z1(e={}){K(e,L1,"pwrAnovaTest");let{k:t,n,power:r}=e,o=mr(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),fr(s,r),!Ne(o)&&!(o>=0))throw new RangeError("f must be positive");if(!Ne(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Ne(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=j1([c,a,l,l])),h===1/0?1:Vs(m,u)?$s(m,u,h,f):Tn(Yn(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(Ne(r))r=i(t,n,o,s);else{let c=r;Ne(t)?t=Ye(a=>i(a,n,o,s)-c,2+1e-10,100):Ne(n)?n=Ye(a=>i(t,a,o,s)-c,2+1e-10,1e9):Ne(o)?o=Ye(a=>i(t,n,a,s)-c,1e-7,1e7):s=Ye(a=>i(t,n,o,a)-c,1e-10,1-1e-10)}return{k:t,n,f:o,sigLevel:s,power:r,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}function J1(e={}){K(e,C1,"pwrF2Test");let{u:t,v:n,power:r}=e,o=mr(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),fr(s,r),!Ne(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!Ne(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!Ne(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:Vs(c,a)?$s(c,a,m,f):Tn(Yn(f,c,a,{lowerTail:!1}),c,a,{ncp:m,lowerTail:!1})};if(Ne(r))r=i(t,n,o,s);else{let c=r;Ne(t)?t=Ye(a=>i(a,n,o,s)-c,1+1e-10,100):Ne(n)?n=Ye(a=>i(t,a,o,s)-c,1+1e-10,1e9):Ne(o)?o=Ye(a=>i(t,n,a,s)-c,1e-7,1e7):s=Ye(a=>i(t,n,o,a)-c,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:s,power:r,method:"Multiple regression power calculation",warnings:[]}}var eb=["id","dv","between","within","type","correction","es","observed"],ya=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],tb=1e-12,Gu=1e-24;function Xu(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 rs=e=>typeof e=="boolean"?e?"TRUE":"FALSE":ut(e);function nb(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(rs):[...new Set(t.map(rs))].sort(wn)}var Qu=(e,t)=>t.split(":").includes(e);function Zu(e,t,n,r,o){let s=l=>r.some(f=>Qu(f,l));for(let l of r)if(!e.some(f=>Qu(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 rb(e,t){K(t,eb,"aovEz");let n=ya(t.between),r=ya(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[W,U]of[["between",n],["within",r]]){let j=U.filter((te,fe)=>U.indexOf(te)!==fe);if(j.length>0)throw new RangeError(`duplicated factor names in '${W}': ${[...new Set(j)].join(", ")}`)}let o=n.filter(W=>r.includes(W));if(o.length>0)throw new RangeError(`factors in both 'between' and 'within': ${o.join(", ")}`);let s=t.type??3;if(s!==2&&s!==3)throw new RangeError(`invalid type (${String(s)}): use 2 or 3`);let i=t.correction??"GG";if(!["GG","HF","none"].includes(i))throw new RangeError(`invalid 'correction': ${String(i)}`);let c=ya(t.es??"ges");for(let W of c)if(W!=="ges"&&W!=="pes")throw new RangeError(`invalid 'es': ${String(W)}`);let a=ya(t.observed),l=[],f=W=>W==null||typeof W=="number"&&Number.isNaN(W),m=Xu(e,t.dv),u=[t.id,...n,...r].map(W=>Xu(e,W));for(let W of u)if(W.length!==m.length)throw new RangeError("the columns must have the same length");let h=m.map((W,U)=>u.every(j=>!f(j[U]))),d=W=>W.filter((U,j)=>h[j]),w=kt(d(u[0])).map(rs),g=d(m).map(W=>{if(f(W))return NaN;if(typeof W!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(W))throw new RangeError(`the response '${t.dv}' must be finite`);return W}),b=n.map((W,U)=>kt(d(u[1+U]))),N=r.map((W,U)=>kt(d(u[1+n.length+U]))),v=w.length;for(let W of[g,...b,...N])if(W.length!==v)throw new RangeError("the columns must have the same length");let M=b.map(W=>nb(W)),p=b.map(W=>W.map(rs)),y=N.map(W=>W.map(rs)),x=y.map(W=>[...new Set(W)]);M.forEach((W,U)=>{if(W.length<2)throw new Error(`Factor "${n[U]}" consists of one level only. Remove factor from model?`)}),x.forEach((W,U)=>{if(W.length<2)throw new Error(`Factor "${r[U]}" consists of one level only. Remove factor from model?`)});let E=[...new Set(w)],R=new Map,A=new Set;if(w.forEach((W,U)=>{let j=JSON.stringify(p.map(fe=>fe[U])),te=R.get(W);te===void 0?R.set(W,j):te!==j&&A.add(W)}),A.size>0)throw new Error(`Following ids are in more than one between subjects condition:
|
|
8
|
+
${E.filter(W=>A.has(W)).join(", ")}`);let S=[[]];for(let W of x)S=S.flatMap(U=>W.map(j=>[...U,j]));let T=W=>JSON.stringify(y.map(U=>U[W])),L=new Map(S.map((W,U)=>[JSON.stringify(W),U])),C=new Set(w.map((W,U)=>T(U)));if(S.some(W=>!C.has(JSON.stringify(W))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let q=new Map,F=new Set;w.forEach((W,U)=>{let j=q.get(W);j===void 0&&q.set(W,j=new Set),j.add(T(U)),Number.isNaN(g[U])&&F.add(W)});let _=E.filter(W=>F.has(W)||q.get(W).size<S.length),O=new Set(_),I=Se([g.filter((W,U)=>!O.has(w[U]))]).k,D=I===0?g:g.map(W=>P(W,-I)),k=W=>P(W,2*I),V=S.length,B=new Map;if(w.forEach((W,U)=>{let j=B.get(W);j===void 0&&B.set(W,j={s:S.map(()=>[0,0]),n:S.map(()=>0)});let te=L.get(T(U));j.s[te]=he(j.s[te],[D[U],0]),j.n[te]++}),[...B.values()].some(W=>W.n.some(U=>U>1))&&l.push("More than one observation per design cell, aggregating data using `fun_aggregate = mean`.\nTo turn off this warning, pass `fun_aggregate = mean` explicitly."),_.length>0){let W=[..._].sort(wn),U=W.length>=10?`${W.slice(0,10).join(", ")}, ... [showing first 10 only]`:W.join(", ");l.push(`Missing values for ${_.length} ID(s), which were removed before analysis:
|
|
9
|
+
${U}`)}let Q=E.filter(W=>!_.includes(W)),re=new Map;w.forEach((W,U)=>re.has(W)||re.set(W,U));let z=Q.map(W=>{let U=B.get(W);return U.s.map((j,te)=>lt(j,U.n[te]))}),J={};n.forEach((W,U)=>J[W]=Q.map(j=>p[U][re.get(j)]));let Z=Object.fromEntries(n.map(W=>[W,"contr.sum"])),ke=Object.fromEntries(n.map((W,U)=>[W,M[U]])),ne="Rank deficient model matrix; insufficient data to estimate full model.";if(V===1)return X("aovEz",$());return X("aovEz",Y());function $(){let W="y";for(;n.includes(W);)W+="_";let U={...J,[W]:z.map(me=>me[0][0])},j=rt(()=>Go(`${W} ~ ${n.length>0?n.join(" * "):"1"}`,U,{contrasts:Z,levels:ke}));if(j.rank<j.coefNames.length)throw new Error(ne);if(j.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${z.length} subject(s) for ${j.rank} parameter(s)`);let te=rt(()=>yc(j,{type:s}));l.push(...te.warnings.filter(me=>!l.includes(me)));let fe=j.dfResidual,Re=te.rowNames.flatMap((me,qe)=>{if(me==="(Intercept)"||me==="Residuals")return[];let[de,Fe,xe,Ve]=te.table[qe];return[{name:me,ss:de,numDf:Fe,errSS:j.rss,denDf:fe,f:xe,p:Ve}]}),Ce=te.colNames.indexOf("Sum Sq"),be={anova:Zu(Re,new Map([[0,j.rss]]),c,a,k),univariate:{colNames:te.colNames,rowNames:te.rowNames,table:te.table.map(me=>me.map((qe,de)=>de===Ce?k(qe):qe))},within:r,warnings:l};return Bt.set(be,{colNames:te.colNames,rowNames:te.rowNames,table:te.table}),be}function Y(){let W=Q.length,U=z.reduce((Me,ye)=>ye.reduce((Ee,ee)=>Ee+ee[0]*ee[0],Me),0),j=n.length>0?rt(()=>Gr(`~ ${n.join(" * ")}`,J,{contrasts:Z,levels:ke})):{colNames:["(Intercept)"],assign:[0],x:Q.map(()=>[1]),termLabels:[],factors:[]},te=j.colNames.length;if(W===0||At(j.x).rank<te)throw new Error(ne);let fe=W-te;if(fe<=0)throw new RangeError(`no residual degrees of freedom: ${W} subject(s) for ${te} between-subject parameter(s)`);let Re=[0,...j.termLabels.map((Me,ye)=>ye+1)],Ce=["(Intercept)",...j.termLabels],be=Me=>j.assign.flatMap((ye,Ee)=>Me.includes(ye)?[Ee]:[]),me=Me=>j.factors.flatMap((ye,Ee)=>ye[Me-1]>0?[Ee]:[]),qe=Me=>j.x.map(ye=>[ye[Me],0]),de=new Array(W).fill([0,0]),Fe=(Me,ye,Ee)=>{let{qtv:ee}=Kn([...Me,...ye].map(qe),de,Ee);return ee.map(ve=>ve.slice(Me.length,Me.length+ye.length))},xe=Me=>{let{qtv:ye}=Kn(j.colNames.map((Ee,ee)=>qe(ee)),de,Me);return ye.map(Ee=>Ee.slice(te))},Ve=Object.fromEntries(r.map((Me,ye)=>[Me,S.map(Ee=>Ee[ye])])),Oe=rt(()=>Gr(`~ ${r.join(" * ")}`,Ve,{contrasts:Object.fromEntries(r.map(Me=>[Me,"contr.sum"])),levels:Object.fromEntries(r.map((Me,ye)=>[Me,x[ye]]))})),Be=[0,...Oe.termLabels.map((Me,ye)=>ye+1)],ze=["(Intercept)",...Oe.termLabels],et=Me=>Me.map(ye=>Me.map(Ee=>{let ee=[0,0];return ye.forEach((ve,ie)=>ee=he(ee,_e(ve,Ee[ie]))),ee})),Ue=[],Xe=[],pe=[],oe=[],we=[],ge=[],ce=[],tt=new Map,G=!1;for(let Me of Be){let ye=Oe.assign.flatMap((We,je)=>We===Me?[je]:[]),Ee=ye.length,ee=ye.map(We=>z.map(je=>je.reduce((xt,Rt,Sn)=>he(xt,Tt(Rt,Oe.x[Sn][We])),[0,0]))),ve=ye.map(We=>ye.map(je=>[Oe.x.reduce((xt,Rt)=>xt+Rt[We]*Rt[je],0),0])),ie=ju(ve),Ae=et(xe(ee)),Ke=Lr(Ae,ie),$e=Cr(Ke)[0],ot=NaN,ht=NaN,Ct=NaN,Ut=NaN,Hn=!1;if(Ee>=2){let We=ze[Me],je=Bl(Nc(ie)).map(Ht=>Ht.map(on=>[on,0])),xt=Nc(Lr(Lr(je,Ae),$u(je))),Rt=jt(xt,{symmetric:!0,onlyValues:!0}).values,Sn=Rt.reduce((Ht,on)=>Math.max(Ht,on),0),$t=Math.max(Sn*tb,U*Gu),It=Cr(Ke),pt=Cr(Lr(Ke,Ke));Sn>U*Gu&&pt[0]>0&&Number.isFinite(pt[0])?(Hn=!0,ot=at(_e(It,It),Tt(pt,Ee))[0],fe-Ee*ot>0?ht=((fe+1)*Ee*ot-2)/(Ee*(fe-Ee*ot)):l.push(`The Huynh-Feldt epsilon of ${We} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${We} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),Hn&&Rt.every(Ht=>Ht>$t)?(Ct=Mc(Lr(ie,Ae)),Ut=Ec(Ct,Ee,fe,V)):Hn&&l.push(`The error SSP matrix of ${We} is singular: no Mauchly test (the epsilons are still defined)`)}for(let We=0;We<Re.length;We++){let je=Re[We],xt;if(s===3)xt=Fe(be(Re.filter(Ft=>Ft!==je)),be([je]),ee);else if(je===0)xt=Fe([],be([0]),ee);else{let Ft=me(je),Jt=Re.filter(H=>H!==0&&H!==je&&Ft.every(ae=>j.factors[ae][H-1]>0));xt=Fe(be(Re.filter(H=>H!==je&&!Jt.includes(H))),be([je]),ee)}let Rt=Cr(Lr(et(xt),ie))[0],$t=(je===0?1:be([je]).length)*Ee,It=fe*Ee,pt=Rt/$t/($e/It),Ht=dt(pt,$t,It,!1,!1),on=Me===0?Ce[We]:je===0?ze[Me]:`${Ce[We]}:${ze[Me]}`;Ue.push([k(Rt),$t,k($e),It,pt,Ht]),Xe.push([Rt,$t,$e,It,pt,Ht]),pe.push(on),tt.set(Me,$e);let Zt={name:on,ss:Rt,numDf:$t,errSS:$e,denDf:It,f:pt,p:Ht};if(Hn){let Ft=Math.min(1,ht),Jt=dt(pt,ot*$t,ot*It,!1,!1),H=dt(pt,Ft*$t,Ft*It,!1,!1);oe.push([ot,Jt,ht,H]),we.push([Ct,Ut]),ge.push(on),i==="GG"?Zt={...Zt,numDf:ot*$t,denDf:ot*It,p:Jt}:i==="HF"&&(ht>1&&(G=!0),Zt=Number.isNaN(ht)?{...Zt,numDf:ot*$t,denDf:ot*It,p:Jt}:{...Zt,numDf:Ft*$t,denDf:Ft*It,p:H})}Me===0&&je===0||ce.push(Zt)}}G&&l.push("HF eps > 1 treated as 1");let ue={anova:Zu(ce,tt,c,a,k),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:pe,table:Ue},within:r,warnings:l};return oe.length>0&&(ue.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:ge,table:oe},ue.sphericity={colNames:["Test statistic","p-value"],rowNames:[...ge],table:we}),Bt.set(ue,{colNames:ue.univariate.colNames,rowNames:pe,table:Xe}),ue}}function io(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(s=>Number.isFinite(s)))).sort((s,i)=>s-i);if(n.length<2)return e.slice();let r=it(n.map(Math.abs)),o=[n[0]];for(let s=0;s+1<n.length;s++){let i=n[s+1]-n[s];i<=t||i/r<=t||o.push(n[s+1])}return e.map(s=>{if(!Number.isFinite(s))return s;let i=0,c=o.length-1;for(;i<c;){let a=Math.floor((i+c+1)/2);o[a]<=s?i=a:c=a-1}return o[i]})}var ob=["rho"],sb=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),Ac=e=>e==null||typeof e=="number"&&Number.isNaN(e);function ab(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&&Ac(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=kt(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=io(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=${ut(a)}`)}}function ib(e,t,n,r={}){K(r,ob,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=ab(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,d=1;for(let v=0;v<s.length;){let M=s[v].time,p=v,y=new Array(c).fill(0),x=new Array(c).fill(0),E=0;for(;p<s.length&&s[p].time===M;){let R=s[p];x[R.group]++,R.event&&(y[R.group]++,E++),p++}if(E>0){let R=o===0?1:d**o;for(let A=0;A<c;A++)l[A]+=R*y[A],f[A]+=R*E*u[A]/h;if(h>1){let A=R*R*E*(h-E)/(h*(h-1));for(let S=0;S<c;S++)for(let T=0;T<c;T++)m[S][T]+=A*u[S]*((S===T?1:0)-u[T]/h)}d*=1-E/h}for(let R=0;R<c;R++)u[R]-=x[R];h-=p-v,v=p}let w=f.map((v,M)=>v>0?M:-1).filter(v=>v>=0),g=0,b=Math.max(w.length-1,0);if(w.length>=2){let v=w.slice(1),M=v.map(y=>l[y]-f[y]),p=v.map(y=>v.map(x=>m[y][x]));try{let y=nt(p,M);g=M.reduce((x,E,R)=>x+E*y[R],0)}catch{({chisq:g,df:b}=cb(p,M))}}let N=b===0?NaN:_t(g,b,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:g,df:b,pvalue:N,pValue:N,warnings:[]}}function cb(e,t){let n=jt(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 Rc(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 tn(e[r],e[s])}return e[r]}function ur(e,t,n={}){K(n,sb,"survfit");let{group:r,start:o,id:s,weights:i,ctype:c=1,stype:a=1,robust:l=!1,rmean:f="common",confType:m="log",confInt:u=.95}=n;if(typeof m!="string")throw new TypeError("confType must be a string");let h=["log","log-log","plain","none","logit","arcsin"],d=typeof m=="string"?h.filter(k=>k.startsWith(m)):[],w=h.includes(m)?m:d.length===1?d[0]:m;if(typeof c!="number")throw new TypeError("ctype must be a number");if(typeof a!="number")throw new TypeError("stype must be a number");if(c!==1&&c!==2)throw new RangeError("ctype must be 1 or 2");if(a!==1&&a!==2)throw new RangeError("stype must be 1 or 2");if(!h.some(k=>k===w))throw new RangeError("'arg' should be one of \u201Clog\u201D, \u201Clog-log\u201D, \u201Cplain\u201D, \u201Cnone\u201D, \u201Clogit\u201D, \u201Carcsin\u201D");if(typeof u!="number")throw new TypeError("confInt must be a number");if(!(u>0&&u<1))throw new RangeError("confidence intervals must be between 0 and 1");if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(r!==void 0&&r.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(o!==void 0&&o.length!==e.length)throw new RangeError("'start' must have the same length as 'time'");if(s!==void 0&&s.length!==e.length)throw new RangeError("'id' must have the same length as 'time'");if(s!==void 0&&o===void 0)throw new RangeError("'id' requires 'start'");if(i!==void 0&&!Array.isArray(i))throw new TypeError("'weights' must be an array");if(i!==void 0&&i.length!==e.length)throw new RangeError("'weights' must have the same length as 'time'");if(typeof l!="boolean")throw new TypeError("'robust' must be a boolean");if(typeof f!="number"&&typeof f!="string")throw new TypeError("rmean must be a number or string");if(typeof f=="number"&&!Number.isFinite(f))throw new RangeError("'rmean' must be finite");if(typeof f!="number"&&f!=="none"&&f!=="common"&&f!=="individual")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Ccommon\u201D, \u201Cindividual\u201D");let g=[];for(let k=0;k<e.length;k++){let V=i?.[k];if(i&&V!=null&&typeof V!="number")throw new TypeError("weights must contain numbers");if(i&&(V==null||Number.isNaN(V)))continue;let B=V??1;if(!Number.isFinite(B))throw new RangeError("weights must be finite");if(B<0)throw new RangeError("weights must be non-negative");if(!Number.isSafeInteger(B))throw new RangeError("weights must be non-negative safe integers");if(e[k]!=null&&typeof e[k]!="number")throw new TypeError("'time' must contain numbers");if(t[k]!=null&&typeof t[k]!="number")throw new TypeError("'status' must contain numbers");if(o!==void 0&&o[k]!=null&&typeof o[k]!="number")throw new TypeError("'start' must contain numbers");if(!(e[k]==null||t[k]==null||Number.isNaN(e[k])||Number.isNaN(t[k])||o!==void 0&&(o[k]==null||Number.isNaN(o[k]))||r!==void 0&&Ac(r[k])||s!==void 0&&Ac(s[k]))){if(o!==void 0&&!(o[k]<e[k]))throw new RangeError("stop time must be > start time");if(t[k]!==0&&t[k]!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[k]===-1/0||e[k]===1/0&&t[k]===1)throw new RangeError("event times must be finite");g.push(k)}}if(g.length===0)throw new RangeError("no observations without missing values");let b=r===void 0?[]:kt(r),N=s===void 0?[]:Array.from(s),v=r===void 0?["all"]:Array.from(new Set(g.map(k=>b[k]))).sort((k,V)=>typeof k=="number"&&typeof V=="number"?k-V:String(k)<String(V)?-1:String(k)>String(V)?1:0).map(k=>`group=${ut(k)}`),M=new Map;r!==void 0&&Array.from(new Set(g.map(k=>b[k]))).sort((k,V)=>typeof k=="number"&&typeof V=="number"?k-V:String(k)<String(V)?-1:String(k)>String(V)?1:0).forEach((k,V)=>M.set(k,V));let p=new Map;s!==void 0&&Array.from(new Set(g.map(k=>N[k]))).forEach((k,V)=>p.set(k,V));let y=g.flatMap(k=>o===void 0?[e[k]]:[o[k],e[k]]),x=io(y),E=new Map,R=new Map;g.forEach((k,V)=>{if(o===void 0)R.set(k,x[V]);else if(E.set(k,x[2*V]),R.set(k,x[2*V+1]),!(E.get(k)<R.get(k)))throw new Error("aeqSurv exception, an interval has effective length 0")});let A=g.map(k=>({time:R.get(k),start:o===void 0?-1/0:E.get(k),event:t[k]===1,group:r===void 0?0:M.get(b[k]),id:s===void 0?k:p.get(N[k]),weight:i?.[k]??1})).sort((k,V)=>k.time-V.time),S=s===void 0?void 0:new Map;if(S)for(let k of A){let V=S.get(k.id)??[];V.push(k),S.set(k.id,V)}let T=v,L=A.map(k=>k.time),C=Math.min(...L),q=Math.min(0,C);if(typeof f=="number"&&f<=q)throw new RangeError("'rmean' must be greater than the integration origin");let F=(1-u)/2,_=F<1e-8?le(F,{lowerTail:!1}):le(1-F),O=[],I={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:O,stdError:O,lower:[],upper:[],cumhaz:[],stdChaz:[],logse:!l||c===2,n:[],records:[],nMax:[],nStart:[],events:[],rmean:[],seRmean:[],median:[],medianRows:T,table:{colNames:[],rowNames:T,table:[]},warnings:[]};i!==void 0&&(I.counts={nRisk:[],nEvent:[],nCensor:[]});let D=[];for(let k=0;k<T.length;k++){let V=A.filter(pe=>pe.group===k);if(!V.some(pe=>pe.weight>0))throw new Error("a group has no positive weight");let B=I.time.length,Q=V.length,re=Math.max(...V.map(pe=>pe.time)),z=0,J=0,Z=1,ke=0,ne=0,$=0;if(o===void 0){let pe=0;for(let oe of V)if(pe+=oe.weight,!Number.isSafeInteger(pe))throw new RangeError("total case weight per group must be a safe integer");z=pe,J=V.length}let Y=[],W=[],U=[],j=s===void 0?void 0:new Map;if(j)for(let pe of V){let oe=j.get(pe.id)??[];oe.push(pe),j.set(pe.id,oe)}for(let pe=0;pe<V.length;){let oe=V[pe].time,we=pe,ge=0,ce=0,tt=0,G=J;for(;we<V.length&&V[we].time===oe;)we++;let ue=V.slice(pe,we);if(o!==void 0){let We=V.filter(je=>je.start<oe&&je.time>=oe);if(z=We.reduce((je,xt)=>je+xt.weight,0),J=We.length,G=J,!Number.isSafeInteger(z))throw new RangeError("total case weight at risk must be a safe integer")}let Me=ue.filter(We=>We.event),ye=ue.filter(We=>!We.event&&(S===void 0||!(S.get(We.id)??[]).some(je=>je.start===oe&&je.time>oe)));for(let We of Me)ge+=We.weight,ce++;for(let We of ye)tt+=We.weight;let Ee=Me.length,ee=ye.length;if(s!==void 0&&Me.length===0&&ye.length===0){pe=we;continue}let ve=0,ie=0;if(ge>0){if(c===1)ve=ge/z,ie=ge/(z*z);else for(let We=0;We<ce;We++){let je=ge/ce,xt=z-We*je;ve+=je/xt,ie+=je/(xt*xt)}ke+=ve,$+=ie,a===1?(Z*=1-ge/z,ne+=ge<z?ge/(z*(z-ge)):1/0):Z=Math.exp(-ke),U.push({t:oe,d:ge,n:z,e:ce,S:Z})}let Ae=Math.sqrt(a===1?ne:$),Ke=0,$e=0;if(l){let We=0,je=0,xt=j===void 0?V:Array.from(j.values(),Rt=>Rt[0]);for(let Rt of xt){let Sn=j?.get(Rt.id)??[Rt],$t=0,It=0;for(let pt of U){if(pt.t>oe)break;let Ht=Sn.some(Ft=>Ft.start<pt.t&&Ft.time>=pt.t),on=Sn.some(Ft=>Ft.time===pt.t&&Ft.event),Zt=pt.d/pt.n;if(c===1)It+=(Number(on)-Number(Ht)*Zt)/pt.n;else{let Ft=0,Jt=0;for(let H=0;H<pt.e;H++){let ae=pt.d/pt.e,Ie=pt.n-H*ae;Ft+=(Ie+H*ae)/(pt.e*Ie*Ie),Jt-=ae/(Ie*Ie)}It+=Ft*Number(on)+Jt*Number(Ht)}a===1&&Zt<1&&($t-=Z*(Number(on)-Number(Ht)*Zt)/((1-Zt)*pt.n))}We+=(Rt.weight*$t)**2,je+=(Rt.weight*It)**2}Ke=Math.sqrt(We),$e=Math.sqrt(je)}let ot=l?c===2?a===1?Ke:$e:a===1?Z===0?NaN:Ke/Z:$e:Ae,ht=l?c===2?Z*(a===1?Ke:$e):a===1?Ke:Z*$e:Z*Ae,Ct=NaN,Ut=NaN;if(w!=="none")if(w==="log")Ct=Z===0?ht===0?0:NaN:Z*Math.exp(-_*ot),Ut=Z===0?ht===0?0:NaN:Math.min(1,Z*Math.exp(_*ot));else if(w==="log-log")if(Z===0||Z===1)Ct=ht===0?Z:NaN,Ut=Ct;else{let We=Math.log(-Math.log(Z));Ct=Math.exp(-Math.exp(We-_*ot/Math.log(Z))),Ut=Math.exp(-Math.exp(We+_*ot/Math.log(Z)))}else if(w==="plain")Z===0&&ht===1/0?(Ct=NaN,Ut=NaN):(Ct=Math.max(0,Z-_*ht),Ut=Math.min(1,Z+_*ht));else if(w==="logit")if(Z===0||Z===1)Ct=ht===0?Z:NaN,Ut=Ct;else{let We=Math.log(Z/(1-Z)),je=_*ht/(Z*(1-Z));Ct=1-1/(1+Math.exp(We-je)),Ut=1-1/(1+Math.exp(We+je))}else if(Z>0&&Z<1){let We=Math.asin(Math.sqrt(Z)),je=_*ht/(2*Math.sqrt(Z*(1-Z)));Ct=Math.sin(Math.max(0,We-je))**2,Ut=Math.sin(Math.min(Math.PI/2,We+je))**2}else Ct=NaN,Ut=NaN;let Hn=z;I.strata.push(T[k]),I.time.push(oe),I.nRisk.push(Hn),I.nEvent.push(ge),I.nCensor.push(tt),I.surv.push(Z),I.cumhaz.push(ke),I.stdChaz.push(l?$e:Math.sqrt($)),I.stdErr.push(l?c===2?Z*(a===1?Ke:$e):a===1?Ke:Z*$e:Z*Ae),I.lower.push(Ct),I.upper.push(Ut),I.counts&&(I.counts.nRisk.push(G),I.counts.nEvent.push(Ee),I.counts.nCensor.push(ee)),o===void 0&&(z-=ge+tt,J-=we-pe),pe=we}let te=I.time.slice(B),fe=I.surv.slice(B),Re=I.lower.slice(B),Ce=I.upper.slice(B);I.median.push([Rc(te,fe),w==="none"?NaN:Rc(te,Re),w==="none"?NaN:Rc(te,Ce)]),I.n.push(Q),I.records.push(Q);let be=I.nRisk.slice(B);I.nMax.push(Math.max(...be)),I.nStart.push(be[0]??0),I.events.push(V.reduce((pe,oe)=>pe+(oe.event?oe.weight:0),0));let me=f==="none"?NaN:f==="common"?Math.max(...L):f==="individual"?re:f;if(f!=="none"&&!Number.isFinite(me))throw new RangeError("'rmean' must be finite");let qe=V.filter(pe=>Number.isFinite(pe.time)).map(pe=>Math.abs(pe.time)),de=Number.isFinite(me)?Math.max(Math.abs(me),Math.abs(q),...qe):Math.max(Math.abs(q),...qe),Fe=de===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(de)))),xe=de===0?1:2**Fe,Ve=Number.isFinite(me)&&!Number.isFinite(me-q),Oe=pe=>{let oe=1;for(let we of U)if(we.t<=pe)oe=we.S;else break;return oe},Be=pe=>{let oe=1;for(let we of U)if(we.t/xe<=pe)oe=we.S;else break;return oe},ze=Pn();if(Ve){let pe=Array.from(new Set([q/xe,...U.map(oe=>oe.t/xe).filter(oe=>oe>q/xe&&oe<me/xe),me/xe])).sort((oe,we)=>oe-we);for(let oe=0;oe<pe.length-1;oe++){let we=pe[oe],ge=pe[oe+1];gn(ze,Be(we)*(ge-we),pe.length+1)}}else{let pe=Array.from(new Set([q,...U.map(oe=>oe.t).filter(oe=>oe>q&&oe<me),me])).sort((oe,we)=>oe-we);for(let oe=0;oe<pe.length-1;oe++){let we=pe[oe],ge=pe[oe+1];gn(ze,Oe(we)*(ge-we),pe.length+1)}}let et=Vn(ze)*(Ve?xe:1),Ue=Pn();if(Number.isFinite(me))for(let pe of U){if(pe.t>me||pe.n<=pe.d)continue;let oe=Pn();if(Ve){let ge=Array.from(new Set([pe.t/xe,...U.map(ce=>ce.t/xe).filter(ce=>ce>pe.t/xe&&ce<me/xe),me/xe])).sort((ce,tt)=>ce-tt);for(let ce=0;ce<ge.length-1;ce++)gn(oe,Be(ge[ce])*(ge[ce+1]-ge[ce]),ge.length+1)}else{let ge=Array.from(new Set([pe.t,...U.map(ce=>ce.t).filter(ce=>ce>pe.t&&ce<me),me])).sort((ce,tt)=>ce-tt);for(let ce=0;ce<ge.length-1;ce++)gn(oe,Oe(ge[ce])*((ge[ce+1]-ge[ce])/xe),ge.length+1)}let we=Vn(oe);gn(Ue,we*we*pe.d/(pe.n*(pe.n-pe.d)),U.length+1)}let Xe=Vn(Ue);I.rmean.push(f==="none"?NaN:et),I.seRmean.push(f==="none"?NaN:Math.sqrt(Xe)*xe),D.push(f==="none"?w==="none"?[Q,I.nMax[k],I.nStart[k],I.events[k],I.median[k][0]]:[Q,I.nMax[k],I.nStart[k],I.events[k],I.median[k][0],I.median[k][1],I.median[k][2]]:w==="none"?[Q,I.nMax[k],I.nStart[k],I.events[k],et,Math.sqrt(Xe)*xe,I.median[k][0]]:[Q,I.nMax[k],I.nStart[k],I.events[k],et,Math.sqrt(Xe)*xe,I.median[k][0],I.median[k][1],I.median[k][2]])}return I.table={colNames:[...f==="none"?["records","n.max","n.start","events","median"]:["records","n.max","n.start","events","rmean","se(rmean)","median"],...w==="none"?[]:[`${u}LCL`,`${u}UCL`]],rowNames:T,table:D},X("survfit",I)}var lb=["tau","covariates","covariateNames","confLevel"];function os(e){return 2*se(-Math.abs(e))}function th(e,t,n){return[e-n*t,e+n*t]}function Ju(e,t,n){return{estimate:e,se:t,confInt:th(e,t,n)}}function fb(e,t,n,r){return{estimate:e,confInt:th(e,t,n),pValue:os(r)}}function eh(e,t){let n=ur(e.map(l=>l.time),e.map(l=>l.status),{rmean:t}),r=n.rmean[0],o=n.seRmean[0],s=0,i=1,c=Pn();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];gn(c,(1-i)*(f-s),n.time.length+1),i=n.surv[l],s=f}gn(c,(1-i)*(t-s),n.time.length+1);let a=Vn(c);return{rmst:{estimate:r,se:o},rmtl:{estimate:a,se:o}}}function Sc(e,t,n,r){if(n==="difference"){let l=e.estimate-t.estimate,f=Math.hypot(e.se,t.se);return fb(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:os(c/a)}}function mb(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=ur(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 Tc(e,t,n,r,o,s){let i=e.map(C=>[C.arm,...C.x]),c=i[0].map((C,q)=>{let F=i.reduce((O,I)=>Math.max(O,Math.abs(I[q])),0);if(F===0)return 1;let _=Math.floor(Math.log2(F));return _>500||_<-500?2**_:1}),a=i.map(C=>C.map((q,F)=>q/c[F])),l=e.map((C,q)=>[1,...a[q]]),f=[1,...c],m=mb(e),u=r?ic(a,n,{family:"poisson",weights:m}):Go(a,n,{weights:m});if(r&&!u.converged)throw new Error("the adjusted RMST Poisson model did not converge");let d=u.coefficients.map((C,q)=>C/f[q]),w=u.fittedValues;if(d.some(C=>!Number.isFinite(C))||w.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,q)=>C.map(F=>m[q]*F*(n[q]-w[q]))),b=g.map(C=>C.slice());for(let C of[0,1]){let q=e.flatMap((_,O)=>_.arm===C?[O]:[]),F=ur(q.map(_=>e[_].time),q.map(_=>e[_].status===0?1:0),{rmean:"none"});for(let _=0;_<F.time.length;_++){let O=F.time[_],I=F.nEvent[_];if(!(O<t)||I===0)continue;let D=F.nRisk[_];if(!(D>0))continue;let k=new Array(l[0].length).fill(0);for(let B=0;B<k.length;B++){let Q=Pn();for(let re of q)e[re].time>=O&&gn(Q,g[re][B],q.length);k[B]=Vn(Q)}let V=I/D;for(let B of q){let Q=e[B],re=Q.time===O&&Q.status===0,z=Q.time>=O;for(let J=0;J<k.length;J++)b[B][J]+=k[J]/D*(Number(re)-(z?V:0))}}}let N=l.map((C,q)=>C.map(F=>F*Math.sqrt(r?w[q]:1))),v=Ya(N),M=Ya(b),p=nt(v),x=Ga(Ga(p,M),p).map((C,q)=>C.map((F,_)=>F/f[q]/f[_])),E=x.map((C,q)=>Math.sqrt(Math.max(0,C[q]))),R=d.map((C,q)=>C/E[q]),A=R.map(os),S=d.map((C,q)=>C-s*E[q]),T=d.map((C,q)=>C+s*E[q]),L=r?[S.map(Math.exp),T.map(Math.exp)]:[S,T];return{coef:d,cov:x,table:{names:["intercept","arm",...o],estimate:d,se:E,z:R,pValue:A,confInt:L}}}function ub(e,t,n,r={}){if(K(r,lb,"rmst2"),e.length!==t.length||e.length!==n.length)throw new RangeError("'time', 'status' and 'arm' must have the same length");if(r.tau!==void 0&&typeof r.tau!="number")throw new TypeError("'tau' must be a number");if(r.confLevel!==void 0&&typeof r.confLevel!="number")throw new TypeError("'confLevel' must be a number");if(r.covariateNames!==void 0&&!Array.isArray(r.covariateNames))throw new TypeError("'covariateNames' must be an array of strings");let o=r.confLevel??.95;if(!(o>0&&o<1))throw new RangeError("'confLevel' must be between 0 and 1");let s=e.reduce((z,J)=>Number.isFinite(J)?Math.max(z,Math.abs(J)):z,0),i=s>2**500?2**Math.floor(Math.log2(s)):1,a=e.every(z=>!Number.isFinite(z)||z===0||z/i!==0)&&(r.tau===void 0||r.tau===0||r.tau/i!==0)?i:1,l=z=>z/a,f=Array.from(t),m=new Set;for(let z=0;z<f.length;z++){let J=n[z]===!0?1:n[z]===!1?0:n[z];(J===0||J===1)&&f[z]===2&&m.add(J)}let u=r.covariates?.[0],h=r.covariates!==void 0&&typeof u=="number";if(r.covariates!==void 0&&r.covariates.length>0&&!h&&!Array.isArray(u))throw new TypeError("'covariates' must be a numeric vector or a matrix of numeric rows");let d=r.covariates===void 0?void 0:h?r.covariates.map(z=>[z]):r.covariates.map(z=>Array.from(z));if(d&&d.length!==e.length)throw new RangeError("'covariates' must have one row per observation");if(d&&d.some(z=>z.length!==d[0].length))throw new RangeError("'covariates' must have the same number of columns in every row");if(d&&d.some(z=>z.some(J=>typeof J!="number")))throw new TypeError("'covariates' must contain numeric values");if(d&&d.some(z=>z.some(J=>!Number.isFinite(J)&&!Number.isNaN(J))))throw new RangeError("'covariates' must be finite");let w=[],g=[],b=[],N=[];for(let z=0;z<e.length;z++){if(e[z]==null||Number.isNaN(e[z])||f[z]==null||Number.isNaN(f[z])||n[z]==null||Number.isNaN(n[z]))throw new Error("missing value where TRUE/FALSE needed");if(typeof e[z]!="number")throw new TypeError("'time' must contain numbers");if(!Number.isFinite(e[z]))throw new RangeError("'time' must be finite");if(typeof n[z]!="number"&&typeof n[z]!="boolean")throw new TypeError("'arm' must contain numbers or booleans");let J=n[z]===!0?1:n[z]===!1?0:n[z];if(J!==0&&J!==1){b.push(l(e[z]));continue}if(typeof f[z]!="number")throw new TypeError("'status' must contain numbers");let Z=f[z],ke=Z===1?1:0,ne=d?d[z]:[];if(ne.some(Y=>!Number.isFinite(Y)))continue;g.push({time:l(e[z]),status:ke,arm:J,x:ne,index:z});let $=Z;if(m.has(J))if(Z===2)$=1;else if(Z===1)$=0;else{N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}else if($!==0&&$!==1){N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}w.push({time:l(e[z]),status:$,arm:J,x:ne,index:z})}if(g.some(z=>z.time<0))throw new Error("negative follow-up times are not supported");if(!w.some(z=>z.arm===0)||!w.some(z=>z.arm===1))throw new Error("both arms 0 and 1 are required");let v=[0,1].map(z=>Math.max(...g.filter(J=>J.arm===z).map(J=>J.time))),M=[0,1].map(z=>{let J=v[z];return g.some(Z=>Z.arm===z&&Z.time===J&&Z.status===0)}),p=M.some(Boolean)?Math.min(...v.filter((z,J)=>M[J])):Math.max(...v),y=r.tau===void 0,x=r.tau===void 0?p:l(r.tau),E=r.tau===void 0?x*a:r.tau;if(b.some(z=>z<=x))throw new RangeError("'arm' must contain only 0 and 1 through tau");if(!(x>0))throw new RangeError("'tau' must be greater than zero");if(x>p)throw new RangeError(`'tau' must be at most ${p*a}: the minimum of the largest times allowed by both arms`);if(x<Math.min(...g.map(z=>z.time)))throw new RangeError("'tau' must not be below the first observed time");let R=(1-o)/2,A=R<1e-8?le(R,{lowerTail:!1}):le(1-R),S=w.filter(z=>z.arm===1),T=w.filter(z=>z.arm===0),L=eh(S,x),C=eh(T,x),q=z=>({estimate:z.estimate*a,se:z.se*a}),F={rmst:q(L.rmst),rmtl:q(L.rmtl)},_={rmst:q(C.rmst),rmtl:q(C.rmtl)},O=Ju(F.rmst.estimate,F.rmst.se,A),I=Ju(_.rmst.estimate,_.rmst.se,A),D={estimate:F.rmtl.estimate,se:F.rmtl.se,confInt:[E-O.confInt[1],E-O.confInt[0]]},k={estimate:_.rmtl.estimate,se:_.rmtl.se,confInt:[E-I.confInt[1],E-I.confInt[0]]},V=Sc(F.rmst,_.rmst,"difference",A),B=Sc(F.rmst,_.rmst,"ratio",A),Q=Sc(F.rmtl,_.rmtl,"rmtlRatio",A),re={tau:E,tauDefault:y,confLevel:o,arm1:{rmst:O,rmtl:D},arm0:{rmst:I,rmtl:k},difference:V,ratio:B,rmtlRatio:Q,warnings:N};if(d){let z=r.covariateNames===void 0?h?["covariates"]:Array.from({length:d[0].length},()=>""):Array.from(r.covariateNames);if(z.some(be=>typeof be!="string"))throw new TypeError("'covariateNames' must contain strings");if(z.length!==d[0].length)throw new RangeError("'covariateNames' must have one name per covariate column");let J=w.map(be=>({...be,time:Math.min(be.time,x),status:be.time>=x?1:be.status})),Z=J.map(be=>be.time),ke=J.map(be=>x-be.time),ne=Tc(J,x,Z,!1,z,A),$=!(F.rmst.estimate>0&&_.rmst.estimate>0),Y=!(F.rmtl.estimate>0&&_.rmtl.estimate>0),W=$?void 0:Tc(J,x,Z,!0,z,A),U=Y?void 0:Tc(J,x,ke,!0,z,A),j=(be,me)=>{let qe=be.coef[1],de=Math.sqrt(Math.max(0,be.cov[1][1]));return me?{estimate:Math.exp(qe),confInt:[Math.exp(qe-A*de),Math.exp(qe+A*de)],pValue:os(qe/de)}:{estimate:qe*a,confInt:[(qe-A*de)*a,(qe+A*de)*a],pValue:os(qe/de)}},te=be=>({...be,estimate:be.estimate.map(me=>me*a),se:be.se.map(me=>me*a),confInt:[be.confInt[0].map(me=>me*a),be.confInt[1].map(me=>me*a)]}),fe=be=>({...be,estimate:be.estimate.map((me,qe)=>qe===0?me+Math.log(a):me),confInt:[be.confInt[0].map((me,qe)=>qe===0?me+Math.log(a):me),be.confInt[1].map((me,qe)=>qe===0?me+Math.log(a):me)]}),Re={names:["intercept","arm",...z],estimate:Array(ne.coef.length).fill(NaN),se:Array(ne.coef.length).fill(NaN),z:Array(ne.coef.length).fill(NaN),pValue:Array(ne.coef.length).fill(NaN),confInt:[Array(ne.coef.length).fill(NaN),Array(ne.coef.length).fill(NaN)]},Ce={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};re.adjusted={difference:j(ne,!1),ratio:$?Ce:j(W,!0),rmtlRatio:Y?Ce:j(U,!0)},re.coefficients={difference:te(ne.table),ratio:W?fe(W.table):Re,rmtlRatio:U?fe(U.table):Re},delete re.arm1,delete re.arm0}return X("rmst2",re)}var hb=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),pb=["eps","iterMax","tolerChol","tolerInf","timefix"],kc=e=>e==null||typeof e=="number"&&Number.isNaN(e),db=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,gb=2**64,wb=2**-1022,nh=700;function Na(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),d=u(),w=h(),g=0,b=0,N=u(),v=h(),M=u(),p=h(),y=0,x=0,E=R=>{let A=Math.exp(x-R);b*=A,y*=A;for(let S=0;S<m;S++){N[S]*=A,M[S]*=A;let T=v[S],L=p[S];for(let C=0;C<=S;C++)T[C]*=A,L[C]*=A}x=R};for(let R=f-1;R>=0;){c[R]&&(x=0,b=0,N=u(),v=h());let A=r[R],S=!1;if(l!==void 0){let q=R;for(;q>0&&r[q-1]===A&&!c[q-1];)q--;for(let F=q;F<=R;F++)o[F]&&(S=!0)}if(l!==void 0&&S){let q=R;for(;q>0&&!c[q-1];)q--;let F=R;for(;!c[F];)F++;let _=I=>{let D=0;for(let k=0;k<m;k++)D+=t[k]*i[I][k];return D},O=-1/0;for(let I=q;I<=F;I++)l[I]<A&&r[I]>=A&&(O=Math.max(O,_(I)));x=Number.isFinite(O)?O:0,b=0,N=u(),v=h();for(let I=q;I<=F;I++){if(!(l[I]<A&&r[I]>=A)||r[I]===A&&o[I])continue;let D=Math.exp(_(I)-x)*s[I];b+=D;for(let k=0;k<m;k++){N[k]+=D*i[I][k];for(let V=0;V<=k;V++)v[k][V]+=D*i[I][k]*i[I][V]}}}let T=0,L=0;y=0;let C=R;do{let q=i[C],F=0;for(let I=0;I<m;I++)F+=t[I]*q[I];let _=s[C];Number.isFinite(F)&&(F-x>nh?E(F):F-x<-nh&&b===0&&y===0&&(x=F));let O=Math.exp(F-x)*_;if(o[C]){T++,L+=_,y+=O,g+=_*F;for(let I=0;I<m;I++){d[I]+=_*q[I],M[I]+=O*q[I];let D=p[I];for(let k=0;k<=I;k++)D[k]+=O*q[I]*q[k]}}else if(l===void 0){b+=O;for(let I=0;I<m;I++){N[I]+=O*q[I];let D=v[I];for(let k=0;k<=I;k++)D[k]+=O*q[I]*q[k]}}C--}while(C>=0&&r[C]===A&&!c[C]);if(T>0){let q=n?T:1,F=n?T:1,_=L/q;for(let O=0;O<q;O++){b+=n?y/F:y,g-=_*(Math.log(b)+x+a);for(let I=0;I<m;I++){N[I]+=n?M[I]/F:M[I];let D=N[I]/b;d[I]-=_*D;let k=v[I],V=p[I],B=w[I];for(let Q=0;Q<=I;Q++){k[Q]+=n?V[Q]/F:V[Q];let re=k[Q]-D*N[Q],z=_*re;B[Q]+=z<1/0&&z>-1/0&&(Math.abs(z)>=wb||re===0)||!Number.isFinite(_)?z/b:_*(re/b)}}}M.fill(0);for(let O of p)O.fill(0)}R=C}for(let R=0;R<m;R++)for(let A=0;A<R;A++)w[A][R]=w[R][A];return{loglik:g,score:d,info:w}}function rh(e,t,n){let{time:r,event:o,x:s,stratumEnd:i}=e,c=Se([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 d=0;d<l;d++)h+=t[d]*s[u][d];return h};for(let u=0;u<a;){let h=u;for(;!i[h];)h++;let d=[];for(let R=u;R<=h;R++)d.push(m(R));let w=[];for(let R=u;R<=h;R++)(R===u||r[R]!==r[R-1])&&w.push(R);let g=R=>R+1<w.length?w[R+1]:h+1,b=new Array(w.length),N=new Array(w.length),v=new Array(w.length),M=-1/0,p=0,y=new Array(l).fill(0);for(let R=w.length-1;R>=0;R--){let A=Number.isFinite(M)?M:0;for(let T=w[R];T<g(R);T++)M=Math.max(M,d[T-u]);let S=Number.isFinite(M)?M:0;if(R<w.length-1&&S!==A){let T=Math.exp(A-S);p*=T;for(let L=0;L<l;L++)y[L]*=T}for(let T=w[R];T<g(R);T++){let L=c[T]*Math.exp(d[T-u]-S);p+=L;for(let C=0;C<l;C++)y[C]+=L*s[T][C]}b[R]=p,N[R]=y.slice(),v[R]=S}let x=0,E=new Array(l).fill(0);for(let R=0;R<w.length;R++){let A=v[R];if(R>0&&A!==v[R-1]){let I=Math.exp(A-v[R-1]);x*=I;for(let D=0;D<l;D++)E[D]*=I}let S=w[R],T=g(R),L=[];for(let I=S;I<T;I++)L.push(Math.exp(d[I-u]-A));let C=0,q=0,F=0,_=new Array(l).fill(0);for(let I=S;I<T;I++){if(!o[I])continue;let D=c[I]*L[I-S];C++,q+=c[I],F+=D;for(let k=0;k<l;k++)_[k]+=D*s[I][k]}for(let I=S;I<T;I++){let D=L[I-S],k=s[I],V=f[I];for(let B=0;B<l;B++)V[B]=-D*(k[B]*x-E[B])}if(C===0)continue;let O=n?C:1;for(let I=0;I<O;I++){let D=I/O,k=b[R]-D*F,V=N[R].map((Q,re)=>(Q-D*_[re])/k),B=q/O/k;for(let Q=S;Q<T;Q++){let re=L[Q-S],z=s[Q],J=f[Q];if(o[Q])for(let Z=0;Z<l;Z++){let ke=z[Z]-V[Z];J[Z]+=ke/O-(1-D)*re*ke*B}else for(let Z=0;Z<l;Z++)J[Z]-=re*(z[Z]-V[Z])*B}x+=B;for(let Q=0;Q<l;Q++)E[Q]+=B*V[Q]}}u=h+1}return f}function qc(e,t,n){let r=e.start,{time:o,event:s,x:i,stratumEnd:c}=e,a=Se([e.weight]).groups[0],l=o.length,f=t.length,m=Array.from({length:l},()=>new Array(f).fill(0)),u=o.map((h,d)=>{let w=0;for(let g=0;g<f;g++)w+=t[g]*i[d][g];return w});for(let h=0;h<l;){let d=h;for(;!c[d];)d++;for(let w=h;w<=d;){let g=o[w],b=w;for(;b<d&&o[b+1]===g;)b++;let N=[],v=[],M=-1/0;for(let p=w;p<=b;p++)s[p]&&v.push(p);if(v.length>0)for(let p=h;p<=d;p++)r[p]<g&&o[p]>=g&&(N.push(p),M=Math.max(M,u[p]));if(v.length>0&&N.length>0){Number.isFinite(M)||(M=0);let p=0,y=new Array(f).fill(0),x=0,E=new Array(f).fill(0),R=0;for(let S of N){let T=Math.exp(u[S]-M),L=a[S]*T;p+=L;for(let C=0;C<f;C++)y[C]+=L*i[S][C];if(s[S]&&o[S]===g){R+=a[S],x+=L;for(let C=0;C<f;C++)E[C]+=L*i[S][C]}}let A=n?v.length:1;for(let S=0;S<A;S++){let T=S/A,L=p-T*x,C=R/A/L,q=y.map((F,_)=>(F-T*E[_])/L);for(let F of N){let _=Math.exp(u[F]-M),O=s[F]&&o[F]===g;for(let I=0;I<f;I++){let D=i[F][I]-q[I];O?m[F][I]+=D/A-(1-T)*_*D*C:m[F][I]-=_*D*C}}}}w=b+1}h=d+1}return m}function ss(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 Ma(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 Ea(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 as=(e,t)=>({statistic:e,df:t,pValue:_t(e,t,{lowerTail:!1})});function bb(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=as(0,0),i=new Array(t).fill(0),c=o(),a=o();return{coefficients:o(),var:Array.from({length:t},()=>new Array(t).fill(0)),loglik:[0,0],score:0,wald:0,iter:0,means:Array.from({length:t},(l,f)=>it(e.map(m=>m[f]))),n:r,nevent:0,linearPredictors:new Array(r).fill(0),se:i,z:c,p:a,stdError:i,zValue:c,pValue:a,lower:o(),upper:o(),logtest:s,sctest:{...s},waldtest:{...s},rsq:{rsq:0,maxrsq:0},usedRobust:!1,warnings:n}}function vb(e,t,n,r={}){return K(r,hb,"coxph"),K(sn(r,"control"),pb,"coxph","control"),X("coxph",yb(e,t,n,r).result)}function yb(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:d,id:w}=r,{eps:g=1e-9,iterMax:b=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 M=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(b)&&b>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(m>0&&m<1))throw new RangeError("'confLevel' must be between 0 and 1");let p=e.length;if(t.length!==p||n.length!==p)throw new RangeError("'time', 'status' and 'x' must have the same length");if(i!==void 0&&i.length!==p)throw new RangeError("'weights' must have the same length as 'time'");if(c!==void 0&&c.length!==p)throw new RangeError("'strata' must have the same length as 'time'");if(h!==void 0&&h.length!==p)throw new RangeError("'cluster' must have the same length as 'time'");if(d!==void 0&&d.length!==p)throw new RangeError("'start' must have the same length as 'time'");if(w!==void 0&&w.length!==p)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=p===0?0:n[0].length;if(y<1)throw new RangeError("'x' must have at least one column");if(n.some(H=>H.length!==y))throw new RangeError("the rows of 'x' must have the same length");if(a!==void 0&&a.length!==y)throw new RangeError("'init' must have one value per column of 'x'");if(a?.some(H=>!Number.isFinite(H)))throw new RangeError("'init' must contain finite values");let x=new mt,E=[];for(let H=0;H<p;H++){if(Number.isNaN(e[H])||Number.isNaN(t[H])||n[H].some(Ie=>Number.isNaN(Ie))||d!==void 0&&Number.isNaN(d[H])||i!==void 0&&Number.isNaN(i[H])||c!==void 0&&kc(c[H])||h!==void 0&&kc(h[H])||w!==void 0&&kc(w[H]))continue;if(d!==void 0&&!(d[H]<e[H]))throw new RangeError("stop time must be > start time");let ae=t[H];if(ae!==0&&ae!==1){if(d===void 0)throw new RangeError("'status' must be 0 (censored) or 1 (event)");x.add("Invalid status value, converted to NA");continue}if(e[H]===-1/0||e[H]===1/0&&ae===1)throw new RangeError("event times must be finite");E.push(H)}let R=E.length;if(R===0)throw new RangeError(d===void 0?"no observations without missing values":"No (non-missing) observations");if(E.some(H=>n[H].some(ae=>!Number.isFinite(ae))))throw new RangeError("'x' contains infinite values");let A=E.map(H=>i===void 0?1:i[H]);if(A.some(H=>!(H>0)))throw new RangeError("Invalid weights, must be >0");if(A.some(H=>H===1/0))throw new RangeError("'weights' must be finite");let S=w===void 0?void 0:kt(w),T=new Map;if(S!==void 0)for(let H of E){if(t[H]!==1)continue;let ae=S[H];T.set(ae,(T.get(ae)??0)+1)}let L=d!==void 0&&[...T.values()].some(H=>H>1),C=u??(h!==void 0||A.some(H=>!Number.isInteger(H))||L);if(d!==void 0&&u===!0&&h===void 0&&w===void 0)throw new RangeError("one of cluster or id is needed");h!==void 0&&!C&&x.add("cluster specified with robust=FALSE, cluster ignored");let q=0;for(let H of E)q+=t[H];let F=E.map(H=>e[H]),_=d===void 0?void 0:E.map(H=>d[H]),O,I;if(_===void 0)O=M?io(F):F;else if(!M)O=F,I=_;else{let H=io([..._,...F]);if(I=H.slice(0,R),O=H.slice(R),I.some((ae,Ie)=>ae>=O[Ie]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(q===0){let H=bb(E.map(ae=>n[ae]),y,x.list);return S!==void 0&&(H.nId=new Set(E.map(ae=>S[ae])).size),{result:H}}let D=new Array(R).fill(0),k,V=[];if(c!==void 0){let H=kt(c);V=Array.from(new Set(E.map(Ie=>H[Ie]))).sort(db);let ae=new Map;V.forEach((Ie,Ge)=>ae.set(Ie,Ge)),k=E.map(Ie=>H[Ie]),D=E.map(Ie=>ae.get(H[Ie]))}let B=Array.from({length:R},(H,ae)=>ae).sort((H,ae)=>D[H]-D[ae]||O[H]-O[ae]||H-ae),Q=B.map(H=>A[H]),{groups:re,k:z}=gt(Q)>=gb?Se([Q]):{groups:[Q],k:0},J=re[0],Z=0;for(let H of A)Z+=z===0?H:P(H,-z);let ke=B.map(H=>n[E[H]].slice()),ne=new Array(y).fill(0),$=new Array(y).fill(1),Y=new Array(y).fill(0),W=new Set(f);for(let H=0;H<y;H++){if(ke.every(He=>W.has(He[H])))continue;let ae=ke.map(He=>He[H]),Ie=He=>{let zt=0;for(let Fn=0;Fn<R;Fn++)zt+=(d===void 0?J[Fn]:1)*He[Fn];let Cn=d===void 0?Z:R,wr=zt/Cn,$r=0,$a=He.map((Fn,gs)=>($r+=(d===void 0?J[gs]:1)*Math.abs(Fn-wr),Fn-wr)),zr=$r===0?1:Cn/$r;return{m:wr,s:zr,d:$a,ok:Number.isFinite(wr)&&zr>=2**-1022&&zr<1/0}},Ge=Ie(ae);if(!Ge.ok){let{groups:He,k:zt}=Se([ae]);Ge=Ie(He[0]),Y[H]=zt}for(let He=0;He<R;He++)ke[He][H]=Ge.d[He]*Ge.s;ne[H]=P(Ge.m,Y[H]),$[H]=Ge.s}let U={time:B.map(H=>O[H]),...I===void 0?{}:{start:B.map(H=>I[H])},event:B.map(H=>t[E[H]]===1),weight:J,x:ke,stratumEnd:B.map((H,ae)=>ae===R-1||D[B[ae+1]]!==D[H]),logUnit:z*Math.LN2},j=s==="efron",te=a===void 0?new Array(y).fill(0):a.map((H,ae)=>P(H/$[ae],Y[ae])),fe=Na(U,te,j),Re=[fe.loglik,fe.loglik],Ce=fe.info.map(H=>H.slice()),be=ss(Ce,N),me=Ma(Ce,fe.score),qe=0;for(let H=0;H<y;H++)qe+=fe.score[H]*me[H];let de=0,Fe,xe=!1,Ve=b===0;if(b===0||!Number.isFinite(fe.loglik))Fe=Ea(Ce);else{let H=fe.loglik,ae=te.map((Ge,He)=>Ge+me[He]),Ie=0;for(Ve=!1,de=1;de<=b;de++){fe=Na(U,ae,j),Ce=fe.info.map(He=>He.slice()),be=ss(Ce,N);let Ge=Number.isFinite(fe.loglik)&&fe.score.every(Number.isFinite)&&fe.info.every(He=>He.every(Number.isFinite));if(Ge&&(fe.loglik===H||Math.abs(1-H/fe.loglik)<=g)){Ve=!0,Re[1]=fe.loglik,te=ae;break}!Ge||fe.loglik<H?(Ie++,ae=ae.map((He,zt)=>(He+Ie*te[zt])/(Ie+1))):(Ie=0,H=fe.loglik,Re[1]=H,me=Ma(Ce,fe.score),te=ae,ae=ae.map((He,zt)=>He+me[zt]))}Ve?Fe=Ea(Ce):(de=d===void 0?b+1:b,xe=!0,d!==void 0?(fe=Na(U,te,j),Re[1]=fe.loglik,Ce=fe.info.map(Ge=>Ge.slice()),be=ss(Ce,N),Fe=Ea(Ce)):b>1?(fe=Na(U,te,j),Re[1]=fe.loglik,Fe=fe.info.map((Ge,He)=>Ge.map((zt,Cn)=>He===Cn?Ge[He]>0?1/Ge[He]:0:-0))):Fe=Ea(Ce))}let Oe=$.map(H=>Math.floor(Math.log2(H))),Be=$.map((H,ae)=>P(H,-Oe[ae])),ze=Oe.map((H,ae)=>H-Y[ae]),et=te.map((H,ae)=>H*Be[ae]),Ue=fe.score.map((H,ae)=>H/Be[ae]),Xe=Fe.map((H,ae)=>H.map((Ie,Ge)=>Ie*Be[ae]*Be[Ge])),pe=et.map((H,ae)=>P(H,ze[ae])),oe=Xe.map((H,ae)=>H.map((Ie,Ge)=>P(Ie,ze[ae]+ze[Ge]-z))),we=pe.slice();if(b>0&&Math.abs(be)<y)for(let H=0;H<y;H++)Fe[H][H]===0&&(we[H]=NaN);let ge=d===void 0?pe:pe.map((H,ae)=>Number.isNaN(we[ae])?0:H),ce=0;for(let H=0;H<y;H++)ce+=ge[H]*ne[H];let tt=E.map(H=>{let ae=0;for(let Ie=0;Ie<y;Ie++)ae+=n[H][Ie]*ge[Ie];return ae-ce});if(b>1){let H=Xe.map((ae,Ie)=>{let Ge=0;for(let He=0;He<y;He++)Ge+=Ue[He]*Xe[He][Ie];return Math.abs(Ge)});if(xe)(d===void 0||b>1)&&x.add("Ran out of iterations and did not converge"),d===void 0&&(gt(tt)>500||H.some(ae=>!Number.isFinite(ae)))&&x.add("one or more coefficients may be infinite");else if(d===void 0){let ae=[];for(let Ie=0;Ie<y;Ie++){let Ge=H[Ie];(!Number.isFinite(Ue[Ie])||P(Ge,ze[Ie])>g&&Ge>v*Math.abs(et[Ie]))&&ae.push(Ie+1)}ae.length>0&&x.add("Loglik converged before variable "+ae.join(",")+" ; coefficient may be infinite. ")}}let G=we.map((H,ae)=>Number.isNaN(H)?-1:ae).filter(H=>H>=0),ue=G.length,Me,ye=0,Ee=0,ee;if(C&&ue>0){let H=B.map((ct,Qe)=>Qe),ae=R,Ie=h??(d===void 0?void 0:w);if(Ie!==void 0){let ct=kt(Ie),Qe=new Map;H=B.map(wt=>{let Mt=ct[E[wt]],Et=Qe.get(Mt);return Et===void 0&&(Et=Qe.size,Qe.set(Mt,Et)),Et}),ae=Qe.size}if(ae<2)throw new RangeError(d===void 0?"at least two clusters are needed for the robust variance":"(subscript) logical subscript too long");let Ge=ct=>ct.map(Qe=>Qe.map((wt,Mt)=>wt/Be[Mt])),He=Ge(d===void 0?rh(U,te,j):qc(U,te,j)),zt=He.map(ct=>Xe.map((Qe,wt)=>{let Mt=0;for(let Et=0;Et<y;Et++)Mt+=ct[Et]*Xe[Et][wt];return Mt})),Cn;if(z===0)Cn=zt.map((ct,Qe)=>ct.map(wt=>J[Qe]*wt));else{let ct=wt=>wt===0||!Number.isFinite(wt)?0:Math.floor(Math.log2(Math.abs(wt))),Qe=-1/0;zt.forEach((wt,Mt)=>wt.forEach(Et=>Et!==0&&Number.isFinite(Et)&&(Qe=Math.max(Qe,ct(J[Mt])+ct(Et))))),ye=Qe===-1/0?0:-Qe,Cn=zt.map((wt,Mt)=>{let Et=ct(J[Mt]);return wt.map(br=>{let ll=ct(br);return P(P(J[Mt],-Et)*P(br,-ll),Et+ll+ye)})})}let wr=ct=>{let Qe=Array.from({length:ae},()=>new Array(y).fill(0));return ct.forEach((wt,Mt)=>{let Et=Qe[H[Mt]];for(let br=0;br<y;br++)Et[br]+=wt[br]}),Qe},$r=ct=>{let Qe=Array.from({length:y},()=>new Array(y).fill(0));for(let wt of ct)for(let Mt=0;Mt<y;Mt++)for(let Et=0;Et<y;Et++)Qe[Mt][Et]+=wt[Mt]*wt[Et];return Qe};Me=$r(wr(Cn));let $a=Se([Q]).groups[0],zr=a===void 0?new Array(y).fill(0):a.map((ct,Qe)=>P(ct/$[Qe],Y[Qe])),Fn=d===void 0?rh(U,zr,j):qc(U,zr,j),gs=wr(Ge(Fn).map((ct,Qe)=>ct.map(wt=>$a[Qe]*wt))),So=new Array(y).fill(0);for(let ct of gs)for(let Qe=0;Qe<y;Qe++)So[Qe]+=ct[Qe];let za=$r(gs);if(y===1)Ee=So[0]*So[0]/za[0][0];else{ss(za,N);let ct=Ma(za,So);for(let Qe=0;Qe<y;Qe++)Ee+=So[Qe]*ct[Qe]}let il=new Array(R),cl=new Array(R);B.forEach((ct,Qe)=>{il[ct]=He[Qe].map((wt,Mt)=>P(wt,-ze[Mt])),cl[ct]=Cn[Qe].map((wt,Mt)=>P(wt,ze[Mt]-ye))}),ee={scoreResiduals:il,dfbeta:cl}}let ve=Me!==void 0,ie=Me??Xe,Ae=Me===void 0?-z:-2*ye,Ke=G.map(H=>et[H]-(a===void 0?0:P(a[H],-ze[H]))),$e=0;if(ue===1)$e=Ke[0]*Ke[0]/ie[G[0]][G[0]];else if(ue>1){let H=G.map(Ie=>G.map(Ge=>ie[Ie][Ge]));ss(H,N);let ae=Ma(H,Ke);for(let Ie=0;Ie<ue;Ie++)$e+=Ke[Ie]*ae[Ie]}$e=P($e,-Ae);let ot=(1-m)/2,ht=ot<1e-8?le(ot,{lowerTail:!1}):le((1+m)/2),Ct=(H,ae)=>{let Ie=Math.floor(ae/2),Ge=H.map((He,zt)=>Math.sqrt(P(He[zt],ae-2*Ie)));return{unit:Ge,h:Ie,se:Ge.map((He,zt)=>P(He,ze[zt]+Ie))}},Ut=Ct(Xe,-z).se,{unit:Hn,h:We,se:je}=Ct(ie,Ae),xt=we.map((H,ae)=>P((Number.isNaN(H)?H:et[ae])/Hn[ae],-We)),Rt=xt.map(H=>_t(H*H,1,{lowerTail:!1})),Sn=we.map((H,ae)=>Math.exp(H-ht*je[ae])),$t=we.map((H,ae)=>Math.exp(H+ht*je[ae])),It=P(-2*(Re[0]-Re[1]),z),pt=[P(Re[0],z),P(Re[1],z)],Ht=P(qe,z),on=Me?.map((H,ae)=>H.map((Ie,Ge)=>P(Ie,ze[ae]+ze[Ge]-2*ye))),Zt={coefficients:we,var:on??oe,loglik:pt,score:Ht,wald:$e,iter:de,means:ne,n:R,nevent:q,...S===void 0?{}:{nId:new Set(E.map(H=>S[H])).size},linearPredictors:tt,se:Ut,z:xt,p:Rt,stdError:Ut,zValue:xt,pValue:Rt,lower:Sn,upper:$t,logtest:as(It,ue),sctest:as(Ht,ue),waldtest:as($e,ue),rsq:{rsq:1-Math.exp(-It/R),maxrsq:1-Math.exp(2*pt[0]/R)},usedRobust:ve,...ve?{naiveVar:oe,robustSe:je,rscore:Ee,robscore:as(Ee,ue)}:{},warnings:x.list},Ft=fe.score.map((H,ae)=>P(H/$[ae],Y[ae]+z));Object.defineProperty(Zt,"coxZphData",{enumerable:!1,value:{time:O.slice(),...I===void 0?{}:{start:I.slice()},status:E.map(H=>t[H]===1),weights:A.slice(),...k===void 0?{}:{strata:k.slice()},x:E.map(H=>n[H].slice()),sortedToInput:B.slice(),sortedTime:U.time.slice(),...U.start===void 0?{}:{sortedStart:U.start.slice()},sortedEvent:U.event.slice(),sortedWeight:U.weight.slice(),sortedX:U.x.map(H=>H.slice()),stratumIndex:B.map(H=>D[H]),stratumEnd:U.stratumEnd.slice(),betaScaled:te.slice(),inverseCovariateScale:Be.map((H,ae)=>P(1/H,-ze[ae])),weightExp:z,ties:s}});let Jt=ee?.scoreResiduals;if(d!==void 0&&o&&Jt===void 0){let H=qc(U,te,j);Jt=new Array(R),B.forEach((ae,Ie)=>{Jt[ae]=H[Ie].map((Ge,He)=>P(Ge/Be[He],-ze[He]))})}return{result:Zt,...ee,...Jt===void 0?{}:{scoreResiduals:Jt},first:Ft,converged:Ve}}var oh=["transform","terms","collapseTerms","global"],Bn=e=>new Array(e).fill(0),hr=e=>Array.from({length:e},()=>Bn(e));function Nb(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 Mb(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=sh(i,a)-sh(s,a);l.deaths>0&&(c*=1-l.deaths/f)}return n}function Eb(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=Mb(e);else if(t==="rank")o=Nb(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 xb(e,t={}){K(t,oh,"coxZph"),K(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(($,Y)=>Number.isNaN($)?-1:Y).filter($=>$>=0),c=i.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let a=[];for(let $=0;$<s.sortedTime.length;){let Y=$;for(;!s.stratumEnd[Y];)Y++;for(let W=$;W<=Y;){let U=s.sortedTime[W],j=W;for(;j<Y&&s.sortedTime[j+1]===U;)j++;let te=[];for(let fe=W;fe<=j;fe++)s.sortedEvent[fe]&&te.push(fe);s.start!==void 0&&te.reverse();for(let fe of te)a.push(s.sortedToInput[fe]);W=j+1}$=Y+1}if(a.length===0)throw new Error("the proportional-hazards test requires events");let l=a.map($=>s.time[$]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=Eb(s,n,a,f),u=m[a[0]];if(a.every($=>m[$]===u))throw new Error("the transform is constant at the event times");let h=a.reduce(($,Y)=>Math.max($,Math.abs(m[Y])),0),d=h===0?0:Math.floor(Math.log2(h)),w=m.map($=>Number.isFinite($)?P($,-d):$),g=a.reduce(($,Y)=>$+w[Y],0)/a.length,b=w.map($=>$-g);if(a.every($=>b[$]===0))throw new Error("the transform is constant at the event times");let N=i.map($=>s.betaScaled[$]),v=hr(c),M=hr(c),p=hr(c),y=Bn(c),x=[];for(let $=0;$<s.sortedTime.length;){let Y=$;for(;!s.stratumEnd[Y];)Y++;for(let W=$;W<=Y;){let U=s.sortedTime[W],j=W;for(;j<Y&&s.sortedTime[j+1]===U;)j++;let te=[];for(let fe=W;fe<=j;fe++)s.sortedEvent[fe]&&te.push(fe);if(te.length>0){let fe=[],Re=-1/0;for(let oe=$;oe<=Y;oe++){if(!((s.sortedStart===void 0||s.sortedStart[oe]<U)&&s.sortedTime[oe]>=U))continue;fe.push(oe);let ge=0;for(let ce=0;ce<c;ce++)ge+=N[ce]*s.sortedX[oe][i[ce]];Re=Math.max(Re,ge)}if(fe.length===0||!Number.isFinite(Re))throw new Error("empty Cox proportional-hazards risk set");let Ce=Bn(c),be=hr(c),me=Bn(c),qe=hr(c),de=0,Fe=0;for(let oe of fe){let we=0;for(let ce=0;ce<c;ce++)we+=N[ce]*s.sortedX[oe][i[ce]];let ge=s.sortedWeight[oe]*Math.exp(we-Re);de+=ge;for(let ce=0;ce<c;ce++){let tt=s.sortedX[oe][i[ce]];Ce[ce]+=ge*tt;for(let G=0;G<c;G++)be[ce][G]+=ge*tt*s.sortedX[oe][i[G]]}if(s.sortedEvent[oe]&&s.sortedTime[oe]===U){Fe+=ge;for(let ce=0;ce<c;ce++){let tt=s.sortedX[oe][i[ce]];me[ce]+=ge*tt;for(let G=0;G<c;G++)qe[ce][G]+=ge*tt*s.sortedX[oe][i[G]]}}}let xe=0,Ve=Bn(c);for(let oe of te){let we=s.sortedWeight[oe];xe+=we;for(let ge=0;ge<c;ge++)Ve[ge]+=we*s.sortedX[oe][i[ge]]}let Oe=te.length,Be=s.ties==="efron"&&Oe>1?Oe:1,ze=Bn(c),et=hr(c);for(let oe=0;oe<Be;oe++){let we=oe/Be,ge=de-we*Fe,ce=Ce.map((G,ue)=>(G-we*me[ue])/ge);for(let G=0;G<c;G++)ze[G]+=ce[G]/Be;let tt=xe/Be;for(let G=0;G<c;G++)for(let ue=0;ue<c;ue++)et[G][ue]+=tt*((be[G][ue]-we*qe[G][ue])/ge-ce[G]*ce[ue])}let Ue=Ve.map((oe,we)=>oe-xe*ze[we]),Xe=s.sortedToInput[te[0]],pe=b[Xe];for(let oe=0;oe<c;oe++){y[oe]+=pe*Ue[oe];for(let we=0;we<c;we++)v[oe][we]+=et[oe][we],M[oe][we]+=pe*et[oe][we],p[oe][we]+=pe*pe*et[oe][we]}s.start!==void 0&&te.reverse();for(let oe of te){let we=s.sortedToInput[oe],ge=i.map(ce=>s.sortedX[oe][ce]);for(let ce=0;ce<c;ce++)ge[ce]-=ze[ce];x.push({input:we,time:U,stratum:s.stratumIndex[oe],g:m[we],residual:ge})}}W=j+1}$=Y+1}let E;try{E=nt(v,M)}catch{throw new Error("the proportional-hazards information is singular")}let R=hr(c);for(let $=0;$<c;$++)for(let Y=0;Y<c;Y++){let W=0;for(let U=0;U<c;U++)W+=M[U][$]*E[U][Y];R[$][Y]=p[$][Y]-W}for(let $=0;$<c;$++)for(let Y=0;Y<$;Y++){let W=(R[$][Y]+R[Y][$])/2;R[$][Y]=W,R[Y][$]=W}let A=t.terms===!1||t.terms===void 0?i.map($=>[$]):t.terms,S=new Set,T=[],L=[];if(A.forEach(($,Y)=>{if(!Array.isArray($)||$.length===0)throw new RangeError("each terms group must be a non-empty array");let W=[];for(let U of $){if(!Number.isInteger(U)||U<0||U>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(S.has(U))throw new RangeError("terms groups must not overlap");S.add(U),i.includes(U)&&W.push(U)}W.length>0&&(T.push(W),L.push(Y))}),i.forEach($=>{S.has($)||(T.push([$]),L.push(A.length))}),T.length===0)throw new Error("the proportional-hazards test has no estimable terms");let C=[];T.forEach(($,Y)=>{if(o&&$.length>1)C.push({col:-1,group:Y,collapsed:!0});else for(let W of $)C.push({col:W,group:Y,collapsed:!1})});let q=C.length,F=Array.from({length:c},()=>Bn(q)),_=[],O=[];C.forEach(($,Y)=>{let W=T[$.group],U=L[$.group];if($.collapsed){for(let j of W){let te=i.indexOf(j);F[te][Y]=N[te]}_.push(1),O.push(`g${U+1}`)}else{let j=i.indexOf($.col);F[j][Y]=s.inverseCovariateScale[$.col],_.push(e.coefficients[$.col]),O.push(W.length>1?`g${U+1}x${$.col+1}`:`x${$.col+1}`)}});let I=Array.from({length:q},($,Y)=>{let W=0;for(let U=0;U<c;U++)W=Math.max(W,Math.abs(F[U][Y]));if(!(W>0&&Number.isFinite(W)))throw new Error("the proportional-hazards residual variance is singular");return Math.floor(Math.log2(W))}),D=F.map($=>$.map((Y,W)=>P(Y,-I[W]))),k=Array.from({length:c},()=>Bn(q));for(let $=0;$<c;$++)for(let Y=0;Y<q;Y++)for(let W=0;W<c;W++)k[$][Y]+=v[$][W]*D[W][Y];let V=hr(q);for(let $=0;$<q;$++)for(let Y=0;Y<q;Y++)for(let W=0;W<c;W++)V[$][Y]+=D[W][$]*k[W][Y];for(let $=0;$<q;$++)for(let Y=0;Y<$;Y++){let W=(V[$][Y]+V[Y][$])/2;V[$][Y]=W,V[Y][$]=W}let B;try{B=nt(V)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let $=0;$<q;$++)for(let Y=0;Y<$;Y++){let W=(B[$][Y]+B[Y][$])/2;B[$][Y]=W,B[Y][$]=W}let Q=B.map(($,Y)=>$.map((W,U)=>a.length*Le(W,-I[Y]-I[U]-s.weightExp))),re=x.map($=>{let Y=Bn(q);for(let W=0;W<q;W++)for(let U=0;U<c;U++)Y[W]+=D[U][W]*$.residual[U];return _.map((W,U)=>{let j=0;for(let te=0;te<q;te++)j+=Y[te]*B[te][U];return W+a.length*Le(j,-I[U]-s.weightExp)})}),z=[],J=[],Z=[],ke=C.map($=>$.collapsed?T[$.group].map(Y=>i.indexOf(Y)):[i.indexOf($.col)]);for(let $ of ke){let Y=$.map(te=>$.map(fe=>R[te][fe])),W=$.map(te=>y[te]),U;try{U=nt(Y,W)}catch{throw new Error("the proportional-hazards term information is singular")}let j=P(Math.max(0,W.reduce((te,fe,Re)=>te+fe*U[Re],0)),s.weightExp);z.push(j),J.push($.length),Z.push(_t(j,$.length,{lowerTail:!1}))}if(r){let $;try{$=nt(R,y)}catch{throw new Error("the global proportional-hazards information is singular")}let Y=P(Math.max(0,y.reduce((W,U,j)=>W+U*$[j],0)),s.weightExp);z.push(Y),J.push(c),Z.push(_t(Y,c,{lowerTail:!1})),O.push("GLOBAL")}let ne={rowNames:O,chisq:z,df:J,p:Z,pValue:Z,x:x.map($=>$.g),time:x.map($=>$.time),...s.strata===void 0?{}:{strata:x.map($=>ut(s.strata[$.input]))},y:re,var:Q,transform:n,warnings:f};return X("coxZph",ne,e)}var Rb=["center"],Ab=2**-1022;function Sb(e,t,n={}){K(n,Rb,"leveneTest");let r=Te(n.center??"median",["median","mean"]),{groups:o}=or(e,t);if(o.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let s=l=>{let f=r==="median"?na(l):it(l);return l.map(m=>Math.abs(m-f))},i=Se(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<Ab)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let h=Se([u]);return{d:s(h.groups[0]),k:h.k}}),f=Math.max(...l.map(u=>u.d.some(h=>h>0)?Math.floor(Math.log2(Math.max(...u.d)))+u.k:-1/0)),m=Number.isFinite(f)?f:0;i=l.map(u=>u.d.map(h=>P(h,u.k-m)))}let a=lu(Sr(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 Tb=["score"],kb=["alternative","exact","variant","levels"];function qb(e,t,...n){K(Ur(n),Tb,"propTrendTest");let{score:r}=Br(["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:d,sxy:w,sxx:g}=Ic(f,m,h,u);if(!(g>=2**-1022&&g<1/0&&Math.abs(w)<1/0)){let N=Se([u.map(v=>c[v])]);N.k!==0&&(h=c.slice(),u.forEach((v,M)=>h[v]=N.groups[0][M]),{sw:d,sxy:w,sxx:g}=Ic(f,m,h,u))}let b=Vt(w,g);if(!(a<1/0)||l>0&&l<1&&!(d<1/0&&Math.abs(w)<1/0&&g<1/0)){let{groups:[N,v],k:M}=Se([o,s]),p=N.reduce((x,E)=>x+E,0)/v.reduce((x,E)=>x+E,0),y=Ic(v,m,h,u);b=P(y.sxy/p*(y.sxy/y.sxx/(1-p)),M)}return{method:"Chi-squared Test for Trend in Proportions",statistic:b,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:yt(b,.5,2,!1,!1),warnings:[]}}function Ic(e,t,n,r){let o=r.reduce((l,f)=>l+e[f],0),s=r.reduce((l,f)=>l+e[f]*n[f],0)/o,i=r.reduce((l,f)=>l+e[f]*t[f],0)/o,c=0,a=0;for(let l of r){let f=n[l]-s;c+=e[l]*f*(t[l]-i),a+=e[l]*f*f}return{sw:o,sxy:c,sxx:a}}var Ib=2**24,_b=2**32,Lb=`Sample size > 100 or data with ties
|
|
10
|
+
p-value based on normal approximation. Specify nperm for permutation p-value`,Cb=`Sample data with ties
|
|
11
|
+
p-value based on normal approximation. Specify nperm for permutation p-value.`;function Fb(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=kt(t));let c;if(n!==void 0)c=n.map(ut);else{let f=[...new Set(i.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(ut):[...new Set(f.map(ut))].sort(wn)}let a=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of i){let m=l.get(ut(t[f]));if(m===void 0)throw new RangeError(`the group '${ut(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function Ob(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 Db(e,t,n={}){K(n,kb,"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=Fb(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 M=v+1;M<s.length;M++)for(let p of N)for(let y of s[M])l+=p<y?1:p===y?.5:0});let u=[],h=!a&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&a&&(h=!1,u.push(Cb))):h||u.push(Lb);let d,w;if(h){let N=1,v=0;for(let[p,y]of f)p===0||y===0||(v+=N*(p*y+1),N+=p*y);if(N>Ib||v>_b)throw new RangeError(`the exact distribution of JT has ${N} values and needs ${v} steps, too many: use exact: false`);let M=[1];for(let[p,y]of f){if(p===0||y===0)continue;let x=Array.from({length:p*y+1},(R,A)=>Rl(A,p,y,!1)),E=new Array(M.length+x.length-1).fill(0);M.forEach((R,A)=>x.forEach((S,T)=>E[A+T]+=R*S)),M=E}d=0,w=0,M.forEach((p,y)=>{y<=l&&(d+=p),y>=l&&(w+=p)})}else{let N=f.reduce((p,[y,x])=>p+y*x,0)/2,v=a?Ob(s.map(p=>p.length),i):f.reduce((p,[y,x])=>p+y*x*(y+x+1)/12,0),M=(l-N)/Math.sqrt(v);d=se(M),w=1-d,w<1e-8&&(w=se(M,{lowerTail:!1}))}let b=r==="increasing"?w:r==="decreasing"?d:Math.min(1,2*Math.min(w,d,o==="clinfun"?.5:1));return X("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:b,warnings:u})}var Pb=["nClasses","adjust"];function is(e){return Pt(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 xa(e){ah(e);let t=vl(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function Vb(e){let t=is(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=xa(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(se(r[a-1],{logP:!0})+se(r[n-a],{lowerTail:!1,logP:!0}));let s=-n-o/n,i=(1+.75/n+2.25/n**2)*s,c;return i<.2?c=1-Math.exp(-13.436+101.14*i-223.73*i**2):i<.34?c=1-Math.exp(-8.318+42.796*i-59.938*i**2):i<.6?c=Math.exp(.9177-4.279*i-1.38*i**2):i<10?c=Math.exp(1.2937-5.709*i+.0186*i**2):c=37e-25,{method:"Anderson-Darling normality test",statistic:s,statisticName:"A",pValue:c,warnings:[]}}function $b(e){let t=is(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=xa(t),o=1/(12*n);r.forEach((a,l)=>{o+=(se(a)-(2*(l+1)-1)/(2*n))**2});let s=(1+.5/n)*o,i=[],c;return s<.0275?c=1-Math.exp(-13.953+775.5*s-12542.61*s**2):s<.051?c=1-Math.exp(-5.903+179.546*s-1515.29*s**2):s<.092?c=Math.exp(.886-31.62*s+10.897*s**2):s<1.1?c=Math.exp(1.111-34.242*s+12.832*s**2):(c=737e-12,i.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),X("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function zb(e){let t=is(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=xa(t).map(f=>se(f)),o=-1/0,s=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),s=Math.max(s,f-m/n)});let i=Math.max(o,s),c=n<=100?i:i*(n/100)**.49,a=n<=100?n:100,l=Math.exp(-7.01256*c**2*(a+2.78019)+2.99587*c*Math.sqrt(a+2.78019)-.122119+.974598/Math.sqrt(a)+1.67997/a);if(l>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*i;f<=.302?l=1:f<=.5?l=2.76773-19.828315*f+80.709644*f**2-138.55152*f**3+81.218052*f**4:f<=.9?l=-4.901232+40.662806*f-97.490286*f**2+94.029866*f**3-32.355711*f**4:f<=1.31?l=6.198765-19.558097*f+23.186922*f**2-12.234627*f**3+2.423045*f**4:l=0}return{method:"Lilliefors (Kolmogorov-Smirnov) normality test",statistic:i,statisticName:"D",pValue:l,warnings:[]}}function Wb(e){let t=is(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(ah(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>le((u+1-3/8)/(n+1/4))),o=Se([t]).groups[0],s=Math.min(bl(o,r)**2/(st(o)*st(r)),1),i=Math.log(n),c=Math.log(i),a=-1.2725+1.0521*(c-i),l=1.0308-.26758*(c+2/i),f=se((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function jb(e,t={}){K(t,Pb,"pearsonTest");let n=is(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),s=t.adjust??!0;if(!(Number.isInteger(o)&&o-1-(s?2:0)>0))throw new RangeError("'nClasses' must be an integer leaving positive degrees of freedom");if(o>2147483647)throw new RangeError("attempt to make a table with >= 2^31 elements");let i=[];for(let f of xa(n)){let m=Math.min(o,Math.floor(1+o*se(f)));m>=1&&i.push(m)}i.sort((f,m)=>f-m);let c=r/o,a=0;for(let f=1,m=0;f<=o;f++){let u=0;for(;m<i.length&&i[m]===f;)u++,m++;a=a+(u-c)**2/c}let l=o-1-(s?2:0);return{method:"Pearson chi-square normality test",statistic:a,statisticName:"P",pValue:yt(a,l/2,2,!1,!1),warnings:[]}}var Kb=["weight"],Bb=["exact"],Ub=["model","type","unit","r0","confLevel"];function cs(e,t){let n=e[0]?.length??0;if(e.some(r=>r.length!==n))throw new TypeError(`'${t}' must be a matrix (rows of equal length)`);return Array.from({length:n},(r,o)=>e.map(s=>s[o]))}function Ra(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`);if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError(`'${t}' contains infinite values`)}var ch=e=>2*se(-Math.abs(e));function Hb(e,t={}){K(t,Kb,"kappa2");let n=Te(t.weight,["unweighted","equal","squared"]);if(e.some(x=>x.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(x=>!x.some(Number.isNaN)),o=r.length;if(o===0)throw new RangeError("no complete rating pairs");if(o<2)throw new RangeError("at least 2 complete rating pairs are needed");let s=r.map(x=>$n(x[0])),i=r.map(x=>$n(x[1])),c=[...new Set([...s,...i])].sort((x,E)=>x<E?-1:x>E?1:0),a=c.length,l=new Map(c.map((x,E)=>[x,E])),f=Array.from({length:a},()=>new Array(a).fill(0));for(let x=0;x<o;x++)f[l.get(s[x])][l.get(i[x])]++;let m=x=>n==="unweighted"?x===0?1:0:n==="equal"?1-x/(a-1):1-x*x/((a-1)*(a-1)),u=Array.from({length:a},(x,E)=>Array.from({length:a},(R,A)=>m(Math.abs(E-A)))),h=f.map(x=>Pe(x)/o),d=Array.from({length:a},(x,E)=>Pe(f.map(R=>R[E]))/o),w=x=>{let E=[];for(let R=0;R<a;R++)for(let A=0;A<a;A++)E.push(x(R,A));return Pe(E)},g=w((x,E)=>f[x][E]*u[x][E])/o,b=w((x,E)=>h[x]*d[E]*u[x][E]),N=(g-b)/(1-b),v=Array.from({length:a},(x,E)=>Pe(d.map((R,A)=>R*u[E][A]))),M=Array.from({length:a},(x,E)=>Pe(h.map((R,A)=>R*u[A][E]))),p=(w((x,E)=>h[x]*d[E]*(u[x][E]-(v[x]+M[E]))**2)-b*b)/(o*(1-b)**2),y=N/Math.sqrt(p);return{value:N,statistic:y,pValue:ch(y),warnings:[]}}function Yb(e,t={}){K(t,Bb,"kappamFleiss");let n=t.exact??!1,r=cs(e,"ratings");Ra(e,"ratings");let o=e.length,s=r.length;if(o<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let i=e.flat(),c=kt(i),a=[...new Set(c)].sort((p,y)=>p-y),l=new Map(a.map((p,y)=>[p,y])),f=new Map(i.map((p,y)=>[p,l.get(c[y])])),m=a.length,u=e.map(p=>{let y=new Array(m).fill(0);for(let x of p)y[f.get(x)]++;return y}),h=Pe(u.map(p=>(Pe(p.map(y=>y*y))-s)/(s*(s-1))))/o,d=Array.from({length:m},(p,y)=>Pe(u.map(x=>x[y]))/(o*s)),w=Pe(d.map(p=>p*p));if(n){let p=r.map(x=>{let E=new Array(m).fill(0);for(let R of x)E[f.get(R)]++;return E.map(R=>R/o)}),y=Array.from({length:m},(x,E)=>st(p.map(R=>R[E]))*(s-1)/s);w-=Pe(y)/(s-1)}let g=(h-w)/(1-w);if(n)return{value:g,warnings:[]};let b=Pe(d.map(p=>p*(1-p))),N=Pe(d.map(p=>p*(1-p)*(1-p-p))),v=2/(b*b*o*s*(s-1))*(b*b-N),M=g/Math.sqrt(v);return{value:g,statistic:M,pValue:ch(M),warnings:[]}}function Gb(e,t={}){K(t,Ub,"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;Lt(i),cs(e,"ratings"),Ra(e,"ratings");let c=Se(e).groups,a=cs(c,"ratings"),l=c.length,f=a.length;if(l<2||f<2)throw new RangeError("at least 2 subjects and 2 raters are needed");if(!(s>=-1&&s<1))throw new RangeError("'r0' must be in [-1, 1)");let m=1-i,u=st(c.map(D=>it(D)))*f,h=st(a.map(D=>it(D)))*l,d=it(c.map(D=>st(D))),w=st(c.flat())*(l*f-1),g=w-u*(l-1)-h*(f-1),b=g>2**-20*w?g/((l-1)*(f-1)):Jb(c)/((l-1)*(f-1)),N=(D,k,V)=>dt(D,k,V,!1,!1),v=(D,k)=>m/2<1e-8?pn(m/2,D,k,!1,!1):pn(1-m/2,D,k,!0,!1),M=l-1;if(n==="oneway"||r==="consistency"){let D=n==="oneway"?d:b,k=n==="oneway"?l*(f-1):(l-1)*(f-1),V=u/D,B=V/v(M,k),Q=V*v(k,M),[re,z,J]=o==="single"?[(u-D)/(u+(f-1)*D),V*(1-s)/(1+(f-1)*s),Z=>(Z-1)/(Z+f-1)]:[(u-D)/u,V*(1-s),Z=>1-1/Z];return{value:re,Fvalue:z,fValue:z,df1:M,df2:k,pValue:N(z,M,k),lbound:J(B),ubound:J(Q),warnings:[]}}let p=o==="single"?f:1,y=D=>[p*D/(l*(1-D)),1+p*D*(l-1)/(l*(1-D))],x=(D,k)=>yl([D*h,k*b],[f-1,(l-1)*(f-1)]),E=o==="single"?(u-b)/(u+(f-1)*b+f/l*(h-b)):(u-b)/(u+(h-b)/l),[R,A]=y(s),S=u/(R*h+A*b),T=x(R,A),[L,C]=[f*E/(l*(1-E)),1+f*E*(l-1)/(l*(1-E))],q=x(L,C),F=v(M,q),_=v(q,M),[O,I]=o==="single"?[l*(u-F*b)/(F*(f*h+(f*l-f-l)*b)+l*u),l*(_*u-b)/(f*h+(f*l-f-l)*b+l*_*u)]:[l*(u-F*b)/(F*(h-b)+l*u),l*(_*u-b)/(h-b+l*_*u)];return{value:E,Fvalue:S,fValue:S,df1:M,df2:T,pValue:N(S,M,T),lbound:O,ubound:I,warnings:[]}}function ih(e,t){let n=t.length,r=Pe(t.flatMap(s=>t.map(i=>e[s][i])));return(1-Pe(t.map(s=>e[s][s]))/r)*n/(n-1)}function Xb(e){let t=cs(e,"x").length;if(Ra(e,"x"),t<2)throw new RangeError("'x' needs at least two items");if(e.length<2)throw new RangeError("'x' needs at least two subjects");let n=Os(Se(e).groups),r=Co(e),o=Array.from({length:t},(c,a)=>a),s=Pe(r.flat()),i;try{let c=nt(r);i=o.map(a=>1-1/c[a][a])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let a=Se(e).groups;i=o.map(l=>{let f=a.map(h=>h[l]),m=Yr(At(a.map(h=>[1,...h.filter((d,w)=>w!==l)])),f),u=it(f);return 1-Pe(m.map(h=>h*h))/Pe(f.map(h=>(h-u)**2))})}return X("cronbachAlpha",{rawAlpha:ih(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-Pe(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>ih(n,o.filter(a=>a!==c))),warnings:Qb(e,r)})}function Qb(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}=jt(r.map(c=>r.map(a=>t[c][a])),{symmetric:!0}),s=o.map(c=>c[0]),i=Pe(s)<0?-1:1;s.some(c=>i*c<0)&&n.push(`Some items were negatively correlated with the first principal component and probably
|
|
12
12
|
should be reversed.
|
|
13
|
-
To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function Jb(e,t,n){let r=cs(e,"x").length;Ra(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=Co(e),s=[t,n],i=Array.from({length:r},(a,l)=>l).filter(a=>a!==t&&a!==n),c=s.map(a=>s.map(l=>o[a][l]));if(i.length>0){let a=i.map(m=>i.map(u=>o[m][u])),l=i.map(m=>s.map(u=>o[m][u])),f=nt(a,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=De(i.map((h,d)=>o[s[m]][h]*f[d][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function ev(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>he(l,[f,0]),[0,0]),o=e.map(a=>lt(r(a),n)),s=e[0].map((a,l)=>lt(r(e.map(f=>f[l])),t)),i=lt(r(e.flat()),t*n),c=[0,0];return e.forEach((a,l)=>a.forEach((f,m)=>{let u=he(Pe(Pe([f,0],o[l]),s[m]),i);c=he(c,_e(u,u))})),c[0]+c[1]}var tv=["controls","cases"],fh=["levels","direction","naRm"],nv=["partialAuc","focus","correct"],rv=["bestMethod","bestWeights"],gh=new WeakMap;function Sa(e){return gh.get(e)}var ov=["auto","<",">"];function _c(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:$n(e);if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="string")return e;throw new TypeError("the response must hold numbers, strings or booleans")}function sv(e){let t=e.filter(s=>_c(s)!==null),n=new Set(t.map(s=>typeof s));if(n.size>1)throw new TypeError("the response mixes values of different types");let r=[...n][0],o;return r==="number"?o=[...new Set(t)].sort((s,i)=>s-i).map($n):r==="boolean"?o=[...new Set(t)].sort((s,i)=>Number(s)-Number(i)).map(s=>s?"TRUE":"FALSE"):o=[...new Set(t)].sort((s,i)=>s<i?-1:s>i?1:0),[...new Set(o)]}function fs(e){return Float64Array.from(e).sort()}function Lc(e,t){let n=fs(e),r=fs(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 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),c=e.ctrlLE[o]-s,a=n==="<"?s:t-e.ctrlLE[o];r+=i*(2*a+c)}return r}function Oc(e,t,n){let r=e.length,o=t.length;return Cc(Lc(e,t),r,n)/(2*o*r)}function Fc(e,t,n){return Cc(Lc(e,t),e.length,n)}var mh=e=>e===0n?0:e.toString(2).length;function fo(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(mh(r)-mh(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=Le(Number(c),-o);return n?-a:a}function av(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 iv(e,t,n){let r=Lc(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:av(r.u[m-1],r.u[m],n),h=m===0?0:r.caseLE[m-1],d=m===0?0:r.ctrlLE[m-1];c.push(u),a.push((n==="<"?s-h:h)/s),l.push((n==="<"?d:o-d)/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:Cc(r,o,n)/(2*s*o)}}function cv(e,t){let n=r=>{let o=fs(r),s=o.length>>1;return o.length%2===1?o[s]:tn(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function wh(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"?cv(e,t):n,a={...iv(e,t,i),direction:i,levels:r,controls:e,cases:t,warnings:s};return gh.set(a,o),a}function bh(e){if(e===void 0)return"auto";if(!ov.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function co(e,t,n){let r=bh(n.direction),o=new mt,s=e.map(_c),i;if(n.levels===void 0){if(i=sv(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(d=>{let w=_c(d);if(w===null)throw new RangeError("'levels' must not be missing");return w})}if(t.some(d=>typeof d!="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((d,w)=>d===null||Number.isNaN(t[w]));if(n.naRm===!1&&c.some(d=>d))throw new RangeError("missing values in the response or the predictor with naRm false");let a=[],l=[],f=[],m=[],u=[];s.forEach((d,w)=>{if(c[w])return;let g=t[w];if(d===i[0])a.push(g);else if(d===i[1])l.push(g);else return;f.push(g),m.push(d===i[1]),u.push(w)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return wh(a,l,r,[i[0],i[1]],h,o.list)}function lv(e,t){let n=ks(Jn(e),Object.keys(t));if(n.response!==0||n.variables.length!==2||n.terms.length!==1)throw new TypeError("the formula must be of the form response ~ predictor");let r=Fl(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 fv(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function mv(e,t,n={}){if(vt(e)&&K(e,tv,"roc"),typeof e!="string"&&vt(t)&&K(t,fh,"roc"),K(n,fh,"roc"),typeof e=="string"){let r=lv(e,t);return X("roc",co(r.response,r.predictor,n))}if(fv(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=bh(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 X("roc",wh(a,l,s,["0","1"],m,[]))}return X("roc",co(Array.from(e),Array.from(t),n))}var Aa=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function uv(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=Aa(e,t,r,a,l),m=Aa(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=Aa(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=Aa(e,t,n,l,a);s+=(e[l]-n)*(t[l]+f)/2}return s}var pr=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],Or=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],qn=(e,t)=>[e[0]*t[0],e[1]*t[1]],uh=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],ls=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function hh(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 ph(e,t,n,r,o,s){let i=hh(e,n),c=hh(t,r);if(i!==void 0&&c!==void 0)return dh(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,d)=>h>=0&&h<=1&&(d===0||h>=e[d-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(lo),f=t.map(lo),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return dh(l.map(u),f.map(u),m,m,o,s)}function dh(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=lo(o),f=lo(s),m=(g,b,N)=>pr(c(b),qn(uh(Or(g,a(b)),Or(a(N),a(b))),Or(c(N),c(b)))),u=[];e.forEach((g,b)=>{ls(a(b),l)>=0&&ls(a(b),f)<=0&&u.push(b)});let h;if(u.length===0){let g=e.findIndex((N,v)=>ls(a(v),f)>=0),b=g-1;h=qn(pr(m(f,b,g),m(l,b,g)),qn(Or(f,l),[1n,2n]))}else{let g=0n;for(let v=1;v<u.length;v++){let M=u[v-1],p=u[v];g+=(e[p]-e[M])*(c(M)[0]+c(p)[0])}h=[g,2n*n*i];let b=u[u.length-1];if(ls(a(b),f)!==0){let v=b+1;h=pr(h,qn(Or(f,a(b)),qn(pr(c(b),m(f,b,v)),[1n,2n])))}let N=u[0];if(ls(a(N),l)!==0){let v=N-1;h=pr(h,qn(Or(a(N),l),qn(pr(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 d=qn(qn(Or(f,l),pr(f,l)),[1n,2n]),w=uh(pr(d,h),qn(d,[2n,1n]));return fo(w[0],w[1])}function hv(e,t={}){K(t,nv,"auc");let n=Te(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return X("auc",{auc:e.auc,partialAuc:null,focus:n,correct:r,warnings:[]});let o=t.partialAuc;if(!Array.isArray(o)||o.length!==2||o.some(d=>typeof d!="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"?ph(l,f,u,h,i,s):ph(l,f,h,u,i,s):m=uv(l,f,i,s),X("auc",{auc:m,partialAuc:[s,i],focus:n,correct:r,warnings:[]})}function lo(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 pv(e,t){let n=fs(e.controls),r=fs(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 dv(e,t="all",n={}){K(n,rv,"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 c=(1-i)/(s*i),a,l=null;if(typeof t=="string"&&(l=Te(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}=pv(e,a);if(l==="best"){let[g,b]=lo(s),[N,v]=lo(i),M=(v-N)*b,p=g*N,y=BigInt(f),x=BigInt(m),E=u.map((S,T)=>{if(r==="youden")return BigInt(S)*x*p+M*BigInt(h[T])*y;let L=BigInt(f-S),C=BigInt(m-h[T]);return-(L*L*x*x*p+M*C*C*y*y)}),R=E.reduce((S,T)=>T>S?T:S),A=E.map((S,T)=>S===R?T:-1).filter(S=>S>=0);a=A.map(S=>a[S]),u=A.map(S=>u[S]),h=A.map(S=>h[S])}let d=u.map(g=>f-g),w=h.map(g=>m-g);return X("coords",{threshold:a,sensitivity:u.map(g=>g/f),specificity:h.map(g=>g/m),accuracy:u.map((g,b)=>(g+h[b])/(f+m)),tn:h,tp:u,fn:d,fp:w,npv:h.map((g,b)=>g/(g+d[b])),ppv:u.map((g,b)=>g/(g+w[b])),fpr:w.map(g=>g/m),lrPos:u.map((g,b)=>g*m/(f*w[b])),lrNeg:d.map((g,b)=>g*m/(f*h[b])),youden:u.map((g,b)=>g/f+c*(h[b]/m)),closestTopleft:d.map((g,b)=>(g/f)**2+c*(w[b]/m)**2),warnings:[]})}var gv=["confLevel","method","bootN","bootStratified"],vh=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function qa(e){let t=Float64Array.from(e.controls).sort(),n=Float64Array.from(e.cases).sort(),r=t.length,o=n.length,s=(l,f)=>{let m=0,u=l.length;for(;m<u;){let d=m+u>>1;l[d]<f?m=d+1:u=d}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 $c(e,t,n,r){return st(e)/(4*n*n)/r+st(t)/(4*r*r)/n}function yh(e){let{k10:t,k01:n}=qa(e);return $c(t,n,e.controls.length,e.cases.length)}function Ta(e,t,n,r,o,s){let i=BigInt(Fc(e,t,n)),c=BigInt(Fc(r,o,s)),a=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return fo(i*l-c*a,2n*a*l)}var Dc=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function uo(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?le(t,{lowerTail:!1}):le(1-t)}function Eh(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function ho(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[qo(e.length)];return t}function Vc(e){let t=Sa(e);if(t!==void 0)return{values:t.values,isCase:t.isCase};let n=[...e.controls,...e.cases];return{values:n,isCase:n.map((r,o)=>o>=e.controls.length)}}function ms(e,t,n){let r=[],o=[];for(let s of n)(t[s]?o:r).push(e[s]);if(!(r.length===0||o.length===0))return{controls:r,cases:o}}function wv(e,t,n,r){let o=ms(e,t,n);return o===void 0?NaN:Oc(o.controls,o.cases,r)}function mo(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=qo(e);return t}function bv(e,t={}){K(t,gv,"ciAuc");let n=t.confLevel??.95;fn(n);let r=Te(t.method,["delong","bootstrap"]),o=new mt;Dc(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"){uo(e,"DeLong's interval");let{k10:m,k01:u}=qa(e),h=e.auc,d=$c(m,u,e.controls.length,e.cases.length),w=Math.sqrt(d),g=v=>Math.min(1,Math.max(0,v)),b=s/2,N=n===1?[0,1]:w===0?[h,h]:[g(h+le(b)*w),g(h+Mh(n)*w)];return X("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:d,warnings:o.list})}let i=t.bootN??2e3;Eh(i);let c=t.bootStratified??!0,a=[],l=0;if(c){uo(e,"a stratified bootstrap");for(let m=0;m<i;m++){let u=ho(e.controls),h=ho(e.cases);a.push(Oc(u,h,e.direction))}}else{let{values:m,isCase:u}=Vc(e);for(let h=0;h<i;h++){let d=wv(m,u,mo(m.length),e.direction);Number.isNaN(d)?l++:a.push(d)}}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=Ps(a,[s/2,1-s/2]);return X("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:a.length>1?st(a):NaN,warnings:o.list})}var Nh=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,ka=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function vv(e,t){let n=Sa(e),r=Sa(t);if(n?.labels==null||r?.labels==null||!ka(e.levels,t.levels))return;let o=c=>c.kept.map(a=>c.labels[a]);if(ka(o(n),o(r))&&ka(n.isCase,r.isCase))return[e,t];if(!ka(n.labels,r.labels))return;let s=n.labels.map((c,a)=>a).filter(c=>n.labels[c]!==null&&!Number.isNaN(n.predictor[c])&&!Number.isNaN(r.predictor[c])),i=(c,a)=>co(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 Pc(e,t){return t==="two.sided"?2*se(-Math.abs(e)):t==="greater"?se(e,{lowerTail:!1}):se(e)}function yv(e,t,n,r){vt(n)&&K(n,vh,"rocTest"),K(r,vh,"rocTest");let o,s=new mt,i,c;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,c=t}else{o=r??{};let p=Array.from(e),y=Array.from(t),x=Array.from(n);if(p.length!==y.length||p.length!==x.length)throw new TypeError("the response and the predictors must have the same length");let E=p.map((S,T)=>T).filter(S=>{let T=p[S];return T!=null&&!(typeof T=="number"&&Number.isNaN(T))&&!Number.isNaN(y[S])&&!Number.isNaN(x[S])}),R=E.map(S=>p[S]),A={levels:o.levels,direction:o.direction};i=co(R,E.map(S=>y[S]),A),c=co(R,E.map(S=>x[S]),A);for(let S of i.warnings)s.add(S)}let a=Ze(o.alternative),l=Te(o.method,["delong","bootstrap"]),f=o.confLevel??.95;fn(f),Dc(i)&&Dc(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=vv(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},d=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."),uo(i,"DeLong's test"),uo(c,"DeLong's test"),u){let L=qa(i),C=qa(c),q=L.k10.map((P,k)=>P-C.k10[k]),O=L.k01.map((P,k)=>P-C.k01[k]),_=Math.sqrt($c(q,O,i.controls.length,i.cases.length)),F=d===0&&_===0?0:d/_,I=_===0?0:Mh(f)*_;return X("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:F,statisticName:"Z",pValue:Pc(F,a),confInt:[d-I,d+I],confLevel:f,...h,warnings:s.list})}let p=yh(i),y=yh(c),x=i.controls.length+i.cases.length,E=c.controls.length+c.cases.length,R=p+y,A,S,T;return R===0?(A=d===0?0:d>0?1/0:-1/0,S=NaN,T=Pc(A,a)):(A=d/Math.sqrt(R),S=R*R/(p*p/(x-1)+y*y/(E-1)),T=a==="two.sided"?2*ft(-Math.abs(A),S,!0,!1):a==="greater"?ft(A,S,!1,!1):ft(A,S,!0,!1)),X("rocTest",{method:"DeLong's test for two ROC curves",statistic:A,statisticName:"D",parameter:S,parameterName:"df",pValue:T,...h,warnings:s.list})}let w=o.bootN??2e3;Eh(w);let g=o.bootStratified??!0,b=[],N=0;if(g){uo(i,"a stratified bootstrap"),uo(c,"a stratified bootstrap");for(let p=0;p<w;p++)if(u){let y=mo(i.controls.length),x=mo(i.cases.length);b.push(Ta(y.map(E=>i.controls[E]),x.map(E=>i.cases[E]),i.direction,y.map(E=>c.controls[E]),x.map(E=>c.cases[E]),c.direction))}else{let y=ho(i.controls),x=ho(i.cases),E=ho(c.controls),R=ho(c.cases);b.push(Ta(y,x,i.direction,E,R,c.direction))}}else{let p=Vc(i),y=Vc(c);for(let x=0;x<w;x++){let E,R;if(u){let A=mo(p.values.length);E=ms(p.values,p.isCase,A),R=ms(y.values,y.isCase,A)}else E=ms(p.values,p.isCase,mo(p.values.length)),R=ms(y.values,y.isCase,mo(y.values.length));E===void 0||R===void 0?N++:b.push(Ta(E.controls,E.cases,i.direction,R.controls,R.cases,c.direction))}}if(N>0&&s.add(`${N} NA value(s) produced during bootstrap were ignored.`),b.length<2)throw new Error("fewer than 2 bootstrap replicates had both controls and cases");let v=Math.sqrt(st(b)),M=d===0&&v===0?0:d/v;return X("rocTest",{method:u?"Bootstrap test for two correlated ROC curves":"Bootstrap test for two ROC curves",statistic:M,statisticName:"D",parameter:w,parameterName:"boot.n",pValue:Pc(M,a),bootStratified:g,...h,warnings:s.list})}var Nv=["method","confLevel","prevalence"];function Mv(e,t={}){K(t,Nv,"diagnosticTests");let n=Te(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(Lt(r),!Array.isArray(e)||e.length!==2||e.some(p=>!(Array.isArray(p)||ArrayBuffer.isView(p))||p.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(p=>typeof p!="number"))throw new TypeError("the counts must be numbers");if(!o.every(Mn))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 d=(p,y)=>{let x=rt(()=>yi(p,y,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:x.estimate,confInt:x.confInt}},w=1-r,g=w/2<1e-8?le(w/2,{lowerTail:!1}):le(1-w/2),b=(p,y)=>{let x=g*Math.sqrt(y);return{estimate:p,confInt:[Math.exp(Math.log(p)-x),Math.exp(Math.log(p)+x)]}},N=s/l,v=a/f,M={sensitivity:d(s,l),specificity:d(a,f),ppv:d(s,m),npv:d(a,u),lrPos:b(i===0?N/0:fo(BigInt(s)*BigInt(f),BigInt(l)*BigInt(i)),c/(s*l)+a/(i*f)),lrNeg:b(a===0?c/l/v:fo(BigInt(c)*BigInt(f),BigInt(l)*BigInt(a)),s/(c*l)+i/(a*f)),accuracy:d(s+a,h),diagnosticOR:b(s*a/(i*c),1/s+1/i+1/c+1/a),prevalence:d(l,h),apparentPrevalence:d(m,h),method:n,confLevel:r,warnings:[]};if(t.prevalence!==void 0){let p=t.prevalence;if(!(typeof p=="number"&&p>0&&p<1))throw new RangeError("'prevalence' must be a number in (0, 1)");let y=p<2**-500?2**600:1,x=N*(p*y),E=i/f*((1-p)*y),R=v*(1-p),A=c/l*p;M.ppvAtPrevalence=x/(x+E),M.npvAtPrevalence=R/(R+A)}return X("diagnosticTests",M)}var Ev=["two","mode","confLevel","ciMethod"],xv=["package","exact"],In=(e,t)=>t===0?0:e*t;function Rv(e,t,n={}){if(K(n,Ev,"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,xv),c=[],a=[];for(let _=0;_<e.length;_++){let F=e[_],I=t[_];if(typeof F!="number"||typeof I!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(F)||Number.isNaN(I))){if(!Number.isFinite(F)||!Number.isFinite(I))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(F),a.push(I)}}let l=c.length;if(l<2)throw new RangeError("fewer than 2 complete pairs");let[f,m]=o===1?[c,a]:[a,c],u=c.map((_,F)=>{let I=(_+a[F])/2;return Number.isFinite(I)?I:_/2+a[F]/2}),h=f.map((_,F)=>_-m[F]),d=!h.every(Number.isFinite),{groups:w,k:g}=Se([d?f.map((_,F)=>_/2-m[F]/2):h]),b=w[0],N=g+(d?1:0),v=it(b),M=Qn(b),p=r*M,y=v-p,x=v+p,E=l-1,R=(1-s)/2,A=bt(R,E,!0,!1),S=R<1e-8?bt(R,E,!1,!1):bt((s+1)/2,E,!0,!1),T=[v+In(A,M)/Math.sqrt(l),v+In(S,M)/Math.sqrt(l)],L,C;if(i==="package"){let _=3*M*M/l,F=_>=2**-1022||_===0?Math.sqrt(_):M*Math.sqrt(3/l);L=[y+In(A,F),y+In(S,F)],C=[x+In(A,F),x+In(S,F)]}else{let _=r*Math.sqrt(l),F=ja(R,E,_,!0,!1)/Math.sqrt(l),I=ja(R,E,_,!1,!1)/Math.sqrt(l);L=[v-In(I,M),v-In(F,M)],C=[v+In(F,M),v+In(I,M)]}let q=_=>D(_,N),O=_=>[q(_[0]),q(_[1])];return X("blandAltman",{means:u,diffs:h,n:l,meanDiff:q(v),lowerLimit:q(y),upperLimit:q(x),criticalDiff:q(p),meanDiffConfInt:O(T),lowerLimitConfInt:O(L),upperLimitConfInt:O(C),confLevel:s,two:r,mode:o,ciMethod:i,warnings:[]})}var Av=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),Sv=["PaBa","Deming","WDeming"],Tv=["analytical","jackknife","bootstrap","nestedbootstrap"],kv=["quantile","Student","BCa","tBoot"],xh=5793,qv=2**27;function jc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function Iv(e,t){let n=Xn(e),r=Xn(t),o=[0,0],s=[0,0],i=[0,0];for(let c=0;c<e.length;c++){let a=Pe([e[c],0],n),l=Pe([t[c],0],r);o=he(o,_e(a,a)),s=he(s,_e(l,l)),i=he(i,_e(a,l))}return{u:o,q:s,p:i,mx:n,my:r}}function Th(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 Kc(e,t,n){let r=e.length,{u:o,q:s,p:i,mx:c,my:a}=Iv(e,t),l=Th(o[0],s[0],i[0],n),f=Pe(a,_e([l,0],c))[0],m=_e(o,s),u=i[0]*i[0]/(o[0]*s[0]),h=Pe(m,_e(i,i))[0]/(m[0]+m[1]);Math.abs(h)<1e-12*(1+u)/2&&(h=0);let d=Math.sqrt(l*l*h/u/(r-2)),w=[0,0];for(let b of e)w=he(w,_e([b,0],[b,0]));let g=Math.sqrt(d*d*lt(w,r)[0]);return{b0:f,b1:l,se0:g,se1:d,xmean:c[0]}}function Rh(e,t,n,r,o,s,i,c,a){let l=e.length,f=Kc(e,t,n),m=f.b0,u=f.b1,h=NaN,d=new Array(l),w=s;for(let p=0;p<s;p++){let y=1+n*u*u,x=1/0;for(let I=0;I<l;I++){let P=t[I]-(m+u*e[I]),k=e[I]+n*u*P/y,V=t[I]-P/y;d[I]=r>1?k/r+V:k+r*V,x=Math.min(x,Math.abs(d[I]))}let E=[0,0],R=[0,0],A=[0,0];for(let I=0;I<l;I++){let P=x/d[I],k=[P*P,0];E=he(E,k),R=he(R,_e(k,[e[I],0])),A=he(A,_e(k,[t[I],0]))}let S=lt(R,E[0]+E[1]),T=lt(A,E[0]+E[1]),L=[0,0],C=[0,0],q=[0,0];for(let I=0;I<l;I++){let P=lt(Pe([e[I],0],S),d[I]),k=lt(Pe([t[I],0],T),d[I]);L=he(L,_e(P,P)),C=he(C,_e(k,k)),q=he(q,_e(P,k))}let O=Th(L[0],C[0],q[0],n),_=Pe(T,_e([O,0],S))[0];h=S[0];let F=D(Math.abs(u-O),a-c)<i&&D(Math.abs(m-_),a)<i;if(m=_,u=O,F){w=p+1;break}}let[g,b]=zc(o),[N,v]=zc(1+o),M=d.map(p=>{if(p===0)return 1/0;let[y,x]=zc(Math.abs(p));return r>1?Le((N/(g*y))**2,2*(v-b-x-a)):Le((N/y)**2,2*(v-x-c))});return{b0:m,b1:u,xmean:h,weights:M,converged:w<s}}function zc(e){let t=Math.floor(Math.log2(e));return[Le(e,-t),t]}var wo=e=>Math.atan(e),go=e=>Number.isFinite(e)?e:Math.tan(wo(e));function _v(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=jc(e[u],e[m]),d=jc(t[u],t[m]),w;if(h!==0)w=d/h;else if(d!==0)w=o?1/0:-1/0;else continue;s[i++]=w;let g=wo(w);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 Lv(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?go(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?(go(s)+go(i))/2:Math.tan((wo(s)+wo(i))/2)}function Cv(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)?Cv(e.map((r,o)=>jc(t[o],n*r))):-n}function Ah(e,t,n){let r=_v(e,t),o=Lv(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:Uc(e,t,o),b1:o,slopes:r}}function Ov(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=(d,w)=>{let g=Math.trunc((d+1)/2);if(w?g<2*a+1:g>l)return NaN;if(d%2===0){if(g+1>l)return NaN;let b=i[g-1],N=i[g];return o?(go(b)+go(N))/2:Math.tan((wo(b)+wo(N))/2)}return go(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 Fv(e,t,n,r){let o=e.length,s=e.map((d,w)=>t[w]-n-r*d),i=s.filter(d=>d>0).length,c=s.filter(d=>d<0).length,a=Math.sqrt(c/i),l=-Math.sqrt(i/c),f=e.map((d,w)=>(t[w]+d/r-n)/Math.sqrt(1+1/(r*r))),m=Array.from({length:o},(d,w)=>w).sort((d,w)=>f[d]-f[w]||d-w),u=0,h=0;for(let d of m){let w=s[d];u+=w>0?a:w<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:i,nNeg:c,maxCusum:h}}var Ia=e=>e.every(t=>t===e[0]);function Wc(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var po=(e,t)=>Ps(e,t),Sh=e=>Qn(e.filter(t=>!Number.isNaN(t)));function Pv(e,t,n={}){K(n,Av,"mcreg");let r=Te(n.methodReg,Sv),o=Te(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),Tv),s=o==="bootstrap"||o==="nestedbootstrap",i=Te(n.methodBootstrapCi,kv),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=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=Wc(n.nsamples??999,"nsamples"),h=Wc(n.nnested??25,"nnested"),d=Wc(n.iterMax??30,"iterMax"),w=n.threshold??1e-6;if(!(typeof w=="number"&&w>=0&&w<1/0))throw new RangeError("'threshold' must be a nonnegative number");if(r==="WDeming"&&o==="analytical")throw new RangeError('there is no analytical interval for weighted Deming regression (use methodCi "jackknife" or a bootstrap)');if(o==="bootstrap"&&i==="tBoot"&&r!=="Deming")throw new RangeError(`the bootstrap-t interval of ${r} needs methodCi "nestedbootstrap" (there is no analytical standard error)`);if(e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let g=[],b=[];for(let ne=0;ne<e.length;ne++){let $=e[ne],Y=t[ne];if(typeof $!="number"||typeof Y!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN($)||Number.isNaN(Y)){if(n.naRm===!0)continue;throw new RangeError("'x' or 'y' has missing values (use naRm: true to drop the incomplete pairs)")}if(!Number.isFinite($)||!Number.isFinite(Y))throw new RangeError("'x' and 'y' must not contain infinite values");g.push($),b.push(Y)}let N=g.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(Ia(g)||Ia(b))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(g.some($=>$<0)||b.some($=>$<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>xh)throw new RangeError(`Passing-Bablok regression is limited to ${xh} 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)>qv)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(ne=>ne<=0)||b.some(ne=>ne<=0)))throw new RangeError("weighted Deming regression needs positive values");let v=Se([g,b]),[M,p]=f?[Se([g]).k,Se([b]).k]:[v.k,v.k],y=f?g.map(ne=>D(ne,-M)):v.groups[0],x=f?b.map(ne=>D(ne,-p)):v.groups[1],E=Math.min(Math.max(Le(m,2*(p-M)),Number.MIN_VALUE),2**1e3),R=Le(m,p-M),A=new Set,S=`no konvergence after ${d} iterations`,T=(ne,$)=>{if(r==="Deming")return Kc(ne,$,E);if(r==="WDeming"){let Y=Rh(ne,$,E,R,m,d,w,M,p);return Y.converged||A.add(S),Y}return Ah(ne,$,l)},L,C=NaN,q,O,_;if(r==="Deming"){let ne=Kc(y,x,E);L=ne,C=D(ne.xmean,M),O=[ne.se0,ne.se1]}else if(r==="WDeming"){let ne=Rh(y,x,E,R,m,d,w,M,p);ne.converged||A.add(S),L=ne,C=D(ne.xmean,M),q=ne.weights}else{let ne=Ah(y,x,l);L=ne,_=ne.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 F=1-c,I=bt(F/2,N-2,!1,!1),P=le(F/2,{lowerTail:!1}),k=[L.b0,L.b1],V=[NaN,NaN],U=[[NaN,NaN],[NaN,NaN]],Q,re,z,J=()=>{let ne=[],$=[];for(let Y=0;Y<N;Y++){let W=T(y.filter((B,j)=>j!==Y),x.filter((B,j)=>j!==Y));ne.push(W.b0),$.push(W.b1)}Q=[ne,$],re=[(N-1)*Qn(ne)/Math.sqrt(N),(N-1)*Qn($)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let ne of[0,1])V[ne]=O[ne],U[ne]=[k[ne]-I*V[ne],k[ne]+I*V[ne]];else{let ne=Ov(y,x,_,P,l);ne.defined||A.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),U[0]=ne.intercept,U[1]=ne.slope}else if(o==="jackknife"){J();for(let ne of[0,1])V[ne]=re[ne],U[ne]=[k[ne]-I*V[ne],k[ne]+I*V[ne]]}else{if(i==="BCa"&&(J(),Q.some(de=>de.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let ne=r==="Deming"?O:r==="WDeming"&&re!==void 0?re:[NaN,NaN],$=o==="nestedbootstrap",Y=[],W=[],B=[],j=[],te=0,fe=()=>Array.from({length:N},()=>qo(N)),Re=de=>{let Oe=de.map(Ve=>y[Ve]),xe=de.map(Ve=>x[Ve]);if(Ia(Oe)||Ia(xe)){if(r==="PaBa")return A.add("Resampling PaBa regression: x or y coordinates of all data points in subsample identical - regression coefficients undetermined!"),{b0:NaN,b1:NaN};A.add("Resampling Deming regression: all x coordinates in subsample identical - regression coefficients undetermined!")}return T(Oe,xe)},Ce=()=>{for(let de=0;de<u;de++){let Oe=fe(),xe=Re(Oe),Ve;if($){let Fe=[],Ue=[];for(let ze=0;ze<h;ze++){let et=Re(fe().map(Be=>Oe[Be]));Fe.push(et.b0),Ue.push(et.b1)}Ve=[Sh(Fe),Sh(Ue)]}else r==="Deming"?Ve=[xe.se0,xe.se1]:Ve=ne;if(Number.isNaN(xe.b0)||Number.isNaN(xe.b1)){te++;continue}Y.push(xe.b0),W.push(xe.b1),B.push(Ve[0]),j.push(Ve[1])}};if(n.rngSeed!==void 0){let de=Tl()?Il():void 0;try{ql({kind:"Mersenne-Twister"}),kl(n.rngSeed),Ce()}finally{de!==void 0&&_l(de)}}else Ce();if(te>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let be=Y.length,me=[Y,W],qe=[B,j];for(let de of[0,1]){let Oe=me[de],xe=k[de];if(i==="quantile")U[de]=[po(Oe,F/2),po(Oe,1-F/2)];else if(i==="Student")V[de]=Qn(Oe),U[de]=[xe-I*V[de],xe+I*V[de]];else if(i==="BCa"){let Ve=Oe.filter(ce=>ce<xe).length;Oe.every(ce=>ce===xe)?Ve=be/2:Ve===0?Ve=.5:Ve===be&&(Ve=be-.5);let Fe=le(Ve/be),Ue=Q[de],ze=Ue.reduce((ce,tt)=>ce+tt,0)/Ue.length,et=0,Be=0;for(let ce of Ue){let tt=ze-ce;et+=tt*tt,Be+=tt*tt*tt}let Xe=et===0?0:Be/(6*et**1.5),pe=-P,oe=P,we=se(Fe-(pe-Fe)/(1+Xe*(pe-Fe))),ge=se(Fe-(oe-Fe)/(1+Xe*(oe-Fe)));U[de]=[po(Oe,ge),po(Oe,we)]}else{let Ve=Oe.map((Ue,ze)=>(Ue-xe)/qe[de][ze]),Fe=Number.isNaN(ne[de])?Qn(Oe):ne[de];V[de]=Fe,U[de]=[xe-po(Ve,1-F/2)*Fe,xe-po(Ve,F/2)*Fe]}}z={intercept:Y.map(de=>D(de,p)),slope:W.map(de=>D(de,p-M)),sigmaIntercept:B.map(de=>D(de,p)),sigmaSlope:j.map(de=>D(de,p-M)),globalSigma:[D(ne[0],p),D(ne[1],p-M)],nsamples:be}}let Z=ne=>{let $=Y=>D(Y,ne===0?p:p-M);return{estimate:$(k[ne]),se:$(V[ne]),confInt:[$(U[ne][0]),$(U[ne][1])]}},ke={methodReg:r,methodCi:o,...s?{methodBootstrapCi:i}:{},confLevel:c,errorRatio:m,n:N,intercept:Z(0),slope:Z(1),xmean:C,...q===void 0?{}:{weights:q},...Q===void 0?{}:{jackknife:{intercept:Q[0].map(ne=>D(ne,p)),slope:Q[1].map(ne=>D(ne,p-M))}},...z===void 0?{}:{bootstrap:z},...r==="PaBa"?{cusum:Fv(y,x,L.b0,L.b1)}:{},warnings:[...A]};return X("mcreg",ke)}var Dv=["alpha","confLevel","lmer"],Vv={ICC1:"Single_raters_absolute",ICC2:"Single_random_raters",ICC3:"Single_fixed_raters",ICC1k:"Average_raters_absolute",ICC2k:"Average_random_raters",ICC3k:"Average_fixed_raters"};function _a(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function $v(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,i]=_a(r,n[0]),[c,a]=_a(t[o],t[0]);return s===c&&i===a}))}var kh=(e,t)=>e.every((n,r)=>n===t[r]);function Fr(e){let t=[0,0];for(let n of e)t=he(t,n);return lt(t,e.length)}var bo=(e,t)=>({v:lt(e.v,t),e:e.e});function vo(...e){let t=e.filter(r=>r.v[0]!==0),n=t.length===0?0:Math.max(...t.map(r=>r.e));return e.map(r=>({at:Le(r.v[0],r.e-n),E:n}))}function qh(e,t){if(t.v[0]===0)return e;if(e.v[0]===0)return t;let n=Math.max(e.e,t.e),r=o=>[Le(o.v[0],o.e-n),Le(o.v[1],o.e-n)];return{v:he(r(e),r(t)),e:n}}var Ih=(e,t)=>Le(e.v[0]/t.v[0],e.e-t.e),zv=(e,t)=>{let[n,r]=vo(e,t);return n.at<r.at},Wv=(e,t)=>{let[n,r]=vo(e,t);return n.at<=r.at};function _h(e,t,n){let r=[0,0];for(let o of e){let s=Pe(o,t);r=he(r,_e(s,s))}return _e(r,[n,0])}function jv(e,t={}){K(t,Dv,"ICC");let n=t.lmer??!0;if(typeof n!="boolean")throw new TypeError("'lmer' must be true or false");let r,o;if(t.alpha!==void 0){if(r=t.alpha,!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'alpha' must be a single number in (0, 1)");o=1-r}else o=t.confLevel??.95,Lt(o),r=1-o;if(!Array.isArray(e)||e.length===0||!e.every(ee=>Array.isArray(ee)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(ee=>ee.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(ee=>ee.some(ve=>typeof ve!="number")))throw new TypeError("'x' must contain numbers");if(e.some(ee=>ee.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(ee=>ee.some(ve=>!Number.isFinite(ve))))throw new RangeError("'x' contains infinite ratings");let i=e.length;if(i<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let{groups:c}=Se(e);if(c.every(ee=>kh(ee,c[0])))throw new RangeError(c[0].every(ee=>ee===c[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let a=Array.from({length:s},(ee,ve)=>c.map(ie=>ie[ve])),l=0;for(let ee of e)for(let ve of ee)l=Math.max(l,Math.abs(ve));let f=l>2**1020?3:0,m=f>0?e.map(ee=>ee.map(ve=>ve/8)):e,u=m.map(ee=>ee.map((ve,ie)=>_a(ve,m[0][ie]))),h=m.map(ee=>ee.map(ve=>_a(ve,ee[0]))),d=h.map(ee=>ee.map((ve,ie)=>Pe(ve,h[0][ie]))),w=ee=>{let ve=0;for(let ie of ee)for(let Ae of ie)ve=Math.max(ve,Math.abs(Ae[0]));return ve===0?0:Math.floor(Math.log2(ve))},g=(ee,ve)=>ee.map(ie=>ie.map(Ae=>[D(Ae[0],-ve),D(Ae[1],-ve)])),[b,N,v]=[w(u),w(h),w(d)],[M,p,y]=[g(u,b),g(h,N),g(d,v)],x=ee=>Array.from({length:s},(ve,ie)=>ee.map(Ae=>Ae[ie])),E=_h(M.map(Fr),Fr(M.flat()),s),R=a.every(ee=>kh(ee,a[0]))?[0,0]:_h(x(p).map(Fr),Fr(p.flat()),i),A=[0,0];if(!$v(c)){let ee=y.map(Fr),ve=x(y).map(Fr),ie=Fr(y.flat());y.forEach((Ae,Ke)=>Ae.forEach(($e,ot)=>{let ht=he(Pe(Pe($e,ee[Ke]),ve[ot]),ie);A=he(A,_e(ht,ht))}))}let S=(ee,ve)=>({v:ee,e:2*(ve+f)}),T=S(E,b),L=S(R,N),C=S(A,v),q=i-1,O=s-1,_=(i-1)*(s-1),F=bo(T,q),I=bo(L,O),P=bo(C,_),k=bo(qh(L,C),O+_),V=[];if(n){let ee={B:{ss:T,df:q,ms:F,pooled:!1},J:{ss:L,df:O,ms:I,pooled:!1}},ve=C,ie=_;for(let Ke of Wv(F,I)?["B","J"]:["J","B"]){let $e=ee[Ke];zv($e.ms,bo(ve,ie))&&(ve=qh(ve,$e.ss),ie+=$e.df,$e.pooled=!0)}let Ae=bo(ve,ie);if(ee.B.pooled&&(F=Ae),ee.J.pooled&&(I=Ae),P=Ae,ee.B.pooled){let[Ke,$e]=vo(P,I);k={v:[Ke.at+($e.at-Ke.at)/i,0],e:Ke.E}}}else A[0]===0&&V.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let U=(ee,ve)=>r/2<1e-8?pn(r/2,ee,ve,!1,!1):pn(1-r/2,ee,ve,!0,!1),Q=(ee,ve,ie)=>dt(ee,ve,ie,!1,!1),re=ee=>ee===1/0?1:(ee-1)/(ee+s-1),z=ee=>1-1/ee,J=i*(s-1),Z=Ih(F,k),ke=Ih(F,P),[ne,$]=[Z/U(q,J),Z*U(J,q)],[Y,W]=[ke/U(q,_),ke*U(_,q)],[B,j]=vo(F,k).map(ee=>ee.at),[te,fe]=vo(F,P).map(ee=>ee.at),[Re,Ce,be]=vo(F,I,P).map(ee=>ee.at),me=(B-j)/(B+(s-1)*j),qe=(Re-be)/(Re+(s-1)*be+s*(Ce-be)/i),de=(te-fe)/(te+(s-1)*fe),Oe=s*qe*Ce,xe=(i*(1+(s-1)*qe)-s*qe)*be,Ve=Math.max(Math.abs(Oe),Math.abs(xe)),Fe=Ve>0&&Number.isFinite(Ve)?Math.floor(Math.log2(Ve)):0,Ue=D(Oe,-Fe),ze=D(xe,-Fe),et=(i-1)*Ue*Ue+ze*ze,Be=et===0?s-1:(s-1)*(i-1)*(Ue+ze)**2/et,Xe=U(q,Be),pe=U(Be,q),oe=s*Ce+(s*i-s-i)*be,we=(ee,ve)=>ee>.5?1-(i*be+oe)/ve:ee,ge=we(i*(Re/Xe-be)/(oe+i*Re/Xe),oe+i*Re/Xe),ce=pe>1?we(i*(Re-be/pe)/(oe/pe+i*Re),oe+i*pe*Re):we(i*(pe*Re-be)/(oe+i*pe*Re),oe+i*pe*Re),tt=ee=>ee*s/(1+ee*(s-1)),G=Q(Z,q,J),ue=Q(ke,q,_),Me=(ee,ve,ie,Ae,Ke,$e,ot)=>({type:ee,name:Vv[ee],ICC:ve,F:ie,df1:q,df2:Ae,pValue:Ke,lower:$e,upper:ot}),ye=[Me("ICC1",me,Z,J,G,re(ne),re($)),Me("ICC2",qe,ke,_,ue,ge,ce),Me("ICC3",de,ke,_,ue,re(Y),re(W)),Me("ICC1k",(B-j)/B,Z,J,G,z(ne),z($)),Me("ICC2k",(Re-be)/(Re+(Ce-be)/i),ke,_,ue,tt(ge),tt(ce)),Me("ICC3k",(te-fe)/te,ke,_,ue,z(Y),z(W))],Ee=ee=>Le(ee.v[0],ee.e);return X("ICC",{results:ye,MSB:Ee(F),MSJ:Ee(I),MSE:Ee(P),MSW:Ee(k),n:i,k:s,confLevel:o,lmer:n,warnings:V})}function La(e){let t=e.length;if(t===0||e.some(n=>n.length!==t))throw new RangeError("'covmat' must be a square matrix");if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError("'covmat' must be finite");if(e.some((n,r)=>!(n[r]>0)))throw new RangeError("the diagonal of 'covmat' must be positive");if(!Gl(jl(e)))throw new RangeError("'covmat' must be symmetric")}var Kv=["covmat","nObs","rotation","scores","control"],Uv=["lower","start"];function St(e){let t=0,n=0;for(let r of e){let[o,s]=Ft(t,r);t=o,n+=s}return t+n}var Bc=(e,t)=>e[0].map((n,r)=>St(e.map(o=>t(o[r]))));function Oh(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 Lh(e,t,n){let r=jt(Oh(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 Ch(e,t){let n=Bc(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=Bc(o,c=>c).map(c=>c<0?-1:1);return o=o.map(c=>c.map((a,l)=>i[l]<0?-a:a)),s=s?.map(c=>c.map((a,l)=>i[l]<0?-a:a)),s===void 0?{L:o}:{L:o,rot:s}}var _n=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let i=0;i<n.length;i++)s+=n[i]*t[i][o];return s})),yo=e=>e[0].map((t,n)=>e.map(r=>r[n]));function Ca(e){let t=e[0].length,n=e.map(m=>Math.sqrt(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,d)=>u===d?1:0));if(s===0)return{loadings:e.map(m=>m.slice()),rotmat:i};let c=yo(o),a=0;for(let m=0;m<1e3;m++){let u=_n(o,i),h=u[0].map((N,v)=>{let M=0;for(let p=0;p<s;p++)M+=u[p][v]*u[p][v];return M}),d=u.map(N=>N.map((v,M)=>v**3-v*h[M]/s)),w=_n(c,d),g=Os(w);i=_n(g.u,yo(g.v));let b=a;if(a=St(g.d),a<b*(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 Bv(e){let t=Ca(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=Hr(At(n),r),s=nt(_n(yo(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:_n(n,i),rotmat:_n(t.rotmat,i)}}function Hv(e,t,n={}){K(n,Kv,"factanal"),K(sn(n,"control"),Uv,"factanal","control");let r=n.scores??"none";if(r!=="none"&&r!=="regression"&&r!=="Bartlett")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Cregression\u201D, \u201CBartlett\u201D");let o,s;if(n.covmat!==void 0){if(r!=="none")throw new RangeError("requested scores without an 'x' matrix");La(n.covmat),o=n.covmat.map(x=>x.slice()),s=n.nObs??NaN}else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");o=Fs(e),s=e.length}let i=o.length;if(i<3)throw new RangeError("factor analysis requires at least three variables");let c=t;if(!Number.isInteger(c)||c<1)throw new RangeError("'factors' must be a positive integer");let a=((i-c)**2-i-c)/2;if(a<0)throw new RangeError(`${c} ${c===1?"factor is":"factors are"} too many for ${i} variables`);let l=o.map((x,E)=>Math.sqrt(x[E])),f=o.map((x,E)=>x.map((R,A)=>R/(l[E]*l[A]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??nt(f).map((x,E)=>(1-c/(2*i))/x[E]),h=x=>{let E=jt(Oh(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(St(E.map(R=>Math.log(R)-R))-c+i)},d=x=>{let E=Lh(f,x,c);return x.map((R,A)=>{let S=0;for(let T=0;T<c;T++)S+=E[A][T]*E[A][T];return(S+R-f[A][A])/(R*R)})},w=rt(()=>Oo(u,h,{gr:d,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(w.convergence!==0)throw new Error("unable to optimize from this starting value");let g=w.par,b=Ch(Lh(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"?Ca(b):Bv(b),E=Ch(x.loadings,x.rotmat);b=E.L,N=E.rot}let M=NaN,p=NaN;!Number.isNaN(s)&&a>0&&(M=(s-1-(2*i+5)/6-2*c/3)*w.value,p=_t(M,a,{lowerTail:!1}));let y={uniquenesses:g,loadings:b,STATISTIC:M,dof:a,PVAL:p,converged:!0,objective:w.value,counts:w.counts,warnings:[]};return r!=="none"&&(y.scores=Yv(e,f,g,b,r)),N!==void 0&&(y.rotmat=N),y}function Yv(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=Bc(c,m=>m*m).map(m=>Math.sqrt(m/(s-1))),l=c.map(m=>m.map((u,h)=>u/a[h]));if(o==="regression")return _n(l,nt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return _n(l,yo(nt(_n(yo(r),f),yo(f))))}var Gv=["nfactors","covmat"],No=2**-52,Xv=`
|
|
13
|
+
To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function Zb(e,t,n){let r=cs(e,"x").length;Ra(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=Co(e),s=[t,n],i=Array.from({length:r},(a,l)=>l).filter(a=>a!==t&&a!==n),c=s.map(a=>s.map(l=>o[a][l]));if(i.length>0){let a=i.map(m=>i.map(u=>o[m][u])),l=i.map(m=>s.map(u=>o[m][u])),f=nt(a,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=Pe(i.map((h,d)=>o[s[m]][h]*f[d][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function Jb(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>he(l,[f,0]),[0,0]),o=e.map(a=>lt(r(a),n)),s=e[0].map((a,l)=>lt(r(e.map(f=>f[l])),t)),i=lt(r(e.flat()),t*n),c=[0,0];return e.forEach((a,l)=>a.forEach((f,m)=>{let u=he(De(De([f,0],o[l]),s[m]),i);c=he(c,_e(u,u))})),c[0]+c[1]}var ev=["controls","cases"],lh=["levels","direction","naRm"],tv=["partialAuc","focus","correct"],nv=["bestMethod","bestWeights"],dh=new WeakMap;function Sa(e){return dh.get(e)}var rv=["auto","<",">"];function _c(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:$n(e);if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="string")return e;throw new TypeError("the response must hold numbers, strings or booleans")}function ov(e){let t=e.filter(s=>_c(s)!==null),n=new Set(t.map(s=>typeof s));if(n.size>1)throw new TypeError("the response mixes values of different types");let r=[...n][0],o;return r==="number"?o=[...new Set(t)].sort((s,i)=>s-i).map($n):r==="boolean"?o=[...new Set(t)].sort((s,i)=>Number(s)-Number(i)).map(s=>s?"TRUE":"FALSE"):o=[...new Set(t)].sort((s,i)=>s<i?-1:s>i?1:0),[...new Set(o)]}function fs(e){return Float64Array.from(e).sort()}function Lc(e,t){let n=fs(e),r=fs(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 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),c=e.ctrlLE[o]-s,a=n==="<"?s:t-e.ctrlLE[o];r+=i*(2*a+c)}return r}function Fc(e,t,n){let r=e.length,o=t.length;return Cc(Lc(e,t),r,n)/(2*o*r)}function Oc(e,t,n){return Cc(Lc(e,t),e.length,n)}var fh=e=>e===0n?0:e.toString(2).length;function fo(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),c=s/i;c*i!==s&&(c|=1n);let a=Le(Number(c),-o);return n?-a:a}function sv(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 av(e,t,n){let r=Lc(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:sv(r.u[m-1],r.u[m],n),h=m===0?0:r.caseLE[m-1],d=m===0?0:r.ctrlLE[m-1];c.push(u),a.push((n==="<"?s-h:h)/s),l.push((n==="<"?d:o-d)/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:Cc(r,o,n)/(2*s*o)}}function iv(e,t){let n=r=>{let o=fs(r),s=o.length>>1;return o.length%2===1?o[s]:tn(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"?iv(e,t):n,a={...av(e,t,i),direction:i,levels:r,controls:e,cases:t,warnings:s};return dh.set(a,o),a}function wh(e){if(e===void 0)return"auto";if(!rv.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function co(e,t,n){let r=wh(n.direction),o=new mt,s=e.map(_c),i;if(n.levels===void 0){if(i=ov(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(d=>{let w=_c(d);if(w===null)throw new RangeError("'levels' must not be missing");return w})}if(t.some(d=>typeof d!="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((d,w)=>d===null||Number.isNaN(t[w]));if(n.naRm===!1&&c.some(d=>d))throw new RangeError("missing values in the response or the predictor with naRm false");let a=[],l=[],f=[],m=[],u=[];s.forEach((d,w)=>{if(c[w])return;let g=t[w];if(d===i[0])a.push(g);else if(d===i[1])l.push(g);else return;f.push(g),m.push(d===i[1]),u.push(w)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return gh(a,l,r,[i[0],i[1]],h,o.list)}function cv(e,t){let n=ks(Jn(e),Object.keys(t));if(n.response!==0||n.variables.length!==2||n.terms.length!==1)throw new TypeError("the formula must be of the form response ~ predictor");let r=Ol(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 lv(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function fv(e,t,n={}){if(vt(e)&&K(e,ev,"roc"),typeof e!="string"&&vt(t)&&K(t,lh,"roc"),K(n,lh,"roc"),typeof e=="string"){let r=cv(e,t);return X("roc",co(r.response,r.predictor,n))}if(lv(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),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 X("roc",gh(a,l,s,["0","1"],m,[]))}return X("roc",co(Array.from(e),Array.from(t),n))}var Aa=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function mv(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=Aa(e,t,r,a,l),m=Aa(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=Aa(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=Aa(e,t,n,l,a);s+=(e[l]-n)*(t[l]+f)/2}return s}var pr=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],Fr=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],qn=(e,t)=>[e[0]*t[0],e[1]*t[1]],mh=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],ls=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function 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),c=uh(t,r);if(i!==void 0&&c!==void 0)return ph(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,d)=>h>=0&&h<=1&&(d===0||h>=e[d-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(lo),f=t.map(lo),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,c=g=>[t[g]*n+e[g]*r-i,i],a=g=>[e[g],n],l=lo(o),f=lo(s),m=(g,b,N)=>pr(c(b),qn(mh(Fr(g,a(b)),Fr(a(N),a(b))),Fr(c(N),c(b)))),u=[];e.forEach((g,b)=>{ls(a(b),l)>=0&&ls(a(b),f)<=0&&u.push(b)});let h;if(u.length===0){let g=e.findIndex((N,v)=>ls(a(v),f)>=0),b=g-1;h=qn(pr(m(f,b,g),m(l,b,g)),qn(Fr(f,l),[1n,2n]))}else{let g=0n;for(let v=1;v<u.length;v++){let M=u[v-1],p=u[v];g+=(e[p]-e[M])*(c(M)[0]+c(p)[0])}h=[g,2n*n*i];let b=u[u.length-1];if(ls(a(b),f)!==0){let v=b+1;h=pr(h,qn(Fr(f,a(b)),qn(pr(c(b),m(f,b,v)),[1n,2n])))}let N=u[0];if(ls(a(N),l)!==0){let v=N-1;h=pr(h,qn(Fr(a(N),l),qn(pr(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 d=qn(qn(Fr(f,l),pr(f,l)),[1n,2n]),w=mh(pr(d,h),qn(d,[2n,1n]));return fo(w[0],w[1])}function uv(e,t={}){K(t,tv,"auc");let n=Te(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return X("auc",{auc:e.auc,partialAuc:null,focus:n,correct:r,warnings:[]});let o=t.partialAuc;if(!Array.isArray(o)||o.length!==2||o.some(d=>typeof d!="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"?hh(l,f,u,h,i,s):hh(l,f,h,u,i,s):m=mv(l,f,i,s),X("auc",{auc:m,partialAuc:[s,i],focus:n,correct:r,warnings:[]})}function lo(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 hv(e,t){let n=fs(e.controls),r=fs(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 pv(e,t="all",n={}){K(n,nv,"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 c=(1-i)/(s*i),a,l=null;if(typeof t=="string"&&(l=Te(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}=hv(e,a);if(l==="best"){let[g,b]=lo(s),[N,v]=lo(i),M=(v-N)*b,p=g*N,y=BigInt(f),x=BigInt(m),E=u.map((S,T)=>{if(r==="youden")return BigInt(S)*x*p+M*BigInt(h[T])*y;let L=BigInt(f-S),C=BigInt(m-h[T]);return-(L*L*x*x*p+M*C*C*y*y)}),R=E.reduce((S,T)=>T>S?T:S),A=E.map((S,T)=>S===R?T:-1).filter(S=>S>=0);a=A.map(S=>a[S]),u=A.map(S=>u[S]),h=A.map(S=>h[S])}let d=u.map(g=>f-g),w=h.map(g=>m-g);return X("coords",{threshold:a,sensitivity:u.map(g=>g/f),specificity:h.map(g=>g/m),accuracy:u.map((g,b)=>(g+h[b])/(f+m)),tn:h,tp:u,fn:d,fp:w,npv:h.map((g,b)=>g/(g+d[b])),ppv:u.map((g,b)=>g/(g+w[b])),fpr:w.map(g=>g/m),lrPos:u.map((g,b)=>g*m/(f*w[b])),lrNeg:d.map((g,b)=>g*m/(f*h[b])),youden:u.map((g,b)=>g/f+c*(h[b]/m)),closestTopleft:d.map((g,b)=>(g/f)**2+c*(w[b]/m)**2),warnings:[]})}var dv=["confLevel","method","bootN","bootStratified"],bh=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function qa(e){let t=Float64Array.from(e.controls).sort(),n=Float64Array.from(e.cases).sort(),r=t.length,o=n.length,s=(l,f)=>{let m=0,u=l.length;for(;m<u;){let d=m+u>>1;l[d]<f?m=d+1:u=d}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 $c(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}=qa(e);return $c(t,n,e.controls.length,e.cases.length)}function Ta(e,t,n,r,o,s){let i=BigInt(Oc(e,t,n)),c=BigInt(Oc(r,o,s)),a=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return fo(i*l-c*a,2n*a*l)}var Pc=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function uo(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function Nh(e){let t=(1-e)/2;return t<1e-8?le(t,{lowerTail:!1}):le(1-t)}function Mh(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function ho(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[qo(e.length)];return t}function Vc(e){let t=Sa(e);if(t!==void 0)return{values:t.values,isCase:t.isCase};let n=[...e.controls,...e.cases];return{values:n,isCase:n.map((r,o)=>o>=e.controls.length)}}function ms(e,t,n){let r=[],o=[];for(let s of n)(t[s]?o:r).push(e[s]);if(!(r.length===0||o.length===0))return{controls:r,cases:o}}function gv(e,t,n,r){let o=ms(e,t,n);return o===void 0?NaN:Fc(o.controls,o.cases,r)}function mo(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=qo(e);return t}function wv(e,t={}){K(t,dv,"ciAuc");let n=t.confLevel??.95;fn(n);let r=Te(t.method,["delong","bootstrap"]),o=new mt;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"){uo(e,"DeLong's interval");let{k10:m,k01:u}=qa(e),h=e.auc,d=$c(m,u,e.controls.length,e.cases.length),w=Math.sqrt(d),g=v=>Math.min(1,Math.max(0,v)),b=s/2,N=n===1?[0,1]:w===0?[h,h]:[g(h+le(b)*w),g(h+Nh(n)*w)];return X("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:d,warnings:o.list})}let i=t.bootN??2e3;Mh(i);let c=t.bootStratified??!0,a=[],l=0;if(c){uo(e,"a stratified bootstrap");for(let m=0;m<i;m++){let u=ho(e.controls),h=ho(e.cases);a.push(Fc(u,h,e.direction))}}else{let{values:m,isCase:u}=Vc(e);for(let h=0;h<i;h++){let d=gv(m,u,mo(m.length),e.direction);Number.isNaN(d)?l++:a.push(d)}}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=Ds(a,[s/2,1-s/2]);return X("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:a.length>1?st(a):NaN,warnings:o.list})}var yh=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,ka=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function bv(e,t){let n=Sa(e),r=Sa(t);if(n?.labels==null||r?.labels==null||!ka(e.levels,t.levels))return;let o=c=>c.kept.map(a=>c.labels[a]);if(ka(o(n),o(r))&&ka(n.isCase,r.isCase))return[e,t];if(!ka(n.labels,r.labels))return;let s=n.labels.map((c,a)=>a).filter(c=>n.labels[c]!==null&&!Number.isNaN(n.predictor[c])&&!Number.isNaN(r.predictor[c])),i=(c,a)=>co(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 Dc(e,t){return t==="two.sided"?2*se(-Math.abs(e)):t==="greater"?se(e,{lowerTail:!1}):se(e)}function vv(e,t,n,r){vt(n)&&K(n,bh,"rocTest"),K(r,bh,"rocTest");let o,s=new mt,i,c;if(yh(e)){if(!yh(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 p=Array.from(e),y=Array.from(t),x=Array.from(n);if(p.length!==y.length||p.length!==x.length)throw new TypeError("the response and the predictors must have the same length");let E=p.map((S,T)=>T).filter(S=>{let T=p[S];return T!=null&&!(typeof T=="number"&&Number.isNaN(T))&&!Number.isNaN(y[S])&&!Number.isNaN(x[S])}),R=E.map(S=>p[S]),A={levels:o.levels,direction:o.direction};i=co(R,E.map(S=>y[S]),A),c=co(R,E.map(S=>x[S]),A);for(let S of i.warnings)s.add(S)}let a=Ze(o.alternative),l=Te(o.method,["delong","bootstrap"]),f=o.confLevel??.95;fn(f),Pc(i)&&Pc(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=bv(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},d=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."),uo(i,"DeLong's test"),uo(c,"DeLong's test"),u){let L=qa(i),C=qa(c),q=L.k10.map((D,k)=>D-C.k10[k]),F=L.k01.map((D,k)=>D-C.k01[k]),_=Math.sqrt($c(q,F,i.controls.length,i.cases.length)),O=d===0&&_===0?0:d/_,I=_===0?0:Nh(f)*_;return X("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:O,statisticName:"Z",pValue:Dc(O,a),confInt:[d-I,d+I],confLevel:f,...h,warnings:s.list})}let p=vh(i),y=vh(c),x=i.controls.length+i.cases.length,E=c.controls.length+c.cases.length,R=p+y,A,S,T;return R===0?(A=d===0?0:d>0?1/0:-1/0,S=NaN,T=Dc(A,a)):(A=d/Math.sqrt(R),S=R*R/(p*p/(x-1)+y*y/(E-1)),T=a==="two.sided"?2*ft(-Math.abs(A),S,!0,!1):a==="greater"?ft(A,S,!1,!1):ft(A,S,!0,!1)),X("rocTest",{method:"DeLong's test for two ROC curves",statistic:A,statisticName:"D",parameter:S,parameterName:"df",pValue:T,...h,warnings:s.list})}let w=o.bootN??2e3;Mh(w);let g=o.bootStratified??!0,b=[],N=0;if(g){uo(i,"a stratified bootstrap"),uo(c,"a stratified bootstrap");for(let p=0;p<w;p++)if(u){let y=mo(i.controls.length),x=mo(i.cases.length);b.push(Ta(y.map(E=>i.controls[E]),x.map(E=>i.cases[E]),i.direction,y.map(E=>c.controls[E]),x.map(E=>c.cases[E]),c.direction))}else{let y=ho(i.controls),x=ho(i.cases),E=ho(c.controls),R=ho(c.cases);b.push(Ta(y,x,i.direction,E,R,c.direction))}}else{let p=Vc(i),y=Vc(c);for(let x=0;x<w;x++){let E,R;if(u){let A=mo(p.values.length);E=ms(p.values,p.isCase,A),R=ms(y.values,y.isCase,A)}else E=ms(p.values,p.isCase,mo(p.values.length)),R=ms(y.values,y.isCase,mo(y.values.length));E===void 0||R===void 0?N++:b.push(Ta(E.controls,E.cases,i.direction,R.controls,R.cases,c.direction))}}if(N>0&&s.add(`${N} NA value(s) produced during bootstrap were ignored.`),b.length<2)throw new Error("fewer than 2 bootstrap replicates had both controls and cases");let v=Math.sqrt(st(b)),M=d===0&&v===0?0:d/v;return X("rocTest",{method:u?"Bootstrap test for two correlated ROC curves":"Bootstrap test for two ROC curves",statistic:M,statisticName:"D",parameter:w,parameterName:"boot.n",pValue:Dc(M,a),bootStratified:g,...h,warnings:s.list})}var yv=["method","confLevel","prevalence"];function Nv(e,t={}){K(t,yv,"diagnosticTests");let n=Te(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(Lt(r),!Array.isArray(e)||e.length!==2||e.some(p=>!(Array.isArray(p)||ArrayBuffer.isView(p))||p.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(p=>typeof p!="number"))throw new TypeError("the counts must be numbers");if(!o.every(Mn))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 d=(p,y)=>{let x=rt(()=>Ni(p,y,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:x.estimate,confInt:x.confInt}},w=1-r,g=w/2<1e-8?le(w/2,{lowerTail:!1}):le(1-w/2),b=(p,y)=>{let x=g*Math.sqrt(y);return{estimate:p,confInt:[Math.exp(Math.log(p)-x),Math.exp(Math.log(p)+x)]}},N=s/l,v=a/f,M={sensitivity:d(s,l),specificity:d(a,f),ppv:d(s,m),npv:d(a,u),lrPos:b(i===0?N/0:fo(BigInt(s)*BigInt(f),BigInt(l)*BigInt(i)),c/(s*l)+a/(i*f)),lrNeg:b(a===0?c/l/v:fo(BigInt(c)*BigInt(f),BigInt(l)*BigInt(a)),s/(c*l)+i/(a*f)),accuracy:d(s+a,h),diagnosticOR:b(s*a/(i*c),1/s+1/i+1/c+1/a),prevalence:d(l,h),apparentPrevalence:d(m,h),method:n,confLevel:r,warnings:[]};if(t.prevalence!==void 0){let p=t.prevalence;if(!(typeof p=="number"&&p>0&&p<1))throw new RangeError("'prevalence' must be a number in (0, 1)");let y=p<2**-500?2**600:1,x=N*(p*y),E=i/f*((1-p)*y),R=v*(1-p),A=c/l*p;M.ppvAtPrevalence=x/(x+E),M.npvAtPrevalence=R/(R+A)}return X("diagnosticTests",M)}var Mv=["two","mode","confLevel","ciMethod"],Ev=["package","exact"],In=(e,t)=>t===0?0:e*t;function xv(e,t,n={}){if(K(n,Mv,"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,Ev),c=[],a=[];for(let _=0;_<e.length;_++){let O=e[_],I=t[_];if(typeof O!="number"||typeof I!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(O)||Number.isNaN(I))){if(!Number.isFinite(O)||!Number.isFinite(I))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(O),a.push(I)}}let l=c.length;if(l<2)throw new RangeError("fewer than 2 complete pairs");let[f,m]=o===1?[c,a]:[a,c],u=c.map((_,O)=>{let I=(_+a[O])/2;return Number.isFinite(I)?I:_/2+a[O]/2}),h=f.map((_,O)=>_-m[O]),d=!h.every(Number.isFinite),{groups:w,k:g}=Se([d?f.map((_,O)=>_/2-m[O]/2):h]),b=w[0],N=g+(d?1:0),v=it(b),M=Qn(b),p=r*M,y=v-p,x=v+p,E=l-1,R=(1-s)/2,A=bt(R,E,!0,!1),S=R<1e-8?bt(R,E,!1,!1):bt((s+1)/2,E,!0,!1),T=[v+In(A,M)/Math.sqrt(l),v+In(S,M)/Math.sqrt(l)],L,C;if(i==="package"){let _=3*M*M/l,O=_>=2**-1022||_===0?Math.sqrt(_):M*Math.sqrt(3/l);L=[y+In(A,O),y+In(S,O)],C=[x+In(A,O),x+In(S,O)]}else{let _=r*Math.sqrt(l),O=ja(R,E,_,!0,!1)/Math.sqrt(l),I=ja(R,E,_,!1,!1)/Math.sqrt(l);L=[v-In(I,M),v-In(O,M)],C=[v+In(O,M),v+In(I,M)]}let q=_=>P(_,N),F=_=>[q(_[0]),q(_[1])];return X("blandAltman",{means:u,diffs:h,n:l,meanDiff:q(v),lowerLimit:q(y),upperLimit:q(x),criticalDiff:q(p),meanDiffConfInt:F(T),lowerLimitConfInt:F(L),upperLimitConfInt:F(C),confLevel:s,two:r,mode:o,ciMethod:i,warnings:[]})}var Rv=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),Av=["PaBa","Deming","WDeming"],Sv=["analytical","jackknife","bootstrap","nestedbootstrap"],Tv=["quantile","Student","BCa","tBoot"],Eh=5793,kv=2**27;function jc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function qv(e,t){let n=Xn(e),r=Xn(t),o=[0,0],s=[0,0],i=[0,0];for(let c=0;c<e.length;c++){let a=De([e[c],0],n),l=De([t[c],0],r);o=he(o,_e(a,a)),s=he(s,_e(l,l)),i=he(i,_e(a,l))}return{u:o,q:s,p:i,mx:n,my:r}}function Sh(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 Kc(e,t,n){let r=e.length,{u:o,q:s,p:i,mx:c,my:a}=qv(e,t),l=Sh(o[0],s[0],i[0],n),f=De(a,_e([l,0],c))[0],m=_e(o,s),u=i[0]*i[0]/(o[0]*s[0]),h=De(m,_e(i,i))[0]/(m[0]+m[1]);Math.abs(h)<1e-12*(1+u)/2&&(h=0);let d=Math.sqrt(l*l*h/u/(r-2)),w=[0,0];for(let b of e)w=he(w,_e([b,0],[b,0]));let g=Math.sqrt(d*d*lt(w,r)[0]);return{b0:f,b1:l,se0:g,se1:d,xmean:c[0]}}function xh(e,t,n,r,o,s,i,c,a){let l=e.length,f=Kc(e,t,n),m=f.b0,u=f.b1,h=NaN,d=new Array(l),w=s;for(let p=0;p<s;p++){let y=1+n*u*u,x=1/0;for(let I=0;I<l;I++){let D=t[I]-(m+u*e[I]),k=e[I]+n*u*D/y,V=t[I]-D/y;d[I]=r>1?k/r+V:k+r*V,x=Math.min(x,Math.abs(d[I]))}let E=[0,0],R=[0,0],A=[0,0];for(let I=0;I<l;I++){let D=x/d[I],k=[D*D,0];E=he(E,k),R=he(R,_e(k,[e[I],0])),A=he(A,_e(k,[t[I],0]))}let S=lt(R,E[0]+E[1]),T=lt(A,E[0]+E[1]),L=[0,0],C=[0,0],q=[0,0];for(let I=0;I<l;I++){let D=lt(De([e[I],0],S),d[I]),k=lt(De([t[I],0],T),d[I]);L=he(L,_e(D,D)),C=he(C,_e(k,k)),q=he(q,_e(D,k))}let F=Sh(L[0],C[0],q[0],n),_=De(T,_e([F,0],S))[0];h=S[0];let O=P(Math.abs(u-F),a-c)<i&&P(Math.abs(m-_),a)<i;if(m=_,u=F,O){w=p+1;break}}let[g,b]=zc(o),[N,v]=zc(1+o),M=d.map(p=>{if(p===0)return 1/0;let[y,x]=zc(Math.abs(p));return r>1?Le((N/(g*y))**2,2*(v-b-x-a)):Le((N/y)**2,2*(v-x-c))});return{b0:m,b1:u,xmean:h,weights:M,converged:w<s}}function zc(e){let t=Math.floor(Math.log2(e));return[Le(e,-t),t]}var wo=e=>Math.atan(e),go=e=>Number.isFinite(e)?e:Math.tan(wo(e));function Iv(e,t){let n=e.length,r=0;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++)r+=Math.sign(e[u]-e[m])*Math.sign(t[u]-t[m]);let o=r>=0,s=new Float64Array(n*(n-1)/2),i=0,c=0,a=0,l=Math.PI/4;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++){let h=jc(e[u],e[m]),d=jc(t[u],t[m]),w;if(h!==0)w=d/h;else if(d!==0)w=o?1/0:-1/0;else continue;s[i++]=w;let g=wo(w);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 _v(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?go(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?(go(s)+go(i))/2:Math.tan((wo(s)+wo(i))/2)}function Lv(e){let t=Float64Array.from(e).sort(),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function Bc(e,t,n){return Number.isFinite(n)?Lv(e.map((r,o)=>jc(t[o],n*r))):-n}function Rh(e,t,n){let r=Iv(e,t),o=_v(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:Bc(e,t,o),b1:o,slopes:r}}function Cv(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=(d,w)=>{let g=Math.trunc((d+1)/2);if(w?g<2*a+1:g>l)return NaN;if(d%2===0){if(g+1>l)return NaN;let b=i[g-1],N=i[g];return o?(go(b)+go(N))/2:Math.tan((wo(b)+wo(N))/2)}return go(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:[Bc(e,t,h),Bc(e,t,u)],defined:!0}}function Fv(e,t,n,r){let o=e.length,s=e.map((d,w)=>t[w]-n-r*d),i=s.filter(d=>d>0).length,c=s.filter(d=>d<0).length,a=Math.sqrt(c/i),l=-Math.sqrt(i/c),f=e.map((d,w)=>(t[w]+d/r-n)/Math.sqrt(1+1/(r*r))),m=Array.from({length:o},(d,w)=>w).sort((d,w)=>f[d]-f[w]||d-w),u=0,h=0;for(let d of m){let w=s[d];u+=w>0?a:w<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:i,nNeg:c,maxCusum:h}}var Ia=e=>e.every(t=>t===e[0]);function Wc(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var po=(e,t)=>Ds(e,t),Ah=e=>Qn(e.filter(t=>!Number.isNaN(t)));function Ov(e,t,n={}){K(n,Rv,"mcreg");let r=Te(n.methodReg,Av),o=Te(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),Sv),s=o==="bootstrap"||o==="nestedbootstrap",i=Te(n.methodBootstrapCi,Tv),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=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=Wc(n.nsamples??999,"nsamples"),h=Wc(n.nnested??25,"nnested"),d=Wc(n.iterMax??30,"iterMax"),w=n.threshold??1e-6;if(!(typeof w=="number"&&w>=0&&w<1/0))throw new RangeError("'threshold' must be a nonnegative number");if(r==="WDeming"&&o==="analytical")throw new RangeError('there is no analytical interval for weighted Deming regression (use methodCi "jackknife" or a bootstrap)');if(o==="bootstrap"&&i==="tBoot"&&r!=="Deming")throw new RangeError(`the bootstrap-t interval of ${r} needs methodCi "nestedbootstrap" (there is no analytical standard error)`);if(e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let g=[],b=[];for(let ne=0;ne<e.length;ne++){let $=e[ne],Y=t[ne];if(typeof $!="number"||typeof Y!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN($)||Number.isNaN(Y)){if(n.naRm===!0)continue;throw new RangeError("'x' or 'y' has missing values (use naRm: true to drop the incomplete pairs)")}if(!Number.isFinite($)||!Number.isFinite(Y))throw new RangeError("'x' and 'y' must not contain infinite values");g.push($),b.push(Y)}let N=g.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(Ia(g)||Ia(b))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(g.some($=>$<0)||b.some($=>$<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>Eh)throw new RangeError(`Passing-Bablok regression is limited to ${Eh} 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)>kv)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(ne=>ne<=0)||b.some(ne=>ne<=0)))throw new RangeError("weighted Deming regression needs positive values");let v=Se([g,b]),[M,p]=f?[Se([g]).k,Se([b]).k]:[v.k,v.k],y=f?g.map(ne=>P(ne,-M)):v.groups[0],x=f?b.map(ne=>P(ne,-p)):v.groups[1],E=Math.min(Math.max(Le(m,2*(p-M)),Number.MIN_VALUE),2**1e3),R=Le(m,p-M),A=new Set,S=`no konvergence after ${d} iterations`,T=(ne,$)=>{if(r==="Deming")return Kc(ne,$,E);if(r==="WDeming"){let Y=xh(ne,$,E,R,m,d,w,M,p);return Y.converged||A.add(S),Y}return Rh(ne,$,l)},L,C=NaN,q,F,_;if(r==="Deming"){let ne=Kc(y,x,E);L=ne,C=P(ne.xmean,M),F=[ne.se0,ne.se1]}else if(r==="WDeming"){let ne=xh(y,x,E,R,m,d,w,M,p);ne.converged||A.add(S),L=ne,C=P(ne.xmean,M),q=ne.weights}else{let ne=Rh(y,x,l);L=ne,_=ne.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 O=1-c,I=bt(O/2,N-2,!1,!1),D=le(O/2,{lowerTail:!1}),k=[L.b0,L.b1],V=[NaN,NaN],B=[[NaN,NaN],[NaN,NaN]],Q,re,z,J=()=>{let ne=[],$=[];for(let Y=0;Y<N;Y++){let W=T(y.filter((U,j)=>j!==Y),x.filter((U,j)=>j!==Y));ne.push(W.b0),$.push(W.b1)}Q=[ne,$],re=[(N-1)*Qn(ne)/Math.sqrt(N),(N-1)*Qn($)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let ne of[0,1])V[ne]=F[ne],B[ne]=[k[ne]-I*V[ne],k[ne]+I*V[ne]];else{let ne=Cv(y,x,_,D,l);ne.defined||A.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),B[0]=ne.intercept,B[1]=ne.slope}else if(o==="jackknife"){J();for(let ne of[0,1])V[ne]=re[ne],B[ne]=[k[ne]-I*V[ne],k[ne]+I*V[ne]]}else{if(i==="BCa"&&(J(),Q.some(de=>de.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let ne=r==="Deming"?F:r==="WDeming"&&re!==void 0?re:[NaN,NaN],$=o==="nestedbootstrap",Y=[],W=[],U=[],j=[],te=0,fe=()=>Array.from({length:N},()=>qo(N)),Re=de=>{let Fe=de.map(Ve=>y[Ve]),xe=de.map(Ve=>x[Ve]);if(Ia(Fe)||Ia(xe)){if(r==="PaBa")return A.add("Resampling PaBa regression: x or y coordinates of all data points in subsample identical - regression coefficients undetermined!"),{b0:NaN,b1:NaN};A.add("Resampling Deming regression: all x coordinates in subsample identical - regression coefficients undetermined!")}return T(Fe,xe)},Ce=()=>{for(let de=0;de<u;de++){let Fe=fe(),xe=Re(Fe),Ve;if($){let Oe=[],Be=[];for(let ze=0;ze<h;ze++){let et=Re(fe().map(Ue=>Fe[Ue]));Oe.push(et.b0),Be.push(et.b1)}Ve=[Ah(Oe),Ah(Be)]}else r==="Deming"?Ve=[xe.se0,xe.se1]:Ve=ne;if(Number.isNaN(xe.b0)||Number.isNaN(xe.b1)){te++;continue}Y.push(xe.b0),W.push(xe.b1),U.push(Ve[0]),j.push(Ve[1])}};if(n.rngSeed!==void 0){let de=Tl()?Il():void 0;try{ql({kind:"Mersenne-Twister"}),kl(n.rngSeed),Ce()}finally{de!==void 0&&_l(de)}}else Ce();if(te>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let be=Y.length,me=[Y,W],qe=[U,j];for(let de of[0,1]){let Fe=me[de],xe=k[de];if(i==="quantile")B[de]=[po(Fe,O/2),po(Fe,1-O/2)];else if(i==="Student")V[de]=Qn(Fe),B[de]=[xe-I*V[de],xe+I*V[de]];else if(i==="BCa"){let Ve=Fe.filter(ce=>ce<xe).length;Fe.every(ce=>ce===xe)?Ve=be/2:Ve===0?Ve=.5:Ve===be&&(Ve=be-.5);let Oe=le(Ve/be),Be=Q[de],ze=Be.reduce((ce,tt)=>ce+tt,0)/Be.length,et=0,Ue=0;for(let ce of Be){let tt=ze-ce;et+=tt*tt,Ue+=tt*tt*tt}let Xe=et===0?0:Ue/(6*et**1.5),pe=-D,oe=D,we=se(Oe-(pe-Oe)/(1+Xe*(pe-Oe))),ge=se(Oe-(oe-Oe)/(1+Xe*(oe-Oe)));B[de]=[po(Fe,ge),po(Fe,we)]}else{let Ve=Fe.map((Be,ze)=>(Be-xe)/qe[de][ze]),Oe=Number.isNaN(ne[de])?Qn(Fe):ne[de];V[de]=Oe,B[de]=[xe-po(Ve,1-O/2)*Oe,xe-po(Ve,O/2)*Oe]}}z={intercept:Y.map(de=>P(de,p)),slope:W.map(de=>P(de,p-M)),sigmaIntercept:U.map(de=>P(de,p)),sigmaSlope:j.map(de=>P(de,p-M)),globalSigma:[P(ne[0],p),P(ne[1],p-M)],nsamples:be}}let Z=ne=>{let $=Y=>P(Y,ne===0?p:p-M);return{estimate:$(k[ne]),se:$(V[ne]),confInt:[$(B[ne][0]),$(B[ne][1])]}},ke={methodReg:r,methodCi:o,...s?{methodBootstrapCi:i}:{},confLevel:c,errorRatio:m,n:N,intercept:Z(0),slope:Z(1),xmean:C,...q===void 0?{}:{weights:q},...Q===void 0?{}:{jackknife:{intercept:Q[0].map(ne=>P(ne,p)),slope:Q[1].map(ne=>P(ne,p-M))}},...z===void 0?{}:{bootstrap:z},...r==="PaBa"?{cusum:Fv(y,x,L.b0,L.b1)}:{},warnings:[...A]};return X("mcreg",ke)}var Dv=["alpha","confLevel","lmer"],Pv={ICC1:"Single_raters_absolute",ICC2:"Single_random_raters",ICC3:"Single_fixed_raters",ICC1k:"Average_raters_absolute",ICC2k:"Average_random_raters",ICC3k:"Average_fixed_raters"};function _a(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function Vv(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,i]=_a(r,n[0]),[c,a]=_a(t[o],t[0]);return s===c&&i===a}))}var Th=(e,t)=>e.every((n,r)=>n===t[r]);function Or(e){let t=[0,0];for(let n of e)t=he(t,n);return lt(t,e.length)}var bo=(e,t)=>({v:lt(e.v,t),e:e.e});function vo(...e){let t=e.filter(r=>r.v[0]!==0),n=t.length===0?0:Math.max(...t.map(r=>r.e));return e.map(r=>({at:Le(r.v[0],r.e-n),E:n}))}function kh(e,t){if(t.v[0]===0)return e;if(e.v[0]===0)return t;let n=Math.max(e.e,t.e),r=o=>[Le(o.v[0],o.e-n),Le(o.v[1],o.e-n)];return{v:he(r(e),r(t)),e:n}}var qh=(e,t)=>Le(e.v[0]/t.v[0],e.e-t.e),$v=(e,t)=>{let[n,r]=vo(e,t);return n.at<r.at},zv=(e,t)=>{let[n,r]=vo(e,t);return n.at<=r.at};function Ih(e,t,n){let r=[0,0];for(let o of e){let s=De(o,t);r=he(r,_e(s,s))}return _e(r,[n,0])}function Wv(e,t={}){K(t,Dv,"ICC");let n=t.lmer??!0;if(typeof n!="boolean")throw new TypeError("'lmer' must be true or false");let r,o;if(t.alpha!==void 0){if(r=t.alpha,!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'alpha' must be a single number in (0, 1)");o=1-r}else o=t.confLevel??.95,Lt(o),r=1-o;if(!Array.isArray(e)||e.length===0||!e.every(ee=>Array.isArray(ee)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(ee=>ee.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(ee=>ee.some(ve=>typeof ve!="number")))throw new TypeError("'x' must contain numbers");if(e.some(ee=>ee.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(ee=>ee.some(ve=>!Number.isFinite(ve))))throw new RangeError("'x' contains infinite ratings");let i=e.length;if(i<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let{groups:c}=Se(e);if(c.every(ee=>Th(ee,c[0])))throw new RangeError(c[0].every(ee=>ee===c[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let a=Array.from({length:s},(ee,ve)=>c.map(ie=>ie[ve])),l=0;for(let ee of e)for(let ve of ee)l=Math.max(l,Math.abs(ve));let f=l>2**1020?3:0,m=f>0?e.map(ee=>ee.map(ve=>ve/8)):e,u=m.map(ee=>ee.map((ve,ie)=>_a(ve,m[0][ie]))),h=m.map(ee=>ee.map(ve=>_a(ve,ee[0]))),d=h.map(ee=>ee.map((ve,ie)=>De(ve,h[0][ie]))),w=ee=>{let ve=0;for(let ie of ee)for(let Ae of ie)ve=Math.max(ve,Math.abs(Ae[0]));return ve===0?0:Math.floor(Math.log2(ve))},g=(ee,ve)=>ee.map(ie=>ie.map(Ae=>[P(Ae[0],-ve),P(Ae[1],-ve)])),[b,N,v]=[w(u),w(h),w(d)],[M,p,y]=[g(u,b),g(h,N),g(d,v)],x=ee=>Array.from({length:s},(ve,ie)=>ee.map(Ae=>Ae[ie])),E=Ih(M.map(Or),Or(M.flat()),s),R=a.every(ee=>Th(ee,a[0]))?[0,0]:Ih(x(p).map(Or),Or(p.flat()),i),A=[0,0];if(!Vv(c)){let ee=y.map(Or),ve=x(y).map(Or),ie=Or(y.flat());y.forEach((Ae,Ke)=>Ae.forEach(($e,ot)=>{let ht=he(De(De($e,ee[Ke]),ve[ot]),ie);A=he(A,_e(ht,ht))}))}let S=(ee,ve)=>({v:ee,e:2*(ve+f)}),T=S(E,b),L=S(R,N),C=S(A,v),q=i-1,F=s-1,_=(i-1)*(s-1),O=bo(T,q),I=bo(L,F),D=bo(C,_),k=bo(kh(L,C),F+_),V=[];if(n){let ee={B:{ss:T,df:q,ms:O,pooled:!1},J:{ss:L,df:F,ms:I,pooled:!1}},ve=C,ie=_;for(let Ke of zv(O,I)?["B","J"]:["J","B"]){let $e=ee[Ke];$v($e.ms,bo(ve,ie))&&(ve=kh(ve,$e.ss),ie+=$e.df,$e.pooled=!0)}let Ae=bo(ve,ie);if(ee.B.pooled&&(O=Ae),ee.J.pooled&&(I=Ae),D=Ae,ee.B.pooled){let[Ke,$e]=vo(D,I);k={v:[Ke.at+($e.at-Ke.at)/i,0],e:Ke.E}}}else A[0]===0&&V.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let B=(ee,ve)=>r/2<1e-8?pn(r/2,ee,ve,!1,!1):pn(1-r/2,ee,ve,!0,!1),Q=(ee,ve,ie)=>dt(ee,ve,ie,!1,!1),re=ee=>ee===1/0?1:(ee-1)/(ee+s-1),z=ee=>1-1/ee,J=i*(s-1),Z=qh(O,k),ke=qh(O,D),[ne,$]=[Z/B(q,J),Z*B(J,q)],[Y,W]=[ke/B(q,_),ke*B(_,q)],[U,j]=vo(O,k).map(ee=>ee.at),[te,fe]=vo(O,D).map(ee=>ee.at),[Re,Ce,be]=vo(O,I,D).map(ee=>ee.at),me=(U-j)/(U+(s-1)*j),qe=(Re-be)/(Re+(s-1)*be+s*(Ce-be)/i),de=(te-fe)/(te+(s-1)*fe),Fe=s*qe*Ce,xe=(i*(1+(s-1)*qe)-s*qe)*be,Ve=Math.max(Math.abs(Fe),Math.abs(xe)),Oe=Ve>0&&Number.isFinite(Ve)?Math.floor(Math.log2(Ve)):0,Be=P(Fe,-Oe),ze=P(xe,-Oe),et=(i-1)*Be*Be+ze*ze,Ue=et===0?s-1:(s-1)*(i-1)*(Be+ze)**2/et,Xe=B(q,Ue),pe=B(Ue,q),oe=s*Ce+(s*i-s-i)*be,we=(ee,ve)=>ee>.5?1-(i*be+oe)/ve:ee,ge=we(i*(Re/Xe-be)/(oe+i*Re/Xe),oe+i*Re/Xe),ce=pe>1?we(i*(Re-be/pe)/(oe/pe+i*Re),oe+i*pe*Re):we(i*(pe*Re-be)/(oe+i*pe*Re),oe+i*pe*Re),tt=ee=>ee*s/(1+ee*(s-1)),G=Q(Z,q,J),ue=Q(ke,q,_),Me=(ee,ve,ie,Ae,Ke,$e,ot)=>({type:ee,name:Pv[ee],ICC:ve,F:ie,df1:q,df2:Ae,pValue:Ke,lower:$e,upper:ot}),ye=[Me("ICC1",me,Z,J,G,re(ne),re($)),Me("ICC2",qe,ke,_,ue,ge,ce),Me("ICC3",de,ke,_,ue,re(Y),re(W)),Me("ICC1k",(U-j)/U,Z,J,G,z(ne),z($)),Me("ICC2k",(Re-be)/(Re+(Ce-be)/i),ke,_,ue,tt(ge),tt(ce)),Me("ICC3k",(te-fe)/te,ke,_,ue,z(Y),z(W))],Ee=ee=>Le(ee.v[0],ee.e);return X("ICC",{results:ye,MSB:Ee(O),MSJ:Ee(I),MSE:Ee(D),MSW:Ee(k),n:i,k:s,confLevel:o,lmer:n,warnings:V})}function La(e){let t=e.length;if(t===0||e.some(n=>n.length!==t))throw new RangeError("'covmat' must be a square matrix");if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError("'covmat' must be finite");if(e.some((n,r)=>!(n[r]>0)))throw new RangeError("the diagonal of 'covmat' must be positive");if(!Xl(jl(e)))throw new RangeError("'covmat' must be symmetric")}var jv=["covmat","nObs","rotation","scores","control"],Kv=["lower","start"];function St(e){let t=0,n=0;for(let r of e){let[o,s]=Ot(t,r);t=o,n+=s}return t+n}var Uc=(e,t)=>e[0].map((n,r)=>St(e.map(o=>t(o[r]))));function Ch(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 _h(e,t,n){let r=jt(Ch(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 Lh(e,t){let n=Uc(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=Uc(o,c=>c).map(c=>c<0?-1:1);return o=o.map(c=>c.map((a,l)=>i[l]<0?-a:a)),s=s?.map(c=>c.map((a,l)=>i[l]<0?-a:a)),s===void 0?{L:o}:{L:o,rot:s}}var _n=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let i=0;i<n.length;i++)s+=n[i]*t[i][o];return s})),yo=e=>e[0].map((t,n)=>e.map(r=>r[n]));function Ca(e){let t=e[0].length,n=e.map(m=>Math.sqrt(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,d)=>u===d?1:0));if(s===0)return{loadings:e.map(m=>m.slice()),rotmat:i};let c=yo(o),a=0;for(let m=0;m<1e3;m++){let u=_n(o,i),h=u[0].map((N,v)=>{let M=0;for(let p=0;p<s;p++)M+=u[p][v]*u[p][v];return M}),d=u.map(N=>N.map((v,M)=>v**3-v*h[M]/s)),w=_n(c,d),g=Fs(w);i=_n(g.u,yo(g.v));let b=a;if(a=St(g.d),a<b*(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 Bv(e){let t=Ca(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=Hr(At(n),r),s=nt(_n(yo(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:_n(n,i),rotmat:_n(t.rotmat,i)}}function Uv(e,t,n={}){K(n,jv,"factanal"),K(sn(n,"control"),Kv,"factanal","control");let r=n.scores??"none";if(r!=="none"&&r!=="regression"&&r!=="Bartlett")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Cregression\u201D, \u201CBartlett\u201D");let o,s;if(n.covmat!==void 0){if(r!=="none")throw new RangeError("requested scores without an 'x' matrix");La(n.covmat),o=n.covmat.map(x=>x.slice()),s=n.nObs??NaN}else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");o=Os(e),s=e.length}let i=o.length;if(i<3)throw new RangeError("factor analysis requires at least three variables");let c=t;if(!Number.isInteger(c)||c<1)throw new RangeError("'factors' must be a positive integer");let a=((i-c)**2-i-c)/2;if(a<0)throw new RangeError(`${c} ${c===1?"factor is":"factors are"} too many for ${i} variables`);let l=o.map((x,E)=>Math.sqrt(x[E])),f=o.map((x,E)=>x.map((R,A)=>R/(l[E]*l[A]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??nt(f).map((x,E)=>(1-c/(2*i))/x[E]),h=x=>{let E=jt(Ch(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(St(E.map(R=>Math.log(R)-R))-c+i)},d=x=>{let E=_h(f,x,c);return x.map((R,A)=>{let S=0;for(let T=0;T<c;T++)S+=E[A][T]*E[A][T];return(S+R-f[A][A])/(R*R)})},w=rt(()=>Fo(u,h,{gr:d,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(w.convergence!==0)throw new Error("unable to optimize from this starting value");let g=w.par,b=Lh(_h(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"?Ca(b):Bv(b),E=Lh(x.loadings,x.rotmat);b=E.L,N=E.rot}let M=NaN,p=NaN;!Number.isNaN(s)&&a>0&&(M=(s-1-(2*i+5)/6-2*c/3)*w.value,p=_t(M,a,{lowerTail:!1}));let y={uniquenesses:g,loadings:b,STATISTIC:M,dof:a,PVAL:p,converged:!0,objective:w.value,counts:w.counts,warnings:[]};return r!=="none"&&(y.scores=Hv(e,f,g,b,r)),N!==void 0&&(y.rotmat=N),y}function Hv(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=Uc(c,m=>m*m).map(m=>Math.sqrt(m/(s-1))),l=c.map(m=>m.map((u,h)=>u/a[h]));if(o==="regression")return _n(l,nt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return _n(l,yo(nt(_n(yo(r),f),yo(f))))}var Yv=["nfactors","covmat"],No=2**-52,Gv=`
|
|
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
|
-
`,Mo=(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})),us=e=>e[0].map((t,n)=>e.map(r=>r[n])),hs=(e,t=n=>n)=>e[0].map((n,r)=>St(e.map(o=>t(o[r])))),
|
|
19
|
-
`),dr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let k=Jv(c);if(k.converged||dr(s,"oblimin did not converge in 2000 iterations"),a=k.loadings,l=k.phi,n===2){let V=l[0][1],U=Math.sqrt(Math.abs(V));f=[U,Oa(V)*U],dr(i,Xv)}else f=Fh(l,1,s,i).map(V=>V[0])}let m=a.map(k=>{let V=0;for(let U=0;U<n;U++)V+=k[U]*f[U];return V}),u=Ph(c,k=>k*k),h=u.map((k,V)=>r[V][V]-k),d=a.map((k,V)=>k.map((U,Q)=>{if(n===1)return 0;let re=r[V][V],z=re-(re-U*U)-(U*f[Q])**2;return z<0&&(z=0),Oa(U)*Math.sqrt(z)})),w=m.map((k,V)=>k*k/u[V]),g=m.map((k,V)=>{let U=[k,...d[V]];return St(U.map(Q=>Q*Q))**2/St(U.map(Q=>Q**4))}),b=m.map(Oa);b.some(k=>k<0)&&(r=r.map((k,V)=>k.map((U,Q)=>b[V]*U*b[Q])));let N=m.map((k,V)=>b[V]*k),v=d.map((k,V)=>k.map(U=>b[V]*U)),M=N.map((k,V)=>[k,...v[V],u[V],h[V],w[V],g[V]]),p=Eo(r),y=St(r.map((k,V)=>k[V])),x=St(N)**2,E=St(h),R=x/p,A=(p-E)/p,S=x/(p-E),T=(p-y)/p*o/(o-1),L=(p+St(Dh(r,i))-y)/p,C=hs(M.map(k=>k.slice(0,n+1)),k=>k*k),q=C[0]/St(C),O=v.map(k=>{let V=0;for(let U=1;U<n;U++)Math.abs(k[U])>Math.abs(k[V])&&(V=U);return V}),_=[],F=[];for(let k=0;k<n;k++){let V=O.map((z,J)=>J).filter(z=>O[z]===k),U=St(V.flatMap(z=>V.map(J=>r[J][z]))),Q=St(V.map(z=>v[z][k])),re=St(V.map(z=>N[z]));F.push(Q*Q),_.push([(Q*Q+re*re)/U,re*re/U,Q*Q/U])}let I=[[A,St(N)**2/p,St(F)/p],..._],P={omegaH:R,omegaLim:S,alpha:T,omegaTot:A,G6:L,ECV:q,sl:M,slNames:["g",...Array.from({length:n},(k,V)=>`F${V+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:b,omegaGroup:I,warnings:s,messages:i};return l!==void 0&&(P.phi=l),X("omega",P)}var Vh=["ML","stdErr"],ty=["ML","stdErr"],rn=.9999;function Qc(e){let t=De(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(le(r/t));return n}var Zc=e=>[...new Set(e)].sort((t,n)=>t-n),$h=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,zh=e=>Array.isArray(e)||ArrayBuffer.isView(e),Wh=(e,t)=>e>0?se(e,{lowerTail:!1})-se(t,{lowerTail:!1}):se(t)-se(e);function Jc(e){let t=Se([e]).groups[0],n=it(t),r=t.map(s=>s-n),o=Math.sqrt(st(r));return r.map(s=>s/o)}function jh(e){if(Math.abs(e)>=.1)return e-Math.log1p(e);let t=e,n=0;for(let r=2;r<40;r++){t*=-e;let o=t/r;if(n+=o,Math.abs(o)<=1e-17*Math.abs(n))break}return-n}var Fa=e=>Array.from({length:e},()=>new Array(e).fill(0));function Bh(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 Xc={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function ny(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Xc;if(e===1/0)return{...Xc,k:Gt(t),kk:-t*Gt(t)};if(t===1/0)return{...Xc,h:Gt(e),hh:-e*Gt(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),s=e*e-2*n*e*t+t*t,i=Math.exp(-s/(2*r))/(2*Math.PI*o),c=(t-n*e)/o,a=(e-n*t)/o,l=Gt(e),f=Gt(t);return{r:i,h:l*se(c),k:f*se(a),rr:i*(n/r+(e*t*r-n*s)/(r*r)),rh:-i*(e-n*t)/r,rk:-i*(t-n*e)/r,hh:-e*l*se(c)-n/o*l*Gt(c),hk:i,kk:-t*f*se(a)-n/o*f*Gt(a)}}function Kh(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:Fa(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(d=>ny(h,d,r))),u=[];for(let h=0;h<s;h++)for(let d=0;d<i;d++){let w=e[h][d];if(w===0)continue;let g=ko([t[h],n[d]],[t[h+1],n[d+1]],r),b=new Array(c).fill(0),N=Fa(c);for(let[v,M,p]of[[h+1,d+1,1],[h,d+1,-1],[h+1,d,-1],[h,d,1]]){let y=m[v][M],x=l(v),E=f(M);b[0]+=p*y.r,N[0][0]+=p*y.rr,x>=0&&(b[x]+=p*y.h,N[0][x]+=p*y.rh,N[x][0]+=p*y.rh,N[x][x]+=p*y.hh),E>=0&&(b[E]+=p*y.k,N[0][E]+=p*y.rk,N[E][0]+=p*y.rk,N[E][E]+=p*y.kk),x>=0&&E>=0&&(N[x][E]+=p*y.hk,N[E][x]+=p*y.hk)}Bh(a,w,g,b,N),u.push(w*Math.log(g))}return a.value=De(u),a}function ry(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:Fa(s)},c=(1-r)*(1+r),a=Math.sqrt(c),l=c*a,f=new Array(s),m=Fa(s),u=[],h=(d,w,g)=>{let b=n[d],N=(b-r*w)/a,v=Gt(N),M=(r*b-w)/l,p=1/a,y=b/l+3*r*(r*b-w)/(l*c),x=r/l;if(f[0]+=g*v*M,m[0][0]+=g*v*(y-N*M*M),o){let E=d+1;f[E]+=g*v*p;let R=g*v*(x-N*M*p);m[0][E]+=R,m[E][0]+=R,m[E][E]+=g*v*(-N*p*p)}};for(let d=0;d<e.length;d++){let w=e[d],g=t[d];f.fill(0);for(let v of m)v.fill(0);let b=g<n.length?(n[g]-r*w)/a:1/0,N=g>0?(n[g-1]-r*w)/a:-1/0;g<n.length&&h(g,w,1),g>0&&h(g-1,w,-1),Bh(i,1,Wh(N,b),f,m),u.push(Math.log(Wh(N,b)),Gt(w,{log:!0}))}return i.value=De(u),i}function oy(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(i=>e.gradient[i]),o=t.map(i=>t.map(c=>-e.hessian[i][c])),s;try{s=nt(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||De(s.map((i,c)=>i*r[c]))<=0)&&(s=r.map((i,c)=>i/Math.max(Math.abs(o[c][c]),1))),t.forEach((i,c)=>n[i]=s[c]),n}var Hh=(e,t)=>e>=t-1e-12*Math.abs(t);function Uh(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=oy(o,a);if(!s&&Math.abs(r[0])===rn&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>rn?(Math.sign(l[0])*rn-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((w,g)=>w+f*l[g]);f*l[0]!==0&&Math.abs(h[0])>=rn*(1-1e-15)&&(h[0]=Math.sign(h[0])*rn);let d=e(h);if(Hh(d.value,o.value)){let w=gt(h.map((g,b)=>Math.abs(g-r[b])/Math.max(1,Math.abs(r[b]))));r=h,o=d,m=w>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function Yh(e,t){let n=Uh(e,[Math.max(-rn,Math.min(rn,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Uh(e,[Math.sign(n.theta[0])*rn,...n.theta.slice(1)],!0);return Hh(r.at.value,n.at.value)?r:n}function Gh(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=nt(e.map(o=>o.map(s=>-s)))}catch{return n()}return r.map((o,s)=>o.map((i,c)=>(i+r[c][s])/2))}var Xh=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function sy(e,t,n){vt(t)&&K(t,Vh,"polychor"),K(n,Vh,"polychor");let r,o;if(zh(t)){let _=e,F=t;if(_.length!==F.length)throw new RangeError("'x' and 'y' must have the same length");let I=_.map((Q,re)=>!Number.isNaN(Q)&&!Number.isNaN(F[re])),P=kt(_.filter((Q,re)=>I[re])),k=kt(F.filter((Q,re)=>I[re])),V=new Map(Zc(P).map((Q,re)=>[Q,re])),U=new Map(Zc(k).map((Q,re)=>[Q,re]));r=Array.from({length:V.size},()=>new Array(U.size).fill(0)),P.forEach((Q,re)=>r[V.get(Q)][U.get(k[re])]++),o=n??{}}else{let _=e;if(!_.every(zh))throw new TypeError("give a table (array of rows) or two vectors");let F=_[0]?.length??0;if(_.some(I=>I.length!==F))throw new TypeError("the table must have rows of equal length");if(r=_.map(I=>Array.from(I)),r.some(I=>I.some(P=>!(P>=0&&P<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:i=!1}=o,c=[],a=0;De(r.map(_=>De(_)))<2**1e3||({groups:r,k:a}=Se(r));let l=r.map(_=>De(_)),f=l.filter(_=>_===0).length;f>0&&(c.push($h(f,"row")),r=r.filter((_,F)=>l[F]!==0));let m=(r[0]??[]).map((_,F)=>De(r.map(I=>I[F]))),u=m.filter(_=>_===0).length;u>0&&(c.push($h(u,"column")),r=r.map(_=>_.filter((F,I)=>m[I]!==0)));let h=r.length,d=r[0]?.length??0;if(h<2&&c.push("the table has fewer than 2 rows"),d<2&&c.push("the table has fewer than 2 columns"),h<2||d<2)return X("polychor",{rho:NaN,warnings:c});let w=[-1/0,...Qc(r.map(_=>De(_))),1/0],g=[-1/0,...Qc(r[0].map((_,F)=>De(r.map(I=>I[F])))),1/0],N=$l(_=>{let F=Math.max(-rn,Math.min(rn,_)),I=[];for(let P=0;P<h;P++)for(let k=0;k<d;k++){if(r[P][k]===0)continue;let V=ko([w[P],g[k]],[w[P+1],g[k+1]],F);I.push(r[P][k]*Math.log(V))}return-De(I)},-rn,rn).optimum;if(!s&&!i)return X("polychor",{rho:N,warnings:c});let v=_=>[-1/0,..._,1/0],M=w.slice(1,h),p=g.slice(1,d),y=_=>s?Kh(r,v(_.slice(1,h)),v(_.slice(h)),_[0],!0):Kh(r,w,g,_[0],!1),{theta:x,at:E,bounded:R}=Yh(y,s?[N,...M,...p]:[N]),A=x[0];if(R&&c.push(Xh(A)),!i)return X("polychor",{rho:A,warnings:c});let S=s?v(x.slice(1,h)):w,T=s?v(x.slice(h)):g,L=De(r.map(_=>De(_))),C=[],q=[];for(let _=0;_<h;_++)for(let F=0;F<d;F++){let I=r[_][F],P=ko([S[_],T[F]],[S[_+1],T[F+1]],A);if(I===0)C.push(L*P);else if(a===0)C.push(I*Math.log1p(1e-6/I),I*jh(L*P/I-1));else{let k=D(I,a);q.push(k*Math.log1p(1e-6/k)),C.push(I*jh(L*P/I-1))}}let O=Gh(E.hessian,R);return X("polychor",{rho:A,warnings:c,var:a===0?O:O.map(_=>_.map(F=>D(F,-a))),chisq:a===0?2*De(C):D(2*De(C),a)+2*De(q),df:h*d-h-d,n:D(L,a),rowCuts:S.slice(1,h),colCuts:T.slice(1,d)})}function ay(e,t,n,r){let o=e.length,s=[le(.25),le(.5),le(.75)],i=(h,d)=>d.filter(w=>w<h).length,c=Jc(e).map(h=>i(h,n)),a=Jc(t).map(h=>i(h,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));c.forEach((h,d)=>l[h][a[d]]++);let f=[-1/0,...n,1/0],m=[-1/0,...s,1/0],u=[];return l.forEach((h,d)=>h.forEach((w,g)=>{if(w===0)return;let b=ko([f[d],m[g]],[f[d+1],m[g+1]],r);u.push(w*Math.log((w+1e-6)/(b*o)))})),2*De(u)}function iy(e,t,n={}){if(K(n,ty,"polyserial"),e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,s=e.map((E,R)=>!Number.isNaN(E)&&!Number.isNaN(t[R])),i=e.filter((E,R)=>s[R]),c=kt(t.filter((E,R)=>s[R])),a=i.length,l=Zc(c);if(l.length<2)return X("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((E,R)=>[E,R+1])),m=c.map(E=>f.get(E)),u=l.map(()=>0);for(let E of m)u[E-1]++;let h=Qc(u),d=[],w=Math.sqrt((a-1)/a)*Math.sqrt(st(m))*Wl(i,m)[0]/De(h.map(E=>Gt(E)));if(Math.abs(w)>rn){let E=Math.sign(w)*rn;d.push(`initial correlation inadmissible, ${$n(w)}, set to ${$n(E)}`),w=E}if(!r&&!o)return X("polyserial",{rho:w,warnings:d});let g=Jc(i),b=m.map(E=>E-1),N=E=>ry(g,b,r?E.slice(1):h,E[0],r),{theta:v,at:M,bounded:p}=Yh(N,r?[w,...h]:[w]);if(p&&d.push(Xh(v[0])),!o)return X("polyserial",{rho:v[0],warnings:d});let y=r?v.slice(1):h,x=l.length;return X("polyserial",{rho:v[0],warnings:d,var:Gh(M.hessian,p),chisq:ay(m,g,y,r?v[0]:w),df:4*x-x-1,n:a,cuts:y})}var cy=["group","strata","rho","cencode"],ly=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function Pa(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function el(e){let t=vn(e),n=Array.from(new Set(t)).sort(Ar);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function fy(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 my(e,t,n={}){K(n,cy,"cuminc"),fy(e,t,n);let{group:r,strata:o,rho:s=0,cencode:i=0}=n,c=[],a=new mt;for(let p=0;p<e.length;p++)if(!(Number.isNaN(e[p])||Pa(t[p])||r!==void 0&&Pa(r[p])||o!==void 0&&Pa(o[p]))){if(!Number.isFinite(e[p]))throw new RangeError("'time' must contain only finite values");if(e[p]<0)throw new RangeError("'time' must be non-negative");c.push(p)}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]:vn(c.map(p=>r[p])),m=o===void 0?[1]:vn(c.map(p=>o[p])),u=vn([...c.map(p=>t[p]),i]),h=el(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let d=el(m),w=el(u),g=u[u.length-1],b=w.labels.filter(p=>p!==g);if(b.length===0)throw new Error("there are no events of any cause");let N=c.map((p,y)=>({time:e[p],status:u[y],group:r===void 0?0:h.index.get(f[y]),strata:o===void 0?0:d.index.get(m[y]),source:p})).sort((p,y)=>p.time-y.time),v=[];for(let p of b){let y=ut(p);for(let x=0;x<h.labels.length;x++){let E=N.filter(I=>I.group===x),R=E.reduce((I,P)=>Math.max(I,P.time),-1/0),A=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,S=ur(E.map(I=>I.time*A),E.map(I=>I.status===g?0:1)),T=[0],L=[0],C=[0],q=[],O=1;for(let I=0;I<S.time.length;I++){let P=S.nEvent[I];if(P===0)continue;let k=S.time[I]/A,V=S.nRisk[I],U=E.filter(Q=>Q.time===k&&Q.status===p).length;q.push({t:k,Y:V,d:P,dk:U,sb:O,sa:S.surv[I]}),O=S.surv[I]}let _=0,F=0;for(let I=0;I<q.length;I++){let P=q[I],k=_,V=F;_+=P.sb*P.dk/P.Y;let U=0;for(let Q=0;Q<=I;Q++){let re=q[Q],z=re.Y,J=re.d,Z=re.dk,ke=z===J?0:(J*(z-J)+2*Z*(J-Z))/((z-1)*(z-J)**2),ne=z===1?Z:Z*(z-Z)/(z*z*(z-1)),$=z===J||z===1?0:Z*(z-Z)/(z*(z-J)*(z-1)),Y=0;for(let B=0;B<=Q;B++){let j=q[B];Y+=j.sb*j.dk/j.Y}let W=_-Y;U+=W*W*ke+re.sb*re.sb*ne-2*W*re.sb*$}F=Math.max(0,U),P.dk>0&&(T.push(P.t,P.t),L.push(k,_),C.push(V,F))}T.push(R),L.push(_),C.push(F),v.push({name:`${ut(h.labels[x])} ${y}`,group:ut(h.labels[x]),cause:y,time:T,est:L,var:C})}}let M=h.labels.length>=2?uy(N,h.labels,b,d.labels.length,s,g):void 0;return X("cuminc",{curves:v,...M===void 0?{}:{tests:M},warnings:a.list})}function uy(e,t,n,r,o,s){let i=[];for(let c of n){let a=t.length,l=new Array(a).fill(0),f=Array.from({length:a},()=>new Array(a).fill(0));for(let w=0;w<r;w++){let g=e.filter(p=>p.strata===w),b=Array.from(new Set(g.filter(p=>p.status!==s).map(p=>p.time))).sort((p,y)=>p-y),N=new Array(a).fill(1),v=new Array(a).fill(0),M=[];for(let p of b){let y=new Array(a).fill(0),x=new Array(a).fill(0),E=new Array(a).fill(0);for(let I of g)I.time>=p&&y[I.group]++;for(let I of g)I.time===p&&I.status!==s&&(I.status===c?x[I.group]++:E[I.group]++);let R=N.slice(),A=v.slice(),S=y.map((I,P)=>I===0?0:I/R[P]),T=y.map((I,P)=>I===0?0:I*(1-A[P])/R[P]),L=x.reduce((I,P)=>I+P,0),C=S.reduce((I,P)=>I+P,0),q=C===0?0:L/C,O=M.length===0?1:M[M.length-1].Gafter,_=O-q,F=T.reduce((I,P)=>I+P,0);for(let I=0;I<a;I++){let P=O**o;F>0&&(l[I]+=P*(x[I]-T[I]*L/F)),y[I]>0?(N[I]=N[I]*(1-(x[I]+E[I])/y[I]),v[I]+=R[I]*x[I]/y[I]):N[I]=0}M.push({time:p,Y:y,d1:x,d2:E,Sbefore:R,Safter:N.slice(),Fbefore:A,h:S,R:T,dF0:q,Gbefore:O,Gafter:_})}for(let p=0;p<a;p++)for(let y=0;y<a;y++){let x=0;for(let E=0;E<a;E++)for(let R=0;R<M.length;R++){let A=M[R],S=A.h.reduce((J,Z)=>J+Z,0);if(!(S>0)||!(A.h[E]>0))continue;let T=A.Gbefore**o,L=T*A.h[p]*((p===E?1:0)-A.h[E]/S),C=T*A.h[y]*((y===E?1:0)-A.h[E]/S),q=0,O=0;for(let J=R+1;J<M.length;J++){let Z=M[J],ke=Z.h.reduce((B,j)=>B+j,0);if(!(ke>0))continue;let ne=Z.Gbefore**o,$=ne*Z.h[p]*((p===E?1:0)-Z.h[E]/ke),Y=ne*Z.h[y]*((y===E?1:0)-Z.h[E]/ke),W=Z.Gbefore===0?0:Z.dF0/Z.Gbefore;q+=$*W,O+=Y*W}let _=A.Safter[E]===0?0:A.Gafter/A.Safter[E],F=L+(1-_)*q,I=C+(1-_)*O,P=-_*q,k=-_*O,V=A.Y[E]===0?0:A.Sbefore[E]*A.d2[E]/A.Y[E],U=(F*I*A.dF0+P*k*V)/A.h[E],Q=A.d1.reduce((J,Z)=>J+Z,0),re=0;if(Q>1&&A.Y[E]>0&&A.Sbefore[E]>0){let J=1/A.Sbefore[E];S>J&&(re=Q*(Q-1)/S*(A.Y[E]*J*J/(S-J))*(F*I/(A.h[E]*A.h[E])))}let z=0;A.d2[E]>1&&A.Y[E]>1&&(z=(A.d2[E]-1)/(A.Y[E]-1)*(V/A.h[E])*P*k),x+=U-re-z}f[p][y]+=x}}let m=a-1,u=NaN,h=NaN,d=f.slice(0,m).map(w=>w.slice(0,m));try{let w=nt(d,l.slice(0,m),{tol:1e-12});u=l.slice(0,m).reduce((g,b,N)=>g+b*w[N],0),h=_t(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}i.push({cause:ut(c),stat:u,df:m,pValue:h})}return i}function hy(e,t){let n=Array.from(new Set(t.filter(s=>!Number.isNaN(s)))).sort((s,i)=>s-i),r=[],o=[];for(let s of e.curves){let i=[],c=[];for(let a of n){let l=-1;for(let f=0;f<s.time.length;f++)s.time[f]<=a&&(l=f);a<0||l<0||a>s.time[s.time.length-1]?(i.push(NaN),c.push(NaN)):(i.push(s.est[l]),c.push(s.var[l]))}r.push(i),o.push(c)}return{curveNames:e.curves.map(s=>s.name),times:n,est:r,var:o}}function Ln(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function py(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 tl(e,t){return De(e.map((n,r)=>n*t[r]))}function dy(e){return e.every(t=>t.every(Number.isFinite))}function gy(e,t,n,r={}){K(r,ly,"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 d=n?.[0]?.length??0,w=o?.[0]?.length??0,g=d+w;if(g===0)throw new RangeError("at least one covariate is required");if(n!==void 0&&n.some(j=>j.length!==d))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(j=>j.length!==w))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 b=[];for(let j=0;j<e.length;j++)if(!(Number.isNaN(e[j])||Pa(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");b.push(j)}let N=e.length-b.length;if(b.length===0)throw new RangeError("no complete observations");let v=b.map(j=>e[j]),M=vn([...b.map(j=>t[j]),c,i]),p=M.slice(0,b.length),y=M[M.length-2],x=M[M.length-1],E=b.map(j=>n===void 0?[]:Array.from(n[j])),R=b.map(j=>o===void 0?[]:Array.from(o[j])),A=Array.from(new Set(v.filter((j,te)=>p[te]===x))).sort((j,te)=>j-te);if(A.length===0)throw new RangeError("no observations of 'failcode'");let S=w===0?[[0]]:s(A).map(j=>Array.from(j));if(w>0&&(S.length!==A.length||S.some(j=>j.length!==w)||!dy(S)))throw new RangeError("'tf' must return a finite matrix with one row per unique failure time and one column per cov2 column");let T=A.map((j,te)=>v.map((fe,Re)=>[...E[Re],...R[Re].map((Ce,be)=>Ce*(S[te][be]??0))])),L=v.reduce((j,te)=>Math.max(j,Math.abs(te)),0),C=L>0&&L<1?2**Math.min(1023,Math.ceil(-Math.log2(L))):1,q=ur(v.map(j=>j*C),p.map(j=>j===y?1:0)),O=j=>{let te=j*C,fe=1;for(let Re=0;Re<q.time.length&&!(q.time[Re]>=te);Re++)fe=q.surv[Re];return fe},_=A.map(j=>v.map((te,fe)=>{if(te>=j)return 1;if(p[fe]===y||p[fe]===x)return 0;let Re=O(te);return Re===0?0:O(j)/Re}));for(let j=0;j<A.length;j++)for(let te=0;te<v.length;te++)v[te]===A[j]&&p[te]===x&&(_[j][te]=1);let F=j=>{let te=Array.from({length:g},()=>[]),fe=Array.from({length:g},()=>Array.from({length:g},()=>[])),Re=[],Ce=Ln(A.length,g),be=[],me=[],qe=[];for(let Fe=0;Fe<A.length;Fe++){let Ue=A[Fe],ze=v.map((ie,Ae)=>ie===Ue&&p[Ae]===x?Ae:-1).filter(ie=>ie>=0),et=ze.length,Be=T[Fe],Xe=Be.map(ie=>tl(j,ie)),pe=_[Fe].slice();for(let ie of ze)pe[ie]=1;let oe=Math.max(...Xe.map((ie,Ae)=>pe[Ae]>0?ie:-1/0));if(oe===-1/0)throw new Error("empty Fine-Gray risk set");let we=oe<500&&oe>-500,ge=we?Xe.map((ie,Ae)=>pe[Ae]*Math.exp(ie)):Xe.map((ie,Ae)=>pe[Ae]*Math.exp(ie-oe)),ce=De(ge),tt=Array.from({length:g},(ie,Ae)=>De(ge.map((Ke,$e)=>Ke*Be[$e][Ae])));if(!(ce>0))throw new Error("empty Fine-Gray risk set");let G=tt.map(ie=>ie/ce),Me=Array.from({length:g},(ie,Ae)=>{let Ke=Be[0][Ae],$e=0,ot=0;for(let ht of Be)$e=Math.max($e,Math.abs(ht[Ae]-Ke)),ot=Math.max(ot,Math.abs(ht[Ae]));return $e>0&&ot/$e>1e6}).some(Boolean),ye=Me?Array.from({length:g},(ie,Ae)=>Be.map(Ke=>Ke[Ae]-Be[0][Ae])):[],Ee=Me?ye.map(ie=>De(ie.map((Ae,Ke)=>ge[Ke]*Ae))/ce):[],ee=Me?void 0:Array.from({length:g},(ie,Ae)=>Array.from({length:g},(Ke,$e)=>De(ge.map((ot,ht)=>ot*Be[ht][Ae]*Be[ht][$e]))));me.push(G),qe.push(ge.map(ie=>ie/ce));let ve=we?et/ce:et*Math.exp(-oe)/ce;be.push(ve),Re.push(De(ze.map(ie=>tl(j,Be[ie])))-et*(we?Math.log(ce):oe+Math.log(ce)));for(let ie=0;ie<g;ie++){let Ke=De(ze.map($e=>Be[$e][ie]))-et*G[ie];te[ie].push(Ke),Ce[Fe][ie]=Ke}for(let ie=0;ie<g;ie++)for(let Ae=0;Ae<g;Ae++){let Ke=Me?De(ge.map(($e,ot)=>$e*(ye[ie][ot]-Ee[ie])*(ye[Ae][ot]-Ee[Ae])))/ce:ee[ie][Ae]/ce-G[ie]*G[Ae];fe[ie][Ae].push(et*Ke)}}let de=Ln(v.length,g);for(let Fe=0;Fe<A.length;Fe++){let Ue=T[Fe],ze=qe[Fe],et=me[Fe],Be=A[Fe];for(let Xe=0;Xe<v.length;Xe++){let pe=v[Xe]===Be&&p[Xe]===x?1:0,oe=v.filter((ge,ce)=>ge===Be&&p[ce]===x).length,we=pe-ze[Xe]*oe;for(let ge=0;ge<g;ge++)de[Xe][ge]+=(Ue[Xe][ge]-et[ge])*we}}let Oe=te.map(De),xe=fe.map(Fe=>Fe.map(De));return{loglik:De(Re),score:Oe,inf:xe,bfitj:be,res:Ce,eta:de,means:me,riskFactors:qe,zByTime:T}},I=f===void 0?new Array(g).fill(0):Array.from(f);if(!I.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let P=I.slice(),k=F(P),V=!1,U=0,Q=(j,te)=>Math.max(...j.score.map((fe,Re)=>Math.abs(fe)*Math.max(Math.abs(te[Re]),1)))<a*Math.max(Math.abs(j.loglik),1);for(;;){if(Q(k,P)){V=!0;break}if(U>=l)break;let j=nt(k.inf,k.score,{tol:0});P=P.map((te,fe)=>te+j[fe]),U++,k=F(P)}let re=nt(k.inf),z=Ln(g);if(m){let j=Ln(g),te=v.length,fe=Array.from(new Set(v)).sort((me,qe)=>me-qe),Re=fe.map(me=>{let qe=v.filter(Oe=>Oe>=me).length,de=v.filter((Oe,xe)=>Oe===me&&p[xe]===y).length;return qe===0?0:de/qe}),Ce=Ln(te,g);for(let me=0;me<fe.length;me++){let qe=fe[me],de=new Array(g).fill(0);for(let xe=0;xe<te;xe++)if(!(!(v[xe]<qe)||p[xe]===y||p[xe]===x))for(let Ve=0;Ve<A.length;Ve++){if(A[Ve]<qe)continue;let Fe=k.riskFactors[Ve][xe],Ue=v.filter((Be,Xe)=>Be===A[Ve]&&p[Xe]===x).length,ze=k.zByTime[Ve][xe],et=k.means[Ve];for(let Be=0;Be<g;Be++)de[Be]+=(ze[Be]-et[Be])*Fe*Ue}let Oe=v.filter(xe=>xe>=qe).length;if(Oe!==0)for(let xe=0;xe<te;xe++){let Ve=v[xe]===qe&&p[xe]===y?1:0,Fe=v[xe]>=qe?Re[me]:0;for(let Ue=0;Ue<g;Ue++)Ce[xe][Ue]+=de[Ue]/Oe*(Ve-Fe)}}for(let me=0;me<te;me++)py(j,k.eta[me].map((qe,de)=>qe+Ce[me][de]),k.eta[me].map((qe,de)=>qe+Ce[me][de]),1);z=Ln(g);let be=Qh(re,j);z=Qh(be,re)}else z=Ln(g).map(j=>j.map(()=>NaN));let J=m?z.map((j,te)=>Math.sqrt(j[te])):void 0,Z=J===void 0?void 0:P.map((j,te)=>j/J[te]),ke=Z===void 0?void 0:Z.map(j=>2*se(-Math.abs(j))),ne=m?le((1+u)/2):void 0,$=ne===void 0?void 0:P.map((j,te)=>Math.exp(j-ne*J[te])),Y=ne===void 0?void 0:P.map((j,te)=>Math.exp(j+ne*J[te])),W=m?P.map(Math.exp):void 0,B=h===void 0?[...Array.from({length:d},(j,te)=>`cov1${te+1}`),...Array.from({length:w},(j,te)=>`cov2${te+1}*tf${te+1}`)]:Array.from(h);return X("crr",{coefficients:P,names:B,var:z,...J===void 0?{}:{se:J,z:Z,pValue:ke,lower:$,upper:Y,expCoef:W},loglik:k.loglik,loglikNull:F(new Array(g).fill(0)).loglik,logtest:{statistic:2*(k.loglik-F(new Array(g).fill(0)).loglik),df:g},score:k.score,inf:m?k.inf:Ln(g).map(j=>j.map(()=>NaN)),invinf:m?re:Ln(g).map(j=>j.map(()=>NaN)),converged:V,iterations:U,n:b.length,nMissing:N,uftime:A,bfitj:k.bfitj,res:m?k.res:null,tfs:S,warnings:[],covariateDimensions:[d,w]})}function Qh(e,t){let n=Ln(e.length,t[0]?.length??0);for(let r=0;r<e.length;r++)for(let o=0;o<(t[0]?.length??0);o++)for(let s=0;s<t.length;s++)n[r][o]+=e[r][s]*t[s][o];return n}function wy(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 d=[...r===0?[]:t[m],...o===0?[]:n[m].map((v,M)=>v*i[h][M])],w=e.bfitj[h],g=tl(e.coefficients,d),b=w*Math.exp(g),N=Number.isFinite(b)&&(b!==0||w===0)?b:w===0?0:Math.exp(Math.log(w)+g);u+=N}f.push(-Math.expm1(-u))}return f})}var by=new Set(["type","time2","dist","weights","offset","strata","scale","init","df","control","intercept","confLevel"]),vy=["maxiter","relTolerance","tolerChol"],yy=["type","p","newX","newStrata","newOffset","seFit"],Ny=["type","weighted"],rl=new WeakMap,My=Math.log(2*Math.PI),Ey=e=>Number(e),xy=e=>e<-Math.LN2?Math.log1p(-Math.exp(e)):Math.log(-Math.expm1(e));function op(e,t){return t===-1/0?e:e<t?NaN:e+xy(t-e)}function Ry(e,t){if(e===t)return 0;let n=Math.max(e,t),r=Math.min(e,t),o=n+Math.log(-Math.expm1(r-n));return(e>t?1:-1)*Math.exp(o)}function Zh(e,t){return t===0?0:Number.isFinite(t)?Math.sign(t)*Math.exp(e+Math.log(Math.abs(t))):Math.exp(e)*t}function Ay(e,t,n){let r=t-e;return Number.isFinite(r)?r/n:t/n-e/n}function sp(e,t,n){let r=t-e;if(Number.isFinite(r))return Math.log(r)-Math.log(n);let o=Math.max(Math.abs(e),Math.abs(t));return Math.log(o)+Math.log(t/o-e/o)-Math.log(n)}function Sy(e,t,n){let r=sp(e,t,n),o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(o<1e-8){let s=t-e;return tn(e,t)+s*(o/24)}return o>50?t-n*r-n*Math.log1p(-Math.exp(-o)):e-n*(r-Math.log(Math.expm1(o)))}function Ty(e,t,n){let r=sp(e,t,n);if(r<Math.log(1e-8))return r-1;let o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(!Number.isFinite(o))return 0;let s=o>50?o+Math.log1p(-Math.exp(-o)):Math.log(Math.expm1(o)),i=r-s,c=o>50?r-Math.log1p(-Math.exp(-o)):i+o;return op(-Math.exp(i),-Math.exp(c))}function ky(e){return e<-36?e:-Math.log1p(Math.exp(-e))}function qy(e){return e>36?-e:-Math.log1p(Math.exp(e))}function Jh(e,t,n){if(e===-1/0)return-1/0;if(e===1/0)return 0;if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"){let r=Math.exp(e);return r===0?e:Math.log(-Math.expm1(-r))}return t==="logistic"||t==="loglogistic"?ky(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?se(e,{logP:!0}):an(e,n,{logP:!0})}function ep(e,t,n){return e===-1/0?0:e===1/0?-1/0:t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?-Math.exp(e):t==="logistic"||t==="loglogistic"?qy(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?se(e,{lowerTail:!1,logP:!0}):an(e,n,{lowerTail:!1,logP:!0})}function Da(e,t,n){return t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?e===1/0?-1/0:e-Math.exp(e):t==="logistic"||t==="loglogistic"?-Math.abs(e)-2*Math.log1p(Math.exp(-Math.abs(e))):t==="gaussian"||t==="loggaussian"||t==="lognormal"?-.5*(e*e+My):El(e,n,{log:!0})}function nl(e,t,n){if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"){let o=Math.exp(e);return[1-o,o*o-3*o+1]}if(t==="logistic"||t==="loglogistic"){let o=Math.exp(-Math.abs(e));return[-Math.tanh(e/2),1-6*o/((1+o)*(1+o))]}if(t==="gaussian"||t==="loggaussian"||t==="lognormal")return[-e,e*e-1];let r=n+e*e;return[-(n+1)*e/r,(n+1)*((n+2)*e*e-n)/(r*r)]}function Iy(e,t,n){if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh")return-Math.exp(e);if(t==="logistic"||t==="loglogistic"){let r=Math.exp(-Math.abs(e));return-2*r/((1+r)*(1+r))}return t==="gaussian"||t==="loggaussian"||t==="lognormal"?-1:-(n+1)*(n-e*e)/((n+e*e)*(n+e*e))}function Va(e,t,n,r,o,s,i){let c=e===-1/0?-1/0:(e-r)/o,a=t===1/0?1/0:(t-r)/o;if(n===1){let[C]=nl(a,s,i),q=Da(a,s,i),O=Iy(a,s,i);return{g:q-Math.log(o),e:-C/o,ee:O/(o*o),t:-a*C-1,tt:a*C+a*a*O,et:(C+a*O)/o}}let l=Jh(a,s,i),f=Jh(c,s,i),m=ep(c,s,i),u=ep(a,s,i),h=n===0?m:n===2?l:op(c>0?m:l,c>0?u:f),w=c===-1/0?0:Math.exp(n===0&&(s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh")?c:Da(c,s,i)-h),g=a===1/0?0:Math.exp(Da(a,s,i)-h),[b]=nl(c,s,i),[N]=nl(a,s,i),v=w===0||c===-1/0?0:w*b,M=g===0||a===1/0?0:g*N,p=c===-1/0||w===0?0:c*w,y=a===1/0||g===0?0:a*g,x=c===-1/0||v===0?0:c*c*v,E=a===1/0||M===0?0:a*a*M,R=-(g-w)/o,A=(M-v)/(o*o)-R*R,S=-(y-p),T=E-x-S*(1+S),L=((a===1/0||M===0?0:a*M)-(c===-1/0||v===0?0:c*v))/o-R*(1+S);return{g:h,e:R,ee:A,t:S,tt:T,et:L}}function _y(e){let t=e.length,n=e.map((r,o)=>[...r,...r.map((s,i)=>o===i?1:0)]);for(let r=0;r<t;r++){let o=r;for(let i=r+1;i<t;i++)Math.abs(n[i][r])>Math.abs(n[o][r])&&(o=i);if(!Number.isFinite(n[o][r])||Math.abs(n[o][r])<=1e-13*Math.max(Number.MIN_VALUE,...e.map((i,c)=>Math.abs(i[c]??0))))throw new Error("singular information matrix");[n[r],n[o]]=[n[o],n[r]];let s=n[r][r];for(let i=0;i<2*t;i++)n[r][i]/=s;for(let i=0;i<t;i++)if(i!==r){let c=n[i][r];for(let a=0;a<2*t;a++)n[i][a]-=c*n[r][a]}}return n.map(r=>r.slice(t))}function ap(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 ip(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 tp(e){let t=e.length;for(let n=0;n<t;n++)if(e[n][n]>0){e[n][n]=1/e[n][n];for(let r=n+1;r<t;r++){e[r][n]=-e[r][n];for(let o=0;o<n;o++)e[r][o]+=e[r][n]*e[n][o]}}for(let n=0;n<t;n++){if(e[n][n]===0){for(let r=0;r<t;r++)e[n][r]=0,e[r][n]=0;continue}for(let r=n+1;r<t;r++){let o=e[r][n]*e[r][r];e[n][r]=o;for(let s=n;s<r;s++)e[n][s]+=o*e[r][s]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}function ps(e,t){return e.map(n=>gr(n,t))}function gr(e,t){let n=e.reduce((r,o,s)=>r+o*t[s],0);return vr(e,t,n)}function cp(e,t=1e-10){let n=t>0?Math.min(65536,1/Math.sqrt(t)):65536,r=Array.from({length:e[0].length},(o,s)=>{let i=e.map(a=>a[s]),c=0;for(let a of i)c=Math.max(c,Math.abs(a));if(c>n||c>0&&c<1/n){let a=Se([i]);return{values:a.groups[0],exponent:a.k}}return{values:i,exponent:0}});return{values:e.map((o,s)=>r.map(i=>i.values[s])),exponent:r.map(o=>o.exponent)}}function np(e,t,n){return e<=0?-1/0:e>=1?1/0:t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?Math.log(-Math.log1p(-e)):t==="logistic"||t==="loglogistic"?Math.log(e/(1-e)):t==="gaussian"||t==="loggaussian"||t==="lognormal"?le(e):Wt(e,n)}function rp(e,t,n,r,o,s,i,c,a,l,f,m,u,h,d,w=1e-10){let g=e[0].length,b=g+(m===void 0?a:0),N=cp(e,w),v=N.values,M=N.exponent,p=u.map((k,V)=>V<g?Le(k,M[V]):k),y=(k,V)=>{let U=0,Q=Array(b).fill(0),re=Array.from({length:b},()=>Array(b).fill(0)),z=Array.from({length:b},()=>Array(b).fill(0));for(let J=0;J<t.length;J++){let Z=c[J],ke=gr(v[J],k.slice(0,g))+i[J],ne=m??Math.exp(k[g+Z]),$=Va(n[J],r[J],o[J],ke,ne,l,f),Y=s[J];U+=Y*$.g;let W=Array(b).fill(0);for(let B=0;B<g;B++)W[B]=v[J][B]*$.e;m===void 0&&(W[g+Z]=$.t);for(let B=0;B<b;B++){Q[B]+=Y*W[B];for(let j=0;j<b;j++)z[B][j]+=Y*W[B]*W[j]}if(V){for(let B=0;B<g;B++){for(let j=0;j<g;j++)re[B][j]+=Y*v[J][B]*v[J][j]*$.ee;m===void 0&&(re[B][g+Z]+=Y*v[J][B]*$.et)}if(m===void 0){for(let B=0;B<g;B++)re[g+Z][B]+=Y*v[J][B]*$.et;re[g+Z][g+Z]+=Y*$.tt}}}return{ll:U,u:Q,H:re,B:z}},x=k=>{let V=k.H.map(Q=>Q.map(re=>-re)),U=ap(V,w);return{factor:V,rank:U}},E=k=>{let V=y(k,!0),U=x(V),Q=U.rank>=0?ip(U.factor,V.u):ps(_y(V.B),V.u);return{e:V,delta:Q}},R=y(p,!0),A=R.ll,S=0,T=!1,L=!1,C;try{C=E(p)}catch{C={e:R,delta:Array(b).fill(0)}}let q=p.map((k,V)=>k+C.delta[V]);if(h===0){let k=x(R),V=tp(k.factor),U=V.map(re=>re.slice()),Q=V.map((re,z)=>re[z]===0?NaN:p[z]/Math.sqrt(Math.max(0,re[z])));return{b:p.map((re,z)=>z<g?Le(re,-M[z]):re),ll:R.ll,iter:S,V:V.map((re,z)=>re.map((J,Z)=>Le(J,(z<g?-M[z]:0)+(Z<g?-M[Z]:0)))),VScaled:U,xExponent:M,score:R.u.map((re,z)=>z<g?Le(re,M[z]):re),z:Q,warn:T}}for(let k=1;k<=h;k++){let V=y(q,!0);if(Number.isFinite(V.ll)&&Math.abs(1-A/V.ll)<=d){p.splice(0,p.length,...q),R=V,A=V.ll,S=k,L=!0;break}if(!Number.isFinite(V.ll)||V.ll<A){let U=q.slice();q=p.map((Q,re)=>Q+(U[re]-Q)/3);for(let Q=0;Q<a;Q++)m===void 0&&(q[g+Q]=Math.max(q[g+Q],p[g+Q]-1.1));S=k;continue}if(p.splice(0,p.length,...q),R=V,A=V.ll,S=k,k<h)try{let U=E(p);q=p.map((Q,re)=>Q+U.delta[re])}catch{q=p.slice()}else try{let U=E(p),Q=p.map((z,J)=>z+U.delta[J]),re=y(Q,!1);Number.isFinite(re.ll)&&re.ll>=A&&p.splice(0,p.length,...Q)}catch{}}S===h&&h>=2&&!L&&(T=!0);let O=y(p,!0),_=x(O),F=tp(_.factor),I=F.map(k=>k.slice()),P=F.map((k,V)=>k[V]===0?NaN:p[V]/Math.sqrt(Math.max(0,k[V])));return{b:p.map((k,V)=>V<g?Le(k,-M[V]):k),ll:A,iter:S,V:F.map((k,V)=>k.map((U,Q)=>Le(U,(V<g?-M[V]:0)+(Q<g?-M[Q]:0)))),VScaled:I,xExponent:M,score:O.u.map((k,V)=>V<g?Le(k,M[V]):k),z:P,warn:T}}function Ly(e,t,n=[],r={}){K(r,by,"survreg"),K(sn(r,"control"),vy,"survreg","control");let o=r.type??"right",s=r.dist??"weibull",i=r.df??4,c=r.intercept??!0,a=e.length,l=t===void 0?Array(a).fill(1):Array.from(t,Number);if(l.length!==a||n.length!==0&&n.length!==a)throw new RangeError("variable lengths differ");if(o==="counting")throw new Error("start-stop type Surv objects are not supported");if(!["right","left","interval","interval2"].includes(o))throw new TypeError("invalid survival type");if(!["extreme","logistic","gaussian","weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic","t"].includes(s))throw new RangeError("'arg' should be one of \u201Cextreme\u201D, \u201Clogistic\u201D, \u201Cgaussian\u201D, \u201Cweibull\u201D, \u201Cexponential\u201D, \u201Crayleigh\u201D, \u201Cloggaussian\u201D, \u201Clognormal\u201D, \u201Cloglogistic\u201D, \u201Ct\u201D");if((o==="interval"||o==="interval2")&&r.time2===void 0)throw new TypeError("time2 is required for interval data");if(r.time2!==void 0&&r.time2.length!==a)throw new RangeError("variable lengths differ (found for 'time2')");if(r.scale!==void 0&&(!Number.isFinite(r.scale)||r.scale<0))throw new RangeError("Invalid scale value");if(s==="t"&&(!(i>=3)||!Number.isFinite(i)))throw new RangeError("Degrees of freedom must be >=3");if(s!=="t"&&r.df!==void 0)throw new RangeError("Extreme value distribution has no optional parameters");let f=["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(s),m=r.scale!==void 0&&r.scale>0?r.scale:void 0,u=s==="exponential"||s==="rayleigh"?s==="exponential"?1:.5:m,h=r.time2,d=r.weights??Array(a).fill(1),w=r.offset??Array(a).fill(0),g=r.strata??Array(a).fill("1");for(let[G,ue]of[[d,"weights"],[w,"offset"],[g,"strata"]])if(G.length!==a)throw new RangeError(`variable lengths differ (found for '${ue}')`);let b=[],N=[],v=[],M=[],p=[],y=[],x=[],E=[],R=[],A=(o==="right"||o==="left")&&l.some(G=>Number(G)===2),S=G=>{R.includes(G)||R.push(G)},T=[...new Set(g.map(String))].sort(),L=g.map(G=>T.indexOf(String(G))),C=[];if(u!==void 0&&T.length>1)throw new Error("The scale argument is not valid with multiple strata");for(let G=0;G<a;G++){let ue=n.length===0?[]:Array.from(n[G]);if((o==="interval2"?Number.isNaN(e[G])&&Number.isNaN(h[G]):o==="interval"&&Ey(l[G])===3?Number.isNaN(e[G])||Number.isNaN(h[G]):Number.isNaN(e[G]))||o!=="interval2"&&Number.isNaN(l[G])||Number.isNaN(d[G])||Number.isNaN(w[G])||Number.isNaN(g[G])||ue.some(Number.isNaN))continue;if(!(d[G]>0))throw new RangeError("Invalid weights, must be >0");if(ue.some(ve=>!Number.isFinite(ve)))throw new Error("data contains an infinite predictor");let ye=e[G],Ee=e[G],ee=Number(l[G]);if(o==="right")if(A){if(ee!==1&&ee!==2){S("Invalid status value, converted to NA");continue}ee===1&&(Ee=1/0),ee=ee===2?1:0}else{if(ee!==0&&ee!==1){S("Invalid status value, converted to NA");continue}ee===0&&(Ee=1/0),ee=ee===1?1:0}else if(o==="left")if(A){if(ee!==1&&ee!==2){S("Invalid status value, converted to NA");continue}ee===1&&(ye=-1/0),ee=ee===2?1:2}else{if(ee!==0&&ee!==1){S("Invalid status value, converted to NA");continue}ee===0&&(ye=-1/0),ee=ee===1?1:2}else if(o==="interval"){if(Ee=h[G],ee===0)ye=e[G],Ee=1/0;else if(ee===1)Ee=ye;else if(ee===2)Ee=e[G],ye=-1/0;else if(ee!==3){S("Invalid status value, converted to NA");continue}if(ee===3&&ye>Ee){S("Invalid interval: start > stop, NA created");continue}if(ee===3&&ye===Ee)throw new Error("initial iteration failed (use starting estimates?)")}else if(o==="interval2"){let ve=e[G],ie=h[G];if(Number.isNaN(ve)&&Number.isNaN(ie))continue;if(ve>ie){S("Invalid interval: start > stop, NA created");continue}ve===ie?(ye=ve,Ee=ie,ee=1):!Number.isFinite(ve)||ve===0?(ye=-1/0,Ee=ie,ee=2):Number.isFinite(ie)?(ye=ve,Ee=ie,ee=3):(ye=ve,Ee=1/0,ee=0)}if(f){if((Number.isFinite(ye)&&ye<0||Number.isFinite(Ee)&&Ee<0)&&S("NaNs produced"),ye===1/0||Ee===-1/0||Number.isFinite(ye)&&ye<=0||Number.isFinite(Ee)&&Ee<=0)throw new Error("Invalid survival times for this distribution");Number.isFinite(ye)&&(ye=Math.log(ye)),Number.isFinite(Ee)&&(Ee=Math.log(Ee))}b.push(G),N.push(ee===1?Ee:Number.isFinite(ye)&&Number.isFinite(Ee)?tn(ye,Ee):Number.isFinite(ye)?ye:Ee),v.push(ye),M.push(Ee),p.push(ee),y.push(d[G]),x.push(w[G]),E.push(ue),C.push(L[G])}if(b.length===0)throw new Error("initial iteration failed (use starting estimates?)");if(!p.some(G=>G===1||G===3))throw new Error("initial iteration failed (use starting estimates?)");if(N.some(G=>!Number.isFinite(G)))throw new Error("initial iteration failed (use starting estimates?)");if(E.some(G=>G.length!==E[0].length))throw new TypeError("all rows of the matrix must have the same length");let q=E[0].length,O=q+(c?1:0),_=E.map(G=>c?[1,...G]:G),F=T.length,I=N.map((G,ue)=>G-x[ue]),P=y.reduce((G,ue)=>G+ue,0),k=I.reduce((G,ue,Me)=>G+ue*y[Me],0)/P,V=I.reduce((G,ue,Me)=>G+y[Me]*(ue-k)**2,0)/P,U=Math.max(...I)-Math.min(...I),Q=s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh"?V/1.64:s==="logistic"||s==="loglogistic"?V/3.2:s==="t"?V*(i-2)/i:V,re=u??2*Math.sqrt(Q);if(u===void 0&&(!(re>0)||!(U>0)))throw new Error("initial iteration failed (use starting estimates?)");let z=O+(u===void 0?F:0),J=Array(z).fill(0);if(c&&(J[0]=k),u===void 0)for(let G=0;G<F;G++)J[O+G]=Math.log(re);let Z=r.control??{},ke=Z.maxiter??30,ne=Z.relTolerance??1e-9;if(!Number.isInteger(ke)||ke<0||!Number.isFinite(ne)||ne<0)throw new RangeError("invalid control");if(Z.tolerChol!==void 0&&(!Number.isFinite(Z.tolerChol)||Z.tolerChol<0))throw new RangeError("invalid control");let $=_.map(()=>[1]),Y=_.every(G=>G.length===1&&G[0]===1);if(r.init!==void 0&&r.init.length!==O&&r.init.length!==z)throw new Error("Wrong length for initial parameters");let B=N.map((G,ue)=>Va(v[ue],M[ue],p[ue],G,u??re,s,i)).map((G,ue)=>{let Me=-y[ue]*G.ee;return Me>0&&Number.isFinite(Me)?{w:Me,r:N[ue]-G.e/G.ee-x[ue]}:{w:0,r:0}}),j=B.reduce((G,ue)=>G+ue.w,0);if(!(j>0))throw new Error("initial iteration failed (use starting estimates?)");let te=B.reduce((G,ue)=>G+ue.w*ue.r,0)/j,fe=[r.init!==void 0&&Y?r.init[0]:te,...u===void 0?Array(F).fill(Math.log(re)):[]];if(Y&&r.init!==void 0&&r.init.length===z)for(let G=0;G<F;G++)fe[1+G]=r.init[1+G];let Re=Y?ke:Math.max(ke,1e3),Ce=rp($,N,v,M,p,y,x,C,F,s,i,u,fe,Re,ne,Z.tolerChol??1e-10),be=J.slice();if(u===void 0)for(let G=0;G<F;G++)be[O+G]=Ce.b[1+G];if(!Y&&r.init!==void 0){for(let G=0;G<O;G++)be[G]=r.init[G];if(r.init.length===z)for(let G=O;G<z;G++)be[G]=r.init[G]}else if(!Y&&O>0){let G=E.map((Ee,ee)=>{let ve=u??Math.exp(Ce.b[1+C[ee]]),ie=Va(v[ee],M[ee],p[ee],N[ee],ve,s,i),Ae=-y[ee]*ie.ee;return Ae>0&&Number.isFinite(Ae)?{w:Ae,r:N[ee]-ie.e/ie.ee-x[ee]}:{w:0,r:0}}),ue=cp(_,Z.tolerChol??1e-10),Me=Array.from({length:O},(Ee,ee)=>Array.from({length:O},(ve,ie)=>ue.values.reduce((Ae,Ke,$e)=>Ae+G[$e].w*Ke[ee]*Ke[ie],0))),ye=Array.from({length:O},(Ee,ee)=>ue.values.reduce((ve,ie,Ae)=>ve+G[Ae].w*ie[ee]*G[Ae].r,0));try{let Ee=Me.map(ie=>ie.slice()),ve=ap(Ee,Z.tolerChol??1e-10)>=0?ip(Ee,ye):ps(nt(Me),ye);be.splice(0,O,...ve.map((ie,Ae)=>Le(ie,-ue.exponent[Ae])))}catch{}}let me=Y?Ce:rp(_,N,v,M,p,y,x,C,F,s,i,u,be,ke,ne,Z.tolerChol??1e-10),qe=f?b.reduce((G,ue,Me)=>G+(p[Me]===1?y[Me]*Math.log(e[ue]):0),0)*-1:0,de=me.V.map(G=>G.slice()),Oe=me.b.slice(0,O).map((G,ue)=>Number.isNaN(me.z[ue])?NaN:G),xe=Oe.map((G,ue)=>(de[ue]?.[ue]??0)===0?0:Math.sqrt(Math.max(0,de[ue]?.[ue]??0))),Fe=Oe.map((G,ue)=>me.z[ue]).map(G=>2*se(-Math.abs(G))),Ue=u!==void 0?[u]:Array.from({length:F},(G,ue)=>Math.exp(me.b[O+ue])),ze=Array.from({length:O},(G,ue)=>_.reduce((Me,ye)=>Me+ye[ue],0)/_.length),et=_.map((G,ue)=>gr(G,me.b.slice(0,O))+x[ue]),Be=2*(me.ll+qe-(Ce.ll+qe)),Xe=O+(u===void 0?F:0)-(1+(u===void 0?F:0)),pe=[...Oe.map((G,ue)=>ue===0&&c?"(Intercept)":`x${ue+(c?0:1)}`),...u===void 0?F===1?["Log(scale)"]:T:[]],oe=[...Oe,...u===void 0?me.b.slice(O):[]],we=oe.map((G,ue)=>(de[ue]?.[ue]??0)===0?0:Math.sqrt(Math.max(0,de[ue]?.[ue]??0))),ge=oe.map((G,ue)=>me.z[ue]),ce=ge.map(G=>2*se(-Math.abs(G))),tt={coefficients:Oe,coefNames:pe.slice(0,O),scale:Ue,table:{value:oe,se:we,z:ge,p:ce},strataLevels:T.map(G=>g.find(ue=>String(ue)===G)),hasOffset:r.offset!==void 0,icoef:u===void 0||b.length===1?Ce.b.slice():[Ce.b[0],Math.log(u)],var:de,loglik:[Ce.ll+qe,me.ll+qe],iter:me.iter,df:O+(u===void 0?F:0),idf:1+(u===void 0?F:0),dfResidual:b.length-(O+(u===void 0?F:0)),n:b.length,linearPredictors:et,means:ze,score:me.score.slice(),tableRowNames:pe,value:oe,se:we,z:ge,p:ce,chi:Be,chiDf:Xe,chiP:_t(Be,Xe,{lowerTail:!1}),dist:s,distributionDf:i,parms:i,intercept:c,x:_,offset:x,rows:b,weights:y,strata:C,y:N,lower:v,upper:M,status:p,type:o,warnings:[...R,...me.warn||Ce.warn?["Ran out of iterations and did not converge"]:[],...s==="exponential"&&r.scale!==void 0&&r.scale>0?["Exponential has a fixed scale, user specified value ignored"]:[]]};return rl.set(tt,{V:me.VScaled,xExponent:me.xExponent}),X("survreg",tt)}function Cy(e,t={}){K(t,yy,"survregPredict");let n=t.type??"lp",r=t.newX===void 0?e.x:t.newX.map(h=>e.intercept?[1,...h]:Array.from(h)),o=e.hasOffset?t.newOffset??(t.newX===void 0?e.offset:Array(r.length).fill(0)):Array(r.length).fill(0);if(o.length!==r.length)throw new RangeError("variable lengths differ (found for 'newOffset')");let s=e.strataLevels.map(String),i=t.newStrata?.map(h=>s.indexOf(String(h)))??(t.newX===void 0?e.strata.slice():r.map(()=>0));if(e.strataLevels.length>1&&t.newStrata===void 0&&t.newX!==void 0)throw new Error("object 's' not found");if(t.newStrata!==void 0&&t.newStrata.length!==r.length)throw new RangeError("variable lengths differ (found for 'newStrata')");if(t.newX!==void 0&&r.some(h=>h.length!==e.coefficients.length))throw new RangeError("newX has the wrong number of columns");if(i.some(h=>h<0))throw new Error("unknown stratum in newStrata");if(!["lp","linear","link","response","quantile","uquantile"].includes(n))throw new RangeError("invalid prediction type");let c=t.p===void 0?[.1,.9]:typeof t.p=="number"?[t.p]:Array.from(t.p),a=["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist),l=r.map((h,d)=>{if(h.some(Number.isNaN))return n==="quantile"||n==="uquantile"?c.map(()=>NaN):NaN;let w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[d]:gr(h,e.coefficients)+o[d],g=e.scale.length===1?e.scale[0]:e.scale[i[d]];return n==="lp"||n==="linear"||n==="link"?w:n==="response"?a?Math.exp(w):w:c.map(b=>{let N=w+g*np(b,e.dist,e.distributionDf);return n==="uquantile"?N:a?Math.exp(N):N})}),f=(n==="quantile"||n==="uquantile")&&c.length===1?l.map(h=>h[0]):l;if(!t.seFit)return f;let m=r.map((h,d)=>{if(h.some(Number.isNaN))return n==="quantile"||n==="uquantile"?c.map(()=>NaN):NaN;let w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[d]:gr(h,e.coefficients)+o[d],g=e.scale.length===1?e.scale[0]:e.scale[i[d]],b=rl.get(e),N=b?.V??e.var,v=M=>{let p=Array(N.length).fill(0);for(let y=0;y<h.length;y++)p[y]=b?Le(h[y],-b.xExponent[y]):h[y];return N.length>h.length&&(p[h.length+i[d]]=M*g),Math.sqrt(Math.max(0,gr(p,ps(N,p))))};return n==="lp"||n==="linear"||n==="link"?v(0):n==="response"?a?Zh(w,v(0)):v(0):c.map(M=>{let p=np(M,e.dist,e.parms),y=v(p),x=w+g*p;return n==="uquantile"?y:a?Zh(x,y):y})}),u=(n==="quantile"||n==="uquantile")&&c.length===1?m.map(h=>h[0]):m;return{fit:f,seFit:u,warnings:[]}}function Oy(e,t={}){K(t,Ny,"survregResiduals");let n=t.type??"response",r=e.distributionDf,o=e.coefficients.length,s=e.var.length,i=t.weighted??!1;if(!["response","deviance","working","dfbeta","dfbetas","ldcase","ldresp","ldshape","matrix"].includes(n))throw new RangeError("invalid residual type");let c=rl.get(e),a=c?.V??e.var,l=e.x.map((f,m)=>{let u=e.linearPredictors[m],h=e.scale.length===1?e.scale[0]:e.scale[e.strata[m]],d=Va(e.lower[m],e.upper[m],e.status[m],u,h,e.dist,r),w=i?e.weights[m]:1,g=f.map((R,A)=>c?Le(R,-c.xExponent[A]):R),b=Array(s).fill(0);for(let R=0;R<o;R++)b[R]=g[R]*d.e;s>o&&(b[o+e.strata[m]]=d.t);let N=ps(a,b),v=N.map((R,A)=>c&&A<o?Le(R,-c.xExponent[A]):R),M=gr(b,N);if(n==="matrix")return[d.g,d.e,d.ee,d.t,d.tt,d.et].map(R=>R*w);if(n==="working")return-d.e/d.ee*w;if(n==="dfbeta")return v.map(R=>R*w);if(n==="dfbetas")return N.map((R,A)=>R/Math.sqrt(a[A]?.[A]??NaN)*w);if(n==="ldcase")return M*w;if(n==="ldresp"||n==="ldshape"){let R=Array(s).fill(0);for(let A=0;A<o;A++)R[A]=n==="ldresp"?h*(-g[A]*d.ee):g[A]*d.et;return s>o&&(R[o+e.strata[m]]=n==="ldresp"?-h*d.et:d.tt),gr(R,ps(a,R))*w}let p=Da(0,e.dist,r)-Math.log(h),y=e.status[m]===3?tn(e.lower[m],e.upper[m]):e.y[m];e.status[m]===3&&(e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh")&&(y=Sy(e.lower[m],e.upper[m],h));let x=e.status[m]===0?1:e.status[m]===2?-1:Math.sign(y-u);if(n==="deviance"){let R=e.status[m]===1?p:0;if(e.status[m]===3){let A=Ay(e.lower[m],e.upper[m],h);e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh"?R=Ty(e.lower[m],e.upper[m],h):e.dist==="logistic"||e.dist==="loglogistic"?R=Math.log(Math.tanh(A/4)):e.dist==="gaussian"||e.dist==="loggaussian"||e.dist==="lognormal"?R=Math.log(2*se(A/2)-1):e.dist==="t"&&(R=Math.log(2*an(A/2,e.distributionDf)-1))}return x*Math.sqrt(Math.max(0,2*(R-d.g)))*w}let E=y-u;return(["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist)?Ry(y,u):E)*w});return l}var Fy=["n","power","r","type","nfractional","alpha","sidedTest"],Py=["n","power","r","type","nfractional","alpha","sidedTest"],Dy=["n","power","r","type","nfractional","alpha","sidedTest"],Vy=["n","power","r","type","nfractional","alpha","sidedTest"],$y=["n","power","r","type","nfractional","alpha","sidedTest"],zy=["n","power","r","type","nfractional","alpha","sidedTest"],Wy=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],jy=["alpha"],Ky=["alpha"],Uy=["alpha"],By=["alpha"],Hy=["alpha"],Yy=["nmax"],Dr=.05,hn=e=>le(e),Bn=e=>se(e),Pr=e=>Number.isFinite(e),qt=e=>Pr(e)&&e>=0&&e<=1,xn=e=>Pr(e)&&e>0;function Je(e,t,n,r){if(typeof e!="number")throw new TypeError(`'${t}' must be numeric`);if(!n(e))throw new RangeError(r)}function Vr(e){Je(e,"alpha",qt,"'alpha' must be a probability in [0, 1]")}function Gy(e,t){return[e*t/(1+t),e/(1+t)]}function Ro(e,t,n,r,o,s,i={}){let{n:c,power:a}=i,l=i.r??1,f=i.alpha??Dr,m=i.nfractional??!1,u=e==="equivalence"?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(Je(n,"treat",Pr,"'treat' must be finite"),Je(r,"control",Pr,"'control' must be finite"),t==="binary"?(Je(n,"treat",qt,"'treat' must be a probability in [0, 1]"),Je(r,"control",qt,"'control' must be a probability in [0, 1]")):Je(o,"sigma",xn,"'sigma' must be positive and finite"),Je(l,"r",xn,"'r' must be positive and finite"),Vr(f),(e!=="equivalence"||u!=="equality")&&Je(s,"delta",L=>Pr(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");nn([c,a],"exactly one of 'n' and 'power' must be NULL"),Ne(a)||Je(a,"power",L=>qt(L)&&L>0&&L<1,"'power' must be strictly between 0 and 1"),c!=null&&Je(c,"n",L=>Pr(L)&&L>0,"'n' must be positive and finite");let d=n-r,w=t==="binary"?n*(1-n):o*o,g=t==="binary"?r*(1-r):o*o,b=e==="equivalence"&&u==="equality",N=b?void 0:s,v=e==="noninferiority"?d+N:e==="superiority"?d-N:b?d:N-Math.abs(d);if(e==="noninferiority"&&v<=0)throw new RangeError("Target power is not reachable under the specified hypotheses");let M=e==="superiority"?le(f/h,{lowerTail:!1}):b?le(f/2,{lowerTail:!1}):le(f,{lowerTail:!1}),p=a??.8,y=b||e!=="equivalence"?le(1-p,{lowerTail:!1}):le((1-p)/2,{lowerTail:!1}),x,E,R,A,S;if(Ne(c))v===0?(R=1/0,A=1/0):(A=(M+y)**2*(w/l+g)/(v*v),R=l*A),m?(x=R,E=A):(E=Math.ceil(A),x=Math.ceil(e==="equivalence"&&t==="continuous"||e==="superiority"?l*E:R)),S=a;else{[R,A]=Gy(c,l),x=m?R:Math.ceil(R),E=m?A:Math.ceil(A);let L=Math.sqrt(w/x+g/E);if(e==="equivalence"&&!b)S=Math.max(0,Bn((s-d)/L-hn(1-f))+Bn((s+d)/L-hn(1-f))-1);else if(b)S=Bn(Math.abs(d)/L-hn(1-f/2))+Bn(-Math.abs(d)/L-hn(1-f/2));else if(e==="noninferiority")S=Bn((d+s)/L-hn(1-f));else{let C=Math.abs(d-s)/L;S=Bn(C-M)+Bn(-C-M)}}return{nTotal:x+E,nTreat:x,nControl:E,nTotalUnrounded:R+A,nTreatUnrounded:R,nControlUnrounded:A,power:S,...N===void 0?{}:{delta:N},warnings:[]}}function Xy(e,t,n,r={}){return K(r,Fy,"epiSsequb"),X("epiSsequb",Ro("equivalence","binary",e,t,void 0,n??void 0,r))}function Qy(e,t,n,r,o={}){K(o,Py,"epiSsequc");let s=Ro("equivalence","continuous",e,t,n,r??void 0,o);return X("epiSsequc",s)}function Zy(e,t,n,r={}){return K(r,Dy,"epiSsninfb"),X("epiSsninfb",Ro("noninferiority","binary",e,t,void 0,n,r))}function Jy(e,t,n,r,o={}){return K(o,Vy,"epiSsninfc"),X("epiSsninfc",Ro("noninferiority","continuous",e,t,n,r,o))}function eN(e,t,n,r={}){return K(r,$y,"epiSssupb"),X("epiSssupb",Ro("superiority","binary",e,t,void 0,n,r))}function tN(e,t,n,r,o={}){return K(o,zy,"epiSssupc"),X("epiSssupc",Ro("superiority","continuous",e,t,n,r,o))}function xo(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 xo(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 nN(e,t,n,r={}){K(r,Wy,"powerPaired2x2");let o=r.sigLevel??Dr,s=Te(r.alternative,["two.sided","one.sided"]),i=r.strict??!1,c=r.nullOddsRatio??1,a=r.errbound??1e-6,l=Te(r.tsmethod,["central","minlike","blaker"]);if(Je(e,"pb",qt,"'pb' must be in [0, 1]"),Je(t,"pc",qt,"'pc' must be in [0, 1]"),Je(n,"npairs",y=>Number.isSafeInteger(y)&&y>=0&&y<=1e5,"'npairs' must be a nonnegative safe integer"),Vr(o),Je(c,"nullOddsRatio",xn,"'nullOddsRatio' must be positive and finite"),Je(a,"errbound",y=>Pr(y)&&y>=0,"'errbound' must be nonnegative and finite"),e+t>1)throw new RangeError("probabilities must be finite, non-negative and not all 0");if(s!=="two.sided"&&s!=="one.sided")throw new RangeError("'alternative' should be one of \u201Ctwo.sided\u201D, \u201Cone.sided\u201D");if(!["central","minlike","blaker"].includes(l))throw new RangeError("'tsmethod' should be one of \u201Ccentral\u201D, \u201Cminlike\u201D, \u201Cblaker\u201D");let f=e+t,m=f===0?.5:e/f,u=c/(1+c),h=e>=t?1:-1,d=s==="two.sided"&&i&&l!=="blaker",w=s==="one.sided",g=xo(n,f),b=0,N=n;if(a>0){let y=0;for(;b<=n&&(y+=g[b])<=a/2;)b++;for(y=0;N>=0&&(y+=g[N])<=a/2;)N--}let v=0;for(let y=b;y<=N;y++){let x=g[y];if(x===0)continue;let E=xo(y,m),R=xo(y,u),A=0,S=0,T=new Array(y+1),L=new Array(y+1);for(let C=0;C<=y;C++)A+=R[C],T[C]=A;for(let C=y;C>=0;C--)S+=R[C],L[C]=S;for(let C=0;C<=y;C++){let q=T[C],O=L[C],_,F=u===.5&&o===1;w?_=F||(h>0?O<=o:q<=o):d?_=F||q<=o/2||O<=o/2:_=h>0?F?C>Math.floor(y/2):O<=o/2:F?C<Math.ceil(y/2):q<=o/2,_&&(v+=x*E[C])}}let M=`errbound= ${a===0?"0":a>=.001?String(a):`${a.toExponential(0).replace(/e([+-]?)(\\d+)$/,(y,x,E)=>`e${x||"+"}${E.padStart(2,"0")}`)}`}${e+t===1?" no non-informative pairs: pa+pd=0":""}`,p="Power for McNemar's Exact Test"+(d?", 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 X("powerPaired2x2",{power:v,npairs:n,pb:e,pc:t,sigLevel:o,alternative:s,nullOddsRatio:c,note:M,method:p,warnings:[]})}function rN(e,t,n,r,o,s={}){K(s,Ky,"powerCT");let i=s.alpha??Dr;Vr(i);for(let[l,f]of[[e,"nE"],[t,"nC"]])Je(l,f,xn,`'${f}' must be positive and finite`);for(let[l,f]of[[n,"pE"],[r,"pC"]])Je(l,f,qt,`'${f}' must be in [0, 1]`);Je(o,"RR",xn,"'RR' must be positive and finite");let c=e/t,a=e*n+t*r;return Bn(Math.sqrt(c*a)*Math.abs(o-1)/(c*o+1)-hn(1-i/2))}function oN(e,t,n,r,o,s={}){K(s,jy,"ssizeCT");let i=s.alpha??Dr;Vr(i),Je(e,"power",f=>qt(f)&&f>0&&f<1,"'power' must be strictly between 0 and 1"),Je(t,"k",xn,"'k' must be positive and finite"),Je(n,"pE",qt,"'pE' must be in [0, 1]"),Je(r,"pC",qt,"'pC' must be in [0, 1]"),Je(o,"RR",xn,"'RR' must be positive and finite");let c=hn(1-i/2)+hn(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 sN(e,t,n,r,o,s={}){K(s,By,"powerEpi");let i=s.alpha??Dr;Vr(i),Je(e,"n",xn,"'n' must be positive and finite"),Je(t,"theta",xn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"psi",qt,"'psi' must be in [0, 1]"),Je(o,"rho2",qt,"'rho2' must be in [0, 1]");let c=e*Math.log(t)**2*n*(1-n)*r*(1-o);return Bn(Math.sqrt(c)-hn(1-i/2))}function aN(e,t,n,r,o,s={}){K(s,Uy,"ssizeEpi");let i=s.alpha??Dr;Vr(i),Je(e,"power",qt,"'power' must be in [0, 1]"),Je(t,"theta",xn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"psi",qt,"'psi' must be in [0, 1]"),Je(o,"rho2",qt,"'rho2' must be in [0, 1]");let c=Math.log(t)**2*n*(1-n)*r*(1-o);return Math.ceil((hn(1-i/2)+hn(e))**2/c)}function iN(e,t,n,r,o={}){K(o,Hy,"numDEpi");let s=o.alpha??Dr;Vr(s),Je(e,"power",c=>qt(c)&&c>0&&c<1,"'power' must be strictly between 0 and 1"),Je(t,"theta",xn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"rho2",qt,"'rho2' must be in [0, 1]");let i=Math.log(t)**2*n*(1-n)*(1-r);return Math.ceil((hn(1-s/2)+hn(e))**2/i)}function lp(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 cN(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 fp(e,t,n,r,o,s){let i=0,c=0;c=r.slice(0,e+1).reduce((a,l)=>a+l,0);for(let a=e+1;a<n.length;a++){let l=t-a,f=l<0?1:l+1>=o.length?0:o[l+1],m=l<0?0:s[Math.min(l,s.length-1)];i+=n[a]*f,c+=r[a]*m}return[i,c]}function lN(e,t,n,r,o={}){K(o,Yy,"ph2simon");let s=o.nmax??100;for(let[M,p]of[[e,"pu"],[t,"pa"],[n,"ep1"],[r,"ep2"]])Je(M,p,qt,`'${p}' must be in [0, 1]`);Je(s,"nmax",M=>Number.isInteger(M)&&M>=1&&M<=1e3,"nmax cannot exceed 1000");let i=Array.from({length:s+1},(M,p)=>xo(p,e)),c=Array.from({length:s+1},(M,p)=>xo(p,t)),a=i.map(lp),l=c.map(lp),f=i.map(cN),m=[];for(let M=2;M<=s;M++){let p;for(let y=1;y<M&&(p===void 0||y<p.enP0);y++){let x=M-y,E=i[y],R=c[y],A=l[x],S=a[y];for(let T=0;T<y;T++){let L=S[T],C=y+x*(1-L),q=T,O=M-1,_=-1;for(;q<=O;){let F=Math.floor((q+O)/2),[,I]=fp(T,F,E,R,f[x],A);I<=r?(_=F,q=F+1):O=F-1}if(_>=0){let[F]=fp(T,_,E,R,f[x],A);F>n&&(_=-1)}_>=0&&(p===void 0||C<p.enP0)&&(p={r1:T,n1:y,r:_,n:M,enP0:C,petP0:L})}}p&&m.push(p)}if(m.length===0)throw new Error(` No feasible solution found.
|
|
18
|
+
`,Mo=(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})),us=e=>e[0].map((t,n)=>e.map(r=>r[n])),hs=(e,t=n=>n)=>e[0].map((n,r)=>St(e.map(o=>t(o[r])))),Oh=(e,t=n=>n)=>e.map(n=>St(n.map(t))),Eo=(e,t=n=>n)=>St(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),Fa=e=>e<0?-1:1;function Gc(e){let t=e.map((n,r)=>Math.sqrt(n[r]));return e.map((n,r)=>n.map((o,s)=>r===s?1:o/(t[r]*t[s])))}function Dh(e,t){let n=Gc(e),{d:r,u:o,v:s}=Fs(n),i=r.map((a,l)=>l).filter(a=>r[a]>Math.sqrt(No)*r[0]),c=n.map((a,l)=>{let f=0;for(let m of i)f+=s[l][m]*(1/r[m])*o[l][m];return 1-1/f});return c.some(a=>a>1)&&dr(t,"In smc, smcs > 1 were set to 1.0"),c.some(a=>a<0)&&dr(t,"In smc, smcs < 0 were set to .0"),c.map(a=>a>1?1:a<0?0:a)}var dr=(e,t)=>{e.includes(t)||e.push(t)};function Hc(e,t,n,r){let o=e.map((c,a)=>c.map((l,f)=>a===f?t[a]:l)),s=jt(o,{symmetric:!0}),i=s.values.slice(0,n).map(c=>Math.sqrt(r(c)));return s.vectors.map(c=>c.slice(0,n).map((a,l)=>a*i[l]))}var Xv=e=>Mo(e,us(e));function Fh(e,t,n,r){let o=Gc(e),s=o.length,i=Dh(o,r),c=g=>{let b=Hc(o,g.map(M=>1-M),t,M=>M<No?100*No:M),N=Xv(b),v=[];for(let M=0;M<s;M++)for(let p=M+1;p<s;p++)v.push((o[p][M]-N[p][M])**2);return St(v)},a=g=>{let b=Hc(o,g.map((N,v)=>o[v][v]-N),t,N=>Math.max(N,0));return g.map((N,v)=>{let M=0;for(let p=0;p<t;p++)M+=b[v][p]*b[v][p];return M+N-o[v][v]})},l=g=>{let b=jt(o.map((A,S)=>A.map((T,L)=>S===L?1-g[S]:T)),{symmetric:!0}),N=b.values,v=b.vectors,M=N.map(A=>A>=No),p=N.map(A=>A<No?100*No:A),y=o.map((A,S)=>A.map((T,L)=>{if(S===L)return 0;let C=0;for(let q=0;q<t;q++)C+=p[q]*v[S][q]*v[L][q];return T-C})),x=Mo(y,v),E=(A,S)=>{let T=0;for(let L=0;L<s;L++)T+=v[L][A]*x[L][S];return T},R=new Array(s).fill(0);for(let A=0;A<t;A++){let S=(T,L)=>{if(L===0)return;let C=L*E(A,T);for(let q=0;q<s;q++)R[q]+=C*v[q][A]*v[q][T]};M[A]&&S(A,1);for(let T=A+1;T<t;T++)S(T,2*(M[A]&&M[T]?1:(p[A]-p[T])/(N[A]-N[T])));for(let T=t;T<s;T++)S(T,2*p[A]/(N[A]-N[T]))}return R},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(gt(i),1)},u=new Array(s).fill(.01),h=rt(()=>Fo(i.map(g=>1-g),c,{gr:a,...m,control:{fnscale:1,parscale:u}}));h.par.some(g=>g===f)&&(h=rt(()=>Fo(h.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&dr(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let d=h.par,w=Hc(o,d.map((g,b)=>o[b][b]-g),t,g=>Math.max(g,0));if(w=Yc(w,t),w=w.map(g=>g.map(b=>b===0?1e-15:b)),Oh(w,g=>g*g).some(g=>g>1)&&dr(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){w=Yc(Ca(w).loadings,t);let g=hs(w,N=>N*N),b=g.map((N,v)=>v).sort((N,v)=>g[v]-g[N]||N-v);w=w.map(N=>b.map(v=>N[v]))}else w=Yc(w,t);return w}function Yc(e,t){let n=hs(e).map(Fa);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 Qv(e){let t=e[0].length,n=e.map(o=>o.map(s=>s*s)),r=n.map(o=>o.map((s,i)=>{let c=0;for(let a=0;a<t;a++)a!==i&&(c+=o[a]);return c}));return{f:Eo(n.map((o,s)=>o.map((i,c)=>i*r[s][c])))/4,Gq:e.map((o,s)=>o.map((i,c)=>i*r[s][c]))}}function Zv(e){let t=e[0].length,n=l=>{let f=nt(l),m=Mo(e,us(f)),{f:u,Gq:h}=Qv(m),d=us(Mo(Mo(us(m),h),f)).map(w=>w.map(g=>-g));return{L:m,f:u,G:d}},r=Array.from({length:t},(l,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),o=1,s=n(r),i,c=[],a=!1;for(let l=0;l<=2e3;l++){let f=hs(r.map((g,b)=>g.map((N,v)=>N*s.G[b][v]))),m=s.G.map((g,b)=>g.map((N,v)=>N-r[b][v]*f[v])),u=Math.sqrt(Eo(m,g=>g*g));if(c.push(s.f),u<1e-5){a=!0;break}if(i!==void 0){let g=i,b=r.map((v,M)=>v.map((p,y)=>p-g.T[M][y])),N=m.map((v,M)=>v.map((p,y)=>p-g.Gp[M][y]));Eo(N,v=>v*v)>0&&(o=Eo(b,v=>v*v)/Math.abs(Eo(b.map((v,M)=>v.map((p,y)=>p*N[M][y])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=gt(c.slice(-10)),d=s,w=r;for(let g=0;g<=10;g++){let b=r.map((v,M)=>v.map((p,y)=>p-o*m[M][y])),N=hs(b,v=>v*v).map(v=>1/Math.sqrt(v));if(w=b.map(v=>v.map((M,p)=>M*N[p])),d=n(w),h-d.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=w,s=d}return{loadings:s.L,phi:Mo(us(r),r),converged:a}}function Jv(e,t={}){K(t,Yv,"omega");let n=t.nfactors??3;if(!Number.isInteger(n)||n<1)throw new RangeError("'nfactors' must be a positive integer");let r;if(t.covmat!==void 0)La(t.covmat),r=Gc(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Co(e,void 0,{use:"pairwise.complete.obs"})}let o=r.length;if(o<2)throw new RangeError(`omega needs at least 2 variables (got ${o})`);if(n>o)throw new RangeError(`'nfactors' (${n}) must not exceed the number of variables (${o})`);let s=[],i=[],c=Fh(r,n,s,i),a,l,f;if(n===1)a=c.map(k=>k.slice()),f=[1],dr(i,`Omega_h for 1 factor is not meaningful, just omega_t
|
|
19
|
+
`),dr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let k=Zv(c);if(k.converged||dr(s,"oblimin did not converge in 2000 iterations"),a=k.loadings,l=k.phi,n===2){let V=l[0][1],B=Math.sqrt(Math.abs(V));f=[B,Fa(V)*B],dr(i,Gv)}else f=Fh(l,1,s,i).map(V=>V[0])}let m=a.map(k=>{let V=0;for(let B=0;B<n;B++)V+=k[B]*f[B];return V}),u=Oh(c,k=>k*k),h=u.map((k,V)=>r[V][V]-k),d=a.map((k,V)=>k.map((B,Q)=>{if(n===1)return 0;let re=r[V][V],z=re-(re-B*B)-(B*f[Q])**2;return z<0&&(z=0),Fa(B)*Math.sqrt(z)})),w=m.map((k,V)=>k*k/u[V]),g=m.map((k,V)=>{let B=[k,...d[V]];return St(B.map(Q=>Q*Q))**2/St(B.map(Q=>Q**4))}),b=m.map(Fa);b.some(k=>k<0)&&(r=r.map((k,V)=>k.map((B,Q)=>b[V]*B*b[Q])));let N=m.map((k,V)=>b[V]*k),v=d.map((k,V)=>k.map(B=>b[V]*B)),M=N.map((k,V)=>[k,...v[V],u[V],h[V],w[V],g[V]]),p=Eo(r),y=St(r.map((k,V)=>k[V])),x=St(N)**2,E=St(h),R=x/p,A=(p-E)/p,S=x/(p-E),T=(p-y)/p*o/(o-1),L=(p+St(Dh(r,i))-y)/p,C=hs(M.map(k=>k.slice(0,n+1)),k=>k*k),q=C[0]/St(C),F=v.map(k=>{let V=0;for(let B=1;B<n;B++)Math.abs(k[B])>Math.abs(k[V])&&(V=B);return V}),_=[],O=[];for(let k=0;k<n;k++){let V=F.map((z,J)=>J).filter(z=>F[z]===k),B=St(V.flatMap(z=>V.map(J=>r[J][z]))),Q=St(V.map(z=>v[z][k])),re=St(V.map(z=>N[z]));O.push(Q*Q),_.push([(Q*Q+re*re)/B,re*re/B,Q*Q/B])}let I=[[A,St(N)**2/p,St(O)/p],..._],D={omegaH:R,omegaLim:S,alpha:T,omegaTot:A,G6:L,ECV:q,sl:M,slNames:["g",...Array.from({length:n},(k,V)=>`F${V+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:b,omegaGroup:I,warnings:s,messages:i};return l!==void 0&&(D.phi=l),X("omega",D)}var Ph=["ML","stdErr"],ey=["ML","stdErr"],rn=.9999;function Qc(e){let t=Pe(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(le(r/t));return n}var Zc=e=>[...new Set(e)].sort((t,n)=>t-n),Vh=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,$h=e=>Array.isArray(e)||ArrayBuffer.isView(e),zh=(e,t)=>e>0?se(e,{lowerTail:!1})-se(t,{lowerTail:!1}):se(t)-se(e);function Jc(e){let t=Se([e]).groups[0],n=it(t),r=t.map(s=>s-n),o=Math.sqrt(st(r));return r.map(s=>s/o)}function Wh(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 Oa=e=>Array.from({length:e},()=>new Array(e).fill(0));function Bh(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 Xc={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function ty(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Xc;if(e===1/0)return{...Xc,k:Gt(t),kk:-t*Gt(t)};if(t===1/0)return{...Xc,h:Gt(e),hh:-e*Gt(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),s=e*e-2*n*e*t+t*t,i=Math.exp(-s/(2*r))/(2*Math.PI*o),c=(t-n*e)/o,a=(e-n*t)/o,l=Gt(e),f=Gt(t);return{r:i,h:l*se(c),k:f*se(a),rr:i*(n/r+(e*t*r-n*s)/(r*r)),rh:-i*(e-n*t)/r,rk:-i*(t-n*e)/r,hh:-e*l*se(c)-n/o*l*Gt(c),hk:i,kk:-t*f*se(a)-n/o*f*Gt(a)}}function jh(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:Oa(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(d=>ty(h,d,r))),u=[];for(let h=0;h<s;h++)for(let d=0;d<i;d++){let w=e[h][d];if(w===0)continue;let g=ko([t[h],n[d]],[t[h+1],n[d+1]],r),b=new Array(c).fill(0),N=Oa(c);for(let[v,M,p]of[[h+1,d+1,1],[h,d+1,-1],[h+1,d,-1],[h,d,1]]){let y=m[v][M],x=l(v),E=f(M);b[0]+=p*y.r,N[0][0]+=p*y.rr,x>=0&&(b[x]+=p*y.h,N[0][x]+=p*y.rh,N[x][0]+=p*y.rh,N[x][x]+=p*y.hh),E>=0&&(b[E]+=p*y.k,N[0][E]+=p*y.rk,N[E][0]+=p*y.rk,N[E][E]+=p*y.kk),x>=0&&E>=0&&(N[x][E]+=p*y.hk,N[E][x]+=p*y.hk)}Bh(a,w,g,b,N),u.push(w*Math.log(g))}return a.value=Pe(u),a}function ny(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:Oa(s)},c=(1-r)*(1+r),a=Math.sqrt(c),l=c*a,f=new Array(s),m=Oa(s),u=[],h=(d,w,g)=>{let b=n[d],N=(b-r*w)/a,v=Gt(N),M=(r*b-w)/l,p=1/a,y=b/l+3*r*(r*b-w)/(l*c),x=r/l;if(f[0]+=g*v*M,m[0][0]+=g*v*(y-N*M*M),o){let E=d+1;f[E]+=g*v*p;let R=g*v*(x-N*M*p);m[0][E]+=R,m[E][0]+=R,m[E][E]+=g*v*(-N*p*p)}};for(let d=0;d<e.length;d++){let w=e[d],g=t[d];f.fill(0);for(let v of m)v.fill(0);let b=g<n.length?(n[g]-r*w)/a:1/0,N=g>0?(n[g-1]-r*w)/a:-1/0;g<n.length&&h(g,w,1),g>0&&h(g-1,w,-1),Bh(i,1,zh(N,b),f,m),u.push(Math.log(zh(N,b)),Gt(w,{log:!0}))}return i.value=Pe(u),i}function ry(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(i=>e.gradient[i]),o=t.map(i=>t.map(c=>-e.hessian[i][c])),s;try{s=nt(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||Pe(s.map((i,c)=>i*r[c]))<=0)&&(s=r.map((i,c)=>i/Math.max(Math.abs(o[c][c]),1))),t.forEach((i,c)=>n[i]=s[c]),n}var Uh=(e,t)=>e>=t-1e-12*Math.abs(t);function Kh(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=ry(o,a);if(!s&&Math.abs(r[0])===rn&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>rn?(Math.sign(l[0])*rn-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((w,g)=>w+f*l[g]);f*l[0]!==0&&Math.abs(h[0])>=rn*(1-1e-15)&&(h[0]=Math.sign(h[0])*rn);let d=e(h);if(Uh(d.value,o.value)){let w=gt(h.map((g,b)=>Math.abs(g-r[b])/Math.max(1,Math.abs(r[b]))));r=h,o=d,m=w>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function Hh(e,t){let n=Kh(e,[Math.max(-rn,Math.min(rn,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Kh(e,[Math.sign(n.theta[0])*rn,...n.theta.slice(1)],!0);return Uh(r.at.value,n.at.value)?r:n}function Yh(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=nt(e.map(o=>o.map(s=>-s)))}catch{return n()}return r.map((o,s)=>o.map((i,c)=>(i+r[c][s])/2))}var Gh=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function oy(e,t,n){vt(t)&&K(t,Ph,"polychor"),K(n,Ph,"polychor");let r,o;if($h(t)){let _=e,O=t;if(_.length!==O.length)throw new RangeError("'x' and 'y' must have the same length");let I=_.map((Q,re)=>!Number.isNaN(Q)&&!Number.isNaN(O[re])),D=kt(_.filter((Q,re)=>I[re])),k=kt(O.filter((Q,re)=>I[re])),V=new Map(Zc(D).map((Q,re)=>[Q,re])),B=new Map(Zc(k).map((Q,re)=>[Q,re]));r=Array.from({length:V.size},()=>new Array(B.size).fill(0)),D.forEach((Q,re)=>r[V.get(Q)][B.get(k[re])]++),o=n??{}}else{let _=e;if(!_.every($h))throw new TypeError("give a table (array of rows) or two vectors");let O=_[0]?.length??0;if(_.some(I=>I.length!==O))throw new TypeError("the table must have rows of equal length");if(r=_.map(I=>Array.from(I)),r.some(I=>I.some(D=>!(D>=0&&D<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:i=!1}=o,c=[],a=0;Pe(r.map(_=>Pe(_)))<2**1e3||({groups:r,k:a}=Se(r));let l=r.map(_=>Pe(_)),f=l.filter(_=>_===0).length;f>0&&(c.push(Vh(f,"row")),r=r.filter((_,O)=>l[O]!==0));let m=(r[0]??[]).map((_,O)=>Pe(r.map(I=>I[O]))),u=m.filter(_=>_===0).length;u>0&&(c.push(Vh(u,"column")),r=r.map(_=>_.filter((O,I)=>m[I]!==0)));let h=r.length,d=r[0]?.length??0;if(h<2&&c.push("the table has fewer than 2 rows"),d<2&&c.push("the table has fewer than 2 columns"),h<2||d<2)return X("polychor",{rho:NaN,warnings:c});let w=[-1/0,...Qc(r.map(_=>Pe(_))),1/0],g=[-1/0,...Qc(r[0].map((_,O)=>Pe(r.map(I=>I[O])))),1/0],N=$l(_=>{let O=Math.max(-rn,Math.min(rn,_)),I=[];for(let D=0;D<h;D++)for(let k=0;k<d;k++){if(r[D][k]===0)continue;let V=ko([w[D],g[k]],[w[D+1],g[k+1]],O);I.push(r[D][k]*Math.log(V))}return-Pe(I)},-rn,rn).optimum;if(!s&&!i)return X("polychor",{rho:N,warnings:c});let v=_=>[-1/0,..._,1/0],M=w.slice(1,h),p=g.slice(1,d),y=_=>s?jh(r,v(_.slice(1,h)),v(_.slice(h)),_[0],!0):jh(r,w,g,_[0],!1),{theta:x,at:E,bounded:R}=Hh(y,s?[N,...M,...p]:[N]),A=x[0];if(R&&c.push(Gh(A)),!i)return X("polychor",{rho:A,warnings:c});let S=s?v(x.slice(1,h)):w,T=s?v(x.slice(h)):g,L=Pe(r.map(_=>Pe(_))),C=[],q=[];for(let _=0;_<h;_++)for(let O=0;O<d;O++){let I=r[_][O],D=ko([S[_],T[O]],[S[_+1],T[O+1]],A);if(I===0)C.push(L*D);else if(a===0)C.push(I*Math.log1p(1e-6/I),I*Wh(L*D/I-1));else{let k=P(I,a);q.push(k*Math.log1p(1e-6/k)),C.push(I*Wh(L*D/I-1))}}let F=Yh(E.hessian,R);return X("polychor",{rho:A,warnings:c,var:a===0?F:F.map(_=>_.map(O=>P(O,-a))),chisq:a===0?2*Pe(C):P(2*Pe(C),a)+2*Pe(q),df:h*d-h-d,n:P(L,a),rowCuts:S.slice(1,h),colCuts:T.slice(1,d)})}function sy(e,t,n,r){let o=e.length,s=[le(.25),le(.5),le(.75)],i=(h,d)=>d.filter(w=>w<h).length,c=Jc(e).map(h=>i(h,n)),a=Jc(t).map(h=>i(h,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));c.forEach((h,d)=>l[h][a[d]]++);let f=[-1/0,...n,1/0],m=[-1/0,...s,1/0],u=[];return l.forEach((h,d)=>h.forEach((w,g)=>{if(w===0)return;let b=ko([f[d],m[g]],[f[d+1],m[g+1]],r);u.push(w*Math.log((w+1e-6)/(b*o)))})),2*Pe(u)}function ay(e,t,n={}){if(K(n,ey,"polyserial"),e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,s=e.map((E,R)=>!Number.isNaN(E)&&!Number.isNaN(t[R])),i=e.filter((E,R)=>s[R]),c=kt(t.filter((E,R)=>s[R])),a=i.length,l=Zc(c);if(l.length<2)return X("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((E,R)=>[E,R+1])),m=c.map(E=>f.get(E)),u=l.map(()=>0);for(let E of m)u[E-1]++;let h=Qc(u),d=[],w=Math.sqrt((a-1)/a)*Math.sqrt(st(m))*Wl(i,m)[0]/Pe(h.map(E=>Gt(E)));if(Math.abs(w)>rn){let E=Math.sign(w)*rn;d.push(`initial correlation inadmissible, ${$n(w)}, set to ${$n(E)}`),w=E}if(!r&&!o)return X("polyserial",{rho:w,warnings:d});let g=Jc(i),b=m.map(E=>E-1),N=E=>ny(g,b,r?E.slice(1):h,E[0],r),{theta:v,at:M,bounded:p}=Hh(N,r?[w,...h]:[w]);if(p&&d.push(Gh(v[0])),!o)return X("polyserial",{rho:v[0],warnings:d});let y=r?v.slice(1):h,x=l.length;return X("polyserial",{rho:v[0],warnings:d,var:Yh(M.hessian,p),chisq:sy(m,g,y,r?v[0]:w),df:4*x-x-1,n:a,cuts:y})}var iy=["group","strata","rho","cencode"],cy=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function Da(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function el(e){let t=vn(e),n=Array.from(new Set(t)).sort(Ar);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function ly(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 fy(e,t,n={}){K(n,iy,"cuminc"),ly(e,t,n);let{group:r,strata:o,rho:s=0,cencode:i=0}=n,c=[],a=new mt;for(let p=0;p<e.length;p++)if(!(Number.isNaN(e[p])||Da(t[p])||r!==void 0&&Da(r[p])||o!==void 0&&Da(o[p]))){if(!Number.isFinite(e[p]))throw new RangeError("'time' must contain only finite values");if(e[p]<0)throw new RangeError("'time' must be non-negative");c.push(p)}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]:vn(c.map(p=>r[p])),m=o===void 0?[1]:vn(c.map(p=>o[p])),u=vn([...c.map(p=>t[p]),i]),h=el(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let d=el(m),w=el(u),g=u[u.length-1],b=w.labels.filter(p=>p!==g);if(b.length===0)throw new Error("there are no events of any cause");let N=c.map((p,y)=>({time:e[p],status:u[y],group:r===void 0?0:h.index.get(f[y]),strata:o===void 0?0:d.index.get(m[y]),source:p})).sort((p,y)=>p.time-y.time),v=[];for(let p of b){let y=ut(p);for(let x=0;x<h.labels.length;x++){let E=N.filter(I=>I.group===x),R=E.reduce((I,D)=>Math.max(I,D.time),-1/0),A=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,S=ur(E.map(I=>I.time*A),E.map(I=>I.status===g?0:1)),T=[0],L=[0],C=[0],q=[],F=1;for(let I=0;I<S.time.length;I++){let D=S.nEvent[I];if(D===0)continue;let k=S.time[I]/A,V=S.nRisk[I],B=E.filter(Q=>Q.time===k&&Q.status===p).length;q.push({t:k,Y:V,d:D,dk:B,sb:F,sa:S.surv[I]}),F=S.surv[I]}let _=0,O=0;for(let I=0;I<q.length;I++){let D=q[I],k=_,V=O;_+=D.sb*D.dk/D.Y;let B=0;for(let Q=0;Q<=I;Q++){let re=q[Q],z=re.Y,J=re.d,Z=re.dk,ke=z===J?0:(J*(z-J)+2*Z*(J-Z))/((z-1)*(z-J)**2),ne=z===1?Z:Z*(z-Z)/(z*z*(z-1)),$=z===J||z===1?0:Z*(z-Z)/(z*(z-J)*(z-1)),Y=0;for(let U=0;U<=Q;U++){let j=q[U];Y+=j.sb*j.dk/j.Y}let W=_-Y;B+=W*W*ke+re.sb*re.sb*ne-2*W*re.sb*$}O=Math.max(0,B),D.dk>0&&(T.push(D.t,D.t),L.push(k,_),C.push(V,O))}T.push(R),L.push(_),C.push(O),v.push({name:`${ut(h.labels[x])} ${y}`,group:ut(h.labels[x]),cause:y,time:T,est:L,var:C})}}let M=h.labels.length>=2?my(N,h.labels,b,d.labels.length,s,g):void 0;return X("cuminc",{curves:v,...M===void 0?{}:{tests:M},warnings:a.list})}function my(e,t,n,r,o,s){let i=[];for(let c of n){let a=t.length,l=new Array(a).fill(0),f=Array.from({length:a},()=>new Array(a).fill(0));for(let w=0;w<r;w++){let g=e.filter(p=>p.strata===w),b=Array.from(new Set(g.filter(p=>p.status!==s).map(p=>p.time))).sort((p,y)=>p-y),N=new Array(a).fill(1),v=new Array(a).fill(0),M=[];for(let p of b){let y=new Array(a).fill(0),x=new Array(a).fill(0),E=new Array(a).fill(0);for(let I of g)I.time>=p&&y[I.group]++;for(let I of g)I.time===p&&I.status!==s&&(I.status===c?x[I.group]++:E[I.group]++);let R=N.slice(),A=v.slice(),S=y.map((I,D)=>I===0?0:I/R[D]),T=y.map((I,D)=>I===0?0:I*(1-A[D])/R[D]),L=x.reduce((I,D)=>I+D,0),C=S.reduce((I,D)=>I+D,0),q=C===0?0:L/C,F=M.length===0?1:M[M.length-1].Gafter,_=F-q,O=T.reduce((I,D)=>I+D,0);for(let I=0;I<a;I++){let D=F**o;O>0&&(l[I]+=D*(x[I]-T[I]*L/O)),y[I]>0?(N[I]=N[I]*(1-(x[I]+E[I])/y[I]),v[I]+=R[I]*x[I]/y[I]):N[I]=0}M.push({time:p,Y:y,d1:x,d2:E,Sbefore:R,Safter:N.slice(),Fbefore:A,h:S,R:T,dF0:q,Gbefore:F,Gafter:_})}for(let p=0;p<a;p++)for(let y=0;y<a;y++){let x=0;for(let E=0;E<a;E++)for(let R=0;R<M.length;R++){let A=M[R],S=A.h.reduce((J,Z)=>J+Z,0);if(!(S>0)||!(A.h[E]>0))continue;let T=A.Gbefore**o,L=T*A.h[p]*((p===E?1:0)-A.h[E]/S),C=T*A.h[y]*((y===E?1:0)-A.h[E]/S),q=0,F=0;for(let J=R+1;J<M.length;J++){let Z=M[J],ke=Z.h.reduce((U,j)=>U+j,0);if(!(ke>0))continue;let ne=Z.Gbefore**o,$=ne*Z.h[p]*((p===E?1:0)-Z.h[E]/ke),Y=ne*Z.h[y]*((y===E?1:0)-Z.h[E]/ke),W=Z.Gbefore===0?0:Z.dF0/Z.Gbefore;q+=$*W,F+=Y*W}let _=A.Safter[E]===0?0:A.Gafter/A.Safter[E],O=L+(1-_)*q,I=C+(1-_)*F,D=-_*q,k=-_*F,V=A.Y[E]===0?0:A.Sbefore[E]*A.d2[E]/A.Y[E],B=(O*I*A.dF0+D*k*V)/A.h[E],Q=A.d1.reduce((J,Z)=>J+Z,0),re=0;if(Q>1&&A.Y[E]>0&&A.Sbefore[E]>0){let J=1/A.Sbefore[E];S>J&&(re=Q*(Q-1)/S*(A.Y[E]*J*J/(S-J))*(O*I/(A.h[E]*A.h[E])))}let z=0;A.d2[E]>1&&A.Y[E]>1&&(z=(A.d2[E]-1)/(A.Y[E]-1)*(V/A.h[E])*D*k),x+=B-re-z}f[p][y]+=x}}let m=a-1,u=NaN,h=NaN,d=f.slice(0,m).map(w=>w.slice(0,m));try{let w=nt(d,l.slice(0,m),{tol:1e-12});u=l.slice(0,m).reduce((g,b,N)=>g+b*w[N],0),h=_t(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}i.push({cause:ut(c),stat:u,df:m,pValue:h})}return i}function uy(e,t){let n=Array.from(new Set(t.filter(s=>!Number.isNaN(s)))).sort((s,i)=>s-i),r=[],o=[];for(let s of e.curves){let i=[],c=[];for(let a of n){let l=-1;for(let f=0;f<s.time.length;f++)s.time[f]<=a&&(l=f);a<0||l<0||a>s.time[s.time.length-1]?(i.push(NaN),c.push(NaN)):(i.push(s.est[l]),c.push(s.var[l]))}r.push(i),o.push(c)}return{curveNames:e.curves.map(s=>s.name),times:n,est:r,var:o}}function Ln(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function hy(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 tl(e,t){return Pe(e.map((n,r)=>n*t[r]))}function py(e){return e.every(t=>t.every(Number.isFinite))}function dy(e,t,n,r={}){K(r,cy,"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 d=n?.[0]?.length??0,w=o?.[0]?.length??0,g=d+w;if(g===0)throw new RangeError("at least one covariate is required");if(n!==void 0&&n.some(j=>j.length!==d))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(j=>j.length!==w))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 b=[];for(let j=0;j<e.length;j++)if(!(Number.isNaN(e[j])||Da(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");b.push(j)}let N=e.length-b.length;if(b.length===0)throw new RangeError("no complete observations");let v=b.map(j=>e[j]),M=vn([...b.map(j=>t[j]),c,i]),p=M.slice(0,b.length),y=M[M.length-2],x=M[M.length-1],E=b.map(j=>n===void 0?[]:Array.from(n[j])),R=b.map(j=>o===void 0?[]:Array.from(o[j])),A=Array.from(new Set(v.filter((j,te)=>p[te]===x))).sort((j,te)=>j-te);if(A.length===0)throw new RangeError("no observations of 'failcode'");let S=w===0?[[0]]:s(A).map(j=>Array.from(j));if(w>0&&(S.length!==A.length||S.some(j=>j.length!==w)||!py(S)))throw new RangeError("'tf' must return a finite matrix with one row per unique failure time and one column per cov2 column");let T=A.map((j,te)=>v.map((fe,Re)=>[...E[Re],...R[Re].map((Ce,be)=>Ce*(S[te][be]??0))])),L=v.reduce((j,te)=>Math.max(j,Math.abs(te)),0),C=L>0&&L<1?2**Math.min(1023,Math.ceil(-Math.log2(L))):1,q=ur(v.map(j=>j*C),p.map(j=>j===y?1:0)),F=j=>{let te=j*C,fe=1;for(let Re=0;Re<q.time.length&&!(q.time[Re]>=te);Re++)fe=q.surv[Re];return fe},_=A.map(j=>v.map((te,fe)=>{if(te>=j)return 1;if(p[fe]===y||p[fe]===x)return 0;let Re=F(te);return Re===0?0:F(j)/Re}));for(let j=0;j<A.length;j++)for(let te=0;te<v.length;te++)v[te]===A[j]&&p[te]===x&&(_[j][te]=1);let O=j=>{let te=Array.from({length:g},()=>[]),fe=Array.from({length:g},()=>Array.from({length:g},()=>[])),Re=[],Ce=Ln(A.length,g),be=[],me=[],qe=[];for(let Oe=0;Oe<A.length;Oe++){let Be=A[Oe],ze=v.map((ie,Ae)=>ie===Be&&p[Ae]===x?Ae:-1).filter(ie=>ie>=0),et=ze.length,Ue=T[Oe],Xe=Ue.map(ie=>tl(j,ie)),pe=_[Oe].slice();for(let ie of ze)pe[ie]=1;let oe=Math.max(...Xe.map((ie,Ae)=>pe[Ae]>0?ie:-1/0));if(oe===-1/0)throw new Error("empty Fine-Gray risk set");let we=oe<500&&oe>-500,ge=we?Xe.map((ie,Ae)=>pe[Ae]*Math.exp(ie)):Xe.map((ie,Ae)=>pe[Ae]*Math.exp(ie-oe)),ce=Pe(ge),tt=Array.from({length:g},(ie,Ae)=>Pe(ge.map((Ke,$e)=>Ke*Ue[$e][Ae])));if(!(ce>0))throw new Error("empty Fine-Gray risk set");let G=tt.map(ie=>ie/ce),Me=Array.from({length:g},(ie,Ae)=>{let Ke=Ue[0][Ae],$e=0,ot=0;for(let ht of Ue)$e=Math.max($e,Math.abs(ht[Ae]-Ke)),ot=Math.max(ot,Math.abs(ht[Ae]));return $e>0&&ot/$e>1e6}).some(Boolean),ye=Me?Array.from({length:g},(ie,Ae)=>Ue.map(Ke=>Ke[Ae]-Ue[0][Ae])):[],Ee=Me?ye.map(ie=>Pe(ie.map((Ae,Ke)=>ge[Ke]*Ae))/ce):[],ee=Me?void 0:Array.from({length:g},(ie,Ae)=>Array.from({length:g},(Ke,$e)=>Pe(ge.map((ot,ht)=>ot*Ue[ht][Ae]*Ue[ht][$e]))));me.push(G),qe.push(ge.map(ie=>ie/ce));let ve=we?et/ce:et*Math.exp(-oe)/ce;be.push(ve),Re.push(Pe(ze.map(ie=>tl(j,Ue[ie])))-et*(we?Math.log(ce):oe+Math.log(ce)));for(let ie=0;ie<g;ie++){let Ke=Pe(ze.map($e=>Ue[$e][ie]))-et*G[ie];te[ie].push(Ke),Ce[Oe][ie]=Ke}for(let ie=0;ie<g;ie++)for(let Ae=0;Ae<g;Ae++){let Ke=Me?Pe(ge.map(($e,ot)=>$e*(ye[ie][ot]-Ee[ie])*(ye[Ae][ot]-Ee[Ae])))/ce:ee[ie][Ae]/ce-G[ie]*G[Ae];fe[ie][Ae].push(et*Ke)}}let de=Ln(v.length,g);for(let Oe=0;Oe<A.length;Oe++){let Be=T[Oe],ze=qe[Oe],et=me[Oe],Ue=A[Oe];for(let Xe=0;Xe<v.length;Xe++){let pe=v[Xe]===Ue&&p[Xe]===x?1:0,oe=v.filter((ge,ce)=>ge===Ue&&p[ce]===x).length,we=pe-ze[Xe]*oe;for(let ge=0;ge<g;ge++)de[Xe][ge]+=(Be[Xe][ge]-et[ge])*we}}let Fe=te.map(Pe),xe=fe.map(Oe=>Oe.map(Pe));return{loglik:Pe(Re),score:Fe,inf:xe,bfitj:be,res:Ce,eta:de,means:me,riskFactors:qe,zByTime:T}},I=f===void 0?new Array(g).fill(0):Array.from(f);if(!I.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let D=I.slice(),k=O(D),V=!1,B=0,Q=(j,te)=>Math.max(...j.score.map((fe,Re)=>Math.abs(fe)*Math.max(Math.abs(te[Re]),1)))<a*Math.max(Math.abs(j.loglik),1);for(;;){if(Q(k,D)){V=!0;break}if(B>=l)break;let j=nt(k.inf,k.score,{tol:0});D=D.map((te,fe)=>te+j[fe]),B++,k=O(D)}let re=nt(k.inf),z=Ln(g);if(m){let j=Ln(g),te=v.length,fe=Array.from(new Set(v)).sort((me,qe)=>me-qe),Re=fe.map(me=>{let qe=v.filter(Fe=>Fe>=me).length,de=v.filter((Fe,xe)=>Fe===me&&p[xe]===y).length;return qe===0?0:de/qe}),Ce=Ln(te,g);for(let me=0;me<fe.length;me++){let qe=fe[me],de=new Array(g).fill(0);for(let xe=0;xe<te;xe++)if(!(!(v[xe]<qe)||p[xe]===y||p[xe]===x))for(let Ve=0;Ve<A.length;Ve++){if(A[Ve]<qe)continue;let Oe=k.riskFactors[Ve][xe],Be=v.filter((Ue,Xe)=>Ue===A[Ve]&&p[Xe]===x).length,ze=k.zByTime[Ve][xe],et=k.means[Ve];for(let Ue=0;Ue<g;Ue++)de[Ue]+=(ze[Ue]-et[Ue])*Oe*Be}let Fe=v.filter(xe=>xe>=qe).length;if(Fe!==0)for(let xe=0;xe<te;xe++){let Ve=v[xe]===qe&&p[xe]===y?1:0,Oe=v[xe]>=qe?Re[me]:0;for(let Be=0;Be<g;Be++)Ce[xe][Be]+=de[Be]/Fe*(Ve-Oe)}}for(let me=0;me<te;me++)hy(j,k.eta[me].map((qe,de)=>qe+Ce[me][de]),k.eta[me].map((qe,de)=>qe+Ce[me][de]),1);z=Ln(g);let be=Xh(re,j);z=Xh(be,re)}else z=Ln(g).map(j=>j.map(()=>NaN));let J=m?z.map((j,te)=>Math.sqrt(j[te])):void 0,Z=J===void 0?void 0:D.map((j,te)=>j/J[te]),ke=Z===void 0?void 0:Z.map(j=>2*se(-Math.abs(j))),ne=m?le((1+u)/2):void 0,$=ne===void 0?void 0:D.map((j,te)=>Math.exp(j-ne*J[te])),Y=ne===void 0?void 0:D.map((j,te)=>Math.exp(j+ne*J[te])),W=m?D.map(Math.exp):void 0,U=h===void 0?[...Array.from({length:d},(j,te)=>`cov1${te+1}`),...Array.from({length:w},(j,te)=>`cov2${te+1}*tf${te+1}`)]:Array.from(h);return X("crr",{coefficients:D,names:U,var:z,...J===void 0?{}:{se:J,z:Z,pValue:ke,lower:$,upper:Y,expCoef:W},loglik:k.loglik,loglikNull:O(new Array(g).fill(0)).loglik,logtest:{statistic:2*(k.loglik-O(new Array(g).fill(0)).loglik),df:g},score:k.score,inf:m?k.inf:Ln(g).map(j=>j.map(()=>NaN)),invinf:m?re:Ln(g).map(j=>j.map(()=>NaN)),converged:V,iterations:B,n:b.length,nMissing:N,uftime:A,bfitj:k.bfitj,res:m?k.res:null,tfs:S,warnings:[],covariateDimensions:[d,w]})}function Xh(e,t){let n=Ln(e.length,t[0]?.length??0);for(let r=0;r<e.length;r++)for(let o=0;o<(t[0]?.length??0);o++)for(let s=0;s<t.length;s++)n[r][o]+=e[r][s]*t[s][o];return n}function gy(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 d=[...r===0?[]:t[m],...o===0?[]:n[m].map((v,M)=>v*i[h][M])],w=e.bfitj[h],g=tl(e.coefficients,d),b=w*Math.exp(g),N=Number.isFinite(b)&&(b!==0||w===0)?b:w===0?0:Math.exp(Math.log(w)+g);u+=N}f.push(-Math.expm1(-u))}return f})}var wy=new Set(["type","time2","dist","weights","offset","strata","scale","init","df","control","intercept","confLevel"]),by=["maxiter","relTolerance","tolerChol"],vy=["type","p","newX","newStrata","newOffset","seFit"],yy=["type","weighted"],rl=new WeakMap,Ny=Math.log(2*Math.PI),My=e=>Number(e),Ey=e=>e<-Math.LN2?Math.log1p(-Math.exp(e)):Math.log(-Math.expm1(e));function rp(e,t){return t===-1/0?e:e<t?NaN:e+Ey(t-e)}function xy(e,t){if(e===t)return 0;let n=Math.max(e,t),r=Math.min(e,t),o=n+Math.log(-Math.expm1(r-n));return(e>t?1:-1)*Math.exp(o)}function Qh(e,t){return t===0?0:Number.isFinite(t)?Math.sign(t)*Math.exp(e+Math.log(Math.abs(t))):Math.exp(e)*t}function Ry(e,t,n){let r=t-e;return Number.isFinite(r)?r/n:t/n-e/n}function op(e,t,n){let r=t-e;if(Number.isFinite(r))return Math.log(r)-Math.log(n);let o=Math.max(Math.abs(e),Math.abs(t));return Math.log(o)+Math.log(t/o-e/o)-Math.log(n)}function Ay(e,t,n){let r=op(e,t,n),o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(o<1e-8){let s=t-e;return tn(e,t)+s*(o/24)}return o>50?t-n*r-n*Math.log1p(-Math.exp(-o)):e-n*(r-Math.log(Math.expm1(o)))}function Sy(e,t,n){let r=op(e,t,n);if(r<Math.log(1e-8))return r-1;let o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(!Number.isFinite(o))return 0;let s=o>50?o+Math.log1p(-Math.exp(-o)):Math.log(Math.expm1(o)),i=r-s,c=o>50?r-Math.log1p(-Math.exp(-o)):i+o;return rp(-Math.exp(i),-Math.exp(c))}function Ty(e){return e<-36?e:-Math.log1p(Math.exp(-e))}function ky(e){return e>36?-e:-Math.log1p(Math.exp(e))}function Zh(e,t,n){if(e===-1/0)return-1/0;if(e===1/0)return 0;if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"){let r=Math.exp(e);return r===0?e:Math.log(-Math.expm1(-r))}return t==="logistic"||t==="loglogistic"?Ty(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?se(e,{logP:!0}):an(e,n,{logP:!0})}function Jh(e,t,n){return e===-1/0?0:e===1/0?-1/0:t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?-Math.exp(e):t==="logistic"||t==="loglogistic"?ky(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?se(e,{lowerTail:!1,logP:!0}):an(e,n,{lowerTail:!1,logP:!0})}function Pa(e,t,n){return t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?e===1/0?-1/0:e-Math.exp(e):t==="logistic"||t==="loglogistic"?-Math.abs(e)-2*Math.log1p(Math.exp(-Math.abs(e))):t==="gaussian"||t==="loggaussian"||t==="lognormal"?-.5*(e*e+Ny):El(e,n,{log:!0})}function nl(e,t,n){if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"){let o=Math.exp(e);return[1-o,o*o-3*o+1]}if(t==="logistic"||t==="loglogistic"){let o=Math.exp(-Math.abs(e));return[-Math.tanh(e/2),1-6*o/((1+o)*(1+o))]}if(t==="gaussian"||t==="loggaussian"||t==="lognormal")return[-e,e*e-1];let r=n+e*e;return[-(n+1)*e/r,(n+1)*((n+2)*e*e-n)/(r*r)]}function qy(e,t,n){if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh")return-Math.exp(e);if(t==="logistic"||t==="loglogistic"){let r=Math.exp(-Math.abs(e));return-2*r/((1+r)*(1+r))}return t==="gaussian"||t==="loggaussian"||t==="lognormal"?-1:-(n+1)*(n-e*e)/((n+e*e)*(n+e*e))}function Va(e,t,n,r,o,s,i){let c=e===-1/0?-1/0:(e-r)/o,a=t===1/0?1/0:(t-r)/o;if(n===1){let[C]=nl(a,s,i),q=Pa(a,s,i),F=qy(a,s,i);return{g:q-Math.log(o),e:-C/o,ee:F/(o*o),t:-a*C-1,tt:a*C+a*a*F,et:(C+a*F)/o}}let l=Zh(a,s,i),f=Zh(c,s,i),m=Jh(c,s,i),u=Jh(a,s,i),h=n===0?m:n===2?l:rp(c>0?m:l,c>0?u:f),w=c===-1/0?0:Math.exp(n===0&&(s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh")?c:Pa(c,s,i)-h),g=a===1/0?0:Math.exp(Pa(a,s,i)-h),[b]=nl(c,s,i),[N]=nl(a,s,i),v=w===0||c===-1/0?0:w*b,M=g===0||a===1/0?0:g*N,p=c===-1/0||w===0?0:c*w,y=a===1/0||g===0?0:a*g,x=c===-1/0||v===0?0:c*c*v,E=a===1/0||M===0?0:a*a*M,R=-(g-w)/o,A=(M-v)/(o*o)-R*R,S=-(y-p),T=E-x-S*(1+S),L=((a===1/0||M===0?0:a*M)-(c===-1/0||v===0?0:c*v))/o-R*(1+S);return{g:h,e:R,ee:A,t:S,tt:T,et:L}}function Iy(e){let t=e.length,n=e.map((r,o)=>[...r,...r.map((s,i)=>o===i?1:0)]);for(let r=0;r<t;r++){let o=r;for(let i=r+1;i<t;i++)Math.abs(n[i][r])>Math.abs(n[o][r])&&(o=i);if(!Number.isFinite(n[o][r])||Math.abs(n[o][r])<=1e-13*Math.max(Number.MIN_VALUE,...e.map((i,c)=>Math.abs(i[c]??0))))throw new Error("singular information matrix");[n[r],n[o]]=[n[o],n[r]];let s=n[r][r];for(let i=0;i<2*t;i++)n[r][i]/=s;for(let i=0;i<t;i++)if(i!==r){let c=n[i][r];for(let a=0;a<2*t;a++)n[i][a]-=c*n[r][a]}}return n.map(r=>r.slice(t))}function sp(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 ap(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 ep(e){let t=e.length;for(let n=0;n<t;n++)if(e[n][n]>0){e[n][n]=1/e[n][n];for(let r=n+1;r<t;r++){e[r][n]=-e[r][n];for(let o=0;o<n;o++)e[r][o]+=e[r][n]*e[n][o]}}for(let n=0;n<t;n++){if(e[n][n]===0){for(let r=0;r<t;r++)e[n][r]=0,e[r][n]=0;continue}for(let r=n+1;r<t;r++){let o=e[r][n]*e[r][r];e[n][r]=o;for(let s=n;s<r;s++)e[n][s]+=o*e[r][s]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}function ps(e,t){return e.map(n=>gr(n,t))}function gr(e,t){let n=e.reduce((r,o,s)=>r+o*t[s],0);return vr(e,t,n)}function ip(e,t=1e-10){let n=t>0?Math.min(65536,1/Math.sqrt(t)):65536,r=Array.from({length:e[0].length},(o,s)=>{let i=e.map(a=>a[s]),c=0;for(let a of i)c=Math.max(c,Math.abs(a));if(c>n||c>0&&c<1/n){let a=Se([i]);return{values:a.groups[0],exponent:a.k}}return{values:i,exponent:0}});return{values:e.map((o,s)=>r.map(i=>i.values[s])),exponent:r.map(o=>o.exponent)}}function tp(e,t,n){return e<=0?-1/0:e>=1?1/0:t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?Math.log(-Math.log1p(-e)):t==="logistic"||t==="loglogistic"?Math.log(e/(1-e)):t==="gaussian"||t==="loggaussian"||t==="lognormal"?le(e):Wt(e,n)}function np(e,t,n,r,o,s,i,c,a,l,f,m,u,h,d,w=1e-10){let g=e[0].length,b=g+(m===void 0?a:0),N=ip(e,w),v=N.values,M=N.exponent,p=u.map((k,V)=>V<g?Le(k,M[V]):k),y=(k,V)=>{let B=0,Q=Array(b).fill(0),re=Array.from({length:b},()=>Array(b).fill(0)),z=Array.from({length:b},()=>Array(b).fill(0));for(let J=0;J<t.length;J++){let Z=c[J],ke=gr(v[J],k.slice(0,g))+i[J],ne=m??Math.exp(k[g+Z]),$=Va(n[J],r[J],o[J],ke,ne,l,f),Y=s[J];B+=Y*$.g;let W=Array(b).fill(0);for(let U=0;U<g;U++)W[U]=v[J][U]*$.e;m===void 0&&(W[g+Z]=$.t);for(let U=0;U<b;U++){Q[U]+=Y*W[U];for(let j=0;j<b;j++)z[U][j]+=Y*W[U]*W[j]}if(V){for(let U=0;U<g;U++){for(let j=0;j<g;j++)re[U][j]+=Y*v[J][U]*v[J][j]*$.ee;m===void 0&&(re[U][g+Z]+=Y*v[J][U]*$.et)}if(m===void 0){for(let U=0;U<g;U++)re[g+Z][U]+=Y*v[J][U]*$.et;re[g+Z][g+Z]+=Y*$.tt}}}return{ll:B,u:Q,H:re,B:z}},x=k=>{let V=k.H.map(Q=>Q.map(re=>-re)),B=sp(V,w);return{factor:V,rank:B}},E=k=>{let V=y(k,!0),B=x(V),Q=B.rank>=0?ap(B.factor,V.u):ps(Iy(V.B),V.u);return{e:V,delta:Q}},R=y(p,!0),A=R.ll,S=0,T=!1,L=!1,C;try{C=E(p)}catch{C={e:R,delta:Array(b).fill(0)}}let q=p.map((k,V)=>k+C.delta[V]);if(h===0){let k=x(R),V=ep(k.factor),B=V.map(re=>re.slice()),Q=V.map((re,z)=>re[z]===0?NaN:p[z]/Math.sqrt(Math.max(0,re[z])));return{b:p.map((re,z)=>z<g?Le(re,-M[z]):re),ll:R.ll,iter:S,V:V.map((re,z)=>re.map((J,Z)=>Le(J,(z<g?-M[z]:0)+(Z<g?-M[Z]:0)))),VScaled:B,xExponent:M,score:R.u.map((re,z)=>z<g?Le(re,M[z]):re),z:Q,warn:T}}for(let k=1;k<=h;k++){let V=y(q,!0);if(Number.isFinite(V.ll)&&Math.abs(1-A/V.ll)<=d){p.splice(0,p.length,...q),R=V,A=V.ll,S=k,L=!0;break}if(!Number.isFinite(V.ll)||V.ll<A){let B=q.slice();q=p.map((Q,re)=>Q+(B[re]-Q)/3);for(let Q=0;Q<a;Q++)m===void 0&&(q[g+Q]=Math.max(q[g+Q],p[g+Q]-1.1));S=k;continue}if(p.splice(0,p.length,...q),R=V,A=V.ll,S=k,k<h)try{let B=E(p);q=p.map((Q,re)=>Q+B.delta[re])}catch{q=p.slice()}else try{let B=E(p),Q=p.map((z,J)=>z+B.delta[J]),re=y(Q,!1);Number.isFinite(re.ll)&&re.ll>=A&&p.splice(0,p.length,...Q)}catch{}}S===h&&h>=2&&!L&&(T=!0);let F=y(p,!0),_=x(F),O=ep(_.factor),I=O.map(k=>k.slice()),D=O.map((k,V)=>k[V]===0?NaN:p[V]/Math.sqrt(Math.max(0,k[V])));return{b:p.map((k,V)=>V<g?Le(k,-M[V]):k),ll:A,iter:S,V:O.map((k,V)=>k.map((B,Q)=>Le(B,(V<g?-M[V]:0)+(Q<g?-M[Q]:0)))),VScaled:I,xExponent:M,score:F.u.map((k,V)=>V<g?Le(k,M[V]):k),z:D,warn:T}}function _y(e,t,n=[],r={}){K(r,wy,"survreg"),K(sn(r,"control"),by,"survreg","control");let o=r.type??"right",s=r.dist??"weibull",i=r.df??4,c=r.intercept??!0,a=e.length,l=t===void 0?Array(a).fill(1):Array.from(t,Number);if(l.length!==a||n.length!==0&&n.length!==a)throw new RangeError("variable lengths differ");if(o==="counting")throw new Error("start-stop type Surv objects are not supported");if(!["right","left","interval","interval2"].includes(o))throw new TypeError("invalid survival type");if(!["extreme","logistic","gaussian","weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic","t"].includes(s))throw new RangeError("'arg' should be one of \u201Cextreme\u201D, \u201Clogistic\u201D, \u201Cgaussian\u201D, \u201Cweibull\u201D, \u201Cexponential\u201D, \u201Crayleigh\u201D, \u201Cloggaussian\u201D, \u201Clognormal\u201D, \u201Cloglogistic\u201D, \u201Ct\u201D");if((o==="interval"||o==="interval2")&&r.time2===void 0)throw new TypeError("time2 is required for interval data");if(r.time2!==void 0&&r.time2.length!==a)throw new RangeError("variable lengths differ (found for 'time2')");if(r.scale!==void 0&&(!Number.isFinite(r.scale)||r.scale<0))throw new RangeError("Invalid scale value");if(s==="t"&&(!(i>=3)||!Number.isFinite(i)))throw new RangeError("Degrees of freedom must be >=3");if(s!=="t"&&r.df!==void 0)throw new RangeError("Extreme value distribution has no optional parameters");let f=["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(s),m=r.scale!==void 0&&r.scale>0?r.scale:void 0,u=s==="exponential"||s==="rayleigh"?s==="exponential"?1:.5:m,h=r.time2,d=r.weights??Array(a).fill(1),w=r.offset??Array(a).fill(0),g=r.strata??Array(a).fill("1");for(let[G,ue]of[[d,"weights"],[w,"offset"],[g,"strata"]])if(G.length!==a)throw new RangeError(`variable lengths differ (found for '${ue}')`);let b=[],N=[],v=[],M=[],p=[],y=[],x=[],E=[],R=[],A=(o==="right"||o==="left")&&l.some(G=>Number(G)===2),S=G=>{R.includes(G)||R.push(G)},T=[...new Set(g.map(String))].sort(),L=g.map(G=>T.indexOf(String(G))),C=[];if(u!==void 0&&T.length>1)throw new Error("The scale argument is not valid with multiple strata");for(let G=0;G<a;G++){let ue=n.length===0?[]:Array.from(n[G]);if((o==="interval2"?Number.isNaN(e[G])&&Number.isNaN(h[G]):o==="interval"&&My(l[G])===3?Number.isNaN(e[G])||Number.isNaN(h[G]):Number.isNaN(e[G]))||o!=="interval2"&&Number.isNaN(l[G])||Number.isNaN(d[G])||Number.isNaN(w[G])||Number.isNaN(g[G])||ue.some(Number.isNaN))continue;if(!(d[G]>0))throw new RangeError("Invalid weights, must be >0");if(ue.some(ve=>!Number.isFinite(ve)))throw new Error("data contains an infinite predictor");let ye=e[G],Ee=e[G],ee=Number(l[G]);if(o==="right")if(A){if(ee!==1&&ee!==2){S("Invalid status value, converted to NA");continue}ee===1&&(Ee=1/0),ee=ee===2?1:0}else{if(ee!==0&&ee!==1){S("Invalid status value, converted to NA");continue}ee===0&&(Ee=1/0),ee=ee===1?1:0}else if(o==="left")if(A){if(ee!==1&&ee!==2){S("Invalid status value, converted to NA");continue}ee===1&&(ye=-1/0),ee=ee===2?1:2}else{if(ee!==0&&ee!==1){S("Invalid status value, converted to NA");continue}ee===0&&(ye=-1/0),ee=ee===1?1:2}else if(o==="interval"){if(Ee=h[G],ee===0)ye=e[G],Ee=1/0;else if(ee===1)Ee=ye;else if(ee===2)Ee=e[G],ye=-1/0;else if(ee!==3){S("Invalid status value, converted to NA");continue}if(ee===3&&ye>Ee){S("Invalid interval: start > stop, NA created");continue}if(ee===3&&ye===Ee)throw new Error("initial iteration failed (use starting estimates?)")}else if(o==="interval2"){let ve=e[G],ie=h[G];if(Number.isNaN(ve)&&Number.isNaN(ie))continue;if(ve>ie){S("Invalid interval: start > stop, NA created");continue}ve===ie?(ye=ve,Ee=ie,ee=1):!Number.isFinite(ve)||ve===0?(ye=-1/0,Ee=ie,ee=2):Number.isFinite(ie)?(ye=ve,Ee=ie,ee=3):(ye=ve,Ee=1/0,ee=0)}if(f){if((Number.isFinite(ye)&&ye<0||Number.isFinite(Ee)&&Ee<0)&&S("NaNs produced"),ye===1/0||Ee===-1/0||Number.isFinite(ye)&&ye<=0||Number.isFinite(Ee)&&Ee<=0)throw new Error("Invalid survival times for this distribution");Number.isFinite(ye)&&(ye=Math.log(ye)),Number.isFinite(Ee)&&(Ee=Math.log(Ee))}b.push(G),N.push(ee===1?Ee:Number.isFinite(ye)&&Number.isFinite(Ee)?tn(ye,Ee):Number.isFinite(ye)?ye:Ee),v.push(ye),M.push(Ee),p.push(ee),y.push(d[G]),x.push(w[G]),E.push(ue),C.push(L[G])}if(b.length===0)throw new Error("initial iteration failed (use starting estimates?)");if(!p.some(G=>G===1||G===3))throw new Error("initial iteration failed (use starting estimates?)");if(N.some(G=>!Number.isFinite(G)))throw new Error("initial iteration failed (use starting estimates?)");if(E.some(G=>G.length!==E[0].length))throw new TypeError("all rows of the matrix must have the same length");let q=E[0].length,F=q+(c?1:0),_=E.map(G=>c?[1,...G]:G),O=T.length,I=N.map((G,ue)=>G-x[ue]),D=y.reduce((G,ue)=>G+ue,0),k=I.reduce((G,ue,Me)=>G+ue*y[Me],0)/D,V=I.reduce((G,ue,Me)=>G+y[Me]*(ue-k)**2,0)/D,B=Math.max(...I)-Math.min(...I),Q=s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh"?V/1.64:s==="logistic"||s==="loglogistic"?V/3.2:s==="t"?V*(i-2)/i:V,re=u??2*Math.sqrt(Q);if(u===void 0&&(!(re>0)||!(B>0)))throw new Error("initial iteration failed (use starting estimates?)");let z=F+(u===void 0?O:0),J=Array(z).fill(0);if(c&&(J[0]=k),u===void 0)for(let G=0;G<O;G++)J[F+G]=Math.log(re);let Z=r.control??{},ke=Z.maxiter??30,ne=Z.relTolerance??1e-9;if(!Number.isInteger(ke)||ke<0||!Number.isFinite(ne)||ne<0)throw new RangeError("invalid control");if(Z.tolerChol!==void 0&&(!Number.isFinite(Z.tolerChol)||Z.tolerChol<0))throw new RangeError("invalid control");let $=_.map(()=>[1]),Y=_.every(G=>G.length===1&&G[0]===1);if(r.init!==void 0&&r.init.length!==F&&r.init.length!==z)throw new Error("Wrong length for initial parameters");let U=N.map((G,ue)=>Va(v[ue],M[ue],p[ue],G,u??re,s,i)).map((G,ue)=>{let Me=-y[ue]*G.ee;return Me>0&&Number.isFinite(Me)?{w:Me,r:N[ue]-G.e/G.ee-x[ue]}:{w:0,r:0}}),j=U.reduce((G,ue)=>G+ue.w,0);if(!(j>0))throw new Error("initial iteration failed (use starting estimates?)");let te=U.reduce((G,ue)=>G+ue.w*ue.r,0)/j,fe=[r.init!==void 0&&Y?r.init[0]:te,...u===void 0?Array(O).fill(Math.log(re)):[]];if(Y&&r.init!==void 0&&r.init.length===z)for(let G=0;G<O;G++)fe[1+G]=r.init[1+G];let Re=Y?ke:Math.max(ke,1e3),Ce=np($,N,v,M,p,y,x,C,O,s,i,u,fe,Re,ne,Z.tolerChol??1e-10),be=J.slice();if(u===void 0)for(let G=0;G<O;G++)be[F+G]=Ce.b[1+G];if(!Y&&r.init!==void 0){for(let G=0;G<F;G++)be[G]=r.init[G];if(r.init.length===z)for(let G=F;G<z;G++)be[G]=r.init[G]}else if(!Y&&F>0){let G=E.map((Ee,ee)=>{let ve=u??Math.exp(Ce.b[1+C[ee]]),ie=Va(v[ee],M[ee],p[ee],N[ee],ve,s,i),Ae=-y[ee]*ie.ee;return Ae>0&&Number.isFinite(Ae)?{w:Ae,r:N[ee]-ie.e/ie.ee-x[ee]}:{w:0,r:0}}),ue=ip(_,Z.tolerChol??1e-10),Me=Array.from({length:F},(Ee,ee)=>Array.from({length:F},(ve,ie)=>ue.values.reduce((Ae,Ke,$e)=>Ae+G[$e].w*Ke[ee]*Ke[ie],0))),ye=Array.from({length:F},(Ee,ee)=>ue.values.reduce((ve,ie,Ae)=>ve+G[Ae].w*ie[ee]*G[Ae].r,0));try{let Ee=Me.map(ie=>ie.slice()),ve=sp(Ee,Z.tolerChol??1e-10)>=0?ap(Ee,ye):ps(nt(Me),ye);be.splice(0,F,...ve.map((ie,Ae)=>Le(ie,-ue.exponent[Ae])))}catch{}}let me=Y?Ce:np(_,N,v,M,p,y,x,C,O,s,i,u,be,ke,ne,Z.tolerChol??1e-10),qe=f?b.reduce((G,ue,Me)=>G+(p[Me]===1?y[Me]*Math.log(e[ue]):0),0)*-1:0,de=me.V.map(G=>G.slice()),Fe=me.b.slice(0,F).map((G,ue)=>Number.isNaN(me.z[ue])?NaN:G),xe=Fe.map((G,ue)=>(de[ue]?.[ue]??0)===0?0:Math.sqrt(Math.max(0,de[ue]?.[ue]??0))),Oe=Fe.map((G,ue)=>me.z[ue]).map(G=>2*se(-Math.abs(G))),Be=u!==void 0?[u]:Array.from({length:O},(G,ue)=>Math.exp(me.b[F+ue])),ze=Array.from({length:F},(G,ue)=>_.reduce((Me,ye)=>Me+ye[ue],0)/_.length),et=_.map((G,ue)=>gr(G,me.b.slice(0,F))+x[ue]),Ue=2*(me.ll+qe-(Ce.ll+qe)),Xe=F+(u===void 0?O:0)-(1+(u===void 0?O:0)),pe=[...Fe.map((G,ue)=>ue===0&&c?"(Intercept)":`x${ue+(c?0:1)}`),...u===void 0?O===1?["Log(scale)"]:T:[]],oe=[...Fe,...u===void 0?me.b.slice(F):[]],we=oe.map((G,ue)=>(de[ue]?.[ue]??0)===0?0:Math.sqrt(Math.max(0,de[ue]?.[ue]??0))),ge=oe.map((G,ue)=>me.z[ue]),ce=ge.map(G=>2*se(-Math.abs(G))),tt={coefficients:Fe,coefNames:pe.slice(0,F),scale:Be,table:{value:oe,se:we,z:ge,p:ce},strataLevels:T.map(G=>g.find(ue=>String(ue)===G)),hasOffset:r.offset!==void 0,icoef:u===void 0||b.length===1?Ce.b.slice():[Ce.b[0],Math.log(u)],var:de,loglik:[Ce.ll+qe,me.ll+qe],iter:me.iter,df:F+(u===void 0?O:0),idf:1+(u===void 0?O:0),dfResidual:b.length-(F+(u===void 0?O:0)),n:b.length,linearPredictors:et,means:ze,score:me.score.slice(),tableRowNames:pe,value:oe,se:we,z:ge,p:ce,chi:Ue,chiDf:Xe,chiP:_t(Ue,Xe,{lowerTail:!1}),dist:s,distributionDf:i,parms:i,intercept:c,x:_,offset:x,rows:b,weights:y,strata:C,y:N,lower:v,upper:M,status:p,type:o,warnings:[...R,...me.warn||Ce.warn?["Ran out of iterations and did not converge"]:[],...s==="exponential"&&r.scale!==void 0&&r.scale>0?["Exponential has a fixed scale, user specified value ignored"]:[]]};return rl.set(tt,{V:me.VScaled,xExponent:me.xExponent}),X("survreg",tt)}function Ly(e,t={}){K(t,vy,"survregPredict");let n=t.type??"lp",r=t.newX===void 0?e.x:t.newX.map(h=>e.intercept?[1,...h]:Array.from(h)),o=e.hasOffset?t.newOffset??(t.newX===void 0?e.offset:Array(r.length).fill(0)):Array(r.length).fill(0);if(o.length!==r.length)throw new RangeError("variable lengths differ (found for 'newOffset')");let s=e.strataLevels.map(String),i=t.newStrata?.map(h=>s.indexOf(String(h)))??(t.newX===void 0?e.strata.slice():r.map(()=>0));if(e.strataLevels.length>1&&t.newStrata===void 0&&t.newX!==void 0)throw new Error("object 's' not found");if(t.newStrata!==void 0&&t.newStrata.length!==r.length)throw new RangeError("variable lengths differ (found for 'newStrata')");if(t.newX!==void 0&&r.some(h=>h.length!==e.coefficients.length))throw new RangeError("newX has the wrong number of columns");if(i.some(h=>h<0))throw new Error("unknown stratum in newStrata");if(!["lp","linear","link","response","quantile","uquantile"].includes(n))throw new RangeError("invalid prediction type");let c=t.p===void 0?[.1,.9]:typeof t.p=="number"?[t.p]:Array.from(t.p),a=["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist),l=r.map((h,d)=>{if(h.some(Number.isNaN))return n==="quantile"||n==="uquantile"?c.map(()=>NaN):NaN;let w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[d]:gr(h,e.coefficients)+o[d],g=e.scale.length===1?e.scale[0]:e.scale[i[d]];return n==="lp"||n==="linear"||n==="link"?w:n==="response"?a?Math.exp(w):w:c.map(b=>{let N=w+g*tp(b,e.dist,e.distributionDf);return n==="uquantile"?N:a?Math.exp(N):N})}),f=(n==="quantile"||n==="uquantile")&&c.length===1?l.map(h=>h[0]):l;if(!t.seFit)return f;let m=r.map((h,d)=>{if(h.some(Number.isNaN))return n==="quantile"||n==="uquantile"?c.map(()=>NaN):NaN;let w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[d]:gr(h,e.coefficients)+o[d],g=e.scale.length===1?e.scale[0]:e.scale[i[d]],b=rl.get(e),N=b?.V??e.var,v=M=>{let p=Array(N.length).fill(0);for(let y=0;y<h.length;y++)p[y]=b?Le(h[y],-b.xExponent[y]):h[y];return N.length>h.length&&(p[h.length+i[d]]=M*g),Math.sqrt(Math.max(0,gr(p,ps(N,p))))};return n==="lp"||n==="linear"||n==="link"?v(0):n==="response"?a?Qh(w,v(0)):v(0):c.map(M=>{let p=tp(M,e.dist,e.parms),y=v(p),x=w+g*p;return n==="uquantile"?y:a?Qh(x,y):y})}),u=(n==="quantile"||n==="uquantile")&&c.length===1?m.map(h=>h[0]):m;return{fit:f,seFit:u,warnings:[]}}function Cy(e,t={}){K(t,yy,"survregResiduals");let n=t.type??"response",r=e.distributionDf,o=e.coefficients.length,s=e.var.length,i=t.weighted??!1;if(!["response","deviance","working","dfbeta","dfbetas","ldcase","ldresp","ldshape","matrix"].includes(n))throw new RangeError("invalid residual type");let c=rl.get(e),a=c?.V??e.var,l=e.x.map((f,m)=>{let u=e.linearPredictors[m],h=e.scale.length===1?e.scale[0]:e.scale[e.strata[m]],d=Va(e.lower[m],e.upper[m],e.status[m],u,h,e.dist,r),w=i?e.weights[m]:1,g=f.map((R,A)=>c?Le(R,-c.xExponent[A]):R),b=Array(s).fill(0);for(let R=0;R<o;R++)b[R]=g[R]*d.e;s>o&&(b[o+e.strata[m]]=d.t);let N=ps(a,b),v=N.map((R,A)=>c&&A<o?Le(R,-c.xExponent[A]):R),M=gr(b,N);if(n==="matrix")return[d.g,d.e,d.ee,d.t,d.tt,d.et].map(R=>R*w);if(n==="working")return-d.e/d.ee*w;if(n==="dfbeta")return v.map(R=>R*w);if(n==="dfbetas")return N.map((R,A)=>R/Math.sqrt(a[A]?.[A]??NaN)*w);if(n==="ldcase")return M*w;if(n==="ldresp"||n==="ldshape"){let R=Array(s).fill(0);for(let A=0;A<o;A++)R[A]=n==="ldresp"?h*(-g[A]*d.ee):g[A]*d.et;return s>o&&(R[o+e.strata[m]]=n==="ldresp"?-h*d.et:d.tt),gr(R,ps(a,R))*w}let p=Pa(0,e.dist,r)-Math.log(h),y=e.status[m]===3?tn(e.lower[m],e.upper[m]):e.y[m];e.status[m]===3&&(e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh")&&(y=Ay(e.lower[m],e.upper[m],h));let x=e.status[m]===0?1:e.status[m]===2?-1:Math.sign(y-u);if(n==="deviance"){let R=e.status[m]===1?p:0;if(e.status[m]===3){let A=Ry(e.lower[m],e.upper[m],h);e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh"?R=Sy(e.lower[m],e.upper[m],h):e.dist==="logistic"||e.dist==="loglogistic"?R=Math.log(Math.tanh(A/4)):e.dist==="gaussian"||e.dist==="loggaussian"||e.dist==="lognormal"?R=Math.log(2*se(A/2)-1):e.dist==="t"&&(R=Math.log(2*an(A/2,e.distributionDf)-1))}return x*Math.sqrt(Math.max(0,2*(R-d.g)))*w}let E=y-u;return(["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist)?xy(y,u):E)*w});return l}var Fy=["n","power","r","type","nfractional","alpha","sidedTest"],Oy=["n","power","r","type","nfractional","alpha","sidedTest"],Dy=["n","power","r","type","nfractional","alpha","sidedTest"],Py=["n","power","r","type","nfractional","alpha","sidedTest"],Vy=["n","power","r","type","nfractional","alpha","sidedTest"],$y=["n","power","r","type","nfractional","alpha","sidedTest"],zy=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],Wy=["alpha"],jy=["alpha"],Ky=["alpha"],By=["alpha"],Uy=["alpha"],Hy=["nmax"],Pr=.05,hn=e=>le(e),Un=e=>se(e),Dr=e=>Number.isFinite(e),qt=e=>Dr(e)&&e>=0&&e<=1,xn=e=>Dr(e)&&e>0;function Je(e,t,n,r){if(typeof e!="number")throw new TypeError(`'${t}' must be numeric`);if(!n(e))throw new RangeError(r)}function Vr(e){Je(e,"alpha",qt,"'alpha' must be a probability in [0, 1]")}function Yy(e,t){return[e*t/(1+t),e/(1+t)]}function Ro(e,t,n,r,o,s,i={}){let{n:c,power:a}=i,l=i.r??1,f=i.alpha??Pr,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(Je(n,"treat",Dr,"'treat' must be finite"),Je(r,"control",Dr,"'control' must be finite"),t==="binary"?(Je(n,"treat",qt,"'treat' must be a probability in [0, 1]"),Je(r,"control",qt,"'control' must be a probability in [0, 1]")):Je(o,"sigma",xn,"'sigma' must be positive and finite"),Je(l,"r",xn,"'r' must be positive and finite"),Vr(f),(e!=="equivalence"||u!=="equality")&&Je(s,"delta",L=>Dr(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");nn([c,a],"exactly one of 'n' and 'power' must be NULL"),Ne(a)||Je(a,"power",L=>qt(L)&&L>0&&L<1,"'power' must be strictly between 0 and 1"),c!=null&&Je(c,"n",L=>Dr(L)&&L>0,"'n' must be positive and finite");let d=n-r,w=t==="binary"?n*(1-n):o*o,g=t==="binary"?r*(1-r):o*o,b=e==="equivalence"&&u==="equality",N=b?void 0:s,v=e==="noninferiority"?d+N:e==="superiority"?d-N:b?d:N-Math.abs(d);if(e==="noninferiority"&&v<=0)throw new RangeError("Target power is not reachable under the specified hypotheses");let M=e==="superiority"?le(f/h,{lowerTail:!1}):b?le(f/2,{lowerTail:!1}):le(f,{lowerTail:!1}),p=a??.8,y=b||e!=="equivalence"?le(1-p,{lowerTail:!1}):le((1-p)/2,{lowerTail:!1}),x,E,R,A,S;if(Ne(c))v===0?(R=1/0,A=1/0):(A=(M+y)**2*(w/l+g)/(v*v),R=l*A),m?(x=R,E=A):(E=Math.ceil(A),x=Math.ceil(e==="equivalence"&&t==="continuous"||e==="superiority"?l*E:R)),S=a;else{[R,A]=Yy(c,l),x=m?R:Math.ceil(R),E=m?A:Math.ceil(A);let L=Math.sqrt(w/x+g/E);if(e==="equivalence"&&!b)S=Math.max(0,Un((s-d)/L-hn(1-f))+Un((s+d)/L-hn(1-f))-1);else if(b)S=Un(Math.abs(d)/L-hn(1-f/2))+Un(-Math.abs(d)/L-hn(1-f/2));else if(e==="noninferiority")S=Un((d+s)/L-hn(1-f));else{let C=Math.abs(d-s)/L;S=Un(C-M)+Un(-C-M)}}return{nTotal:x+E,nTreat:x,nControl:E,nTotalUnrounded:R+A,nTreatUnrounded:R,nControlUnrounded:A,power:S,...N===void 0?{}:{delta:N},warnings:[]}}function Gy(e,t,n,r={}){return K(r,Fy,"epiSsequb"),X("epiSsequb",Ro("equivalence","binary",e,t,void 0,n??void 0,r))}function Xy(e,t,n,r,o={}){K(o,Oy,"epiSsequc");let s=Ro("equivalence","continuous",e,t,n,r??void 0,o);return X("epiSsequc",s)}function Qy(e,t,n,r={}){return K(r,Dy,"epiSsninfb"),X("epiSsninfb",Ro("noninferiority","binary",e,t,void 0,n,r))}function Zy(e,t,n,r,o={}){return K(o,Py,"epiSsninfc"),X("epiSsninfc",Ro("noninferiority","continuous",e,t,n,r,o))}function Jy(e,t,n,r={}){return K(r,Vy,"epiSssupb"),X("epiSssupb",Ro("superiority","binary",e,t,void 0,n,r))}function eN(e,t,n,r,o={}){return K(o,$y,"epiSssupc"),X("epiSssupc",Ro("superiority","continuous",e,t,n,r,o))}function xo(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 xo(e,1-t).reverse();let r=Math.floor((e+1)*t);n[r]=Math.exp(e*Math.log1p(-t)+r*Math.log(t/(1-t))+Array.from({length:r},(o,s)=>Math.log(e-s)-Math.log(s+1)).reduce((o,s)=>o+s,0));for(let o=r;o>0;o--)n[o-1]=n[o]*(o/(e-o+1))*((1-t)/t);for(let o=r;o<e;o++)n[o+1]=n[o]*((e-o)/(o+1))*(t/(1-t));return n}function tN(e,t,n,r={}){K(r,zy,"powerPaired2x2");let o=r.sigLevel??Pr,s=Te(r.alternative,["two.sided","one.sided"]),i=r.strict??!1,c=r.nullOddsRatio??1,a=r.errbound??1e-6,l=Te(r.tsmethod,["central","minlike","blaker"]);if(Je(e,"pb",qt,"'pb' must be in [0, 1]"),Je(t,"pc",qt,"'pc' must be in [0, 1]"),Je(n,"npairs",y=>Number.isSafeInteger(y)&&y>=0&&y<=1e5,"'npairs' must be a nonnegative safe integer"),Vr(o),Je(c,"nullOddsRatio",xn,"'nullOddsRatio' must be positive and finite"),Je(a,"errbound",y=>Dr(y)&&y>=0,"'errbound' must be nonnegative and finite"),e+t>1)throw new RangeError("probabilities must be finite, non-negative and not all 0");if(s!=="two.sided"&&s!=="one.sided")throw new RangeError("'alternative' should be one of \u201Ctwo.sided\u201D, \u201Cone.sided\u201D");if(!["central","minlike","blaker"].includes(l))throw new RangeError("'tsmethod' should be one of \u201Ccentral\u201D, \u201Cminlike\u201D, \u201Cblaker\u201D");let f=e+t,m=f===0?.5:e/f,u=c/(1+c),h=e>=t?1:-1,d=s==="two.sided"&&i&&l!=="blaker",w=s==="one.sided",g=xo(n,f),b=0,N=n;if(a>0){let y=0;for(;b<=n&&(y+=g[b])<=a/2;)b++;for(y=0;N>=0&&(y+=g[N])<=a/2;)N--}let v=0;for(let y=b;y<=N;y++){let x=g[y];if(x===0)continue;let E=xo(y,m),R=xo(y,u),A=0,S=0,T=new Array(y+1),L=new Array(y+1);for(let C=0;C<=y;C++)A+=R[C],T[C]=A;for(let C=y;C>=0;C--)S+=R[C],L[C]=S;for(let C=0;C<=y;C++){let q=T[C],F=L[C],_,O=u===.5&&o===1;w?_=O||(h>0?F<=o:q<=o):d?_=O||q<=o/2||F<=o/2:_=h>0?O?C>Math.floor(y/2):F<=o/2:O?C<Math.ceil(y/2):q<=o/2,_&&(v+=x*E[C])}}let M=`errbound= ${a===0?"0":a>=.001?String(a):`${a.toExponential(0).replace(/e([+-]?)(\\d+)$/,(y,x,E)=>`e${x||"+"}${E.padStart(2,"0")}`)}`}${e+t===1?" no non-informative pairs: pa+pd=0":""}`,p="Power for McNemar's Exact Test"+(d?", 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 X("powerPaired2x2",{power:v,npairs:n,pb:e,pc:t,sigLevel:o,alternative:s,nullOddsRatio:c,note:M,method:p,warnings:[]})}function nN(e,t,n,r,o,s={}){K(s,jy,"powerCT");let i=s.alpha??Pr;Vr(i);for(let[l,f]of[[e,"nE"],[t,"nC"]])Je(l,f,xn,`'${f}' must be positive and finite`);for(let[l,f]of[[n,"pE"],[r,"pC"]])Je(l,f,qt,`'${f}' must be in [0, 1]`);Je(o,"RR",xn,"'RR' must be positive and finite");let c=e/t,a=e*n+t*r;return Un(Math.sqrt(c*a)*Math.abs(o-1)/(c*o+1)-hn(1-i/2))}function rN(e,t,n,r,o,s={}){K(s,Wy,"ssizeCT");let i=s.alpha??Pr;Vr(i),Je(e,"power",f=>qt(f)&&f>0&&f<1,"'power' must be strictly between 0 and 1"),Je(t,"k",xn,"'k' must be positive and finite"),Je(n,"pE",qt,"'pE' must be in [0, 1]"),Je(r,"pC",qt,"'pC' must be in [0, 1]"),Je(o,"RR",xn,"'RR' must be positive and finite");let c=hn(1-i/2)+hn(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 oN(e,t,n,r,o,s={}){K(s,By,"powerEpi");let i=s.alpha??Pr;Vr(i),Je(e,"n",xn,"'n' must be positive and finite"),Je(t,"theta",xn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"psi",qt,"'psi' must be in [0, 1]"),Je(o,"rho2",qt,"'rho2' must be in [0, 1]");let c=e*Math.log(t)**2*n*(1-n)*r*(1-o);return Un(Math.sqrt(c)-hn(1-i/2))}function sN(e,t,n,r,o,s={}){K(s,Ky,"ssizeEpi");let i=s.alpha??Pr;Vr(i),Je(e,"power",qt,"'power' must be in [0, 1]"),Je(t,"theta",xn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"psi",qt,"'psi' must be in [0, 1]"),Je(o,"rho2",qt,"'rho2' must be in [0, 1]");let c=Math.log(t)**2*n*(1-n)*r*(1-o);return Math.ceil((hn(1-i/2)+hn(e))**2/c)}function aN(e,t,n,r,o={}){K(o,Uy,"numDEpi");let s=o.alpha??Pr;Vr(s),Je(e,"power",c=>qt(c)&&c>0&&c<1,"'power' must be strictly between 0 and 1"),Je(t,"theta",xn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"rho2",qt,"'rho2' must be in [0, 1]");let i=Math.log(t)**2*n*(1-n)*(1-r);return Math.ceil((hn(1-s/2)+hn(e))**2/i)}function cp(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 iN(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 lp(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 cN(e,t,n,r,o={}){K(o,Hy,"ph2simon");let s=o.nmax??100;for(let[M,p]of[[e,"pu"],[t,"pa"],[n,"ep1"],[r,"ep2"]])Je(M,p,qt,`'${p}' must be in [0, 1]`);Je(s,"nmax",M=>Number.isInteger(M)&&M>=1&&M<=1e3,"nmax cannot exceed 1000");let i=Array.from({length:s+1},(M,p)=>xo(p,e)),c=Array.from({length:s+1},(M,p)=>xo(p,t)),a=i.map(cp),l=c.map(cp),f=i.map(iN),m=[];for(let M=2;M<=s;M++){let p;for(let y=1;y<M&&(p===void 0||y<p.enP0);y++){let x=M-y,E=i[y],R=c[y],A=l[x],S=a[y];for(let T=0;T<y;T++){let L=S[T],C=y+x*(1-L),q=T,F=M-1,_=-1;for(;q<=F;){let O=Math.floor((q+F)/2),[,I]=lp(T,O,E,R,f[x],A);I<=r?(_=O,q=O+1):F=O-1}if(_>=0){let[O]=lp(T,_,E,R,f[x],A);O>n&&(_=-1)}_>=0&&(p===void 0||C<p.enP0)&&(p={r1:T,n1:y,r:_,n:M,enP0:C,petP0:L})}}p&&m.push(p)}if(m.length===0)throw new Error(` No feasible solution found.
|
|
20
20
|
Increase maximum sample size. Current nmax value = ${s}.`);let u=m.filter((M,p)=>!m.some((y,x)=>x!==p&&y.n<=M.n&&y.enP0<=M.enP0&&(y.n<M.n||y.enP0<M.enP0))),h=[],d=(M,p)=>(M.enP0-p.enP0)/(p.n-M.n+(M.enP0-p.enP0));for(let M of u)for(h.push(M);h.length>=3;){let p=h[h.length-3],y=h[h.length-2],x=h[h.length-1];if(d(p,y)>d(y,x))break;h.splice(h.length-2,1)}let w=h.map((M,p)=>{let y=p===h.length-1?0:d(M,h[p+1]),x=p===0?1:d(h[p-1],M);return{...M,qLo:y,qHi:x}}),g=w[0],b=w.reduce((M,p)=>p.enP0<M.enP0?p:M),N=w,v=[];return b.n>=s-1&&v.push(` Optimal sample size too close to nmax.
|
|
21
21
|
Try increasing nmax (current value = ${s})
|
|
22
|
-
`),X("ph2simon",{out:m,minimax:g,optimal:b,admissible:N,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:v})}var fN=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),mp=["method","mods","data","intercept","test","level","control"],up=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],mN=["type","level"],uN=["newmods","predtype","level"],hp=["model","predictor","retFit","level"],Rn=e=>e.length===1?e[0]:e,An=(e,t)=>e.reduce((n,r,o)=>n+r*t[o],0),ds=(e,t)=>e.map(n=>An(n,t)),wp=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),Ao=(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},pp=(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?Ao(e,0,n):NaN};function Yt(e,t,n,r){let o=e.length,s=n[0].length,i=t.map(O=>O+r),c=i.reduce((O,_)=>Math.min(O,_),1/0),a=i.map(O=>c/O),l=a.reduce((O,_)=>O+_,0),f=Array.from({length:s},(O,_)=>Array.from({length:s},(F,I)=>n.reduce((P,k,V)=>P+a[V]*k[_]*k[I],0))),m=Array.from({length:s},(O,_)=>n.reduce((F,I,P)=>F+a[P]*I[_]*e[P],0)),u;try{u=nt(f,wp(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=ds(u,m),d=ds(n,h),w=e.map((O,_)=>O-d[_]),g=e.every(O=>O===e[0])?0:w.reduce((O,_,F)=>O+a[F]*_*_,0),b=g/c,N=Array.from({length:o},(O,_)=>Array.from({length:o},(F,I)=>(_===I?a[_]:0)-a[_]*An(n[_],ds(u,n[I]).map(P=>P*a[I])))),v=N.reduce((O,_,F)=>O+_[F],0),M=v/c,p=N.reduce((O,_,F)=>O+_.reduce((I,P,k)=>I+P*N[k][F],0),0),y=p/c/c,x=N.map(O=>An(O,e)),E=.5*(An(x,x)-c*v),R=.5*(w.reduce((O,_,F)=>O+_*_*a[F]*a[F],0)-c*a.reduce((O,_)=>O+_,0)),A=a.reduce((O,_)=>O+_*_,0),S=.5*A,T=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(hN(f))),L=-.5*(i.reduce((O,_)=>O+Math.log(_),0)+T-s*Math.log(c)+b),C=u.map(O=>O.map(_=>_*c)),q=u.map((O,_)=>Math.sqrt(Math.max(0,O[_]))*Math.sqrt(c));return{beta:h,cov:C,se:q,fitted:d,residuals:w,qe:b,qeScaled:g,weights:a,minVariance:c,P:N,traceP:M,tracePNorm:v,traceP2:y,traceP2Norm:p,score:E,mlScore:R,mlInfo:S,logLik:L,sumWeightsNorm:l,sumWeightSquaredNorm:A}}function hN(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 ol(e,t){return e.reduce((n,r,o)=>n+r.reduce((s,i,c)=>s+i*t[c]*e[c][o]*t[o],0),0)}function pN(e,t,n,r,o,s,i){let c=e.length,a=c-n[0].length,l=t.map(m=>m+o);if(r==="EE"||r==="FE")return NaN;if(r==="REML")return s.traceP2Norm>0?s.minVariance*Math.sqrt(2/s.traceP2Norm):NaN;if(r==="ML")return s.sumWeightSquaredNorm>0?s.minVariance*Math.sqrt(2/s.sumWeightSquaredNorm):NaN;if(r==="DL"||r==="HS"||r==="HSk"){let m=Math.sqrt(2*ol(i.P,l)),u=r==="DL"?i.tracePNorm:r==="HS"?i.sumWeightsNorm:i.sumWeightsNorm*(c-1)/c;return u>0?m/u:NaN}if(r==="HE"){let m=Yt(e,Array(c).fill(1),n,0);return a>0?Math.sqrt(2*ol(m.P,l))/a:NaN}if(r==="SJ"){let m=Yt(e,Array(c).fill(1),n,0),u=m.residuals.reduce((d,w)=>d+w*w,0)/c;if(!(u>0)||a<=0)return NaN;let h=Yt(e,t,n,u);return u/a*Math.sqrt(2*ol(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 dp(e,t,n,r,o){let s=e.length,i=n[0].length,c=s-i,a=Yt(e,t,n,0);if(r==="EE"||r==="FE")return 0;if(r==="DL")return Math.max(0,(a.qeScaled-c*a.minVariance)/a.tracePNorm);if(r==="HS")return Math.max(0,(a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);if(r==="HSk")return Math.max(0,(s/(s-1)*a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);let l=Yt(e,Array(s).fill(1),n,0),f=l.residuals.reduce((b,N)=>b+N*N,0),m=Math.max(0,(f-t.reduce((b,N,v)=>b+l.P[v][v]*N,0))/Math.max(1,c));if(r==="HE")return m;if(r==="SJ"){let b=f/s,N=Yt(e,t,n,b),v=t.map(M=>b/(M+b));return Math.max(0,v.reduce((M,p,y)=>M+p*N.residuals[y]*N.residuals[y],0)/Math.max(1,c))}let u=b=>Yt(e,t,n,b).qe-c;if(r==="EB"){let b=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,Ao(u,0,b,1e-14))}if(r==="PM"){let b=o.tau2Max??Math.max(100,10*f/s);for(;u(b)>0&&b<Number.MAX_VALUE/4;)b*=2;let N=o.tol??1e-14;return Math.max(0,Ao(u,0,b,N))}let h=Math.max(0,o.tau2Init??m),d=o.maxiter??100,w=o.threshold??1e-5,g=o.stepadj??1;if(d<=1)return h;for(let b=0;b<d;b++){let N=Yt(e,t,n,h),v=r==="REML"?N.score:N.mlScore,M=r==="REML"?.5*N.traceP2Norm:N.mlInfo;if(!(M>0)||!Number.isFinite(M))return h;let p=Math.max(0,h+g*v/M);if(!Number.isFinite(p))return h;if(Math.abs(p-h)<w)return p;h=p}return h}function gp(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(An(o,o)),i=o.slice();for(let a of n){let l=An(i,a);i=i.map((f,m)=>f-l*a[m])}let c=Math.sqrt(An(i,i));s>0&&c>1e-10*s&&(t.push(r),n.push(i.map(a=>a/c)))}return t}function dN(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=zn(Jn(f),r,{intercept:n});if(o=m.x,s=m.colNames.map(h=>h==="(Intercept)"?"intrcpt":h),m.frame.rows.length!==e)throw new RangeError("variable lengths differ between yi and mods");if(o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let u=gp(o);return{x:o.map(h=>u.map(d=>h[d])),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(d=>Number(h[d]))),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=gp(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 al(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))-al(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 bp(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function sl(e,t,n,r,o){let s=bp(e,t,n,r);if(s===0)return NaN;let i=e/s,c=t/s,a=n/s,l=r/s,f=Pe(en(i,l),en(c,a)),m=Ft(i,c),u=Ft(a,l),h=o==="RR"?_e([a,0],m):o==="OR"?en(c,a):_e(m,u),d=at(f,h);return d[0]+d[1]}function gN(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(al(t)-.5*Math.log(t)-al(t-.5))}function wN(e,t){K(t,fN,"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],d=s[1][f];if(Math.abs(h)>1)throw new RangeError("One or more correlations are > 1 or < -1.");d>3?(m=Math.atanh(h),u=1/(d-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],d=s[1][f],w=s[2][f],g=s[3][f],b=s[4][f],N=s[5][f];if(!(w>1&&N>1&&d>=0&&b>=0))throw new RangeError("invalid means, standard deviations, or sample sizes");if(e==="MD")m=h-g,u=d*d/w+b*b/N;else{let v=w+N-2,M=((w-1)*d*d+(N-1)*b*b)/v,p=(h-g)/Math.sqrt(M);m=gN(v)*p,u=(w+N)/(w*N)+m*m/(2*(w+N))}}else{let h=s[0][f],d=s[1][f],w=s[2][f],g=s[3][f],b=h,N=d,v=w,M=g,p=t.add??.5,y=t.to??"only0",x=[b,N,v,M].some(_=>_===0),E=y==="all"||y==="only0"&&x||y==="if0all"&&i;if(e!=="RD"&&E&&(b+=p,N+=p,v+=p,M+=p),e==="RD"&&E&&(b+=p,N+=p,v+=p,M+=p),t.drop00&&(h===0&&w===0||d===0&&g===0)){a.push(NaN),l.push(NaN);continue}let R=bp(b,N,v,M),A=b/R,S=N/R,T=v/R,L=M/R,C=A+S,q=T+L,O=1/R;e==="RR"?(m=Math.log1p(sl(b,N,v,M,"RR")),u=O*(S/(A*C)+L/(T*q))):e==="OR"?(m=Math.log1p(sl(b,N,v,M,"OR")),u=O*(1/A+1/S+1/T+1/L)):(m=sl(b,N,v,M,"RD"),u=O*(A*S/(C*C*C)+T*L/(q*q*q)))}a.push(m),l.push(u)}return X("escalc",{yi:a,vi:l,measure:e,warnings:n})}function vp(e,t,n={}){vt(t)&&K(t,mp,"rma"),vt(t)&&K(sn(t,"control"),up,"rma","control"),K(n,mp,"rma"),K(sn(n,"control"),up,"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(U=>!Number.isFinite(U)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(i.some(U=>!Number.isFinite(U)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(s.method==="DL"&&i.some(U=>U<=0))throw new Error("DL estimator cannot be used when there are non-positive sampling variances in the data.");if(i.some(U=>U<0))throw new RangeError("sampling variances must be non-negative");if(i.some(U=>U===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=dN(o.length,s.mods,f,s.data),u=m.x,h=u[0].length,d=o.length,w=d-h,g=m.names[0]==="intrcpt"||!f&&u.every(U=>U[0]===1);if(h===0)throw new RangeError("the model must contain at least one coefficient");if(d<h)throw new Error("Number of parameters to be estimated is larger than the number of observations.");if(!(l>0&&l<100))throw new RangeError("level must be between 0 and 100");if(!["EE","FE","DL","HE","HS","HSk","SJ","ML","REML","EB","PM"].includes(c))throw new RangeError(`unknown method '${c}'`);if(!["z","t","knha"].includes(a))throw new RangeError(`unknown test '${a}'`);if(c==="DL"&&!Number.isFinite(Yt(o,i,u,0).qe))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");let b=Yt(o,i,u,0),N=dp(o,i,u,c,s.control??{}),v=Yt(o,i,u,N),M=s.mods===void 0?N:dp(o,i,Array.from({length:d},()=>[1]),c,s.control??{}),p=a==="knha"&&w>0?Math.max(1,v.qe/w):1,y=v.cov.map(U=>U.map(Q=>Q*p)),x=v.se.map(U=>U*Math.sqrt(p)),E=v.beta.map((U,Q)=>U/x[Q]),R=a==="z"?le(1-(1-l/100)/2):Wt(1-(1-l/100)/2,w),A=E.map(U=>2*(a==="z"?se(-Math.abs(U)):an(-Math.abs(U),w))),S=v.beta.map((U,Q)=>U-R*x[Q]),T=v.beta.map((U,Q)=>U+R*x[Q]),L=g?1:0,C=h===1?1:Math.max(1,h-L),q=0,O=1;if(h===1)q=E[0]*E[0],O=a==="z"?_t(q,1,{lowerTail:!1}):Tn(q,1,w,{lowerTail:!1});else{let U=v.beta.slice(L),Q=y.slice(L).map(re=>re.slice(L));try{q=An(U,nt(Q,U))}catch{q=NaN}O=a==="z"?_t(q,C,{lowerTail:!1}):Tn(q/C,C,w,{lowerTail:!1})}let _=b.tracePNorm,F=w>0?w*b.minVariance/_:0,I=c==="EE"||c==="FE"?b.qe>0?100*Math.max(0,(b.qe-w)/b.qe):0:N===0?0:100*N/(N+F),P=c==="EE"||c==="FE"?w>0?Math.max(1,b.qe/w):1:N===0?1:(N+F)/F,k=s.mods===void 0||h===1?null:M>0?100*Math.max(0,(M-N)/M):0,V=pN(o,i,u,c,N,v,b);return X("rma",{beta:{dim:[h,1],rowNames:h===1?m.names[0]??"intrcpt":m.names,colNames:null,rows:v.beta.map(U=>[U])},se:Rn(x),zval:Rn(E),pval:Rn(A),"ci.lb":Rn(S),"ci.ub":Rn(T),tau2:N,"se.tau2":V,QE:b.qe,QEp:w===0?1:_t(b.qe,w,{lowerTail:!1}),QM:q,QMp:O,R2:k,p:h,m:C,I2:I,H2:P,k:d,yi:o,vi:i,mods:u,intercept:g,method:c,test:a,level:l,warnings:m.warnings,covariance:y,tau2Null:M})}function bN(e,t={}){K(t,mN,"confintRma");let n=t.type??"QP",r=t.level??e.level,o=1-r/100;if(n!=="QP"&&n!=="PL")throw new RangeError("type must be 'QP' or 'PL'");let s=Yt(e.yi,e.vi,e.mods,e.tau2),i=e.k-e.mods[0].length,c=0,a=NaN;if(n==="QP"){let g=Yt(e.yi,e.vi,e.mods,0),b=Fn(1-o/2,i),N=Fn(o/2,i),v=g.qe,M=p=>Yt(e.yi,e.vi,e.mods,p).qe;c=v<=b?0:pp(p=>M(p)-b,Math.max(e.tau2,1e-8)),a=v<=N?NaN:pp(p=>M(p)-N,Math.max(e.tau2,1e-8))}else{let g=Fn(r/100,1),b=v=>Yt(e.yi,e.vi,e.mods,v).logLik,N=v=>2*(s.logLik-b(v))-g;if(e.tau2>0){c=N(0)<=0?0:Ao(N,0,e.tau2);let v=Math.max(e.tau2*2,1e-8),M=N(v);for(let p=0;p<80&&M<0;p++)v*=2,M=N(v);a=M>=0?Ao(N,e.tau2,v):NaN}else{let v=1e-8,M=N(v);for(let p=0;p<80&&M<0;p++)v*=2,M=N(v);a=M>=0?Ao(N,0,v):NaN}}let l=Yt(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),d=m(a),w=["tau^2","tau","I^2(%)","H^2"];return{random:{dim:[4,3],rowNames:w,colNames:["estimate","ci.lb","ci.ub"],rows:w.map((g,b)=>({estimate:u[b],"ci.lb":h[b],"ci.ub":d[b]}))},digits:{est:"4",se:"4",test:"4",pval:"4",ci:"4",var:"4",sevar:"4",fit:"4",het:"4"},"ci.null":!1,"lb.sign":"","ub.sign":"","tau2.min":"0"}}function vN(e,t={}){K(t,uN,"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=>An(u,i)),a=s.map(u=>Math.sqrt(Math.max(0,An(u,ds(e.covariance,u))))),l=e.test==="z"?le(1-r/2):Wt(1-r/2,e.k-o),f=t.predtype==="riley"?Wt(1-r/2,e.k-o-1):l,m=s.map((u,h)=>Math.sqrt(a[h]**2+e.tau2));return{pred:Rn(c),se:Rn(a),"ci.lb":Rn(c.map((u,h)=>u-l*a[h])),"ci.ub":Rn(c.map((u,h)=>u+l*a[h])),"pi.lb":Rn(c.map((u,h)=>u-f*m[h])),"pi.ub":Rn(c.map((u,h)=>u+f*m[h]))}}function yN(e,t,n={}){vt(t)&&K(t,hp,"regtest"),K(n,hp,"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(p=>Math.sqrt(p)),m=i==="sei"?f:i==="vi"?l:l.map((p,y)=>1/(y+1)),u,h,d,w,g,b;if(s==="lm"){let p=m.map((A,S)=>[1,1/A]),y=a.map((A,S)=>A/f[S]),x=NN(p,y);u=x.beta[1];let E=Math.sqrt(x.cov[0][0]*x.sigma2),R=Math.sqrt(x.cov[1][1]*x.sigma2);d=x.beta[0]/E,h=R,w=a.length-2,g=2*an(-Math.abs(d),w)}else{b=vp(a,l,{method:"REML",mods:m,intercept:!0,test:"z"}),u=b.beta.rows[0][0],h=Array.isArray(b.se)?b.se[0]:b.se;let p=Array.isArray(b.zval)?b.zval[1]:NaN,y=Array.isArray(b.pval)?b.pval[1]:NaN;d=p,w=NaN,g=y}let N=s==="lm"?Wt(1-(1-c/100)/2,w):le(1-(1-c/100)/2),v=[];s==="lm"&&(!Number.isFinite(d)||!Number.isFinite(g)||!Number.isFinite(h))&&v.push("NaNs produced");let M={model:s,predictor:i,zval:d,pval:g,dfs:w,est:u,"ci.lb":u-N*h,"ci.ub":u+N*h,warnings:v};return X("regtest",o.retFit&&b!==void 0?{...M,fit:b}:M)}function NN(e,t){let n=Array.from({length:2},(c,a)=>Array.from({length:2},(l,f)=>e.reduce((m,u,h)=>m+u[a]*u[f],0))),r=nt(n,wp(2)),o=ds(r,Array.from({length:2},(c,a)=>e.reduce((l,f,m)=>l+f[a]*t[m],0))),s=t.map((c,a)=>c-An(e[a],o)),i=t.length>2?s.reduce((c,a)=>c+a*a,0)/(t.length-2):NaN;return{beta:o,cov:r,sigma2:i}}export{yc as Anova,jv as ICC,Ew as TukeyHSD,$b as adTest,Ii as anova,Wm as ansariTest,Nw as aov,ob as aovEz,_i as aovSummary,hv as auc,pm as bartlettTest,yi as binomCI,Zd as binomDiffCI,f0 as binomDiffEquivalenceTest,e0 as binomRatioCI,Ad as binomTest,Rv as blandAltman,H0 as boschlooTest,Jg as brunnerMunzelTest,zp as chisqTest,bv as ciAuc,eg as cochranQTest,d1 as coefci,h1 as coeftest,Hu as cohenES,N0 as cohensD,bw as confint,bN as confintRma,dv as coords,Np as corTest,Rb as coxZph,yb as coxph,Qb as cronbachAlpha,gy as crr,my as cuminc,zb as cvmTest,Mv as diagnosticTests,xg as dscfAllPairsTest,Sg as dunnTest,Mg as dunnettTest,Xy as epiSsequb,Qy as epiSsequc,Zy as epiSsninfb,Jy as epiSsninfc,eN as epiSssupb,tN as epiSssupc,q0 as epsilonSquared,D1 as esH,V1 as esW1,$1 as esW2,wN as escalc,T0 as etaSquared,Hv as factanal,$o as fisherTest,wm as flignerTest,$i as friedmanTest,Cp as gTest,Rg as gamesHowellTest,kg as glhtDunnett,ic as glm,v1 as glmAnova,Hw as glmConfint,jw as glmCooksDistance,Jo as glmHatvalues,Uw as glmPredict,$w as glmResiduals,Ww as glmRstandard,cc as glmSummary,Dp as goodmanKruskalGamma,M0 as hedgesG,Xb as icc,Db as jonckheereTest,Yb as kappa2,Gb as kappamFleiss,ra as kruskalTest,Qm as ksTest,yg as kwAllPairsDunnTest,Ng as kwManyOneDunnTest,$p as lblTest,Tb as leveneTest,Wb as lillieTest,Go as lm,Fg as mantelhaenTest,Eg as manyOneUTest,R1 as mauchlyTest,X0 as mcnemarTest,Pv as mcreg,Gr as modelMatrix,bm as moodTest,iN as numDEpi,s0 as oddsRatio,ey as omega,k0 as omegaSquared,qm as onewayTest,Zr as pAdjust,lw as pairwisePropTest,iw as pairwiseTTest,cw as pairwiseWilcoxTest,Jb as partialR,Kb as pearsonTest,lN as ph2simon,_d as poissonTest,sy as polychor,iy as polyserial,kp as powerAnovaTest,rN as powerCT,sN as powerEpi,nN as powerPaired2x2,Sp as powerPropTest,Ap as powerTTest,vw as predict,wy as predictCrr,vN as predictRma,bi as propTest,Ib as propTrendTest,X1 as pwr2p2nTest,H1 as pwr2pTest,J1 as pwrAnovaTest,Z1 as pwrChisqTest,eb as pwrF2Test,G1 as pwrNormTest,Y1 as pwrPTest,Q1 as pwrRTest,B1 as pwrT2nTest,U1 as pwrTTest,gm as quadeTest,yN as regtest,vp as rma,hb as rmst2,mv as roc,yv as rocTest,yp as setWarningHandler,jb as sfTest,Ig as shapiroTest,oN as ssizeCT,aN as ssizeEpi,Pp as stuartMaxwellTest,cb as survdiff,ur as survfit,Ly as survreg,Cy as survregPredict,Oy as survregResiduals,D0 as tTOST,En as tTest,hy as timepoints,c0 as tost,V0 as tsumTOST,Ag as tukeyTest,hm as varTest,l1 as vcovCL,Zw as vcovHC,zo as wilcoxTest,xw as zTest};
|
|
22
|
+
`),X("ph2simon",{out:m,minimax:g,optimal:b,admissible:N,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:v})}var lN=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),fp=["method","mods","data","intercept","test","level","control"],mp=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],fN=["type","level"],mN=["newmods","predtype","level"],up=["model","predictor","retFit","level"],Rn=e=>e.length===1?e[0]:e,An=(e,t)=>e.reduce((n,r,o)=>n+r*t[o],0),ds=(e,t)=>e.map(n=>An(n,t)),gp=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),Ao=(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},hp=(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?Ao(e,0,n):NaN};function Yt(e,t,n,r){let o=e.length,s=n[0].length,i=t.map(F=>F+r),c=i.reduce((F,_)=>Math.min(F,_),1/0),a=i.map(F=>c/F),l=a.reduce((F,_)=>F+_,0),f=Array.from({length:s},(F,_)=>Array.from({length:s},(O,I)=>n.reduce((D,k,V)=>D+a[V]*k[_]*k[I],0))),m=Array.from({length:s},(F,_)=>n.reduce((O,I,D)=>O+a[D]*I[_]*e[D],0)),u;try{u=nt(f,gp(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=ds(u,m),d=ds(n,h),w=e.map((F,_)=>F-d[_]),g=e.every(F=>F===e[0])?0:w.reduce((F,_,O)=>F+a[O]*_*_,0),b=g/c,N=Array.from({length:o},(F,_)=>Array.from({length:o},(O,I)=>(_===I?a[_]:0)-a[_]*An(n[_],ds(u,n[I]).map(D=>D*a[I])))),v=N.reduce((F,_,O)=>F+_[O],0),M=v/c,p=N.reduce((F,_,O)=>F+_.reduce((I,D,k)=>I+D*N[k][O],0),0),y=p/c/c,x=N.map(F=>An(F,e)),E=.5*(An(x,x)-c*v),R=.5*(w.reduce((F,_,O)=>F+_*_*a[O]*a[O],0)-c*a.reduce((F,_)=>F+_,0)),A=a.reduce((F,_)=>F+_*_,0),S=.5*A,T=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(uN(f))),L=-.5*(i.reduce((F,_)=>F+Math.log(_),0)+T-s*Math.log(c)+b),C=u.map(F=>F.map(_=>_*c)),q=u.map((F,_)=>Math.sqrt(Math.max(0,F[_]))*Math.sqrt(c));return{beta:h,cov:C,se:q,fitted:d,residuals:w,qe:b,qeScaled:g,weights:a,minVariance:c,P:N,traceP:M,tracePNorm:v,traceP2:y,traceP2Norm:p,score:E,mlScore:R,mlInfo:S,logLik:L,sumWeightsNorm:l,sumWeightSquaredNorm:A}}function uN(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 ol(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 hN(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*ol(i.P,l)),u=r==="DL"?i.tracePNorm:r==="HS"?i.sumWeightsNorm:i.sumWeightsNorm*(c-1)/c;return u>0?m/u:NaN}if(r==="HE"){let m=Yt(e,Array(c).fill(1),n,0);return a>0?Math.sqrt(2*ol(m.P,l))/a:NaN}if(r==="SJ"){let m=Yt(e,Array(c).fill(1),n,0),u=m.residuals.reduce((d,w)=>d+w*w,0)/c;if(!(u>0)||a<=0)return NaN;let h=Yt(e,t,n,u);return u/a*Math.sqrt(2*ol(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 pp(e,t,n,r,o){let s=e.length,i=n[0].length,c=s-i,a=Yt(e,t,n,0);if(r==="EE"||r==="FE")return 0;if(r==="DL")return Math.max(0,(a.qeScaled-c*a.minVariance)/a.tracePNorm);if(r==="HS")return Math.max(0,(a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);if(r==="HSk")return Math.max(0,(s/(s-1)*a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);let l=Yt(e,Array(s).fill(1),n,0),f=l.residuals.reduce((b,N)=>b+N*N,0),m=Math.max(0,(f-t.reduce((b,N,v)=>b+l.P[v][v]*N,0))/Math.max(1,c));if(r==="HE")return m;if(r==="SJ"){let b=f/s,N=Yt(e,t,n,b),v=t.map(M=>b/(M+b));return Math.max(0,v.reduce((M,p,y)=>M+p*N.residuals[y]*N.residuals[y],0)/Math.max(1,c))}let u=b=>Yt(e,t,n,b).qe-c;if(r==="EB"){let b=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,Ao(u,0,b,1e-14))}if(r==="PM"){let b=o.tau2Max??Math.max(100,10*f/s);for(;u(b)>0&&b<Number.MAX_VALUE/4;)b*=2;let N=o.tol??1e-14;return Math.max(0,Ao(u,0,b,N))}let h=Math.max(0,o.tau2Init??m),d=o.maxiter??100,w=o.threshold??1e-5,g=o.stepadj??1;if(d<=1)return h;for(let b=0;b<d;b++){let N=Yt(e,t,n,h),v=r==="REML"?N.score:N.mlScore,M=r==="REML"?.5*N.traceP2Norm:N.mlInfo;if(!(M>0)||!Number.isFinite(M))return h;let p=Math.max(0,h+g*v/M);if(!Number.isFinite(p))return h;if(Math.abs(p-h)<w)return p;h=p}return h}function dp(e){if(e.length===0||e[0].length===0)return[];let t=[],n=[];for(let r=0;r<e[0].length;r++){let o=e.map(a=>a[r]),s=Math.sqrt(An(o,o)),i=o.slice();for(let a of n){let l=An(i,a);i=i.map((f,m)=>f-l*a[m])}let c=Math.sqrt(An(i,i));s>0&&c>1e-10*s&&(t.push(r),n.push(i.map(a=>a/c)))}return t}function pN(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=zn(Jn(f),r,{intercept:n});if(o=m.x,s=m.colNames.map(h=>h==="(Intercept)"?"intrcpt":h),m.frame.rows.length!==e)throw new RangeError("variable lengths differ between yi and mods");if(o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let u=dp(o);return{x:o.map(h=>u.map(d=>h[d])),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(d=>Number(h[d]))),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=dp(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 al(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))-al(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 wp(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function sl(e,t,n,r,o){let s=wp(e,t,n,r);if(s===0)return NaN;let i=e/s,c=t/s,a=n/s,l=r/s,f=De(en(i,l),en(c,a)),m=Ot(i,c),u=Ot(a,l),h=o==="RR"?_e([a,0],m):o==="OR"?en(c,a):_e(m,u),d=at(f,h);return d[0]+d[1]}function dN(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(al(t)-.5*Math.log(t)-al(t-.5))}function gN(e,t){K(t,lN,"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],d=s[1][f];if(Math.abs(h)>1)throw new RangeError("One or more correlations are > 1 or < -1.");d>3?(m=Math.atanh(h),u=1/(d-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],d=s[1][f],w=s[2][f],g=s[3][f],b=s[4][f],N=s[5][f];if(!(w>1&&N>1&&d>=0&&b>=0))throw new RangeError("invalid means, standard deviations, or sample sizes");if(e==="MD")m=h-g,u=d*d/w+b*b/N;else{let v=w+N-2,M=((w-1)*d*d+(N-1)*b*b)/v,p=(h-g)/Math.sqrt(M);m=dN(v)*p,u=(w+N)/(w*N)+m*m/(2*(w+N))}}else{let h=s[0][f],d=s[1][f],w=s[2][f],g=s[3][f],b=h,N=d,v=w,M=g,p=t.add??.5,y=t.to??"only0",x=[b,N,v,M].some(_=>_===0),E=y==="all"||y==="only0"&&x||y==="if0all"&&i;if(e!=="RD"&&E&&(b+=p,N+=p,v+=p,M+=p),e==="RD"&&E&&(b+=p,N+=p,v+=p,M+=p),t.drop00&&(h===0&&w===0||d===0&&g===0)){a.push(NaN),l.push(NaN);continue}let R=wp(b,N,v,M),A=b/R,S=N/R,T=v/R,L=M/R,C=A+S,q=T+L,F=1/R;e==="RR"?(m=Math.log1p(sl(b,N,v,M,"RR")),u=F*(S/(A*C)+L/(T*q))):e==="OR"?(m=Math.log1p(sl(b,N,v,M,"OR")),u=F*(1/A+1/S+1/T+1/L)):(m=sl(b,N,v,M,"RD"),u=F*(A*S/(C*C*C)+T*L/(q*q*q)))}a.push(m),l.push(u)}return X("escalc",{yi:a,vi:l,measure:e,warnings:n})}function bp(e,t,n={}){vt(t)&&K(t,fp,"rma"),vt(t)&&K(sn(t,"control"),mp,"rma","control"),K(n,fp,"rma"),K(sn(n,"control"),mp,"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=pN(o.length,s.mods,f,s.data),u=m.x,h=u[0].length,d=o.length,w=d-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(d<h)throw new Error("Number of parameters to be estimated is larger than the number of observations.");if(!(l>0&&l<100))throw new RangeError("level must be between 0 and 100");if(!["EE","FE","DL","HE","HS","HSk","SJ","ML","REML","EB","PM"].includes(c))throw new RangeError(`unknown method '${c}'`);if(!["z","t","knha"].includes(a))throw new RangeError(`unknown test '${a}'`);if(c==="DL"&&!Number.isFinite(Yt(o,i,u,0).qe))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");let b=Yt(o,i,u,0),N=pp(o,i,u,c,s.control??{}),v=Yt(o,i,u,N),M=s.mods===void 0?N:pp(o,i,Array.from({length:d},()=>[1]),c,s.control??{}),p=a==="knha"&&w>0?Math.max(1,v.qe/w):1,y=v.cov.map(B=>B.map(Q=>Q*p)),x=v.se.map(B=>B*Math.sqrt(p)),E=v.beta.map((B,Q)=>B/x[Q]),R=a==="z"?le(1-(1-l/100)/2):Wt(1-(1-l/100)/2,w),A=E.map(B=>2*(a==="z"?se(-Math.abs(B)):an(-Math.abs(B),w))),S=v.beta.map((B,Q)=>B-R*x[Q]),T=v.beta.map((B,Q)=>B+R*x[Q]),L=g?1:0,C=h===1?1:Math.max(1,h-L),q=0,F=1;if(h===1)q=E[0]*E[0],F=a==="z"?_t(q,1,{lowerTail:!1}):Tn(q,1,w,{lowerTail:!1});else{let B=v.beta.slice(L),Q=y.slice(L).map(re=>re.slice(L));try{q=An(B,nt(Q,B))}catch{q=NaN}F=a==="z"?_t(q,C,{lowerTail:!1}):Tn(q/C,C,w,{lowerTail:!1})}let _=b.tracePNorm,O=w>0?w*b.minVariance/_:0,I=c==="EE"||c==="FE"?b.qe>0?100*Math.max(0,(b.qe-w)/b.qe):0:N===0?0:100*N/(N+O),D=c==="EE"||c==="FE"?w>0?Math.max(1,b.qe/w):1:N===0?1:(N+O)/O,k=s.mods===void 0||h===1?null:M>0?100*Math.max(0,(M-N)/M):0,V=hN(o,i,u,c,N,v,b);return X("rma",{beta:{dim:[h,1],rowNames:h===1?m.names[0]??"intrcpt":m.names,colNames:null,rows:v.beta.map(B=>[B])},se:Rn(x),zval:Rn(E),pval:Rn(A),"ci.lb":Rn(S),"ci.ub":Rn(T),tau2:N,"se.tau2":V,QE:b.qe,QEp:w===0?1:_t(b.qe,w,{lowerTail:!1}),QM:q,QMp:F,R2:k,p:h,m:C,I2:I,H2:D,k:d,yi:o,vi:i,mods:u,intercept:g,method:c,test:a,level:l,warnings:m.warnings,covariance:y,tau2Null:M})}function wN(e,t={}){K(t,fN,"confintRma");let n=t.type??"QP",r=t.level??e.level,o=1-r/100;if(n!=="QP"&&n!=="PL")throw new RangeError("type must be 'QP' or 'PL'");let s=Yt(e.yi,e.vi,e.mods,e.tau2),i=e.k-e.mods[0].length,c=0,a=NaN;if(n==="QP"){let g=Yt(e.yi,e.vi,e.mods,0),b=On(1-o/2,i),N=On(o/2,i),v=g.qe,M=p=>Yt(e.yi,e.vi,e.mods,p).qe;c=v<=b?0:hp(p=>M(p)-b,Math.max(e.tau2,1e-8)),a=v<=N?NaN:hp(p=>M(p)-N,Math.max(e.tau2,1e-8))}else{let g=On(r/100,1),b=v=>Yt(e.yi,e.vi,e.mods,v).logLik,N=v=>2*(s.logLik-b(v))-g;if(e.tau2>0){c=N(0)<=0?0:Ao(N,0,e.tau2);let v=Math.max(e.tau2*2,1e-8),M=N(v);for(let p=0;p<80&&M<0;p++)v*=2,M=N(v);a=M>=0?Ao(N,e.tau2,v):NaN}else{let v=1e-8,M=N(v);for(let p=0;p<80&&M<0;p++)v*=2,M=N(v);a=M>=0?Ao(N,0,v):NaN}}let l=Yt(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),d=m(a),w=["tau^2","tau","I^2(%)","H^2"];return{random:{dim:[4,3],rowNames:w,colNames:["estimate","ci.lb","ci.ub"],rows:w.map((g,b)=>({estimate:u[b],"ci.lb":h[b],"ci.ub":d[b]}))},digits:{est:"4",se:"4",test:"4",pval:"4",ci:"4",var:"4",sevar:"4",fit:"4",het:"4"},"ci.null":!1,"lb.sign":"","ub.sign":"","tau2.min":"0"}}function bN(e,t={}){K(t,mN,"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=>An(u,i)),a=s.map(u=>Math.sqrt(Math.max(0,An(u,ds(e.covariance,u))))),l=e.test==="z"?le(1-r/2):Wt(1-r/2,e.k-o),f=t.predtype==="riley"?Wt(1-r/2,e.k-o-1):l,m=s.map((u,h)=>Math.sqrt(a[h]**2+e.tau2));return{pred:Rn(c),se:Rn(a),"ci.lb":Rn(c.map((u,h)=>u-l*a[h])),"ci.ub":Rn(c.map((u,h)=>u+l*a[h])),"pi.lb":Rn(c.map((u,h)=>u-f*m[h])),"pi.ub":Rn(c.map((u,h)=>u+f*m[h]))}}function vN(e,t,n={}){vt(t)&&K(t,up,"regtest"),K(n,up,"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(p=>Math.sqrt(p)),m=i==="sei"?f:i==="vi"?l:l.map((p,y)=>1/(y+1)),u,h,d,w,g,b;if(s==="lm"){let p=m.map((A,S)=>[1,1/A]),y=a.map((A,S)=>A/f[S]),x=yN(p,y);u=x.beta[1];let E=Math.sqrt(x.cov[0][0]*x.sigma2),R=Math.sqrt(x.cov[1][1]*x.sigma2);d=x.beta[0]/E,h=R,w=a.length-2,g=2*an(-Math.abs(d),w)}else{b=bp(a,l,{method:"REML",mods:m,intercept:!0,test:"z"}),u=b.beta.rows[0][0],h=Array.isArray(b.se)?b.se[0]:b.se;let p=Array.isArray(b.zval)?b.zval[1]:NaN,y=Array.isArray(b.pval)?b.pval[1]:NaN;d=p,w=NaN,g=y}let N=s==="lm"?Wt(1-(1-c/100)/2,w):le(1-(1-c/100)/2),v=[];s==="lm"&&(!Number.isFinite(d)||!Number.isFinite(g)||!Number.isFinite(h))&&v.push("NaNs produced");let M={model:s,predictor:i,zval:d,pval:g,dfs:w,est:u,"ci.lb":u-N*h,"ci.ub":u+N*h,warnings:v};return X("regtest",o.retFit&&b!==void 0?{...M,fit:b}:M)}function yN(e,t){let n=Array.from({length:2},(c,a)=>Array.from({length:2},(l,f)=>e.reduce((m,u,h)=>m+u[a]*u[f],0))),r=nt(n,gp(2)),o=ds(r,Array.from({length:2},(c,a)=>e.reduce((l,f,m)=>l+f[a]*t[m],0))),s=t.map((c,a)=>c-An(e[a],o)),i=t.length>2?s.reduce((c,a)=>c+a*a,0)/(t.length-2):NaN;return{beta:o,cov:r,sigma2:i}}export{yc as Anova,Wv as ICC,Mw as TukeyHSD,Vb as adTest,_i as anova,jm as ansariTest,yw as aov,rb as aovEz,Li as aovSummary,uv as auc,dm as bartlettTest,Ni as binomCI,Qd as binomDiffCI,l0 as binomDiffEquivalenceTest,Jd as binomRatioCI,Rd as binomTest,xv as blandAltman,U0 as boschlooTest,Zg as brunnerMunzelTest,$p as chisqTest,wv as ciAuc,J0 as cochranQTest,p1 as coefci,u1 as coeftest,Uu as cohenES,y0 as cohensD,ww as confint,wN as confintRma,pv as coords,yp as corTest,xb as coxZph,vb as coxph,Xb as cronbachAlpha,dy as crr,fy as cuminc,$b as cvmTest,Nv as diagnosticTests,Eg as dscfAllPairsTest,Ag as dunnTest,Ng as dunnettTest,Gy as epiSsequb,Xy as epiSsequc,Qy as epiSsninfb,Zy as epiSsninfc,Jy as epiSssupb,eN as epiSssupc,k0 as epsilonSquared,D1 as esH,P1 as esW1,V1 as esW2,gN as escalc,S0 as etaSquared,Uv as factanal,$o as fisherTest,bm as flignerTest,zi as friedmanTest,Lp as gTest,xg as gamesHowellTest,Tg as glhtDunnett,ic as glm,b1 as glmAnova,Uw as glmConfint,Ww as glmCooksDistance,Jo as glmHatvalues,Kw as glmPredict,Vw as glmResiduals,zw as glmRstandard,cc as glmSummary,Dp as goodmanKruskalGamma,N0 as hedgesG,Gb as icc,Db as jonckheereTest,Hb as kappa2,Yb as kappamFleiss,ra as kruskalTest,Zm as ksTest,vg as kwAllPairsDunnTest,yg as kwManyOneDunnTest,Vp as lblTest,Sb as leveneTest,zb as lillieTest,Go as lm,Fg as mantelhaenTest,Mg as manyOneUTest,x1 as mauchlyTest,G0 as mcnemarTest,Ov as mcreg,Gr as modelMatrix,vm as moodTest,aN as numDEpi,o0 as oddsRatio,Jv as omega,T0 as omegaSquared,Im as onewayTest,Jr as pAdjust,cw as pairwisePropTest,aw as pairwiseTTest,iw as pairwiseWilcoxTest,Zb as partialR,jb as pearsonTest,cN as ph2simon,Id as poissonTest,oy as polychor,ay as polyserial,Tp as powerAnovaTest,nN as powerCT,oN as powerEpi,tN as powerPaired2x2,Ap as powerPropTest,Rp as powerTTest,bw as predict,gy as predictCrr,bN as predictRma,vi as propTest,qb as propTrendTest,G1 as pwr2p2nTest,U1 as pwr2pTest,Z1 as pwrAnovaTest,Q1 as pwrChisqTest,J1 as pwrF2Test,Y1 as pwrNormTest,H1 as pwrPTest,X1 as pwrRTest,B1 as pwrT2nTest,K1 as pwrTTest,wm as quadeTest,vN as regtest,bp as rma,ub as rmst2,fv as roc,vv as rocTest,vp as setWarningHandler,Wb as sfTest,qg as shapiroTest,rN as ssizeCT,sN as ssizeEpi,Op as stuartMaxwellTest,ib as survdiff,ur as survfit,_y as survreg,Ly as survregPredict,Cy as survregResiduals,D0 as tTOST,En as tTest,uy as timepoints,i0 as tost,P0 as tsumTOST,Rg as tukeyTest,pm as varTest,c1 as vcovCL,Qw as vcovHC,zo as wilcoxTest,Ew as zTest};
|