@numero-uno-jpn/rstats 0.17.0 → 1.1.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 +117 -0
- package/README.ja.md +20 -12
- package/README.md +22 -12
- package/THIRD_PARTY_NOTICES.md +3 -2
- package/dist/chunks/{chunk-4WE3OO62.js → chunk-4KCSBTR5.js} +2 -2
- package/dist/chunks/chunk-4VRMAYPY.js +3 -0
- package/dist/chunks/{chunk-RFE7MVP7.js → chunk-7IJCUSA5.js} +2 -2
- package/dist/chunks/chunk-7KJ24LYK.js +2 -0
- package/dist/chunks/{chunk-Z37NELOS.js → chunk-DR4GQ7PW.js} +1 -1
- package/dist/chunks/chunk-DTJG2IL2.js +2 -0
- package/dist/chunks/{chunk-C7V2FH6L.js → chunk-EJYCDE4Z.js} +1 -1
- package/dist/chunks/{chunk-LV3W2F7N.js → chunk-GGJA3JRO.js} +2 -2
- package/dist/chunks/{chunk-H3WVPZDL.js → chunk-GJEKNJP7.js} +2 -2
- package/dist/chunks/{chunk-2C33MXSW.js → chunk-GRUQRNX7.js} +2 -2
- package/dist/chunks/chunk-GYGQUYEP.js +2 -0
- package/dist/chunks/chunk-HRA7PCQT.js +2 -0
- package/dist/chunks/{chunk-WM7VFXTV.js → chunk-HUWOSLKE.js} +1 -1
- package/dist/chunks/{chunk-TNMJPQPV.js → chunk-HZLSNJQM.js} +2 -2
- package/dist/chunks/{chunk-ZFQWB222.js → chunk-JNWWLNE3.js} +2 -2
- package/dist/chunks/{chunk-DXI5N2ET.js → chunk-N5BDSMXS.js} +1 -1
- package/dist/chunks/{chunk-LHUIJQHY.js → chunk-NDTJ23A6.js} +2 -2
- package/dist/chunks/{chunk-SE7V2KLO.js → chunk-OG7UORIX.js} +1 -1
- package/dist/chunks/{chunk-ONY5CISH.js → chunk-QUTJEYDX.js} +2 -2
- package/dist/chunks/chunk-U7BRXOUS.js +2 -0
- package/dist/chunks/{chunk-FY3FVOBM.js → chunk-UMLMY4XS.js} +2 -2
- package/dist/chunks/{chunk-6JJQL6HL.js → chunk-X4J6UJHN.js} +2 -2
- package/dist/chunks/{chunk-34S5SQY6.js → chunk-Y4YWLSWU.js} +2 -2
- package/dist/chunks/{chunk-5AYHCITP.js → chunk-YN5MLKDL.js} +2 -2
- package/dist/chunks/{chunk-CZI7KUO3.js → chunk-Z2HGLFZN.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions/signrank.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/mixed.d.ts +6 -0
- package/dist/entries/mixed.js +6 -1
- package/dist/entries/tests.d.ts +1 -1
- package/dist/htest/ansariTest.js +32 -34
- package/dist/htest/cor.d.ts +2 -2
- package/dist/htest/cor.js +2 -1
- package/dist/htest/core/scoreDistribution.d.ts +39 -0
- package/dist/htest/core/scoreDistribution.js +151 -0
- package/dist/htest/wilcoxTest.d.ts +23 -8
- package/dist/htest/wilcoxTest.js +521 -199
- package/dist/linalg/chol.d.ts +3 -3
- package/dist/linalg/chol.js +10 -2
- package/dist/linalg/qr.js +39 -15
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +5 -4
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +40 -5
- package/dist/models/aovStrata.js +21 -4
- package/dist/models/formula.d.ts +5 -2
- package/dist/models/formula.js +31 -8
- package/dist/models/glm.d.ts +15 -4
- package/dist/models/glm.js +13 -6
- package/dist/models/glmer.d.ts +255 -0
- package/dist/models/glmer.js +635 -0
- package/dist/models/glmerAnova.d.ts +81 -0
- package/dist/models/glmerAnova.js +139 -0
- package/dist/models/glmerDeviance.d.ts +98 -0
- package/dist/models/glmerDeviance.js +576 -0
- package/dist/models/glmerFamily.d.ts +57 -0
- package/dist/models/glmerFamily.js +236 -0
- package/dist/models/glmerGradient.d.ts +46 -0
- package/dist/models/glmerGradient.js +301 -0
- package/dist/models/glmerStructure.d.ts +45 -0
- package/dist/models/glmerStructure.js +168 -0
- package/dist/models/glmerSummary.d.ts +102 -0
- package/dist/models/glmerSummary.js +94 -0
- package/dist/models/lmer.d.ts +16 -0
- package/dist/models/lmer.js +55 -306
- package/dist/models/lmerAnova.d.ts +49 -0
- package/dist/models/lmerAnova.js +91 -40
- package/dist/models/lmerTest.d.ts +8 -1
- package/dist/models/lmerTest.js +21 -8
- package/dist/models/mixedDeviance.d.ts +28 -2
- package/dist/models/mixedDeviance.js +84 -56
- package/dist/models/mixedMinimiser.d.ts +83 -0
- package/dist/models/mixedMinimiser.js +438 -0
- package/dist/models/mixedPredict.d.ts +121 -0
- package/dist/models/mixedPredict.js +530 -0
- package/dist/models/mixedStructure.d.ts +62 -3
- package/dist/models/mixedStructure.js +74 -29
- package/dist/models/modelMatrix.d.ts +8 -1
- package/dist/models/modelMatrix.js +49 -37
- package/dist/models/survival.d.ts +4 -1
- package/dist/models/survival.js +7 -1
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +16 -16
- package/dist/special.js +2 -2
- package/dist/tests.js +13 -14
- package/llms.txt +11 -3
- package/package.json +1 -1
- package/dist/chunks/chunk-M4ITG7YR.js +0 -2
- package/dist/chunks/chunk-NSYZTS26.js +0 -2
- package/dist/chunks/chunk-THIGHOKK.js +0 -2
- package/dist/chunks/chunk-WMDYDTVQ.js +0 -2
- package/dist/chunks/chunk-XLSFJTTK.js +0 -2
package/dist/tests.js
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
/*! rStats.js
|
|
2
|
-
import{A as gn,C as qo,H as wn,I as xa,M as Ra,N as bn,O as Ta,S as Lr,T as ka,U as Qn,W as Sa,Y as Or,Z as Fr,_ as Pr,b as St,c as la,g as Tr,h as kr,i as fa,j as un,k as pn,p as qt,q as hn,u as Ma,v as ya,w as Ea}from"./chunks/chunk-THIGHOKK.js";import{a as Vr,b as Ct,c as er,d as _a,e as La,f as Lo,g as fe,h as $t,i as Zt,j as Qe,k as Pa,l as Tl,n as jr,o as nr}from"./chunks/chunk-34S5SQY6.js";import"./chunks/chunk-CZI7KUO3.js";import{b as Oa,c as Fa,d as tr,f as rr}from"./chunks/chunk-2C33MXSW.js";import{b as Io}from"./chunks/chunk-Z37NELOS.js";import"./chunks/chunk-ONY5CISH.js";import{a as Ve,b as Nn,c as Mn}from"./chunks/chunk-RFE7MVP7.js";import{a as Oo}from"./chunks/chunk-4WE3OO62.js";import{a as va,d as Na}from"./chunks/chunk-TNMJPQPV.js";import{a as Jt,b as xn,c as $r,d as zr,e as Wr}from"./chunks/chunk-WMDYDTVQ.js";import{d as we,e as At,g as Xt,h as Vt,j as Sr}from"./chunks/chunk-M4ITG7YR.js";import{C as $a,E as za,H as ct,I as Wa,c as Da,h as Va,j as Ne,l as Ca,m as yn,n as En,t as Cr,y as Ce}from"./chunks/chunk-XLSFJTTK.js";import{a as Dr,b as qa,g as Ia}from"./chunks/chunk-NSYZTS26.js";import{A as _r,B as Bt,h as ia,i as xe,l as Rr,x as Ke,y as gt}from"./chunks/chunk-LV3W2F7N.js";import{a as it}from"./chunks/chunk-H3WVPZDL.js";import{a as Rl,c as j,d as Re,e as Te,f as Zn,g as _o}from"./chunks/chunk-DXI5N2ET.js";import{a as Jn,b as vn,c as Aa,d as Fe,e as Pe}from"./chunks/chunk-C7V2FH6L.js";import{a as ge,b as Qt,c as et}from"./chunks/chunk-SE7V2KLO.js";import{b as ca,c as Yn}from"./chunks/chunk-6JJQL6HL.js";import{A as sa,C as Gn,D as aa,M as ma,q as Be,r as U,s as Q}from"./chunks/chunk-LHUIJQHY.js";import{A as da,C as ga,D as Ie,E as Ar,F as wa,G as qr,H as ba,I as se,J as ve,K as ut,L as Ye,M as dn,N as Oe,O as Ir,P as ee,Q as Ao,b as Tt,c as Ht,d as Z,e as ra,f as de,g as pe,h as at,i as Se,j as kt,k as oa,l as V,m as Dt,p as Hn,q as Xn,r as ua,s as pa,t as ha,u as q,v as De,w as Bn,x as Ae,y as qe}from"./chunks/chunk-WM7VFXTV.js";var ja=["alternative","mu","paired","varEqual","confLevel"];function Rn(e,t,n={}){if(typeof e!="string"&&Ve(t)&&V(t,ja,"tTest"),V(n,ja,"tTest"),typeof e=="string")return kl(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=fe(n.alternative),s=n.mu??0,c=n.paired??!1,i=n.varEqual??!1,a=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if($t(a),c&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(c){let[S,A]=dn(Array.from(e),Array.from(r));({d:l,k:m}=Ar(S,A))}else{let S=ee(r===void 0?[Ye(Array.from(e))]:[Ye(Array.from(e)),Ye(Array.from(r))]);m=S.k,l=S.groups[0],f=S.groups[1]}let u=q(s,-m),p,g,w,h,N,M,v,b=l.length,d=f!==void 0&&i;if(b<1||!d&&b<2)throw new Error("not enough 'x' observations");let E=Ae(l),x=b>1?qe(l):NaN;if(f===void 0){if(g=b-1,w=Math.sqrt(x/b),w===0||w<10*Number.EPSILON*Math.abs(E))throw new Error("data are essentially constant");p=(E-u)/w,M=c?"Paired t-test":"One Sample t-test",h=E,N=c?"mean difference":"mean of x",v=c?"mean difference":"mean"}else{let S=f.length;if(S<1||!i&&S<2)throw new Error("not enough 'y' observations");if(i&&b+S<3)throw new Error("not enough observations");let A=Ae(f),I=S>1?qe(f):NaN;if(M=`${i?"":"Welch"} Two Sample t-test`,h=[E,A],N=["mean of x","mean of y"],v="difference in means",i){g=b+S-2;let _=0;b>1&&(_+=(b-1)*x),S>1&&(_+=(S-1)*I),_/=g,w=Math.sqrt(_*(1/b+1/S))}else{let _=x/b,$=I/S;w=Math.sqrt(Math.sqrt(_)**2+Math.sqrt($)**2),g=qr([_,$],[b-1,S-1])}if(w===0||w<10*Number.EPSILON*Math.max(Math.abs(E),Math.abs(A)))throw new Error("data are essentially constant");p=(E-A-u)/w}let y=typeof h=="number"?h:h[0]-h[1],R=(S,A)=>{let I=u+(p+A*S)*w;return Number.isFinite(I)&&Math.max(Math.abs(u),Math.abs(p*w))<=16*(Math.abs(y)+S*w)?I:y+A*S*w},T,k;if(o==="less")T=Ke(p,g,!0,!1),k=[-1/0,R(gt(a,g,!0,!1),1)];else if(o==="greater")T=Ke(p,g,!1,!1),k=[R(gt(a,g,!0,!1),-1),1/0];else{T=2*Ke(-Math.abs(p),g,!0,!1);let S=1-a,A=S/2<1e-8?gt(S/2,g,!1,!1):gt(1-S/2,g,!0,!1);k=[R(A,-1),R(A,1)]}return k=[q(k[0],m),q(k[1],m)],h=typeof h=="number"?q(h,m):h.map(S=>q(S,m)),w=q(w,m),{method:M,alternative:o,statistic:p,statisticName:"t",parameter:g,parameterName:"df",pValue:T,confInt:k,confLevel:a,estimate:h,estimateNames:N,nullValue:s,nullValueName:v,stdErr:w,stdError:w,warnings:[]}}function kl(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=Vr(e,t,!0);if(r.kind==="one")return Rn(r.x,n);if(r.kind==="paired")return Rn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...Rn(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 Ka=["correct","p","rescaleP","simulatePValue","B"];function lt(e){let t=[0,0],n=0;for(let r of e)t=Z(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Sl(e,t,n){return Ve(t)&&V(t,Ka,"chisqTest"),V(n,Ka,"chisqTest"),Array.isArray(t)?j("chisqTest",Al(e,t,n??{})):j("chisqTest",ql(e,t??{}))}var Ya="Chi-squared approximation may be incorrect";function Al(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,p)=>{let g=t[p];!r(u)&&!r(g)&&(o.push(u),s.push(g))});let c=[...new Set(o)].sort(it),i=[...new Set(s)].sort(it);if(c.length<2||i.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let a=new Map(c.map((u,p)=>[u,p])),l=new Map(i.map((u,p)=>[u,p])),f=c.map(()=>i.map(()=>0));return o.forEach((u,p)=>f[a.get(u)][l.get(s[p])]++),{...Za(f,n),rowLevels:c,colLevels:i}}function ql(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?Fo(r[0],t):o===1?Fo(r.map(s=>s[0]),t):Za(r,t)}return Fo(Array.from(e),t)}function Ha(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 Xa(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 Ba(e,t){return Number.isNaN(e)?NaN:xe(e,t/2,2,!1,!1)}function Qa(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 Fo(e,t){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=lt(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=lt(o),h=w<1/0?0:Math.ceil(Math.log2(n)),N=h===0?o:o.map(v=>q(v,-h)),M=h===0?w:lt(N);o=N.map(v=>v/M)}else if(Math.abs(lt(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),c=s===0?e:e.map(w=>q(w,-s)),i=s===0?r:lt(c),a=o.map(w=>i*w),l=s===0?a:a.map(w=>q(w,s));Qa(l);let f=o.map((w,h)=>a[h]*(1-w)),m=q(lt(c.map((w,h)=>Oe(w-a[h],a[h]))),s),u="Chi-squared test for given probabilities",p=n-1,g;if(t.simulatePValue??!1){let w=Ha(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=Jn(w),p=NaN,g=Xa(Il(r,o,l,w),m,w)}else g=Ba(m,p);return{method:u,statistic:m,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:g,observed:e,expected:l,residuals:c.map((w,h)=>q((w-a[h])/Math.sqrt(a[h]),s/2)),stdres:c.map((w,h)=>q((w-a[h])/Math.sqrt(f[h]),s/2)),warnings:Number.isFinite(p)&&l.some(w=>w<5)?[Ya]:[]}}function Il(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=Sa(o,t);if(r*e===0)return new Float64Array(0);let c=new Float64Array(r),i=new Float64Array(o);for(let a=0;a<r;a++){i.fill(0);for(let f=0;f<e;f++)i[s()]++;let l=0;for(let f=0;f<o;f++)l+=(i[f]-n[f])**2/n[f];c[a]=l}return c}function _l(e,t,n,r){let o=e.length,s=t.length,c=e.reduce((f,m)=>f+m,0);if(c>Or)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let i=Fr(c),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){Pr(e,t,i,a);let m=0;for(let u=0;u<s;u++)for(let p=0;p<o;p++){let g=n[p][u];m+=(a[p+u*o]-g)**2/g}l[f]=m}return l}function Za(e,t){let n=t.correct??!0,r=e.length,o=e[0].length,s=e.flat();if(s.some(M=>!(M>=0&&M<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=lt(s);if(!(c>0))throw new RangeError("at least one entry of 'x' must be positive");let i=e.map(M=>lt(M)),a=e[0].map((M,v)=>lt(e.map(b=>b[v]))),l=i.map(M=>a.map(v=>Ir(M,v,c)));Qa(l.flat());let f=e.map((M,v)=>M.map((b,d)=>b-l[v][d])),m="Pearson's Chi-squared test",u,p=(r-1)*(o-1),g,w=(t.simulatePValue??!1)&&i.every(M=>M>0)&&a.every(M=>M>0),h=!w&&n&&r===2&&o===2?Math.min(.5,...f.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!w&&N.push("cannot compute simulated p-value with zero marginals"),w){if(e.some(b=>b.some(d=>!Number.isInteger(d))))throw new RangeError("the simulation needs integer counts");let M=Ha(t);m+=Jn(M),u=lt(f.flatMap((b,d)=>b.map((E,x)=>Oe(E,l[d][x])))),p=NaN;let v=_l(i.map(b=>Math.trunc(b)),a.map(b=>Math.trunc(b)),l,M);g=Xa(v,u,M)}else h>0?(m+=" with Yates' continuity correction",u=lt(f.flatMap((M,v)=>M.map((b,d)=>Oe(Math.abs(b)-h,l[v][d]))))):u=lt(f.flatMap((M,v)=>M.map((b,d)=>Oe(b,l[v][d]))));return{method:m,statistic:u,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:g??Ba(u,p),observed:e,expected:l,residuals:f.map((M,v)=>M.map((b,d)=>b/Math.sqrt(l[v][d]))),stdres:Ll(e,c,i,a,l,f),warnings:Number.isFinite(p)&&l.some(M=>M.some(v=>v<5))?[...N,Ya]:N}}function Ll(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((g,w)=>g.map((h,N)=>h/Math.sqrt(o[w][N]*(1-n[w]/t)*(1-r[N]/t))));let c=e.map(g=>Ua(g)),i=e[0].map((g,w)=>Ua(e.map(h=>h[w]))),a=Fl(c),l=Ga(c),f=Ga(i),m=l.map(g=>Se(g,a)),u=i.map(g=>Se(g,a)),p=f.map(g=>Se(g,a));return e.map((g,w)=>g.map((h,N)=>{let M=pe(c[w],u[N]),v=de([h,0],M),b=v[0]+v[1],d=pe(pe(M,m[w]),p[N]),E=d[0]+d[1];if(E>=Ol||b===0)return b/Math.sqrt(E);let x=M[0]+M[1],y=m[w][0]+m[w][1],R=p[N][0]+p[N][1];return b/Math.sqrt(x)/Math.sqrt(y)/Math.sqrt(R)}))}var Ol=2**-1022;function Ua(e){let t=[0,0];for(let n of e)t=Z(t,[n,0]);return t}function Fl(e){let t=[0,0];for(let n of e)t=Z(t,n);return t}function Ga(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=Z(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(Z(o,n[s+1])),o=Z(o,e[s]);return r}var Pl=Math.pow(Number.EPSILON,.25);function wt(e,t,n,r={}){let o={tol:r.tol??Pl,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),tr(e,t,n,o).root}function Kr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,c=n(s);for(;c!==0&&Math.sign(c)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=c,s*=2,c=n(s)}if(c===0)return Math.exp(s);let i=r<s?wt(n,r,s,{tol:2**-50,fLower:o,fUpper:c}):wt(n,s,r,{tol:2**-50,fLower:c,fUpper:o});return Math.exp(i)}var Dl=["alternative","confInt","confLevel","or","simulatePValue","B"];function Vl(e,t={}){V(t,Dl,"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(Yn));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let c=[],i=Cl(o,s);i!==null&&c.push(`'x' has been rounded to integer: ${i}`);let a="Fisher's Exact Test for Count Data";if(n===2&&r===2)return j("fisherTest",{...Kl(s,t,a),warnings:c});let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let f=t.B??2e3;if(!(Number.isInteger(f)&&f>=1&&f<2**31))throw new RangeError("'B' must be a positive integer");return j("fisherTest",{method:a+Jn(f),alternative:l,pValue:$l(s,f),warnings:c})}return j("fisherTest",{method:a,alternative:l,pValue:Ul(s),warnings:c})}function Cl(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 c=0;for(let l of r)c+=Math.abs(l)/s;let i="relative";Number.isFinite(c)&&c>n||(c=1,i="absolute");let a=0;for(let l=0;l<s;l++)a+=Math.abs(r[l]-o[l])/(s*c);return Number.isNaN(a)||a>n?`Mean ${i} difference: ${Aa(a)}`:null}function $l(e,t){let n=e.filter(h=>h.some(N=>N>0)),r=e[0].map((h,N)=>N).filter(h=>e.some(N=>N[h]>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(h=>r.map(N=>h[N])),s=o.map(h=>h.reduce((N,M)=>N+M,0)),c=o[0].map((h,N)=>o.reduce((M,v)=>M+v[N],0)),i=s.length,a=c.length,l=[0,0];for(let h of o.flat())l=Z(l,[-Na(h),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((h,N)=>h+N,0);if(m>Or)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Fr(m),p=new Int32Array(i*a),g=0,w=Math.trunc(t);for(let h=0;h<w;h++){Pr(s,c,u,p);let N=0;for(let M=0;M<p.length;M++)N-=u[p[M]];N<=f&&g++}return Math.max(0,Math.min(1,(1+g)/(w+1)))}var zl=1+1e-7,Wl=16,jl=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=Z(r,[e[o],0]));return r[0]}var Vo=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 c=this.lo;c<=this.hi;c++){let i=c-this.lo;i%Wl===0||!(s>0&&s<1/0)?(o=gn(c,t,n,r,!0),s=1,this.logCentral[i]=o):this.logCentral[i]=o+Math.log(s),s*=(t-c)*(r-c)/((c+1)*(n-r+c+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),s=-1/0;for(let i=0;i<n;i++){let a=this.logCentral[i]+(this.lo+i)*r;o[i]=a,a>s&&(s=a)}for(let i=0;i<n;i++){let a=o[i]-s;o[i]=a<-746?0:Math.exp(a)}let c=Po(o,0,n-1);for(let i=0;i<n;i++)o[i]!==0&&(o[i]=o[i]/c);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=Z(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?qo(t-1,this.m,this.n,this.k,!1,!1):qo(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let c=n===0?this.lo:this.hi;return(r?t<=c:t>=c)?1:0}let o=this.density(n),s=t-this.lo;return r?Po(o,s,o.length-1):Po(o,0,s)}};function Do(e,t){return Kr(e,t)}function Kl(e,t,n){let r=fe(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[[c,i],[a,l]]=e,f=c,m=Math.min(c,l)+Math.min(i,a)+1;if(m>jl)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Vo(c+a,i+l,c+i),{lo:p,hi:g}=u,w;if(r==="less")w=u.tail(f,o,!1);else if(r==="greater")w=u.tail(f,o,!0);else if(o===0)w=f===p?1:0;else if(o===1/0)w=f===g?1:0;else{let d=u.density(o),E=d[f-p]*zl,x=[0,0];for(let y of d)y<=E&&(x=Z(x,[y,0]));w=x[0]}let h;if(f===p)h=0;else if(f===g)h=1/0;else{let d=u.mean(1);h=d===f?1:Do(E=>u.mean(E)-f,d>f)}let N={method:n,alternative:r,pValue:w,estimate:h,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let M=d=>{if(f===g)return 1/0;let E=u.tail(f,1,!1);return E===d?1:Do(x=>u.tail(f,x,!1)-d,E<d)},v=d=>{if(f===p)return 0;let E=u.tail(f,1,!0);return E===d?1:Do(x=>u.tail(f,x,!0)-d,E>d)},b;if(r==="less")b=[0,M(1-s)];else if(r==="greater")b=[v(1-s),1/0];else{let d=(1-s)/2;b=[v(d),M(d)]}return{...N,confInt:b,confLevel:s}}var Ur=[[0,0]];function bt(e){for(let t=Ur.length;t<=e;t++)Ur.push(Z(Ur[t-1],oa([t,0])));return Ur[e]}function Gr(e,t,n,r){let o=e+n,s=o-e,c=e-(o-s)+(n-s)+t+r,i=o+c;return[i,c-(i-o)]}var Co=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,c=s-this.hi,i=this.hi-(s-c)+(o-c)+this.lo;this.hi=s+i,this.lo=i-(this.hi-s)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function Ul(e){let t=e.filter(v=>v.some(b=>b>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,b)=>t.some(d=>d[b]>0));if(t=t.map(v=>v.filter((b,d)=>n[d])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,b)=>t.map(d=>d[b])));let r=t.length,o=t.map(v=>v.reduce((b,d)=>b+d,0)),s=t[0].map((v,b)=>t.reduce((d,E)=>d+E[b],0)).sort((v,b)=>b-v),c=s.length,i=o.reduce((v,b)=>v+b,0),a=ra(bt(i));for(let v of o)a=Z(a,bt(v));for(let v of s)a=Z(a,bt(v));let l=[0,0];for(let v of t.flat())l=de(l,bt(v));let f=Z(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,p]=f,g=Array.from({length:c},()=>new Map),w=(v,b)=>{let d=b.join(","),E=g[v].get(d);if(E!==void 0)return E;let x=Z(a,bt(b.reduce((A,I)=>A+I,0)));for(let A of b)x=de(x,bt(A));for(let A=v;A<c;A++)x=de(x,bt(s[A]));let y={R:b,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(g[v].set(d,y),v===c-1){let A=[0,0];for(let I of b)A=de(A,bt(I));return y.max=y.min=A[0],y.edges.push({to:y,hi:A[0],lo:A[1],mult:1}),y}let R=new Map,T=new Array(r+1).fill(0);for(let A=r-1;A>=0;A--)T[A]=T[A+1]+b[A];let k=new Array(r).fill(0),S=(A,I,_)=>{if(A===r-1){k[A]=I;let C=de(_,bt(I)),W=b.map((X,te)=>X-k[te]).sort((X,te)=>X-te),F=w(v+1,W),L=`${W.join(",")}|${C[0]}`,z=R.get(L);z===void 0?R.set(L,{to:F,hi:C[0],lo:C[1],mult:1}):z.mult++;return}let $=Math.max(0,I-T[A+1]),P=Math.min(b[A],I);for(let C=$;C<=P;C++)k[A]=C,S(A+1,I-C,de(_,bt(C)))};if(S(0,s[v],[0,0]),y.edges=[...R.values()],y.max=ge(y.edges.map(A=>A.hi+A.to.max)),y.min=et(y.edges.map(A=>A.hi+A.to.min)),v===c-2){let A=y.edges.map(F=>{let L=F.to.edges[0];return{v:Z([F.hi,F.lo],[L.hi,L.lo]),mult:F.mult}});A.sort((F,L)=>F.v[0]-L.v[0]||F.v[1]-L.v[1]);let I=A.length,_=A[I-1].v[0],$=new Float64Array(I),P=new Float64Array(I),C=new Float64Array(I),W=[0,0];A.forEach(({v:F,mult:L},z)=>{$[z]=F[0],P[z]=F[1];let X=de(F,[_,0]);W=Z(W,[L*Math.exp(X[0])*(1+X[1]),0]),C[z]=W[0]}),y.dist={hi:$,lo:P,cum:C,vmax:_,kv:Z(a,[_,0])}}return y},h=w(0,[...o].sort((v,b)=>v-b)),N=new Co,M=(v,b,d,E)=>{if(b+v.max<=u-m){let[S,A]=Gr(v.KZ[0],v.KZ[1],b,d);return N.add(S,A,E),!0}if(b+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[y,R]=Gr(u,p,-b,-d),T=0,k=x.hi.length;for(;T<k;){let S=T+k>>>1,A=x.hi[S];A<y||A===y&&x.lo[S]<=R?T=S+1:k=S}if(T>0){let[S,A]=Gr(x.kv[0],x.kv[1],b,d);N.add(S,A,E*x.cum[T-1])}return!0};M(h,0,0,1)||(h.pastHi.push(0),h.pastLo.push(0),h.count.push(1));for(let v=0;v<c-2;v++)for(let b of g[v].values()){let{pastHi:d,pastLo:E,count:x}=b;if(x.length!==0){for(let{to:y,hi:R,lo:T,mult:k}of b.edges)for(let S=0;S<x.length;S++){let[A,I]=Gr(d[S],E[S],R,T),_=x[S]*k;if(M(y,A,I,_))continue;let $=y.index.get(A);$===void 0?(y.index.set(A,y.count.length),y.pastHi.push(A),y.pastLo.push(I),y.count.push(_)):y.count[$]+=_}b.pastHi=b.pastLo=b.count=[],b.index.clear()}}return Math.min(1,N.value())}var Ja=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function or(e,t,n={}){if(typeof e!="string"&&Ve(t)&&V(t,Ja,"wilcoxTest"),V(n,Ja,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let w=Vr(e,t,!0);return w.kind==="one"?or(w.x,n):or(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=fe(n.alternative),s=n.mu??0,c=n.paired??!1,i=n.exact??null,a=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(c&&r===void 0)throw new TypeError("y is required when paired is true");let u,p;if(c){let[w,h]=dn(Array.from(e),Array.from(r));u=Ye(w.map((N,M)=>N-h[M]))}else u=Ye(Array.from(e)),r!==void 0&&(p=Ye(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(p!==void 0&&p.length<1)throw new Error("not enough (non-missing) 'y' observations");let g={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new Te};return j("wilcoxTest",p===void 0?Yl(u,i,c,g):Ql(u,p,i,g))}function Yr(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function ni(e,t){return t==="less"?U(e):t==="greater"?U(e,{lowerTail:!1}):2*Math.min(U(e),U(e,{lowerTail:!1}))}function Gl(e){if(!(e>0)||!Number.isFinite(e))return e;let n=1-Math.floor(Math.log10(e)),r=n>=0?e*10**n:e/10**-n,o=Math.round(r);return o-r===.5&&o%2!==0&&(o-=1),n>=0?o/10**n:o*10**-n}function $o(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:Ae([e[n-1],e[n]])}var zo=(e,t)=>e-t;function ri(e){let t=e.length,n=ve(e.map(Math.abs)),r=0;for(let c=0;c<t;c++)e[c]>0&&(r+=n[c]);let o=0;for(let c of ut(n))o+=c*c*c-c;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,s]}function Yl(e,t,n,r){let{alternative:o,mu:s,correct:c}=r,i=e.map(y=>y-s),a=i.some((y,R)=>!Number.isFinite(y)&&Number.isFinite(e[R]));a&&(i=e.map(y=>y/2-s/2));let l=i.some(y=>y===0);l&&(i=i.filter(y=>y!==0));let f=i.length;if(f===0)throw new RangeError("no non-zero differences for the signed rank test");let m=t??f<50,u=ve(i.map(Math.abs)),p=ut(u).length>0,[g,w,h]=ri(i),N,M,v=m&&!p&&!l;if(v){M="Wilcoxon signed rank exact test";let y=f*(f+1)/2;if(o==="less")N=wn(g,f,!0,!1);else if(o==="greater")N=wn(g-1,f,!1,!1);else{let R=g>y/2?wn(g-1,f,!1,!1):wn(g,f,!0,!1);N=Math.min(1,2*R)}}else M=`Wilcoxon signed rank test${c?" with continuity correction":""}`,N=ni((w-Yr(w,o,c))/h,o);let b={method:M,alternative:o,statistic:g,statisticName:"V",pValue:N,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list},d=()=>{m&&p&&(r.warnings.add("cannot compute exact p-value with ties"),r.confInt&&r.warnings.add("cannot compute exact confidence interval with ties")),m&&l&&(r.warnings.add("cannot compute exact p-value with zeroes"),r.confInt&&r.warnings.add("cannot compute exact confidence interval with zeroes"))};if(!r.confInt)return d(),b;let E=a?e.filter(y=>y!==s):i.map(y=>y+s);if(!a){let y=e.filter(T=>T!==s),R=0;for(let T of y)R=Math.max(R,Math.abs(T));E.some((T,k)=>Math.abs(T-y[k])>2**-26*R)&&(E=y)}let x=v?Hl(E,r):Xl(E,r);return d(),{...b,...x}}function Hr(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function ei(e,t,n){let r={confInt:Hr(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function oi(e,t,n){let r=e(t==="two.sided"?n/2:n);return r===0?1:r}function si(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Hl(e,t){let{alternative:n,confLevel:r}=t,o=e.length,s=[];for(let u=0;u<o;u++)for(let p=u;p<o;p++)s.push(Qt(e[u],e[p]));s.sort(zo);let c=s.length,i=1-r,a=oi(u=>xa(u,o,!0,!1),n,i),l=wn(a-1,o,!0,!1),f=n==="two.sided"?2*l:l,m=f-i>i/2;return m&&t.warnings.add("requested conf.level not achievable"),{confInt:Hr(n,s[a-1],s[c-a]),confLevel:m?1-Gl(f):r,estimate:$o(s),estimateNames:"(pseudo)median"}}function Xl(e,t){let{alternative:n,correct:r,confLevel:o,tolRoot:s}=t,c=e.length;if(c===0)return ei(n,NaN);let i=et(e),a=ge(e);if(!Number.isFinite(i)||!Number.isFinite(a)||c===1||i===a)return Number.isFinite(i)&&i===a&&t.warnings.add("cannot compute confidence interval when all observations are zero or tied"),ei(n,Qt(i,a),"midrange");let l=(v,b)=>{let[,d,E]=ri(e.map(x=>x-v).filter(x=>x!==0));return(d-Yr(d,n,b))/E},f=l(i,r),m=l(a,r),u=v=>-Q(v),p=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>Q(v/2)):!(f<u(v/2))&&!(m>Q(v/2)),g=1-o,w=!1;for(;!p(g)&&g<1;)g*=2,w=!0;w&&t.warnings.add("requested conf.level not achievable"),si(s);let h=v=>wt(b=>l(b,r)-v,i,a,{tol:s,fLower:f-v,fUpper:m-v}),N;if(g>=1){let v=$o([...e].sort(zo));N=Hr(n,v,v)}else n==="greater"?N=[h(u(g)),1/0]:n==="less"?N=[-1/0,h(Q(g))]:N=[h(u(g/2)),h(Q(g/2))];let M=wt(v=>l(v,!1),i,a,{tol:s,fLower:l(i,!1),fUpper:l(a,!1)});return{confInt:N,confLevel:w?1-Math.min(1,g):o,estimate:M,estimateNames:"(pseudo)median"}}function ai(e,t){let n=e.length,r=t.length,o=ve([...e,...t]),s=0;for(let f=0;f<n;f++)s+=o[f];let c=s-n*(n+1)/2,i=0;for(let f of ut(o))i+=f*f*f-f;let a=n+r,l=Math.sqrt(n*r/12*(a+1-i/(a*(a-1))));return[c,c-n*r/2,l]}function Bl(e,t){let n=e.length,r=t.length,o=n+r,s=(p,g)=>{let w=p===0||!Number.isFinite(p)||!Number.isFinite(g)?g:g/2;return p<w?-1:p>w?1:0},c=(p,g)=>p<n&&g<n?e[p]<e[g]?-1:e[p]>e[g]?1:0:p>=n&&g>=n?t[p-n]<t[g-n]?-1:t[p-n]>t[g-n]?1:0:p<n?s(e[p],t[g-n]):-s(e[g],t[p-n]),i=Array.from({length:o},(p,g)=>g);i.sort((p,g)=>c(p,g)||p-g);let a=0,l=0,f=0;for(;f<o;){let p=f;for(;p+1<o&&c(i[p+1],i[f])===0;)p++;let g=(f+p+2)/2;for(let h=f;h<=p;h++)i[h]<n&&(a+=g);let w=p-f+1;l+=w*w*w-w,f=p+1}let m=a-n*(n+1)/2,u=Math.sqrt(n*r/12*(o+1-l/(o*(o-1))));return[m,m-n*r/2,u,l>0]}function Ql(e,t,n,r){let{alternative:o,mu:s,correct:c}=r,i=e.length,a=t.length,l=e.map(d=>d-s),f=n??(i<50&&a<50),m=l.some((d,E)=>!Number.isFinite(d)&&Number.isFinite(e[E])),u,p,g,w;m?(l=e.map(d=>d/2-s/2),[p,g,w,u]=Bl(l,t)):(u=ut([...l,...t]).length>0,[p,g,w]=ai(l,t));let h,N,M=f&&!u;M?(N="Wilcoxon rank sum exact test",h=ti(()=>{if(o==="less")return bn(p,i,a,!0,!1);if(o==="greater")return bn(p-1,i,a,!1,!1);let d=p>i*a/2?bn(p-1,i,a,!1,!1):bn(p,i,a,!0,!1);return Math.min(1,2*d)})):(N=`Wilcoxon rank sum test${c?" with continuity correction":""}`,h=!m&&l.every(E=>E===t[0])&&t.every(E=>E===t[0])?1:ni((g-Yr(g,o,c))/w,o));let v={method:N,alternative:o,statistic:p,statisticName:"W",pValue:h,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list},b=r.confInt?M?ti(()=>Zl(e,t,r)):Jl(e,t,r):{};return f&&u&&(r.warnings.add("cannot compute exact p-value with ties"),r.confInt&&r.warnings.add("cannot compute exact confidence intervals with ties")),{...v,...b}}function ti(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Zl(e,t,n){let{alternative:r,confLevel:o}=n,s=e.length,c=t.length,i=[];for(let g of e)for(let w of t)i.push(g-w);i.sort(zo);let a=i.length,l=1-o,f=oi(g=>Ta(g,s,c,!0,!1),r,l),m=bn(f-1,s,c,!0,!1),u=r==="two.sided"?2*m:m,p=u-l>l/2;return p&&n.warnings.add("Requested conf.level not achievable"),{confInt:Hr(r,i[f-1],i[a-f]),confLevel:p?1-u:o,estimate:$o(i),estimateNames:"difference in location"}}function Jl(e,t,n){let{alternative:r,correct:o,confLevel:s,tolRoot:c}=n,i=et(e)-ge(t),a=ge(e)-et(t);if(!Number.isFinite(i)||!Number.isFinite(a))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(p,g)=>{let[,w,h]=ai(e.map(N=>N-p),t);return(w-Yr(w,r,g))/h},f=(p,g)=>{let w=l(i,g)-p,h=l(a,g)-p;if(Number.isNaN(w)||Number.isNaN(h))throw new Error("cannot compute the confidence interval because all observations are tied");return w<=0?i:h>=0?a:(si(c),wt(N=>l(N,g)-p,i,a,{tol:c,fLower:w,fUpper:h}))},m=1-s,u;return r==="greater"?u=[f(-Q(m),o),1/0]:r==="less"?u=[-1/0,f(Q(m),o)]:u=[f(-Q(m/2),o),f(Q(m/2),o)],{confInt:u,confLevel:s,estimate:f(0,!1),estimateNames:"difference in location"}}var ef=["method","n"],ii=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function tf(e){if(ii.includes(e))return e;let t=ii.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Tn(e,t={}){V(t,ef,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=tf(n.method??"holm"),o=Array.from(e),s=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||s.push(m);let c=s.length,i=n.n??c;if(!(i>=c))throw new RangeError("n must be at least the number of non-NaN p-values");if(i===1/0)throw new RangeError("n must be finite");if(i<=1||c===0)return o;let a=s.map(m=>o[m]),l=li(a,r,i),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Xr(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 li(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=Xr(e,!1),c=-1/0;return s.forEach((i,a)=>{c=Math.max(c,(n-a)*e[i]),o[i]=Math.min(1,c)}),o}case"hochberg":{let s=Xr(e,!0),c=1/0;return s.forEach((i,a)=>{let l=r-a;c=Math.min(c,(n-l+1)*e[i]),o[i]=Math.min(1,c)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=ua(),u=Math.floor(n);if(u<=1e6){for(let p=1;p<=u;p++)pa(m,1/p,u);s=ha(m)}else{let p=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*p)+1/(120*p*p)}}let c=s*n,i=Number.isFinite(c)?1:1024,a=Number.isFinite(c)?c:s*(n/1024),l=Xr(e,!0),f=1/0;return l.forEach((m,u)=>{let p=r-u;f=Math.min(f,a/p*e[m]*i),o[m]=Math.min(1,f)}),o}case"hommel":return n===2?li(e,"hochberg",n):nf(e,n)}}var ci=2**16;function nf(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>ci)throw new RangeError(`n must be at most ${ci} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Xr(r,!1),s=o.map(a=>r[a]),c=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++)c[f]=Math.max(c[f],l);for(let f=0;f<t-a;f++)c[f]=Math.max(c[f],Math.min(a*s[f],l))}let i=new Array(n);return o.forEach((a,l)=>{a<n&&(i[a]=c[l])}),i}var fi=["p","alternative","confLevel"],rf=1+1e-7;function Wo(e,t,n){let r=e-1;return e===0||e-r===1&&Tt(t,-r)[1]===0?hn(r,t,n,!1,!1):hn(e,t,n,!1,!1)+qt(e,t,n,!1)}function of(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=qt(e,t,n,!1)*rf,s=a=>qt(a,t,n,!1)<=o;if(e<r){let a=Tr(s,Math.ceil(r),t),l=a>t?0:Wo(a,t,n);return Math.min(1,hn(e,t,n,!0,!1)+l)}let c=kr(s,0,Math.floor(r)),i=c<0?0:hn(c,t,n,!0,!1);return Math.min(1,Wo(e,t,n)+i)}function sf(e,t,n={}){V(t,fi,"binomTest"),V(n,fi,"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),!Zt(e[0])||!Zt(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(!Zt(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Zt(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return jo(r,o,n)}function jo(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=fe(n.alternative),s=n.confLevel??.95;Qe(s);let c;o==="less"?c=hn(e,t,r,!0,!1):o==="greater"?c=Wo(e,t,r):c=of(e,t,r);let i=f=>e===0?0:Sr(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Sr(f,e+1,t-e,!1,!1):Sr(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,a(1-s)];else if(o==="greater")l=[i(1-s),1];else{let f=(1-s)/2;l=[i(f),a(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:c,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var af=["T","r","alternative","confLevel"],cf=1+1e-7;function lf(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=un(e,t,!1)*cf,r=c=>un(c,t,!1)<=n,o=Math.floor(t);if(e<t){let c=Tr(r,Math.max(e+1,o),1/0);return Math.min(1,pn(e,t,!0,!1)+Ko(c,t))}let s=kr(r,0,Math.min(e-1,o));return Math.min(1,pn(s,t,!0,!1)+Ko(e,t))}var sr=2**-1022;function Ko(e,t){return e-(e-1)===1?pn(e-1,t,!1,!1):pn(e,t,!1,!1)+un(e,t,!1)}function en(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 ff(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=sr&&(s!==0||e===0||t===0))return s;let c=e*t,i=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(c)&&Number.isFinite(i))return c/i;let[a,l]=en(e),[f,m]=en(t),[u,p]=en(n),[g,w]=en(r),h=f*g,N=p-m-w,M=N>=0?u/(u+q(h,-N)):u/(q(u,N)+h);return q(a*f*M,l+m+Math.min(N,0))}function mf(e,t,n,r){let o=e/n,s=t/r,c=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=sr&&(o===0||o>=sr)?c:e===0?0:e/t*(r/n)}function uf(e,t={}){V(t,af,"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(Zt))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,c=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(c.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!c.every(N=>Number.isFinite(N)&&N>0))throw new RangeError("'T' must be positive");let i=t.r??1;if(!(Number.isFinite(i)&&i>=0))throw new RangeError("'r' must be a single positive number");let a=fe(t.alternative),l=t.confLevel??.95;if(o===2?Qe(l):$t(l),o===2){let[N,M]=r,[v,b]=c,d=N+M;if(d===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let E=i*v,x=E+b,y=Number.isFinite(x)&&x>=sr&&(E===0||E>=sr)?E/x:1/(1+b/v/i),R=jo(Math.round(N),Math.round(N)+Math.round(M),{p:y,alternative:a,confLevel:l}),T=b/v,k=S=>{if(S===0)return 0;let A=S/(1-S);if(Number.isFinite(T)||A===1/0)return A*T;let[I,_]=en(A),[$,P]=en(v),[C,W]=en(b);return q(I*C/$,_+W-P)};return{method:"Comparison of Poisson rates",alternative:a,statistic:N,statisticName:"count1",parameter:ff(d,i,v,b),parameterName:"expected count1",pValue:R.pValue,confInt:[k(R.confInt[0]),k(R.confInt[1])],confLevel:l,estimate:mf(N,M,v,b),estimateNames:"rate ratio",nullValue:i,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=c[0],u=i*m,p;a==="less"?p=pn(f,u,!0,!1):a==="greater"?p=Ko(f,u):p=lf(f,u);let g=N=>f===0?0:Rr(N,f,1,!0,!1),w=N=>N<1e-8?Rr(N,f+1,1,!1,!1):Rr(1-N,f+1,1,!0,!1),h;if(a==="less")h=[0,w(1-l)];else if(a==="greater")h=[g(1-l),1/0];else{let N=(1-l)/2;h=[g(N),w(N)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:p,confInt:[h[0]/m,h[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:i,nullValueName:"event rate",warnings:[]}}var mi=["p","alternative","confLevel","correct"],pf="Chi-squared approximation may be incorrect";function Uo(e,t,n={}){Ve(t)&&V(t,mi,"propTest"),V(n,mi,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let k=e;if(k.some(S=>S.length!==2))throw new RangeError("'x' must have 2 columns");r=k.map(S=>S[0]),o=k.map(S=>S[0]+S[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let c=r.length,i=[];for(let k=0;k<c;k++)!Number.isNaN(r[k])&&!Number.isNaN(o[k])&&i.push(k);let a=i.length;if(a<1)throw new RangeError("not enough data");if(r=i.map(k=>r[k]),o=i.map(k=>o[k]),o.some(k=>!(k>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(k=>k===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(k=>!(k>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((k,S)=>k>o[S]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let k=typeof s.p=="number"?[s.p]:Array.from(s.p);if(k.length!==c)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=i.map(S=>k[S]),l.some(S=>!(S>0&&S<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;Qe(m);let u=fe(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let p=r.map((k,S)=>k/o[S]),g=a===2?p[0]-p[1]:0,w=a===2?1/o[0]+1/o[1]:0,h=(s.correct??!0)&&a<=2?.5:0,N=!1;if(h>0&&a===1){let k=Math.abs(r[0]-o[0]*l[0]);k<=h&&([h,N]=[k,!0])}else if(h>0&&a===2&&!f){let k=Math.abs(g)/w;k<=h&&([h,N]=[k,!0])}let M=hf(r,o),v=o.some((k,S)=>{let A=l?.[S]??M;return k*A<5||k*(1-A)<5}),b=0;if(!N)for(let k=0;k<a;k++){let S=l?.[k]??M,A=o[k]*S,I=o[k]*(1-S);b+=Oe(Math.abs(r[k]-A)-h,A),b+=Oe(Math.abs(o[k]-r[k]-I)-h,I)}let d=l!==void 0?a:a-1,E;if(u==="two.sided")E=xe(b,d/2,2,!1,!1);else{let k=Math.sign(a===1?p[0]-l[0]:g);E=U(k*Math.sqrt(b),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let k=(1-m)/2,S=u==="two.sided"&&k<1e-8?Q(k,{lowerTail:!1}):Q(u==="two.sided"?(1+m)/2:m);if(a===1){let A=o[0],I=S*S,_=(F,L)=>(F+I/(2*A)+L*S*Math.sqrt(F*(1-F)/A+I/(4*A*A)))/(1+I/A),$=p[0]+h/A,P=p[0]-h/A,C=Math.min(1,Math.max(0,$>=1?1:_($,1))),W=Math.min(1,Math.max(0,P<=0?0:_(P,-1)));x=u==="two.sided"?[W,C]:u==="greater"?[W,1]:[0,C]}else{let A=Math.sqrt(p.reduce((P,C,W)=>P+C*(1-C)/o[W],0)),I=S*A+h*w,_=Math.max(-1,g-I),$=Math.min(1,g+I);x=u==="two.sided"?[_,$]:u==="greater"?[_,1]:[-1,$]}}let y=a===1?"p":i.map(k=>`prop ${k+1}`),T={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${h!==0?"with":"without"} continuity correction`,alternative:u,statistic:b,statisticName:"X-squared",parameter:d,parameterName:"df",pValue:E,estimate:a===1?p[0]:p,estimateNames:y,warnings:v?[pf]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=y),x!==void 0&&(T.confInt=x,T.confLevel=m),j("propTest",T)}function hf(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+q(s,-r),0)/t.reduce((o,s)=>o+q(s,-r),0)}function kn(e){let t=Fe(e);return t.some(n=>typeof n=="string")?t.map(n=>typeof n=="number"&&!Number.isNaN(n)?Pe(n):n):t}function ar(e,t){return typeof e=="number"&&typeof t=="number"?e-t:it(String(e),String(t))}function zt(e,t,n="drop"){if(t===void 0){let i=e;if(i.length>0&&typeof i[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let a=i.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=kn(t),s=new Map;for(let i=0;i<r.length;i++){let a=r[i],l=o[i];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 c=Array.from(s.keys()).sort(ar);return{labels:c.map(Pe),groups:c.map(i=>s.get(i))}}function tn(e,t,n="drop"){return zt(e,t,n).groups}function Br(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:Qt(t[n/2-1],t[n/2])}function Go(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 ui=["correct"];function df(e,t,n={}){Ve(t)&&V(t,ui,"mcnemarTest"),V(n,ui,"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=gf(e,t)}let o=n.correct??!0,s=r.length,c,i=o&&s===2&&r[0][1]!==r[1][0];if(i){let l=r[0][1],f=r[1][0];c=pi(Math.max(0,Math.abs(l-f)-1),l,f)}else{c=0;let l=0;for(let f=0;f<s;f++)for(let m=f+1;m<s;m++){let u=r[f][m],p=r[m][f];u+p!==0&&(c+=pi(u-p,u,p),l++)}if(l===0)throw new RangeError("the table has no discordant pairs")}let a=s*(s-1)/2;return{method:i?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:c,statisticName:"McNemar's chi-squared",parameter:a,parameterName:"df",pValue:xe(c,a/2,2,!1,!1),warnings:[]}}function pi(e,t,n){let r=t+n;return r<1/0?Oe(e,r):Oe(q(e,-520),q(t,-1040)+q(n,-1040))}function gf(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=kn(e),o=kn(t);for(let m=0;m<e.length;m++){let u=r[m],p=o[m];typeof u=="number"&&Number.isNaN(u)||typeof p=="number"&&Number.isNaN(p)||n.push([u,p])}let s=m=>Array.from(new Set(m)).sort(ar),c=s(n.map(m=>m[0])),i=s(n.map(m=>m[1]));if(c.length<2||c.length!==i.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let a=new Map(c.map((m,u)=>[m,u])),l=new Map(i.map((m,u)=>[m,u])),f=c.map(()=>new Array(c.length).fill(0));for(let[m,u]of n)f[a.get(m)][l.get(u)]++;return f}var wf=["ratio","alternative","confLevel"],Yo=2**-1022;function hi(e,t,n={}){if(V(n,wf,"varTest"),typeof e=="string"){let v=Ct(e,t);return hi(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 c=fe(n.alternative),i=n.confLevel??.95;Qe(i);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]=Ie([r,o]).variance,u=0;if(!(f>=Yo&&m>=Yo)){let v=Ie([r]),b=Ie([o]);f=v.variance[0],m=b.variance[0],u=2*(v.k-b.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let p=u===0?f/m:De(f/m,u),g=p/s,w=we(g,a,l,!0,!1),h=we(g,a,l,!1,!1),N,M;if(c==="less")N=w,M=[0,p/At(1-i,a,l,!0,!1)];else if(c==="greater")N=h,M=[p/At(i,a,l,!0,!1),1/0];else{N=Math.min(1,2*Math.min(w,h));let v=(1-i)/2,b=v<1e-8?At(v,a,l,!1,!1):At(1-v,a,l,!0,!1);M=[p/b,p/At(v,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:c,statistic:g,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:N,confInt:M,confLevel:i,estimate:p,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function di(e,t){if(typeof e=="string"){let g=er(e,t);return di(_a(g.values,g.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(g=>Array.from(g).filter(Number.isFinite));if(o.some(g=>g.length<2))throw new RangeError("there must be at least 2 observations in each group");let s=o.map(g=>g.length-1),c=Ie(o).variance,i=0,a=0,l=0,f=0;for(let g=0;g<r;g++)i+=s[g],a+=s[g]*c[g],l+=s[g]*Math.log(c[g]),f+=1/s[g];a/=i;let m=i*Math.log(a)-l;if(c.some(g=>g<Yo)){let g=o.map(b=>{let d=Ie([b]);return{w:d.variance[0],e:d.k}}),w=Math.max(...g.map(b=>b.e)),h=g.map(b=>Math.log(b.w)+2*(b.e-w)*Math.LN2),N=h.map((b,d)=>Math.log(s[d])+b),M=Math.max(...N),v=M+Math.log(N.reduce((b,d)=>b+Math.exp(d-M),0))-Math.log(i);m=i*v-h.reduce((b,d,E)=>b+s[E]*d,0)}let u=m/(1+(f-1/i)/(3*(r-1))),p=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:u,statisticName:"Bartlett's K-squared",parameter:p,parameterName:"df",pValue:xe(u,p/2,2,!1,!1),warnings:[]}}var bf=["alternative"];function Ho(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:xe(n,r/2,2,!1,!1),warnings:[]}}function gi(e){let t=0;for(let n of ut(e))t+=n*n*n-n;return t}function Qr(e,t){if(typeof e=="string"){let l=er(e,t);return Qr(l.values,l.group.codes)}let n=tn(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,c=ve(o),i=0,a=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=c[a+m];a+=l.length,i+=f*f/l.length}return i=(12*i/(s*(s+1))-3*(s+1))/(1-gi(o)/(s*s*s-s)),Ho("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",i,r-1)}function Xo(e,t){if(typeof e=="string")return Xo(Lo(e,t));let n=Go(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),c=0;for(let l of n){let f=ve(l);for(let m=0;m<o;m++)s[m]+=f[m];c+=gi(l)}let i=0;for(let l of s)i+=(l-r*(o+1)/2)**2;let a=12*i/(r*o*(o+1)-c/(o-1));return Ho("Friedman rank sum test","Friedman chi-squared",a,o-1)}function vf(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{...Xo(e),method:"Cochran's Q test",statisticName:"Q"}}function wi(e,t){if(typeof e=="string")return wi(Lo(e,t));let n=Go(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=>ge(w)-et(w));s.some(w=>w===1/0)&&n.every(w=>w.every(Number.isFinite))&&(s=ee(n).groups.map(w=>ge(w)-et(w)));let c=ve(s),i=new Array(o).fill(0),a=0;n.forEach((w,h)=>{let N=ve(w);for(let M=0;M<o;M++){let v=c[h]*(N[M]-(o+1)/2);a+=v*v,i[M]+=v}});let l=0;for(let w of i)l+=w*w;l/=r;let f=o-1,m=(r-1)*(o-1),u,p,g=[f,m];if(a===l){u=NaN,g=[NaN,NaN];let w=0;for(let h=2;h<=o;h++)w+=Math.log(h);p=Math.exp((1-r)*w)}else u=(r-1)*l/(a-l),p=we(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:g,parameterName:["num df","denom df"],pValue:p,warnings:[]}}function bi(e,t){if(typeof e=="string"){let p=er(e,t);return bi(p.values,p.group.codes)}let n=tn(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=p=>p.flatMap(g=>{let w=Br(g);return g.map(h=>Math.abs(h-w))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(p=>p===1/0)&&n.every(p=>p.every(Number.isFinite))&&(s=o(ee(n).groups));let c=s.length,a=ve(s).map(p=>Q((1+p/(c+1))/2)),l=0;for(let p of a)l+=p;let f=l/c,m=0,u=0;for(let p of n){let g=0;for(let w=0;w<p.length;w++)g+=a[u+w];u+=p.length,m+=p.length*(g/p.length-f)**2}return m/=qe(a),Ho("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function vi(e,t,n={}){if(V(n,bf,"moodTest"),typeof e=="string"){let b=Ct(e,t);return vi(b.x,b.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=fe(n.alternative),c=r.length,i=o.length,a=c+i;if(a<3)throw new RangeError("not enough observations");if(c===0||i===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:a},(b,d)=>d).sort((b,d)=>l[b]-l[d]),m=new Array(a),u=0;for(;u<a;){let b=u;for(;b+1<a&&l[f[b+1]]===l[f[u]];)b++;let d=0;for(let E=u;E<=b;E++)d+=(E+1-(a+1)/2)**2;d/=b-u+1;for(let E=u;E<=b;E++)m[f[E]]=d;u=b+1}let p=0;for(let b=0;b<c;b++)p+=m[b];let g=0;for(let b of m)g+=b;g/=a;let w=0;for(let b of m)w+=(b-g)**2;let h=c*g,N=c*i/(a*(a-1))*w,M=(p-h)/Math.sqrt(N),v=s==="less"?U(M):s==="greater"?U(M,{lowerTail:!1}):2*U(-Math.abs(M));return{method:"Mood two-sample test of scale",alternative:s,statistic:M,statisticName:"Z",pValue:v,warnings:[]}}function nn(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var Nf=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],Mf=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],yf=[.544,-.39978,.025054,-6714e-7],Ef=[1.3822,-.77857,.062767,-.0020322],xf=[-1.5861,-.31082,-.083751,.0038915],Rf=[-.4803,-.082676,.0030302],Tf=[-2.273,.459];function kf(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]=-Q((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),c=1/Math.sqrt(e),i=r[0]/s+nn(Nf,c),a=new Float64Array(n);a[0]=i;let l,f;if(e>5){let m=r[1]/s+nn(Mf,c);a[1]=m,l=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*i*i-2*m*m))}else l=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*i*i));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 Ni(e){let t=ee([e]).groups[0],n=t.length,r=kf(n),o=t[n-1]-t[0],s=0;for(let f of t)s+=f/o;s/=n;let c=0,i=0,a=0;for(let f=0;f<n;f++){let m=t[f]/o-s;c+=r[f]*r[f],i+=m*m,a+=r[f]*m}let l=Math.sqrt(c*i);return(l-a)*(l+a)/(c*i)}var Sf=1.90985931710274,Af=1.0471975511966;function Mi(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let c=Sf*(Math.asin(Math.sqrt(1-e))-Af);return Math.max(c,0)}let n=Math.log(e);if(t<=11){let c=nn(Tf,t);if(n>=c)return 1e-99;let i=nn(yf,t),a=Math.exp(nn(Ef,t));return U(-Math.log(c-n),{mean:i,sd:a,lowerTail:!1})}let r=Math.log(t),o=nn(xf,r),s=Math.exp(nn(Rf,r));return U(n,{mean:o,sd:s,lowerTail:!1})}var Ri=Math.log(2*Math.PI),yi=2e-14,Ei=500;function qf(e){return Math.exp(-.5*e*e-.5*Ri)}function If(e){return U(e,{logP:!0})}function Ti(e,t=-1/0,n=!1){let r=t===-1/0?Io(e,0,2,0,yi,Ei):Io(e,t,1,0,yi,Ei),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 xi(e,t,n){let r=t.map(Math.sqrt),o=t.map(c=>Math.sqrt(1-c));return Ti(c=>{let i=0;for(let a=0;a<r.length;a++){let l=(r[a]*c+e)/o[a];if(!n){i+=If(l);continue}let f=(r[a]*c-e)/o[a],m=U(f)+U(l,{lowerTail:!1});m<.5?i+=Math.log1p(-m):i+=Math.log(Math.max(0,l+f<0?U(l)-U(f):U(f,{lowerTail:!1})-U(l,{lowerTail:!1})))}return qf(c)*-Math.expm1(i)})}function rn(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,xi(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Ri-sa(o);return Math.min(1,Ti(i=>{let a=s-o*(Math.expm1(2*i)-2*i);return a<-745?0:Math.exp(a)*xi(e*Math.exp(i),t,r)},-1/0,!0))}var _f=["pAdjustMethod"],Lf=["pAdjustMethod","alternative"],Of=["alternative"],Ff=["pAdjustMethod","alternative"],Pf=["method","altp"],Df=["alternative"],ki="Ties are present. z-quantiles were corrected for ties.";function It(e,t){let{labels:n,groups:r}=zt(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(c=>c.length);return{labels:n,groups:r,n:s,N:s.reduce((c,i)=>c+i,0),k:o}}function Bo(e){let t=e.groups.flat(),n=ve(t),r=[],o=0;for(let c of e.n){let i=0;for(let a=0;a<c;a++)i+=n[o+a];o+=c,r.push(i/c)}let s=0;for(let c of ut(t))s+=c*c*c-c;return{R:r,C:s/(12*(e.N-1))}}function ir(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 Vf(e,t){let n=e.length+1,r=[];for(let i=0;i<n-1;i++)for(let a=i+1;a<n;a++)r.push(e[a-1][i]);let o=Tn(r,t),s=0,c=e.map(i=>i.slice());for(let i=0;i<n-1;i++)for(let a=i+1;a<n;a++)c[a-1][i]=o[s++];return c}function Jr(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 Qo(e,t,n,r,o,s=[]){return{method:t,statistic:n.map(c=>[c]),pValue:r.map(c=>[c]),rowNames:e.labels.slice(1),colNames:e.labels.slice(0,1),pAdjustMethod:o,warnings:s}}var Zr=e=>U(e,{lowerTail:!1}),Zo=(e,t,n)=>Lr(Math.abs(e),t,n,{lowerTail:!1});function Jo(e){let t=Ie(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return cr(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function cr(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 Cf(e,t,n={}){V(n,_f,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=It(e,t),{R:s,C:c}=Bo(o),i=o.N*(o.N+1)/12-c,a=ir(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(i*(1/o.n[f]+1/o.n[m]))),l=Vf(a.map(f=>f.map(m=>2*Zr(m))),r);return j("kwAllPairsDunnTest",Jr(o,"Dunn's all-pairs test",a,l,r,c!==0?[ki]:[]))}function $f(e,t,n={}){V(n,Lf,"kwManyOneDunnTest");let r=fe(n.alternative),o=n.pAdjustMethod??"single-step",s=It(e,t),{R:c,C:i}=Bo(s),a=s.N*(s.N+1)/12-i,l=s.n.slice(1).map((p,g)=>g+1),f=l.map(p=>(c[p]-c[0])/Math.sqrt(a*(1/s.n[0]+1/s.n[p]))),m=l.map(p=>s.n[p]/(s.n[p]+s.n[0])),u=Si(f,m,r,o);return j("kwManyOneDunnTest",Qo(s,"Dunn's many-to-one test",f,u,o,i!==0?[ki]:[]))}function Si(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?rn(o,t,1/0,!0):rn(n==="greater"?o:-o,t,1/0,!1)):Tn(e.map(o=>n==="two.sided"?2*Zr(Math.abs(o)):Zr(n==="greater"?o:-o)),r)}function zf(e,t,n={}){V(n,Of,"dunnettTest");let r=fe(n.alternative),o=It(e,t),{m:s,s2:c,df:i}=Jo(o),a=Math.sqrt(c),l=o.n.slice(1).map((p,g)=>g+1),f=l.map(p=>(s[p]-s[0])/(a*Math.sqrt(1/o.n[p]+1/o.n[0]))),m=l.map(p=>o.n[p]/(o.n[p]+o.n[0])),u=f.map(p=>r==="two.sided"?Math.min(1,2*rn(Math.abs(p),m,i,!1)):rn(r==="greater"?p:-p,m,i,!1));return j("dunnettTest",Qo(o,`Dunnett's-test for multiple
|
|
3
|
-
comparisons with one control`,f,u,"single-step"))}function
|
|
1
|
+
/*! rStats.js 1.1.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{B as _r,C as Ir,D as Lr,b as St,c as fa,d as gn,f as ko,k as wn,l as ya,p as Ma,q as bn,r as Ea,v as qr,w as xa,x as Hn,z as Ta}from"./chunks/chunk-7KJ24LYK.js";import{a as Fr,b as Dt,c as Qn,d as Aa,e as qa,f as qo,g as fe,h as Vt,i as Zt,j as Qe,k as La,l as Sl,n as zr,o as Jn}from"./chunks/chunk-Y4YWLSWU.js";import"./chunks/chunk-Z2HGLFZN.js";import{b as _a,c as Ia,d as Zn,f as er}from"./chunks/chunk-GRUQRNX7.js";import{b as Ao}from"./chunks/chunk-DR4GQ7PW.js";import"./chunks/chunk-QUTJEYDX.js";import{a as De,b as vn,c as yn}from"./chunks/chunk-7IJCUSA5.js";import{a as Na,d as va}from"./chunks/chunk-HZLSNJQM.js";import{a as Jt,b as xn,c as Cr,d as Dr,e as Vr,f as Tn,g as Va,h as za}from"./chunks/chunk-4VRMAYPY.js";import{A as hn,g as Mr,h as Er,j as xr,k as pn,p as we,q as kt,s as Xt,t as Pt,v as Tr,z as Bt}from"./chunks/chunk-DTJG2IL2.js";import{C as Ca,E as Da,H as ct,I as $a,c as Oa,h as Fa,j as ve,l as Pa,m as Mn,n as En,t as Pr,y as Ve}from"./chunks/chunk-GYGQUYEP.js";import{a as Or,b as Sa,g as ka}from"./chunks/chunk-U7BRXOUS.js";import{A as Ar,B as Qt,i as xe,l as yr,x as Ke,y as gt}from"./chunks/chunk-GGJA3JRO.js";import{a as it}from"./chunks/chunk-GJEKNJP7.js";import{a as Rl,c as j,d as Te,e as Re,f as Xn}from"./chunks/chunk-N5BDSMXS.js";import{a as Bn,b as Nn,c as Ra,d as Fe,e as Pe}from"./chunks/chunk-EJYCDE4Z.js";import{a as ge,b as Ct,c as et}from"./chunks/chunk-OG7UORIX.js";import"./chunks/chunk-4KCSBTR5.js";import{b as ca,c as la}from"./chunks/chunk-X4J6UJHN.js";import{A as aa,D as ia,M as ma,q as Ge,r as Y,s as Z}from"./chunks/chunk-NDTJ23A6.js";import{A as da,C as ga,D as _e,E as Rr,F as wa,G as Sr,H as ba,I as se,J as Ne,K as ut,L as He,M as dn,N as Oe,O as kr,P as ee,Q as So,b as Tt,c as Ht,d as Q,e as oa,f as de,g as pe,h as at,i as ke,j as Rt,k as sa,l as z,m as Ft,p as Un,q as Yn,r as ua,s as pa,t as ha,u as I,v as Ce,w as Gn,x as Ae,y as qe}from"./chunks/chunk-HUWOSLKE.js";var Wa=["alternative","mu","paired","varEqual","confLevel"];function Rn(e,t,n={}){if(typeof e!="string"&&De(t)&&z(t,Wa,"tTest"),z(n,Wa,"tTest"),typeof e=="string")return kl(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=fe(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(Vt(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[k,A]=dn(Array.from(e),Array.from(r));({d:l,k:m}=Rr(k,A))}else{let k=ee(r===void 0?[He(Array.from(e))]:[He(Array.from(e)),He(Array.from(r))]);m=k.k,l=k.groups[0],f=k.groups[1]}let u=I(s,-m),p,w,g,d,N,b,v,y=l.length,h=f!==void 0&&c;if(y<1||!h&&y<2)throw new Error("not enough 'x' observations");let E=Ae(l),x=y>1?qe(l):NaN;if(f===void 0){if(w=y-1,g=Math.sqrt(x/y),g===0||g<10*Number.EPSILON*Math.abs(E))throw new Error("data are essentially constant");p=(E-u)/g,b=i?"Paired t-test":"One Sample t-test",d=E,N=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let k=f.length;if(k<1||!c&&k<2)throw new Error("not enough 'y' observations");if(c&&y+k<3)throw new Error("not enough observations");let A=Ae(f),_=k>1?qe(f):NaN;if(b=`${c?"":"Welch"} Two Sample t-test`,d=[E,A],N=["mean of x","mean of y"],v="difference in means",c){w=y+k-2;let q=0;y>1&&(q+=(y-1)*x),k>1&&(q+=(k-1)*_),q/=w,g=Math.sqrt(q*(1/y+1/k))}else{let q=x/y,C=_/k;g=Math.sqrt(Math.sqrt(q)**2+Math.sqrt(C)**2),w=Sr([q,C],[y-1,k-1])}if(g===0||g<10*Number.EPSILON*Math.max(Math.abs(E),Math.abs(A)))throw new Error("data are essentially constant");p=(E-A-u)/g}let M=typeof d=="number"?d:d[0]-d[1],T=(k,A)=>{let _=u+(p+A*k)*g;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(p*g))<=16*(Math.abs(M)+k*g)?_:M+A*k*g},R,S;if(o==="less")R=Ke(p,w,!0,!1),S=[-1/0,T(gt(a,w,!0,!1),1)];else if(o==="greater")R=Ke(p,w,!1,!1),S=[T(gt(a,w,!0,!1),-1),1/0];else{R=2*Ke(-Math.abs(p),w,!0,!1);let k=1-a,A=k/2<1e-8?gt(k/2,w,!1,!1):gt(1-k/2,w,!0,!1);S=[T(A,-1),T(A,1)]}return S=[I(S[0],m),I(S[1],m)],d=typeof d=="number"?I(d,m):d.map(k=>I(k,m)),g=I(g,m),{method:b,alternative:o,statistic:p,statisticName:"t",parameter:w,parameterName:"df",pValue:R,confInt:S,confLevel:a,estimate:d,estimateNames:N,nullValue:s,nullValueName:v,stdErr:g,stdError:g,warnings:[]}}function kl(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=Fr(e,t,!0);if(r.kind==="one")return Rn(r.x,n);if(r.kind==="paired")return Rn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...Rn(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 ja=["correct","p","rescaleP","simulatePValue","B"];function lt(e){let t=[0,0],n=0;for(let r of e)t=Q(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Al(e,t,n){return De(t)&&z(t,ja,"chisqTest"),z(n,ja,"chisqTest"),Array.isArray(t)?j("chisqTest",ql(e,t,n??{})):j("chisqTest",_l(e,t??{}))}var Ya="Chi-squared approximation may be incorrect";function ql(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,p)=>{let w=t[p];!r(u)&&!r(w)&&(o.push(u),s.push(w))});let i=[...new Set(o)].sort(it),c=[...new Set(s)].sort(it);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,p)=>[u,p])),l=new Map(c.map((u,p)=>[u,p])),f=i.map(()=>c.map(()=>0));return o.forEach((u,p)=>f[a.get(u)][l.get(s[p])]++),{...Qa(f,n),rowLevels:i,colLevels:c}}function _l(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?_o(r[0],t):o===1?_o(r.map(s=>s[0]),t):Qa(r,t)}return _o(Array.from(e),t)}function Ga(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 Ha(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 Xa(e,t){return Number.isNaN(e)?NaN:xe(e,t/2,2,!1,!1)}function Ba(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 _o(e,t){let n=e.length;if(e.some(g=>!(g>=0&&g<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=lt(e);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=t.p===void 0?e.map(()=>1/n):Array.from(t.p);if(o.length!==n)throw new TypeError("'x' and 'p' must have the same number of elements");if(o.some(Number.isNaN))throw new RangeError("p contains missing values");if(o.some(g=>g<0))throw new RangeError("probabilities must be non-negative");if(o.some(g=>g===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let g=lt(o),d=g<1/0?0:Math.ceil(Math.log2(n)),N=d===0?o:o.map(v=>I(v,-d)),b=d===0?g:lt(N);o=N.map(v=>v/b)}else if(Math.abs(lt(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r<1/0?0:2*Math.ceil(Math.log2(n)/2),i=s===0?e:e.map(g=>I(g,-s)),c=s===0?r:lt(i),a=o.map(g=>c*g),l=s===0?a:a.map(g=>I(g,s));Ba(l);let f=o.map((g,d)=>a[d]*(1-g)),m=I(lt(i.map((g,d)=>Oe(g-a[d],a[d]))),s),u="Chi-squared test for given probabilities",p=n-1,w;if(t.simulatePValue??!1){let g=Ga(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=Bn(g),p=NaN,w=Ha(Il(r,o,l,g),m,g)}else w=Xa(m,p);return{method:u,statistic:m,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:w,observed:e,expected:l,residuals:i.map((g,d)=>I((g-a[d])/Math.sqrt(a[d]),s/2)),stdres:i.map((g,d)=>I((g-a[d])/Math.sqrt(f[d]),s/2)),warnings:Number.isFinite(p)&&l.some(g=>g<5)?[Ya]:[]}}function Il(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=Ta(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 Ll(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>_r)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=Ir(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){Lr(e,t,c,a);let m=0;for(let u=0;u<s;u++)for(let p=0;p<o;p++){let w=n[p][u];m+=(a[p+u*o]-w)**2/w}l[f]=m}return l}function Qa(e,t){let n=t.correct??!0,r=e.length,o=e[0].length,s=e.flat();if(s.some(b=>!(b>=0&&b<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let i=lt(s);if(!(i>0))throw new RangeError("at least one entry of 'x' must be positive");let c=e.map(b=>lt(b)),a=e[0].map((b,v)=>lt(e.map(y=>y[v]))),l=c.map(b=>a.map(v=>kr(b,v,i)));Ba(l.flat());let f=e.map((b,v)=>b.map((y,h)=>y-l[v][h])),m="Pearson's Chi-squared test",u,p=(r-1)*(o-1),w,g=(t.simulatePValue??!1)&&c.every(b=>b>0)&&a.every(b=>b>0),d=!g&&n&&r===2&&o===2?Math.min(.5,...f.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(y=>y.some(h=>!Number.isInteger(h))))throw new RangeError("the simulation needs integer counts");let b=Ga(t);m+=Bn(b),u=lt(f.flatMap((y,h)=>y.map((E,x)=>Oe(E,l[h][x])))),p=NaN;let v=Ll(c.map(y=>Math.trunc(y)),a.map(y=>Math.trunc(y)),l,b);w=Ha(v,u,b)}else d>0?(m+=" with Yates' continuity correction",u=lt(f.flatMap((b,v)=>b.map((y,h)=>Oe(Math.abs(y)-d,l[v][h]))))):u=lt(f.flatMap((b,v)=>b.map((y,h)=>Oe(y,l[v][h]))));return{method:m,statistic:u,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:w??Xa(u,p),observed:e,expected:l,residuals:f.map((b,v)=>b.map((y,h)=>y/Math.sqrt(l[v][h]))),stdres:Ol(e,i,c,a,l,f),warnings:Number.isFinite(p)&&l.some(b=>b.some(v=>v<5))?[...N,Ya]:N}}function Ol(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((w,g)=>w.map((d,N)=>d/Math.sqrt(o[g][N]*(1-n[g]/t)*(1-r[N]/t))));let i=e.map(w=>Ka(w)),c=e[0].map((w,g)=>Ka(e.map(d=>d[g]))),a=Pl(i),l=Ua(i),f=Ua(c),m=l.map(w=>ke(w,a)),u=c.map(w=>ke(w,a)),p=f.map(w=>ke(w,a));return e.map((w,g)=>w.map((d,N)=>{let b=pe(i[g],u[N]),v=de([d,0],b),y=v[0]+v[1],h=pe(pe(b,m[g]),p[N]),E=h[0]+h[1];if(E>=Fl||y===0)return y/Math.sqrt(E);let x=b[0]+b[1],M=m[g][0]+m[g][1],T=p[N][0]+p[N][1];return y/Math.sqrt(x)/Math.sqrt(M)/Math.sqrt(T)}))}var Fl=2**-1022;function Ka(e){let t=[0,0];for(let n of e)t=Q(t,[n,0]);return t}function Pl(e){let t=[0,0];for(let n of e)t=Q(t,n);return t}function Ua(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=Q(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(Q(o,n[s+1])),o=Q(o,e[s]);return r}var Cl=Math.pow(Number.EPSILON,.25);function wt(e,t,n,r={}){let o={tol:r.tol??Cl,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),Zn(e,t,n,o).root}function $r(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?wt(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):wt(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var Dl=["alternative","confInt","confLevel","or","simulatePValue","B"];function Vl(e,t={}){z(t,Dl,"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(la));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=zl(o,s);c!==null&&i.push(`'x' has been rounded to integer: ${c}`);let a="Fisher's Exact Test for Count Data";if(n===2&&r===2)return j("fisherTest",{...Ul(s,t,a),warnings:i});let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let f=t.B??2e3;if(!(Number.isInteger(f)&&f>=1&&f<2**31))throw new RangeError("'B' must be a positive integer");return j("fisherTest",{method:a+Bn(f),alternative:l,pValue:$l(s,f),warnings:i})}return j("fisherTest",{method:a,alternative:l,pValue:Yl(s),warnings:i})}function zl(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: ${Ra(a)}`:null}function $l(e,t){let n=e.filter(d=>d.some(N=>N>0)),r=e[0].map((d,N)=>N).filter(d=>e.some(N=>N[d]>0));if(n.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(r.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let o=n.map(d=>r.map(N=>d[N])),s=o.map(d=>d.reduce((N,b)=>N+b,0)),i=o[0].map((d,N)=>o.reduce((b,v)=>b+v[N],0)),c=s.length,a=i.length,l=[0,0];for(let d of o.flat())l=Q(l,[-va(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,N)=>d+N,0);if(m>_r)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Ir(m),p=new Int32Array(c*a),w=0,g=Math.trunc(t);for(let d=0;d<g;d++){Lr(s,i,u,p);let N=0;for(let b=0;b<p.length;b++)N-=u[p[b]];N<=f&&w++}return Math.max(0,Math.min(1,(1+w)/(g+1)))}var Wl=1+1e-7,jl=16,Kl=2**24;function Io(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=Q(r,[e[o],0]));return r[0]}var Oo=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%jl===0||!(s>0&&s<1/0)?(o=gn(i,t,n,r,!0),s=1,this.logCentral[c]=o):this.logCentral[c]=o+Math.log(s),s*=(t-i)*(r-i)/((i+1)*(n-r+i+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),s=-1/0;for(let c=0;c<n;c++){let a=this.logCentral[c]+(this.lo+c)*r;o[c]=a,a>s&&(s=a)}for(let c=0;c<n;c++){let a=o[c]-s;o[c]=a<-746?0:Math.exp(a)}let i=Io(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=Q(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?ko(t-1,this.m,this.n,this.k,!1,!1):ko(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?Io(o,s,o.length-1):Io(o,0,s)}};function Lo(e,t){return $r(e,t)}function Ul(e,t,n){let r=fe(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>Kl)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Oo(i+a,c+l,i+c),{lo:p,hi:w}=u,g;if(r==="less")g=u.tail(f,o,!1);else if(r==="greater")g=u.tail(f,o,!0);else if(o===0)g=f===p?1:0;else if(o===1/0)g=f===w?1:0;else{let h=u.density(o),E=h[f-p]*Wl,x=[0,0];for(let M of h)M<=E&&(x=Q(x,[M,0]));g=x[0]}let d;if(f===p)d=0;else if(f===w)d=1/0;else{let h=u.mean(1);d=h===f?1:Lo(E=>u.mean(E)-f,h>f)}let N={method:n,alternative:r,pValue:g,estimate:d,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let b=h=>{if(f===w)return 1/0;let E=u.tail(f,1,!1);return E===h?1:Lo(x=>u.tail(f,x,!1)-h,E<h)},v=h=>{if(f===p)return 0;let E=u.tail(f,1,!0);return E===h?1:Lo(x=>u.tail(f,x,!0)-h,E>h)},y;if(r==="less")y=[0,b(1-s)];else if(r==="greater")y=[v(1-s),1/0];else{let h=(1-s)/2;y=[v(h),b(h)]}return{...N,confInt:y,confLevel:s}}var Wr=[[0,0]];function bt(e){for(let t=Wr.length;t<=e;t++)Wr.push(Q(Wr[t-1],sa([t,0])));return Wr[e]}function jr(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 Fo=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 Yl(e){let t=e.filter(v=>v.some(y=>y>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,y)=>t.some(h=>h[y]>0));if(t=t.map(v=>v.filter((y,h)=>n[h])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,y)=>t.map(h=>h[y])));let r=t.length,o=t.map(v=>v.reduce((y,h)=>y+h,0)),s=t[0].map((v,y)=>t.reduce((h,E)=>h+E[y],0)).sort((v,y)=>y-v),i=s.length,c=o.reduce((v,y)=>v+y,0),a=oa(bt(c));for(let v of o)a=Q(a,bt(v));for(let v of s)a=Q(a,bt(v));let l=[0,0];for(let v of t.flat())l=de(l,bt(v));let f=Q(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,p]=f,w=Array.from({length:i},()=>new Map),g=(v,y)=>{let h=y.join(","),E=w[v].get(h);if(E!==void 0)return E;let x=Q(a,bt(y.reduce((A,_)=>A+_,0)));for(let A of y)x=de(x,bt(A));for(let A=v;A<i;A++)x=de(x,bt(s[A]));let M={R:y,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(w[v].set(h,M),v===i-1){let A=[0,0];for(let _ of y)A=de(A,bt(_));return M.max=M.min=A[0],M.edges.push({to:M,hi:A[0],lo:A[1],mult:1}),M}let T=new Map,R=new Array(r+1).fill(0);for(let A=r-1;A>=0;A--)R[A]=R[A+1]+y[A];let S=new Array(r).fill(0),k=(A,_,q)=>{if(A===r-1){S[A]=_;let D=de(q,bt(_)),W=y.map((H,te)=>H-S[te]).sort((H,te)=>H-te),F=g(v+1,W),L=`${W.join(",")}|${D[0]}`,$=T.get(L);$===void 0?T.set(L,{to:F,hi:D[0],lo:D[1],mult:1}):$.mult++;return}let C=Math.max(0,_-R[A+1]),O=Math.min(y[A],_);for(let D=C;D<=O;D++)S[A]=D,k(A+1,_-D,de(q,bt(D)))};if(k(0,s[v],[0,0]),M.edges=[...T.values()],M.max=ge(M.edges.map(A=>A.hi+A.to.max)),M.min=et(M.edges.map(A=>A.hi+A.to.min)),v===i-2){let A=M.edges.map(F=>{let L=F.to.edges[0];return{v:Q([F.hi,F.lo],[L.hi,L.lo]),mult:F.mult}});A.sort((F,L)=>F.v[0]-L.v[0]||F.v[1]-L.v[1]);let _=A.length,q=A[_-1].v[0],C=new Float64Array(_),O=new Float64Array(_),D=new Float64Array(_),W=[0,0];A.forEach(({v:F,mult:L},$)=>{C[$]=F[0],O[$]=F[1];let H=de(F,[q,0]);W=Q(W,[L*Math.exp(H[0])*(1+H[1]),0]),D[$]=W[0]}),M.dist={hi:C,lo:O,cum:D,vmax:q,kv:Q(a,[q,0])}}return M},d=g(0,[...o].sort((v,y)=>v-y)),N=new Fo,b=(v,y,h,E)=>{if(y+v.max<=u-m){let[k,A]=jr(v.KZ[0],v.KZ[1],y,h);return N.add(k,A,E),!0}if(y+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[M,T]=jr(u,p,-y,-h),R=0,S=x.hi.length;for(;R<S;){let k=R+S>>>1,A=x.hi[k];A<M||A===M&&x.lo[k]<=T?R=k+1:S=k}if(R>0){let[k,A]=jr(x.kv[0],x.kv[1],y,h);N.add(k,A,E*x.cum[R-1])}return!0};b(d,0,0,1)||(d.pastHi.push(0),d.pastLo.push(0),d.count.push(1));for(let v=0;v<i-2;v++)for(let y of w[v].values()){let{pastHi:h,pastLo:E,count:x}=y;if(x.length!==0){for(let{to:M,hi:T,lo:R,mult:S}of y.edges)for(let k=0;k<x.length;k++){let[A,_]=jr(h[k],E[k],T,R),q=x[k]*S;if(b(M,A,_,q))continue;let C=M.index.get(A);C===void 0?(M.index.set(A,M.count.length),M.pastHi.push(A),M.pastLo.push(_),M.count.push(q)):M.count[C]+=q}y.pastHi=y.pastLo=y.count=[],y.index.clear()}}return Math.min(1,N.value())}function Kr(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function Co(e){return[...e].sort((t,n)=>t-n).join(",")}function Po(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 Do(e){let[,t]=Kr(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function Vo(e){let[t,n]=Kr(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 Po(t,0,o)}function zo(e,t){let[,n]=Kr(e),r=n.length,o=Math.min(t,r-t),s=Za(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 Za(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function $o(e,t){let[n,r]=Kr(e),o=r.length,s=Math.min(t,o-t),i=Za(r),c=[];for(let p=0;p<=s;p++)c.push(new Float64Array(i[o]-i[o-p]-i[p]+1));c[0][0]=1;for(let p=1;p<=o;p++){let w=r[p-1];for(let g=Math.min(p,s);g>=Math.max(1,s-(o-p));g--){let d=c[g],N=c[g-1],b=g/p,v=(p-g)/p,y=i[g],h=i[p]-i[p-g],E=i[g-1]+w,x=Math.max(y,E);for(let M=h;M>=x;M--)d[M-y]=v*d[M-y]+b*N[M-E];for(let M=x-1;M>=y;M--)d[M-y]=v*d[M-y]}}let a=c[s],l=t*(t+1)/2;if(s===t)return Po(n,i[s]/n-l,a);let f=a.length,m=new Float64Array(f);for(let p=0;p<f;p++)m[p]=a[f-1-p];let u=i[o]-i[o-s];return Po(n,(i[o]-u)/n-l,m)}function zt(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 en(e,t){let n=zt(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 Ja=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function tr(e,t,n={}){if(typeof e!="string"&&De(t)&&z(t,Ja,"wilcoxTest"),z(n,Ja,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let g=Fr(e,t,!0);return g.kind==="one"?tr(g.x,n):tr(g.x,g.y,g.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=fe(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=Hl(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,p;if(i){let[g,d]=dn(Array.from(e),Array.from(r));u=He(g.map((N,b)=>N-d[b]))}else u=He(Array.from(e)),r!==void 0&&(p=He(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(p!==void 0&&p.length<1)throw new Error("not enough (non-missing) 'y' observations");let w={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new Re};return j("wilcoxTest",p===void 0?Jl(u,c,i,w):cf(u,p,c,w))}function Hl(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 Ur(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var ni=10*Number.EPSILON;function ri(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?Y(e):r(Y(e)-n*Ge(e)),s=()=>n===0?Y(e,{lowerTail:!1}):r(Y(e,{lowerTail:!1})+n*Ge(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 Xl(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Bl(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 oi(e,o,s,n)}function Ql(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 oi(e,i,c,r)}function oi(e,t,n,r){let[o,s,i]=Xl(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function Ko(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:Ae([e[n-1],e[n]])}var Uo=(e,t)=>e-t,Wo=5e9,Zl=40;function jo(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function Yr(e,t){if(e.size>16777216||e.steps>Wo)throw jo(t);return e.steps}function si(e){let t=e.length,n=Ne(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 ut(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 Jl(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(h=>h-s);i.some((h,E)=>!Number.isFinite(h)&&Number.isFinite(e[E]))&&(i=e.map(h=>h/2-s/2));let a=i.some(h=>h===0),l=t??i.length<50,f=(h,E,x)=>({method:h,alternative:o,statistic:E,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let h=Ne(i.map(Math.abs)),E=ut(h).length>0,x=0;for(let A=0;A<i.length;A++)i[A]>0&&(x+=h[A]);let M="Wilcoxon signed rank exact test";if(!E&&!a){let A=i.length,_=A*(A+1)/2,q;if(o==="less")q=wn(x,A,!0,!1);else if(o==="greater")q=wn(x-1,A,!1,!1);else{let O=x>_/2?wn(x-1,A,!1,!1):wn(x,A,!0,!1);q=Math.min(1,2*O)}let C=f(M,x,q);return r.confInt?{...C,...ef(e,r)}:C}let T=h.filter((A,_)=>i[_]!==0);Yr(Do(T),"distribution");let R=Vo(T),S;if(o==="less")S=zt(R,x);else if(o==="greater")S=en(R,x-.25);else{let A=0;for(let _ of T)A+=_/2;S=Math.min(1,2*(x>A?en(R,x-.25):zt(R,x)))}let k=f(M,x,S);return r.confInt?{...k,...nf(e,r)}:k}let m=a?i.filter(h=>h!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[p,w,g]=si(m),N=ut(Ne(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,b=(w-Ur(w,o,N>=0))/g,v=ri(b,o,N>=1?Bl(b,u,N):0),y=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,p,v);return r.confInt?{...y,...rf(e,r)}:y}function Yo(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function ei(e,t,n){let r={confInt:Yo(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function ai(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function ii(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=Ct(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(Uo)}function ci(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+ni&&l++;let f=Ko(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:Yo(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function ef(e,t){let n=e.length;return ci(ii(e),r=>wn(r,n,!0,!1),r=>ya(r,n,!0,!1),t,"(pseudo)median")}function li(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 fi(e,t,n,r,o,s,i,c,a,l){let{alternative:f,confLevel:m}=a,u=e.length,w=(f==="two.sided"?(1-m)/2:1-m)+ni,g=f!=="less",d=f!=="greater",N=q=>Ct(e[q-1],e[q]),b=Math.min(u-1,Math.max(1,Math.ceil(i.mean+Z(Math.min(w,1))*i.sd+.1*i.sd+3))),v=8,y=q=>q===0?e[0]-1:N(q),h=q=>q===0?e[u-1]+1:N(u-q),E=[new Map,new Map];{let q=new Map,C=0,O=Zl*c;for(let[D,W]of[[0,y],[1,h]])if(!(D===0?!g:!d))for(let F=0;F<=b;F++){if(C+=O,C>Wo)throw jo("interval");let L;try{L=t(W(F))}catch{break}if(E[D].set(F,[L.stat,L.key]),q.has(L.key)){q.delete(L.key),q.set(L.key,!0);continue}if(C+=r(L.scores),C>Wo)throw jo("interval");q.set(L.key,!0),q.size>v&&q.delete(q.keys().next().value)}}let x=new Map,M=(q,C)=>{let O=q===0?y(C):h(C),D=E[q].get(C),W;if(D===void 0){let H=t(O);D=[H.stat,H.key],W=H.scores}let[F,L]=D,$=x.get(L);return $!==void 0?x.delete(L):($=n(W??t(O).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(L,$),[F,$]},T=q=>{let[C,O]=M(0,q);return o(O,C)},R=q=>{let[C,O]=M(1,q);return s(O,C)},S=-1/0,k=0;if(g){let q=T(0);if(!(q>w)){let C=q,O=1;for(;O<=u-1;O++){let D=T(O);if(D>w)break;C=D}S=e[O-1],k=C}}let A=1/0,_=0;if(d){let q=R(0);if(!(q>w)){let C=q,O=u-1;for(;O>=1;O--){let D=R(u-O);if(D>w)break;C=D}A=e[O],_=C}}return{confInt:[S,A],confLevel:1-(k+_),estimate:Ko(e),estimateNames:l}}function tf(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 nf(e,t){let n=e.length,r=s=>{let i=tf(e,s),{ranks:c,key:a}=li(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 fi(ii(e),r,Vo,s=>Yr(Do(s),"interval"),(s,i)=>en(s,i-.25),zt,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function rf(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return ei(n,NaN);let c=et(e),a=ge(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"),ei(n,Ct(c,a),"midrange");let l=(v,y)=>{let[,h,E]=si(e.map(x=>x-v).filter(x=>x!==0));return(h-Ur(h,n,y))/E},f=l(c,s),m=l(a,s),u=v=>-Z(v),p=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>Z(v/2)):!(f<u(v/2))&&!(m>Z(v/2)),w=1-r,g=!1;for(;!p(w)&&w<1;)w*=2,g=!0;g&&t.warnings.add("requested conf.level not achievable"),ai(o);let d=v=>wt(y=>l(y,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),N;if(w>=1){let v=Ko([...e].sort(Uo));N=Yo(n,v,v)}else n==="greater"?N=[d(u(w)),1/0]:n==="less"?N=[-1/0,d(Z(w))]:N=[d(u(w/2)),d(Z(w/2))];let b=wt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:N,confLevel:g?1-Math.min(1,w):r,estimate:b,estimateNames:"(pseudo)median"}}function mi(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 ut(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 of(e,t){let[n,r,o]=mi(Ne([...e,...t]),e.length);return[n,r,o]}function sf(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 af(e,t,n){return ui(e,t,n).ranks}function ui(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=Ne([...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:Co(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=sf(e.map(s=>s/2-n/2),t);return{ranks:o,key:Co(o)}}return li([...r,...t])}function cf(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=af(e,t,s),[f,m,u,p]=mi(l,i),w,g;if(a)if(g="Wilcoxon rank sum exact test",!p)w=ti(()=>o==="less"?bn(f,i,c,!0,!1):o==="greater"?bn(f-1,i,c,!1,!1):Math.min(2*bn(f,i,c,!0,!1),2*bn(f-1,i,c,!1,!1),1));else{Yr(zo(l,i),"distribution");let b=$o(l,i);o==="less"?w=zt(b,f):o==="greater"?w=en(b,f-.25):w=Math.min(2*zt(b,f),2*en(b,f-.25),1)}else{g=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let b=p?Math.min(r.correct,0):r.correct,v=l.every(h=>h===l[0]),y=(m-Ur(m,o,b>=0))/u;w=v?1:ri(y,o,b>=1?Ql(y,i,c,b):0)}let d={method:g,alternative:o,statistic:f,statisticName:"W",pValue:w,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return d;let N=a?p?ff(e,t,r):ti(()=>lf(e,t,r)):mf(e,t,r);return{...d,...N}}function ti(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function pi(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(Uo)}function lf(e,t,n){let r=e.length,o=t.length;return ci(pi(e,t,!1),s=>bn(s,r,o,!0,!1),s=>Ea(s,r,o,!0,!1),n,"difference in location")}function ff(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=ui(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 fi(pi(e,t,!0),s,i=>$o(i,r),i=>Yr(zo(i,r),"interval"),(i,c)=>en(i,c-.25),zt,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function mf(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=et(e)-ge(t),a=ge(e)-et(t);if(!Number.isFinite(c)||!Number.isFinite(a))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(p,w)=>{let[,g,d]=of(e.map(N=>N-p),t);return(g-Ur(g,r,w))/d},f=(p,w)=>{let g=l(c,w)-p,d=l(a,w)-p;if(Number.isNaN(g)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return g<=0?c:d>=0?a:(ai(s),wt(N=>l(N,w)-p,c,a,{tol:s,fLower:g,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-Z(m),i),1/0]:r==="less"?u=[-1/0,f(Z(m),i)]:u=[f(-Z(m/2),i),f(Z(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var uf=["method","n"],hi=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function pf(e){if(hi.includes(e))return e;let t=hi.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Sn(e,t={}){z(t,uf,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=pf(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=gi(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Gr(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 gi(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=Gr(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=Gr(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=ua(),u=Math.floor(n);if(u<=1e6){for(let p=1;p<=u;p++)pa(m,1/p,u);s=ha(m)}else{let p=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*p)+1/(120*p*p)}}let i=s*n,c=Number.isFinite(i)?1:1024,a=Number.isFinite(i)?i:s*(n/1024),l=Gr(e,!0),f=1/0;return l.forEach((m,u)=>{let p=r-u;f=Math.min(f,a/p*e[m]*c),o[m]=Math.min(1,f)}),o}case"hommel":return n===2?gi(e,"hochberg",n):hf(e,n)}}var di=2**16;function hf(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>di)throw new RangeError(`n must be at most ${di} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Gr(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 wi=["p","alternative","confLevel"],df=1+1e-7;function Go(e,t,n){let r=e-1;return e===0||e-r===1&&Tt(t,-r)[1]===0?hn(r,t,n,!1,!1):hn(e,t,n,!1,!1)+Bt(e,t,n,!1)}function gf(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=Bt(e,t,n,!1)*df,s=a=>Bt(a,t,n,!1)<=o;if(e<r){let a=Mr(s,Math.ceil(r),t),l=a>t?0:Go(a,t,n);return Math.min(1,hn(e,t,n,!0,!1)+l)}let i=Er(s,0,Math.floor(r)),c=i<0?0:hn(i,t,n,!0,!1);return Math.min(1,Go(e,t,n)+c)}function wf(e,t,n={}){z(t,wi,"binomTest"),z(n,wi,"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),!Zt(e[0])||!Zt(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(!Zt(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Zt(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return Ho(r,o,n)}function Ho(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=fe(n.alternative),s=n.confLevel??.95;Qe(s);let i;o==="less"?i=hn(e,t,r,!0,!1):o==="greater"?i=Go(e,t,r):i=gf(e,t,r);let c=f=>e===0?0:Tr(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Tr(f,e+1,t-e,!1,!1):Tr(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 bf=["T","r","alternative","confLevel"],Nf=1+1e-7;function vf(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=xr(e,t,!1)*Nf,r=i=>xr(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=Mr(r,Math.max(e+1,o),1/0);return Math.min(1,pn(e,t,!0,!1)+Xo(i,t))}let s=Er(r,0,Math.min(e-1,o));return Math.min(1,pn(s,t,!0,!1)+Xo(e,t))}var nr=2**-1022;function Xo(e,t){return e-(e-1)===1?pn(e-1,t,!1,!1):pn(e,t,!1,!1)+xr(e,t,!1)}function tn(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 yf(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=nr&&(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]=tn(e),[f,m]=tn(t),[u,p]=tn(n),[w,g]=tn(r),d=f*w,N=p-m-g,b=N>=0?u/(u+I(d,-N)):u/(I(u,N)+d);return I(a*f*b,l+m+Math.min(N,0))}function Mf(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=nr&&(o===0||o>=nr)?i:e===0?0:e/t*(r/n)}function Ef(e,t={}){z(t,bf,"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(Zt))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(N=>Number.isFinite(N)&&N>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=fe(t.alternative),l=t.confLevel??.95;if(o===2?Qe(l):Vt(l),o===2){let[N,b]=r,[v,y]=i,h=N+b;if(h===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let E=c*v,x=E+y,M=Number.isFinite(x)&&x>=nr&&(E===0||E>=nr)?E/x:1/(1+y/v/c),T=Ho(Math.round(N),Math.round(N)+Math.round(b),{p:M,alternative:a,confLevel:l}),R=y/v,S=k=>{if(k===0)return 0;let A=k/(1-k);if(Number.isFinite(R)||A===1/0)return A*R;let[_,q]=tn(A),[C,O]=tn(v),[D,W]=tn(y);return I(_*D/C,q+W-O)};return{method:"Comparison of Poisson rates",alternative:a,statistic:N,statisticName:"count1",parameter:yf(h,c,v,y),parameterName:"expected count1",pValue:T.pValue,confInt:[S(T.confInt[0]),S(T.confInt[1])],confLevel:l,estimate:Mf(N,b,v,y),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,p;a==="less"?p=pn(f,u,!0,!1):a==="greater"?p=Xo(f,u):p=vf(f,u);let w=N=>f===0?0:yr(N,f,1,!0,!1),g=N=>N<1e-8?yr(N,f+1,1,!1,!1):yr(1-N,f+1,1,!0,!1),d;if(a==="less")d=[0,g(1-l)];else if(a==="greater")d=[w(1-l),1/0];else{let N=(1-l)/2;d=[w(N),g(N)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:p,confInt:[d[0]/m,d[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var bi=["p","alternative","confLevel","correct"],xf="Chi-squared approximation may be incorrect";function Bo(e,t,n={}){De(t)&&z(t,bi,"propTest"),z(n,bi,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(k=>k[0]),o=S.map(k=>k[0]+k[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let i=r.length,c=[];for(let S=0;S<i;S++)!Number.isNaN(r[S])&&!Number.isNaN(o[S])&&c.push(S);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(S=>r[S]),o=c.map(S=>o[S]),o.some(S=>!(S>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(S=>S===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(S=>!(S>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((S,k)=>S>o[k]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let S=typeof s.p=="number"?[s.p]:Array.from(s.p);if(S.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(k=>S[k]),l.some(k=>!(k>0&&k<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;a===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;Qe(m);let u=fe(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let p=r.map((S,k)=>S/o[k]),w=a===2?p[0]-p[1]:0,g=a===2?1/o[0]+1/o[1]:0,d=(s.correct??!0)&&a<=2?.5:0,N=!1;if(d>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=d&&([d,N]=[S,!0])}else if(d>0&&a===2&&!f){let S=Math.abs(w)/g;S<=d&&([d,N]=[S,!0])}let b=Tf(r,o),v=o.some((S,k)=>{let A=l?.[k]??b;return S*A<5||S*(1-A)<5}),y=0;if(!N)for(let S=0;S<a;S++){let k=l?.[S]??b,A=o[S]*k,_=o[S]*(1-k);y+=Oe(Math.abs(r[S]-A)-d,A),y+=Oe(Math.abs(o[S]-r[S]-_)-d,_)}let h=l!==void 0?a:a-1,E;if(u==="two.sided")E=xe(y,h/2,2,!1,!1);else{let S=Math.sign(a===1?p[0]-l[0]:w);E=Y(S*Math.sqrt(y),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,k=u==="two.sided"&&S<1e-8?Z(S,{lowerTail:!1}):Z(u==="two.sided"?(1+m)/2:m);if(a===1){let A=o[0],_=k*k,q=(F,L)=>(F+_/(2*A)+L*k*Math.sqrt(F*(1-F)/A+_/(4*A*A)))/(1+_/A),C=p[0]+d/A,O=p[0]-d/A,D=Math.min(1,Math.max(0,C>=1?1:q(C,1))),W=Math.min(1,Math.max(0,O<=0?0:q(O,-1)));x=u==="two.sided"?[W,D]:u==="greater"?[W,1]:[0,D]}else{let A=Math.sqrt(p.reduce((O,D,W)=>O+D*(1-D)/o[W],0)),_=k*A+d*g,q=Math.max(-1,w-_),C=Math.min(1,w+_);x=u==="two.sided"?[q,C]:u==="greater"?[q,1]:[-1,C]}}let M=a===1?"p":c.map(S=>`prop ${S+1}`),R={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${d!==0?"with":"without"} continuity correction`,alternative:u,statistic:y,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:E,estimate:a===1?p[0]:p,estimateNames:M,warnings:v?[xf]:[]};return l!==void 0&&(R.nullValue=a===1?l[0]:l,R.nullValueName=M),x!==void 0&&(R.confInt=x,R.confLevel=m),j("propTest",R)}function Tf(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+I(s,-r),0)/t.reduce((o,s)=>o+I(s,-r),0)}function kn(e){let t=Fe(e);return t.some(n=>typeof n=="string")?t.map(n=>typeof n=="number"&&!Number.isNaN(n)?Pe(n):n):t}function rr(e,t){return typeof e=="number"&&typeof t=="number"?e-t:it(String(e),String(t))}function $t(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=kn(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(rr);return{labels:i.map(Pe),groups:i.map(c=>s.get(c))}}function nn(e,t,n="drop"){return $t(e,t,n).groups}function Hr(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:Ct(t[n/2-1],t[n/2])}function Qo(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 Ni=["correct"];function Rf(e,t,n={}){De(t)&&z(t,Ni,"mcnemarTest"),z(n,Ni,"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=Sf(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=vi(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],p=r[m][f];u+p!==0&&(i+=vi(u-p,u,p),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:xe(i,a/2,2,!1,!1),warnings:[]}}function vi(e,t,n){let r=t+n;return r<1/0?Oe(e,r):Oe(I(e,-520),I(t,-1040)+I(n,-1040))}function Sf(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=kn(e),o=kn(t);for(let m=0;m<e.length;m++){let u=r[m],p=o[m];typeof u=="number"&&Number.isNaN(u)||typeof p=="number"&&Number.isNaN(p)||n.push([u,p])}let s=m=>Array.from(new Set(m)).sort(rr),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 kf=["ratio","alternative","confLevel"],Zo=2**-1022;function yi(e,t,n={}){if(z(n,kf,"varTest"),typeof e=="string"){let v=Dt(e,t);return yi(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=fe(n.alternative),c=n.confLevel??.95;Qe(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]=_e([r,o]).variance,u=0;if(!(f>=Zo&&m>=Zo)){let v=_e([r]),y=_e([o]);f=v.variance[0],m=y.variance[0],u=2*(v.k-y.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let p=u===0?f/m:Ce(f/m,u),w=p/s,g=we(w,a,l,!0,!1),d=we(w,a,l,!1,!1),N,b;if(i==="less")N=g,b=[0,p/kt(1-c,a,l,!0,!1)];else if(i==="greater")N=d,b=[p/kt(c,a,l,!0,!1),1/0];else{N=Math.min(1,2*Math.min(g,d));let v=(1-c)/2,y=v<1e-8?kt(v,a,l,!1,!1):kt(1-v,a,l,!0,!1);b=[p/y,p/kt(v,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:w,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:N,confInt:b,confLevel:c,estimate:p,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function Mi(e,t){if(typeof e=="string"){let w=Qn(e,t);return Mi(Aa(w.values,w.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(w=>Array.from(w).filter(Number.isFinite));if(o.some(w=>w.length<2))throw new RangeError("there must be at least 2 observations in each group");let s=o.map(w=>w.length-1),i=_e(o).variance,c=0,a=0,l=0,f=0;for(let w=0;w<r;w++)c+=s[w],a+=s[w]*i[w],l+=s[w]*Math.log(i[w]),f+=1/s[w];a/=c;let m=c*Math.log(a)-l;if(i.some(w=>w<Zo)){let w=o.map(y=>{let h=_e([y]);return{w:h.variance[0],e:h.k}}),g=Math.max(...w.map(y=>y.e)),d=w.map(y=>Math.log(y.w)+2*(y.e-g)*Math.LN2),N=d.map((y,h)=>Math.log(s[h])+y),b=Math.max(...N),v=b+Math.log(N.reduce((y,h)=>y+Math.exp(h-b),0))-Math.log(c);m=c*v-d.reduce((y,h,E)=>y+s[E]*h,0)}let u=m/(1+(f-1/c)/(3*(r-1))),p=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:u,statisticName:"Bartlett's K-squared",parameter:p,parameterName:"df",pValue:xe(u,p/2,2,!1,!1),warnings:[]}}var Af=["alternative"];function Jo(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:xe(n,r/2,2,!1,!1),warnings:[]}}function Ei(e){let t=0;for(let n of ut(e))t+=n*n*n-n;return t}function Xr(e,t){if(typeof e=="string"){let l=Qn(e,t);return Xr(l.values,l.group.codes)}let n=nn(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=Ne(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-Ei(o)/(s*s*s-s)),Jo("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function es(e,t){if(typeof e=="string")return es(qo(e,t));let n=Qo(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=Ne(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=Ei(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 Jo("Friedman rank sum test","Friedman chi-squared",a,o-1)}function qf(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{...es(e),method:"Cochran's Q test",statisticName:"Q"}}function xi(e,t){if(typeof e=="string")return xi(qo(e,t));let n=Qo(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(g=>ge(g)-et(g));s.some(g=>g===1/0)&&n.every(g=>g.every(Number.isFinite))&&(s=ee(n).groups.map(g=>ge(g)-et(g)));let i=Ne(s),c=new Array(o).fill(0),a=0;n.forEach((g,d)=>{let N=Ne(g);for(let b=0;b<o;b++){let v=i[d]*(N[b]-(o+1)/2);a+=v*v,c[b]+=v}});let l=0;for(let g of c)l+=g*g;l/=r;let f=o-1,m=(r-1)*(o-1),u,p,w=[f,m];if(a===l){u=NaN,w=[NaN,NaN];let g=0;for(let d=2;d<=o;d++)g+=Math.log(d);p=Math.exp((1-r)*g)}else u=(r-1)*l/(a-l),p=we(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:w,parameterName:["num df","denom df"],pValue:p,warnings:[]}}function Ti(e,t){if(typeof e=="string"){let p=Qn(e,t);return Ti(p.values,p.group.codes)}let n=nn(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=p=>p.flatMap(w=>{let g=Hr(w);return w.map(d=>Math.abs(d-g))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(p=>p===1/0)&&n.every(p=>p.every(Number.isFinite))&&(s=o(ee(n).groups));let i=s.length,a=Ne(s).map(p=>Z((1+p/(i+1))/2)),l=0;for(let p of a)l+=p;let f=l/i,m=0,u=0;for(let p of n){let w=0;for(let g=0;g<p.length;g++)w+=a[u+g];u+=p.length,m+=p.length*(w/p.length-f)**2}return m/=qe(a),Jo("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function Ri(e,t,n={}){if(z(n,Af,"moodTest"),typeof e=="string"){let y=Dt(e,t);return Ri(y.x,y.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=fe(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},(y,h)=>h).sort((y,h)=>l[y]-l[h]),m=new Array(a),u=0;for(;u<a;){let y=u;for(;y+1<a&&l[f[y+1]]===l[f[u]];)y++;let h=0;for(let E=u;E<=y;E++)h+=(E+1-(a+1)/2)**2;h/=y-u+1;for(let E=u;E<=y;E++)m[f[E]]=h;u=y+1}let p=0;for(let y=0;y<i;y++)p+=m[y];let w=0;for(let y of m)w+=y;w/=a;let g=0;for(let y of m)g+=(y-w)**2;let d=i*w,N=i*c/(a*(a-1))*g,b=(p-d)/Math.sqrt(N),v=s==="less"?Y(b):s==="greater"?Y(b,{lowerTail:!1}):2*Y(-Math.abs(b));return{method:"Mood two-sample test of scale",alternative:s,statistic:b,statisticName:"Z",pValue:v,warnings:[]}}function rn(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var _f=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],If=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],Lf=[.544,-.39978,.025054,-6714e-7],Of=[1.3822,-.77857,.062767,-.0020322],Ff=[-1.5861,-.31082,-.083751,.0038915],Pf=[-.4803,-.082676,.0030302],Cf=[-2.273,.459];function Df(e){if(!(e>=3)||e!==Math.floor(e))throw new RangeError("n must be an integer of at least 3");let t=new Float64Array(e);if(e===3)return t[0]=-Math.SQRT1_2,t[2]=Math.SQRT1_2,t;let n=Math.floor(e/2),r=new Float64Array(n),o=0;for(let m=0;m<n;m++)r[m]=-Z((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),i=1/Math.sqrt(e),c=r[0]/s+rn(_f,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+rn(If,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 Si(e){let t=ee([e]).groups[0],n=t.length,r=Df(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 Vf=1.90985931710274,zf=1.0471975511966;function ki(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=Vf*(Math.asin(Math.sqrt(1-e))-zf);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=rn(Cf,t);if(n>=i)return 1e-99;let c=rn(Lf,t),a=Math.exp(rn(Of,t));return Y(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=rn(Ff,r),s=Math.exp(rn(Pf,r));return Y(n,{mean:o,sd:s,lowerTail:!1})}var Ii=Math.log(2*Math.PI),Ai=2e-14,qi=500;function $f(e){return Math.exp(-.5*e*e-.5*Ii)}function Wf(e){return Y(e,{logP:!0})}function Li(e,t=-1/0,n=!1){let r=t===-1/0?Ao(e,0,2,0,Ai,qi):Ao(e,t,1,0,Ai,qi),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 _i(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Li(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=Wf(l);continue}let f=(r[a]*i-e)/o[a],m=Y(f)+Y(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?Y(l)-Y(f):Y(f,{lowerTail:!1})-Y(l,{lowerTail:!1})))}return $f(i)*-Math.expm1(c)})}function on(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,_i(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Ii-aa(o);return Math.min(1,Li(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*_i(e*Math.exp(c),t,r)},-1/0,!0))}var jf=["pAdjustMethod"],Kf=["pAdjustMethod","alternative"],Uf=["alternative"],Yf=["pAdjustMethod","alternative"],Gf=["method","altp"],Hf=["alternative"],Oi="Ties are present. z-quantiles were corrected for ties.";function At(e,t){let{labels:n,groups:r}=$t(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 ts(e){let t=e.groups.flat(),n=Ne(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 ut(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function or(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 Xf(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=Sn(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 Qr(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 ns(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 Br=e=>Y(e,{lowerTail:!1}),rs=(e,t,n)=>qr(Math.abs(e),t,n,{lowerTail:!1});function os(e){let t=_e(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return sr(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function sr(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 Bf(e,t,n={}){z(n,jf,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=At(e,t),{R:s,C:i}=ts(o),c=o.N*(o.N+1)/12-i,a=or(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=Xf(a.map(f=>f.map(m=>2*Br(m))),r);return j("kwAllPairsDunnTest",Qr(o,"Dunn's all-pairs test",a,l,r,i!==0?[Oi]:[]))}function Qf(e,t,n={}){z(n,Kf,"kwManyOneDunnTest");let r=fe(n.alternative),o=n.pAdjustMethod??"single-step",s=At(e,t),{R:i,C:c}=ts(s),a=s.N*(s.N+1)/12-c,l=s.n.slice(1).map((p,w)=>w+1),f=l.map(p=>(i[p]-i[0])/Math.sqrt(a*(1/s.n[0]+1/s.n[p]))),m=l.map(p=>s.n[p]/(s.n[p]+s.n[0])),u=Fi(f,m,r,o);return j("kwManyOneDunnTest",ns(s,"Dunn's many-to-one test",f,u,o,c!==0?[Oi]:[]))}function Fi(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?on(o,t,1/0,!0):on(n==="greater"?o:-o,t,1/0,!1)):Sn(e.map(o=>n==="two.sided"?2*Br(Math.abs(o)):Br(n==="greater"?o:-o)),r)}function Zf(e,t,n={}){z(n,Uf,"dunnettTest");let r=fe(n.alternative),o=At(e,t),{m:s,s2:i,df:c}=os(o),a=Math.sqrt(i),l=o.n.slice(1).map((p,w)=>w+1),f=l.map(p=>(s[p]-s[0])/(a*Math.sqrt(1/o.n[p]+1/o.n[0]))),m=l.map(p=>o.n[p]/(o.n[p]+o.n[0])),u=f.map(p=>r==="two.sided"?Math.min(1,2*on(Math.abs(p),m,c,!1)):on(r==="greater"?p:-p,m,c,!1));return j("dunnettTest",ns(o,`Dunnett's-test for multiple
|
|
3
|
+
comparisons with one control`,f,u,"single-step"))}function Jf(e,t,n={}){z(n,Yf,"manyOneUTest");let r=fe(n.alternative),o=n.pAdjustMethod??"single-step",s=At(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
|
|
4
4
|
|
|
5
|
-
k = 3 groups are required.`);let
|
|
6
|
-
for multiple comparisons with one control`,a,f,o))}function jf(e,t){let n=It(e,t),r=ir(n.k,(s,c)=>{let i=n.n[s],a=n.n[c],l=[...n.groups[s],...n.groups[c]],f=ve(l),m=0;for(let N=0;N<i;N++)m+=f[N];let u=m-i*(i+1)/2,p=Math.min(u,i*a-u),g=i+a,w=0;for(let N of ut(l))w+=N*N*N-N;let h=i*a/(g*(g-1))*((g*g*g-g)/12-w/12);return Math.SQRT2*(p-i*a/2)/Math.sqrt(h)}),o=r.map(s=>s.map(c=>Number.isNaN(c)?NaN:Zo(c,n.k,1/0)));return j("dscfAllPairsTest",Jr(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function Kf(e,t){let n=It(e,t),r=Ie(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),c=ir(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),i=ir(n.k,(a,l)=>{let f=ba([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return Zo(c[a-1][l],n.k,f)});return j("gamesHowellTest",Jr(n,"Games-Howell test",c,i,"none"))}function Uf(e,t){let n=It(e,t),{m:r,s2:o,df:s}=Jo(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let c=ir(n.k,(a,l)=>(r[a]-r[l])/Math.sqrt(o*(1/n.n[a]+1/n.n[l])/2)),i=c.map(a=>a.map(l=>Number.isNaN(l)?NaN:Zo(l,n.k,s)));return j("tukeyTest",Jr(n,"Tukey's test",c,i,"single-step"))}function Gf(e,t,n={}){V(n,Pf,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=It(e,t),c=Qr(e,t).statistic,{R:i,C:a}=Bo(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let g=1;g<s.k;g++)for(let w=0;w<g;w++)f.push((i[w]-i[g])/Math.sqrt(l*(1/s.n[w]+1/s.n[g]))),m.push(`${s.labels[w]} - ${s.labels[g]}`);let u=f.map(g=>(o?2:1)*Zr(Math.abs(g))),p=Yf(u,r);return{chi2:c,Z:f,P:u,PAdjusted:p,zValue:f,pValue:u,pAdjusted:p,comparisons:m,warnings:[]}}function Yf(e,t){let n=e.length,r=(s,c)=>Math.min(1,-Math.expm1(c*Math.log1p(-s))),o=s=>{let c=e.map((a,l)=>l).sort((a,l)=>(s?e[l]-e[a]:e[a]-e[l])||a-l),i=new Array(n);return c.forEach((a,l)=>i[a]=l>0&&e[a]===e[c[l-1]]?i[c[l-1]]:l+1),i};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((c,i)=>Math.min(1,(n+1-s[i])*c))}case"hs":{let s=o(!1);return e.map((c,i)=>r(c,n+1-s[i]))}case"hochberg":{let s=o(!0);return e.map((c,i)=>Math.min(1,s[i]*c))}case"bh":{let s=o(!0);return e.map((c,i)=>Math.min(1,n/(n+1-s[i])*c))}case"by":{let s=o(!0),c=0;for(let i=1;i<=n;i++)c+=1/i;return e.map((i,a)=>Math.min(1,n/(n+1-s[a])*c*i))}default:throw new RangeError(`invalid method "${String(t)}"`)}}function Hf(e,t,n={}){V(n,Df,"glhtDunnett");let r=fe(n.alternative),o=It(e,t),{m:s,s2:c,df:i,k:a}=Jo(o),l=Math.sqrt(c),f=o.n.slice(1).map((M,v)=>v+1),m=f.map(M=>s[M]-s[0]),u=f.map(M=>l*Math.sqrt(1/o.n[M]+1/o.n[0])),p=m.map((M,v)=>M/u[v]),g=m.map(M=>q(M,a)),w=u.map(M=>q(M,a)),h=f.map(M=>o.n[M]/(o.n[M]+o.n[0])),N=p.map(M=>r==="two.sided"?rn(M,h,i,!0):rn(r==="greater"?M:-M,h,i,!1));return{names:f.map(M=>`${o.labels[M]} - ${o.labels[0]}`),estimate:g,se:w,t:p,p:N,stdError:w,tValue:p,pValue:N,df:i,warnings:[]}}var Ai=["varEqual"],Xf=2**-1022;function Ii(e,t,n={}){if(typeof e!="string"&&Ve(t)&&V(t,Ai,"onewayTest"),V(n,Ai,"onewayTest"),typeof e=="string"){let h=La(e,t);return Ii(h.values,h.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=tn(e,r),s=Ie(o),c=s.groups,i=n.varEqual??!1,a=c.length;if(a<2)throw new RangeError("not enough groups");let l=c.map(h=>h.length);if(l.some(h=>h<2))throw new RangeError("not enough observations");let f=s.mean,m=s.variance,u=l.reduce((h,N)=>h+N,0),p,g,w;if(i){let h=s.grandMean,N=0,M=0;for(let v=0;v<a;v++)N+=l[v]*(f[v]-h)**2,M+=(l[v]-1)*m[v];g=u-a,cr(g,s.ssWithin),p=N/(a-1)/(M/g)}else if(m.some(h=>!(h>=Xf))&&(w=Qf(o))!==void 0){let h=w.map((y,R)=>qi(l[R]/y.variance,-2*y.k)),N=Math.max(...h.map(y=>y.e)),M=h.map(y=>De(y.m,y.e-N)),v=M.reduce((y,R)=>y+R,0),b=0;for(let y=0;y<a;y++){let R=M.reduce((T,k,S)=>S===y?T:T+k,0);b+=(R/v)**2/(l[y]-1)}b/=a*a-1;let d=[];for(let y=0;y<a;y++)for(let R=y+1;R<a;R++){let[T,k]=w[y].k>=w[R].k?[w[y],w[R]]:[w[R],w[y]],S=T.mean-De(k.mean,k.k-T.k);if(S===0)continue;let A=qi(S,T.k);d.push({m:h[y].m*h[R].m*A.m*A.m/v,e:h[y].e+h[R].e+2*A.e-N})}let E=Math.max(...d.map(y=>y.e)),x=d.reduce((y,R)=>y+De(R.m,R.e-E),0);p=(d.length===0?0:De(x,E))/((a-1)*(1+2*(a-2)*b)),g=1/(3*b)}else if(m.some(h=>h===0)){let h=m.flatMap((b,d)=>b===0?[d]:[]);if(h.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let N=h[0],M=0,v=0;for(let b=0;b<a;b++)b!==N&&(M+=1/(l[b]-1),v+=l[b]/m[b]*(f[b]-f[N])**2);M/=a*a-1,p=v/((a-1)*(1+2*(a-2)*M)),g=1/(3*M)}else{let h=l.map((d,E)=>d/m[E]),N=h.reduce((d,E)=>d+E,0),M=0,v=0;for(let d=0;d<a;d++)M+=(1-h[d]/N)**2/(l[d]-1),v+=h[d]*f[d];M/=a*a-1,v/=N;let b=0;for(let d=0;d<a;d++)b+=h[d]*(f[d]-v)**2;p=b/((a-1)*(1+2*(a-2)*M)),g=1/(3*M)}return{method:i?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:p,statisticName:"F",parameter:[a-1,g],parameterName:["num df","denom df"],pValue:we(p,a-1,g,!1,!1),warnings:[]}}function Bf(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=Ni(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:Mi(r,n),warnings:[]}}function Qf(e){let t=[];for(let n of e){let r=Ie([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function qi(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=De(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function es(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=Z(r,[e[o],0]);return r[0]}function Zf([[e,t],[n,r]]){let o=e+n,s=t+r,c=e+t,i=Math.max(0,c-s),a=Math.min(c,o),l=new Float64Array(a-i+1);for(let f=i;f<=a;f++)l[f-i]=gn(f,o,s,c,!1);return{lo:i,d:l}}function Jf(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 eo=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=Zf(o);n+=s.lo,r=Jf(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 i=0;i<n;i++){let a=this.logNull[i]+(this.lo+i)*o;r[i]=a,a>s&&(s=a)}for(let i=0;i<n;i++)r[i]=Math.exp(r[i]-s);let c=es(r,0,n);for(let i=0;i<n;i++)r[i]=r[i]/c;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=Z(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?es(o,s,o.length):es(o,0,s+1)}};function ts(e,t){return Kr(e,t)}function _i(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:ts(r=>e.mean(r)-t,n>t)}function ns(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:ts(o=>n-e.cdf(t,o,!1),r<n)}function rs(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:ts(o=>e.cdf(t,o,!0)-n,r>n)}var em=["alternative","correct","exact","confLevel"];function tm(e,t={}){V(t,em,"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(os);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let c=fe(t.alternative),i=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(Qe(l),r===2&&o===2){if(a&&e.some(m=>m.some(u=>u.some(p=>!Number.isInteger(p)))))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?om(f,c,l):nm(f,s,c,i,l)}return sm(e,s,r,o)}function os(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Fi(e){let t=0,n=0;for(let o of e)for(let s of o)for(let c of s)t=Math.max(t,c),n++;let r=Math.floor(Math.log2(t))+1+Math.ceil(Math.log2(8*n))-1020;return r<=0?0:r+r%2}function Pi(e,t){return e.map(n=>n.map(r=>r.map(o=>q(o,-t))))}function Li(e,t,n){let r=0,o=0,s=0,c=0,i=0,a=0,l=0,f=0;return e.forEach(([[m,u],[p,g]],w)=>{let h=t[w],N=m+u,M=p+g,v=m+p,b=u+g;r+=m;let d=N*v/h;o+=Number.isFinite(d)?d:N/h*v;let E=N*M*v*b/(h*h*(h-n));s+=Number.isFinite(E)?E:N/h*(M/h)*(v/(h-n))*b;let x=(m+g)/h,y=(u+p)/h,R=m*g/h,T=u*p/h,k=Number.isFinite(R)?R:m/h*g,S=Number.isFinite(T)?T:u/h*p;c+=k,i+=S,a+=x*k,l+=x*S+y*k,f+=y*S}),{sa:r,se:o,sv:s,sR:c,sS:i,sPR:a,sPSQR:l,sQS:f}}function nm(e,t,n,r,o){let s=Li(e,t,1),c=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){c=Fi(e);let x=Pi(e,c);s=Li(x,x.map(os),q(1,-c))}let{sa:i,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:p,sQS:g}=s,w=i-a,h=q(.5,-c),N=r&&Math.abs(w)>=h?h:0,M=q(Oe(Math.abs(w)-N,l),c),v=n==="two.sided"?xe(M,.5,2,!1,!1):U(Math.sign(w)*Math.sqrt(M),{lowerTail:n==="less"}),b=f/m,d=q(Math.sqrt(u/(2*f*f)+p/(2*f*m)+g/(2*m*m)),-c/2),E;if(n==="less")E=[0,b*Math.exp(Q(o)*d)];else if(n==="greater")E=[b*Math.exp(Q(o,{lowerTail:!1})*d),1/0];else{let x=(1-o)/2,y=x<1e-8?Q(x,{lowerTail:!1}):Q((1+o)/2);E=[b*Math.exp(-y*d),b*Math.exp(y*d)]}return{method:`Mantel-Haenszel chi-squared test ${N!==0?"with":"without"} continuity correction`,alternative:n,statistic:M,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:v,confInt:E,confLevel:o,estimate:b,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var rm=2**24;function om(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>rm)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new eo(e),s=e.reduce((a,l)=>a+l[0][0],0),c;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=Z(f,[m,0]));c=Math.min(1,f[0])}else c=o.cdf(s,1,t==="greater");let i;if(t==="less")i=[0,ns(o,s,1-n)];else if(t==="greater")i=[rs(o,s,1-n),1/0];else{let a=(1-n)/2;i=[rs(o,s,a),ns(o,s,a)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:s,statisticName:"S",pValue:c,confInt:i,confLevel:n,estimate:_i(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function sm(e,t,n,r){let{diff:o,V:s}=Oi(e,t,n,r,1),c=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){c=Fi(e);let f=Pi(e,c);({diff:o,V:s}=Oi(f,f.map(os),n,r,q(1,-c)))}let i=o.length,a=Ce(s,o),l=0;for(let f=0;f<i;f++)l+=o[f]*a[f];return l=q(l,c),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:i,parameterName:"df",pValue:xe(l,i/2,2,!1,!1),warnings:[]}}var am=2**-1022;function Oi(e,t,n,r,o){let s=(n-1)*(r-1),c=new Array(s).fill(0),i=Array.from({length:s},()=>new Array(s).fill(0));return e.forEach((a,l)=>{let f=t[l],m=a.map(g=>g.reduce((w,h)=>w+h,0)),u=Array.from({length:r},(g,w)=>a.reduce((h,N)=>h+N[w],0)),p=1/(f*f*(f-o));for(let g=0;g<r-1;g++)for(let w=0;w<n-1;w++){let h=w+(n-1)*g,N=m[w]*u[g]/f;c[h]+=a[w][g]-(Number.isFinite(N)?N:Ir(m[w],u[g],f));for(let M=0;M<r-1;M++)for(let v=0;v<n-1;v++){let b=(w===v?f*m[w]:0)-m[w]*m[v],d=(g===M?f*u[g]:0)-u[g]*u[M],E=p*b*d;i[h][v+(n-1)*M]+=Number.isFinite(E)&&p>=am?E:m[w]*((w===v?1:0)-m[v]/f)*(((g===M?1:0)-u[M]/f)*(u[g]/(f-o)))}}}),{diff:c,V:i}}var Di=new Map,im=2**24,cm=2**32;function Ci(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>im||n*r>cm)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function lm(e,t){let n=`${e},${t}`,r=Di.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 c=s.reduce((u,p)=>u+p,0),i=Array.from({length:e+1},()=>new Float64Array(c+1));i[0][0]=1;for(let u=0;u<o;u++){let p=o-u,g=s[u],w=Array.from({length:e+1},()=>new Float64Array(c+1));for(let h=0;h<=e;h++){let N=i[h],M=e-h;if(M>p)continue;let v=M/p;for(let b=0;b<=c;b++){let d=N[b];d!==0&&(M>0&&(w[h+1][b+g]+=d*v),M<p&&(w[h][b]+=d*(1-v)))}}i=w}let a=i[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 Di.set(n,m),m}function ss(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}=lm(t,n),c=ca(e)-o,i=0;if(r)for(let a=0;a<=Math.min(c,s.length-1);a++)i+=s[a];else for(let a=Math.max(c+1,0);a<s.length;a++)i+=s[a];return Math.min(i,1)}var Vi=new Map;function fm(e,t){let n=`${e},${t}`,r=Vi.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 c=Array.from({length:e+1},()=>new Float64Array(s+1));c[0][0]=1;for(let m=0;m<o;m++){let u=Math.min(m+1,o-m),p=Array.from({length:e+1},()=>new Float64Array(s+1));for(let g=0;g<=e;g++){if(e-g>o-m)continue;let w=c[g];for(let h=0;h<=s;h++){let N=w[h];N!==0&&(g<e&&(p[g+1][h+u]+=N),e-g<o-m&&(p[g][h]+=N))}}c=p}let i=c[e],a=0;for(;i[a]===0;)a++;let l=i.length-1;for(;i[l]===0;)l--;let f={min:a,counts:i.slice(a,l+1)};return Vi.set(n,f),f}function to(e,t,n){let{min:r,counts:o}=fm(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=va(t+n,t),c=0;for(let i=0;i<o.length;i++)if(c+=o[i]/s,c>=e)return r+i;return r+o.length-1}var mm=["alternative","exact","confInt","confLevel"];function ji(e,t,n={}){if(V(n,mm,"ansariTest"),typeof e=="string"){let d=Ct(e,t);return ji(d.x,d.y,n)}let r=Array.from(e).filter(d=>!Number.isNaN(d)),o=Array.from(t).filter(d=>!Number.isNaN(d)),s=r.length,c=o.length;if(s<1)throw new RangeError("not enough 'x' observations");if(c<1)throw new RangeError("not enough 'y' observations");let i=fe(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&Qe(l);let f=s+c,m=ve(r.concat(o)),u=m.map(d=>Math.min(d,f-d+1)),p=0;for(let d=0;d<s;d++)p+=u[d];let g=new Set(m).size<f,w=n.exact??(s<50&&c<50&&!g),h=n.exact??(s<50&&c<50),N=new Te,M,v;if(w&&!g){Ci(s,c);let d=x=>ss(x-1,s,c,!1),E=x=>ss(x,s,c,!0);if(i==="two.sided"){let x=Math.floor((s+1)**2/4)+Math.floor(s*c/2)/2;M=Math.min(1,2*(p>x?d(p):E(p)))}else i==="less"?M=d(p):M=E(p);a&&(v=dm(r,o,i,l,N))}else{let d=0;for(let T of u)d+=T*T;let E=f%2===0,x=E?s*(f+2)/4:s*(f+1)**2/(4*f),y=E?s*c*(16*d-f*(f+2)**2)/(16*f*(f-1)):s*c*(16*f*d-(f+1)**4)/(16*f*f*(f-1)),R=(p-x)/Math.sqrt(y);M=i==="two.sided"?2*Math.min(U(R),U(R,{lowerTail:!1})):i==="less"?U(R,{lowerTail:!1}):U(R),a&&(v=gm(r,o,i,l,h?new Te:N)),h&&g&&(N.add("cannot compute exact p-value with ties"),a&&N.add("cannot compute exact confidence intervals with ties"))}let b={method:"Ansari-Bradley test",alternative:i,statistic:p,statisticName:"AB",pValue:M,warnings:N.list};return v!==void 0&&(b.confInt=v.confInt,b.confLevel=l,b.estimate=v.estimate,b.estimateNames="ratio of scales"),b.nullValue=1,b.nullValueName="ratio of scales",j("ansariTest",b)}function Ki(e,t,n){let r=e.length,o=r+t.length,s=e.map(f=>f/n),i=n>0&&n<1/0&&s.some((f,m)=>Number.isFinite(e[m])&&(!Number.isFinite(f)||e[m]!==0&&Math.abs(f)<um))?pm(e.map(f=>$i(f,n)).concat(t.map(f=>$i(f,1)))):hm(s.concat(t)),a=i.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(i).size<o}}var um=2**-1022;function $i(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]=zi(Math.abs(e)),[s,c]=zi(t),i=o/c,a=r-s;return i<1&&(i*=2,a-=1),[n,a,i]}function zi(e){let t=Math.floor(Math.log2(e)),n=q(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Wi(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 pm(e){let t=e.map((r,o)=>o).sort((r,o)=>Wi(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Wi(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 hm(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return ve(e);let n=ve(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function no(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function dm(e,t,n,r,o){let s=e.length,c=t.length,i=[];for(let N of e)for(let M of t)N/M>=0&&i.push(N/M);i.sort((N,M)=>N-M);let a=i.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:no(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=i.map((N,M)=>Ki(e,t,N+(M<a-1?(i[M+1]-N)/2:N*1.01)).ab),f=(N,M,v)=>{let b=1/0;for(let E of l)(M?E-N>0:E-N>=0)&&E-N<b&&(b=E-N);let d=v?-1/0:1/0;return l.forEach((E,x)=>{E-N===b&&(d=v?Math.max(d,i[x]):Math.min(d,i[x]))}),d},m=N=>{let M=to(N/2,s,c),v=to(1-N/2,s,c);if(!l.some(E=>E-M>=0)||!l.some(E=>E-v>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let b=f(M,!1,!1),d=f(v,!0,!0);return b>d?[f(v,!1,!1),f(M,!0,!0)]:[b,d]},u=no(n,1-r,m),p=to(.5,s,c),g=-1/0,w=1/0;l.forEach((N,M)=>{N-p<=0?g=Math.max(g,i[M]):w=Math.min(w,i[M])});let h=g===-1/0||w===1/0?NaN:(w+g)/2;return h===1/0&&(h=w/2+g/2),{confInt:u,estimate:h}}function gm(e,t,n,r,o){let s=e.length,c=t.length,i=s+c,a=i%2===0,l=(v,b)=>{let{ab:d,a:E,ties:x}=Ki(e,t,v),y=a?d-s*(i+2)/4:d-s*(i+1)**2/(4*i),R;if(!x)R=Math.sqrt(a?s*c*(i+2)*(i-2)/(48*(i-1)):s*c*(i+1)*(3+i*i)/(48*i*i));else{let T=0;for(let k of E)T+=k*k;R=Math.sqrt(a?s*c*(16*T-i*(i+2)**2)/(16*i*(i-1)):s*c*(16*i*T-(i+1)**4)/(16*i*i*(i-1)))}return y/R-b},f=[],m=(v,b)=>{v.length===0||b.length===0||f.push(et(v)/ge(b),ge(v)/et(b))};if(m(e.filter(v=>v>0),t.filter(v=>v>0)),m(e.filter(v=>v<=0),t.filter(v=>v<0)),f.some(v=>!Number.isFinite(v))){o.add("cannot compute asymptotic confidence set or estimator");return}if(f.length===0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),{confInt:no(n,1-r,()=>[NaN,NaN]),estimate:NaN};let u=et(f),p=ge(f),g=v=>wt(b=>l(b,v),u,p,{tol:1e-4}),w=v=>{let b=Q(v/2),d=Q(v/2,{lowerTail:!1});if(l(u,b)>0||l(p,d)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=g(b),x=g(d);return E<=x?[E,x]:[x,E]},h=no(n,1-r,w),N=l(u,0)>0||l(p,0)<0;N&&o.add("cannot compute estimate, returning NA");let M=N?NaN:g(0);return{confInt:h,estimate:M}}var wm=["alternative","confLevel"];function bm(e,t,n={}){V(n,wm,"brunnerMunzelTest");let r=Array.from(e).filter(y=>!Number.isNaN(y)),o=Array.from(t).filter(y=>!Number.isNaN(y)),s=r.length,c=o.length;if(s<2)throw new RangeError("not enough 'x' observations");if(c<2)throw new RangeError("not enough 'y' observations");let i=fe(n.alternative),a=n.confLevel??.95;$t(a);let l=ve(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=y=>y.reduce((R,T)=>R+T,0)/y.length,p=u(f),g=u(m),w=(y,R,T)=>{let k=(y.length+1)/2,S=0;for(let A=0;A<y.length;A++)S+=(y[A]-R[A]-T+k)**2;return S/(y.length-1)},h=w(f,ve(r),p),N=w(m,ve(o),g),M=(g-(c+1)/2)/s,v=s*h+c*N,b=s*c*(g-p)/(s+c)/Math.sqrt(v),d=v**2/((s*h)**2/(s-1)+(c*N)**2/(c-1)),E;i==="greater"?E=Ke(b,d,!0,!1):i==="less"?E=Ke(b,d,!1,!1):E=2*Ke(-Math.abs(b),d,!0,!1);let x=gt((1-a)/2,d,!1,!1)*Math.sqrt(h/(s*c**2)+N/(c*s**2));return{method:"Brunner-Munzel Test",alternative:i,statistic:b,statisticName:"Brunner-Munzel Test Statistic",parameter:d,parameterName:"df",pValue:E,confInt:[M-x,M+x],confLevel:a,estimate:M,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var vm=[1.2337005501361697,7831619385924639e-32];function Nm(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let p=0;p<t;p++)for(let g=0;g<t;g++){let w=f[p*t+g];if(w!==0)for(let h=0;h<t;h++)u[p*t+h]+=w*m[g*t+h]}return u},o=f=>{let m=0;for(let g of f)m=Math.max(m,Math.abs(g));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let p=2**-u;for(let g=0;g<f.length;g++)f[g]*=p;return u},s,c=0,i=e.slice(),a=0,l=n;for(;l>0;)l&1&&(s=s===void 0?i.slice():r(s,i),c+=a,c+=o(s)),l>>=1,l>0&&(i=r(i,i),a*=2,a+=o(i));return[s,c]}var Mm=2**33;function Gi(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>Mm)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 Yi(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=1/(2*e))return 0;if(t>=1)return 1;if(e*t<1){let[f,m]=Ht(e,t),u=2*f-1+2*m,p=1,g=0;for(let w=1;w<=e;w++)p*=w/e*u,p<2**-500&&(p*=2**500,g-=500);for(;g<-1e3;)p*=2**-1e3,g+=1e3;return p*2**g}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 p=1;for(let g=2;g<=u;g++)p*=g;s[f*r+m]/=p}}let[c,i]=Nm(s,r,e),a=c[(n-1)*r+n-1],l=i;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 as(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=Se(vm,Ht(e,e)),s=o[0]+o[1],c=Math.exp(-o[0])*(1-o[1]),i=0;for(let l=2;l<100;l++){let f=Math.exp(-(2*l-1)*(2*l-1)*s);if(i+=f,f<=1e-17*(i+c))break}if(c+i===0)return t?0:1;let a=Math.sqrt(2*Math.PI)/e*(c+i);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 is(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=0)return 1;if(t>1)return 0;let[n,r]=Ht(e,t),o=Math.floor(e-n-r),s=0;for(let c=0;c<=o;c++){let i=Z(Tt(e-c,-n),[-r,0]);if(i[0]<=0)break;let a=Z(Tt(c,n),[r,0]),l=Se(a,[e,0]),f=Se(i,[e,0]);s+=aa(c,e-c,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var Ui=2**28;function Hi(e,t){let[n,r]=Ht(e,t),o=Math.floor(n);o===n&&r<0&&(o-=1);let s=[];for(let p=0;p<=e;p++){let g=(p-n-r)/e;g>0&&g<1&&s.push({t:g,up:!0,i:p});let w=(p+n+r)/e;w>0&&w<1&&s.push({t:w,up:!1,i:p})}if(s.length*(2*o+1)*Math.min(e+1,100)>Ui)return NaN;s.sort((p,g)=>p.t-g.t);let c=o+1,i=new Float64Array(e+1),a=new Float64Array(e+1);i[0]=1;let l=0,f=0,m=0,u=0;for(let p of s){let g=(p.t-f)/(1-f);if(g>0){let w=g/(1-g);a.fill(0);for(let h=l;h<c;h++){let N=i[h];if(N===0)continue;let M=e-h,v=Math.min(M,Math.floor((M+1)*g)),b=c-h,d=qt(v,M,g,!1);for(let E=v;E<=M&&d!==0;E++)E>v&&(d=d*(M-E+1)*w/E),E>=b?m+=N*d:a[h+E]+=N*d,u++;d=v>0?qt(v-1,M,g,!1):0;for(let E=v-1;E>=0&&d!==0;E--)E<v-1&&(d=d*(E+1)/((M-E)*w)),E>=b?m+=N*d:a[h+E]+=N*d,u++}if(u>2*Ui)return NaN;[i,a]=[a,i]}if(f=p.t,p.up)c=p.i+1;else{for(let w=l;w<=p.i;w++)m+=i[w],i[w]=0;l=Math.max(l,p.i+1)}}return Math.min(m,1)}function Xi(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,c;if(r.z!==void 0){if(r.z.length!==s)throw new RangeError("z must have length m + n");let u=[...r.z].sort((p,g)=>p-g);c=new Array(s+1).fill(!1);for(let p=1;p<s;p++)c[p]=u[p]!==u[p-1]}let i=e*t*n*(1-1e-10),a=(u,p)=>{let g=u*n-p*t;return(o==="two.sided"?Math.abs(g):o==="greater"?g:-g)>=i},l=new Float64Array(n+1),f=new Float64Array(n+1),m=0;for(let u=0;u<=t;u++){for(let p=0;p<=n;p++){if(u===0&&p===0){f[0]=1;continue}let g=u+p,w=(u>0?u/g*l[p]:0)+(p>0?p/g*f[p-1]:0);(c===void 0?g<s:c[g])&&a(u,p)?(w>0&&(m+=w*gn(u,t,n,g,!1)),f[p]=0):f[p]=w}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var ym=["alternative","exact"];function Bi(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 Qi(e,t,n={}){if(V(n,ym,"ksTest"),typeof e=="string"){let M=Ct(e,t);return Qi(M.x,M.y,n)}let r=Array.from(e).filter(M=>!Number.isNaN(M)).sort((M,v)=>M-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=fe(n.alternative),c=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let M=-1/0,v=-1/0;for(let y=0;y<o;y++){let R=t(r[y]);if(!(R>=0&&R<=1))throw new RangeError(`the distribution function returned ${R} at ${r[y]}, not a value in [0, 1]`);M=Math.max(M,(y+1)/o-R),v=Math.max(v,R-y/o)}let b=s==="two.sided"?Math.max(M,v):s==="greater"?M:v,d=new Set(r).size<o,E=n.exact??(o<100&&!d),x;if(E&&s==="two.sided"&&b<.5&&Gi(o,b),s==="two.sided")if(!E)x=as(Math.sqrt(o)*b,!1);else{let y=2*is(o,b);if(b>=.5)x=Math.min(1,y);else{let R=y<.01?Hi(o,b):NaN;x=Number.isNaN(R)?1-Yi(o,b):R}}else x=E?is(o,b):Math.exp(-2*o*b*b);return j("ksTest",{method:`${E?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Bi(s,!0),exact:E,statistic:b,statisticName:c,pValue:Math.min(1,Math.max(0,x)),warnings:d?["ties should not be present for the one-sample Kolmogorov-Smirnov test"]:[]})}let i=Array.from(t).filter(M=>!Number.isNaN(M)),a=i.length;if(a<1)throw new RangeError("not enough 'y' data");let l=r.map(M=>[M,0]).concat(i.map(M=>[M,1]));l.sort((M,v)=>M[0]-v[0]);let f=0,m=0,u=0,p=0;for(let M=0;M<l.length;M++){if(l[M][1]===0?f++:m++,M+1<l.length&&l[M+1][0]===l[M][0])continue;let v=f/o-m/a;u=Math.max(u,v),p=Math.max(p,-v)}let g=s==="two.sided"?Math.max(u,p):s==="greater"?u:p,w=new Set(l.map(M=>M[0])).size<l.length,h=n.exact??o*a<1e4,N;if(h)N=Xi(g,o,a,w?{alternative:s,z:l.map(M=>M[0])}:{alternative:s});else{let M=o*a/(o+a);N=s==="two.sided"?as(Math.sqrt(M)*g,!1):Math.exp(-2*M*g*g)}return j("ksTest",{method:`${h?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Bi(s,!1),exact:h,statistic:g,statisticName:c,pValue:Math.min(1,Math.max(0,N)),warnings:w&&!h?["p-value will be approximate in the presence of ties"]:[]})}var Em=["poolSd","paired","pAdjustMethod","alternative"],xm=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],Rm=["pAdjustMethod","correct"];function Zi(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=kn(t);for(let i=0;i<r.length;i++){let a=s[i];if(typeof a=="number"&&Number.isNaN(a)||n&&Number.isNaN(r[i]))continue;let l=o.get(a);l===void 0&&o.set(a,l=[]),l.push(r[i])}let c=Array.from(o.keys()).sort(ar);return{levels:c,groups:c.map(i=>o.get(i))}}function cs(e,t,n){let r=[],o=[];for(let i=0;i<e-1;i++)for(let a=i+1;a<e;a++)r.push(t(a,i)),o.push([a-1,i]);let s=Tn(r,n),c=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([i,a],l)=>c[i][a]=s[l]),c}function ls(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(Pe),colNames:t.slice(0,-1).map(Pe),pAdjustMethod:r,warnings:o}}function Tm(e,t,n={}){V(n,Em,"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=fe(n.alternative),c=n.pAdjustMethod??"holm",{levels:i,groups:a}=Zi(e,t,!r),l=i.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(N=>N.length),p=Ie(a),g=p.mean,w=0;for(let N of u)N>1&&(w+=N-1);cr(w,p.ssWithin);let h=Math.sqrt(p.ssWithin/w);f=(N,M)=>{let v=(g[N]-g[M])/(h*Math.sqrt(1/u[N]+1/u[M]));return s==="less"?Ke(v,w,!0,!1):s==="greater"?Ke(v,w,!1,!1):2*Ke(-Math.abs(v),w,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,p)=>Re(()=>Rn(a[u],a[p],{paired:r,alternative:s})).pValue;return ls(m,i,cs(l,f,c),c)}function km(e,t,n={}){V(n,xm,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,c=r??"holm",{levels:i,groups:a}=Zi(e,t,!(s.paired??!1)),l=i.length;if(l<2)throw new RangeError("there must be at least two groups");let f="",m=new Te,u=cs(l,(p,g)=>{let w=Re(()=>or(a[p],a[g],{...s,...o===void 0?{}:{alternative:o}}));f=w.method;for(let h of w.warnings)m.add(h);return w.pValue},c);return j("pairwiseWilcoxTest",ls(f,i,u,c,m.list))}function Sm(e,t,n={}){if(V(n,Rm,"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 c=n.pAdjustMethod??"holm",i=n.correct??!0,a=new Te,l=cs(s,(m,u)=>{let p=Re(()=>Uo([r[m],r[u]],[o[m],o[u]],{correct:i}));for(let g of p.warnings)a.add(g);return p.pValue},c),f=Array.from({length:s},(m,u)=>u+1);return j("pairwisePropTest",ls("Pairwise comparison of proportions",f,l,c,a.list))}var lr=[0,0];function ro(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=de(e,Ht(t,t));return Tt(t,n[0]/(2*t))}function oo(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:ro([e[r],0]))}function Sn(e,t){return _t(e,t,[]).fit}function _t(e,t,n){let r=t.length,o=Math.min(e.length,r),s=e.map(f=>f.slice()),c=t.slice(),i=n.map(f=>f.slice()),a=[];for(let f=0;f<o;f++){let m=s[f],u=lr;for(let N=f;N<r;N++)u=Z(u,pe(m[N],m[N]));if(u[0]===0){a.push(lr);continue}let p=ro(u),g=m[f][0]<0?[-p[0],-p[1]]:p;m[f]=Z(m[f],g);let w=pe(g,m[f]),h=N=>{let M=lr;for(let b=f;b<r;b++)M=Z(M,pe(m[b],N[b]));let v=Se(M,w);for(let b=f;b<r;b++)N[b]=de(N[b],pe(v,m[b]))};for(let N=f+1;N<e.length;N++)h(s[N]);h(c);for(let N of i)h(N);a.push([-g[0],-g[1]])}let l=new Array(o).fill(lr);for(let f=o-1;f>=0;f--){let m=c[f];for(let u=f+1;u<o;u++)m=de(m,pe(s[u][f],l[u]));l[f]=Se(m,a[f])}return{fit:{qty:c,coef:l},qtv:i}}function An(e,t=0,n=e.length){let r=lr;for(let o=t;o<n;o++)r=Z(r,pe(e[o],e[o]));return r}var $e=new WeakMap;function so(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return ee(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function ao(e,t){return e.map(n=>{if(n.rss>=Am&&n.rss<1/0)return q(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let c=n.weights?.[s]??1;if(c===0)return;let i=q(o,-t);r+=c*i*i}),r})}var Am=2**-1022;function us(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,q(n,-t)]])}function io(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function on(e,t,n=0){let r=c=>e.reduce((i,[a,l])=>i+q(a,-c)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=io(e),s=r(o)),!(q(t,-o)>=1e-24*s)}function qm(e){let t=so([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let c=e.assign[n[s]],i=o.get(c);i===void 0&&(i={term:c,df:0,ss:0},o.set(c,i),r.push(i)),i.df++,i.ss+=q(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:ao([e],t)[0],k:t}}var Ji=(e,t,n)=>we(e,t,n,!1,!1);function Im(e){let t=qm(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let c=e.weights?.[s]??1;c!==0&&(r+=c*q(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 fs(e){let t=Im(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:c,rss:i}=t;if(r>0){let w=us(e,o);on(w,i)&&(i=0,c=c.map(h=>on(w,h.ss)?{...h,ss:0}:h))}let a=i/r,l=w=>q(w,2*o),f=c.map(({df:w,ss:h})=>{let N=h/w/a;return[w,l(h),l(h/w),N,Ji(N,w,r)]});f.push([r,l(i),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],p={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},g=c.map(({df:w,ss:h})=>[w,h]);return g.push([r,i]),$e.set(p,{colNames:["Df","Sum Sq"],rowNames:u,table:g}),p}function _m(e){let t=e.map(f=>f.dfResidual),n=so(e),r=ao(e,n),o=f=>q(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let c=r[s]/t[s],i=0;e[s].fittedValues.forEach((f,m)=>{let u=e[s].weights?.[m]??1;u!==0&&(i+=u*q(f,-n)**2)});let a=r[s]<1e-10*i,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],p=r[m-1]-r[m],[g,w]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],h=M=>M.filter((v,b)=>(g.weights?.[b]??1)!==0),N=u===0||!Wr(h(g.x),h(w.x))?NaN:p/u/c;return N<0&&(N=NaN),[t[m],o(r[m]),u,o(p),N,Number.isNaN(N)?NaN:Ji(N,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 ms(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function ps(e,...t){let n=[e,...t];for(let i of n)if(typeof i!="object"||i===null||!Array.isArray(i.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1){let i=fs(e),a={...i,warnings:ms(e.warnings,i.warnings)},l=$e.get(i);return l!==void 0&&$e.set(a,l),j("anova",a,e)}if(n.some(i=>i.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=(i,a)=>i===a||i!==void 0&&a!==void 0&&i.length===a.length&&i.every((l,f)=>Object.is(l,a[f]));if(n.some(i=>!r(i.rows,e.rows)))throw new Error("models were not all fitted to the same rows of the data");let o=n.filter(i=>i.response===e.response);if(o.some(i=>!r(i.y,e.y)))throw new Error("models were not all fitted to the same response values");if(o.some(i=>!r(i.weights,e.weights)))throw new Error("models were not all fitted with the same weights");let s=[];if(o.length<n.length){let i=[...new Set(n.filter(a=>a.response!==e.response).map(a=>a.response))];s.push(`models with response ${i.map(a=>`\u2018${a}\u2019`).join(", ")} removed because response differs from model 1`)}let c=o.length===1?fs(e):_m(o);return c.warnings=ms(...n.map(i=>i.warnings),s,c.warnings),j("anova",c,...n)}function hs(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let r=e,o={strata:r.strata.filter(c=>c.stratum!=="Error: (Intercept)").map(c=>({...c})),warnings:[...r.warnings]},s=$e.get(r);return Array.isArray(s)&&$e.set(o,s.filter((c,i)=>r.strata[i].stratum!=="Error: (Intercept)")),j("aovSummary",o,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=fs(e),n=ms(e.warnings,t.warnings);if(e.dfResidual>0){let r={...t,warnings:n},o=$e.get(t);return o!==void 0&&$e.set(r,o),j("aovSummary",r,e)}return j("aovSummary",{colNames:t.colNames.slice(0,3),rowNames:t.rowNames.slice(0,-1),table:t.table.slice(0,-1).map(r=>r.slice(0,3)),warnings:n},e)}var Lm=1e-14;function Om(e,t,n,r,o,s,c,i){let a=b=>q(b,2*c),l=t.length,f=n.flatMap((b,d)=>An(b)[0]>Lm*s[d]?[d]:[]),m=0,u=[],p=t.slice();if(f.length>0){let b=Ne(Array.from({length:l},(d,E)=>f.map(x=>n[x][E][0])));m=b.rank,u=b.pivot.slice(0,m).map(d=>f[d]),p=Sn(u.map(d=>n[d]),t).qty}let g=[];u.forEach((b,d)=>{let E=r[b],x=g.find(y=>y.term===E);x===void 0&&g.push(x={term:E,df:0,ss:[0,0]}),x.df++,x.ss=Z(x.ss,pe(p[d],p[d]))});let w=l-m,h=An(p,m)[0],N=g.filter(b=>b.term!==0);w>0&&on(i,h)&&(h=0,N=N.map(b=>on(i,b.ss[0])?{...b,ss:[0,0]}:b));let M=N.map(b=>o[b.term-1]),v=b=>{if(w>0){let d=h/w,E=N.map(({df:x,ss:y})=>{let R=y[0]/x/d;return[x,b(y[0]),b(y[0]/x),R,we(R,x,w,!1,!1)]});return E.push([w,b(h),b(d),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:N.map(({df:d,ss:E})=>[d,b(E[0]),b(E[0]/d)])}};return[v(a),v(b=>b)]}function ec(e,t,n,r,o,s){if(s.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let c=n??{type:"num",value:1,text:"1"},i=Ia({response:t.response,rhs:c},Object.keys(o)).variables,a=Jt(t,o,{...s,dropUnusedLevels:!0,contrastVariables:i},c),l=a.y;if(l===void 0)throw new TypeError("the formula has no response");if(l.some(_=>!Number.isFinite(_)))throw new RangeError("NA/NaN/Inf in 'y'");let f=Jt({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(_=>!a.warnings.includes(_))],p=f.colNames.length,g=f.colNames.map((_,$)=>ee([f.x.map(P=>P[$])]).groups[0]),w=f.x.map((_,$)=>g.map(P=>P[$])),h=p>0?Ne(w):void 0,N=h?.rank??0;N<p&&u.push("Error() model is singular");let M=h?.pivot.slice(0,N)??[],v=_=>_.map($=>[$,0]),b=a.colNames.map((_,$)=>v(ee([a.x.map(P=>P[$])]).groups[0])),{groups:d,k:E}=ee([l]),{fit:x,qtv:y}=_t(M.map(_=>v(g[_])),v(d[0]),b),R=d[0].map(_=>[1,_]),T=b.map(_=>An(_)[0]),k=[];M.forEach((_,$)=>{let P=f.assign[_],C=P===0?"(Intercept)":f.terms.termLabels[P-1],W=k.find(F=>F.name===C);W===void 0&&k.push(W={name:C,idx:[]}),W.idx.push($)}),m>N&&k.push({name:"Within",idx:Array.from({length:m-N},(_,$)=>N+$)});let S=k.map(_=>Om(_.name,_.idx.map($=>x.qty[$]),y.map($=>_.idx.map(P=>$[P])),a.assign,a.terms.termLabels,T,E,R)),A=S.map(_=>_[0]),I={formula:e,multistratum:!0,strata:A,rows:a.frame.rows,warnings:u};return $e.set(I,S.map(_=>_[1])),I}function co(e){let t=e.length,n=new Array(t).fill(0);for(let o=0;o<t;o++){let s=0;for(let c=0;c<=o;c++)s=Math.max(s,Math.abs(e[c][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,c)=>q(s,-n[c])));return{inv:za(r),e:n}}var Fm=["weights","contrasts","levels","intercept"],Pm=["level"],Dm=["interval","level"],ds=2**-1022;function gs(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=oc(t.x,r,t.frame.weights,t.terms.intercept,!0),s=o.result,c=[...t.warnings];if(!n){let a=o.yScale??0,l=o.fitRows.map(p=>q(s.fittedValues[p],-a)),f=l.reduce((p,g)=>p+g,0)/l.length,m=l.length>1?l.reduce((p,g)=>p+(g-f)**2,0)/(l.length-1):NaN,u=(o.rssScaled??o.rss)/s.dfResidual;Number.isFinite(u)&&u<1e-30*(f*f+m)&&c.push("essentially perfect fit: summary may be unreliable")}let i={...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:c};return t.frame.weights!==void 0&&(i.weights=t.frame.weights),i}function ws(e,t,n={}){return V(n,Fm,"lm"),typeof e=="string"?j("lm",gs(e,Jt(Dr(e),t,{...n,dropUnusedLevels:!0}),!1)):Vm(e,t,n)}function Vm(e,t,n){let r=n.intercept??!0,o=e.length>0&&typeof e[0]!="number"?e.map(m=>Array.from(m)):Array.from(e,m=>[m]),s=Array.from(t);if(o.some(m=>m.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 c=n.weights===void 0?void 0:Array.from(n.weights);if(c!==void 0&&c.length!==s.length)throw new RangeError("variable lengths differ (found for '(weights)')");let i=s.map((m,u)=>!Number.isNaN(m)&&o[u].every(p=>!Number.isNaN(p))&&(c===void 0||!Number.isNaN(c[u]))),a=c?.filter((m,u)=>i[u]);if(a?.some(m=>m<0))throw new RangeError("missing or negative weights not allowed");let l=o.filter((m,u)=>i[u]).map(m=>r?[1,...m]:m),f=s.filter((m,u)=>i[u]);return oc(l,f,a,r).result}function Cm(e,t,n,r,o,s,c){let i=s.rank,a=s.pivot.slice(0,i),l=n?.map(d=>q(d,-r)),f=oo(l===void 0?void 0:o.map(d=>l[d]),o.length).map((d,E)=>{if(n===void 0||l===void 0)return d;let x=n[o[E]];if(x===0||l[o[E]]>=ds)return d;let y=ro([x,0]);return[q(y[0],-r/2),q(y[1],-r/2)]}),m=a.map(d=>{let E=o.map((y,R)=>at(f[R],e[y][d])),x=Va(E.map(y=>y[0]));return{c:x===0?E:E.map(y=>[q(y[0],-x),q(y[1],-x)]),e:x}}),{qty:u,coef:p}=Sn(m.map(({c:d})=>d),o.map((d,E)=>at(f[E],t[d]))),g=p.map((d,E)=>m[E].e===0?d:[q(d[0],-m[E].e),q(d[1],-m[E].e)]),w=e[0]?.map(()=>NaN)??[];a.forEach((d,E)=>w[d]=g[E][0]);let h=e.map(d=>{let E=[0,0];return a.forEach((x,y)=>E=Z(E,at(g[y],d[x]))),E}),N=An(u,i)[0],M=o.map(d=>l?.[d]??1),v=[0,0];if(c){let d=[0,0],E=[0,0];o.forEach((x,y)=>{d=Z(d,[M[y],0]),E=Z(E,at(h[x],M[y]))}),v=Se(E,d)}let b=[0,0];return o.forEach((d,E)=>{let x=de(h[d],v);b=Z(b,at(pe(x,x),M[E]))}),{coefficients:w,fittedValues:h.map(d=>d[0]),residuals:h.map((d,E)=>de([t[E],0],d)[0]),effects:u.map(d=>d[0]),rss:N,mss:b[0]}}function oc(e,t,n,r,o=!1){let s=ee([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return tc(nc(e,t,n,r,o),e,t,n);let c=nc(e,t.map(l=>q(l,-s)),n,r,o),i=c.result,a=l=>q(l,s);return i.coefficients=i.coefficients.map(a),i.residuals=i.residuals.map(a),i.fittedValues=i.fittedValues.map(a),i.sigma=a(i.sigma),i.coefTable.estimate=i.coefTable.estimate.map(a),i.coefTable.stdError=i.coefTable.stdError.map(a),tc({...c,effects:c.effects.map(a),rss:q(c.rss,2*s),yScale:s,rssScaled:c.rss},e,t,n)}function tc(e,t,n,r){if(r===void 0)return e;let o=e.result,s=o.coefficients.flatMap((c,i)=>Number.isNaN(c)?[]:[i]);return r.forEach((c,i)=>{if(c!==0)return;let a=!1;if(!Number.isFinite(o.fittedValues[i])){let l=t[i],f=s.reduce((m,u)=>m+l[u]*o.coefficients[u],0);o.fittedValues[i]=Hn(f,0)?Xn(s.map(m=>l[m]),s.map(m=>o.coefficients[m]),f):f,a=!0}(a||!Number.isFinite(o.residuals[i]))&&(o.residuals[i]=n[i]-o.fittedValues[i])}),e}var sc=new WeakMap;function $m(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function nc(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((B,J)=>n===void 0||n[J]!==0),c=e.filter((B,J)=>s[J]),i=t.filter((B,J)=>s[J]),a=n?.filter((B,J)=>s[J]),l=i.length;if(l===0)throw new RangeError("all weights are zero");if(c.some(B=>B.some(J=>!Number.isFinite(J))))throw new RangeError("NA/NaN/Inf in 'x'");if(i.some(B=>!Number.isFinite(B)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some(B=>!Number.isFinite(B)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((B,J)=>s[J]?[J]:[]),m=a===void 0?1:ge(a),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,p=0,g=a?.map(Math.sqrt),w=g===void 0?c:c.map((B,J)=>B.map(D=>D*g[J])),h=Ne(w);a!==void 0&&Number.isFinite(m)&&u+2>0&&!$m(h)&&(p=u+2,g=a.map(B=>q(Math.sqrt(B),-p/2)),w=c.map((B,J)=>B.map(D=>D*g[J])),h=Ne(w),sc.set(h,p));let N=g===void 0?i:i.map((B,J)=>B*g[J]),M=h.rank,v=B=>q(B,-u),b=o?Cm(e,t,n,u,f,h,r):void 0,d=b?.coefficients??yn(h,N),E=b?.residuals??[],x=b?.fittedValues??[],y=l-M,R=a?.map(v)??new Array(l).fill(1),T=b?.rss??0;if(b===void 0){let B=En(h,N),J=g===void 0?B:B.map((G,H)=>G/g[H]),D=0;t.forEach((G,H)=>{if(s[H])E.push(J[D]),x.push(G-J[D]),D++;else{let me=e[H].reduce((Le,He,Xe)=>Number.isNaN(d[Xe])?Le:Le+He*d[Xe],0);x.push(me),E.push(G-me)}});for(let G=0;G<l;G++)T+=R[G]*J[G]**2}let k=x.filter((B,J)=>s[J]),S=0;if(a!==void 0&&a.some(B=>B!==0&&q(B,-u)<ds)){let B=f.flatMap((J,D)=>{let G=E[J];return G===0?[]:[{w:a[D],r:G,a:Math.floor(Math.log2(Math.abs(G)))}]});if(B.length>0&&B.every(J=>Number.isFinite(J.r))){let J=Math.max(...B.map(H=>Math.log2(H.w)+2*H.a)),D=2*Math.floor(Math.floor(J)/2),G=0;for(let H of B)G+=q(H.w,2*H.a-D)*q(H.r,-H.a)**2;T=G,S=D-u}}let A=T/y,I=q(Math.sqrt(A),(u+S)/2),_=p===0?0:(p-u)/2,$=h.qr.slice(0,M).map(B=>B.slice(0,M).map(J=>p===0?q(J,-u/2):J)),{inv:P,e:C}=M>0?co($):{inv:[],e:[]},F=h.pivot.slice(0,M).map((B,J)=>[B,J]).sort((B,J)=>B[0]-J[0]),L=F.map(([B])=>d[B]),z=F.map(([,B])=>q(Math.sqrt(P[B][B]*A),S/2-C[B]-_)),X=L.map((B,J)=>B/z[J]),te=X.map(B=>2*Ke(-Math.abs(B),y,!0,!1)),ne={coefficients:d,residuals:E,fittedValues:x,rank:M,dfResidual:y,coefTable:{estimate:L,stdError:z,tValue:X,pValue:te},sigma:I,rSquared:0,adjRSquared:0,intercept:r,qr:h},ae=r?1:0,ye=M-ae,_e={result:ne,fitRows:f,effects:(b?.effects??[]).map(B=>q(B,u/2)),rss:q(T,u+S)};if(ye===0)return _e;let ze=0;if(b!==void 0)ze=b.mss;else if(r){let B=R.reduce((D,G)=>D+G,0),J=0;for(let D=0;D<l;D++)J+=R[D]*k[D]/B;for(let D=0;D<l;D++)ze+=R[D]*(k[D]-J)**2}else for(let B=0;B<l;B++)ze+=R[B]*k[B]**2;ne.rSquared=ze/(ze+q(T,S)),ne.adjRSquared=1-(1-ne.rSquared)*((l-ae)/y);let pt=q(ze/ye/A,-S);return ne.fStatistic={value:pt,numdf:ye,dendf:y},ne.fPValue=we(pt,ye,y,!1,!1),_e}function ac(e,t){let n=(1-e)/2;return n<1e-8?gt(n,t,!1,!1):gt((1+e)/2,t,!0,!1)}function zm(e,t={}){V(t,Pm,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=ac(n,e.dfResidual),{estimate:o,stdError:s}=e.coefTable,c=e.coefficients.map(()=>NaN),i=e.coefficients.map(()=>NaN),a=0;return e.coefficients.forEach((l,f)=>{Number.isNaN(l)||(c[f]=o[a]-r*s[a],i[f]=o[a]+r*s[a],a++)}),{lower:c,upper:i}}function rc(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 q(q(e,-r)*q(t,-o),r+o+n)}function Wm(e,t,n={}){V(n,Dm,"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,c=e.qr.pivot.slice(0,s),i=e.qr.qr.slice(0,s).map(v=>v.slice(0,s)),a=-(sc.get(e.qr)??0)/2,l=e.coefficients.flatMap((v,b)=>Number.isNaN(v)?[]:[b]),f=o.map(v=>{let b=v.reduce((d,E,x)=>Number.isNaN(e.coefficients[x])?d:d+E*e.coefficients[x],0);return Hn(b,0)?Xn(l.map(d=>v[d]),l.map(d=>e.coefficients[d]),b):b}),m=v=>{let b=new Array(s).fill(0);for(let d=0;d<s;d++){let E=v[d];for(let x=0;x<d;x++)E-=i[x][d]*b[x];b[d]=E/i[d][d]}return b},u=o.map(v=>{let b=c.map(x=>v[x]),d=Ao(m(b));if(d>=ds&&d<1/0||!b.every(Number.isFinite)||b.every(x=>x===0))return a===0||!(d>0&&d<1/0)?d*e.sigma:rc(d,e.sigma,a);let E=Math.floor(Math.log2(ge(b.map(Math.abs))));for(let x of[0,600]){let y=Ao(m(b.map(R=>q(R,-(E+x)))));if(y<1/0)return y===0?y*e.sigma:rc(y,e.sigma,E+x+a)}return d*e.sigma}),p=n.interval??"none";if(p!=="none"&&p!=="confidence"&&p!=="prediction")throw new RangeError(`invalid 'interval': ${String(p)}`);let g=zr(e.qr,e.rank,o),w=g.length>0?{nonEstimable:g,warnings:[$r]}:{};if(p==="none")return j("predict",{fit:f,seFit:u,...w},e);let h=n.level??.95;if(!(h>0&&h<1))throw new RangeError("'level' must be between 0 and 1");let N=ac(h,e.dfResidual),M=u.map(v=>N*(p==="prediction"?Math.hypot(v,e.sigma):v));return j("predict",{fit:f,seFit:u,lwr:f.map((v,b)=>v-M[b]),upr:f.map((v,b)=>v+M[b]),...w},e)}var jm=["weights","contrasts","levels"],ic=["confLevel"],cc=["sigmaX","sigmaY","mu","alternative","confLevel"];function Km(e,t,n={}){return V(n,jm,"aov"),typeof e=="string"?j("aov",lc(e,t,n)):j("aov",Um(e,t))}function lc(e,t,n){let r=Dr(e,!0),{treatment:o,errors:s}=qa(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?ec(e,r,o,s[0],t,n):gs(e,Jt(r,t,{...n,dropUnusedLevels:!0},o),!0)}function Um(e,t){let{labels:n,groups:r}=zt(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((c,i)=>c.map(()=>n[i]));return lc("y ~ g",{y:o,g:s},{levels:{g:n}})}function fc(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,p)=>u+p.length,0),{ssBetween:r,ssWithin:o,k:s}=Ie(e),c=t-1,i=n-t,a=r/c,l=o/i,f=a/l,m=u=>q(u,2*s);return{df:[c,i],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:i>0?we(f,c,i,!1,!1):NaN}}function Gm(e,t,n={}){Ve(t)&&V(t,ic,"TukeyHSD"),V(n,ic,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=zt(e,r),c=s.length;if(c<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let i=n.confLevel??.95;$t(i);let l=s.reduce((N,M)=>N+M.length,0)-c;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}=Ie(s),p=f/l,g=ka(i,c,l),w=[],h={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:w,pAdjusted:w,warnings:[]};for(let N=0;N<c-1;N++)for(let M=N+1;M<c;M++){let v=m[M]-m[N],b=Math.sqrt(p/2*(1/s[M].length+1/s[N].length));h.names.push(`${o[M]}-${o[N]}`),h.i.push(M+1),h.j.push(N+1),h.diff.push(q(v,u)),h.lwr.push(q(v-g*b,u)),h.upr.push(q(v+g*b,u)),h.pAdj.push(Lr(Math.abs(v)/b,c,l,{lowerTail:!1}))}return h}function Ym(e,t,n={}){Ve(t)&&V(t,cc,"zTest"),V(n,cc,"zTest");let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:s}=n,c=n.mu??0;if(o===void 0&&r===void 0)throw new RangeError("sigmaX (the known standard deviation of x) is required");if(o===void 0||r!==void 0&&s===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");for(let T of[o,s])if(T!==void 0&&!(T>0&&T<1/0))throw new RangeError("sigmaX and sigmaY must be positive finite numbers");if(Number.isNaN(c))throw new RangeError("mu must be a number");let i=fe(n.alternative),a=n.confLevel??.95;if(!(a>=0&&a<=1))throw new RangeError("confLevel must be between 0 and 1");let l=Array.from(e).filter(T=>!Number.isNaN(T)),f=r===void 0?void 0:Array.from(r).filter(T=>!Number.isNaN(T));if(l.length<3)throw new RangeError("x needs at least 3 non-missing values");if(f!==void 0&&f.length<3)throw new RangeError("y needs at least 3 non-missing values");let m=ee([l,f??[],[o,s??0]]),u=m.k,[p,g,[w,h]]=m.groups,N=Bn(p)[0],M=f===void 0?NaN:Bn(g)[0],v=f===void 0?w/Math.sqrt(l.length):Math.sqrt(w**2/l.length+h**2/f.length),b=f===void 0?N:N-M,d=(b-q(c,-u))/v,E,x;if(i==="less")E=U(d),x=[-1/0,b+Q(a)*v];else if(i==="greater")E=U(d,{lowerTail:!1}),x=[b-Q(a)*v,1/0];else{E=2*U(-Math.abs(d));let T=(1-a)/2,k=(T<1e-8?Q(T,{lowerTail:!1}):Q(1-(1-a)/2))*v;x=[b-k,b+k]}x=[q(x[0],u),q(x[1],u)];let y=f!==void 0,R=(T,k)=>{if(!(u>0&&T.some((A,I)=>A!==0&&Math.abs(k[I])<2**-1022)))return q(Bn(k)[0],u);let S=Math.max(ee([T]).k,0);return q(Bn(S===0?T:T.map(A=>q(A,-S)))[0],S)};return{method:y?"Two-sample z-Test":"One-sample z-Test",alternative:i,statistic:d,statisticName:"z",pValue:E,confInt:x,confLevel:a,estimate:y?[R(l,p),R(f,g)]:R(l,p),estimateNames:y?["mean of x","mean of y"]:"mean of x",nullValue:c,nullValueName:y?"difference in means":"mean",warnings:[]}}var Hm={gaussian:["identity","log","inverse"],binomial:["logit","probit","cauchit","log","cloglog"],quasibinomial:["logit","probit","cauchit","log","cloglog"],poisson:["log","identity","sqrt"],quasipoisson:["log","identity","sqrt"],Gamma:["inverse","identity","log"],"inverse.gaussian":["1/mu^2","inverse","identity","log"],quasi:["identity","logit","probit","cloglog","inverse","log","1/mu^2","sqrt"]},vs=e=>Number.isFinite(e)&&e>0,qn=()=>!0;function Xm(e){return e<0&&_o("NaNs produced"),Math.sqrt(e)}function mc(e){return e<0&&_o("NaNs produced"),Math.log(e)}function Ns(e){if(!Number.isFinite(e))return Number.isNaN(e)?"nan":e>0?"inf":"-inf";let t=e.toExponential(5),n=Number(t.slice(t.indexOf("e")+1));if(n>=-4&&n<6)return e.toFixed(5-n).replace(/\.?0+$/,"");let[r,o]=t.split("e");return`${r.replace(/\.?0+$/,"")}e${n<0?"-":"+"}${String(Math.abs(Number(o))).padStart(2,"0")}`}function Bm(e){if(e<0||e>1)throw new RangeError(`Value ${Ns(e)} out of range (0, 1)`);return Math.log(e/(1-e))}var Qm=["logit","probit","cauchit","cloglog","identity","log","sqrt","1/mu^2","inverse"],bs=e=>Math.min(Math.max(e,Number.MIN_VALUE),1-2**-53);function Ms(e){switch(e){case"identity":return{name:e,link:t=>t,linkinv:t=>t,muEta:()=>1,etaOk:qn,muPower:{c:1,a:0}};case"log":return{name:e,link:Math.log,linkinv:t=>Math.max(Math.exp(t),Number.MIN_VALUE),muEta:t=>Math.max(Math.exp(t),Number.MIN_VALUE),etaOk:qn,muPower:{c:1,a:1}};case"inverse":return{name:e,link:t=>1/t,linkinv:t=>1/t,muEta:t=>-1/(t*t),etaOk:t=>Number.isFinite(t)&&t!==0,muPower:{c:-1,a:2}};case"sqrt":return{name:e,link:Math.sqrt,linkinv:t=>t*t,muEta:t=>2*t,etaOk:vs,muPower:{c:2,a:.5}};case"1/mu^2":return{name:e,link:t=>1/(t*t),linkinv:t=>1/Xm(t),muEta:t=>-1/(2*t**1.5),etaOk:vs,muPower:{c:-.5,a:3}};case"logit":return{name:e,link:Bm,linkinv:t=>{if(t>30)return Math.min(1/(1+Math.exp(-t)),1-2**-53);let n=Math.exp(t);return Math.max(n/(1+n),Number.MIN_VALUE)},muEta:t=>{if(Math.abs(t)>30){let r=Math.exp(-Math.abs(t));return Math.max(r/((1+r)*(1+r)),Number.MIN_VALUE)}let n=Math.exp(t);return n/((1+n)*(1+n))},etaOk:qn};case"probit":return{name:e,link:t=>Q(t),linkinv:t=>bs(U(t)),muEta:t=>Math.max(Be(t),Number.MIN_VALUE),etaOk:qn};case"cauchit":return{name:e,link:t=>Ea(t,0,1,!0,!1),linkinv:t=>bs(ya(t,0,1,!0,!1)),muEta:t=>Math.max(Ma(t,0,1,!1),Number.MIN_VALUE),etaOk:qn};case"cloglog":return{name:e,link:t=>Math.log(-Math.log1p(-t)),linkinv:t=>bs(-Math.expm1(-Math.exp(t))),muEta:t=>{let n=Math.min(t,700);return Math.max(Math.exp(n)*Math.exp(-Math.exp(n)),Number.MIN_VALUE)},etaOk:qn};default:throw new RangeError(`unknown link "${String(e)}"`)}}function Zm(e){if(!Number.isFinite(e))throw new RangeError("invalid power for the power link");if(e<=0)return Ms("log");if(e===1)return Ms("identity");let t=1/e;return{name:`mu^${Math.round(e*1e3)/1e3}`,link:n=>n**e,linkinv:n=>Math.max(n===1?1:n**t,Number.MIN_VALUE),muEta:n=>Jm(n,e,t),etaOk:vs,...Number.isFinite(t)?{muPower:{c:t,a:1-e}}:{}}}function Jm(e,t,n){let r=n*e**(n-1);if(r<1/0)return Math.max(r,Number.MIN_VALUE);if(e===1)return n;let o=e**n,s=t*e;return o>0&&o<1/0&&s>=2**-1022&&s<1/0?Math.max(o/s,Number.MIN_VALUE):Math.max(Math.exp(n*Math.log(e)-Math.log(t)-Math.log(e)),Number.MIN_VALUE)}function eu(e){switch(e){case"constant":return{variance:()=>1,muOk:()=>!0,dev:(t,n,r)=>r*(t-n)**2,varPower:0};case"mu(1-mu)":return{variance:t=>t*(1-t),muOk:t=>t>0&&t<1,dev:gc};case"mu":return{variance:t=>t,muOk:t=>t>0,varPower:1,dev:dc};case"mu^2":return{variance:t=>t*t,muOk:t=>t>0,varPower:2,dev:(t,n,r)=>ys(t,n)?uc(t,n,r):Math.max(hc(t,n,r,t!==0?t/n:1/n),0)};case"mu^3":return{variance:t=>t*t*t,muOk:t=>t>0,dev:wc,varPower:3};default:throw new RangeError(`'variance' "${String(e)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`)}}var ys=(e,t)=>Math.abs(e-t)<.1*t,uc=(e,t,n)=>-2*n*Oo((e-t)/t);function pc(e,t){let n=t/e;return e*(n*Math.log1p(n)+Oo(n))}var vt=e=>Math.floor(Math.log2(Math.abs(e)));function fr(e,t,n){if(e===0)return 0;let r=vt(e);return q(2*q(e,-r)*t,r+n)}var lo=(e,t,n,r,o)=>!(Number.isFinite(o)&&Math.abs(r)>=2**-1022)&&e>0&&e<1/0&&t>0&&t<1/0&&n>=0&&n<1/0&&e!==t;function tu(e,t,n){let r=vt(Math.max(e,t)),o=q(e,-r),s=q(t,-r),c=e/t,i=c>=2**-1022&&c<1/0?Math.log(c):Math.log(e)-Math.log(t);return fr(n,o*i-(o-s),r)}function hc(e,t,n,r){let o=mc(r)-(e-t)/t,s=-2*n*o;if(!lo(e,t,n,o,s))return s;let c=vt(e),i=c-vt(t),a=q(e,-c)/q(t,-(c-i)),l=Math.log(a)+i*Math.LN2;return i>0?fr(n,a-q(1+l,-i),i):fr(n,q(a,i)-1-l,0)}function dc(e,t,n){if(ys(e,t)){let s=Gn(e,t),c=2*n*s;if(!lo(e,t,n,s,c))return c;let i=vt(t);return fr(n,Gn(q(e,-i),q(t,-i)),i)}let r=(e===0?0:e*mc(e/t))-(e-t),o=2*n*r;return lo(e,t,n,r,o)?tu(e,t,n):o}function nu(e,t,n){let r=vt(t);return fr(n,Gn(q(e,-r),q(t,-r))+q(pc(1-t,t-e),-r),r)}function gc(e,t,n){if(Math.abs(e-t)<.1*Math.min(t,1-t)){let s=Gn(e,t)+pc(1-t,t-e),c=2*n*s;return lo(e,t,n,s,c)?nu(e,t,n):c}let r=e===0?0:e*Math.log(e/t),o=e===1?0:e===0&&t<.1?-Math.log1p(-t):(1-e)*Math.log((1-e)/(1-t));return 2*n*(r+o)}function wc(e,t,n){let r=e*t*t,o=n*(e-t)**2/r;if(Number.isFinite(o)&&!(Math.abs(r)<2**-1022))return o;let s=(e-t)/t,c=n*(s*s/e);if(Number.isFinite(c)||!(n>0&&n<1/0&&e>0&&e<1/0&&t>0&&t<1/0))return c;let i=e-t,[a,l,f,m]=[vt(n),vt(i),vt(e),vt(t)],u=q(i,-l),p=q(t,-m);return q(q(n,-a)*(u*u/(q(e,-f)*p*p)),a+2*l-f-2*m)}function In(e,t){let n=typeof e=="string"?{family:e}:e,r=n.family,o=Hm[r];if(o===void 0)throw new RangeError(`unknown family "${String(r)}"`);let s=t??n.link??o[0],c;if(typeof s=="object"){if(r!=="quasi")throw new RangeError(`link "power" not available for ${r} family`);c=Zm(s.power)}else{if(r==="quasi"&&!o.includes(s))throw new RangeError(`link "${s}" not available for ${r} family; available links are ${o.map(a=>`'${a}'`).join(", ")}`);if(!Qm.includes(s))throw new RangeError(`\u2018${s}\u2019 link not recognised`);c=Ms(s)}if(n.variance!==void 0&&r!=="quasi")throw new TypeError("'variance' is only used by the quasi family");let i={...c,family:r,varianceName:void 0,fixedDispersion:!1};switch(r){case"gaussian":return{...i,varPower:0,variance:()=>1,muOk:()=>!0,devContribution:(a,l,f)=>f*(a-l)**2};case"binomial":case"quasibinomial":return{...i,fixedDispersion:r==="binomial",variance:a=>a*(1-a),muOk:a=>Number.isFinite(a)&&a>0&&a<1,devContribution:gc};case"poisson":case"quasipoisson":return{...i,fixedDispersion:r==="poisson",varPower:1,variance:a=>a,muOk:a=>Number.isFinite(a)&&a>0,devContribution:(a,l,f)=>a>0?dc(a,l,f):2*f*l};case"Gamma":return{...i,varPower:2,variance:a=>a*a,muOk:a=>Number.isFinite(a)&&a>0,devContribution:(a,l,f)=>ys(a,l)?uc(a,l,f):hc(a,l,f,a===0?1:a/l)};case"inverse.gaussian":return{...i,varPower:3,variance:a=>a*a*a,muOk:a=>Number.isFinite(a)&&a>0,devContribution:wc};case"quasi":{let a=n.variance??"constant",l=eu(a);return{...i,varianceName:a,variance:l.variance,muOk:l.muOk,devContribution:l.dev,...l.varPower===void 0?{}:{varPower:l.varPower}}}}}function Es(e,t,n,r,o){let s=e.family,c=s==="binomial"||s==="quasibinomial";if(t.length>0&&Array.isArray(t[0])){let f=t;if(f.every(h=>h.length===1))return Es(e,f.map(h=>h[0]),n,r,o);if(!c)throw new TypeError("y must be a vector (or, for the binomial families, a two-column matrix)");if(f.some(h=>h.length!==2))throw new TypeError(`for the '${s}' family, y must be a vector of 0 and 1's
|
|
7
|
-
or a 2 column matrix where col 1 is no. successes and col 2 is no. failures`);if(s==="binomial"&&f.some(h=>Math.abs(h[0]-Math.round(h[0]))>.001||Math.abs(h[1]-Math.round(h[1]))>.001)&&o.add("non-integer counts in a binomial glm!"),f.some(h=>h[0]<0||h[1]<0))throw new RangeError("counts must be nonnegative");let m=f.map(h=>h[0]+h[1]),u=f.findIndex((h,N)=>Number.isFinite(h[0])&&Number.isFinite(h[1])&&!Number.isFinite(m[N]));if(u>=0)throw new RangeError(`the total of successes and failures overflows in row ${u+1} (${Ns(f[u][0])} + ${Ns(f[u][1])}): the binomial size must be a finite double`);let p=f.map((h,N)=>m[N]===0?0:h[0]/m[N]),g=n.map((h,N)=>h*m[N]),w=p.map((h,N)=>(m[N]*h+.5)/(m[N]+1));return{y:p,weights:g,mustart:w,size:m}}let a=t.slice(),l=n.slice();switch(s){case"binomial":case"quasibinomial":{for(let m=0;m<a.length;m++)l[m]===0&&(a[m]=0);if(a.some(m=>m<0||m>1))throw new RangeError("y values must be 0 <= y <= 1");let f=a.map((m,u)=>(l[u]*m+.5)/(l[u]+1));return s==="binomial"&&a.some((m,u)=>Math.abs(l[u]*m-Math.round(l[u]*m))>.001)&&o.add("non-integer #successes in a binomial glm!"),{y:a,weights:l,mustart:f,size:a.map(()=>1)}}case"poisson":case"quasipoisson":if(a.some(f=>f<0))throw new RangeError(`negative values not allowed for the '${s==="poisson"?"Poisson":"quasiPoisson"}' family`);return{y:a,weights:l,mustart:a.map(f=>f+.1),size:void 0};case"Gamma":if(a.some(f=>f<=0))throw new RangeError("non-positive values not allowed for the 'Gamma' family");return{y:a,weights:l,mustart:a.slice(),size:void 0};case"inverse.gaussian":if(a.some(f=>f<=0))throw new RangeError("positive values only are allowed for the 'inverse.gaussian' family");return{y:a,weights:l,mustart:a.slice(),size:void 0};case"gaussian":if(!r&&(e.name==="inverse"&&a.some(f=>f===0)||e.name==="log"&&a.some(f=>f<=0)))throw new Error("cannot find valid starting values: please specify some");return{y:a,weights:l,mustart:a.slice(),size:void 0};case"quasi":{let f=e.varianceName,m=f==="constant"?a.slice():f==="mu(1-mu)"?a.map(u=>Math.min(Math.max(u,.001),.999)):a.map(u=>u+(u===0?.1:0));return{y:a,weights:l,mustart:m,size:void 0}}}}function bc(e,t,n,r,o,s,c){let i=t.length,a=0;switch(e.family){case"gaussian":{let l=0,f=0;for(let p of r)p>0&&(l+=Math.log(p),f++);let m=2*Math.PI*s/f,u=m===1/0&&s<1/0?Math.log(2*Math.PI)+Math.log(s/f):Math.log(m);return f*(u+1)+2-l}case"binomial":{let l=o!==void 0&&o.some(f=>f>1)?o:r;for(let f=0;f<i;f++)l[f]>0&&(a+=r[f]/l[f]*qt(Yn(l[f]*t[f]),Yn(l[f]),n[f],!0));return-2*a}case"poisson":for(let l=0;l<i;l++)r[l]>0&&(fa(t[l])&&c.add(`non-integer x = ${t[l].toFixed(6)}`),a+=r[l]*un(t[l],n[l],!0));return-2*a;case"Gamma":{let l=0;for(let m of r)l+=m;let f=s/l;for(let m=0;m<i;m++)r[m]>0&&(a+=r[m]*ia(t[m],1/f,n[m]*f,!0));return-2*a+2}case"inverse.gaussian":{let l=0,f=0;for(let g=0;g<i;g++)r[g]>0&&(l+=r[g],f+=r[g]*Math.log(t[g]));let m=2*Math.PI*s/l,u=s/l,p=m===1/0&&s<1/0?Math.log(2*Math.PI)+(u<1/0?Math.log(u):Math.log(s)-Math.log(l)):Math.log(m);return l*(p+1)+3*f+2}default:return NaN}}var ru=["family","link","weights","offset","start","epsilon","maxit","intercept"],ou=["family","link","variance"],vc=["power"],su=["dispersion"],au=["type"],iu=["dispersion"],cu=["dispersion"],lu=["newdata","newoffset","type","dispersion"],_n=2**-52;function fu(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function mr(e,t,n){return e.map((r,o)=>{let s=0;for(let c=0;c<r.length;c++)s+=r[c]*t[c];return s+n[o]})}function Wt(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 yc=2**-1022,fo=e=>e===0||Math.abs(e)>=yc&&Math.abs(e)<1/0;function Nc(e,t,n,r){let o=e.variance(r),s=n**2,c=Math.sqrt(t*s/o);if(c>0&&c<1/0&&fo(s)&&s!==0&&fo(o)&&o!==0&&fo(t*s))return c;if(e.muPower!==void 0&&e.varPower!==void 0&&r>0){let{c:i,a}=e.muPower;return Math.sqrt(t)*Math.abs(i)*r**((2*a-e.varPower)/2)}return c}function xs(e,t,n,r){let o=(t-n)/r;if(Math.abs(r)>=yc&&Math.abs(r)<1/0)return o;if(e.muPower!==void 0&&n>0){let{c:s,a:c}=e.muPower,i=n**(c/2);return(t-n)/s/i/i}return o}function mu(e,t,n,r){let o=e.variance(n),s=(t-n)*Math.sqrt(r)/Math.sqrt(o);if(Number.isFinite(s)&&fo(o)&&o!==0||e.varPower===void 0||!(n>0))return s;let c=n**(e.varPower/4);return(t-n)/c*Math.sqrt(r)/c}var uu=.1;function Ec(e,t){let{x:n,p:r,y:o,w:s,offset:c,fam:i,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(p=>!Number.isFinite(p))))throw new RangeError("NA/NaN/Inf in 'x'");return Zn(t,()=>{let m=(A,I)=>A.every(_=>i.etaOk(_))&&I.every(_=>i.muOk(_));if(r===0){let A=c.slice();if(!A.every(P=>i.etaOk(P)))throw new Error("invalid linear predictor values in empty model");let I=A.map(P=>i.linkinv(P));if(!I.every(P=>i.muOk(P)))throw new Error("invalid fitted means in empty model");let _=A.map(P=>i.muEta(P)),$=I.map((P,C)=>Nc(i,s[C],_[C],P)**2);return{coefficients:[],eta:A,mu:I,deviance:Wt(i,o,I,s),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:s.map(P=>P>0),workingWeights:$,workingResiduals:o.map((P,C)=>xs(i,P,I[C],_[C]))}}let u,p;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 ${fu(e.columnNames)}`);p=e.start.slice(),u=mr(n,p,c)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(A=>i.link(A));let g=u.map(A=>i.linkinv(A));if(!m(u,g))throw new Error("cannot find valid starting values: please specify some");let w=Wt(i,o,g,s),h=w,N=!1,M=!1,v=0,b=new Array(r).fill(0),d,E=s.map(()=>!1),x=[],y=[],R=Math.min(1e-7,a/1e3);for(v=1;v<=l;v++){let A=u.map(F=>i.muEta(F));for(let F=0;F<f;F++){if(!(s[F]>0))continue;let L=i.variance(g[F]);if(Number.isNaN(L))throw new Error("NAs in V(mu)");if(L===0&&!(i.varPower!==void 0&&g[F]>0))throw new Error("0s in V(mu)");if(Number.isNaN(A[F]))throw new Error("NAs in d(mu)/d(eta)")}E=s.map((F,L)=>F>0&&A[L]!==0);let I=[];for(let F=0;F<f;F++)E[F]&&I.push(F);if(I.length===0){t.add(`no observations informative at iteration ${v}`),N=!1;break}x=new Array(f).fill(0);let _=[],$=[];for(let F of I){let L=Nc(i,s[F],A[F],g[F]);x[F]=L,_.push((u[F]-c[F]+xs(i,o[F],g[F],A[F]))*L),$.push(n[F].map(z=>z*L))}d=Ne($,{tol:R});let P=yn(d,_);if(P.some((F,L)=>d.pivot.indexOf(L)<d.rank&&!Number.isFinite(F))){t.add(`non-finite coefficients at iteration ${v}`),N=!1;break}b=P.map(F=>Number.isNaN(F)?0:F),M=!1;let C=u;if(u=mr(n,b,c),g=u.map(F=>i.linkinv(F)),h=Wt(i,o,g,s),!Number.isFinite(h)){if(p===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 F=0;for(;!Number.isFinite(h);){if(F>=l)throw new Error("inner loop 1; cannot correct step size");F++,b=b.map((L,z)=>(L+p[z])/2),u=mr(n,b,c),g=u.map(L=>i.linkinv(L)),h=Wt(i,o,g,s)}M=!0}if(!m(u,g)){if(p===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 F=0;for(;!m(u,g);){if(F>=l)throw new Error("inner loop 2; cannot correct step size");F++,b=b.map((L,z)=>(L+p[z])/2),u=mr(n,b,c),g=u.map(L=>i.linkinv(L))}M=!0,h=Wt(i,o,g,s)}let W=Math.max(a,1e-10);if(p!==void 0&&v>1&&(h-w)/(.1+Math.abs(h))>=W){t.add("step size truncated due to increasing deviance");let F=0;for(;(h-w)/(.1+Math.abs(h))>=W&&F<l;)F++,b=b.map((L,z)=>(L+p[z])/2),u=mr(n,b,c),g=u.map(L=>i.linkinv(L)),h=Wt(i,o,g,s);if((h-w)/(.1+Math.abs(h))>=W){t.add("inner loop 3; cannot correct step size"),N=!1;break}}if(e.untilCoefficients){let F=ge(b.map(Math.abs));if(p!==void 0&&b.every((L,z)=>Math.abs(L-p[z])<=8*_n*F)){N=!0;break}if(y.some(L=>L.every((z,X)=>z===b[X]))){N=!0;break}y.push(b.slice()),y.length>16&&y.shift()}else if(Math.abs(h-w)/(Math.abs(h)+.1)<a){N=!0;let F=0;for(let L=0;L<f;L++)E[L]&&(F=Math.max(F,Math.abs(u[L]-C[L])));F>uu&&t.add("glm.fit: the linear predictor was still changing at convergence: the data may be separated (no finite maximum likelihood estimates)");break}w=h,p=b.slice()}v>l&&(v=l);let T=r,k=b.slice();if(d!==void 0){T=d.rank;for(let A=d.rank;A<r;A++)k[d.pivot[A]]=NaN}let S=u.map(A=>i.muEta(A));return{coefficients:k,eta:u,mu:g,deviance:h,iter:v,converged:N,boundary:M,rank:T,qr:d,used:E,workingWeights:x.map(A=>A*A),workingResiduals:o.map((A,I)=>xs(i,A,g[I],S[I]))}})}function xc(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function Rc(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*_n||o<10*_n)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*_n)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function Ln(e,t,n,r={}){let o=In(e.familySpec),s="start"in r?r.start:e.start,c=Es(o,e.yGiven,e.priorGiven,s!==void 0||r.etastart!==void 0,n);if(!c.weights.some(f=>f>0))throw new RangeError("all weights are zero");let i=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=Ec({x:a,p:t.length,y:c.y,w:c.weights,offset:i,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:c.mustart,untilCoefficients:r.untilCoefficients},n);return Rc(l,o,n,t.length===0),{fit:l,fam:o,y:c.y,w:c.weights,size:c.size,offset:i}}function pu(e,t,n={}){V(n,ru,"glm"),V(Dt(n,"family"),ou,"glm","family"),V(Dt(n,"family.link"),vc,"glm","family.link"),V(Dt(n,"link"),vc,"glm","link");let r=n.intercept??!0,o=xc(e);if(o.some(y=>y.length!==o[0].length))throw new TypeError("all rows of x must have the same length");let s=t.length>0&&typeof t[0]!="number",c=s?t.map(y=>Array.from(y)):Array.from(t),i=c.length;if(o.length!==i)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!==i)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!==i)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((y,R)=>{let T=c[R];return!(Array.isArray(T)?T.some(S=>Number.isNaN(S)):Number.isNaN(T))&&y.every(S=>!Number.isNaN(S))&&!(a!==void 0&&Number.isNaN(a[R]))&&!(l!==void 0&&Number.isNaN(l[R]))}),m=o.filter((y,R)=>f[R]).map(y=>r?[1,...y]:y),u=c.filter((y,R)=>f[R]),p=u.length;if(p===0)throw new RangeError("0 (non-NA) cases");let g=a===void 0?new Array(p).fill(1):a.filter((y,R)=>f[R]);if(g.some(y=>y<0))throw new RangeError("negative weights not allowed");if(g.some(y=>!Number.isFinite(y)))throw new RangeError("non-finite weights not allowed");let w=l===void 0?new Array(p).fill(0):l.filter((y,R)=>f[R]);if(w.some(y=>!Number.isFinite(y)))throw new RangeError("the offset must be finite");let h=o.length>0?o[0].length:0,N=[...r?["(Intercept)"]:[],...Array.from({length:h},(y,R)=>`x${R+1}`)],M=N.length,v=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(v.link=n.link);let b={x:m,columnNames:N,yGiven:u,priorGiven:g,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)};if(!(b.epsilon>0&&b.epsilon<1/0))throw new RangeError("value of 'epsilon' must be > 0 and finite");if(!(b.maxit>0&&b.maxit<1/0))throw new RangeError("maximum number of iterations must be > 0");let d=!s&&hu(v)?ee([u]).k:0;if(d===0)return j("glm",Mc(b));let E=y=>q(y,-d),x=Mc({...b,yGiven:u.map(E),offset:w.map(E),start:b.start?.map(E)});return j("glm",du(x,d,{...b,yScale:d}))}function Mc(e){let{x:t,offset:n,intercept:r}=e,o=t.length,s=e.columnNames.length,c=new Te,i=Array.from({length:s},(h,N)=>N),{fit:a,fam:l,y:f,w:m,size:u}=Ln(e,i,c),p;if(r&&e.hasOffset){let h=new Te,N=Ec({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},h);Rc(N,l,h,!1),h.list.forEach(M=>c.add(M)),p=N.deviance,N.converged||c.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let h=Math.floor(Math.log2(ge(m))),N=0,M=0;for(let b=0;b<o;b++){if(!(m[b]>0))continue;let d=q(m[b],-h);N+=d,M+=d*f[b]}let v=M/N;if(!Number.isFinite(v)){let b=0;for(let x=0;x<o;x++)m[x]>0&&(b=Math.max(b,Math.abs(f[x])));let d=Math.floor(Math.log2(b)),E=0;for(let x=0;x<o;x++)m[x]>0&&(E+=q(m[x],-h)*q(f[x],-d));v=q(E/N,d)}p=Zn(c,()=>Wt(l,f,f.map(()=>v),m))}else p=Zn(c,()=>Wt(l,f,n.map(h=>l.linkinv(h)),m));let g=o-m.filter(h=>h===0).length,w=bc(l,f,a.mu,m,u,a.deviance,c)+2*a.rank;return Tc(a.used,{coefficients:a.coefficients,fittedValues:a.mu,linearPredictors:a.eta,deviance:a.deviance,nullDeviance:p,aic:w,rank:a.rank,dfResidual:g-a.rank,dfNull:g-(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:c.list,qr:a.qr,model:e})}function hu(e){return(e.link??"identity")!=="identity"?!1:e.family==="gaussian"||e.family==="quasi"&&(e.variance??"constant")==="constant"}function du(e,t,n){let r=s=>q(s,t),o={...e,coefficients:e.coefficients.map(r),fittedValues:e.fittedValues.map(r),linearPredictors:e.linearPredictors.map(r),deviance:q(e.deviance,2*t),nullDeviance:q(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}),Tc(Rs.get(e),o)}var Rs=new WeakMap;function Tc(e,t){return e!==void 0&&Rs.set(t,e),t}function Nt(e){let t=e.scaled;return t===void 0?{fit:e,k:0}:{fit:t,k:e.model.yScale??0}}function ur(e){return In(e.model.familySpec)}function kc(e,t){let n=Nt(e),r=pr(n.fit,t);return ur(e).fixedDispersion?r:q(r,2*n.k)}function pr(e,t){if(ur(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?gu(e):n/e.dfResidual}function gu(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=ee([t.map(s=>e.residuals[s])]),r=ee([t.map((s,c)=>Math.sqrt(e.weights[s])*Math.abs(n.groups[0][c]))]),o=0;for(let s of r.groups[0])o+=s*s;return q(o/e.dfResidual,2*(n.k+r.k))}function wu(e){return e.qr===void 0||e.rank===0?{inv:[],e:[]}:co(e.qr.qr.slice(0,e.rank).map(t=>t.slice(0,e.rank)))}function bu(e){if(!(e>=0&&e<1/0))throw new RangeError("'dispersion' must be a non-negative finite number");return e}function On(e,t){let n=q(bu(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:q(e,-2*r),s:r-t}}function Ts(e,t={}){V(t,su,"glmSummary");let{fit:n,k:r}=Nt(e),o=new Te,s=ur(n),c=t.dispersion===void 0&&!s.fixedDispersion,i=t.dispersion===void 0?void 0:On(t.dispersion,r),a=i===void 0?pr(n,o):i.m,l=i?.s??0,{inv:f,e:m}=wu(n),u=f.map((d,E)=>d.map((x,y)=>q(x,-m[E]-m[y]))),g=(n.qr===void 0?[]:n.qr.pivot.slice(0,n.rank)).map(d=>n.coefficients[d]),w=f.map((d,E)=>q(Math.sqrt(d[E]*a),-m[E])),h=l===0?g.map((d,E)=>d/w[E]):g.map((d,E)=>De(d/Math.sqrt(f[E][E]*a),m[E]-l)),N=!c,M=h.map(d=>N?2*U(-Math.abs(d)):n.dfResidual>0?2*Ke(-Math.abs(d),n.dfResidual,!0,!1):NaN);c&&n.dfResidual===0&&(w.fill(NaN),h.fill(NaN));let v=d=>q(d,r),b=d=>q(d,2*r);return j("glmSummary",{dispersion:l===0?b(a):t.dispersion,estimate:g.map(v),stdError:l===0?w.map(v):f.map((d,E)=>De(Math.sqrt(d[E]*a),l+r-m[E])),statistic:h,pValue:M,statisticName:N?"z value":"t value",covUnscaled:u,covScaled:l===0?u.map(d=>d.map(E=>b(E*a))):f.map((d,E)=>d.map((x,y)=>De(x*a,2*(l+r)-m[E]-m[y]))),dfResidual:n.dfResidual,warnings:o.list},e)}function vu(e,...t){V(Mn(t),au,"glmResiduals");let n=Nn(["type"],t).type??"deviance",r=Nt(e);return ks(r.fit,n).map(o=>q(o,r.k))}function ks(e,t){let n=ur(e),{y:r,fittedValues:o,priorWeights:s}=e;switch(t){case"response":return r.map((c,i)=>c-o[i]);case"working":return e.residuals.slice();case"pearson":return r.map((c,i)=>mu(n,c,o[i],s[i]));case"deviance":return r.map((c,i)=>s[i]===0?0:Math.sign(c-o[i])*Math.sqrt(Math.max(n.devContribution(c,o[i],s[i]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function Ss(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let c=n.qr.length,i=Array.from({length:c},(l,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=Ca(n,i).map(l=>l.reduce((f,m)=>f+m*m,0))}let o=0,s=Rs.get(e)??e.weights.map(c=>c>0);return e.priorWeights.forEach((c,i)=>{let a=s[i]?r[o++]??0:0;c>0&&t.push(a>1-10*_n?1:a)}),t}function Nu(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 Mu(e,t={}){V(t,iu,"glmRstandard");let{fit:n,k:r}=Nt(e),o=Ss(n),{m:s,s:c}=t.dispersion===void 0?{m:pr(n),s:0}:On(t.dispersion,r),i=ks(n,"deviance").filter((l,f)=>n.priorWeights[f]>0),a=n.rank>0?100*_n*Nu(i.map(Math.abs)):0;return i.map((l,f)=>{let m=De((Math.abs(l)<a?0:l)/Math.sqrt(s*(1-o[f])),-c);return Number.isFinite(m)?m:NaN})}function yu(e,t={}){V(t,cu,"glmCooksDistance");let{fit:n,k:r}=Nt(e),o=Ss(n),{m:s,s:c}=t.dispersion===void 0?{m:pr(n),s:0}:On(t.dispersion,r);return ks(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=Eu(a,o[l],s,n.rank)),c!==0&&(m=De(m,-2*c)),Number.isFinite(m)?m:NaN})}function Eu(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)),c=q(e,-o)/(1-t);return q(c*c*(t/(q(n,-s)*r)),2*o-s)}function xu(e,t={}){V(t,lu,"glmPredict");let{fit:n,k:r}=Nt(e),o=M=>q(M,r),s=ur(n),c,i;if(t.newdata===void 0)c=n.model.x,i=n.linearPredictors.slice();else{if(c=xc(t.newdata).map(b=>n.model.intercept?[1,...b]:b),c.some(b=>b.length!==n.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let M=t.newoffset===void 0?c.map(()=>0):Array.from(t.newoffset,b=>q(b,-r));if(M.length!==c.length)throw new RangeError("newoffset must have one value per row of newdata");let v=n.coefficients.flatMap((b,d)=>Number.isNaN(b)?[]:[d]);i=c.map((b,d)=>{let E=b.reduce((y,R,T)=>Number.isNaN(n.coefficients[T])?y:y+R*n.coefficients[T],0);return(Hn(E,0)?Xn(v.map(y=>b[y]),v.map(y=>n.coefficients[y]),E):E)+M[d]})}let{m:a,s:l}=t.dispersion===void 0?{m:pr(n),s:0}:On(t.dispersion,r),f=M=>q(M,r+l),m=n.rank,u=n.qr===void 0?[]:n.qr.pivot.slice(0,m),p=n.qr?.qr,g=c.map(M=>{let v=new Array(m).fill(0),b=0;for(let d=0;d<m;d++){let E=M[u[d]];for(let x=0;x<d;x++)E-=p[x][d]*v[x];v[d]=E/p[d][d],b+=v[d]*v[d]}if(!(b>=2**-1022&&b<1/0)){let d=v.reduce((E,x)=>Math.max(E,Math.abs(x)),0);if(d>0&&Number.isFinite(d)){let E=Math.sqrt(v.reduce((y,R)=>y+(R/d)**2,0)),x=d*E*Math.sqrt(a);return Number.isFinite(x)?x:d*(E*Math.sqrt(a))}}return Math.sqrt(b)*Math.sqrt(a)}),w=t.type??"link";if(w!=="link"&&w!=="response")throw new RangeError(`invalid 'type': ${String(w)}`);let h=t.newdata===void 0?[]:zr(n.qr,m,c),N=h.length>0?{nonEstimable:h,warnings:[$r]}:{};return w==="link"?j("glmPredict",{fit:i.map(o),seFit:g.map(f),...N},e):j("glmPredict",Zn(new Te,()=>({fit:i.map(M=>o(s.linkinv(M))),seFit:g.map((M,v)=>f(M*Math.abs(s.muEta(i[v])))),...N})),e)}var Ru=["parm","level","test"],Sc={epsilon:1e-300,maxit:100,untilCoefficients:!0};function As(e,t,n,r,o=1e-11){let s=[];for(let w=0;w<t.length;w++)n[w]>0&&s.push(w);let c=0,i=0;for(let w of s)c+=n[w],i+=n[w]*t[w];if(r&&!(Number.isFinite(c)&&Number.isFinite(i))){let w=0;for(let h of s)w=Math.max(w,n[h]);if(Number.isFinite(w)){let h=w>1?Math.floor(Math.log2(w)):0;c=0,i=0;for(let N of s){let M=q(n[N],-h);c+=M,i+=M*t[N]}}}let a=r?i/c: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=Ne(s.map((w,h)=>e[w].map(N=>N*m[h])),{tol:o}),p=En(u,s.map((w,h)=>t[w]*m[h])),g=0;for(let w of p)g+=w*w;if(!Number.isFinite(l)){let w=s.map((N,M)=>m[M]*(t[N]-a)),h=0;for(let N of w)h=Math.max(h,Math.abs(N));for(let N of p)h=Math.max(h,Math.abs(N));if(Number.isFinite(h)){let N=h>1?Math.floor(Math.log2(h)):0,M=0;for(let b of w)M+=q(b,-N)**2;let v=0;for(let b of p)v+=q(b,-N)**2;return q(q(M-v,N),N)}}return l-g}function Tu(e,t={}){V(t,Ru,"glmConfint");let{fit:n,k:r}=Nt(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 c=n.coefficients.length,i=t.parm??Array.from({length:c},(I,_)=>_);for(let I of i)if(!Number.isInteger(I)||I<0||I>=c)throw new RangeError(`invalid coefficient index ${I}`);let a=n.model,l=n.coefficients.map((I,_)=>_).filter(I=>!Number.isNaN(n.coefficients[I])),f=new Te,m=Ln(a,l,f,{...Sc,start:a.start===void 0?void 0:l.map(I=>a.start[I])}),u=new Array(c).fill(NaN);if(l.forEach((I,_)=>u[I]=m.fit.coefficients[_]),!m.fit.converged)return{lower:i.map(()=>NaN),upper:i.map(()=>NaN)};let p=!1,g={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},w=Re(()=>Ts(g)),h=w.dispersion,N=new Array(c).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((I,_)=>N[l[I]]=w.stdError[_]);let v=a.x.map(I=>l.map(_=>I[_])),b=m.fit.eta,d=(I,_)=>{let $=l.filter(W=>W!==I),P=Ln(a,$,new Te,{...Sc,start:void 0,etastart:b,extraOffset:a.x.map(W=>W[I]*_)}).fit;if(!P.converged)return p=!0,NaN;let C=s==="LRT"?P.deviance-m.fit.deviance:As(v,P.workingResiduals,P.workingWeights,!1);return Math.sign(_-u[I])*Math.sqrt(Math.max(C/h,0))},E=(1-o)/2,x=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),y=2**-9,R=x<y,T=R?[-x,x]:[Q(E),E<1e-8?Q(E,{lowerTail:!1}):Q((1+o)/2)],k=i.map(()=>NaN),S=i.map(()=>NaN),A=(I,_,$)=>{let P=$===0?-1:1;p=!1;let C=NaN,W=NaN;for(let ae=0;ae<=30;ae++){let ye=u[I]+P*2**ae*N[I],_e=d(I,ye);if(Math.abs(_e)>=Math.abs(_)){C=ye,W=_e;break}}if(Number.isNaN(C)||p)return NaN;let F=ae=>ae===u[I]?-_:d(I,ae)-_,[L,z]=$===0?[C,u[I]]:[u[I],C],[X,te]=$===0?[W-_,-_]:[-_,W-_],ne;try{ne=wt(F,L,z,{tol:1e-14*N[I],maxIter:500,fLower:X,fUpper:te})}catch(ae){if(!p)throw ae;ne=NaN}return p?NaN:ne};return i.forEach((I,_)=>{if(Number.isNaN(u[I]))return;if(!R){k[_]=A(I,T[0],0),S[_]=A(I,T[1],1);return}let $=[-2*y,-y,0,y,2*y],P=[A(I,-2*y,0),A(I,-y,0),u[I],A(I,y,1),A(I,2*y,1)].map(W=>W-u[I]),C=W=>{let F=0;for(let L=0;L<5;L++){let z=1;for(let X=0;X<5;X++)X!==L&&(z*=(W-$[X])/($[L]-$[X]));F+=z*P[L]}return u[I]+F};k[_]=C(T[0]),S[_]=C(T[1])}),{lower:k.map(I=>q(I,r)),upper:S.map(I=>q(I,r))}}var ku=["test","dispersion"],Su=(e,t)=>xe(e,t/2,2,!1,!1);function Au(e,t,n){let{fit:r,w:o}=Ln(e,t,n,{start:void 0});return{dfRes:o.filter(c=>c>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(c=>t.map(i=>c[i])),intercept:e.intercept}}var qs=(e,t)=>As(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function qu(e,t={}){V(t,ku,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Nt),o=r.every(u=>u.k===r[0].k)?r[0].k:0,s=o===0?n:r.map(u=>u.fit),c=t.dispersion===void 0?void 0:On(t.dispersion,o),i=c?.s??0,a=Iu(s,Array.isArray(e)&&s.length>1,{...t,...c===void 0?{}:{dispersion:c.m}},i);if(o===0&&i===0)return j("glmAnova",a,...n);let l=new Set(["Deviance","Resid. Dev","Rao","Cp"]),f=a.colNames.map(u=>l.has(u)),m=a.table.map(u=>u.map((p,g)=>f[g]?q(p,2*o):p));if(i!==0&&a.colNames.includes("Cp")){let u=a.colNames.indexOf("Cp"),p=a.colNames.indexOf("Resid. Dev"),g=a.colNames.indexOf("Resid. Df"),w=s[0].model.x.length;for(let h of m)h[u]=h[p]+2*(w-h[g])*t.dispersion}return j("glmAnova",{...a,table:m},...n)}function Iu(e,t,n,r=0){let o=new Te,s=e[0],c=In(s.model.familySpec);if(e.some(y=>{let R=In(y.model.familySpec);return R.family!==c.family||R.name!==c.name||R.varianceName!==c.varianceName}))throw new RangeError("models were not all fitted with the same family and link");let i=c.fixedDispersion,a=n.test===void 0?n.dispersion!==void 0||i?"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=[],p=new Set,g;if(t){let y=e[0].model.x.length;if(e.some(T=>T.model.x.length!==y))throw new RangeError("models were not all fitted to the same size of dataset");let R=(T,k)=>T.length===k.length&&T.every((S,A)=>JSON.stringify(S)===JSON.stringify(k[A]));if(e.some(T=>!R(T.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(T=>!R(T.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((T,k)=>l.push({name:String(k+1),dfRes:T.dfResidual,dev:T.deviance})),f=l.map((T,k)=>k===0?NaN:l[k-1].dfRes-T.dfRes),m=l.map((T,k)=>k===0?NaN:l[k-1].dev-T.dev),e.forEach((T,k)=>{if(k===0||f[k]===0)return;let[S,A]=f[k]>0?[e[k-1],T]:[T,e[k-1]],I=P=>P.filter((C,W)=>S.priorWeights[W]>0&&A.priorWeights[W]>0),_=S.model.offset.map((P,C)=>P-A.model.offset[C]),$=_.some((P,C)=>P!==0&&S.priorWeights[C]>0)?S.model.x.map((P,C)=>[...P,_[C]]):S.model.x;Wr(I($),I(A.model.x))||p.add(k)}),a==="Rao"){let T=e.map(k=>({dfRes:k.dfResidual,dev:k.deviance,fit:Ac(k),x:k.model.x,intercept:k.model.intercept}));u=l.map((k,S)=>S===0?NaN:f[S]<0?-qs(T[S],T[S-1]):qs(T[S-1],T[S]))}g=e.reduce((T,k)=>k.dfResidual<T.dfResidual?k:T)}else{let y=s,R=y.model,T=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,k=R.intercept?[0]:[],S=I=>[...k,...Array.from({length:I},(_,$)=>$+k.length)],A=[];for(let I=0;I<=T;I++)I===T?A.push({dfRes:y.dfResidual,dev:y.deviance,fit:Ac(y),x:R.x,intercept:R.intercept}):I>0||a==="Rao"?A.push(Au(R,S(I),o)):A.push(void 0);l.push({name:"NULL",dfRes:y.dfNull,dev:y.nullDeviance});for(let I=1;I<=T;I++)l.push({name:`x${I}`,dfRes:A[I].dfRes,dev:A[I].dev});f=l.map((I,_)=>_===0?NaN:l[_-1].dfRes-I.dfRes),m=l.map((I,_)=>_===0?NaN:Math.max(l[_-1].dev-I.dev,0)),a==="Rao"&&(u=l.map((I,_)=>_===0?NaN:qs(A[_-1],A[_]))),g=y}e.forEach((y,R)=>{y.converged||o.add(`model ${R+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let w=new Te,h=n.dispersion??kc(g,w);w.list.forEach(y=>o.add(y));let N=i?1/0:g.dfResidual,M=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=l.map((y,R)=>t?[y.dfRes,y.dev,f[R],m[R]]:[f[R],m[R],y.dfRes,y.dev]),b=[...M],d=y=>l.map((R,T)=>{if(T===0||f[T]===0||p.has(T))return NaN;let k=De(y[T]/h*Math.sign(f[T]),-2*r);return k<0||Number.isNaN(k)?NaN:Su(k,Math.abs(f[T]))}),E=[];if(a==="Chisq"||a==="LRT")b.push("Pr(>Chi)"),E=[d(m)];else if(a==="Rao")b.push("Rao","Pr(>Chi)"),E=[u.map((y,R)=>p.has(R)?NaN:y),d(u)];else if(a==="F"){if(i&&o.add(`using F test with a '${c.family}' family is inappropriate`),N!==0){let y=l.map((R,T)=>{let k=De(m[T]/f[T]/h,-2*r);return T===0||f[T]===0||p.has(T)||k<0?NaN:k});b.push("F","Pr(>F)"),E=[y,y.map((R,T)=>Number.isNaN(R)?NaN:we(R,Math.abs(f[T]),N,!1,!1))]}}else if(a==="Cp"){let y=g.model.x.length;b.push("Cp"),E=[l.map(R=>R.dev+2*(y-R.dfRes)*h)]}let x=v.map((y,R)=>[...y,...E.map(T=>T[R])]);return{colNames:b,rowNames:l.map(y=>y.name),table:x,warnings:o.list}}function Ac(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 _u=["type"];function qc(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let c=e.y.flatMap((g,w)=>e.weights===void 0||e.weights[w]!==0?[w]:[]),i=c.map(g=>e.weights===void 0?1:Math.sqrt(e.weights[g])),a=Ne(c.map((g,w)=>s.map(h=>e.x[g][h]*i[w]))),l=oo(e.weights===void 0?void 0:c.map(g=>q(e.weights[g],-o)),c.length),f=a.pivot.slice(0,a.rank),{qty:m}=Sn(f.map(g=>c.map((w,h)=>at(l[h],e.x[w][s[g]]))),c.map((g,w)=>at(l[w],q(e.y[g],-r)))),u=[0,0],p=0;return f.forEach((g,w)=>{g>=t.length&&(u=Z(u,pe(m[w],m[w])),p++)}),{ss:u[0],df:p}}function Is(e,t={}){if(V(t,_u,"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=so([e]),c=ao([e],s)[0];if(o===0)throw new Error("residual df = 0");let i=us(e,s),a=E=>{let x=i.map(([T,k])=>[q(T,-E),k]),y=x.reduce((T,[k])=>T+k,0),R=x.reduce((T,[k,S])=>T+k*S,0)/y;return Number.isFinite(y)&&Number.isFinite(R)?x.reduce((T,[k,S])=>T+k*(S-R)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=io(i),f=a(l)),on(i,c,l)||c===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 p=E=>e.assign.flatMap((x,y)=>E.has(x)?[y]:[]),g=[...new Set(e.assign)],w=[],h=[],N=E=>{w.length=0;let x=[];if(r===3){if(e.coefficients.some(y=>Number.isNaN(y)))throw new Error("there are aliased coefficients in the model");for(let y of g){let R=p(new Set([y])),T=p(new Set(g.filter(k=>k!==y)));w.push(y===0?"(Intercept)":e.termLabels[y-1]),x.push({ss:qc(e,T,R,s,E).ss,df:R.length})}}else{let y=R=>e.factors.flatMap((T,k)=>T[R-1]>0?[k]:[]);for(let R of g){if(R===0)continue;let T=y(R),k=g.filter(I=>I!==0&&I!==R&&T.every(_=>e.factors[_][I-1]>0)),S=p(new Set(g.filter(I=>I!==R&&!k.includes(I)))),A=qc(e,S,p(new Set([R])),s,E);w.push(e.termLabels[R-1]),x.push(A)}}return x};h=N(l),l===0&&e.weights!==void 0&&h.some(E=>!Number.isFinite(E.ss))&&(l=io(i),h=N(l));let M=E=>q(E,2*s+l),v=q(c,-l),b=v/o,d=h.map(({ss:E,df:x})=>{let y=E/x/b;return[M(E),x,y,we(y,x,o,!1,!1)]});return d.push([M(v),o,NaN,NaN]),j("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...w,"Residuals"],table:d,warnings:m},e)}var mo=[0,0];var _s=e=>e.map(t=>t.map(n=>n[0]));function Ic(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function sn(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(mo);return r.forEach((s,c)=>{let i=t[c];for(let a=0;a<n;a++)o[a]=Z(o[a],pe(s,i[a]))}),o})}function an(e){let t=mo;return e.forEach((n,r)=>t=Z(t,n[r])),t}function _c(e){let t=e.length,n=e.map(c=>c.slice()),r=Array.from({length:t},(c,i)=>i),o=1,s=!1;for(let c=0;c<t;c++){let i=c;for(let a=c+1;a<t;a++)Math.abs(n[a][c][0])>Math.abs(n[i][c][0])&&(i=a);if(n[i][c][0]===0){s=!0;continue}i!==c&&([n[c],n[i]]=[n[i],n[c]],[r[c],r[i]]=[r[i],r[c]],o=-o);for(let a=c+1;a<t;a++){let l=Se(n[a][c],n[c][c]);n[a][c]=l;for(let f=c+1;f<t;f++)n[a][f]=de(n[a][f],pe(l,n[c][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Lc(e){let{a:t,sign:n,singular:r}=_c(e);if(r)return mo;let o=[n,0];return t.forEach((s,c)=>o=pe(o,s[c])),o}function Oc(e){let t=e.length,{a:n,perm:r,singular:o}=_c(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(mo));for(let c=0;c<t;c++){let i=r.map(a=>[a===c?1:0,0]);for(let a=0;a<t;a++)for(let l=0;l<a;l++)i[a]=de(i[a],pe(n[a][l],i[l]));for(let a=t-1;a>=0;a--){for(let l=a+1;l<t;l++)i[a]=de(i[a],pe(n[a][l],i[l]));i[a]=Se(i[a],n[a][a])}for(let a=0;a<t;a++)s[a][c]=i[a]}return s}var Lu=["group","idata","M","X"],Fc=(e,t)=>xe(e,t/2,2,!1,!1);function Ls(e){let t=e.length,n=kt(an(e),t);return Lc(e.map(r=>r.map(o=>Se(o,n))))[0]}function Os(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),c=-n*o*Math.log(e),i=t*(t+1)/2-1,a=Fc(c,i),l=Fc(c,i+4);return a+s*(l-a)}var Ou=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function Fu(e,t,n){let r=e.length,o=N=>N.length===0?0:Ne(Array.from({length:r},(M,v)=>N.map(b=>b[v]))).rank,s=n.length===0?void 0:Ne(Array.from({length:r},(N,M)=>n.map(v=>v[M]))),c=s===void 0?[]:s.pivot.slice(0,s.rank).map(N=>n[N]),i=c.length,a=[...c,...t],l=Ne(Array.from({length:r},(N,M)=>a.map(v=>v[M])));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(N=>a[N]),m=N=>N.map(M=>[M,0]),u=new Array(r).fill([0,0]),p=_t(f.map(m),u,e.map((N,M)=>e.map(v=>v[M]))).qtv,g=Array.from({length:r},(N,M)=>p.map(v=>v[M])),w=_t(f.map(m),u,g).qtv,h=Array.from({length:l.rank-i},(N,M)=>i+M);return h.map(N=>h.map(M=>w[N][M]))}function Pu(e,t={}){V(t,Lu,"mauchlyTest");let n=e.map(d=>Array.from(d)),r=n[0]?.length??0;if(r===0||n.some(d=>d.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=d=>d==null||typeof d=="number"&&Number.isNaN(d),c=n.map((d,E)=>!d.some(s)&&(o===void 0||!s(o[E])));if(n.some((d,E)=>c[E]&&d.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let i=ee(n.filter((d,E)=>c[E])).groups,a=i.length,l=o?.filter((d,E)=>c[E]).map(Pe)??i.map(()=>""),f=[...new Set(l)].sort(it),m=i.map(d=>d.map(()=>[0,0]));for(let d of f){let E=l.flatMap((x,y)=>x===d?[y]:[]);for(let x=0;x<r;x++){let y=[0,0];for(let T of E)y=Z(y,[i[T][x],0]);let R=kt(y,E.length);for(let T of E)m[T][x]=de([i[T][x],0],R)}}let u=a-f.length;if(u<=0)throw new RangeError(`no residual degrees of freedom: ${a} complete row(s) for ${f.length} group(s)`);let p=Array.from({length:r},(d,E)=>Array.from({length:r},(x,y)=>{let R=[0,0];for(let T of m)R=Z(R,pe(T[E],T[y]));return R})),g=t.idata??{index:Array.from({length:r},(d,E)=>E+1)},w=d=>{let E=Re(()=>xn(d,g));if(E.x.length!==r)throw new RangeError("idata must have one row per column of y");return Ou(E.x,E.colNames.length)},h=t.M===void 0?Array.from({length:r},(d,E)=>Array.from({length:r},(x,y)=>y===E?1:0)):w(t.M),N=w(t.X??"~0"),M=Fu(p,h,N),v=M.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 b=Ls(M);return{statistic:b,statisticName:"W",pValue:Os(b,v,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var Du=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),Vu=["n","p1","p2","sigLevel","power","alternative","strict","tol"],Cu=["groups","n","betweenVar","withinVar","sigLevel","power"],Dc=Number.EPSILON**.25;function be(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 Y=e=>e==null;function tt(e,t,n){if(!Y(e)&&!t(e))throw new RangeError(n)}var hr=e=>e>=0&&e<=1,uo=e=>e>0&&e<1/0;function ft(e,t){if(e.filter(Y).length!==1)throw new RangeError(t)}function oe(e,t,n,r={}){return Fa(e,t,n,r).root}var cn=(e,t,n,r)=>_r(e,t,{ncp:n,lowerTail:r});function Fs(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Ps="'sig.level' must be numeric in [0, 1]";function $u(e={}){V(e,Du,"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;ft([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Fs(s,Ps),tt(r,hr,"'power' must be a probability"),tt(o,uo,"'sd' must be a positive finite number"),tt(t,w=>w>=2,"number of observations in each group must be at least 2"),tt(n,w=>!Number.isNaN(w),"'delta' must not be NaN"),tt(e.tol,uo,"'tol' must be a positive finite number");let c=be(e.type,["two.sample","one.sample","paired"]),i=be(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Dc,f=c==="two.sample"?2:1,m=i==="two.sided"?2:1;m===2&&!Y(n)&&(n=Math.abs(n));let u=(w,h,N,M)=>{let v=Math.max(1e-7,w-1)*f,b=Bt(M/m,v,{lowerTail:!1}),d=Math.sqrt(w/f)*h/N;Number.isFinite(d)||(d=Math.sqrt(w/f)*(h/N));let E=cn(b,v,d,!1);return a&&m===2?E+cn(-b,v,d,!0):E};if(Y(r))r=u(t,n,o,s);else{let w=r;if(Y(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=oe(h=>u(h,n,o,s)-w,2,1e7,{tol:l,extendInt:"upX"})}else if(Y(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let h=Math.abs(n);o=oe(N=>u(t,n,N,s)-w,h*1e-7,h*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else Y(n)?n=oe(h=>u(t,h,o,s)-w,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=oe(h=>u(t,n,o,h)-w,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let p=c==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":c==="two.sample"?"n is number in *each* group":void 0,g={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[c]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:s,power:r,alternative:i,...p===void 0?{}:{note:p},method:g,warnings:[]}}function zu(e={}){V(e,Vu,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;ft([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Fs(s,Ps),tt(o,hr,"'power' must be a probability"),tt(n,hr,"'p1' must be a probability"),tt(r,hr,"'p2' must be a probability"),tt(t,u=>u>0,"number of observations in each group must be positive"),tt(e.tol,uo,"'tol' must be a positive finite number");let c=be(e.alternative,["two.sided","one.sided"]),i=e.strict??!1,a=e.tol??Dc,l=c==="two.sided"?2:1,f=(u,p,g,w)=>{let h=Q(w/l,{lowerTail:!1});if(i&&l===2){let N=(p+g)/2,M=p*(1-p),v=g*(1-g),b=N*(1-N),d=Math.abs(p-g),E=Math.sqrt(M+v);return U((Math.sqrt(u)*d-h*Math.sqrt(2*b))/E)+U((Math.sqrt(u)*d+h*Math.sqrt(2*b))/E,{lowerTail:!1})}return U((Math.sqrt(u)*Math.abs(p-g)-h*Math.sqrt((p+g)*(1-(p+g)/2)))/Math.sqrt(p*(1-p)+g*(1-g)))},m=[];if(Y(o))o=f(t,n,r,s);else{let u=o;if(Y(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=oe(p=>f(p,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(Y(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=oe(p=>f(t,p,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(Y(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=oe(p=>f(t,n,p,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=oe(p=>f(t,n,r,p)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return j("powerPropTest",{n:t,p1:n,p2:r,sigLevel:s,power:o,alternative:c,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var Pc=1e16,po=(e,t)=>e>Pc&&e<1/0&&t>Pc;function ho(e,t,n,r){let o=Q(r,{lowerTail:!1}),s=n/e,c=Math.sqrt(2/e+2/t),i=Math.hypot(Math.sqrt(2*(1+2*s)/e),Math.sqrt(2/t)*(1+s));return U((o*c-s)/i,{lowerTail:!1})}function Wu(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,c,i,a]=[o(e),o(t),o(n),o(r)];return q(q(e,-s)*q(t,-c)*q(n,-i)/q(r,-a),s+c+i-a)}function ju(e={}){V(e,Cu,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,c=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,r,o,s,c],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!Y(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Y(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Fs(c,Ps),tt(s,hr,"'power' must be a probability"),tt(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),tt(o,uo,"'withinVar' must be a positive finite number");let i=(a,l,f,m,u)=>{let p=(a-1)*l*f/m;p<1/0||(p=(a-1)*l*(f/m)),!(p<1/0)&&Number.isFinite(a)&&Number.isFinite(l)&&(p=Wu(a-1,l,f,m));let g=a-1,w=(l-1)*a;return p===1/0?1:po(g,w)?ho(g,w,p,u):Xt(Vt(u,g,w,{lowerTail:!1}),g,w,{ncp:p,lowerTail:!1})};if(Y(s))s=i(t,n,r,o,c);else{let a=s;if(Y(t))t=oe(l=>i(l,n,r,o,c)-a,2,100);else if(Y(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=oe(l=>i(t,l,r,o,c)-a,2,1e5)}else if(Y(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=oe(l=>i(t,n,r,l,c)-a,r*1e-7,r*1e7)}else Y(r)?r=oe(l=>i(t,n,l,o,c)-a,o*1e-7,o*1e7):c=oe(l=>i(t,n,r,o,l)-a,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:c,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var Ku=["n","d","type","sigLevel","power","alternative"],Uu=["n1","n2","d","sigLevel","power","alternative"],Gu=["h","n","sigLevel","power","alternative"],Yu=["h","n1","n2","sigLevel","power","alternative"],Hu=["h","n","sigLevel","power","alternative"],Xu=["d","n","sigLevel","power","alternative"],Bu=["n","r","sigLevel","power","alternative"],Qu=["w","N","df","sigLevel","power"],Zu=["k","n","f","sigLevel","power"],Ju=["u","v","f2","sigLevel","power"],ep=["size"],tp={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 Cc(e="p",...t){V(Mn(t),ep,"cohenES");let n=Nn(["size"],t).size??"small",r=be(e,["p","t","r","anov","chisq","f2"]),o=be(n,["small","medium","large"]);return{test:r,size:o,effectSize:tp[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function np(e,t){if(!go(e)||!go(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 rp(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(go))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+=Oe(t[r]-e[r],e[r]));return Math.sqrt(n)}function op(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(go)))throw new RangeError("'P' must contain probabilities in [0, 1]");let t=e.map(s=>s.reduce((c,i)=>c+i,0)),n=e[0]?.length??0,r=Array.from({length:n},(s,c)=>e.reduce((i,a)=>i+a[c],0)),o=0;for(let s=0;s<n;s++)for(let c=0;c<e.length;c++){let i=t[c]*r[s];if(!(i===0&&e[c][s]===0))if(i>=sp)o+=Oe(e[c][s]-i,i);else{let a=Math.sqrt(t[c]),l=Math.sqrt(r[s]),f=e[c][s]/a/l-a*l;o+=f*f}}return Math.sqrt(o)}var sp=2**-1022,go=e=>e>=0&&e<=1,ap="\u2018sig.level\u2019 must be numeric in [0, 1]",ip="\u2018power\u2019 must be numeric in [0, 1]";function jt(e,t){let n=r=>!Y(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(ap);if(n(t))throw new RangeError(ip)}function Kt(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?Cc(t,e).effectSize:e}var dr=["two.sided","less","greater"];function wo(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function $c(e,t,n,r){if(r==="less")return cn(Bt(n,e),e,t,!0);if(r==="greater")return cn(Bt(n,e,{lowerTail:!1}),e,t,!1);let o=Bt(n/2,e,{lowerTail:!1});return cn(o,e,t,!1)+cn(-o,e,t,!0)}var Vc=e=>e<1e-8?Q(e,{lowerTail:!1}):Q(1-e);function zc(e,t,n){return n==="greater"?U(Vc(t)-e,{lowerTail:!1}):n==="less"?U(Q(t)-e):U(Vc(t/2)-e,{lowerTail:!1})+U(Q(t/2)-e)}function cp(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*=q(r,-o),n+=o}return q(t,n)}function lp(e={}){V(e,Ku,"pwrTTest");let{n:t,power:n}=e,r=Kt(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),jt(o,n),!Y(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=be(e.type,["two.sample","one.sample","paired"]),c=be(e.alternative,dr),i=s==="two.sample"?2:1;c==="two.sided"&&!Y(r)&&(r=Math.abs(r));let a=(m,u,p)=>$c((m-1)*i,Math.sqrt(m/i)*u,p,c);if(Y(n))n=a(t,r,o);else{let m=n;Y(t)?t=oe(u=>a(u,r,o)-m,2+1e-10,1e9):Y(r)?r=oe(u=>a(t,u,o)-m,...wo(c,[1e-7,10])):o=oe(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:c,...l===void 0?{}:{note:l},method:f,warnings:[]}}function fp(e={}){V(e,Uu,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=Kt(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),jt(s,r),!Y(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!Y(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let c=be(e.alternative,dr);c==="two.sided"&&!Y(o)&&(o=Math.abs(o));let i=(a,l,f,m)=>$c(a+l-2,f/Math.sqrt(1/a+1/l),m,c);if(Y(r))r=i(t,n,o,s);else{let a=r;Y(t)?t=oe(l=>i(l,n,o,s)-a,2+1e-10,1e9):Y(n)?n=oe(l=>i(t,l,o,s)-a,2+1e-10,1e9):Y(o)?o=oe(l=>i(t,n,l,s)-a,...wo(c,[1e-7,10])):s=oe(l=>i(t,n,o,l)-a,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:s,power:r,alternative:c,method:"t test power calculation",warnings:[]}}function Ds(e,t,n,r,o,s){let{n:c,power:i}=t,a=Kt(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(ft([a,c,i,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),jt(l,i),!Y(c)&&!(c>=1))throw new RangeError("number of observations in each group must be at least 1");let f=be(t.alternative,dr);f==="two.sided"&&!Y(a)&&(a=Math.abs(a));let m=(u,p,g)=>zc(u*r(p),g,f);if(Y(i))i=m(a,c,l);else{let u=i;Y(a)?a=oe(p=>m(p,c,l)-u,...wo(f,[s,10])):Y(c)?c=oe(p=>m(a,p,l)-u,o,1e9):l=oe(p=>m(a,c,p)-u,1e-10,1-1e-10)}return{es:a,n:c,sigLevel:l,power:i,alternative:f}}function mp(e={}){V(e,Gu,"pwr2pTest");let t=Ds(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 up(e={}){V(e,Hu,"pwrPTest");let t=Ds(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 pp(e={}){V(e,Xu,"pwrNormTest");let t=Ds(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 hp(e={}){V(e,Yu,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=Kt(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),jt(s,r),!Y(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!Y(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let c=be(e.alternative,dr);c==="two.sided"&&!Y(o)&&(o=Math.abs(o));let i=(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)=>zc(l*Math.sqrt(i(f,m)),u,c);if(Y(r))r=a(o,t,n,s);else{let l=r;Y(o)?o=oe(f=>a(f,t,n,s)-l,...wo(c,[1e-10,10])):Y(t)?t=oe(f=>a(o,f,n,s)-l,2+1e-10,1e9):Y(n)?n=oe(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=oe(f=>a(o,t,n,f)-l,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:s,power:r,alternative:c,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes",warnings:[]}}function dp(e={}){V(e,Bu,"pwrRTest");let{n:t,power:n}=e,r=Kt(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),jt(o,n),!Y(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!Y(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=be(e.alternative,dr);s==="two.sided"&&!Y(r)&&(r=Math.abs(r));let c=(a,l,f,m)=>{let u=Bt(m?f/2:f,a-2,{lowerTail:!1}),p=u**2+a-2,g=p===1/0&&Number.isFinite(u)?1/Math.sqrt(1+(a-2)/u/u):Math.sqrt(u**2/p),w=Math.atanh(l)+l/(2*(a-1)),h=Math.atanh(g),N=U((w-h)*Math.sqrt(a-3));return m?N+U((-w-h)*Math.sqrt(a-3)):N},i=(a,l,f)=>s==="two.sided"?c(a,l,f,!0):c(a,s==="less"?-l:l,f,!1);if(Y(n))n=i(t,r,o),s==="less"&&(r=-r);else{let a=n;if(Y(t))t=oe(l=>i(l,r,o)-a,4+1e-10,1e9);else if(Y(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=oe(m=>i(t,m,o)-a,l,f)}else o=oe(l=>i(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 gp(e){V(e,Qu,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=Kt(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([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(jt(s,n),!Y(o)&&!(o>=0))throw new RangeError("w must be positive");if(!Y(t)&&!(t>=1))throw new RangeError("number of observations must be at least 1");let c=(i,a,l)=>{let f=a*i**2;return f===1/0?1:St(la(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(Y(n))n=c(o,t,s);else{let i=n;Y(o)?o=oe(a=>c(a,t,s)-i,1e-10,1e9):Y(t)?t=oe(a=>c(o,a,s)-i,1+1e-10,1e9):s=oe(a=>c(o,t,a)-i,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 wp(e={}){V(e,Zu,"pwrAnovaTest");let{k:t,n,power:r}=e,o=Kt(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),jt(s,r),!Y(o)&&!(o>=0))throw new RangeError("f must be positive");if(!Y(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Y(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");let c=(i,a,l,f)=>{let m=i-1,u=(a-1)*i,p=i*a*l**2;return!Number.isFinite(p)&&Number.isFinite(i)&&Number.isFinite(a)&&Number.isFinite(l)&&(p=cp([i,a,l,l])),p===1/0?1:po(m,u)?ho(m,u,p,f):Xt(Vt(f,m,u,{lowerTail:!1}),m,u,{ncp:p,lowerTail:!1})};if(Y(r))r=c(t,n,o,s);else{let i=r;Y(t)?t=oe(a=>c(a,n,o,s)-i,2+1e-10,100):Y(n)?n=oe(a=>c(t,a,o,s)-i,2+1e-10,1e9):Y(o)?o=oe(a=>c(t,n,a,s)-i,1e-7,1e7):s=oe(a=>c(t,n,o,a)-i,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 bp(e={}){V(e,Ju,"pwrF2Test");let{u:t,v:n,power:r}=e,o=Kt(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),jt(s,r),!Y(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!Y(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!Y(n)&&!(n>=1))throw new RangeError("degree of freedom v for denominator must be at least 1");let c=(i,a,l,f)=>{let m=l*(i+a+1);return!Number.isFinite(m)&&Number.isFinite(i)&&Number.isFinite(a)&&Number.isFinite(l)&&(m=2*(l*(i/2+a/2+.5))),m===1/0?1:po(i,a)?ho(i,a,m,f):Xt(Vt(f,i,a,{lowerTail:!1}),i,a,{ncp:m,lowerTail:!1})};if(Y(r))r=c(t,n,o,s);else{let i=r;Y(t)?t=oe(a=>c(a,n,o,s)-i,1+1e-10,100):Y(n)?n=oe(a=>c(t,a,o,s)-i,1+1e-10,1e9):Y(o)?o=oe(a=>c(t,n,a,s)-i,1e-7,1e7):s=oe(a=>c(t,n,o,a)-i,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:s,power:r,method:"Multiple regression power calculation",warnings:[]}}var vp=["pooledSd","mu","paired","ci","alternative","adjust"],Np=["pooledSd","mu","paired","ci","alternative"],Mp=["generalized","partial","ci","alternative"],yp=["partial","ci","alternative"],Ep=["partial","ci","alternative"];function $s(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var xp="Missing values detected. NAs dropped.",Rp=`'y' is numeric but has only 2 unique values.
|
|
8
|
-
If this is a grouping variable, convert it to a factor.`,Uc={tol:0,maxiter:5e3};function Tp(e){return e===1/0?1:Math.exp(ma(.5,(e-1)/2)-.5*Math.log(e/2))}function Wc(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return tr(o=>_r(e,t,{ncp:o})-n,e-r,e+r,{...Uc,extendInt:"downX"}).root}function Gc(e,t,n,r){let o=n.pooledSd??!0,s=n.mu??0,c=n.paired??!1,i=n.ci===void 0?.95:n.ci;$s(i);let a=be(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(Ye(f)).size===2&&m.push(Rp),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(xp);let u=k=>k===1/0||k===-1/0;if(l.some(u)||(f?.some(u)??!1))throw new RangeError("the observations must be finite (NaN is dropped as missing)");let p,g,w,h,N,M;if(f===void 0||c){let k;if(f===void 0)({groups:[k],k:M}=ee([Ye(l)]));else{let[A,I]=dn(l,f);({d:k,k:M}=Ar(A,I))}let S=k.length;if(S<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");p=Ae(k),g=Math.sqrt(qe(k)),w=g/Math.sqrt(S),h=1/S,N=S-1}else{let k=Ie([Ye(l),Ye(f)]);M=k.k;let[S,A]=k.n;if(S<2||A<2)throw new RangeError("not enough observations: at least 2 are needed in each group");p=k.mean[0]-k.mean[1];let I=Math.sqrt(k.variance[0]),_=Math.sqrt(k.variance[1]);o?(g=Math.sqrt(((S-1)*I**2+(A-1)*_**2)/(S+A-2)),w=g*Math.sqrt(1/S+1/A),h=1/S+1/A,N=S+A-2):(g=Math.sqrt((I**2+_**2)/2),h=2*(A*I**2+S*_**2)/(S*A*(I**2+_**2)),w=Math.sqrt(Math.sqrt(I**2/S)**2+Math.sqrt(_**2/A)**2),N=qr([I**2/S,_**2/A],[S-1,A-1]))}let v=r?Tp(N):1,b=q(s,-M),d=(p-b)/g*v,E=r?"Hedges_g":"Cohens_d";if(i===null)return{name:E,value:d,warnings:m};let x=a==="two.sided"?i:2*i-1,y=(p-b)/w,R=Wc(y,N,(1+x)/2)*Math.sqrt(h)*v,T=Wc(y,N,(1-x)/2)*Math.sqrt(h)*v;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:E,value:d,ci:i,ciLow:R,ciHigh:T,warnings:m}}function kp(e,t,n={}){return V(n,vp,"cohensD"),j("cohensD",Gc(e,t,n,n.adjust??!1))}function Sp(e,t,n={}){return V(n,Np,"hedgesG"),j("hedgesG",Gc(e,t,n,!0))}var Ap=e=>Array.isArray(e.effects),Vs=e=>e.multistratum===!0,Yc=e=>Array.isArray(e.strata),Hc=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Cs=e=>({test:t=>t.split(":").includes(e)});function qp(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let s=o.stratum.replace(/^Error: /,""),c=o.colNames.indexOf("Df"),i=o.colNames.indexOf("Sum Sq"),a=o.rowNames.map((f,m)=>({name:f,df:o.table[m][c],ss:o.table[m][i]})),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 Ip(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 p=t.table[u],g=e.within.filter(N=>Cs(N).test(m)),w=o(g.length===0?"Subject":`Subject:${g.join(":")}`,p[l],p[f]);if(m==="(Intercept)")return;let h={name:m,df:p[a],ss:p[n]};w.terms.push(h),s.set(m,h)})}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 p={name:m,df:t.table[u][a],ss:t.table[u][n]};f.terms.push(p),s.set(m,p)})}let c=r.find(a=>a.name==="Subject");if(c===void 0)throw new Error("the result has no subject stratum");let i=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:c,order:i}}function Xc(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(c=>Cs(s).test(c.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(c=>Cs(c).test(s));return{isObserved:o,ssObserved:t.reduce((s,c)=>o(c.name)?s+c.ss:s,0)}}function jc(e,t,n,r){let o=c=>Xt(e,t,n,{ncp:c})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:tr(o,0,Math.max(10,e*t*4),{...Uc,extendInt:"downX"}).root:0}function Bc(e,t,n,r,o){let s=o==="two.sided"?r:2*r-1,c=Math.max(0,e),i=c/t/((1-c)/n),a=jc(i,t,n,(1+s)/2),l=jc(i,t,n,(1-s)/2);i<=Vt((1-s)/2,t,n)&&(l=0),i<=Vt((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 _p(e){let t=ee([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=q(n.ss,-t)}var Kc="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function zs(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Kc);if(Vs(e)||Yc(e)||Hc(e))return Lp(e,t,n);let r=Ap(e)?Re(()=>ps(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Kc);let o=$e.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,c=n.ci===void 0?.95:n.ci;$s(c);let i=be(n.alternative??"greater",["two.sided","less","greater"]),a=s.colNames.indexOf("Df"),l=s.colNames.indexOf("Sum Sq");if(a<0||l<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let f=s.rowNames.map((R,T)=>({name:R,df:s.table[T][a],ss:s.table[T][l]})).filter(R=>R.name!=="(Intercept)");_p(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),p=m.df,g=m.ss,w=g/p,h=f.reduce((R,T)=>R+T.ss,0),N=f.reduce((R,T)=>R+T.df,0)+1,M=t==="Eta2"?n.generalized??!1:!1,b=u.length===1?"plain":M!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:E}=Xc(M,u),x=R=>{let{ss:T,df:k}=R;if(t==="Eta2")return b==="partial"?T/(T+g):b==="plain"?T/h:T/(T+g+E-(d(R.name)?T:0));if(t==="Omega2"){let A=b==="partial"?(T-k*w)/(T+(N-k)*w):(T-k*w)/(h+w);return Math.max(0,A)}let S=b==="partial"?(T-k*w)/(T+g):(T-k*w)/h;return Math.max(0,S)},y=`${t}${b==="plain"?"":`_${b}`}`;return{colNames:c===null?[y]:[y,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>c===null?[x(R)]:Bc(x(R),R.df,p,c,i)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function Lp(e,t,n){let r=n.ci===void 0?.95:n.ci;$s(r);let o=be(n.alternative??"greater",["two.sided","less","greater"]),s=Hc(e),c,i,a;if(s){let d=$e.get(e),E=Ip(d!==void 0&&!Array.isArray(d)?{...e,univariate:d}:e);({strata:c,subject:i}=E),a=E.order.map(x=>({term:x,stratum:E.strata.find(y=>y.terms.includes(x))}))}else{let d=Vs(e)?Re(()=>hs(e)):e,E=$e.get(d);({strata:c,subject:i}=qp(Array.isArray(E)?{...d,strata:E.map((x,y)=>({...d.strata[y],...x}))}:d)),a=c.flatMap(x=>x.terms.map(y=>({term:y,stratum:x})))}let l=ee([[...c.flatMap(d=>d.terms.map(E=>E.ss)),...c.map(d=>d.sse)]]).k;for(let d of c){d.sse=q(d.sse,-l);for(let E of d.terms)E.ss=q(E.ss,-l)}let f=c.flatMap(d=>d.terms),m=c.reduce((d,E)=>d+E.sse,0),u=f.reduce((d,E)=>d+E.ss,0)+m,p=i.sse/i.dfe,g=t==="Eta2"?n.generalized??!1:!1,w=g!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:h,ssObserved:N}=Xc(g,f),M=(d,E)=>{let{ss:x,df:y}=d,R=E.sse/E.dfe;if(t==="Eta2")return w==="partial"?x/(x+E.sse):w==="plain"?x/u:x/(x+m+N-(h(d.name)?x:0));if(t==="Omega2"){let k=E===i?0:1,S=w==="partial"?(x-y*R)/(x+k*E.sse+i.sse+p):(x-y*R)/(u+p);return Math.max(0,S)}let T=w==="partial"?(x-y*R)/(x+E.sse):(x-y*R)/u;return Math.max(0,T)},v=`${t}${w==="plain"?"":`_${w}`}`,b={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(d=>d.term.name),table:a.map(({term:d,stratum:E})=>{let x=M(d,E);return r===null?[x]:Bc(x,d.df,E.dfe,r,o)}),warnings:[]};return s||(b.groups=a.map(d=>d.stratum.name),b.warnings=[...Vs(e)||Yc(e)?e.warnings:[]]),b}function Op(e,t={}){return V(t,Mp,"etaSquared"),j("etaSquared",zs(e,"Eta2",t),e)}function Fp(e,t={}){return V(t,yp,"omegaSquared"),j("omegaSquared",zs(e,"Omega2",t),e)}function Pp(e,t={}){return V(t,Ep,"epsilonSquared"),j("epsilonSquared",zs(e,"Epsilon2",t),e)}var Dp=["id","dv","between","within","type","correction","es","observed"],bo=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Vp=1e-12,Qc=1e-24;function Zc(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 gr=e=>typeof e=="boolean"?e?"TRUE":"FALSE":Pe(e);function Cp(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(gr):[...new Set(t.map(gr))].sort(it)}var Jc=(e,t)=>t.split(":").includes(e);function el(e,t,n,r,o){let s=l=>r.some(f=>Jc(f,l));for(let l of r)if(!e.some(f=>Jc(l,f.name)))throw new RangeError(`Observed variable not in data: ${l}`);let c=[...t.values()].reduce((l,f)=>l+f,0),i=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+c+i-(s(l.name)?l.ss:0)));return[l.numDf,l.denDf,o(l.errSS/l.denDf),l.f,...f,l.p]})}}function $p(e,t){V(t,Dp,"aovEz");let n=bo(t.between),r=bo(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[D,G]of[["between",n],["within",r]]){let H=G.filter((me,Le)=>G.indexOf(me)!==Le);if(H.length>0)throw new RangeError(`duplicated factor names in '${D}': ${[...new Set(H)].join(", ")}`)}let o=n.filter(D=>r.includes(D));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 c=t.correction??"GG";if(!["GG","HF","none"].includes(c))throw new RangeError(`invalid 'correction': ${String(c)}`);let i=bo(t.es??"ges");for(let D of i)if(D!=="ges"&&D!=="pes")throw new RangeError(`invalid 'es': ${String(D)}`);let a=bo(t.observed),l=[],f=D=>D==null||typeof D=="number"&&Number.isNaN(D),m=Zc(e,t.dv),u=[t.id,...n,...r].map(D=>Zc(e,D));for(let D of u)if(D.length!==m.length)throw new RangeError("the columns must have the same length");let p=m.map((D,G)=>u.every(H=>!f(H[G]))),g=D=>D.filter((G,H)=>p[H]),w=Fe(g(u[0])).map(gr),h=g(m).map(D=>{if(f(D))return NaN;if(typeof D!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(D))throw new RangeError(`the response '${t.dv}' must be finite`);return D}),N=n.map((D,G)=>Fe(g(u[1+G]))),M=r.map((D,G)=>Fe(g(u[1+n.length+G]))),v=w.length;for(let D of[h,...N,...M])if(D.length!==v)throw new RangeError("the columns must have the same length");let b=N.map(D=>Cp(D)),d=N.map(D=>D.map(gr)),E=M.map(D=>D.map(gr)),x=E.map(D=>[...new Set(D)]);b.forEach((D,G)=>{if(D.length<2)throw new Error(`Factor "${n[G]}" consists of one level only. Remove factor from model?`)}),x.forEach((D,G)=>{if(D.length<2)throw new Error(`Factor "${r[G]}" consists of one level only. Remove factor from model?`)});let y=[...new Set(w)],R=new Map,T=new Set;if(w.forEach((D,G)=>{let H=JSON.stringify(d.map(Le=>Le[G])),me=R.get(D);me===void 0?R.set(D,H):me!==H&&T.add(D)}),T.size>0)throw new Error(`Following ids are in more than one between subjects condition:
|
|
9
|
-
${y.filter(D=>T.has(D)).join(", ")}`);let k=[[]];for(let D of x)k=k.flatMap(G=>D.map(H=>[...G,H]));let S=D=>JSON.stringify(E.map(G=>G[D])),A=new Map(k.map((D,G)=>[JSON.stringify(D),G])),I=new Set(w.map((D,G)=>S(G)));if(k.some(D=>!I.has(JSON.stringify(D))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let _=new Map,$=new Set;w.forEach((D,G)=>{let H=_.get(D);H===void 0&&_.set(D,H=new Set),H.add(S(G)),Number.isNaN(h[G])&&$.add(D)});let P=y.filter(D=>$.has(D)||_.get(D).size<k.length),C=new Set(P),W=ee([h.filter((D,G)=>!C.has(w[G]))]).k,F=W===0?h:h.map(D=>q(D,-W)),L=D=>q(D,2*W),z=k.length,X=new Map;if(w.forEach((D,G)=>{let H=X.get(D);H===void 0&&X.set(D,H={s:k.map(()=>[0,0]),n:k.map(()=>0)});let me=A.get(S(G));H.s[me]=Z(H.s[me],[F[G],0]),H.n[me]++}),[...X.values()].some(D=>D.n.some(G=>G>1))&&l.push("More than one observation per design cell, aggregating data using `fun_aggregate = mean`.\nTo turn off this warning, pass `fun_aggregate = mean` explicitly."),P.length>0){let D=[...P].sort(it),G=D.length>=10?`${D.slice(0,10).join(", ")}, ... [showing first 10 only]`:D.join(", ");l.push(`Missing values for ${P.length} ID(s), which were removed before analysis:
|
|
10
|
-
${G}`)}let te=y.filter(D=>!P.includes(D)),ne=new Map;w.forEach((D,G)=>ne.has(D)||ne.set(D,G));let ae=te.map(D=>{let G=X.get(D);return G.s.map((H,me)=>kt(H,G.n[me]))}),ye={};n.forEach((D,G)=>ye[D]=te.map(H=>d[G][ne.get(H)]));let _e=Object.fromEntries(n.map(D=>[D,"contr.sum"])),ze=Object.fromEntries(n.map((D,G)=>[D,b[G]])),pt="Rank deficient model matrix; insufficient data to estimate full model.";if(z===1)return j("aovEz",B());return j("aovEz",J());function B(){let D="y";for(;n.includes(D);)D+="_";let G={...ye,[D]:ae.map(je=>je[0][0])},H=Re(()=>ws(`${D} ~ ${n.length>0?n.join(" * "):"1"}`,G,{contrasts:_e,levels:ze}));if(H.rank<H.coefNames.length)throw new Error(pt);if(H.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${ae.length} subject(s) for ${H.rank} parameter(s)`);let me=Re(()=>Is(H,{type:s}));l.push(...me.warnings.filter(je=>!l.includes(je)));let Le=H.dfResidual,He=me.rowNames.flatMap((je,nt)=>{if(je==="(Intercept)"||je==="Residuals")return[];let[yt,Lt,ln,Ot]=me.table[nt];return[{name:je,ss:yt,numDf:Lt,errSS:H.rss,denDf:Le,f:ln,p:Ot}]}),Xe=me.colNames.indexOf("Sum Sq"),We={anova:el(He,new Map([[0,H.rss]]),i,a,L),univariate:{colNames:me.colNames,rowNames:me.rowNames,table:me.table.map(je=>je.map((nt,yt)=>yt===Xe?L(nt):nt))},within:r,warnings:l};return $e.set(We,{colNames:me.colNames,rowNames:me.rowNames,table:me.table}),We}function J(){let D=te.length,G=ae.reduce((ie,ce)=>ce.reduce((he,Ue)=>he+Ue[0]*Ue[0],ie),0),H=n.length>0?Re(()=>xn(`~ ${n.join(" * ")}`,ye,{contrasts:_e,levels:ze})):{colNames:["(Intercept)"],assign:[0],x:te.map(()=>[1]),termLabels:[],factors:[]},me=H.colNames.length;if(D===0||Ne(H.x).rank<me)throw new Error(pt);let Le=D-me;if(Le<=0)throw new RangeError(`no residual degrees of freedom: ${D} subject(s) for ${me} between-subject parameter(s)`);let He=[0,...H.termLabels.map((ie,ce)=>ce+1)],Xe=["(Intercept)",...H.termLabels],We=ie=>H.assign.flatMap((ce,he)=>ie.includes(ce)?[he]:[]),je=ie=>H.factors.flatMap((ce,he)=>ce[ie-1]>0?[he]:[]),nt=ie=>H.x.map(ce=>[ce[ie],0]),yt=new Array(D).fill([0,0]),Lt=(ie,ce,he)=>{let{qtv:Ue}=_t([...ie,...ce].map(nt),yt,he);return Ue.map(zn=>zn.slice(ie.length,ie.length+ce.length))},ln=ie=>{let{qtv:ce}=_t(H.colNames.map((he,Ue)=>nt(Ue)),yt,ie);return ce.map(he=>he.slice(me))},Ot=Object.fromEntries(r.map((ie,ce)=>[ie,k.map(he=>he[ce])])),Et=Re(()=>xn(`~ ${r.join(" * ")}`,Ot,{contrasts:Object.fromEntries(r.map(ie=>[ie,"contr.sum"])),levels:Object.fromEntries(r.map((ie,ce)=>[ie,x[ce]]))})),Mr=[0,...Et.termLabels.map((ie,ce)=>ce+1)],Cn=["(Intercept)",...Et.termLabels],yr=ie=>ie.map(ce=>ie.map(he=>{let Ue=[0,0];return ce.forEach((zn,fn)=>Ue=Z(Ue,pe(zn,he[fn]))),Ue})),$n=[],O=[],K=[],re=[],ue=[],le=[],Ee=[],rt=new Map,Ft=!1;for(let ie of Mr){let ce=Et.assign.flatMap((Ge,ke)=>Ge===ie?[ke]:[]),he=ce.length,Ue=ce.map(Ge=>ae.map(ke=>ke.reduce((ht,ot,Kn)=>Z(ht,at(ot,Et.x[Kn][Ge])),[0,0]))),zn=ce.map(Ge=>ce.map(ke=>[Et.x.reduce((ht,ot)=>ht+ot[Ge]*ot[ke],0),0])),fn=Oc(zn),ko=yr(ln(Ue)),Er=sn(ko,fn),Wn=an(Er)[0],mt=NaN,jn=NaN,So=NaN,na=NaN,xr=!1;if(he>=2){let Ge=Cn[ie],ke=$a(_s(fn)).map(xt=>xt.map(Yt=>[Yt,0])),ht=_s(sn(sn(ke,ko),Ic(ke))),ot=ct(ht,{symmetric:!0,onlyValues:!0}).values,Kn=ot.reduce((xt,Yt)=>Math.max(xt,Yt),0),st=Math.max(Kn*Vp,G*Qc),Je=an(Er),dt=an(sn(Er,Er));Kn>G*Qc&&dt[0]>0&&Number.isFinite(dt[0])?(xr=!0,mt=Se(pe(Je,Je),at(dt,he))[0],Le-he*mt>0?jn=((Le+1)*he*mt-2)/(he*(Le-he*mt)):l.push(`The Huynh-Feldt epsilon of ${Ge} is undefined (d - p GG <= 0)${c==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${Ge} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),xr&&ot.every(xt=>xt>st)?(So=Ls(sn(fn,ko)),na=Os(So,he,Le,z)):xr&&l.push(`The error SSP matrix of ${Ge} is singular: no Mauchly test (the epsilons are still defined)`)}for(let Ge=0;Ge<He.length;Ge++){let ke=He[Ge],ht;if(s===3)ht=Lt(We(He.filter(Pt=>Pt!==ke)),We([ke]),Ue);else if(ke===0)ht=Lt([],We([0]),Ue);else{let Pt=je(ke),Un=He.filter(Rt=>Rt!==0&&Rt!==ke&&Pt.every(xl=>H.factors[xl][Rt-1]>0));ht=Lt(We(He.filter(Rt=>Rt!==ke&&!Un.includes(Rt))),We([ke]),Ue)}let ot=an(sn(yr(ht),fn))[0],st=(ke===0?1:We([ke]).length)*he,Je=Le*he,dt=ot/st/(Wn/Je),xt=we(dt,st,Je,!1,!1),Yt=ie===0?Xe[Ge]:ke===0?Cn[ie]:`${Xe[Ge]}:${Cn[ie]}`;$n.push([L(ot),st,L(Wn),Je,dt,xt]),O.push([ot,st,Wn,Je,dt,xt]),K.push(Yt),rt.set(ie,Wn);let mn={name:Yt,ss:ot,numDf:st,errSS:Wn,denDf:Je,f:dt,p:xt};if(xr){let Pt=Math.min(1,jn),Un=we(dt,mt*st,mt*Je,!1,!1),Rt=we(dt,Pt*st,Pt*Je,!1,!1);re.push([mt,Un,jn,Rt]),ue.push([So,na]),le.push(Yt),c==="GG"?mn={...mn,numDf:mt*st,denDf:mt*Je,p:Un}:c==="HF"&&(jn>1&&(Ft=!0),mn=Number.isNaN(jn)?{...mn,numDf:mt*st,denDf:mt*Je,p:Un}:{...mn,numDf:Pt*st,denDf:Pt*Je,p:Rt})}ie===0&&ke===0||Ee.push(mn)}}Ft&&l.push("HF eps > 1 treated as 1");let Gt={anova:el(Ee,rt,i,a,L),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:K,table:$n},within:r,warnings:l};return re.length>0&&(Gt.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:le,table:re},Gt.sphericity={colNames:["Test statistic","p-value"],rowNames:[...le],table:ue}),$e.set(Gt,{colNames:Gt.univariate.colNames,rowNames:K,table:O}),Gt}}function vo(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(s=>Number.isFinite(s)))).sort((s,c)=>s-c);if(n.length<2)return e.slice();let r=Ae(n.map(Math.abs)),o=[n[0]];for(let s=0;s+1<n.length;s++){let c=n[s+1]-n[s];c<=t||c/r<=t||o.push(n[s+1])}return e.map(s=>{if(!Number.isFinite(s))return s;let c=0,i=o.length-1;for(;c<i;){let a=Math.floor((c+i+1)/2);o[a]<=s?c=a:i=a-1}return o[c]})}var zp=["rho"],Wp=["group","confType","confInt"],jp=e=>e==null||typeof e=="number"&&Number.isNaN(e);function tl(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&&jp(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=Fe(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 c=vo(r.map(a=>e[a])),i=r.map((a,l)=>({time:c[l],event:t[a]===1,group:n===void 0?0:s.get(n[a])}));return i.sort((a,l)=>a.time-l.time),{obs:i,labels:o.map(a=>`group=${Pe(a)}`)}}function Kp(e,t,n,r={}){V(r,zp,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:c}=tl(e,t,n),i=c.length;if(i<2)throw new RangeError("there is only 1 group");let a=new Array(i).fill(0);for(let v of s)a[v.group]++;let l=new Array(i).fill(0),f=new Array(i).fill(0),m=Array.from({length:i},()=>new Array(i).fill(0)),u=a.slice(),p=s.length,g=1;for(let v=0;v<s.length;){let b=s[v].time,d=v,E=new Array(i).fill(0),x=new Array(i).fill(0),y=0;for(;d<s.length&&s[d].time===b;){let R=s[d];x[R.group]++,R.event&&(E[R.group]++,y++),d++}if(y>0){let R=o===0?1:g**o;for(let T=0;T<i;T++)l[T]+=R*E[T],f[T]+=R*y*u[T]/p;if(p>1){let T=R*R*y*(p-y)/(p*(p-1));for(let k=0;k<i;k++)for(let S=0;S<i;S++)m[k][S]+=T*u[k]*((k===S?1:0)-u[S]/p)}g*=1-y/p}for(let R=0;R<i;R++)u[R]-=x[R];p-=d-v,v=d}let w=f.map((v,b)=>v>0?b:-1).filter(v=>v>=0),h=0,N=Math.max(w.length-1,0);if(w.length>=2){let v=w.slice(1),b=v.map(E=>l[E]-f[E]),d=v.map(E=>v.map(x=>m[E][x]));try{let E=Ce(d,b);h=b.reduce((x,y,R)=>x+y*E[R],0)}catch{({chisq:h,df:N}=Up(d,b))}}let M=N===0?NaN:St(h,N,{lowerTail:!1});return{n:a,groups:c,obs:l,exp:f,var:m,chisq:h,df:N,pvalue:M,pValue:M,warnings:[]}}function Up(e,t){let n=ct(e,{symmetric:!0}),r=n.values,o=r.reduce((i,a)=>Math.max(i,a),0),s=0,c=0;return r.forEach((i,a)=>{if(!(o>0&&i>1e-10*o))return;let l=t.reduce((f,m,u)=>f+m*n.vectors[u][a],0);s+=l*l/i,c++}),{chisq:s,df:c}}function Ws(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 Qt(e[r],e[s])}return e[r]}function Gp(e,t,n={}){V(n,Wp,"survfit");let{group:r,confType:o="log",confInt:s=.95}=n;if(o!=="log"&&o!=="plain"&&o!=="log-log")throw new RangeError(`invalid 'confType': ${String(o)}`);if(!(s>0&&s<1))throw new RangeError("'confInt' must be between 0 and 1");let{obs:c,labels:i}=tl(e,t,r),a=r===void 0?["all"]:i,l=(1-s)/2,f=l<1e-8?Q(l,{lowerTail:!1}):Q(1-(1-s)/2),m=[],u={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:m,stdError:m,lower:[],upper:[],median:[],medianRows:a};return a.forEach((p,g)=>{let w=c.filter(d=>d.group===g),h=u.time.length,N=w.length,M=1,v=0;for(let d=0;d<w.length;){let E=w[d].time,x=d,y=0;for(;x<w.length&&w[x].time===E;)w[x].event&&y++,x++;let R=x-d-y;y>0&&(M*=1-y/N,v+=y<N?y/(N*(N-y)):1/0);let T=Math.sqrt(v),k,S;if(o==="log")k=M===0?NaN:M*Math.exp(-f*T),S=M===0?NaN:Math.min(1,M*Math.exp(f*T));else if(o==="plain"){let A=f*T*M;k=Number.isNaN(A)?NaN:Math.max(0,M-A),S=Number.isNaN(A)?NaN:Math.min(1,M+A)}else if(M===0||M===1)k=NaN,S=NaN;else{let A=Math.log(M),I=Math.log(-A);k=Math.exp(-Math.exp(I-f*T/A)),S=Math.exp(-Math.exp(I+f*T/A))}u.strata.push(p),u.time.push(E),u.nRisk.push(N),u.nEvent.push(y),u.nCensor.push(R),u.surv.push(M),u.stdErr.push(M*T),u.lower.push(k),u.upper.push(S),N-=x-d,d=x}let b=u.time.slice(h);u.median.push([Ws(b,u.surv.slice(h)),Ws(b,u.lower.slice(h)),Ws(b,u.upper.slice(h))])}),u}var Yp=["ties","weights","strata","init","control","nocenter","confLevel"],Hp=["eps","iterMax","tolerChol","tolerInf"],Xp=e=>e==null||typeof e=="number"&&Number.isNaN(e),Bp=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,nl=700;function js(e,t,n){let{time:r,event:o,weight:s,x:c,stratumEnd:i}=e,a=r.length,l=t.length,f=()=>new Array(l).fill(0),m=()=>Array.from({length:l},f),u=f(),p=m(),g=0,w=0,h=f(),N=m(),M=f(),v=m(),b=0,d=0,E=x=>{let y=Math.exp(d-x);w*=y,b*=y;for(let R=0;R<l;R++){h[R]*=y,M[R]*=y;let T=N[R],k=v[R];for(let S=0;S<=R;S++)T[S]*=y,k[S]*=y}d=x};for(let x=a-1;x>=0;){i[x]&&(d=0,w=0,h=f(),N=m());let y=r[x],R=0,T=0;b=0;let k=x;do{let S=c[k],A=0;for(let $=0;$<l;$++)A+=t[$]*S[$];let I=s[k];Number.isFinite(A)&&(A-d>nl?E(A):A-d<-nl&&w===0&&b===0&&(d=A));let _=Math.exp(A-d)*I;if(o[k]){R++,T+=I,b+=_,g+=I*A;for(let $=0;$<l;$++){u[$]+=I*S[$],M[$]+=_*S[$];let P=v[$];for(let C=0;C<=$;C++)P[C]+=_*S[$]*S[C]}}else{w+=_;for(let $=0;$<l;$++){h[$]+=_*S[$];let P=N[$];for(let C=0;C<=$;C++)P[C]+=_*S[$]*S[C]}}k--}while(k>=0&&r[k]===y&&!i[k]);if(R>0){let S=n?R:1,A=n?R:1,I=T/S;for(let _=0;_<S;_++){w+=n?b/A:b,g-=I*(Math.log(w)+d);for(let $=0;$<l;$++){h[$]+=n?M[$]/A:M[$];let P=h[$]/w;u[$]-=I*P;let C=N[$],W=v[$],F=p[$];for(let L=0;L<=$;L++)C[L]+=n?W[L]/A:W[L],F[L]+=I*(C[L]-P*h[L])/w}}M.fill(0);for(let _ of v)_.fill(0)}x=k}for(let x=0;x<l;x++)for(let y=0;y<x;y++)p[y][x]=p[x][y];return{loglik:g,score:u,info:p}}function Ks(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,c=0,i=!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&&(i=!1);for(let f=a+1;f<n;f++)e[f][a]=0;continue}c++;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 i?c:-c}function Us(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let s=r[o];for(let c=0;c<o;c++)s-=r[c]*e[o][c];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 c=o+1;c<n;c++)s-=r[c]*e[c][o];r[o]=s}return r}function Gs(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 No=(e,t)=>({statistic:e,df:t,pValue:St(e,t,{lowerTail:!1})});function Qp(e,t){let n=e.length,r=()=>new Array(t).fill(NaN),o=No(0,0),s=new Array(t).fill(0),c=r(),i=r();return{coefficients:r(),var:Array.from({length:t},()=>new Array(t).fill(0)),loglik:[0,0],score:0,wald:0,iter:0,means:Array.from({length:t},(a,l)=>Ae(e.map(f=>f[l]))),n,nevent:0,linearPredictors:new Array(n).fill(0),se:s,z:c,p:i,stdError:s,zValue:c,pValue:i,lower:r(),upper:r(),logtest:o,sctest:{...o},waldtest:{...o},rsq:{rsq:0,maxrsq:0},warnings:[]}}function Zp(e,t,n,r={}){V(r,Yp,"coxph"),V(Dt(r,"control"),Hp,"coxph","control");let{ties:o="efron",weights:s,strata:c,init:i,control:a={},nocenter:l=[-1,0,1],confLevel:f=.95}=r,{eps:m=1e-9,iterMax:u=20,tolerChol:p=2**-39}=a,g=a.tolerInf??Math.sqrt(m);if(o!=="efron"&&o!=="breslow")throw new RangeError(`invalid 'ties': ${String(o)}`);if(!(m>0&&m<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(p>0&&p<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(g>0&&g<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(u)&&u>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(f>0&&f<1))throw new RangeError("'confLevel' must be between 0 and 1");let w=e.length;if(t.length!==w||n.length!==w)throw new RangeError("'time', 'status' and 'x' must have the same length");if(s!==void 0&&s.length!==w)throw new RangeError("'weights' must have the same length as 'time'");if(c!==void 0&&c.length!==w)throw new RangeError("'strata' must have the same length as 'time'");let h=w===0?0:n[0].length;if(h<1)throw new RangeError("'x' must have at least one column");if(n.some(O=>O.length!==h))throw new RangeError("the rows of 'x' must have the same length");if(i!==void 0&&i.length!==h)throw new RangeError("'init' must have one value per column of 'x'");if(i?.some(O=>!Number.isFinite(O)))throw new RangeError("'init' must contain finite values");let N=[];for(let O=0;O<w;O++){if(Number.isNaN(e[O])||Number.isNaN(t[O])||n[O].some(re=>Number.isNaN(re))||s!==void 0&&Number.isNaN(s[O])||c!==void 0&&Xp(c[O]))continue;let K=t[O];if(K!==0&&K!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[O]===-1/0||e[O]===1/0&&K===1)throw new RangeError("event times must be finite");N.push(O)}let M=N.length;if(M===0)throw new RangeError("no observations without missing values");if(N.some(O=>n[O].some(K=>!Number.isFinite(K))))throw new RangeError("'x' contains infinite values");let v=N.map(O=>s===void 0?1:s[O]);if(v.some(O=>!(O>0)))throw new RangeError("Invalid weights, must be >0");if(v.some(O=>!Number.isInteger(O)))throw new RangeError("non-integer weights (robust variance) are not supported yet");let b=0;for(let O of N)b+=t[O];if(b===0)return Qp(N.map(O=>n[O]),h);let d=vo(N.map(O=>e[O])),E=new Array(M).fill(0);if(c!==void 0){let O=Fe(c),K=Array.from(new Set(N.map(ue=>O[ue]))).sort(Bp),re=new Map;K.forEach((ue,le)=>re.set(ue,le)),E=N.map(ue=>re.get(O[ue]))}let x=Array.from({length:M},(O,K)=>K).sort((O,K)=>E[O]-E[K]||d[O]-d[K]||O-K),y=0;for(let O of v)y+=O;let R=x.map(O=>v[O]),T=x.map(O=>n[N[O]].slice()),k=new Array(h).fill(0),S=new Array(h).fill(1),A=new Array(h).fill(0),I=new Set(l);for(let O=0;O<h;O++){if(T.every(le=>I.has(le[O])))continue;let K=T.map(le=>le[O]),re=le=>{let Ee=0;for(let ce=0;ce<M;ce++)Ee+=R[ce]*le[ce];let rt=Ee/y,Ft=0,Gt=le.map((ce,he)=>(Ft+=R[he]*Math.abs(ce-rt),ce-rt)),ie=Ft===0?1:y/Ft;return{m:rt,s:ie,d:Gt,ok:Number.isFinite(rt)&&ie>=2**-1022&&ie<1/0}},ue=re(K);if(!ue.ok){let{groups:le,k:Ee}=ee([K]);ue=re(le[0]),A[O]=Ee}for(let le=0;le<M;le++)T[le][O]=ue.d[le]*ue.s;k[O]=q(ue.m,A[O]),S[O]=ue.s}let _={time:x.map(O=>d[O]),event:x.map(O=>t[N[O]]===1),weight:R,x:T,stratumEnd:x.map((O,K)=>K===M-1||E[x[K+1]]!==E[O])},$=o==="efron",P=i===void 0?new Array(h).fill(0):i.map((O,K)=>q(O/S[K],A[K])),C=js(_,P,$),W=[C.loglik,C.loglik],F=C.info.map(O=>O.slice()),L=Ks(F,p),z=Us(F,C.score),X=0;for(let O=0;O<h;O++)X+=C.score[O]*z[O];let te=0,ne,ae=!1;if(u===0||!Number.isFinite(C.loglik))ne=Gs(F);else{let O=C.loglik,K=P.map((le,Ee)=>le+z[Ee]),re=0,ue=!1;for(te=1;te<=u;te++){C=js(_,K,$),F=C.info.map(Ee=>Ee.slice()),L=Ks(F,p);let le=Number.isFinite(C.loglik)&&C.score.every(Number.isFinite)&&C.info.every(Ee=>Ee.every(Number.isFinite));if(le&&(C.loglik===O||Math.abs(1-O/C.loglik)<=m)){ue=!0,W[1]=C.loglik,P=K;break}!le||C.loglik<O?(re++,K=K.map((Ee,rt)=>(Ee+re*P[rt])/(re+1))):(re=0,O=C.loglik,W[1]=O,z=Us(F,C.score),P=K,K=K.map((Ee,rt)=>Ee+z[rt]))}ue?ne=Gs(F):(te=u+1,ae=!0,u>1?(C=js(_,P,$),W[1]=C.loglik,ne=C.info.map((le,Ee)=>le.map((rt,Ft)=>Ee===Ft?le[Ee]>0?1/le[Ee]:0:-0))):ne=Gs(F))}let ye=S.map(O=>Math.floor(Math.log2(O))),_e=S.map((O,K)=>q(O,-ye[K])),ze=ye.map((O,K)=>O-A[K]),pt=P.map((O,K)=>O*_e[K]),B=C.score.map((O,K)=>O/_e[K]),J=ne.map((O,K)=>O.map((re,ue)=>re*_e[K]*_e[ue])),D=pt.map((O,K)=>q(O,ze[K])),G=J.map((O,K)=>O.map((re,ue)=>q(re,ze[K]+ze[ue]))),H=D.slice();if(u>0&&Math.abs(L)<h)for(let O=0;O<h;O++)ne[O][O]===0&&(H[O]=NaN);let me=0;for(let O=0;O<h;O++)me+=D[O]*k[O];let Le=N.map(O=>{let K=0;for(let re=0;re<h;re++)K+=n[O][re]*D[re];return K-me}),He=new Te;if(u>1){let O=J.map((K,re)=>{let ue=0;for(let le=0;le<h;le++)ue+=B[le]*J[le][re];return Math.abs(ue)});if(ae)He.add("Ran out of iterations and did not converge"),(ge(Le)>500||O.some(K=>!Number.isFinite(K)))&&He.add("one or more coefficients may be infinite");else{let K=[];for(let re=0;re<h;re++){let ue=O[re];(!Number.isFinite(B[re])||q(ue,ze[re])>m&&ue>g*Math.abs(pt[re]))&&K.push(re+1)}K.length>0&&He.add(`Loglik converged before variable ${K.join(",")} ; coefficient may be infinite. `)}}let Xe=H.map((O,K)=>Number.isNaN(O)?-1:K).filter(O=>O>=0),We=Xe.length,je=Xe.map(O=>pt[O]-(i===void 0?0:q(i[O],-ze[O]))),nt=0;if(We===1)nt=je[0]*je[0]/J[Xe[0]][Xe[0]];else if(We>1){let O=Xe.map(re=>Xe.map(ue=>J[re][ue]));Ks(O,p);let K=Us(O,je);for(let re=0;re<We;re++)nt+=je[re]*K[re]}let yt=(1-f)/2,Lt=yt<1e-8?Q(yt,{lowerTail:!1}):Q((1+f)/2),ln=J.map((O,K)=>Math.sqrt(O[K])),Ot=ln.map((O,K)=>q(O,ze[K])),Et=H.map((O,K)=>(Number.isNaN(O)?O:pt[K])/ln[K]),Mr=Et.map(O=>St(O*O,1,{lowerTail:!1})),Cn=H.map((O,K)=>Math.exp(O-Lt*Ot[K])),yr=H.map((O,K)=>Math.exp(O+Lt*Ot[K])),$n=-2*(W[0]-W[1]);return j("coxph",{coefficients:H,var:G,loglik:W,score:X,wald:nt,iter:te,means:k,n:M,nevent:b,linearPredictors:Le,se:Ot,z:Et,p:Mr,stdError:Ot,zValue:Et,pValue:Mr,lower:Cn,upper:yr,logtest:No($n,We),sctest:No(X,We),waldtest:No(nt,We),rsq:{rsq:1-Math.exp(-$n/M),maxrsq:1-Math.exp(2*W[0]/M)},warnings:He.list})}var Jp=["center"],eh=2**-1022;function th(e,t,n={}){V(n,Jp,"leveneTest");let r=be(n.center??"median",["median","mean"]),{groups:o}=zt(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"?Br(l):Ae(l);return l.map(m=>Math.abs(m-f))},c=ee(o).groups.map(s),i=(l,f)=>l.some(m=>m>0&&m<eh)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(c.some(i)){let l=o.map(u=>{let p=ee([u]);return{d:s(p.groups[0]),k:p.k}}),f=Math.max(...l.map(u=>u.d.some(p=>p>0)?Math.floor(Math.log2(Math.max(...u.d)))+u.k:-1/0)),m=Number.isFinite(f)?f:0;c=l.map(u=>u.d.map(p=>q(p,u.k-m)))}let a=fc(tn(c));return{colNames:["Df","F value","Pr(>F)"],rowNames:["group",""],table:[[a.df[0],a.fValue,a.pValue],[a.df[1],NaN,NaN]],warnings:[]}}var nh=["score"],rh=["alternative","exact","variant","levels"];function oh(e,t,...n){V(Mn(n),nh,"propTrendTest");let{score:r}=Nn(["score"],n),o=Array.from(e),s=Array.from(t),c=o.length,i=r===void 0?o.map((M,v)=>v+1):Array.from(r);if(s.length!==c||i.length!==c)throw new RangeError("'x', 'n' and 'score' must have the same length");if(s.some(M=>!(M>0&&M<1/0)))throw new RangeError("elements of 'n' must be positive and finite");if(o.some((M,v)=>!(M>=0&&M<=s[v])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(M=>M===0)||o.every((M,v)=>M===s[v]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let a=s.reduce((M,v)=>M+v,0),l=o.reduce((M,v)=>M+v,0)/a,f=s.map(M=>M/(l*(1-l))),m=o.map((M,v)=>M/s[v]),u=i.flatMap((M,v)=>Number.isNaN(M)?[]:[v]);if(u.length===0)throw new RangeError("no non-missing scores");if(u.some(M=>!Number.isFinite(i[M])))throw new RangeError("non-missing scores must be finite");if(u.every(M=>i[M]===i[u[0]]))throw new RangeError("at least 2 distinct non-missing scores are needed");let p=i,{sw:g,sxy:w,sxx:h}=Ys(f,m,p,u);if(!(h>=2**-1022&&h<1/0&&Math.abs(w)<1/0)){let M=ee([u.map(v=>i[v])]);M.k!==0&&(p=i.slice(),u.forEach((v,b)=>p[v]=M.groups[0][b]),{sw:g,sxy:w,sxx:h}=Ys(f,m,p,u))}let N=Oe(w,h);if(!(a<1/0)||l>0&&l<1&&!(g<1/0&&Math.abs(w)<1/0&&h<1/0)){let{groups:[M,v],k:b}=ee([o,s]),d=M.reduce((x,y)=>x+y,0)/v.reduce((x,y)=>x+y,0),E=Ys(v,m,p,u);N=q(E.sxy/d*(E.sxy/E.sxx/(1-d)),b)}return{method:"Chi-squared Test for Trend in Proportions",statistic:N,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:xe(N,.5,2,!1,!1),warnings:[]}}function Ys(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,c=r.reduce((l,f)=>l+e[f]*t[f],0)/o,i=0,a=0;for(let l of r){let f=n[l]-s;i+=e[l]*f*(t[l]-c),a+=e[l]*f*f}return{sw:o,sxy:i,sxx:a}}var sh=2**24,ah=2**32,ih=`Sample size > 100 or data with ties
|
|
11
|
-
p-value based on normal approximation. Specify nperm for permutation p-value`,ch=`Sample data with ties
|
|
12
|
-
p-value based on normal approximation. Specify nperm for permutation p-value.`;function lh(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)),c=o.flatMap((f,m)=>s(f)||s(t[m])?[]:[m]);n===void 0&&(t=Fe(t));let i;if(n!==void 0)i=n.map(Pe);else{let f=[...new Set(c.map(m=>t[m]))];i=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(Pe):[...new Set(f.map(Pe))].sort(it)}let a=i.map(()=>[]),l=new Map(i.map((f,m)=>[f,m]));for(let f of c){let m=l.get(Pe(t[f]));if(m===void 0)throw new RangeError(`the group '${Pe(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function fh(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),c=(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,i=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 c+i+a}function mh(e,t,n={}){V(n,rh,"jonckheereTest");let r=be(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=lh(e,t,n.levels,o==="clinfun");if(s.filter(M=>M.length>0).length<2)throw new RangeError(o==="clinfun"?"only one group has non-missing data":"all observations are in the same group");let c=s.flat(),i=c.length,a=new Set(c).size<i,l=0,f=[],m=i;s.forEach((M,v)=>{m-=M.length,f.push([M.length,m]);for(let b=v+1;b<s.length;b++)for(let d of M)for(let E of s[b])l+=d<E?1:d===E?.5:0});let u=[],p=!a&&i<=100;o==="DescTools"&&n.exact!==void 0?(p=n.exact,p&&a&&(p=!1,u.push(ch))):p||u.push(ih);let g,w;if(p){let M=1,v=0;for(let[d,E]of f)d===0||E===0||(v+=M*(d*E+1),M+=d*E);if(M>sh||v>ah)throw new RangeError(`the exact distribution of JT has ${M} values and needs ${v} steps, too many: use exact: false`);let b=[1];for(let[d,E]of f){if(d===0||E===0)continue;let x=Array.from({length:d*E+1},(R,T)=>Ra(T,d,E,!1)),y=new Array(b.length+x.length-1).fill(0);b.forEach((R,T)=>x.forEach((k,S)=>y[T+S]+=R*k)),b=y}g=0,w=0,b.forEach((d,E)=>{E<=l&&(g+=d),E>=l&&(w+=d)})}else{let M=f.reduce((d,[E,x])=>d+E*x,0)/2,v=a?fh(s.map(d=>d.length),c):f.reduce((d,[E,x])=>d+E*x*(E+x+1)/12,0),b=(l-M)/Math.sqrt(v);g=U(b),w=1-g,w<1e-8&&(w=U(b,{lowerTail:!1}))}let N=r==="increasing"?w:r==="decreasing"?g:Math.min(1,2*Math.min(w,g,o==="clinfun"?.5:1));return j("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:N,warnings:u})}var uh=["nClasses","adjust"];function wr(e){return Ye(Array.from(e)).sort((t,n)=>t-n)}function rl(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function Mo(e){rl(e);let t=ga(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function ph(e){let t=wr(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Mo(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(U(r[a-1],{logP:!0})+U(r[n-a],{lowerTail:!1,logP:!0}));let s=-n-o/n,c=(1+.75/n+2.25/n**2)*s,i;return c<.2?i=1-Math.exp(-13.436+101.14*c-223.73*c**2):c<.34?i=1-Math.exp(-8.318+42.796*c-59.938*c**2):c<.6?i=Math.exp(.9177-4.279*c-1.38*c**2):c<10?i=Math.exp(1.2937-5.709*c+.0186*c**2):i=37e-25,{method:"Anderson-Darling normality test",statistic:s,statisticName:"A",pValue:i,warnings:[]}}function hh(e){let t=wr(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Mo(t),o=1/(12*n);r.forEach((a,l)=>{o+=(U(a)-(2*(l+1)-1)/(2*n))**2});let s=(1+.5/n)*o,c=[],i;return s<.0275?i=1-Math.exp(-13.953+775.5*s-12542.61*s**2):s<.051?i=1-Math.exp(-5.903+179.546*s-1515.29*s**2):s<.092?i=Math.exp(.886-31.62*s+10.897*s**2):s<1.1?i=Math.exp(1.111-34.242*s+12.832*s**2):(i=737e-12,c.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),j("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:i,warnings:c})}function dh(e){let t=wr(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=Mo(t).map(f=>U(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 c=Math.max(o,s),i=n<=100?c:c*(n/100)**.49,a=n<=100?n:100,l=Math.exp(-7.01256*i**2*(a+2.78019)+2.99587*i*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))*c;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:c,statisticName:"D",pValue:l,warnings:[]}}function gh(e){let t=wr(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(rl(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>Q((u+1-3/8)/(n+1/4))),o=ee([t]).groups[0],s=Math.min(da(o,r)**2/(qe(o)*qe(r)),1),c=Math.log(n),i=Math.log(c),a=-1.2725+1.0521*(i-c),l=1.0308-.26758*(i+2/c),f=U((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function wh(e,t={}){V(t,uh,"pearsonTest");let n=wr(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 c=[];for(let f of Mo(n)){let m=Math.min(o,Math.floor(1+o*U(f)));m>=1&&c.push(m)}c.sort((f,m)=>f-m);let i=r/o,a=0;for(let f=1,m=0;f<=o;f++){let u=0;for(;m<c.length&&c[m]===f;)u++,m++;a=a+(u-i)**2/i}let l=o-1-(s?2:0);return{method:"Pearson chi-square normality test",statistic:a,statisticName:"P",pValue:xe(a,l/2,2,!1,!1),warnings:[]}}var bh=["weight"],vh=["exact"],Nh=["model","type","unit","r0","confLevel"];function br(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 yo(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 sl=e=>2*U(-Math.abs(e));function Mh(e,t={}){V(t,bh,"kappa2");let n=be(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=>vn(x[0])),c=r.map(x=>vn(x[1])),i=[...new Set([...s,...c])].sort((x,y)=>x<y?-1:x>y?1:0),a=i.length,l=new Map(i.map((x,y)=>[x,y])),f=Array.from({length:a},()=>new Array(a).fill(0));for(let x=0;x<o;x++)f[l.get(s[x])][l.get(c[x])]++;let m=x=>n==="unweighted"?x===0?1:0:n==="equal"?1-x/(a-1):1-x*x/((a-1)*(a-1)),u=Array.from({length:a},(x,y)=>Array.from({length:a},(R,T)=>m(Math.abs(y-T)))),p=f.map(x=>se(x)/o),g=Array.from({length:a},(x,y)=>se(f.map(R=>R[y]))/o),w=x=>{let y=[];for(let R=0;R<a;R++)for(let T=0;T<a;T++)y.push(x(R,T));return se(y)},h=w((x,y)=>f[x][y]*u[x][y])/o,N=w((x,y)=>p[x]*g[y]*u[x][y]),M=(h-N)/(1-N),v=Array.from({length:a},(x,y)=>se(g.map((R,T)=>R*u[y][T]))),b=Array.from({length:a},(x,y)=>se(p.map((R,T)=>R*u[T][y]))),d=(w((x,y)=>p[x]*g[y]*(u[x][y]-(v[x]+b[y]))**2)-N*N)/(o*(1-N)**2),E=M/Math.sqrt(d);return{value:M,statistic:E,pValue:sl(E),warnings:[]}}function yh(e,t={}){V(t,vh,"kappamFleiss");let n=t.exact??!1,r=br(e,"ratings");yo(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 c=e.flat(),i=Fe(c),a=[...new Set(i)].sort((d,E)=>d-E),l=new Map(a.map((d,E)=>[d,E])),f=new Map(c.map((d,E)=>[d,l.get(i[E])])),m=a.length,u=e.map(d=>{let E=new Array(m).fill(0);for(let x of d)E[f.get(x)]++;return E}),p=se(u.map(d=>(se(d.map(E=>E*E))-s)/(s*(s-1))))/o,g=Array.from({length:m},(d,E)=>se(u.map(x=>x[E]))/(o*s)),w=se(g.map(d=>d*d));if(n){let d=r.map(x=>{let y=new Array(m).fill(0);for(let R of x)y[f.get(R)]++;return y.map(R=>R/o)}),E=Array.from({length:m},(x,y)=>qe(d.map(R=>R[y]))*(s-1)/s);w-=se(E)/(s-1)}let h=(p-w)/(1-w);if(n)return{value:h,warnings:[]};let N=se(g.map(d=>d*(1-d))),M=se(g.map(d=>d*(1-d)*(1-d-d))),v=2/(N*N*o*s*(s-1))*(N*N-M),b=h/Math.sqrt(v);return{value:h,statistic:b,pValue:sl(b),warnings:[]}}function Eh(e,t={}){V(t,Nh,"icc");let n=be(t.model,["oneway","twoway"]),r=be(t.type,["consistency","agreement"]),o=be(t.unit,["single","average"]),s=t.r0??0,c=t.confLevel??.95;Qe(c),br(e,"ratings"),yo(e,"ratings");let i=ee(e).groups,a=br(i,"ratings"),l=i.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-c,u=qe(i.map(F=>Ae(F)))*f,p=qe(a.map(F=>Ae(F)))*l,g=Ae(i.map(F=>qe(F))),w=qe(i.flat())*(l*f-1),h=w-u*(l-1)-p*(f-1),N=h>2**-20*w?h/((l-1)*(f-1)):kh(i)/((l-1)*(f-1)),M=(F,L,z)=>we(F,L,z,!1,!1),v=(F,L)=>m/2<1e-8?At(m/2,F,L,!1,!1):At(1-m/2,F,L,!0,!1),b=l-1;if(n==="oneway"||r==="consistency"){let F=n==="oneway"?g:N,L=n==="oneway"?l*(f-1):(l-1)*(f-1),z=u/F,X=z/v(b,L),te=z*v(L,b),[ne,ae,ye]=o==="single"?[(u-F)/(u+(f-1)*F),z*(1-s)/(1+(f-1)*s),_e=>(_e-1)/(_e+f-1)]:[(u-F)/u,z*(1-s),_e=>1-1/_e];return{value:ne,Fvalue:ae,fValue:ae,df1:b,df2:L,pValue:M(ae,b,L),lbound:ye(X),ubound:ye(te),warnings:[]}}let d=o==="single"?f:1,E=F=>[d*F/(l*(1-F)),1+d*F*(l-1)/(l*(1-F))],x=(F,L)=>wa([F*p,L*N],[f-1,(l-1)*(f-1)]),y=o==="single"?(u-N)/(u+(f-1)*N+f/l*(p-N)):(u-N)/(u+(p-N)/l),[R,T]=E(s),k=u/(R*p+T*N),S=x(R,T),[A,I]=[f*y/(l*(1-y)),1+f*y*(l-1)/(l*(1-y))],_=x(A,I),$=v(b,_),P=v(_,b),[C,W]=o==="single"?[l*(u-$*N)/($*(f*p+(f*l-f-l)*N)+l*u),l*(P*u-N)/(f*p+(f*l-f-l)*N+l*P*u)]:[l*(u-$*N)/($*(p-N)+l*u),l*(P*u-N)/(p-N+l*P*u)];return{value:y,Fvalue:k,fValue:k,df1:b,df2:S,pValue:M(k,b,S),lbound:C,ubound:W,warnings:[]}}function ol(e,t){let n=t.length,r=se(t.flatMap(s=>t.map(c=>e[s][c])));return(1-se(t.map(s=>e[s][s]))/r)*n/(n-1)}function xh(e){let t=br(e,"x").length;if(yo(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=jr(ee(e).groups),r=nr(e),o=Array.from({length:t},(i,a)=>a),s=se(r.flat()),c;try{let i=Ce(r);c=o.map(a=>1-1/i[a][a])}catch(i){if(!r.flat().every(Number.isFinite))throw i;let a=ee(e).groups;c=o.map(l=>{let f=a.map(p=>p[l]),m=En(Ne(a.map(p=>[1,...p.filter((g,w)=>w!==l)])),f),u=Ae(f);return 1-se(m.map(p=>p*p))/se(f.map(p=>(p-u)**2))})}return j("cronbachAlpha",{rawAlpha:ol(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-se(c))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(i=>ol(n,o.filter(a=>a!==i))),warnings:Rh(e,r)})}function Rh(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}=ct(r.map(i=>r.map(a=>t[i][a])),{symmetric:!0}),s=o.map(i=>i[0]),c=se(s)<0?-1:1;s.some(i=>c*i<0)&&n.push(`Some items were negatively correlated with the first principal component and probably
|
|
5
|
+
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],p=[...s.groups[0],...s.groups[m]],w=Ne(p),g=0;for(let x=0;x<i;x++)g+=w[x];let d=i+u,N=d*(d+1)/2-g,b=i*u+i*(i+1)/2-g,v=i*u+u*(u+1)/2-N,y=0;for(let x of ut(p))y+=x*x*x-x;let h=i*u/(d*(d-1))*((d*d*d-d)/12-y/12),E=(Math.min(b,v)-i*u/2)/Math.sqrt(h);return g<N?-E:E}),l=c.map(m=>s.n[m]/(s.n[m]+i)),f=Fi(a,l,r,o);return j("manyOneUTest",ns(s,`Wilcoxon, Mann, Whittney U-test
|
|
6
|
+
for multiple comparisons with one control`,a,f,o))}function em(e,t){let n=At(e,t),r=or(n.k,(s,i)=>{let c=n.n[s],a=n.n[i],l=[...n.groups[s],...n.groups[i]],f=Ne(l),m=0;for(let N=0;N<c;N++)m+=f[N];let u=m-c*(c+1)/2,p=Math.min(u,c*a-u),w=c+a,g=0;for(let N of ut(l))g+=N*N*N-N;let d=c*a/(w*(w-1))*((w*w*w-w)/12-g/12);return Math.SQRT2*(p-c*a/2)/Math.sqrt(d)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:rs(i,n.k,1/0)));return j("dscfAllPairsTest",Qr(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function tm(e,t){let n=At(e,t),r=_e(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),i=or(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),c=or(n.k,(a,l)=>{let f=ba([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return rs(i[a-1][l],n.k,f)});return j("gamesHowellTest",Qr(n,"Games-Howell test",i,c,"none"))}function nm(e,t){let n=At(e,t),{m:r,s2:o,df:s}=os(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let i=or(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:rs(l,n.k,s)));return j("tukeyTest",Qr(n,"Tukey's test",i,c,"single-step"))}function rm(e,t,n={}){z(n,Gf,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=At(e,t),i=Xr(e,t).statistic,{R:c,C:a}=ts(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let w=1;w<s.k;w++)for(let g=0;g<w;g++)f.push((c[g]-c[w])/Math.sqrt(l*(1/s.n[g]+1/s.n[w]))),m.push(`${s.labels[g]} - ${s.labels[w]}`);let u=f.map(w=>(o?2:1)*Br(Math.abs(w))),p=om(u,r);return{chi2:i,Z:f,P:u,PAdjusted:p,zValue:f,pValue:u,pAdjusted:p,comparisons:m,warnings:[]}}function om(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 sm(e,t,n={}){z(n,Hf,"glhtDunnett");let r=fe(n.alternative),o=At(e,t),{m:s,s2:i,df:c,k:a}=os(o),l=Math.sqrt(i),f=o.n.slice(1).map((b,v)=>v+1),m=f.map(b=>s[b]-s[0]),u=f.map(b=>l*Math.sqrt(1/o.n[b]+1/o.n[0])),p=m.map((b,v)=>b/u[v]),w=m.map(b=>I(b,a)),g=u.map(b=>I(b,a)),d=f.map(b=>o.n[b]/(o.n[b]+o.n[0])),N=p.map(b=>r==="two.sided"?on(b,d,c,!0):on(r==="greater"?b:-b,d,c,!1));return{names:f.map(b=>`${o.labels[b]} - ${o.labels[0]}`),estimate:w,se:g,t:p,p:N,stdError:g,tValue:p,pValue:N,df:c,warnings:[]}}var Pi=["varEqual"],am=2**-1022;function Di(e,t,n={}){if(typeof e!="string"&&De(t)&&z(t,Pi,"onewayTest"),z(n,Pi,"onewayTest"),typeof e=="string"){let d=qa(e,t);return Di(d.values,d.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=nn(e,r),s=_e(o),i=s.groups,c=n.varEqual??!1,a=i.length;if(a<2)throw new RangeError("not enough groups");let l=i.map(d=>d.length);if(l.some(d=>d<2))throw new RangeError("not enough observations");let f=s.mean,m=s.variance,u=l.reduce((d,N)=>d+N,0),p,w,g;if(c){let d=s.grandMean,N=0,b=0;for(let v=0;v<a;v++)N+=l[v]*(f[v]-d)**2,b+=(l[v]-1)*m[v];w=u-a,sr(w,s.ssWithin),p=N/(a-1)/(b/w)}else if(m.some(d=>!(d>=am))&&(g=cm(o))!==void 0){let d=g.map((M,T)=>Ci(l[T]/M.variance,-2*M.k)),N=Math.max(...d.map(M=>M.e)),b=d.map(M=>Ce(M.m,M.e-N)),v=b.reduce((M,T)=>M+T,0),y=0;for(let M=0;M<a;M++){let T=b.reduce((R,S,k)=>k===M?R:R+S,0);y+=(T/v)**2/(l[M]-1)}y/=a*a-1;let h=[];for(let M=0;M<a;M++)for(let T=M+1;T<a;T++){let[R,S]=g[M].k>=g[T].k?[g[M],g[T]]:[g[T],g[M]],k=R.mean-Ce(S.mean,S.k-R.k);if(k===0)continue;let A=Ci(k,R.k);h.push({m:d[M].m*d[T].m*A.m*A.m/v,e:d[M].e+d[T].e+2*A.e-N})}let E=Math.max(...h.map(M=>M.e)),x=h.reduce((M,T)=>M+Ce(T.m,T.e-E),0);p=(h.length===0?0:Ce(x,E))/((a-1)*(1+2*(a-2)*y)),w=1/(3*y)}else if(m.some(d=>d===0)){let d=m.flatMap((y,h)=>y===0?[h]:[]);if(d.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let N=d[0],b=0,v=0;for(let y=0;y<a;y++)y!==N&&(b+=1/(l[y]-1),v+=l[y]/m[y]*(f[y]-f[N])**2);b/=a*a-1,p=v/((a-1)*(1+2*(a-2)*b)),w=1/(3*b)}else{let d=l.map((h,E)=>h/m[E]),N=d.reduce((h,E)=>h+E,0),b=0,v=0;for(let h=0;h<a;h++)b+=(1-d[h]/N)**2/(l[h]-1),v+=d[h]*f[h];b/=a*a-1,v/=N;let y=0;for(let h=0;h<a;h++)y+=d[h]*(f[h]-v)**2;p=y/((a-1)*(1+2*(a-2)*b)),w=1/(3*b)}return{method:c?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:p,statisticName:"F",parameter:[a-1,w],parameterName:["num df","denom df"],pValue:we(p,a-1,w,!1,!1),warnings:[]}}function im(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=Si(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:ki(r,n),warnings:[]}}function cm(e){let t=[];for(let n of e){let r=_e([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function Ci(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=Ce(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function ss(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=Q(r,[e[o],0]);return r[0]}function lm([[e,t],[n,r]]){let o=e+n,s=t+r,i=e+t,c=Math.max(0,i-s),a=Math.min(i,o),l=new Float64Array(a-c+1);for(let f=c;f<=a;f++)l[f-c]=gn(f,o,s,i,!1);return{lo:c,d:l}}function fm(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 Zr=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=lm(o);n+=s.lo,r=fm(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=ss(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=Q(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?ss(o,s,o.length):ss(o,0,s+1)}};function as(e,t){return $r(e,t)}function Vi(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:as(r=>e.mean(r)-t,n>t)}function is(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:as(o=>n-e.cdf(t,o,!1),r<n)}function cs(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:as(o=>e.cdf(t,o,!0)-n,r>n)}var mm=["alternative","correct","exact","confLevel"];function um(e,t={}){z(t,mm,"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(ls);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=fe(t.alternative),c=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(Qe(l),r===2&&o===2){if(a&&e.some(m=>m.some(u=>u.some(p=>!Number.isInteger(p)))))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?dm(f,i,l):pm(f,s,i,c,l)}return gm(e,s,r,o)}function ls(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Wi(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 ji(e,t){return e.map(n=>n.map(r=>r.map(o=>I(o,-t))))}function zi(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],[p,w]],g)=>{let d=t[g],N=m+u,b=p+w,v=m+p,y=u+w;r+=m;let h=N*v/d;o+=Number.isFinite(h)?h:N/d*v;let E=N*b*v*y/(d*d*(d-n));s+=Number.isFinite(E)?E:N/d*(b/d)*(v/(d-n))*y;let x=(m+w)/d,M=(u+p)/d,T=m*w/d,R=u*p/d,S=Number.isFinite(T)?T:m/d*w,k=Number.isFinite(R)?R:u/d*p;i+=S,c+=k,a+=x*S,l+=x*k+M*S,f+=M*k}),{sa:r,se:o,sv:s,sR:i,sS:c,sPR:a,sPSQR:l,sQS:f}}function pm(e,t,n,r,o){let s=zi(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=Wi(e);let x=ji(e,i);s=zi(x,x.map(ls),I(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:p,sQS:w}=s,g=c-a,d=I(.5,-i),N=r&&Math.abs(g)>=d?d:0,b=I(Oe(Math.abs(g)-N,l),i),v=n==="two.sided"?xe(b,.5,2,!1,!1):Y(Math.sign(g)*Math.sqrt(b),{lowerTail:n==="less"}),y=f/m,h=I(Math.sqrt(u/(2*f*f)+p/(2*f*m)+w/(2*m*m)),-i/2),E;if(n==="less")E=[0,y*Math.exp(Z(o)*h)];else if(n==="greater")E=[y*Math.exp(Z(o,{lowerTail:!1})*h),1/0];else{let x=(1-o)/2,M=x<1e-8?Z(x,{lowerTail:!1}):Z((1+o)/2);E=[y*Math.exp(-M*h),y*Math.exp(M*h)]}return{method:`Mantel-Haenszel chi-squared test ${N!==0?"with":"without"} continuity correction`,alternative:n,statistic:b,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:v,confInt:E,confLevel:o,estimate:y,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var hm=2**24;function dm(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>hm)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new Zr(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=Q(f,[m,0]));i=Math.min(1,f[0])}else i=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,is(o,s,1-n)];else if(t==="greater")c=[cs(o,s,1-n),1/0];else{let a=(1-n)/2;c=[cs(o,s,a),is(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:Vi(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function gm(e,t,n,r){let{diff:o,V:s}=$i(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=Wi(e);let f=ji(e,i);({diff:o,V:s}=$i(f,f.map(ls),n,r,I(1,-i)))}let c=o.length,a=Ve(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*a[f];return l=I(l,i),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:xe(l,c/2,2,!1,!1),warnings:[]}}var wm=2**-1022;function $i(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(w=>w.reduce((g,d)=>g+d,0)),u=Array.from({length:r},(w,g)=>a.reduce((d,N)=>d+N[g],0)),p=1/(f*f*(f-o));for(let w=0;w<r-1;w++)for(let g=0;g<n-1;g++){let d=g+(n-1)*w,N=m[g]*u[w]/f;i[d]+=a[g][w]-(Number.isFinite(N)?N:kr(m[g],u[w],f));for(let b=0;b<r-1;b++)for(let v=0;v<n-1;v++){let y=(g===v?f*m[g]:0)-m[g]*m[v],h=(w===b?f*u[w]:0)-u[w]*u[b],E=p*y*h;c[d][v+(n-1)*b]+=Number.isFinite(E)&&p>=wm?E:m[g]*((g===v?1:0)-m[v]/f)*(((w===b?1:0)-u[b]/f)*(u[w]/(f-o)))}}}),{diff:i,V:c}}var Ki=new Map,bm=2**24,Nm=2**32;function Yi(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>bm||n*r>Nm)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function vm(e,t){let n=`${e},${t}`,r=Ki.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,p)=>u+p,0),c=Array.from({length:e+1},()=>new Float64Array(i+1));c[0][0]=1;for(let u=0;u<o;u++){let p=o-u,w=s[u],g=Array.from({length:e+1},()=>new Float64Array(i+1));for(let d=0;d<=e;d++){let N=c[d],b=e-d;if(b>p)continue;let v=b/p;for(let y=0;y<=i;y++){let h=N[y];h!==0&&(b>0&&(g[d+1][y+w]+=h*v),b<p&&(g[d][y]+=h*(1-v)))}}c=g}let a=c[e],l=0;for(;l<a.length&&a[l]===0;)l++;let f=a.length-1;for(;f>l&&a[f]===0;)f--;let m={min:l,probs:a.slice(l,f+1)};return Ki.set(n,m),m}function fs(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}=vm(t,n),i=ca(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 Ui=new Map;function ym(e,t){let n=`${e},${t}`,r=Ui.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),p=Array.from({length:e+1},()=>new Float64Array(s+1));for(let w=0;w<=e;w++){if(e-w>o-m)continue;let g=i[w];for(let d=0;d<=s;d++){let N=g[d];N!==0&&(w<e&&(p[w+1][d+u]+=N),e-w<o-m&&(p[w][d]+=N))}}i=p}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 Ui.set(n,f),f}function Jr(e,t,n){let{min:r,counts:o}=ym(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=Na(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 Mm=["alternative","exact","confInt","confLevel"];function Bi(e,t,n={}){if(z(n,Mm,"ansariTest"),typeof e=="string"){let h=Dt(e,t);return Bi(h.x,h.y,n)}let r=Array.from(e).filter(h=>!Number.isNaN(h)),o=Array.from(t).filter(h=>!Number.isNaN(h)),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=fe(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&Qe(l);let f=s+i,m=Ne(r.concat(o)),u=m.map(h=>Math.min(h,f-h+1)),p=0;for(let h=0;h<s;h++)p+=u[h];let w=new Set(m).size<f,g=n.exact??(s<50&&i<50&&!w),d=n.exact??(s<50&&i<50),N=new Re,b,v;if(g&&!w){Yi(s,i);let h=x=>fs(x-1,s,i,!1),E=x=>fs(x,s,i,!0);if(c==="two.sided"){let x=Math.floor((s+1)**2/4)+Math.floor(s*i/2)/2;b=Math.min(1,2*(p>x?h(p):E(p)))}else c==="less"?b=h(p):b=E(p);a&&(v=Rm(r,o,c,l,N))}else{let h=Qi(p,u,w,s,i);b=c==="two.sided"?2*Math.min(Y(h),Y(h,{lowerTail:!1})):c==="less"?Y(h,{lowerTail:!1}):Y(h),a&&(v=Sm(r,o,c,l,d?new Re:N)),d&&w&&(N.add("cannot compute exact p-value with ties"),a&&N.add("cannot compute exact confidence interval with ties"))}let y={method:"Ansari-Bradley test",alternative:c,statistic:p,statisticName:"AB",pValue:b,warnings:N.list};return v!==void 0&&(y.confInt=v.confInt,y.confLevel=l,y.estimate=v.estimate,y.estimateNames="ratio of scales"),y.nullValue=1,y.nullValueName="ratio of scales",j("ansariTest",y)}function Qi(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 Zi(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)<Em))?xm(e.map(f=>Gi(f,n)).concat(t.map(f=>Gi(f,1)))):Tm(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 Em=2**-1022;function Gi(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]=Hi(Math.abs(e)),[s,i]=Hi(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function Hi(e){let t=Math.floor(Math.log2(e)),n=I(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Xi(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 xm(e){let t=e.map((r,o)=>o).sort((r,o)=>Xi(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Xi(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 Tm(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return Ne(e);let n=Ne(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function eo(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Rm(e,t,n,r,o){let s=e.length,i=t.length,c=[];for(let N of e)for(let b of t)N/b>=0&&c.push(N/b);c.sort((N,b)=>N-b);let a=c.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:eo(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((N,b)=>Zi(e,t,N+(b<a-1?(c[b+1]-N)/2:N*1.01)).ab),f=(N,b,v)=>{let y=1/0;for(let E of l)(b?E-N>0:E-N>=0)&&E-N<y&&(y=E-N);let h=v?-1/0:1/0;return l.forEach((E,x)=>{E-N===y&&(h=v?Math.max(h,c[x]):Math.min(h,c[x]))}),h},m=N=>{let b=Jr(N/2,s,i),v=Jr(1-N/2,s,i);if(!l.some(E=>E-b>=0)||!l.some(E=>E-v>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let y=f(b,!1,!1),h=f(v,!0,!0);return y>h?[f(v,!1,!1),f(b,!0,!0)]:[y,h]},u=eo(n,1-r,m),p=Jr(.5,s,i),w=-1/0,g=1/0;l.forEach((N,b)=>{N-p<=0?w=Math.max(w,c[b]):g=Math.min(g,c[b])});let d=w===-1/0||g===1/0?NaN:(g+w)/2;return d===1/0&&(d=g/2+w/2),{confInt:u,estimate:d}}function Sm(e,t,n,r,o){let s=e.length,i=t.length,c=(N,b)=>{let{ab:v,a:y,ties:h}=Zi(e,t,N);return Qi(v,y,h,s,i)-b},a=[],l=(N,b)=>{N.length===0||b.length===0||a.push(et(N)/ge(b),ge(N)/et(b))};if(l(e.filter(N=>N>0),t.filter(N=>N>0)),l(e.filter(N=>N<=0),t.filter(N=>N<0)),a.some(N=>!Number.isFinite(N))){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:eo(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=et(a),m=ge(a),u=N=>wt(b=>c(b,N),f,m,{tol:1e-4}),p=N=>{let b=Z(N/2),v=Z(N/2,{lowerTail:!1});if(c(f,b)>0||c(m,v)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let y=u(b),h=u(v);return y<=h?[y,h]:[h,y]},w=eo(n,1-r,p),g=c(f,0)>0||c(m,0)<0;g&&o.add("cannot compute estimate, returning NA");let d=g?NaN:u(0);return{confInt:w,estimate:d}}var km=["alternative","confLevel"];function Am(e,t,n={}){z(n,km,"brunnerMunzelTest");let r=Array.from(e).filter(M=>!Number.isNaN(M)),o=Array.from(t).filter(M=>!Number.isNaN(M)),s=r.length,i=o.length;if(s<2)throw new RangeError("not enough 'x' observations");if(i<2)throw new RangeError("not enough 'y' observations");let c=fe(n.alternative),a=n.confLevel??.95;Vt(a);let l=Ne(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=M=>M.reduce((T,R)=>T+R,0)/M.length,p=u(f),w=u(m),g=(M,T,R)=>{let S=(M.length+1)/2,k=0;for(let A=0;A<M.length;A++)k+=(M[A]-T[A]-R+S)**2;return k/(M.length-1)},d=g(f,Ne(r),p),N=g(m,Ne(o),w),b=(w-(i+1)/2)/s,v=s*d+i*N,y=s*i*(w-p)/(s+i)/Math.sqrt(v),h=v**2/((s*d)**2/(s-1)+(i*N)**2/(i-1)),E;c==="greater"?E=Ke(y,h,!0,!1):c==="less"?E=Ke(y,h,!1,!1):E=2*Ke(-Math.abs(y),h,!0,!1);let x=gt((1-a)/2,h,!1,!1)*Math.sqrt(d/(s*i**2)+N/(i*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:y,statisticName:"Brunner-Munzel Test Statistic",parameter:h,parameterName:"df",pValue:E,confInt:[b-x,b+x],confLevel:a,estimate:b,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var qm=[1.2337005501361697,7831619385924639e-32];function _m(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let p=0;p<t;p++)for(let w=0;w<t;w++){let g=f[p*t+w];if(g!==0)for(let d=0;d<t;d++)u[p*t+d]+=g*m[w*t+d]}return u},o=f=>{let m=0;for(let w of f)m=Math.max(m,Math.abs(w));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let p=2**-u;for(let w=0;w<f.length;w++)f[w]*=p;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 Im=2**33;function ec(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>Im)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 tc(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=1/(2*e))return 0;if(t>=1)return 1;if(e*t<1){let[f,m]=Ht(e,t),u=2*f-1+2*m,p=1,w=0;for(let g=1;g<=e;g++)p*=g/e*u,p<2**-500&&(p*=2**500,w-=500);for(;w<-1e3;)p*=2**-1e3,w+=1e3;return p*2**w}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 p=1;for(let w=2;w<=u;w++)p*=w;s[f*r+m]/=p}}let[i,c]=_m(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 ms(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=ke(qm,Ht(e,e)),s=o[0]+o[1],i=Math.exp(-o[0])*(1-o[1]),c=0;for(let l=2;l<100;l++){let f=Math.exp(-(2*l-1)*(2*l-1)*s);if(c+=f,f<=1e-17*(c+i))break}if(i+c===0)return t?0:1;let a=Math.sqrt(2*Math.PI)/e*(i+c);return t?a:1-a}let n=0;for(let o=1;o<100;o++){let s=Math.exp(-2*o*o*e*e);if(n+=o%2===1?s:-s,s<=1e-17*Math.abs(n))break}let r=2*n;return t?1-r:r}function us(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=0)return 1;if(t>1)return 0;let[n,r]=Ht(e,t),o=Math.floor(e-n-r),s=0;for(let i=0;i<=o;i++){let c=Q(Tt(e-i,-n),[-r,0]);if(c[0]<=0)break;let a=Q(Tt(i,n),[r,0]),l=ke(a,[e,0]),f=ke(c,[e,0]);s+=ia(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var Ji=2**28;function nc(e,t){let[n,r]=Ht(e,t),o=Math.floor(n);o===n&&r<0&&(o-=1);let s=[];for(let p=0;p<=e;p++){let w=(p-n-r)/e;w>0&&w<1&&s.push({t:w,up:!0,i:p});let g=(p+n+r)/e;g>0&&g<1&&s.push({t:g,up:!1,i:p})}if(s.length*(2*o+1)*Math.min(e+1,100)>Ji)return NaN;s.sort((p,w)=>p.t-w.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 p of s){let w=(p.t-f)/(1-f);if(w>0){let g=w/(1-w);a.fill(0);for(let d=l;d<i;d++){let N=c[d];if(N===0)continue;let b=e-d,v=Math.min(b,Math.floor((b+1)*w)),y=i-d,h=Bt(v,b,w,!1);for(let E=v;E<=b&&h!==0;E++)E>v&&(h=h*(b-E+1)*g/E),E>=y?m+=N*h:a[d+E]+=N*h,u++;h=v>0?Bt(v-1,b,w,!1):0;for(let E=v-1;E>=0&&h!==0;E--)E<v-1&&(h=h*(E+1)/((b-E)*g)),E>=y?m+=N*h:a[d+E]+=N*h,u++}if(u>2*Ji)return NaN;[c,a]=[a,c]}if(f=p.t,p.up)i=p.i+1;else{for(let g=l;g<=p.i;g++)m+=c[g],c[g]=0;l=Math.max(l,p.i+1)}}return Math.min(m,1)}function rc(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((p,w)=>p-w);i=new Array(s+1).fill(!1);for(let p=1;p<s;p++)i[p]=u[p]!==u[p-1]}let c=e*t*n*(1-1e-10),a=(u,p)=>{let w=u*n-p*t;return(o==="two.sided"?Math.abs(w):o==="greater"?w:-w)>=c},l=new Float64Array(n+1),f=new Float64Array(n+1),m=0;for(let u=0;u<=t;u++){for(let p=0;p<=n;p++){if(u===0&&p===0){f[0]=1;continue}let w=u+p,g=(u>0?u/w*l[p]:0)+(p>0?p/w*f[p-1]:0);(i===void 0?w<s:i[w])&&a(u,p)?(g>0&&(m+=g*gn(u,t,n,w,!1)),f[p]=0):f[p]=g}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var Lm=["alternative","exact"];function oc(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 sc(e,t,n={}){if(z(n,Lm,"ksTest"),typeof e=="string"){let b=Dt(e,t);return sc(b.x,b.y,n)}let r=Array.from(e).filter(b=>!Number.isNaN(b)).sort((b,v)=>b-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=fe(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let b=-1/0,v=-1/0;for(let M=0;M<o;M++){let T=t(r[M]);if(!(T>=0&&T<=1))throw new RangeError(`the distribution function returned ${T} at ${r[M]}, not a value in [0, 1]`);b=Math.max(b,(M+1)/o-T),v=Math.max(v,T-M/o)}let y=s==="two.sided"?Math.max(b,v):s==="greater"?b:v,h=new Set(r).size<o,E=n.exact??(o<100&&!h),x;if(E&&s==="two.sided"&&y<.5&&ec(o,y),s==="two.sided")if(!E)x=ms(Math.sqrt(o)*y,!1);else{let M=2*us(o,y);if(y>=.5)x=Math.min(1,M);else{let T=M<.01?nc(o,y):NaN;x=Number.isNaN(T)?1-tc(o,y):T}}else x=E?us(o,y):Math.exp(-2*o*y*y);return j("ksTest",{method:`${E?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:oc(s,!0),exact:E,statistic:y,statisticName:i,pValue:Math.min(1,Math.max(0,x)),warnings:h?["ties should not be present for the one-sample Kolmogorov-Smirnov test"]:[]})}let c=Array.from(t).filter(b=>!Number.isNaN(b)),a=c.length;if(a<1)throw new RangeError("not enough 'y' data");let l=r.map(b=>[b,0]).concat(c.map(b=>[b,1]));l.sort((b,v)=>b[0]-v[0]);let f=0,m=0,u=0,p=0;for(let b=0;b<l.length;b++){if(l[b][1]===0?f++:m++,b+1<l.length&&l[b+1][0]===l[b][0])continue;let v=f/o-m/a;u=Math.max(u,v),p=Math.max(p,-v)}let w=s==="two.sided"?Math.max(u,p):s==="greater"?u:p,g=new Set(l.map(b=>b[0])).size<l.length,d=n.exact??o*a<1e4,N;if(d)N=rc(w,o,a,g?{alternative:s,z:l.map(b=>b[0])}:{alternative:s});else{let b=o*a/(o+a);N=s==="two.sided"?ms(Math.sqrt(b)*w,!1):Math.exp(-2*b*w*w)}return j("ksTest",{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:oc(s,!1),exact:d,statistic:w,statisticName:i,pValue:Math.min(1,Math.max(0,N)),warnings:g&&!d?["p-value will be approximate in the presence of ties"]:[]})}var Om=["poolSd","paired","pAdjustMethod","alternative"],Fm=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],Pm=["pAdjustMethod","correct"];function ac(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=kn(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(rr);return{levels:i,groups:i.map(c=>o.get(c))}}function ps(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=Sn(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 hs(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(Pe),colNames:t.slice(0,-1).map(Pe),pAdjustMethod:r,warnings:o}}function Cm(e,t,n={}){z(n,Om,"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=fe(n.alternative),i=n.pAdjustMethod??"holm",{levels:c,groups:a}=ac(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(N=>N.length),p=_e(a),w=p.mean,g=0;for(let N of u)N>1&&(g+=N-1);sr(g,p.ssWithin);let d=Math.sqrt(p.ssWithin/g);f=(N,b)=>{let v=(w[N]-w[b])/(d*Math.sqrt(1/u[N]+1/u[b]));return s==="less"?Ke(v,g,!0,!1):s==="greater"?Ke(v,g,!1,!1):2*Ke(-Math.abs(v),g,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,p)=>Te(()=>Rn(a[u],a[p],{paired:r,alternative:s})).pValue;return hs(m,c,ps(l,f,i),i)}function Dm(e,t,n={}){z(n,Fm,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=ac(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 Re,u=ps(l,(p,w)=>{let g=Te(()=>tr(a[p],a[w],{...s,...o===void 0?{}:{alternative:o}}));f=g.method;for(let d of g.warnings)m.add(d);return g.pValue},i);return j("pairwiseWilcoxTest",hs(f,c,u,i,m.list))}function Vm(e,t,n={}){if(z(n,Pm,"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 Re,l=ps(s,(m,u)=>{let p=Te(()=>Bo([r[m],r[u]],[o[m],o[u]],{correct:c}));for(let w of p.warnings)a.add(w);return p.pValue},i),f=Array.from({length:s},(m,u)=>u+1);return j("pairwisePropTest",hs("Pairwise comparison of proportions",f,l,i,a.list))}var ar=[0,0];function to(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=de(e,Ht(t,t));return Tt(t,n[0]/(2*t))}function no(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:to([e[r],0]))}function An(e,t){return qt(e,t,[]).fit}function qt(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=ar;for(let N=f;N<r;N++)u=Q(u,pe(m[N],m[N]));if(u[0]===0){a.push(ar);continue}let p=to(u),w=m[f][0]<0?[-p[0],-p[1]]:p;m[f]=Q(m[f],w);let g=pe(w,m[f]),d=N=>{let b=ar;for(let y=f;y<r;y++)b=Q(b,pe(m[y],N[y]));let v=ke(b,g);for(let y=f;y<r;y++)N[y]=de(N[y],pe(v,m[y]))};for(let N=f+1;N<e.length;N++)d(s[N]);d(i);for(let N of c)d(N);a.push([-w[0],-w[1]])}let l=new Array(o).fill(ar);for(let f=o-1;f>=0;f--){let m=i[f];for(let u=f+1;u<o;u++)m=de(m,pe(s[u][f],l[u]));l[f]=ke(m,a[f])}return{fit:{qty:i,coef:l},qtv:c}}function qn(e,t=0,n=e.length){let r=ar;for(let o=t;o<n;o++)r=Q(r,pe(e[o],e[o]));return r}var ze=new WeakMap;function oo(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return ee(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function so(e,t){return e.map(n=>{if(n.rss>=zm&&n.rss<1/0)return I(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=I(o,-t);r+=i*c*c}),r})}var zm=2**-1022;function gs(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,I(n,-t)]])}function ao(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function sn(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+I(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=ao(e),s=r(o)),!(I(t,-o)>=1e-24*s)}function ic(e){let t=oo([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+=I(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:so([e],t)[0],k:t}}var cc=(e,t,n)=>we(e,t,n,!1,!1),ws="essentially perfect fit for \u20181\u2019: summary may be unreliable";function bs(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 Ns=new WeakMap;function $m(e){let t=ic(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*I(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function ds(e){let t=$m(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let g=gs(e,o);sn(g,c)&&(c=0,i=i.map(d=>sn(g,d.ss)?{...d,ss:0}:d))}let a=c/r,l=g=>I(g,2*o),f=i.map(({df:g,ss:d})=>{let N=d/g/a;return[g,l(d),l(d/g),N,cc(N,g,r)]});f.push([r,l(c),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],p={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},w=i.map(({df:g,ss:d})=>[g,d]);return w.push([r,c]),ze.set(p,{colNames:["Df","Sum Sq"],rowNames:u,table:w}),p}function Wm(e){let t=e.map(f=>f.dfResidual),n=oo(e),r=so(e,n),o=f=>I(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*I(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],p=r[m-1]-r[m],[w,g]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=b=>b.filter((v,y)=>(w.weights?.[y]??1)!==0),N=u===0||!Vr(d(w.x),d(g.x))?NaN:p/u/i;return N<0&&(N=NaN),[t[m],o(r[m]),u,o(p),N,Number.isNaN(N)?NaN:cc(N,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 ro(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function vs(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=ds(e),a={...c,warnings:ro(e.warnings,c.warnings)},l=ze.get(c);return l!==void 0&&ze.set(a,l),j("anova",a,e)}if(n.some(c=>c.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=(c,a)=>c===a||c!==void 0&&a!==void 0&&c.length===a.length&&c.every((l,f)=>Object.is(l,a[f]));if(n.some(c=>!r(c.rows,e.rows)))throw new Error("models were not all fitted to the same rows of the data");let o=n.filter(c=>c.response===e.response);if(o.some(c=>!r(c.y,e.y)))throw new Error("models were not all fitted to the same response values");if(o.some(c=>!r(c.weights,e.weights)))throw new Error("models were not all fitted with the same weights");let s=[];if(o.length<n.length){let c=[...new Set(n.filter(a=>a.response!==e.response).map(a=>a.response))];s.push(`models with response ${c.map(a=>`\u2018${a}\u2019`).join(", ")} removed because response differs from model 1`)}let i=o.length===1?ds(e):Wm(o);return i.warnings=ro(...n.map(c=>c.warnings),s,i.warnings),j("anova",i,...n)}function ys(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=ro(i.warnings,Ns.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=ze.get(i);return Array.isArray(l)&&ze.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),j("aovSummary",a,e)}if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=e,n=ds(t),r=ic(t),o=bs(t.fittedValues.map(i=>I(i,-r.k)),r.rss,r.rdf),s=ro(t.warnings,n.warnings,o?[ws]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=ze.get(n);return c!==void 0&&ze.set(i,c),j("aovSummary",i,e)}return j("aovSummary",{colNames:n.colNames.slice(0,3),rowNames:n.rowNames.slice(0,-1),table:n.table.slice(0,-1).map(i=>i.slice(0,3)),warnings:s},e)}var jm=1e-14;function Km(e,t,n,r,o,s,i,c){let a=E=>I(E,2*i),l=t.length,f=n.flatMap((E,x)=>qn(E)[0]>jm*s[x]?[x]:[]),m=0,u=[],p=t.slice(),w=new Array(l).fill(0);if(f.length>0){let E=ve(Array.from({length:l},(M,T)=>f.map(R=>n[R][T][0])));m=E.rank,u=E.pivot.slice(0,m).map(M=>f[M]);let x=An(u.map(M=>n[M]),t);p=x.qty,u.forEach((M,T)=>{for(let R=0;R<l;R++)w[R]=w[R]+n[M][R][0]*x.coef[T][0]})}let g=[];u.forEach((E,x)=>{let M=r[E],T=g.find(R=>R.term===M);T===void 0&&g.push(T={term:M,df:0,ss:[0,0]}),T.df++,T.ss=Q(T.ss,pe(p[x],p[x]))});let d=l-m,N=qn(p,m)[0],b=bs(w,N,d),v=g.filter(E=>E.term!==0);d>0&&sn(c,N)&&(N=0,v=v.map(E=>sn(c,E.ss[0])?{...E,ss:[0,0]}:E));let y=v.map(E=>o[E.term-1]),h=E=>{if(d>0){let x=N/d,M=v.map(({df:T,ss:R})=>{let S=R[0]/T/x;return[T,E(R[0]),E(R[0]/T),S,we(S,T,d,!1,!1)]});return M.push([d,E(N),E(x),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...y,"Residuals"],table:M}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:y,table:v.map(({df:x,ss:M})=>[x,E(M[0]),E(M[0]/x)])}};return[h(a),h(E=>E),b]}function lc(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=ka({response:t.response,rhs:i},Object.keys(o)).variables,a=Jt(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=Jt({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))],p=f.colNames.length,w=f.colNames.map((q,C)=>ee([f.x.map(O=>O[C])]).groups[0]),g=f.x.map((q,C)=>w.map(O=>O[C])),d=p>0?ve(g):void 0,N=d?.rank??0;N<p&&u.push("Error() model is singular");let b=d?.pivot.slice(0,N)??[],v=q=>q.map(C=>[C,0]),y=a.colNames.map((q,C)=>v(ee([a.x.map(O=>O[C])]).groups[0])),{groups:h,k:E}=ee([l]),{fit:x,qtv:M}=qt(b.map(q=>v(w[q])),v(h[0]),y),T=h[0].map(q=>[1,q]),R=y.map(q=>qn(q)[0]),S=[];b.forEach((q,C)=>{let O=f.assign[q],D=O===0?"(Intercept)":f.terms.termLabels[O-1],W=S.find(F=>F.name===D);W===void 0&&S.push(W={name:D,idx:[]}),W.idx.push(C)}),m>N&&S.push({name:"Within",idx:Array.from({length:m-N},(q,C)=>N+C)});let k=S.map(q=>Km(q.name,q.idx.map(C=>x.qty[C]),M.map(C=>q.idx.map(O=>C[O])),a.assign,a.terms.termLabels,R,E,T)),A=k.map(q=>q[0]),_={formula:e,multistratum:!0,strata:A,rows:a.frame.rows,warnings:u};return ze.set(_,k.map(q=>q[1])),k.some((q,C)=>q[2]&&S[C].name!=="(Intercept)")&&Ns.set(_,[ws]),_}function io(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)=>I(s,-n[i])));return{inv:Da(r),e:n}}var Um=["weights","contrasts","levels","intercept"],Ym=["level"],Gm=["interval","level"],Ms=2**-1022;function Es(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=pc(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(p=>I(s.fittedValues[p],-a)),f=l.reduce((p,w)=>p+w,0)/l.length,m=l.length>1?l.reduce((p,w)=>p+(w-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 xs(e,t,n={}){return z(n,Um,"lm"),typeof e=="string"?j("lm",Es(e,Jt(Or(e),t,{...n,dropUnusedLevels:!0}),!1)):Hm(e,t,n)}function Hm(e,t,n){let r=n.intercept??!0,o=e.length>0&&typeof e[0]!="number"?e.map(m=>Array.from(m)):Array.from(e,m=>[m]),s=Array.from(t);if(o.some(m=>m.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((m,u)=>!Number.isNaN(m)&&o[u].every(p=>!Number.isNaN(p))&&(i===void 0||!Number.isNaN(i[u]))),a=i?.filter((m,u)=>c[u]);if(a?.some(m=>m<0))throw new RangeError("missing or negative weights not allowed");let l=o.filter((m,u)=>c[u]).map(m=>r?[1,...m]:m),f=s.filter((m,u)=>c[u]);return pc(l,f,a,r).result}function Xm(e,t,n,r,o,s,i){let c=s.rank,a=s.pivot.slice(0,c),l=n?.map(h=>I(h,-r)),f=no(l===void 0?void 0:o.map(h=>l[h]),o.length).map((h,E)=>{if(n===void 0||l===void 0)return h;let x=n[o[E]];if(x===0||l[o[E]]>=Ms)return h;let M=to([x,0]);return[I(M[0],-r/2),I(M[1],-r/2)]}),m=a.map(h=>{let E=o.map((M,T)=>at(f[T],e[M][h])),x=Fa(E.map(M=>M[0]));return{c:x===0?E:E.map(M=>[I(M[0],-x),I(M[1],-x)]),e:x}}),{qty:u,coef:p}=An(m.map(({c:h})=>h),o.map((h,E)=>at(f[E],t[h]))),w=p.map((h,E)=>m[E].e===0?h:[I(h[0],-m[E].e),I(h[1],-m[E].e)]),g=e[0]?.map(()=>NaN)??[];a.forEach((h,E)=>g[h]=w[E][0]);let d=e.map(h=>{let E=[0,0];return a.forEach((x,M)=>E=Q(E,at(w[M],h[x]))),E}),N=qn(u,c)[0],b=o.map(h=>l?.[h]??1),v=[0,0];if(i){let h=[0,0],E=[0,0];o.forEach((x,M)=>{h=Q(h,[b[M],0]),E=Q(E,at(d[x],b[M]))}),v=ke(E,h)}let y=[0,0];return o.forEach((h,E)=>{let x=de(d[h],v);y=Q(y,at(pe(x,x),b[E]))}),{coefficients:g,fittedValues:d.map(h=>h[0]),residuals:d.map((h,E)=>de([t[E],0],h)[0]),effects:u.map(h=>h[0]),rss:N,mss:y[0]}}function pc(e,t,n,r,o=!1){let s=ee([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return fc(mc(e,t,n,r,o),e,t,n);let i=mc(e,t.map(l=>I(l,-s)),n,r,o),c=i.result,a=l=>I(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),fc({...i,effects:i.effects.map(a),rss:I(i.rss,2*s),yScale:s,rssScaled:i.rss},e,t,n)}function fc(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]=Un(f,0)?Yn(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 hc=new WeakMap;function Bm(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function mc(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((B,J)=>n===void 0||n[J]!==0),i=e.filter((B,J)=>s[J]),c=t.filter((B,J)=>s[J]),a=n?.filter((B,J)=>s[J]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(i.some(B=>B.some(J=>!Number.isFinite(J))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some(B=>!Number.isFinite(B)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some(B=>!Number.isFinite(B)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((B,J)=>s[J]?[J]:[]),m=a===void 0?1:ge(a),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,p=0,w=a?.map(Math.sqrt),g=w===void 0?i:i.map((B,J)=>B.map(V=>V*w[J])),d=ve(g);a!==void 0&&Number.isFinite(m)&&u+2>0&&!Bm(d)&&(p=u+2,w=a.map(B=>I(Math.sqrt(B),-p/2)),g=i.map((B,J)=>B.map(V=>V*w[J])),d=ve(g),hc.set(d,p));let N=w===void 0?c:c.map((B,J)=>B*w[J]),b=d.rank,v=B=>I(B,-u),y=o?Xm(e,t,n,u,f,d,r):void 0,h=y?.coefficients??Mn(d,N),E=y?.residuals??[],x=y?.fittedValues??[],M=l-b,T=a?.map(v)??new Array(l).fill(1),R=y?.rss??0;if(y===void 0){let B=En(d,N),J=w===void 0?B:B.map((U,X)=>U/w[X]),V=0;t.forEach((U,X)=>{if(s[X])E.push(J[V]),x.push(U-J[V]),V++;else{let me=e[X].reduce((Le,Xe,Be)=>Number.isNaN(h[Be])?Le:Le+Xe*h[Be],0);x.push(me),E.push(U-me)}});for(let U=0;U<l;U++)R+=T[U]*J[U]**2}let S=x.filter((B,J)=>s[J]),k=0;if(a!==void 0&&a.some(B=>B!==0&&I(B,-u)<Ms)){let B=f.flatMap((J,V)=>{let U=E[J];return U===0?[]:[{w:a[V],r:U,a:Math.floor(Math.log2(Math.abs(U)))}]});if(B.length>0&&B.every(J=>Number.isFinite(J.r))){let J=Math.max(...B.map(X=>Math.log2(X.w)+2*X.a)),V=2*Math.floor(Math.floor(J)/2),U=0;for(let X of B)U+=I(X.w,2*X.a-V)*I(X.r,-X.a)**2;R=U,k=V-u}}let A=R/M,_=I(Math.sqrt(A),(u+k)/2),q=p===0?0:(p-u)/2,C=d.qr.slice(0,b).map(B=>B.slice(0,b).map(J=>p===0?I(J,-u/2):J)),{inv:O,e:D}=b>0?io(C):{inv:[],e:[]},F=d.pivot.slice(0,b).map((B,J)=>[B,J]).sort((B,J)=>B[0]-J[0]),L=F.map(([B])=>h[B]),$=F.map(([,B])=>I(Math.sqrt(O[B][B]*A),k/2-D[B]-q)),H=L.map((B,J)=>B/$[J]),te=H.map(B=>2*Ke(-Math.abs(B),M,!0,!1)),ne={coefficients:h,residuals:E,fittedValues:x,rank:b,dfResidual:M,coefTable:{estimate:L,stdError:$,tValue:H,pValue:te},sigma:_,rSquared:0,adjRSquared:0,intercept:r,qr:d},ae=r?1:0,Me=b-ae,Ie={result:ne,fitRows:f,effects:(y?.effects??[]).map(B=>I(B,u/2)),rss:I(R,u+k)};if(Me===0)return Ie;let $e=0;if(y!==void 0)$e=y.mss;else if(r){let B=T.reduce((V,U)=>V+U,0),J=0;for(let V=0;V<l;V++)J+=T[V]*S[V]/B;for(let V=0;V<l;V++)$e+=T[V]*(S[V]-J)**2}else for(let B=0;B<l;B++)$e+=T[B]*S[B]**2;ne.rSquared=$e/($e+I(R,k)),ne.adjRSquared=1-(1-ne.rSquared)*((l-ae)/M);let pt=I($e/Me/A,-k);return ne.fStatistic={value:pt,numdf:Me,dendf:M},ne.fPValue=we(pt,Me,M,!1,!1),Ie}function dc(e,t){let n=(1-e)/2;return n<1e-8?gt(n,t,!1,!1):gt((1+e)/2,t,!0,!1)}function Qm(e,t={}){z(t,Ym,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=dc(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 uc(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 I(I(e,-r)*I(t,-o),r+o+n)}function Zm(e,t,n={}){z(n,Gm,"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=-(hc.get(e.qr)??0)/2,l=e.coefficients.flatMap((v,y)=>Number.isNaN(v)?[]:[y]),f=o.map(v=>{let y=v.reduce((h,E,x)=>Number.isNaN(e.coefficients[x])?h:h+E*e.coefficients[x],0);return Un(y,0)?Yn(l.map(h=>v[h]),l.map(h=>e.coefficients[h]),y):y}),m=v=>{let y=new Array(s).fill(0);for(let h=0;h<s;h++){let E=v[h];for(let x=0;x<h;x++)E-=c[x][h]*y[x];y[h]=E/c[h][h]}return y},u=o.map(v=>{let y=i.map(x=>v[x]),h=So(m(y));if(h>=Ms&&h<1/0||!y.every(Number.isFinite)||y.every(x=>x===0))return a===0||!(h>0&&h<1/0)?h*e.sigma:uc(h,e.sigma,a);let E=Math.floor(Math.log2(ge(y.map(Math.abs))));for(let x of[0,600]){let M=So(m(y.map(T=>I(T,-(E+x)))));if(M<1/0)return M===0?M*e.sigma:uc(M,e.sigma,E+x+a)}return h*e.sigma}),p=n.interval??"none";if(p!=="none"&&p!=="confidence"&&p!=="prediction")throw new RangeError(`invalid 'interval': ${String(p)}`);let w=Dr(e.qr,e.rank,o),g=w.length>0?{nonEstimable:w,warnings:[Cr]}:{};if(p==="none")return j("predict",{fit:f,seFit:u,...g},e);let d=n.level??.95;if(!(d>0&&d<1))throw new RangeError("'level' must be between 0 and 1");let N=dc(d,e.dfResidual),b=u.map(v=>N*(p==="prediction"?Math.hypot(v,e.sigma):v));return j("predict",{fit:f,seFit:u,lwr:f.map((v,y)=>v-b[y]),upr:f.map((v,y)=>v+b[y]),...g},e)}var Jm=["weights","contrasts","levels"],gc=["confLevel"],wc=["sigmaX","sigmaY","mu","alternative","confLevel"];function eu(e,t,n={}){return z(n,Jm,"aov"),typeof e=="string"?j("aov",bc(e,t,n)):j("aov",tu(e,t))}function bc(e,t,n){let r=Or(e,!0),{treatment:o,errors:s}=Sa(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?lc(e,r,o,s[0],t,n):Es(e,Jt(r,t,{...n,dropUnusedLevels:!0},o),!0)}function tu(e,t){let{labels:n,groups:r}=$t(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 bc("y ~ g",{y:o,g:s},{levels:{g:n}})}function Nc(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,p)=>u+p.length,0),{ssBetween:r,ssWithin:o,k:s}=_e(e),i=t-1,c=n-t,a=r/i,l=o/c,f=a/l,m=u=>I(u,2*s);return{df:[i,c],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:c>0?we(f,i,c,!1,!1):NaN}}function nu(e,t,n={}){De(t)&&z(t,gc,"TukeyHSD"),z(n,gc,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=$t(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;Vt(c);let l=s.reduce((N,b)=>N+b.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}=_e(s),p=f/l,w=xa(c,i,l),g=[],d={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:g,pAdjusted:g,warnings:[]};for(let N=0;N<i-1;N++)for(let b=N+1;b<i;b++){let v=m[b]-m[N],y=Math.sqrt(p/2*(1/s[b].length+1/s[N].length));d.names.push(`${o[b]}-${o[N]}`),d.i.push(b+1),d.j.push(N+1),d.diff.push(I(v,u)),d.lwr.push(I(v-w*y,u)),d.upr.push(I(v+w*y,u)),d.pAdj.push(qr(Math.abs(v)/y,i,l,{lowerTail:!1}))}return d}function ru(e,t,n={}){De(t)&&z(t,wc,"zTest"),z(n,wc,"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 R of[o,s])if(R!==void 0&&!(R>0&&R<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=fe(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(R=>!Number.isNaN(R)),f=r===void 0?void 0:Array.from(r).filter(R=>!Number.isNaN(R));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=ee([l,f??[],[o,s??0]]),u=m.k,[p,w,[g,d]]=m.groups,N=Gn(p)[0],b=f===void 0?NaN:Gn(w)[0],v=f===void 0?g/Math.sqrt(l.length):Math.sqrt(g**2/l.length+d**2/f.length),y=f===void 0?N:N-b,h=(y-I(i,-u))/v,E,x;if(c==="less")E=Y(h),x=[-1/0,y+Z(a)*v];else if(c==="greater")E=Y(h,{lowerTail:!1}),x=[y-Z(a)*v,1/0];else{E=2*Y(-Math.abs(h));let R=(1-a)/2,S=(R<1e-8?Z(R,{lowerTail:!1}):Z(1-(1-a)/2))*v;x=[y-S,y+S]}x=[I(x[0],u),I(x[1],u)];let M=f!==void 0,T=(R,S)=>{if(!(u>0&&R.some((A,_)=>A!==0&&Math.abs(S[_])<2**-1022)))return I(Gn(S)[0],u);let k=Math.max(ee([R]).k,0);return I(Gn(k===0?R:R.map(A=>I(A,-k)))[0],k)};return{method:M?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:h,statisticName:"z",pValue:E,confInt:x,confLevel:a,estimate:M?[T(l,p),T(f,w)]:T(l,p),estimateNames:M?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:M?"difference in means":"mean",warnings:[]}}var ou=["family","link","weights","offset","start","epsilon","maxit","intercept"],su=["family","link","variance"],vc=["power"],au=["dispersion"],iu=["type"],cu=["type","dispersion"],lu=["dispersion"],fu=["newdata","newoffset","type","dispersion"],_n=2**-52;function mu(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function ir(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 Wt(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 Ec=2**-1022,co=e=>e===0||Math.abs(e)>=Ec&&Math.abs(e)<1/0;function yc(e,t,n,r){let o=e.variance(r),s=n**2,i=Math.sqrt(t*s/o);if(i>0&&i<1/0&&co(s)&&s!==0&&co(o)&&o!==0&&co(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 Ts(e,t,n,r){let o=(t-n)/r;if(Math.abs(r)>=Ec&&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 uu(e,t,n,r){let o=e.variance(n),s=(t-n)*Math.sqrt(r)/Math.sqrt(o);if(Number.isFinite(s)&&co(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 pu=.1;function xc(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(p=>!Number.isFinite(p))))throw new RangeError("NA/NaN/Inf in 'x'");return Xn(t,()=>{let m=(A,_)=>A.every(q=>c.etaOk(q))&&_.every(q=>c.muOk(q));if(r===0){let A=i.slice();if(!A.every(O=>c.etaOk(O)))throw new Error("invalid linear predictor values in empty model");let _=A.map(O=>c.linkinv(O));if(!_.every(O=>c.muOk(O)))throw new Error("invalid fitted means in empty model");let q=A.map(O=>c.muEta(O)),C=_.map((O,D)=>yc(c,s[D],q[D],O)**2);return{coefficients:[],eta:A,mu:_,deviance:Wt(c,o,_,s),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:s.map(O=>O>0),workingWeights:C,workingResiduals:o.map((O,D)=>Ts(c,O,_[D],q[D]))}}let u,p;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 ${mu(e.columnNames)}`);p=e.start.slice(),u=ir(n,p,i)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(A=>c.link(A));let w=u.map(A=>c.linkinv(A));if(!m(u,w))throw new Error("cannot find valid starting values: please specify some");let g=Wt(c,o,w,s),d=g,N=!1,b=!1,v=0,y=new Array(r).fill(0),h,E=s.map(()=>!1),x=[],M=[],T=Math.min(1e-7,a/1e3);for(v=1;v<=l;v++){let A=u.map(F=>c.muEta(F));for(let F=0;F<f;F++){if(!(s[F]>0))continue;let L=c.variance(w[F]);if(Number.isNaN(L))throw new Error("NAs in V(mu)");if(L===0&&!(c.varPower!==void 0&&w[F]>0))throw new Error("0s in V(mu)");if(Number.isNaN(A[F]))throw new Error("NAs in d(mu)/d(eta)")}E=s.map((F,L)=>F>0&&A[L]!==0);let _=[];for(let F=0;F<f;F++)E[F]&&_.push(F);if(_.length===0){t.add(`no observations informative at iteration ${v}`),N=!1;break}x=new Array(f).fill(0);let q=[],C=[];for(let F of _){let L=yc(c,s[F],A[F],w[F]);x[F]=L,q.push((u[F]-i[F]+Ts(c,o[F],w[F],A[F]))*L),C.push(n[F].map($=>$*L))}h=ve(C,{tol:T});let O=Mn(h,q);if(O.some((F,L)=>h.pivot.indexOf(L)<h.rank&&!Number.isFinite(F))){t.add(`non-finite coefficients at iteration ${v}`),N=!1;break}y=O.map(F=>Number.isNaN(F)?0:F),b=!1;let D=u;if(u=ir(n,y,i),w=u.map(F=>c.linkinv(F)),d=Wt(c,o,w,s),!Number.isFinite(d)){if(p===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 F=0;for(;!Number.isFinite(d);){if(F>=l)throw new Error("inner loop 1; cannot correct step size");F++,y=y.map((L,$)=>(L+p[$])/2),u=ir(n,y,i),w=u.map(L=>c.linkinv(L)),d=Wt(c,o,w,s)}b=!0}if(!m(u,w)){if(p===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 F=0;for(;!m(u,w);){if(F>=l)throw new Error("inner loop 2; cannot correct step size");F++,y=y.map((L,$)=>(L+p[$])/2),u=ir(n,y,i),w=u.map(L=>c.linkinv(L))}b=!0,d=Wt(c,o,w,s)}let W=Math.max(a,1e-10);if(p!==void 0&&v>1&&(d-g)/(.1+Math.abs(d))>=W){t.add("step size truncated due to increasing deviance");let F=0;for(;(d-g)/(.1+Math.abs(d))>=W&&F<l;)F++,y=y.map((L,$)=>(L+p[$])/2),u=ir(n,y,i),w=u.map(L=>c.linkinv(L)),d=Wt(c,o,w,s);if((d-g)/(.1+Math.abs(d))>=W){t.add("inner loop 3; cannot correct step size"),N=!1;break}}if(e.untilCoefficients){let F=ge(y.map(Math.abs));if(p!==void 0&&y.every((L,$)=>Math.abs(L-p[$])<=8*_n*F)){N=!0;break}if(M.some(L=>L.every(($,H)=>$===y[H]))){N=!0;break}M.push(y.slice()),M.length>16&&M.shift()}else if(Math.abs(d-g)/(Math.abs(d)+.1)<a){N=!0;let F=0;for(let L=0;L<f;L++)E[L]&&(F=Math.max(F,Math.abs(u[L]-D[L])));F>pu&&t.add("glm.fit: the linear predictor was still changing at convergence: the data may be separated (no finite maximum likelihood estimates)");break}g=d,p=y.slice()}v>l&&(v=l);let R=r,S=y.slice();if(h!==void 0){R=h.rank;for(let A=h.rank;A<r;A++)S[h.pivot[A]]=NaN}let k=u.map(A=>c.muEta(A));return{coefficients:S,eta:u,mu:w,deviance:d,iter:v,converged:N,boundary:b,rank:R,qr:h,used:E,workingWeights:x.map(A=>A*A),workingResiduals:o.map((A,_)=>Ts(c,A,w[_],k[_]))}})}function Tc(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function Rc(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*_n||o<10*_n)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*_n)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function In(e,t,n,r={}){let o=Tn(e.familySpec),s="start"in r?r.start:e.start,i=Va(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=xc({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 Rc(l,o,n,t.length===0),{fit:l,fam:o,y:i.y,w:i.weights,size:i.size,offset:c}}function hu(e,t,n={}){z(n,ou,"glm"),z(Ft(n,"family"),su,"glm","family"),z(Ft(n,"family.link"),vc,"glm","family.link"),z(Ft(n,"link"),vc,"glm","link");let r=n.intercept??!0,o=Tc(e);if(o.some(M=>M.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(M=>Array.from(M)):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((M,T)=>{let R=i[T];return!(Array.isArray(R)?R.some(k=>Number.isNaN(k)):Number.isNaN(R))&&M.every(k=>!Number.isNaN(k))&&!(a!==void 0&&Number.isNaN(a[T]))&&!(l!==void 0&&Number.isNaN(l[T]))}),m=o.filter((M,T)=>f[T]).map(M=>r?[1,...M]:M),u=i.filter((M,T)=>f[T]),p=u.length;if(p===0)throw new RangeError("0 (non-NA) cases");let w=a===void 0?new Array(p).fill(1):a.filter((M,T)=>f[T]);if(w.some(M=>M<0))throw new RangeError("negative weights not allowed");if(w.some(M=>!Number.isFinite(M)))throw new RangeError("non-finite weights not allowed");let g=l===void 0?new Array(p).fill(0):l.filter((M,T)=>f[T]);if(g.some(M=>!Number.isFinite(M)))throw new RangeError("the offset must be finite");let d=o.length>0?o[0].length:0,N=[...r?["(Intercept)"]:[],...Array.from({length:d},(M,T)=>`x${T+1}`)],b=N.length,v=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(v.link=n.link);let y={x:m,columnNames:N,yGiven:u,priorGiven:w,offset:g,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)};if(!(y.epsilon>0&&y.epsilon<1/0))throw new RangeError("value of 'epsilon' must be > 0 and finite");if(!(y.maxit>0&&y.maxit<1/0))throw new RangeError("maximum number of iterations must be > 0");let h=!s&&du(v)?ee([u]).k:0;if(h===0)return j("glm",Mc(y));let E=M=>I(M,-h),x=Mc({...y,yGiven:u.map(E),offset:g.map(E),start:y.start?.map(E)});return j("glm",gu(x,h,{...y,yScale:h}))}function Mc(e){let{x:t,offset:n,intercept:r}=e,o=t.length,s=e.columnNames.length,i=new Re,c=Array.from({length:s},(d,N)=>N),{fit:a,fam:l,y:f,w:m,size:u}=In(e,c,i),p;if(r&&e.hasOffset){let d=new Re,N=xc({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},d);Rc(N,l,d,!1),d.list.forEach(b=>i.add(b)),p=N.deviance,N.converged||i.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let d=Math.floor(Math.log2(ge(m))),N=0,b=0;for(let y=0;y<o;y++){if(!(m[y]>0))continue;let h=I(m[y],-d);N+=h,b+=h*f[y]}let v=b/N;if(!Number.isFinite(v)){let y=0;for(let x=0;x<o;x++)m[x]>0&&(y=Math.max(y,Math.abs(f[x])));let h=Math.floor(Math.log2(y)),E=0;for(let x=0;x<o;x++)m[x]>0&&(E+=I(m[x],-d)*I(f[x],-h));v=I(E/N,h)}p=Xn(i,()=>Wt(l,f,f.map(()=>v),m))}else p=Xn(i,()=>Wt(l,f,n.map(d=>l.linkinv(d)),m));let w=o-m.filter(d=>d===0).length,g=za(l,f,a.mu,m,u,a.deviance,i)+2*a.rank;return Sc(a.used,{coefficients:a.coefficients,fittedValues:a.mu,linearPredictors:a.eta,deviance:a.deviance,nullDeviance:p,aic:g,rank:a.rank,dfResidual:w-a.rank,dfNull:w-(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 du(e){return(e.link??"identity")!=="identity"?!1:e.family==="gaussian"||e.family==="quasi"&&(e.variance??"constant")==="constant"}function gu(e,t,n){let r=s=>I(s,t),o={...e,coefficients:e.coefficients.map(r),fittedValues:e.fittedValues.map(r),linearPredictors:e.linearPredictors.map(r),deviance:I(e.deviance,2*t),nullDeviance:I(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}),Sc(Rs.get(e),o)}var Rs=new WeakMap;function Sc(e,t){return e!==void 0&&Rs.set(t,e),t}function Nt(e){let t=e.scaled;return t===void 0?{fit:e,k:0}:{fit:t,k:e.model.yScale??0}}function cr(e){return Tn(e.model.familySpec)}function kc(e,t){let n=Nt(e),r=lr(n.fit,t);return cr(e).fixedDispersion?r:I(r,2*n.k)}function lr(e,t){if(cr(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?wu(e):n/e.dfResidual}function wu(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=ee([t.map(s=>e.residuals[s])]),r=ee([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 I(o/e.dfResidual,2*(n.k+r.k))}function bu(e){return e.qr===void 0||e.rank===0?{inv:[],e:[]}:io(e.qr.qr.slice(0,e.rank).map(t=>t.slice(0,e.rank)))}function Nu(e){if(!(e>=0&&e<1/0))throw new RangeError("'dispersion' must be a non-negative finite number");return e}function Ln(e,t){let n=I(Nu(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:I(e,-2*r),s:r-t}}function Ss(e,t={}){z(t,au,"glmSummary");let{fit:n,k:r}=Nt(e),o=new Re,s=cr(n),i=t.dispersion===void 0&&!s.fixedDispersion,c=t.dispersion===void 0?void 0:Ln(t.dispersion,r),a=c===void 0?lr(n,o):c.m,l=c?.s??0,{inv:f,e:m}=bu(n),u=f.map((h,E)=>h.map((x,M)=>I(x,-m[E]-m[M]))),w=(n.qr===void 0?[]:n.qr.pivot.slice(0,n.rank)).map(h=>n.coefficients[h]),g=f.map((h,E)=>I(Math.sqrt(h[E]*a),-m[E])),d=l===0?w.map((h,E)=>h/g[E]):w.map((h,E)=>Ce(h/Math.sqrt(f[E][E]*a),m[E]-l)),N=!i,b=d.map(h=>N?2*Y(-Math.abs(h)):n.dfResidual>0?2*Ke(-Math.abs(h),n.dfResidual,!0,!1):NaN);i&&n.dfResidual===0&&(g.fill(NaN),d.fill(NaN));let v=h=>I(h,r),y=h=>I(h,2*r);return j("glmSummary",{dispersion:l===0?y(a):t.dispersion,estimate:w.map(v),stdError:l===0?g.map(v):f.map((h,E)=>Ce(Math.sqrt(h[E]*a),l+r-m[E])),statistic:d,pValue:b,statisticName:N?"z value":"t value",covUnscaled:u,covScaled:l===0?u.map(h=>h.map(E=>y(E*a))):f.map((h,E)=>h.map((x,M)=>Ce(x*a,2*(l+r)-m[E]-m[M]))),dfResidual:n.dfResidual,warnings:o.list},e)}function vu(e,...t){z(yn(t),iu,"glmResiduals");let n=vn(["type"],t).type??"deviance",r=Nt(e);return ks(r.fit,n).map(o=>I(o,r.k))}function ks(e,t){let n=cr(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)=>uu(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 As(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=Pa(n,c).map(l=>l.reduce((f,m)=>f+m*m,0))}let o=0,s=Rs.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*_n?1:a)}),t}function yu(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 Mu(e,t={}){z(t,cu,"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}=Nt(e),s=As(r),{m:i,s:c}=t.dispersion===void 0?{m:lr(r),s:0}:Ln(t.dispersion,o),a=Math.sqrt(i),l=ks(r,n).filter((m,u)=>r.priorWeights[u]>0),f=r.rank>0?100*_n*yu(l.map(Math.abs)):0;return l.map((m,u)=>{let p=Ce((Math.abs(m)<f?0:m)/(a*Math.sqrt(1-s[u])),-c);return Number.isFinite(p)?p:NaN})}function Eu(e,t={}){z(t,lu,"glmCooksDistance");let{fit:n,k:r}=Nt(e),o=As(n),{m:s,s:i}=t.dispersion===void 0?{m:lr(n),s:0}:Ln(t.dispersion,r);return ks(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=xu(a,o[l],s,n.rank)),i!==0&&(m=Ce(m,-2*i)),Number.isFinite(m)?m:NaN})}function xu(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=I(e,-o)/(1-t);return I(i*i*(t/(I(n,-s)*r)),2*o-s)}function Tu(e,t={}){z(t,fu,"glmPredict");let{fit:n,k:r}=Nt(e),o=b=>I(b,r),s=cr(n),i,c;if(t.newdata===void 0)i=n.model.x,c=n.linearPredictors.slice();else{if(i=Tc(t.newdata).map(y=>n.model.intercept?[1,...y]:y),i.some(y=>y.length!==n.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let b=t.newoffset===void 0?i.map(()=>0):Array.from(t.newoffset,y=>I(y,-r));if(b.length!==i.length)throw new RangeError("newoffset must have one value per row of newdata");let v=n.coefficients.flatMap((y,h)=>Number.isNaN(y)?[]:[h]);c=i.map((y,h)=>{let E=y.reduce((M,T,R)=>Number.isNaN(n.coefficients[R])?M:M+T*n.coefficients[R],0);return(Un(E,0)?Yn(v.map(M=>y[M]),v.map(M=>n.coefficients[M]),E):E)+b[h]})}let{m:a,s:l}=t.dispersion===void 0?{m:lr(n),s:0}:Ln(t.dispersion,r),f=b=>I(b,r+l),m=n.rank,u=n.qr===void 0?[]:n.qr.pivot.slice(0,m),p=n.qr?.qr,w=i.map(b=>{let v=new Array(m).fill(0),y=0;for(let h=0;h<m;h++){let E=b[u[h]];for(let x=0;x<h;x++)E-=p[x][h]*v[x];v[h]=E/p[h][h],y+=v[h]*v[h]}if(!(y>=2**-1022&&y<1/0)){let h=v.reduce((E,x)=>Math.max(E,Math.abs(x)),0);if(h>0&&Number.isFinite(h)){let E=Math.sqrt(v.reduce((M,T)=>M+(T/h)**2,0)),x=h*E*Math.sqrt(a);return Number.isFinite(x)?x:h*(E*Math.sqrt(a))}}return Math.sqrt(y)*Math.sqrt(a)}),g=t.type??"link";if(g!=="link"&&g!=="response")throw new RangeError(`invalid 'type': ${String(g)}`);let d=t.newdata===void 0?[]:Dr(n.qr,m,i),N=d.length>0?{nonEstimable:d,warnings:[Cr]}:{};return g==="link"?j("glmPredict",{fit:c.map(o),seFit:w.map(f),...N},e):j("glmPredict",Xn(new Re,()=>({fit:c.map(b=>o(s.linkinv(b))),seFit:w.map((b,v)=>f(b*Math.abs(s.muEta(c[v])))),...N})),e)}var Ru=["parm","level","test"],Ac={epsilon:1e-300,maxit:100,untilCoefficients:!0};function qs(e,t,n,r,o=1e-11){let s=[];for(let g=0;g<t.length;g++)n[g]>0&&s.push(g);let i=0,c=0;for(let g of s)i+=n[g],c+=n[g]*t[g];if(r&&!(Number.isFinite(i)&&Number.isFinite(c))){let g=0;for(let d of s)g=Math.max(g,n[d]);if(Number.isFinite(g)){let d=g>1?Math.floor(Math.log2(g)):0;i=0,c=0;for(let N of s){let b=I(n[N],-d);i+=b,c+=b*t[N]}}}let a=r?c/i:0,l=0;for(let g of s)l+=n[g]*(t[g]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(g=>Math.sqrt(n[g])),u=ve(s.map((g,d)=>e[g].map(N=>N*m[d])),{tol:o}),p=En(u,s.map((g,d)=>t[g]*m[d])),w=0;for(let g of p)w+=g*g;if(!Number.isFinite(l)){let g=s.map((N,b)=>m[b]*(t[N]-a)),d=0;for(let N of g)d=Math.max(d,Math.abs(N));for(let N of p)d=Math.max(d,Math.abs(N));if(Number.isFinite(d)){let N=d>1?Math.floor(Math.log2(d)):0,b=0;for(let y of g)b+=I(y,-N)**2;let v=0;for(let y of p)v+=I(y,-N)**2;return I(I(b-v,N),N)}}return l-w}function Su(e,t={}){z(t,Ru,"glmConfint");let{fit:n,k:r}=Nt(e),o=t.level??.95,s=t.test??"LRT";if(s!=="LRT"&&s!=="Rao")throw new RangeError(`invalid 'test': ${String(s)}`);if(!(o>0&&o<1))throw new RangeError("'level' must be in (0, 1)");let i=n.coefficients.length,c=t.parm??Array.from({length:i},(_,q)=>q);for(let _ of c)if(!Number.isInteger(_)||_<0||_>=i)throw new RangeError(`invalid coefficient index ${_}`);let a=n.model,l=n.coefficients.map((_,q)=>q).filter(_=>!Number.isNaN(n.coefficients[_])),f=new Re,m=In(a,l,f,{...Ac,start:a.start===void 0?void 0:l.map(_=>a.start[_])}),u=new Array(i).fill(NaN);if(l.forEach((_,q)=>u[_]=m.fit.coefficients[q]),!m.fit.converged)return{lower:c.map(()=>NaN),upper:c.map(()=>NaN)};let p=!1,w={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},g=Te(()=>Ss(w)),d=g.dispersion,N=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((_,q)=>N[l[_]]=g.stdError[q]);let v=a.x.map(_=>l.map(q=>_[q])),y=m.fit.eta,h=(_,q)=>{let C=l.filter(W=>W!==_),O=In(a,C,new Re,{...Ac,start:void 0,etastart:y,extraOffset:a.x.map(W=>W[_]*q)}).fit;if(!O.converged)return p=!0,NaN;let D=s==="LRT"?O.deviance-m.fit.deviance:qs(v,O.workingResiduals,O.workingWeights,!1);return Math.sign(q-u[_])*Math.sqrt(Math.max(D/d,0))},E=(1-o)/2,x=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),M=2**-9,T=x<M,R=T?[-x,x]:[Z(E),E<1e-8?Z(E,{lowerTail:!1}):Z((1+o)/2)],S=c.map(()=>NaN),k=c.map(()=>NaN),A=(_,q,C)=>{let O=C===0?-1:1;p=!1;let D=NaN,W=NaN;for(let ae=0;ae<=30;ae++){let Me=u[_]+O*2**ae*N[_],Ie=h(_,Me);if(Math.abs(Ie)>=Math.abs(q)){D=Me,W=Ie;break}}if(Number.isNaN(D)||p)return NaN;let F=ae=>ae===u[_]?-q:h(_,ae)-q,[L,$]=C===0?[D,u[_]]:[u[_],D],[H,te]=C===0?[W-q,-q]:[-q,W-q],ne;try{ne=wt(F,L,$,{tol:1e-14*N[_],maxIter:500,fLower:H,fUpper:te})}catch(ae){if(!p)throw ae;ne=NaN}return p?NaN:ne};return c.forEach((_,q)=>{if(Number.isNaN(u[_]))return;if(!T){S[q]=A(_,R[0],0),k[q]=A(_,R[1],1);return}let C=[-2*M,-M,0,M,2*M],O=[A(_,-2*M,0),A(_,-M,0),u[_],A(_,M,1),A(_,2*M,1)].map(W=>W-u[_]),D=W=>{let F=0;for(let L=0;L<5;L++){let $=1;for(let H=0;H<5;H++)H!==L&&($*=(W-C[H])/(C[L]-C[H]));F+=$*O[L]}return u[_]+F};S[q]=D(R[0]),k[q]=D(R[1])}),{lower:S.map(_=>I(_,r)),upper:k.map(_=>I(_,r))}}var ku=["test","dispersion"],Au=(e,t)=>xe(e,t/2,2,!1,!1);function qu(e,t,n){let{fit:r,w:o}=In(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 _s=(e,t)=>qs(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function _u(e,t={}){z(t,ku,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Nt),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:Ln(t.dispersion,o),c=i?.s??0,a=Iu(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return j("glmAnova",a,...n);let l=new Set(["Deviance","Resid. Dev","Rao","Cp"]),f=a.colNames.map(u=>l.has(u)),m=a.table.map(u=>u.map((p,w)=>f[w]?I(p,2*o):p));if(c!==0&&a.colNames.includes("Cp")){let u=a.colNames.indexOf("Cp"),p=a.colNames.indexOf("Resid. Dev"),w=a.colNames.indexOf("Resid. Df"),g=s[0].model.x.length;for(let d of m)d[u]=d[p]+2*(g-d[w])*t.dispersion}return j("glmAnova",{...a,table:m},...n)}function Iu(e,t,n,r=0){let o=new Re,s=e[0],i=Tn(s.model.familySpec);if(e.some(M=>{let T=Tn(M.model.familySpec);return T.family!==i.family||T.name!==i.name||T.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=[],p=new Set,w;if(t){let M=e[0].model.x.length;if(e.some(R=>R.model.x.length!==M))throw new RangeError("models were not all fitted to the same size of dataset");let T=(R,S)=>R.length===S.length&&R.every((k,A)=>JSON.stringify(k)===JSON.stringify(S[A]));if(e.some(R=>!T(R.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(R=>!T(R.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((R,S)=>l.push({name:String(S+1),dfRes:R.dfResidual,dev:R.deviance})),f=l.map((R,S)=>S===0?NaN:l[S-1].dfRes-R.dfRes),m=l.map((R,S)=>S===0?NaN:l[S-1].dev-R.dev),e.forEach((R,S)=>{if(S===0||f[S]===0)return;let[k,A]=f[S]>0?[e[S-1],R]:[R,e[S-1]],_=O=>O.filter((D,W)=>k.priorWeights[W]>0&&A.priorWeights[W]>0),q=k.model.offset.map((O,D)=>O-A.model.offset[D]),C=q.some((O,D)=>O!==0&&k.priorWeights[D]>0)?k.model.x.map((O,D)=>[...O,q[D]]):k.model.x;Vr(_(C),_(A.model.x))||p.add(S)}),a==="Rao"){let R=e.map(S=>({dfRes:S.dfResidual,dev:S.deviance,fit:qc(S),x:S.model.x,intercept:S.model.intercept}));u=l.map((S,k)=>k===0?NaN:f[k]<0?-_s(R[k],R[k-1]):_s(R[k-1],R[k]))}w=e.reduce((R,S)=>S.dfResidual<R.dfResidual?S:R)}else{let M=s,T=M.model,R=T.x.length>0?T.x[0].length-(T.intercept?1:0):0,S=T.intercept?[0]:[],k=_=>[...S,...Array.from({length:_},(q,C)=>C+S.length)],A=[];for(let _=0;_<=R;_++)_===R?A.push({dfRes:M.dfResidual,dev:M.deviance,fit:qc(M),x:T.x,intercept:T.intercept}):_>0||a==="Rao"?A.push(qu(T,k(_),o)):A.push(void 0);l.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let _=1;_<=R;_++)l.push({name:`x${_}`,dfRes:A[_].dfRes,dev:A[_].dev});f=l.map((_,q)=>q===0?NaN:l[q-1].dfRes-_.dfRes),m=l.map((_,q)=>q===0?NaN:Math.max(l[q-1].dev-_.dev,0)),a==="Rao"&&(u=l.map((_,q)=>q===0?NaN:_s(A[q-1],A[q]))),w=M}e.forEach((M,T)=>{M.converged||o.add(`model ${T+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let g=new Re,d=n.dispersion??kc(w,g);g.list.forEach(M=>o.add(M));let N=c?1/0:w.dfResidual,b=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=l.map((M,T)=>t?[M.dfRes,M.dev,f[T],m[T]]:[f[T],m[T],M.dfRes,M.dev]),y=[...b],h=M=>l.map((T,R)=>{if(R===0||f[R]===0||p.has(R))return NaN;let S=Ce(M[R]/d*Math.sign(f[R]),-2*r);return S<0||Number.isNaN(S)?NaN:Au(S,Math.abs(f[R]))}),E=[];if(a==="Chisq"||a==="LRT")y.push("Pr(>Chi)"),E=[h(m)];else if(a==="Rao")y.push("Rao","Pr(>Chi)"),E=[u.map((M,T)=>p.has(T)?NaN:M),h(u)];else if(a==="F"){if(c&&o.add(`using F test with a '${i.family}' family is inappropriate`),N!==0){let M=l.map((T,R)=>{let S=Ce(m[R]/f[R]/d,-2*r);return R===0||f[R]===0||p.has(R)||S<0?NaN:S});y.push("F","Pr(>F)"),E=[M,M.map((T,R)=>Number.isNaN(T)?NaN:we(T,Math.abs(f[R]),N,!1,!1))]}}else if(a==="Cp"){let M=w.model.x.length;y.push("Cp"),E=[l.map(T=>T.dev+2*(M-T.dfRes)*d)]}let x=v.map((M,T)=>[...M,...E.map(R=>R[T])]);return{colNames:y,rowNames:l.map(M=>M.name),table:x,warnings:o.list}}function qc(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 Lu=["type"];function _c(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let i=e.y.flatMap((w,g)=>e.weights===void 0||e.weights[g]!==0?[g]:[]),c=i.map(w=>e.weights===void 0?1:Math.sqrt(e.weights[w])),a=ve(i.map((w,g)=>s.map(d=>e.x[w][d]*c[g]))),l=no(e.weights===void 0?void 0:i.map(w=>I(e.weights[w],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=An(f.map(w=>i.map((g,d)=>at(l[d],e.x[g][s[w]]))),i.map((w,g)=>at(l[g],I(e.y[w],-r)))),u=[0,0],p=0;return f.forEach((w,g)=>{w>=t.length&&(u=Q(u,pe(m[g],m[g])),p++)}),{ss:u[0],df:p}}function Is(e,t={}){if(z(t,Lu,"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=oo([e]),i=so([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=gs(e,s),a=E=>{let x=c.map(([R,S])=>[I(R,-E),S]),M=x.reduce((R,[S])=>R+S,0),T=x.reduce((R,[S,k])=>R+S*k,0)/M;return Number.isFinite(M)&&Number.isFinite(T)?x.reduce((R,[S,k])=>R+S*(k-T)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=ao(c),f=a(l)),sn(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 p=E=>e.assign.flatMap((x,M)=>E.has(x)?[M]:[]),w=[...new Set(e.assign)],g=[],d=[],N=E=>{g.length=0;let x=[];if(r===3){if(e.coefficients.some(M=>Number.isNaN(M)))throw new Error("there are aliased coefficients in the model");for(let M of w){let T=p(new Set([M])),R=p(new Set(w.filter(S=>S!==M)));g.push(M===0?"(Intercept)":e.termLabels[M-1]),x.push({ss:_c(e,R,T,s,E).ss,df:T.length})}}else{let M=T=>e.factors.flatMap((R,S)=>R[T-1]>0?[S]:[]);for(let T of w){if(T===0)continue;let R=M(T),S=w.filter(_=>_!==0&&_!==T&&R.every(q=>e.factors[q][_-1]>0)),k=p(new Set(w.filter(_=>_!==T&&!S.includes(_)))),A=_c(e,k,p(new Set([T])),s,E);g.push(e.termLabels[T-1]),x.push(A)}}return x};d=N(l),l===0&&e.weights!==void 0&&d.some(E=>!Number.isFinite(E.ss))&&(l=ao(c),d=N(l));let b=E=>I(E,2*s+l),v=I(i,-l),y=v/o,h=d.map(({ss:E,df:x})=>{let M=E/x/y;return[b(E),x,M,we(M,x,o,!1,!1)]});return h.push([b(v),o,NaN,NaN]),j("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...g,"Residuals"],table:h,warnings:m},e)}var lo=[0,0];var Ls=e=>e.map(t=>t.map(n=>n[0]));function Ic(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function an(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(lo);return r.forEach((s,i)=>{let c=t[i];for(let a=0;a<n;a++)o[a]=Q(o[a],pe(s,c[a]))}),o})}function cn(e){let t=lo;return e.forEach((n,r)=>t=Q(t,n[r])),t}function Lc(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=ke(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],pe(l,n[i][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Oc(e){let{a:t,sign:n,singular:r}=Lc(e);if(r)return lo;let o=[n,0];return t.forEach((s,i)=>o=pe(o,s[i])),o}function Fc(e){let t=e.length,{a:n,perm:r,singular:o}=Lc(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(lo));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],pe(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],pe(n[a][l],c[l]));c[a]=ke(c[a],n[a][a])}for(let a=0;a<t;a++)s[a][i]=c[a]}return s}var Ou=["group","idata","M","X"],Pc=(e,t)=>xe(e,t/2,2,!1,!1);function Os(e){let t=e.length,n=Rt(cn(e),t);return Oc(e.map(r=>r.map(o=>ke(o,n))))[0]}function Fs(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=Pc(i,c),l=Pc(i,c+4);return a+s*(l-a)}var Fu=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function Pu(e,t,n){let r=e.length,o=N=>N.length===0?0:ve(Array.from({length:r},(b,v)=>N.map(y=>y[v]))).rank,s=n.length===0?void 0:ve(Array.from({length:r},(N,b)=>n.map(v=>v[b]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(N=>n[N]),c=i.length,a=[...i,...t],l=ve(Array.from({length:r},(N,b)=>a.map(v=>v[b])));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(N=>a[N]),m=N=>N.map(b=>[b,0]),u=new Array(r).fill([0,0]),p=qt(f.map(m),u,e.map((N,b)=>e.map(v=>v[b]))).qtv,w=Array.from({length:r},(N,b)=>p.map(v=>v[b])),g=qt(f.map(m),u,w).qtv,d=Array.from({length:l.rank-c},(N,b)=>c+b);return d.map(N=>d.map(b=>g[N][b]))}function Cu(e,t={}){z(t,Ou,"mauchlyTest");let n=e.map(h=>Array.from(h)),r=n[0]?.length??0;if(r===0||n.some(h=>h.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=h=>h==null||typeof h=="number"&&Number.isNaN(h),i=n.map((h,E)=>!h.some(s)&&(o===void 0||!s(o[E])));if(n.some((h,E)=>i[E]&&h.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=ee(n.filter((h,E)=>i[E])).groups,a=c.length,l=o?.filter((h,E)=>i[E]).map(Pe)??c.map(()=>""),f=[...new Set(l)].sort(it),m=c.map(h=>h.map(()=>[0,0]));for(let h of f){let E=l.flatMap((x,M)=>x===h?[M]:[]);for(let x=0;x<r;x++){let M=[0,0];for(let R of E)M=Q(M,[c[R][x],0]);let T=Rt(M,E.length);for(let R of E)m[R][x]=de([c[R][x],0],T)}}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 p=Array.from({length:r},(h,E)=>Array.from({length:r},(x,M)=>{let T=[0,0];for(let R of m)T=Q(T,pe(R[E],R[M]));return T})),w=t.idata??{index:Array.from({length:r},(h,E)=>E+1)},g=h=>{let E=Te(()=>xn(h,w));if(E.x.length!==r)throw new RangeError("idata must have one row per column of y");return Fu(E.x,E.colNames.length)},d=t.M===void 0?Array.from({length:r},(h,E)=>Array.from({length:r},(x,M)=>M===E?1:0)):g(t.M),N=g(t.X??"~0"),b=Pu(p,d,N),v=b.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 y=Os(b);return{statistic:y,statisticName:"W",pValue:Fs(y,v,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var Du=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),Vu=["n","p1","p2","sigLevel","power","alternative","strict","tol"],zu=["groups","n","betweenVar","withinVar","sigLevel","power"],Dc=Number.EPSILON**.25;function be(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 G=e=>e==null;function tt(e,t,n){if(!G(e)&&!t(e))throw new RangeError(n)}var fr=e=>e>=0&&e<=1,fo=e=>e>0&&e<1/0;function ft(e,t){if(e.filter(G).length!==1)throw new RangeError(t)}function oe(e,t,n,r={}){return Ia(e,t,n,r).root}var ln=(e,t,n,r)=>Ar(e,t,{ncp:n,lowerTail:r});function Ps(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Cs="'sig.level' must be numeric in [0, 1]";function $u(e={}){z(e,Du,"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;ft([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Ps(s,Cs),tt(r,fr,"'power' must be a probability"),tt(o,fo,"'sd' must be a positive finite number"),tt(t,g=>g>=2,"number of observations in each group must be at least 2"),tt(n,g=>!Number.isNaN(g),"'delta' must not be NaN"),tt(e.tol,fo,"'tol' must be a positive finite number");let i=be(e.type,["two.sample","one.sample","paired"]),c=be(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Dc,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!G(n)&&(n=Math.abs(n));let u=(g,d,N,b)=>{let v=Math.max(1e-7,g-1)*f,y=Qt(b/m,v,{lowerTail:!1}),h=Math.sqrt(g/f)*d/N;Number.isFinite(h)||(h=Math.sqrt(g/f)*(d/N));let E=ln(y,v,h,!1);return a&&m===2?E+ln(-y,v,h,!0):E};if(G(r))r=u(t,n,o,s);else{let g=r;if(G(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=oe(d=>u(d,n,o,s)-g,2,1e7,{tol:l,extendInt:"upX"})}else if(G(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let d=Math.abs(n);o=oe(N=>u(t,n,N,s)-g,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else G(n)?n=oe(d=>u(t,d,o,s)-g,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=oe(d=>u(t,n,o,d)-g,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let p=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,w={"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,...p===void 0?{}:{note:p},method:w,warnings:[]}}function Wu(e={}){z(e,Vu,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;ft([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Ps(s,Cs),tt(o,fr,"'power' must be a probability"),tt(n,fr,"'p1' must be a probability"),tt(r,fr,"'p2' must be a probability"),tt(t,u=>u>0,"number of observations in each group must be positive"),tt(e.tol,fo,"'tol' must be a positive finite number");let i=be(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Dc,l=i==="two.sided"?2:1,f=(u,p,w,g)=>{let d=Z(g/l,{lowerTail:!1});if(c&&l===2){let N=(p+w)/2,b=p*(1-p),v=w*(1-w),y=N*(1-N),h=Math.abs(p-w),E=Math.sqrt(b+v);return Y((Math.sqrt(u)*h-d*Math.sqrt(2*y))/E)+Y((Math.sqrt(u)*h+d*Math.sqrt(2*y))/E,{lowerTail:!1})}return Y((Math.sqrt(u)*Math.abs(p-w)-d*Math.sqrt((p+w)*(1-(p+w)/2)))/Math.sqrt(p*(1-p)+w*(1-w)))},m=[];if(G(o))o=f(t,n,r,s);else{let u=o;if(G(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=oe(p=>f(p,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(G(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=oe(p=>f(t,p,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(G(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=oe(p=>f(t,n,p,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=oe(p=>f(t,n,r,p)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return j("powerPropTest",{n:t,p1:n,p2:r,sigLevel:s,power:o,alternative:i,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var Cc=1e16,mo=(e,t)=>e>Cc&&e<1/0&&t>Cc;function uo(e,t,n,r){let o=Z(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 Y((o*i-s)/c,{lowerTail:!1})}function ju(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 I(I(e,-s)*I(t,-i)*I(n,-c)/I(r,-a),s+i+c-a)}function Ku(e={}){z(e,zu,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!G(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!G(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Ps(i,Cs),tt(s,fr,"'power' must be a probability"),tt(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),tt(o,fo,"'withinVar' must be a positive finite number");let c=(a,l,f,m,u)=>{let p=(a-1)*l*f/m;p<1/0||(p=(a-1)*l*(f/m)),!(p<1/0)&&Number.isFinite(a)&&Number.isFinite(l)&&(p=ju(a-1,l,f,m));let w=a-1,g=(l-1)*a;return p===1/0?1:mo(w,g)?uo(w,g,p,u):Xt(Pt(u,w,g,{lowerTail:!1}),w,g,{ncp:p,lowerTail:!1})};if(G(s))s=c(t,n,r,o,i);else{let a=s;if(G(t))t=oe(l=>c(l,n,r,o,i)-a,2,100);else if(G(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=oe(l=>c(t,l,r,o,i)-a,2,1e5)}else if(G(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=oe(l=>c(t,n,r,l,i)-a,r*1e-7,r*1e7)}else G(r)?r=oe(l=>c(t,n,l,o,i)-a,o*1e-7,o*1e7):i=oe(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 Uu=["n","d","type","sigLevel","power","alternative"],Yu=["n1","n2","d","sigLevel","power","alternative"],Gu=["h","n","sigLevel","power","alternative"],Hu=["h","n1","n2","sigLevel","power","alternative"],Xu=["h","n","sigLevel","power","alternative"],Bu=["d","n","sigLevel","power","alternative"],Qu=["n","r","sigLevel","power","alternative"],Zu=["w","N","df","sigLevel","power"],Ju=["k","n","f","sigLevel","power"],ep=["u","v","f2","sigLevel","power"],tp=["size"],np={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 zc(e="p",...t){z(yn(t),tp,"cohenES");let n=vn(["size"],t).size??"small",r=be(e,["p","t","r","anov","chisq","f2"]),o=be(n,["small","medium","large"]);return{test:r,size:o,effectSize:np[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function rp(e,t){if(!po(e)||!po(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 op(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(po))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+=Oe(t[r]-e[r],e[r]));return Math.sqrt(n)}function sp(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(po)))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>=ap)o+=Oe(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 ap=2**-1022,po=e=>e>=0&&e<=1,ip="\u2018sig.level\u2019 must be numeric in [0, 1]",cp="\u2018power\u2019 must be numeric in [0, 1]";function jt(e,t){let n=r=>!G(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(ip);if(n(t))throw new RangeError(cp)}function Kt(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?zc(t,e).effectSize:e}var mr=["two.sided","less","greater"];function ho(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function $c(e,t,n,r){if(r==="less")return ln(Qt(n,e),e,t,!0);if(r==="greater")return ln(Qt(n,e,{lowerTail:!1}),e,t,!1);let o=Qt(n/2,e,{lowerTail:!1});return ln(o,e,t,!1)+ln(-o,e,t,!0)}var Vc=e=>e<1e-8?Z(e,{lowerTail:!1}):Z(1-e);function Wc(e,t,n){return n==="greater"?Y(Vc(t)-e,{lowerTail:!1}):n==="less"?Y(Z(t)-e):Y(Vc(t/2)-e,{lowerTail:!1})+Y(Z(t/2)-e)}function lp(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*=I(r,-o),n+=o}return I(t,n)}function fp(e={}){z(e,Uu,"pwrTTest");let{n:t,power:n}=e,r=Kt(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),jt(o,n),!G(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=be(e.type,["two.sample","one.sample","paired"]),i=be(e.alternative,mr),c=s==="two.sample"?2:1;i==="two.sided"&&!G(r)&&(r=Math.abs(r));let a=(m,u,p)=>$c((m-1)*c,Math.sqrt(m/c)*u,p,i);if(G(n))n=a(t,r,o);else{let m=n;G(t)?t=oe(u=>a(u,r,o)-m,2+1e-10,1e9):G(r)?r=oe(u=>a(t,u,o)-m,...ho(i,[1e-7,10])):o=oe(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 mp(e={}){z(e,Yu,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=Kt(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),jt(s,r),!G(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!G(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=be(e.alternative,mr);i==="two.sided"&&!G(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>$c(a+l-2,f/Math.sqrt(1/a+1/l),m,i);if(G(r))r=c(t,n,o,s);else{let a=r;G(t)?t=oe(l=>c(l,n,o,s)-a,2+1e-10,1e9):G(n)?n=oe(l=>c(t,l,o,s)-a,2+1e-10,1e9):G(o)?o=oe(l=>c(t,n,l,s)-a,...ho(i,[1e-7,10])):s=oe(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 Ds(e,t,n,r,o,s){let{n:i,power:c}=t,a=Kt(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(ft([a,i,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),jt(l,c),!G(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=be(t.alternative,mr);f==="two.sided"&&!G(a)&&(a=Math.abs(a));let m=(u,p,w)=>Wc(u*r(p),w,f);if(G(c))c=m(a,i,l);else{let u=c;G(a)?a=oe(p=>m(p,i,l)-u,...ho(f,[s,10])):G(i)?i=oe(p=>m(a,p,l)-u,o,1e9):l=oe(p=>m(a,i,p)-u,1e-10,1-1e-10)}return{es:a,n:i,sigLevel:l,power:c,alternative:f}}function up(e={}){z(e,Gu,"pwr2pTest");let t=Ds(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 pp(e={}){z(e,Xu,"pwrPTest");let t=Ds(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 hp(e={}){z(e,Bu,"pwrNormTest");let t=Ds(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 dp(e={}){z(e,Hu,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=Kt(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),jt(s,r),!G(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!G(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=be(e.alternative,mr);i==="two.sided"&&!G(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)=>Wc(l*Math.sqrt(c(f,m)),u,i);if(G(r))r=a(o,t,n,s);else{let l=r;G(o)?o=oe(f=>a(f,t,n,s)-l,...ho(i,[1e-10,10])):G(t)?t=oe(f=>a(o,f,n,s)-l,2+1e-10,1e9):G(n)?n=oe(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=oe(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 gp(e={}){z(e,Qu,"pwrRTest");let{n:t,power:n}=e,r=Kt(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),jt(o,n),!G(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!G(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=be(e.alternative,mr);s==="two.sided"&&!G(r)&&(r=Math.abs(r));let i=(a,l,f,m)=>{let u=Qt(m?f/2:f,a-2,{lowerTail:!1}),p=u**2+a-2,w=p===1/0&&Number.isFinite(u)?1/Math.sqrt(1+(a-2)/u/u):Math.sqrt(u**2/p),g=Math.atanh(l)+l/(2*(a-1)),d=Math.atanh(w),N=Y((g-d)*Math.sqrt(a-3));return m?N+Y((-g-d)*Math.sqrt(a-3)):N},c=(a,l,f)=>s==="two.sided"?i(a,l,f,!0):i(a,s==="less"?-l:l,f,!1);if(G(n))n=c(t,r,o),s==="less"&&(r=-r);else{let a=n;if(G(t))t=oe(l=>c(l,r,o)-a,4+1e-10,1e9);else if(G(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=oe(m=>c(t,m,o)-a,l,f)}else o=oe(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 wp(e){z(e,Zu,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=Kt(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([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(jt(s,n),!G(o)&&!(o>=0))throw new RangeError("w must be positive");if(!G(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:St(fa(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(G(n))n=i(o,t,s);else{let c=n;G(o)?o=oe(a=>i(a,t,s)-c,1e-10,1e9):G(t)?t=oe(a=>i(o,a,s)-c,1+1e-10,1e9):s=oe(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 bp(e={}){z(e,Ju,"pwrAnovaTest");let{k:t,n,power:r}=e,o=Kt(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),jt(s,r),!G(o)&&!(o>=0))throw new RangeError("f must be positive");if(!G(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!G(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,p=c*a*l**2;return!Number.isFinite(p)&&Number.isFinite(c)&&Number.isFinite(a)&&Number.isFinite(l)&&(p=lp([c,a,l,l])),p===1/0?1:mo(m,u)?uo(m,u,p,f):Xt(Pt(f,m,u,{lowerTail:!1}),m,u,{ncp:p,lowerTail:!1})};if(G(r))r=i(t,n,o,s);else{let c=r;G(t)?t=oe(a=>i(a,n,o,s)-c,2+1e-10,100):G(n)?n=oe(a=>i(t,a,o,s)-c,2+1e-10,1e9):G(o)?o=oe(a=>i(t,n,a,s)-c,1e-7,1e7):s=oe(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 Np(e={}){z(e,ep,"pwrF2Test");let{u:t,v:n,power:r}=e,o=Kt(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),jt(s,r),!G(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!G(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!G(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:mo(c,a)?uo(c,a,m,f):Xt(Pt(f,c,a,{lowerTail:!1}),c,a,{ncp:m,lowerTail:!1})};if(G(r))r=i(t,n,o,s);else{let c=r;G(t)?t=oe(a=>i(a,n,o,s)-c,1+1e-10,100):G(n)?n=oe(a=>i(t,a,o,s)-c,1+1e-10,1e9):G(o)?o=oe(a=>i(t,n,a,s)-c,1e-7,1e7):s=oe(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 vp=["pooledSd","mu","paired","ci","alternative","adjust"],yp=["pooledSd","mu","paired","ci","alternative"],Mp=["generalized","partial","ci","alternative"],Ep=["partial","ci","alternative"],xp=["partial","ci","alternative"];function $s(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var Tp="Missing values detected. NAs dropped.",Rp=`'y' is numeric but has only 2 unique values.
|
|
7
|
+
If this is a grouping variable, convert it to a factor.`,Yc={tol:0,maxiter:5e3};function Sp(e){return e===1/0?1:Math.exp(ma(.5,(e-1)/2)-.5*Math.log(e/2))}function jc(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return Zn(o=>Ar(e,t,{ncp:o})-n,e-r,e+r,{...Yc,extendInt:"downX"}).root}function Gc(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;$s(c);let a=be(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(He(f)).size===2&&m.push(Rp),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(Tp);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 p,w,g,d,N,b;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:b}=ee([He(l)]));else{let[A,_]=dn(l,f);({d:S,k:b}=Rr(A,_))}let k=S.length;if(k<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");p=Ae(S),w=Math.sqrt(qe(S)),g=w/Math.sqrt(k),d=1/k,N=k-1}else{let S=_e([He(l),He(f)]);b=S.k;let[k,A]=S.n;if(k<2||A<2)throw new RangeError("not enough observations: at least 2 are needed in each group");p=S.mean[0]-S.mean[1];let _=Math.sqrt(S.variance[0]),q=Math.sqrt(S.variance[1]);o?(w=Math.sqrt(((k-1)*_**2+(A-1)*q**2)/(k+A-2)),g=w*Math.sqrt(1/k+1/A),d=1/k+1/A,N=k+A-2):(w=Math.sqrt((_**2+q**2)/2),d=2*(A*_**2+k*q**2)/(k*A*(_**2+q**2)),g=Math.sqrt(Math.sqrt(_**2/k)**2+Math.sqrt(q**2/A)**2),N=Sr([_**2/k,q**2/A],[k-1,A-1]))}let v=r?Sp(N):1,y=I(s,-b),h=(p-y)/w*v,E=r?"Hedges_g":"Cohens_d";if(c===null)return{name:E,value:h,warnings:m};let x=a==="two.sided"?c:2*c-1,M=(p-y)/g,T=jc(M,N,(1+x)/2)*Math.sqrt(d)*v,R=jc(M,N,(1-x)/2)*Math.sqrt(d)*v;return a==="less"&&(T=-1/0),a==="greater"&&(R=1/0),{name:E,value:h,ci:c,ciLow:T,ciHigh:R,warnings:m}}function kp(e,t,n={}){return z(n,vp,"cohensD"),j("cohensD",Gc(e,t,n,n.adjust??!1))}function Ap(e,t,n={}){return z(n,yp,"hedgesG"),j("hedgesG",Gc(e,t,n,!0))}var qp=e=>Array.isArray(e.effects),Vs=e=>e.multistratum===!0,Hc=e=>Array.isArray(e.strata),Xc=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},zs=e=>({test:t=>t.split(":").includes(e)});function _p(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 Ip(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 p=t.table[u],w=e.within.filter(N=>zs(N).test(m)),g=o(w.length===0?"Subject":`Subject:${w.join(":")}`,p[l],p[f]);if(m==="(Intercept)")return;let d={name:m,df:p[a],ss:p[n]};g.terms.push(d),s.set(m,d)})}else{let a=t.colNames.indexOf("Df"),l=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[l][n],t.table[l][a]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let p={name:m,df:t.table[u][a],ss:t.table[u][n]};f.terms.push(p),s.set(m,p)})}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 Bc(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>zs(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>zs(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function Kc(e,t,n,r){let o=i=>Xt(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:Zn(o,0,Math.max(10,e*t*4),{...Yc,extendInt:"downX"}).root:0}function Qc(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=Kc(c,t,n,(1+s)/2),l=Kc(c,t,n,(1-s)/2);c<=Pt((1-s)/2,t,n)&&(l=0),c<=Pt((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 Lp(e){let t=ee([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=I(n.ss,-t)}var Uc="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Ws(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Uc);if(Vs(e)||Hc(e)||Xc(e))return Op(e,t,n);let r=qp(e)?Te(()=>vs(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Uc);let o=ze.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;$s(i);let c=be(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((T,R)=>({name:T,df:s.table[R][a],ss:s.table[R][l]})).filter(T=>T.name!=="(Intercept)");Lp(f);let m=f.find(T=>T.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(T=>T!==m),p=m.df,w=m.ss,g=w/p,d=f.reduce((T,R)=>T+R.ss,0),N=f.reduce((T,R)=>T+R.df,0)+1,b=t==="Eta2"?n.generalized??!1:!1,y=u.length===1?"plain":b!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:h,ssObserved:E}=Bc(b,u),x=T=>{let{ss:R,df:S}=T;if(t==="Eta2")return y==="partial"?R/(R+w):y==="plain"?R/d:R/(R+w+E-(h(T.name)?R:0));if(t==="Omega2"){let A=y==="partial"?(R-S*g)/(R+(N-S)*g):(R-S*g)/(d+g);return Math.max(0,A)}let k=y==="partial"?(R-S*g)/(R+w):(R-S*g)/d;return Math.max(0,k)},M=`${t}${y==="plain"?"":`_${y}`}`;return{colNames:i===null?[M]:[M,"CI","CI_low","CI_high"],rowNames:u.map(T=>T.name),table:u.map(T=>i===null?[x(T)]:Qc(x(T),T.df,p,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function Op(e,t,n){let r=n.ci===void 0?.95:n.ci;$s(r);let o=be(n.alternative??"greater",["two.sided","less","greater"]),s=Xc(e),i,c,a;if(s){let h=ze.get(e),E=Ip(h!==void 0&&!Array.isArray(h)?{...e,univariate:h}:e);({strata:i,subject:c}=E),a=E.order.map(x=>({term:x,stratum:E.strata.find(M=>M.terms.includes(x))}))}else{let h=Vs(e)?Te(()=>ys(e)):e,E=ze.get(h);({strata:i,subject:c}=_p(Array.isArray(E)?{...h,strata:E.map((x,M)=>({...h.strata[M],...x}))}:h)),a=i.flatMap(x=>x.terms.map(M=>({term:M,stratum:x})))}let l=ee([[...i.flatMap(h=>h.terms.map(E=>E.ss)),...i.map(h=>h.sse)]]).k;for(let h of i){h.sse=I(h.sse,-l);for(let E of h.terms)E.ss=I(E.ss,-l)}let f=i.flatMap(h=>h.terms),m=i.reduce((h,E)=>h+E.sse,0),u=f.reduce((h,E)=>h+E.ss,0)+m,p=c.sse/c.dfe,w=t==="Eta2"?n.generalized??!1:!1,g=w!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:N}=Bc(w,f),b=(h,E)=>{let{ss:x,df:M}=h,T=E.sse/E.dfe;if(t==="Eta2")return g==="partial"?x/(x+E.sse):g==="plain"?x/u:x/(x+m+N-(d(h.name)?x:0));if(t==="Omega2"){let S=E===c?0:1,k=g==="partial"?(x-M*T)/(x+S*E.sse+c.sse+p):(x-M*T)/(u+p);return Math.max(0,k)}let R=g==="partial"?(x-M*T)/(x+E.sse):(x-M*T)/u;return Math.max(0,R)},v=`${t}${g==="plain"?"":`_${g}`}`,y={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(h=>h.term.name),table:a.map(({term:h,stratum:E})=>{let x=b(h,E);return r===null?[x]:Qc(x,h.df,E.dfe,r,o)}),warnings:[]};return s||(y.groups=a.map(h=>h.stratum.name),y.warnings=[...Vs(e)||Hc(e)?e.warnings:[]]),y}function Fp(e,t={}){return z(t,Mp,"etaSquared"),j("etaSquared",Ws(e,"Eta2",t),e)}function Pp(e,t={}){return z(t,Ep,"omegaSquared"),j("omegaSquared",Ws(e,"Omega2",t),e)}function Cp(e,t={}){return z(t,xp,"epsilonSquared"),j("epsilonSquared",Ws(e,"Epsilon2",t),e)}var Dp=["id","dv","between","within","type","correction","es","observed"],go=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Vp=1e-12,Zc=1e-24;function Jc(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 ur=e=>typeof e=="boolean"?e?"TRUE":"FALSE":Pe(e);function zp(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(ur):[...new Set(t.map(ur))].sort(it)}var el=(e,t)=>t.split(":").includes(e);function tl(e,t,n,r,o){let s=l=>r.some(f=>el(f,l));for(let l of r)if(!e.some(f=>el(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 $p(e,t){z(t,Dp,"aovEz");let n=go(t.between),r=go(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[V,U]of[["between",n],["within",r]]){let X=U.filter((me,Le)=>U.indexOf(me)!==Le);if(X.length>0)throw new RangeError(`duplicated factor names in '${V}': ${[...new Set(X)].join(", ")}`)}let o=n.filter(V=>r.includes(V));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=go(t.es??"ges");for(let V of c)if(V!=="ges"&&V!=="pes")throw new RangeError(`invalid 'es': ${String(V)}`);let a=go(t.observed),l=[],f=V=>V==null||typeof V=="number"&&Number.isNaN(V),m=Jc(e,t.dv),u=[t.id,...n,...r].map(V=>Jc(e,V));for(let V of u)if(V.length!==m.length)throw new RangeError("the columns must have the same length");let p=m.map((V,U)=>u.every(X=>!f(X[U]))),w=V=>V.filter((U,X)=>p[X]),g=Fe(w(u[0])).map(ur),d=w(m).map(V=>{if(f(V))return NaN;if(typeof V!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(V))throw new RangeError(`the response '${t.dv}' must be finite`);return V}),N=n.map((V,U)=>Fe(w(u[1+U]))),b=r.map((V,U)=>Fe(w(u[1+n.length+U]))),v=g.length;for(let V of[d,...N,...b])if(V.length!==v)throw new RangeError("the columns must have the same length");let y=N.map(V=>zp(V)),h=N.map(V=>V.map(ur)),E=b.map(V=>V.map(ur)),x=E.map(V=>[...new Set(V)]);y.forEach((V,U)=>{if(V.length<2)throw new Error(`Factor "${n[U]}" consists of one level only. Remove factor from model?`)}),x.forEach((V,U)=>{if(V.length<2)throw new Error(`Factor "${r[U]}" consists of one level only. Remove factor from model?`)});let M=[...new Set(g)],T=new Map,R=new Set;if(g.forEach((V,U)=>{let X=JSON.stringify(h.map(Le=>Le[U])),me=T.get(V);me===void 0?T.set(V,X):me!==X&&R.add(V)}),R.size>0)throw new Error(`Following ids are in more than one between subjects condition:
|
|
8
|
+
${M.filter(V=>R.has(V)).join(", ")}`);let S=[[]];for(let V of x)S=S.flatMap(U=>V.map(X=>[...U,X]));let k=V=>JSON.stringify(E.map(U=>U[V])),A=new Map(S.map((V,U)=>[JSON.stringify(V),U])),_=new Set(g.map((V,U)=>k(U)));if(S.some(V=>!_.has(JSON.stringify(V))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let q=new Map,C=new Set;g.forEach((V,U)=>{let X=q.get(V);X===void 0&&q.set(V,X=new Set),X.add(k(U)),Number.isNaN(d[U])&&C.add(V)});let O=M.filter(V=>C.has(V)||q.get(V).size<S.length),D=new Set(O),W=ee([d.filter((V,U)=>!D.has(g[U]))]).k,F=W===0?d:d.map(V=>I(V,-W)),L=V=>I(V,2*W),$=S.length,H=new Map;if(g.forEach((V,U)=>{let X=H.get(V);X===void 0&&H.set(V,X={s:S.map(()=>[0,0]),n:S.map(()=>0)});let me=A.get(k(U));X.s[me]=Q(X.s[me],[F[U],0]),X.n[me]++}),[...H.values()].some(V=>V.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."),O.length>0){let V=[...O].sort(it),U=V.length>=10?`${V.slice(0,10).join(", ")}, ... [showing first 10 only]`:V.join(", ");l.push(`Missing values for ${O.length} ID(s), which were removed before analysis:
|
|
9
|
+
${U}`)}let te=M.filter(V=>!O.includes(V)),ne=new Map;g.forEach((V,U)=>ne.has(V)||ne.set(V,U));let ae=te.map(V=>{let U=H.get(V);return U.s.map((X,me)=>Rt(X,U.n[me]))}),Me={};n.forEach((V,U)=>Me[V]=te.map(X=>h[U][ne.get(X)]));let Ie=Object.fromEntries(n.map(V=>[V,"contr.sum"])),$e=Object.fromEntries(n.map((V,U)=>[V,y[U]])),pt="Rank deficient model matrix; insufficient data to estimate full model.";if($===1)return j("aovEz",B());return j("aovEz",J());function B(){let V="y";for(;n.includes(V);)V+="_";let U={...Me,[V]:ae.map(je=>je[0][0])},X=Te(()=>xs(`${V} ~ ${n.length>0?n.join(" * "):"1"}`,U,{contrasts:Ie,levels:$e}));if(X.rank<X.coefNames.length)throw new Error(pt);if(X.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${ae.length} subject(s) for ${X.rank} parameter(s)`);let me=Te(()=>Is(X,{type:s}));l.push(...me.warnings.filter(je=>!l.includes(je)));let Le=X.dfResidual,Xe=me.rowNames.flatMap((je,nt)=>{if(je==="(Intercept)"||je==="Residuals")return[];let[yt,_t,fn,It]=me.table[nt];return[{name:je,ss:yt,numDf:_t,errSS:X.rss,denDf:Le,f:fn,p:It}]}),Be=me.colNames.indexOf("Sum Sq"),We={anova:tl(Xe,new Map([[0,X.rss]]),c,a,L),univariate:{colNames:me.colNames,rowNames:me.rowNames,table:me.table.map(je=>je.map((nt,yt)=>yt===Be?L(nt):nt))},within:r,warnings:l};return ze.set(We,{colNames:me.colNames,rowNames:me.rowNames,table:me.table}),We}function J(){let V=te.length,U=ae.reduce((ie,ce)=>ce.reduce((he,Ue)=>he+Ue[0]*Ue[0],ie),0),X=n.length>0?Te(()=>xn(`~ ${n.join(" * ")}`,Me,{contrasts:Ie,levels:$e})):{colNames:["(Intercept)"],assign:[0],x:te.map(()=>[1]),termLabels:[],factors:[]},me=X.colNames.length;if(V===0||ve(X.x).rank<me)throw new Error(pt);let Le=V-me;if(Le<=0)throw new RangeError(`no residual degrees of freedom: ${V} subject(s) for ${me} between-subject parameter(s)`);let Xe=[0,...X.termLabels.map((ie,ce)=>ce+1)],Be=["(Intercept)",...X.termLabels],We=ie=>X.assign.flatMap((ce,he)=>ie.includes(ce)?[he]:[]),je=ie=>X.factors.flatMap((ce,he)=>ce[ie-1]>0?[he]:[]),nt=ie=>X.x.map(ce=>[ce[ie],0]),yt=new Array(V).fill([0,0]),_t=(ie,ce,he)=>{let{qtv:Ue}=qt([...ie,...ce].map(nt),yt,he);return Ue.map(zn=>zn.slice(ie.length,ie.length+ce.length))},fn=ie=>{let{qtv:ce}=qt(X.colNames.map((he,Ue)=>nt(Ue)),yt,ie);return ce.map(he=>he.slice(me))},It=Object.fromEntries(r.map((ie,ce)=>[ie,S.map(he=>he[ce])])),Mt=Te(()=>xn(`~ ${r.join(" * ")}`,It,{contrasts:Object.fromEntries(r.map(ie=>[ie,"contr.sum"])),levels:Object.fromEntries(r.map((ie,ce)=>[ie,x[ce]]))})),wr=[0,...Mt.termLabels.map((ie,ce)=>ce+1)],Dn=["(Intercept)",...Mt.termLabels],br=ie=>ie.map(ce=>ie.map(he=>{let Ue=[0,0];return ce.forEach((zn,mn)=>Ue=Q(Ue,pe(zn,he[mn]))),Ue})),Vn=[],P=[],K=[],re=[],ue=[],le=[],Ee=[],rt=new Map,Lt=!1;for(let ie of wr){let ce=Mt.assign.flatMap((Ye,Se)=>Ye===ie?[Se]:[]),he=ce.length,Ue=ce.map(Ye=>ae.map(Se=>Se.reduce((ht,ot,jn)=>Q(ht,at(ot,Mt.x[jn][Ye])),[0,0]))),zn=ce.map(Ye=>ce.map(Se=>[Mt.x.reduce((ht,ot)=>ht+ot[Ye]*ot[Se],0),0])),mn=Fc(zn),To=br(fn(Ue)),Nr=an(To,mn),$n=cn(Nr)[0],mt=NaN,Wn=NaN,Ro=NaN,ra=NaN,vr=!1;if(he>=2){let Ye=Dn[ie],Se=Ca(Ls(mn)).map(Et=>Et.map(Gt=>[Gt,0])),ht=Ls(an(an(Se,To),Ic(Se))),ot=ct(ht,{symmetric:!0,onlyValues:!0}).values,jn=ot.reduce((Et,Gt)=>Math.max(Et,Gt),0),st=Math.max(jn*Vp,U*Zc),Je=cn(Nr),dt=cn(an(Nr,Nr));jn>U*Zc&&dt[0]>0&&Number.isFinite(dt[0])?(vr=!0,mt=ke(pe(Je,Je),at(dt,he))[0],Le-he*mt>0?Wn=((Le+1)*he*mt-2)/(he*(Le-he*mt)):l.push(`The Huynh-Feldt epsilon of ${Ye} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${Ye} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),vr&&ot.every(Et=>Et>st)?(Ro=Os(an(mn,To)),ra=Fs(Ro,he,Le,$)):vr&&l.push(`The error SSP matrix of ${Ye} is singular: no Mauchly test (the epsilons are still defined)`)}for(let Ye=0;Ye<Xe.length;Ye++){let Se=Xe[Ye],ht;if(s===3)ht=_t(We(Xe.filter(Ot=>Ot!==Se)),We([Se]),Ue);else if(Se===0)ht=_t([],We([0]),Ue);else{let Ot=je(Se),Kn=Xe.filter(xt=>xt!==0&&xt!==Se&&Ot.every(Tl=>X.factors[Tl][xt-1]>0));ht=_t(We(Xe.filter(xt=>xt!==Se&&!Kn.includes(xt))),We([Se]),Ue)}let ot=cn(an(br(ht),mn))[0],st=(Se===0?1:We([Se]).length)*he,Je=Le*he,dt=ot/st/($n/Je),Et=we(dt,st,Je,!1,!1),Gt=ie===0?Be[Ye]:Se===0?Dn[ie]:`${Be[Ye]}:${Dn[ie]}`;Vn.push([L(ot),st,L($n),Je,dt,Et]),P.push([ot,st,$n,Je,dt,Et]),K.push(Gt),rt.set(ie,$n);let un={name:Gt,ss:ot,numDf:st,errSS:$n,denDf:Je,f:dt,p:Et};if(vr){let Ot=Math.min(1,Wn),Kn=we(dt,mt*st,mt*Je,!1,!1),xt=we(dt,Ot*st,Ot*Je,!1,!1);re.push([mt,Kn,Wn,xt]),ue.push([Ro,ra]),le.push(Gt),i==="GG"?un={...un,numDf:mt*st,denDf:mt*Je,p:Kn}:i==="HF"&&(Wn>1&&(Lt=!0),un=Number.isNaN(Wn)?{...un,numDf:mt*st,denDf:mt*Je,p:Kn}:{...un,numDf:Ot*st,denDf:Ot*Je,p:xt})}ie===0&&Se===0||Ee.push(un)}}Lt&&l.push("HF eps > 1 treated as 1");let Yt={anova:tl(Ee,rt,c,a,L),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:K,table:Vn},within:r,warnings:l};return re.length>0&&(Yt.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:le,table:re},Yt.sphericity={colNames:["Test statistic","p-value"],rowNames:[...le],table:ue}),ze.set(Yt,{colNames:Yt.univariate.colNames,rowNames:K,table:P}),Yt}}function wo(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=Ae(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 Wp=["rho"],jp=["group","confType","confInt"],Kp=e=>e==null||typeof e=="number"&&Number.isNaN(e);function nl(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&&Kp(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=Fe(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=wo(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=${Pe(a)}`)}}function Up(e,t,n,r={}){z(r,Wp,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=nl(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(),p=s.length,w=1;for(let v=0;v<s.length;){let y=s[v].time,h=v,E=new Array(c).fill(0),x=new Array(c).fill(0),M=0;for(;h<s.length&&s[h].time===y;){let T=s[h];x[T.group]++,T.event&&(E[T.group]++,M++),h++}if(M>0){let T=o===0?1:w**o;for(let R=0;R<c;R++)l[R]+=T*E[R],f[R]+=T*M*u[R]/p;if(p>1){let R=T*T*M*(p-M)/(p*(p-1));for(let S=0;S<c;S++)for(let k=0;k<c;k++)m[S][k]+=R*u[S]*((S===k?1:0)-u[k]/p)}w*=1-M/p}for(let T=0;T<c;T++)u[T]-=x[T];p-=h-v,v=h}let g=f.map((v,y)=>v>0?y:-1).filter(v=>v>=0),d=0,N=Math.max(g.length-1,0);if(g.length>=2){let v=g.slice(1),y=v.map(E=>l[E]-f[E]),h=v.map(E=>v.map(x=>m[E][x]));try{let E=Ve(h,y);d=y.reduce((x,M,T)=>x+M*E[T],0)}catch{({chisq:d,df:N}=Yp(h,y))}}let b=N===0?NaN:St(d,N,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:d,df:N,pvalue:b,pValue:b,warnings:[]}}function Yp(e,t){let n=ct(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 js(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 Ct(e[r],e[s])}return e[r]}function Gp(e,t,n={}){z(n,jp,"survfit");let{group:r,confType:o="log",confInt:s=.95}=n;if(o!=="log"&&o!=="plain"&&o!=="log-log")throw new RangeError(`invalid 'confType': ${String(o)}`);if(!(s>0&&s<1))throw new RangeError("'confInt' must be between 0 and 1");let{obs:i,labels:c}=nl(e,t,r),a=r===void 0?["all"]:c,l=(1-s)/2,f=l<1e-8?Z(l,{lowerTail:!1}):Z(1-(1-s)/2),m=[],u={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:m,stdError:m,lower:[],upper:[],median:[],medianRows:a};return a.forEach((p,w)=>{let g=i.filter(h=>h.group===w),d=u.time.length,N=g.length,b=1,v=0;for(let h=0;h<g.length;){let E=g[h].time,x=h,M=0;for(;x<g.length&&g[x].time===E;)g[x].event&&M++,x++;let T=x-h-M;M>0&&(b*=1-M/N,v+=M<N?M/(N*(N-M)):1/0);let R=Math.sqrt(v),S,k;if(o==="log")S=b===0?NaN:b*Math.exp(-f*R),k=b===0?NaN:Math.min(1,b*Math.exp(f*R));else if(o==="plain"){let A=f*R*b;S=Number.isNaN(A)?NaN:Math.max(0,b-A),k=Number.isNaN(A)?NaN:Math.min(1,b+A)}else if(b===0)S=NaN,k=NaN;else if(b===1)S=1,k=1;else{let A=Math.log(b),_=Math.log(-A);S=Math.exp(-Math.exp(_-f*R/A)),k=Math.exp(-Math.exp(_+f*R/A))}u.strata.push(p),u.time.push(E),u.nRisk.push(N),u.nEvent.push(M),u.nCensor.push(T),u.surv.push(b),u.stdErr.push(b*R),u.lower.push(S),u.upper.push(k),N-=x-h,h=x}let y=u.time.slice(d);u.median.push([js(y,u.surv.slice(d)),js(y,u.lower.slice(d)),js(y,u.upper.slice(d))])}),u}var Hp=["ties","weights","strata","init","control","nocenter","confLevel"],Xp=["eps","iterMax","tolerChol","tolerInf"],Bp=e=>e==null||typeof e=="number"&&Number.isNaN(e),Qp=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,rl=700;function Ks(e,t,n){let{time:r,event:o,weight:s,x:i,stratumEnd:c}=e,a=r.length,l=t.length,f=()=>new Array(l).fill(0),m=()=>Array.from({length:l},f),u=f(),p=m(),w=0,g=0,d=f(),N=m(),b=f(),v=m(),y=0,h=0,E=x=>{let M=Math.exp(h-x);g*=M,y*=M;for(let T=0;T<l;T++){d[T]*=M,b[T]*=M;let R=N[T],S=v[T];for(let k=0;k<=T;k++)R[k]*=M,S[k]*=M}h=x};for(let x=a-1;x>=0;){c[x]&&(h=0,g=0,d=f(),N=m());let M=r[x],T=0,R=0;y=0;let S=x;do{let k=i[S],A=0;for(let C=0;C<l;C++)A+=t[C]*k[C];let _=s[S];Number.isFinite(A)&&(A-h>rl?E(A):A-h<-rl&&g===0&&y===0&&(h=A));let q=Math.exp(A-h)*_;if(o[S]){T++,R+=_,y+=q,w+=_*A;for(let C=0;C<l;C++){u[C]+=_*k[C],b[C]+=q*k[C];let O=v[C];for(let D=0;D<=C;D++)O[D]+=q*k[C]*k[D]}}else{g+=q;for(let C=0;C<l;C++){d[C]+=q*k[C];let O=N[C];for(let D=0;D<=C;D++)O[D]+=q*k[C]*k[D]}}S--}while(S>=0&&r[S]===M&&!c[S]);if(T>0){let k=n?T:1,A=n?T:1,_=R/k;for(let q=0;q<k;q++){g+=n?y/A:y,w-=_*(Math.log(g)+h);for(let C=0;C<l;C++){d[C]+=n?b[C]/A:b[C];let O=d[C]/g;u[C]-=_*O;let D=N[C],W=v[C],F=p[C];for(let L=0;L<=C;L++)D[L]+=n?W[L]/A:W[L],F[L]+=_*(D[L]-O*d[L])/g}}b.fill(0);for(let q of v)q.fill(0)}x=S}for(let x=0;x<l;x++)for(let M=0;M<x;M++)p[M][x]=p[x][M];return{loglik:w,score:u,info:p}}function Us(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 Ys(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 Gs(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 bo=(e,t)=>({statistic:e,df:t,pValue:St(e,t,{lowerTail:!1})});function Zp(e,t){let n=e.length,r=()=>new Array(t).fill(NaN),o=bo(0,0),s=new Array(t).fill(0),i=r(),c=r();return{coefficients:r(),var:Array.from({length:t},()=>new Array(t).fill(0)),loglik:[0,0],score:0,wald:0,iter:0,means:Array.from({length:t},(a,l)=>Ae(e.map(f=>f[l]))),n,nevent:0,linearPredictors:new Array(n).fill(0),se:s,z:i,p:c,stdError:s,zValue:i,pValue:c,lower:r(),upper:r(),logtest:o,sctest:{...o},waldtest:{...o},rsq:{rsq:0,maxrsq:0},warnings:[]}}function Jp(e,t,n,r={}){z(r,Hp,"coxph"),z(Ft(r,"control"),Xp,"coxph","control");let{ties:o="efron",weights:s,strata:i,init:c,control:a={},nocenter:l=[-1,0,1],confLevel:f=.95}=r,{eps:m=1e-9,iterMax:u=20,tolerChol:p=2**-39}=a,w=a.tolerInf??Math.sqrt(m);if(o!=="efron"&&o!=="breslow")throw new RangeError(`invalid 'ties': ${String(o)}`);if(!(m>0&&m<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(p>0&&p<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(w>0&&w<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(u)&&u>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(f>0&&f<1))throw new RangeError("'confLevel' must be between 0 and 1");let g=e.length;if(t.length!==g||n.length!==g)throw new RangeError("'time', 'status' and 'x' must have the same length");if(s!==void 0&&s.length!==g)throw new RangeError("'weights' must have the same length as 'time'");if(i!==void 0&&i.length!==g)throw new RangeError("'strata' must have the same length as 'time'");let d=g===0?0:n[0].length;if(d<1)throw new RangeError("'x' must have at least one column");if(n.some(P=>P.length!==d))throw new RangeError("the rows of 'x' must have the same length");if(c!==void 0&&c.length!==d)throw new RangeError("'init' must have one value per column of 'x'");if(c?.some(P=>!Number.isFinite(P)))throw new RangeError("'init' must contain finite values");let N=[];for(let P=0;P<g;P++){if(Number.isNaN(e[P])||Number.isNaN(t[P])||n[P].some(re=>Number.isNaN(re))||s!==void 0&&Number.isNaN(s[P])||i!==void 0&&Bp(i[P]))continue;let K=t[P];if(K!==0&&K!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[P]===-1/0||e[P]===1/0&&K===1)throw new RangeError("event times must be finite");N.push(P)}let b=N.length;if(b===0)throw new RangeError("no observations without missing values");if(N.some(P=>n[P].some(K=>!Number.isFinite(K))))throw new RangeError("'x' contains infinite values");let v=N.map(P=>s===void 0?1:s[P]);if(v.some(P=>!(P>0)))throw new RangeError("Invalid weights, must be >0");if(v.some(P=>!Number.isInteger(P)))throw new RangeError("non-integer weights (robust variance) are not supported yet");let y=0;for(let P of N)y+=t[P];if(y===0)return Zp(N.map(P=>n[P]),d);let h=wo(N.map(P=>e[P])),E=new Array(b).fill(0);if(i!==void 0){let P=Fe(i),K=Array.from(new Set(N.map(ue=>P[ue]))).sort(Qp),re=new Map;K.forEach((ue,le)=>re.set(ue,le)),E=N.map(ue=>re.get(P[ue]))}let x=Array.from({length:b},(P,K)=>K).sort((P,K)=>E[P]-E[K]||h[P]-h[K]||P-K),M=0;for(let P of v)M+=P;let T=x.map(P=>v[P]),R=x.map(P=>n[N[P]].slice()),S=new Array(d).fill(0),k=new Array(d).fill(1),A=new Array(d).fill(0),_=new Set(l);for(let P=0;P<d;P++){if(R.every(le=>_.has(le[P])))continue;let K=R.map(le=>le[P]),re=le=>{let Ee=0;for(let ce=0;ce<b;ce++)Ee+=T[ce]*le[ce];let rt=Ee/M,Lt=0,Yt=le.map((ce,he)=>(Lt+=T[he]*Math.abs(ce-rt),ce-rt)),ie=Lt===0?1:M/Lt;return{m:rt,s:ie,d:Yt,ok:Number.isFinite(rt)&&ie>=2**-1022&&ie<1/0}},ue=re(K);if(!ue.ok){let{groups:le,k:Ee}=ee([K]);ue=re(le[0]),A[P]=Ee}for(let le=0;le<b;le++)R[le][P]=ue.d[le]*ue.s;S[P]=I(ue.m,A[P]),k[P]=ue.s}let q={time:x.map(P=>h[P]),event:x.map(P=>t[N[P]]===1),weight:T,x:R,stratumEnd:x.map((P,K)=>K===b-1||E[x[K+1]]!==E[P])},C=o==="efron",O=c===void 0?new Array(d).fill(0):c.map((P,K)=>I(P/k[K],A[K])),D=Ks(q,O,C),W=[D.loglik,D.loglik],F=D.info.map(P=>P.slice()),L=Us(F,p),$=Ys(F,D.score),H=0;for(let P=0;P<d;P++)H+=D.score[P]*$[P];let te=0,ne,ae=!1;if(u===0||!Number.isFinite(D.loglik))ne=Gs(F);else{let P=D.loglik,K=O.map((le,Ee)=>le+$[Ee]),re=0,ue=!1;for(te=1;te<=u;te++){D=Ks(q,K,C),F=D.info.map(Ee=>Ee.slice()),L=Us(F,p);let le=Number.isFinite(D.loglik)&&D.score.every(Number.isFinite)&&D.info.every(Ee=>Ee.every(Number.isFinite));if(le&&(D.loglik===P||Math.abs(1-P/D.loglik)<=m)){ue=!0,W[1]=D.loglik,O=K;break}!le||D.loglik<P?(re++,K=K.map((Ee,rt)=>(Ee+re*O[rt])/(re+1))):(re=0,P=D.loglik,W[1]=P,$=Ys(F,D.score),O=K,K=K.map((Ee,rt)=>Ee+$[rt]))}ue?ne=Gs(F):(te=u+1,ae=!0,u>1?(D=Ks(q,O,C),W[1]=D.loglik,ne=D.info.map((le,Ee)=>le.map((rt,Lt)=>Ee===Lt?le[Ee]>0?1/le[Ee]:0:-0))):ne=Gs(F))}let Me=k.map(P=>Math.floor(Math.log2(P))),Ie=k.map((P,K)=>I(P,-Me[K])),$e=Me.map((P,K)=>P-A[K]),pt=O.map((P,K)=>P*Ie[K]),B=D.score.map((P,K)=>P/Ie[K]),J=ne.map((P,K)=>P.map((re,ue)=>re*Ie[K]*Ie[ue])),V=pt.map((P,K)=>I(P,$e[K])),U=J.map((P,K)=>P.map((re,ue)=>I(re,$e[K]+$e[ue]))),X=V.slice();if(u>0&&Math.abs(L)<d)for(let P=0;P<d;P++)ne[P][P]===0&&(X[P]=NaN);let me=0;for(let P=0;P<d;P++)me+=V[P]*S[P];let Le=N.map(P=>{let K=0;for(let re=0;re<d;re++)K+=n[P][re]*V[re];return K-me}),Xe=new Re;if(u>1){let P=J.map((K,re)=>{let ue=0;for(let le=0;le<d;le++)ue+=B[le]*J[le][re];return Math.abs(ue)});if(ae)Xe.add("Ran out of iterations and did not converge"),(ge(Le)>500||P.some(K=>!Number.isFinite(K)))&&Xe.add("one or more coefficients may be infinite");else{let K=[];for(let re=0;re<d;re++){let ue=P[re];(!Number.isFinite(B[re])||I(ue,$e[re])>m&&ue>w*Math.abs(pt[re]))&&K.push(re+1)}K.length>0&&Xe.add(`Loglik converged before variable ${K.join(",")} ; coefficient may be infinite. `)}}let Be=X.map((P,K)=>Number.isNaN(P)?-1:K).filter(P=>P>=0),We=Be.length,je=Be.map(P=>pt[P]-(c===void 0?0:I(c[P],-$e[P]))),nt=0;if(We===1)nt=je[0]*je[0]/J[Be[0]][Be[0]];else if(We>1){let P=Be.map(re=>Be.map(ue=>J[re][ue]));Us(P,p);let K=Ys(P,je);for(let re=0;re<We;re++)nt+=je[re]*K[re]}let yt=(1-f)/2,_t=yt<1e-8?Z(yt,{lowerTail:!1}):Z((1+f)/2),fn=J.map((P,K)=>Math.sqrt(P[K])),It=fn.map((P,K)=>I(P,$e[K])),Mt=X.map((P,K)=>(Number.isNaN(P)?P:pt[K])/fn[K]),wr=Mt.map(P=>St(P*P,1,{lowerTail:!1})),Dn=X.map((P,K)=>Math.exp(P-_t*It[K])),br=X.map((P,K)=>Math.exp(P+_t*It[K])),Vn=-2*(W[0]-W[1]);return j("coxph",{coefficients:X,var:U,loglik:W,score:H,wald:nt,iter:te,means:S,n:b,nevent:y,linearPredictors:Le,se:It,z:Mt,p:wr,stdError:It,zValue:Mt,pValue:wr,lower:Dn,upper:br,logtest:bo(Vn,We),sctest:bo(H,We),waldtest:bo(nt,We),rsq:{rsq:1-Math.exp(-Vn/b),maxrsq:1-Math.exp(2*W[0]/b)},warnings:Xe.list})}var eh=["center"],th=2**-1022;function nh(e,t,n={}){z(n,eh,"leveneTest");let r=be(n.center??"median",["median","mean"]),{groups:o}=$t(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"?Hr(l):Ae(l);return l.map(m=>Math.abs(m-f))},i=ee(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<th)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let p=ee([u]);return{d:s(p.groups[0]),k:p.k}}),f=Math.max(...l.map(u=>u.d.some(p=>p>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(p=>I(p,u.k-m)))}let a=Nc(nn(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 rh=["score"],oh=["alternative","exact","variant","levels"];function sh(e,t,...n){z(yn(n),rh,"propTrendTest");let{score:r}=vn(["score"],n),o=Array.from(e),s=Array.from(t),i=o.length,c=r===void 0?o.map((b,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(b=>!(b>0&&b<1/0)))throw new RangeError("elements of 'n' must be positive and finite");if(o.some((b,v)=>!(b>=0&&b<=s[v])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(b=>b===0)||o.every((b,v)=>b===s[v]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let a=s.reduce((b,v)=>b+v,0),l=o.reduce((b,v)=>b+v,0)/a,f=s.map(b=>b/(l*(1-l))),m=o.map((b,v)=>b/s[v]),u=c.flatMap((b,v)=>Number.isNaN(b)?[]:[v]);if(u.length===0)throw new RangeError("no non-missing scores");if(u.some(b=>!Number.isFinite(c[b])))throw new RangeError("non-missing scores must be finite");if(u.every(b=>c[b]===c[u[0]]))throw new RangeError("at least 2 distinct non-missing scores are needed");let p=c,{sw:w,sxy:g,sxx:d}=Hs(f,m,p,u);if(!(d>=2**-1022&&d<1/0&&Math.abs(g)<1/0)){let b=ee([u.map(v=>c[v])]);b.k!==0&&(p=c.slice(),u.forEach((v,y)=>p[v]=b.groups[0][y]),{sw:w,sxy:g,sxx:d}=Hs(f,m,p,u))}let N=Oe(g,d);if(!(a<1/0)||l>0&&l<1&&!(w<1/0&&Math.abs(g)<1/0&&d<1/0)){let{groups:[b,v],k:y}=ee([o,s]),h=b.reduce((x,M)=>x+M,0)/v.reduce((x,M)=>x+M,0),E=Hs(v,m,p,u);N=I(E.sxy/h*(E.sxy/E.sxx/(1-h)),y)}return{method:"Chi-squared Test for Trend in Proportions",statistic:N,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:xe(N,.5,2,!1,!1),warnings:[]}}function Hs(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 ah=2**24,ih=2**32,ch=`Sample size > 100 or data with ties
|
|
10
|
+
p-value based on normal approximation. Specify nperm for permutation p-value`,lh=`Sample data with ties
|
|
11
|
+
p-value based on normal approximation. Specify nperm for permutation p-value.`;function fh(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=Fe(t));let c;if(n!==void 0)c=n.map(Pe);else{let f=[...new Set(i.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(Pe):[...new Set(f.map(Pe))].sort(it)}let a=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of i){let m=l.get(Pe(t[f]));if(m===void 0)throw new RangeError(`the group '${Pe(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function mh(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 uh(e,t,n={}){z(n,oh,"jonckheereTest");let r=be(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=fh(e,t,n.levels,o==="clinfun");if(s.filter(b=>b.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((b,v)=>{m-=b.length,f.push([b.length,m]);for(let y=v+1;y<s.length;y++)for(let h of b)for(let E of s[y])l+=h<E?1:h===E?.5:0});let u=[],p=!a&&c<=100;o==="DescTools"&&n.exact!==void 0?(p=n.exact,p&&a&&(p=!1,u.push(lh))):p||u.push(ch);let w,g;if(p){let b=1,v=0;for(let[h,E]of f)h===0||E===0||(v+=b*(h*E+1),b+=h*E);if(b>ah||v>ih)throw new RangeError(`the exact distribution of JT has ${b} values and needs ${v} steps, too many: use exact: false`);let y=[1];for(let[h,E]of f){if(h===0||E===0)continue;let x=Array.from({length:h*E+1},(T,R)=>Ma(R,h,E,!1)),M=new Array(y.length+x.length-1).fill(0);y.forEach((T,R)=>x.forEach((S,k)=>M[R+k]+=T*S)),y=M}w=0,g=0,y.forEach((h,E)=>{E<=l&&(w+=h),E>=l&&(g+=h)})}else{let b=f.reduce((h,[E,x])=>h+E*x,0)/2,v=a?mh(s.map(h=>h.length),i):f.reduce((h,[E,x])=>h+E*x*(E+x+1)/12,0),y=(l-b)/Math.sqrt(v);w=Y(y),g=1-w,g<1e-8&&(g=Y(y,{lowerTail:!1}))}let N=r==="increasing"?g:r==="decreasing"?w:Math.min(1,2*Math.min(g,w,o==="clinfun"?.5:1));return j("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:N,warnings:u})}var ph=["nClasses","adjust"];function pr(e){return He(Array.from(e)).sort((t,n)=>t-n)}function ol(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function No(e){ol(e);let t=ga(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function hh(e){let t=pr(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=No(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(Y(r[a-1],{logP:!0})+Y(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 dh(e){let t=pr(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=No(t),o=1/(12*n);r.forEach((a,l)=>{o+=(Y(a)-(2*(l+1)-1)/(2*n))**2});let s=(1+.5/n)*o,i=[],c;return s<.0275?c=1-Math.exp(-13.953+775.5*s-12542.61*s**2):s<.051?c=1-Math.exp(-5.903+179.546*s-1515.29*s**2):s<.092?c=Math.exp(.886-31.62*s+10.897*s**2):s<1.1?c=Math.exp(1.111-34.242*s+12.832*s**2):(c=737e-12,i.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),j("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function gh(e){let t=pr(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=No(t).map(f=>Y(f)),o=-1/0,s=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),s=Math.max(s,f-m/n)});let 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 wh(e){let t=pr(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(ol(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>Z((u+1-3/8)/(n+1/4))),o=ee([t]).groups[0],s=Math.min(da(o,r)**2/(qe(o)*qe(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=Y((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function bh(e,t={}){z(t,ph,"pearsonTest");let n=pr(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 No(n)){let m=Math.min(o,Math.floor(1+o*Y(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:xe(a,l/2,2,!1,!1),warnings:[]}}var Nh=["weight"],vh=["exact"],yh=["model","type","unit","r0","confLevel"];function hr(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 vo(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 al=e=>2*Y(-Math.abs(e));function Mh(e,t={}){z(t,Nh,"kappa2");let n=be(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=>Nn(x[0])),i=r.map(x=>Nn(x[1])),c=[...new Set([...s,...i])].sort((x,M)=>x<M?-1:x>M?1:0),a=c.length,l=new Map(c.map((x,M)=>[x,M])),f=Array.from({length:a},()=>new Array(a).fill(0));for(let x=0;x<o;x++)f[l.get(s[x])][l.get(i[x])]++;let m=x=>n==="unweighted"?x===0?1:0:n==="equal"?1-x/(a-1):1-x*x/((a-1)*(a-1)),u=Array.from({length:a},(x,M)=>Array.from({length:a},(T,R)=>m(Math.abs(M-R)))),p=f.map(x=>se(x)/o),w=Array.from({length:a},(x,M)=>se(f.map(T=>T[M]))/o),g=x=>{let M=[];for(let T=0;T<a;T++)for(let R=0;R<a;R++)M.push(x(T,R));return se(M)},d=g((x,M)=>f[x][M]*u[x][M])/o,N=g((x,M)=>p[x]*w[M]*u[x][M]),b=(d-N)/(1-N),v=Array.from({length:a},(x,M)=>se(w.map((T,R)=>T*u[M][R]))),y=Array.from({length:a},(x,M)=>se(p.map((T,R)=>T*u[R][M]))),h=(g((x,M)=>p[x]*w[M]*(u[x][M]-(v[x]+y[M]))**2)-N*N)/(o*(1-N)**2),E=b/Math.sqrt(h);return{value:b,statistic:E,pValue:al(E),warnings:[]}}function Eh(e,t={}){z(t,vh,"kappamFleiss");let n=t.exact??!1,r=hr(e,"ratings");vo(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=Fe(i),a=[...new Set(c)].sort((h,E)=>h-E),l=new Map(a.map((h,E)=>[h,E])),f=new Map(i.map((h,E)=>[h,l.get(c[E])])),m=a.length,u=e.map(h=>{let E=new Array(m).fill(0);for(let x of h)E[f.get(x)]++;return E}),p=se(u.map(h=>(se(h.map(E=>E*E))-s)/(s*(s-1))))/o,w=Array.from({length:m},(h,E)=>se(u.map(x=>x[E]))/(o*s)),g=se(w.map(h=>h*h));if(n){let h=r.map(x=>{let M=new Array(m).fill(0);for(let T of x)M[f.get(T)]++;return M.map(T=>T/o)}),E=Array.from({length:m},(x,M)=>qe(h.map(T=>T[M]))*(s-1)/s);g-=se(E)/(s-1)}let d=(p-g)/(1-g);if(n)return{value:d,warnings:[]};let N=se(w.map(h=>h*(1-h))),b=se(w.map(h=>h*(1-h)*(1-h-h))),v=2/(N*N*o*s*(s-1))*(N*N-b),y=d/Math.sqrt(v);return{value:d,statistic:y,pValue:al(y),warnings:[]}}function xh(e,t={}){z(t,yh,"icc");let n=be(t.model,["oneway","twoway"]),r=be(t.type,["consistency","agreement"]),o=be(t.unit,["single","average"]),s=t.r0??0,i=t.confLevel??.95;Qe(i),hr(e,"ratings"),vo(e,"ratings");let c=ee(e).groups,a=hr(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=qe(c.map(F=>Ae(F)))*f,p=qe(a.map(F=>Ae(F)))*l,w=Ae(c.map(F=>qe(F))),g=qe(c.flat())*(l*f-1),d=g-u*(l-1)-p*(f-1),N=d>2**-20*g?d/((l-1)*(f-1)):kh(c)/((l-1)*(f-1)),b=(F,L,$)=>we(F,L,$,!1,!1),v=(F,L)=>m/2<1e-8?kt(m/2,F,L,!1,!1):kt(1-m/2,F,L,!0,!1),y=l-1;if(n==="oneway"||r==="consistency"){let F=n==="oneway"?w:N,L=n==="oneway"?l*(f-1):(l-1)*(f-1),$=u/F,H=$/v(y,L),te=$*v(L,y),[ne,ae,Me]=o==="single"?[(u-F)/(u+(f-1)*F),$*(1-s)/(1+(f-1)*s),Ie=>(Ie-1)/(Ie+f-1)]:[(u-F)/u,$*(1-s),Ie=>1-1/Ie];return{value:ne,Fvalue:ae,fValue:ae,df1:y,df2:L,pValue:b(ae,y,L),lbound:Me(H),ubound:Me(te),warnings:[]}}let h=o==="single"?f:1,E=F=>[h*F/(l*(1-F)),1+h*F*(l-1)/(l*(1-F))],x=(F,L)=>wa([F*p,L*N],[f-1,(l-1)*(f-1)]),M=o==="single"?(u-N)/(u+(f-1)*N+f/l*(p-N)):(u-N)/(u+(p-N)/l),[T,R]=E(s),S=u/(T*p+R*N),k=x(T,R),[A,_]=[f*M/(l*(1-M)),1+f*M*(l-1)/(l*(1-M))],q=x(A,_),C=v(y,q),O=v(q,y),[D,W]=o==="single"?[l*(u-C*N)/(C*(f*p+(f*l-f-l)*N)+l*u),l*(O*u-N)/(f*p+(f*l-f-l)*N+l*O*u)]:[l*(u-C*N)/(C*(p-N)+l*u),l*(O*u-N)/(p-N+l*O*u)];return{value:M,Fvalue:S,fValue:S,df1:y,df2:k,pValue:b(S,y,k),lbound:D,ubound:W,warnings:[]}}function sl(e,t){let n=t.length,r=se(t.flatMap(s=>t.map(i=>e[s][i])));return(1-se(t.map(s=>e[s][s]))/r)*n/(n-1)}function Th(e){let t=hr(e,"x").length;if(vo(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=zr(ee(e).groups),r=Jn(e),o=Array.from({length:t},(c,a)=>a),s=se(r.flat()),i;try{let c=Ve(r);i=o.map(a=>1-1/c[a][a])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let a=ee(e).groups;i=o.map(l=>{let f=a.map(p=>p[l]),m=En(ve(a.map(p=>[1,...p.filter((w,g)=>g!==l)])),f),u=Ae(f);return 1-se(m.map(p=>p*p))/se(f.map(p=>(p-u)**2))})}return j("cronbachAlpha",{rawAlpha:sl(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-se(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>sl(n,o.filter(a=>a!==c))),warnings:Rh(e,r)})}function Rh(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}=ct(r.map(c=>r.map(a=>t[c][a])),{symmetric:!0}),s=o.map(c=>c[0]),i=se(s)<0?-1:1;s.some(c=>i*c<0)&&n.push(`Some items were negatively correlated with the first principal component and probably
|
|
13
12
|
should be reversed.
|
|
14
|
-
To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function
|
|
13
|
+
To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function Sh(e,t,n){let r=hr(e,"x").length;vo(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=Jn(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=Ve(a,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=se(i.map((p,w)=>o[s[m]][p]*f[w][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function kh(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>Q(l,[f,0]),[0,0]),o=e.map(a=>Rt(r(a),n)),s=e[0].map((a,l)=>Rt(r(e.map(f=>f[l])),t)),i=Rt(r(e.flat()),t*n),c=[0,0];return e.forEach((a,l)=>a.forEach((f,m)=>{let u=Q(de(de([f,0],o[l]),s[m]),i);c=Q(c,pe(u,u))})),c[0]+c[1]}function yo(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(!$a(Oa(e)))throw new RangeError("'covmat' must be symmetric")}var Ah=["covmat","nObs","rotation","scores","control"],qh=["lower","start"];function ye(e){let t=0,n=0;for(let r of e){let[o,s]=Tt(t,r);t=o,n+=s}return t+n}var Xs=(e,t)=>e[0].map((n,r)=>ye(e.map(o=>t(o[r]))));function ll(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 il(e,t,n){let r=ct(ll(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 cl(e,t){let n=Xs(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=Xs(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 vt=(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})),On=e=>e[0].map((t,n)=>e.map(r=>r[n]));function Mo(e){let t=e[0].length,n=e.map(m=>Math.sqrt(ye(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},(p,w)=>u===w?1:0));if(s===0)return{loadings:e.map(m=>m.slice()),rotmat:i};let c=On(o),a=0;for(let m=0;m<1e3;m++){let u=vt(o,i),p=u[0].map((b,v)=>{let y=0;for(let h=0;h<s;h++)y+=u[h][v]*u[h][v];return y}),w=u.map(b=>b.map((v,y)=>v**3-v*p[y]/s)),g=vt(c,w),d=Pr(g);i=vt(d.u,On(d.v));let N=a;if(a=ye(d.d),a<N*(1+1e-5))break}let l=vt(o,i),f=e.map(m=>m.map(()=>0));return r.forEach((m,u)=>f[m]=l[u].map(p=>p*n[m])),{loadings:f,rotmat:i}}function _h(e){let t=Mo(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=Mn(ve(n),r),s=Ve(vt(On(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:vt(n,i),rotmat:vt(t.rotmat,i)}}function Ih(e,t,n={}){z(n,Ah,"factanal"),z(Ft(n,"control"),qh,"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");yo(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=zr(e),s=e.length}let i=o.length;if(i<3)throw new RangeError("factor analysis requires at least three variables");let c=t;if(!Number.isInteger(c)||c<1)throw new RangeError("'factors' must be a positive integer");let a=((i-c)**2-i-c)/2;if(a<0)throw new RangeError(`${c} ${c===1?"factor is":"factors are"} too many for ${i} variables`);let l=o.map((x,M)=>Math.sqrt(x[M])),f=o.map((x,M)=>x.map((T,R)=>T/(l[M]*l[R]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??Ve(f).map((x,M)=>(1-c/(2*i))/x[M]),p=x=>{let M=ct(ll(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(ye(M.map(T=>Math.log(T)-T))-c+i)},w=x=>{let M=il(f,x,c);return x.map((T,R)=>{let S=0;for(let k=0;k<c;k++)S+=M[R][k]*M[R][k];return(S+T-f[R][R])/(T*T)})},g=Te(()=>er(u,p,{gr:w,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(g.convergence!==0)throw new Error("unable to optimize from this starting value");let d=g.par,N=cl(il(f,d,c)).L,b,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"?Mo(N):_h(N),M=cl(x.loadings,x.rotmat);N=M.L,b=M.rot}let y=NaN,h=NaN;!Number.isNaN(s)&&a>0&&(y=(s-1-(2*i+5)/6-2*c/3)*g.value,h=St(y,a,{lowerTail:!1}));let E={uniquenesses:d,loadings:N,STATISTIC:y,dof:a,PVAL:h,converged:!0,objective:g.value,counts:g.counts,warnings:[]};return r!=="none"&&(E.scores=Lh(e,f,d,N,r)),b!==void 0&&(E.rotmat=b),E}function Lh(e,t,n,r,o){let s=e.length,i=e[0].map((m,u)=>ye(e.map(p=>p[u]))/s),c=e.map(m=>m.map((u,p)=>u-i[p])),a=Xs(c,m=>m*m).map(m=>Math.sqrt(m/(s-1))),l=c.map(m=>m.map((u,p)=>u/a[p]));if(o==="regression")return vt(l,Ve(t,r));let f=r.map((m,u)=>m.map(p=>p/n[u]));return vt(l,On(Ve(vt(On(r),f),On(f))))}var Oh=["nfactors","covmat"],Fn=2**-52,Fh=`
|
|
15
14
|
Three factors are required for identification -- general factor loadings set to be equal.
|
|
16
15
|
Proceed with caution.
|
|
17
16
|
Think about redoing the analysis with alternative values of the 'option' setting.
|
|
18
17
|
|
|
19
|
-
`,
|
|
20
|
-
`),Ut(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let L=Dh(i);if(L.converged||Ut(s,"oblimin did not converge in 2000 iterations"),a=L.loadings,l=L.phi,n===2){let z=l[0][1],X=Math.sqrt(Math.abs(z));f=[X,Ro(z)*X],Ut(c,Oh)}else f=ll(l,1,s,c).map(z=>z[0])}let m=a.map(L=>{let z=0;for(let X=0;X<n;X++)z+=L[X]*f[X];return z}),u=fl(i,L=>L*L),p=u.map((L,z)=>r[z][z]-L),g=a.map((L,z)=>L.map((X,te)=>{if(n===1)return 0;let ne=r[z][z],ae=ne-(ne-X*X)-(X*f[te])**2;return ae<0&&(ae=0),Ro(X)*Math.sqrt(ae)})),w=m.map((L,z)=>L*L/u[z]),h=m.map((L,z)=>{let X=[L,...g[z]];return Me(X.map(te=>te*te))**2/Me(X.map(te=>te**4))}),N=m.map(Ro);N.some(L=>L<0)&&(r=r.map((L,z)=>L.map((X,te)=>N[z]*X*N[te])));let M=m.map((L,z)=>N[z]*L),v=g.map((L,z)=>L.map(X=>N[z]*X)),b=M.map((L,z)=>[L,...v[z],u[z],p[z],w[z],h[z]]),d=Vn(r),E=Me(r.map((L,z)=>L[z])),x=Me(M)**2,y=Me(p),R=x/d,T=(d-y)/d,k=x/(d-y),S=(d-E)/d*o/(o-1),A=(d+Me(ml(r,c))-E)/d,I=Nr(b.map(L=>L.slice(0,n+1)),L=>L*L),_=I[0]/Me(I),$=v.map(L=>{let z=0;for(let X=1;X<n;X++)Math.abs(L[X])>Math.abs(L[z])&&(z=X);return z}),P=[],C=[];for(let L=0;L<n;L++){let z=$.map((ae,ye)=>ye).filter(ae=>$[ae]===L),X=Me(z.flatMap(ae=>z.map(ye=>r[ye][ae]))),te=Me(z.map(ae=>v[ae][L])),ne=Me(z.map(ae=>M[ae]));C.push(te*te),P.push([(te*te+ne*ne)/X,ne*ne/X,te*te/X])}let W=[[T,Me(M)**2/d,Me(C)/d],...P],F={omegaH:R,omegaLim:k,alpha:S,omegaTot:T,G6:A,ECV:_,sl:b,slNames:["g",...Array.from({length:n},(L,z)=>`F${z+1}*`),"h2","u2","p2","com"],orthog:i,oblique:a,gloading:f,key:N,omegaGroup:W,warnings:s,messages:c};return l!==void 0&&(F.phi=l),j("omega",F)}var ul=["ML","stdErr"],Ch=["ML","stdErr"],Ze=.9999;function Js(e){let t=se(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(Q(r/t));return n}var ea=e=>[...new Set(e)].sort((t,n)=>t-n),pl=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,hl=e=>Array.isArray(e)||ArrayBuffer.isView(e),dl=(e,t)=>e>0?U(e,{lowerTail:!1})-U(t,{lowerTail:!1}):U(t)-U(e);function ta(e){let t=ee([e]).groups[0],n=Ae(t),r=t.map(s=>s-n),o=Math.sqrt(qe(r));return r.map(s=>s/o)}function gl(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 To=e=>Array.from({length:e},()=>new Array(e).fill(0));function vl(e,t,n,r,o){e.value+=t*Math.log(n);let s=r.length;for(let c=0;c<s;c++){e.gradient[c]+=t*r[c]/n;for(let i=0;i<s;i++)e.hessian[c][i]+=t*(o[c][i]/n-r[c]*r[i]/(n*n))}}var Zs={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function $h(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Zs;if(e===1/0)return{...Zs,k:Be(t),kk:-t*Be(t)};if(t===1/0)return{...Zs,h:Be(e),hh:-e*Be(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),s=e*e-2*n*e*t+t*t,c=Math.exp(-s/(2*r))/(2*Math.PI*o),i=(t-n*e)/o,a=(e-n*t)/o,l=Be(e),f=Be(t);return{r:c,h:l*U(i),k:f*U(a),rr:c*(n/r+(e*t*r-n*s)/(r*r)),rh:-c*(e-n*t)/r,rk:-c*(t-n*e)/r,hh:-e*l*U(i)-n/o*l*Be(i),hk:c,kk:-t*f*U(a)-n/o*f*Be(a)}}function wl(e,t,n,r,o){let s=e.length,c=e[0].length,i=o?s+c-1:1,a={value:0,gradient:new Array(i).fill(0),hessian:To(i)},l=p=>o&&p>=1&&p<=s-1?p:-1,f=p=>o&&p>=1&&p<=c-1?s-1+p:-1,m=t.map(p=>n.map(g=>$h(p,g,r))),u=[];for(let p=0;p<s;p++)for(let g=0;g<c;g++){let w=e[p][g];if(w===0)continue;let h=Qn([t[p],n[g]],[t[p+1],n[g+1]],r),N=new Array(i).fill(0),M=To(i);for(let[v,b,d]of[[p+1,g+1,1],[p,g+1,-1],[p+1,g,-1],[p,g,1]]){let E=m[v][b],x=l(v),y=f(b);N[0]+=d*E.r,M[0][0]+=d*E.rr,x>=0&&(N[x]+=d*E.h,M[0][x]+=d*E.rh,M[x][0]+=d*E.rh,M[x][x]+=d*E.hh),y>=0&&(N[y]+=d*E.k,M[0][y]+=d*E.rk,M[y][0]+=d*E.rk,M[y][y]+=d*E.kk),x>=0&&y>=0&&(M[x][y]+=d*E.hk,M[y][x]+=d*E.hk)}vl(a,w,h,N,M),u.push(w*Math.log(h))}return a.value=se(u),a}function zh(e,t,n,r,o){let s=o?n.length+1:1,c={value:0,gradient:new Array(s).fill(0),hessian:To(s)},i=(1-r)*(1+r),a=Math.sqrt(i),l=i*a,f=new Array(s),m=To(s),u=[],p=(g,w,h)=>{let N=n[g],M=(N-r*w)/a,v=Be(M),b=(r*N-w)/l,d=1/a,E=N/l+3*r*(r*N-w)/(l*i),x=r/l;if(f[0]+=h*v*b,m[0][0]+=h*v*(E-M*b*b),o){let y=g+1;f[y]+=h*v*d;let R=h*v*(x-M*b*d);m[0][y]+=R,m[y][0]+=R,m[y][y]+=h*v*(-M*d*d)}};for(let g=0;g<e.length;g++){let w=e[g],h=t[g];f.fill(0);for(let v of m)v.fill(0);let N=h<n.length?(n[h]-r*w)/a:1/0,M=h>0?(n[h-1]-r*w)/a:-1/0;h<n.length&&p(h,w,1),h>0&&p(h-1,w,-1),vl(c,1,dl(M,N),f,m),u.push(Math.log(dl(M,N)),Be(w,{log:!0}))}return c.value=se(u),c}function Wh(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(c=>e.gradient[c]),o=t.map(c=>t.map(i=>-e.hessian[c][i])),s;try{s=Ce(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||se(s.map((c,i)=>c*r[i]))<=0)&&(s=r.map((c,i)=>c/Math.max(Math.abs(o[i][i]),1))),t.forEach((c,i)=>n[c]=s[i]),n}var Nl=(e,t)=>e>=t-1e-12*Math.abs(t);function bl(e,t,n){let r=[...t],o=e(r),s=n,c=r.map((i,a)=>a);for(let i=0;i<200;i++){let a=s?c.slice(1):c;if(a.length===0)break;let l=Wh(o,a);if(!s&&Math.abs(r[0])===Ze&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>Ze?(Math.sign(l[0])*Ze-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let p=r.map((w,h)=>w+f*l[h]);f*l[0]!==0&&Math.abs(p[0])>=Ze*(1-1e-15)&&(p[0]=Math.sign(p[0])*Ze);let g=e(p);if(Nl(g.value,o.value)){let w=ge(p.map((h,N)=>Math.abs(h-r[N])/Math.max(1,Math.abs(r[N]))));r=p,o=g,m=w>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function Ml(e,t){let n=bl(e,[Math.max(-Ze,Math.min(Ze,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=bl(e,[Math.sign(n.theta[0])*Ze,...n.theta.slice(1)],!0);return Nl(r.at.value,n.at.value)?r:n}function yl(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=Ce(e.map(o=>o.map(s=>-s)))}catch{return n()}return r.map((o,s)=>o.map((c,i)=>(c+r[i][s])/2))}var El=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function jh(e,t,n){Ve(t)&&V(t,ul,"polychor"),V(n,ul,"polychor");let r,o;if(hl(t)){let P=e,C=t;if(P.length!==C.length)throw new RangeError("'x' and 'y' must have the same length");let W=P.map((te,ne)=>!Number.isNaN(te)&&!Number.isNaN(C[ne])),F=Fe(P.filter((te,ne)=>W[ne])),L=Fe(C.filter((te,ne)=>W[ne])),z=new Map(ea(F).map((te,ne)=>[te,ne])),X=new Map(ea(L).map((te,ne)=>[te,ne]));r=Array.from({length:z.size},()=>new Array(X.size).fill(0)),F.forEach((te,ne)=>r[z.get(te)][X.get(L[ne])]++),o=n??{}}else{let P=e;if(!P.every(hl))throw new TypeError("give a table (array of rows) or two vectors");let C=P[0]?.length??0;if(P.some(W=>W.length!==C))throw new TypeError("the table must have rows of equal length");if(r=P.map(W=>Array.from(W)),r.some(W=>W.some(F=>!(F>=0&&F<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:c=!1}=o,i=[],a=0;se(r.map(P=>se(P)))<2**1e3||({groups:r,k:a}=ee(r));let l=r.map(P=>se(P)),f=l.filter(P=>P===0).length;f>0&&(i.push(pl(f,"row")),r=r.filter((P,C)=>l[C]!==0));let m=(r[0]??[]).map((P,C)=>se(r.map(W=>W[C]))),u=m.filter(P=>P===0).length;u>0&&(i.push(pl(u,"column")),r=r.map(P=>P.filter((C,W)=>m[W]!==0)));let p=r.length,g=r[0]?.length??0;if(p<2&&i.push("the table has fewer than 2 rows"),g<2&&i.push("the table has fewer than 2 columns"),p<2||g<2)return j("polychor",{rho:NaN,warnings:i});let w=[-1/0,...Js(r.map(P=>se(P))),1/0],h=[-1/0,...Js(r[0].map((P,C)=>se(r.map(W=>W[C])))),1/0],M=Oa(P=>{let C=Math.max(-Ze,Math.min(Ze,P)),W=[];for(let F=0;F<p;F++)for(let L=0;L<g;L++){if(r[F][L]===0)continue;let z=Qn([w[F],h[L]],[w[F+1],h[L+1]],C);W.push(r[F][L]*Math.log(z))}return-se(W)},-Ze,Ze).optimum;if(!s&&!c)return j("polychor",{rho:M,warnings:i});let v=P=>[-1/0,...P,1/0],b=w.slice(1,p),d=h.slice(1,g),E=P=>s?wl(r,v(P.slice(1,p)),v(P.slice(p)),P[0],!0):wl(r,w,h,P[0],!1),{theta:x,at:y,bounded:R}=Ml(E,s?[M,...b,...d]:[M]),T=x[0];if(R&&i.push(El(T)),!c)return j("polychor",{rho:T,warnings:i});let k=s?v(x.slice(1,p)):w,S=s?v(x.slice(p)):h,A=se(r.map(P=>se(P))),I=[],_=[];for(let P=0;P<p;P++)for(let C=0;C<g;C++){let W=r[P][C],F=Qn([k[P],S[C]],[k[P+1],S[C+1]],T);if(W===0)I.push(A*F);else if(a===0)I.push(W*Math.log1p(1e-6/W),W*gl(A*F/W-1));else{let L=q(W,a);_.push(L*Math.log1p(1e-6/L)),I.push(W*gl(A*F/W-1))}}let $=yl(y.hessian,R);return j("polychor",{rho:T,warnings:i,var:a===0?$:$.map(P=>P.map(C=>q(C,-a))),chisq:a===0?2*se(I):q(2*se(I),a)+2*se(_),df:p*g-p-g,n:q(A,a),rowCuts:k.slice(1,p),colCuts:S.slice(1,g)})}function Kh(e,t,n,r){let o=e.length,s=[Q(.25),Q(.5),Q(.75)],c=(p,g)=>g.filter(w=>w<p).length,i=ta(e).map(p=>c(p,n)),a=ta(t).map(p=>c(p,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));i.forEach((p,g)=>l[p][a[g]]++);let f=[-1/0,...n,1/0],m=[-1/0,...s,1/0],u=[];return l.forEach((p,g)=>p.forEach((w,h)=>{if(w===0)return;let N=Qn([f[g],m[h]],[f[g+1],m[h+1]],r);u.push(w*Math.log((w+1e-6)/(N*o)))})),2*se(u)}function Uh(e,t,n={}){if(V(n,Ch,"polyserial"),e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,s=e.map((y,R)=>!Number.isNaN(y)&&!Number.isNaN(t[R])),c=e.filter((y,R)=>s[R]),i=Fe(t.filter((y,R)=>s[R])),a=c.length,l=ea(i);if(l.length<2)return j("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((y,R)=>[y,R+1])),m=i.map(y=>f.get(y)),u=l.map(()=>0);for(let y of m)u[y-1]++;let p=Js(u),g=[],w=Math.sqrt((a-1)/a)*Math.sqrt(qe(m))*Pa(c,m)[0]/se(p.map(y=>Be(y)));if(Math.abs(w)>Ze){let y=Math.sign(w)*Ze;g.push(`initial correlation inadmissible, ${vn(w)}, set to ${vn(y)}`),w=y}if(!r&&!o)return j("polyserial",{rho:w,warnings:g});let h=ta(c),N=m.map(y=>y-1),M=y=>zh(h,N,r?y.slice(1):p,y[0],r),{theta:v,at:b,bounded:d}=Ml(M,r?[w,...p]:[w]);if(d&&g.push(El(v[0])),!o)return j("polyserial",{rho:v[0],warnings:g});let E=r?v.slice(1):p,x=l.length;return j("polyserial",{rho:v[0],warnings:g,var:yl(b.hessian,d),chisq:Kh(m,h,E,r?v[0]:w),df:4*x-x-1,n:a,cuts:E})}export{Is as Anova,Gm as TukeyHSD,ph as adTest,ps as anova,ji as ansariTest,Km as aov,$p as aovEz,hs as aovSummary,di as bartlettTest,sf as binomTest,bm as brunnerMunzelTest,Sl as chisqTest,vf as cochranQTest,Cc as cohenES,kp as cohensD,zm as confint,Tl as corTest,Zp as coxph,xh as cronbachAlpha,hh as cvmTest,jf as dscfAllPairsTest,Gf as dunnTest,zf as dunnettTest,Pp as epsilonSquared,np as esH,rp as esW1,op as esW2,Op as etaSquared,Ih as factanal,Vl as fisherTest,bi as flignerTest,Xo as friedmanTest,Kf as gamesHowellTest,Hf as glhtDunnett,pu as glm,qu as glmAnova,Tu as glmConfint,yu as glmCooksDistance,Ss as glmHatvalues,xu as glmPredict,vu as glmResiduals,Mu as glmRstandard,Ts as glmSummary,Sp as hedgesG,Eh as icc,mh as jonckheereTest,Mh as kappa2,yh as kappamFleiss,Qr as kruskalTest,Qi as ksTest,Cf as kwAllPairsDunnTest,$f as kwManyOneDunnTest,th as leveneTest,dh as lillieTest,ws as lm,tm as mantelhaenTest,Wf as manyOneUTest,Pu as mauchlyTest,df as mcnemarTest,xn as modelMatrix,vi as moodTest,Vh as omega,Fp as omegaSquared,Ii as onewayTest,Tn as pAdjust,Sm as pairwisePropTest,Tm as pairwiseTTest,km as pairwiseWilcoxTest,Th as partialR,wh as pearsonTest,uf as poissonTest,jh as polychor,Uh as polyserial,ju as powerAnovaTest,zu as powerPropTest,$u as powerTTest,Wm as predict,Uo as propTest,oh as propTrendTest,hp as pwr2p2nTest,mp as pwr2pTest,wp as pwrAnovaTest,gp as pwrChisqTest,bp as pwrF2Test,pp as pwrNormTest,up as pwrPTest,dp as pwrRTest,fp as pwrT2nTest,lp as pwrTTest,wi as quadeTest,Rl as setWarningHandler,gh as sfTest,Bf as shapiroTest,Kp as survdiff,Gp as survfit,Rn as tTest,Uf as tukeyTest,hi as varTest,or as wilcoxTest,Ym as zTest};
|
|
18
|
+
`,Pn=(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})),dr=e=>e[0].map((t,n)=>e.map(r=>r[n])),gr=(e,t=n=>n)=>e[0].map((n,r)=>ye(e.map(o=>t(o[r])))),ml=(e,t=n=>n)=>e.map(n=>ye(n.map(t))),Cn=(e,t=n=>n)=>ye(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),Eo=e=>e<0?-1:1;function Zs(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 ul(e,t){let n=Zs(e),{d:r,u:o,v:s}=Pr(n),i=r.map((a,l)=>l).filter(a=>r[a]>Math.sqrt(Fn)*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)&&Ut(t,"In smc, smcs > 1 were set to 1.0"),c.some(a=>a<0)&&Ut(t,"In smc, smcs < 0 were set to .0"),c.map(a=>a>1?1:a<0?0:a)}var Ut=(e,t)=>{e.includes(t)||e.push(t)};function Bs(e,t,n,r){let o=e.map((c,a)=>c.map((l,f)=>a===f?t[a]:l)),s=ct(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 Ph=e=>Pn(e,dr(e));function fl(e,t,n,r){let o=Zs(e),s=o.length,i=ul(o,r),c=d=>{let N=Bs(o,d.map(y=>1-y),t,y=>y<Fn?100*Fn:y),b=Ph(N),v=[];for(let y=0;y<s;y++)for(let h=y+1;h<s;h++)v.push((o[h][y]-b[h][y])**2);return ye(v)},a=d=>{let N=Bs(o,d.map((b,v)=>o[v][v]-b),t,b=>Math.max(b,0));return d.map((b,v)=>{let y=0;for(let h=0;h<t;h++)y+=N[v][h]*N[v][h];return y+b-o[v][v]})},l=d=>{let N=ct(o.map((R,S)=>R.map((k,A)=>S===A?1-d[S]:k)),{symmetric:!0}),b=N.values,v=N.vectors,y=b.map(R=>R>=Fn),h=b.map(R=>R<Fn?100*Fn:R),E=o.map((R,S)=>R.map((k,A)=>{if(S===A)return 0;let _=0;for(let q=0;q<t;q++)_+=h[q]*v[S][q]*v[A][q];return k-_})),x=Pn(E,v),M=(R,S)=>{let k=0;for(let A=0;A<s;A++)k+=v[A][R]*x[A][S];return k},T=new Array(s).fill(0);for(let R=0;R<t;R++){let S=(k,A)=>{if(A===0)return;let _=A*M(R,k);for(let q=0;q<s;q++)T[q]+=_*v[q][R]*v[q][k]};y[R]&&S(R,1);for(let k=R+1;k<t;k++)S(k,2*(y[R]&&y[k]?1:(h[R]-h[k])/(b[R]-b[k])));for(let k=t;k<s;k++)S(k,2*h[R]/(b[R]-b[k]))}return T},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(ge(i),1)},u=new Array(s).fill(.01),p=Te(()=>er(i.map(d=>1-d),c,{gr:a,...m,control:{fnscale:1,parscale:u}}));p.par.some(d=>d===f)&&(p=Te(()=>er(p.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),p.convergence!==0&&Ut(n,`minres did not converge (optim convergence code ${p.convergence}): examine the results carefully`);let w=p.par,g=Bs(o,w.map((d,N)=>o[N][N]-d),t,d=>Math.max(d,0));if(g=Qs(g,t),g=g.map(d=>d.map(N=>N===0?1e-15:N)),ml(g,d=>d*d).some(d=>d>1)&&Ut(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){g=Qs(Mo(g).loadings,t);let d=gr(g,b=>b*b),N=d.map((b,v)=>v).sort((b,v)=>d[v]-d[b]||b-v);g=g.map(b=>N.map(v=>b[v]))}else g=Qs(g,t);return g}function Qs(e,t){let n=gr(e).map(Eo);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 Ch(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:Cn(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 Dh(e){let t=e[0].length,n=l=>{let f=Ve(l),m=Pn(e,dr(f)),{f:u,Gq:p}=Ch(m),w=dr(Pn(Pn(dr(m),p),f)).map(g=>g.map(d=>-d));return{L:m,f:u,G:w}},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=gr(r.map((d,N)=>d.map((b,v)=>b*s.G[N][v]))),m=s.G.map((d,N)=>d.map((b,v)=>b-r[N][v]*f[v])),u=Math.sqrt(Cn(m,d=>d*d));if(c.push(s.f),u<1e-5){a=!0;break}if(i!==void 0){let d=i,N=r.map((v,y)=>v.map((h,E)=>h-d.T[y][E])),b=m.map((v,y)=>v.map((h,E)=>h-d.Gp[y][E]));Cn(b,v=>v*v)>0&&(o=Cn(N,v=>v*v)/Math.abs(Cn(N.map((v,y)=>v.map((h,E)=>h*b[y][E])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let p=ge(c.slice(-10)),w=s,g=r;for(let d=0;d<=10;d++){let N=r.map((v,y)=>v.map((h,E)=>h-o*m[y][E])),b=gr(N,v=>v*v).map(v=>1/Math.sqrt(v));if(g=N.map(v=>v.map((y,h)=>y*b[h])),w=n(g),p-w.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=g,s=w}return{loadings:s.L,phi:Pn(dr(r),r),converged:a}}function Vh(e,t={}){z(t,Oh,"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)yo(t.covmat),r=Zs(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Jn(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=fl(r,n,s,i),a,l,f;if(n===1)a=c.map(L=>L.slice()),f=[1],Ut(i,`Omega_h for 1 factor is not meaningful, just omega_t
|
|
19
|
+
`),Ut(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let L=Dh(c);if(L.converged||Ut(s,"oblimin did not converge in 2000 iterations"),a=L.loadings,l=L.phi,n===2){let $=l[0][1],H=Math.sqrt(Math.abs($));f=[H,Eo($)*H],Ut(i,Fh)}else f=fl(l,1,s,i).map($=>$[0])}let m=a.map(L=>{let $=0;for(let H=0;H<n;H++)$+=L[H]*f[H];return $}),u=ml(c,L=>L*L),p=u.map((L,$)=>r[$][$]-L),w=a.map((L,$)=>L.map((H,te)=>{if(n===1)return 0;let ne=r[$][$],ae=ne-(ne-H*H)-(H*f[te])**2;return ae<0&&(ae=0),Eo(H)*Math.sqrt(ae)})),g=m.map((L,$)=>L*L/u[$]),d=m.map((L,$)=>{let H=[L,...w[$]];return ye(H.map(te=>te*te))**2/ye(H.map(te=>te**4))}),N=m.map(Eo);N.some(L=>L<0)&&(r=r.map((L,$)=>L.map((H,te)=>N[$]*H*N[te])));let b=m.map((L,$)=>N[$]*L),v=w.map((L,$)=>L.map(H=>N[$]*H)),y=b.map((L,$)=>[L,...v[$],u[$],p[$],g[$],d[$]]),h=Cn(r),E=ye(r.map((L,$)=>L[$])),x=ye(b)**2,M=ye(p),T=x/h,R=(h-M)/h,S=x/(h-M),k=(h-E)/h*o/(o-1),A=(h+ye(ul(r,i))-E)/h,_=gr(y.map(L=>L.slice(0,n+1)),L=>L*L),q=_[0]/ye(_),C=v.map(L=>{let $=0;for(let H=1;H<n;H++)Math.abs(L[H])>Math.abs(L[$])&&($=H);return $}),O=[],D=[];for(let L=0;L<n;L++){let $=C.map((ae,Me)=>Me).filter(ae=>C[ae]===L),H=ye($.flatMap(ae=>$.map(Me=>r[Me][ae]))),te=ye($.map(ae=>v[ae][L])),ne=ye($.map(ae=>b[ae]));D.push(te*te),O.push([(te*te+ne*ne)/H,ne*ne/H,te*te/H])}let W=[[R,ye(b)**2/h,ye(D)/h],...O],F={omegaH:T,omegaLim:S,alpha:k,omegaTot:R,G6:A,ECV:q,sl:y,slNames:["g",...Array.from({length:n},(L,$)=>`F${$+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:N,omegaGroup:W,warnings:s,messages:i};return l!==void 0&&(F.phi=l),j("omega",F)}var pl=["ML","stdErr"],zh=["ML","stdErr"],Ze=.9999;function ea(e){let t=se(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(Z(r/t));return n}var ta=e=>[...new Set(e)].sort((t,n)=>t-n),hl=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,dl=e=>Array.isArray(e)||ArrayBuffer.isView(e),gl=(e,t)=>e>0?Y(e,{lowerTail:!1})-Y(t,{lowerTail:!1}):Y(t)-Y(e);function na(e){let t=ee([e]).groups[0],n=Ae(t),r=t.map(s=>s-n),o=Math.sqrt(qe(r));return r.map(s=>s/o)}function wl(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 xo=e=>Array.from({length:e},()=>new Array(e).fill(0));function vl(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 Js={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function $h(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Js;if(e===1/0)return{...Js,k:Ge(t),kk:-t*Ge(t)};if(t===1/0)return{...Js,h:Ge(e),hh:-e*Ge(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=Ge(e),f=Ge(t);return{r:i,h:l*Y(c),k:f*Y(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*Y(c)-n/o*l*Ge(c),hk:i,kk:-t*f*Y(a)-n/o*f*Ge(a)}}function bl(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:xo(c)},l=p=>o&&p>=1&&p<=s-1?p:-1,f=p=>o&&p>=1&&p<=i-1?s-1+p:-1,m=t.map(p=>n.map(w=>$h(p,w,r))),u=[];for(let p=0;p<s;p++)for(let w=0;w<i;w++){let g=e[p][w];if(g===0)continue;let d=Hn([t[p],n[w]],[t[p+1],n[w+1]],r),N=new Array(c).fill(0),b=xo(c);for(let[v,y,h]of[[p+1,w+1,1],[p,w+1,-1],[p+1,w,-1],[p,w,1]]){let E=m[v][y],x=l(v),M=f(y);N[0]+=h*E.r,b[0][0]+=h*E.rr,x>=0&&(N[x]+=h*E.h,b[0][x]+=h*E.rh,b[x][0]+=h*E.rh,b[x][x]+=h*E.hh),M>=0&&(N[M]+=h*E.k,b[0][M]+=h*E.rk,b[M][0]+=h*E.rk,b[M][M]+=h*E.kk),x>=0&&M>=0&&(b[x][M]+=h*E.hk,b[M][x]+=h*E.hk)}vl(a,g,d,N,b),u.push(g*Math.log(d))}return a.value=se(u),a}function Wh(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:xo(s)},c=(1-r)*(1+r),a=Math.sqrt(c),l=c*a,f=new Array(s),m=xo(s),u=[],p=(w,g,d)=>{let N=n[w],b=(N-r*g)/a,v=Ge(b),y=(r*N-g)/l,h=1/a,E=N/l+3*r*(r*N-g)/(l*c),x=r/l;if(f[0]+=d*v*y,m[0][0]+=d*v*(E-b*y*y),o){let M=w+1;f[M]+=d*v*h;let T=d*v*(x-b*y*h);m[0][M]+=T,m[M][0]+=T,m[M][M]+=d*v*(-b*h*h)}};for(let w=0;w<e.length;w++){let g=e[w],d=t[w];f.fill(0);for(let v of m)v.fill(0);let N=d<n.length?(n[d]-r*g)/a:1/0,b=d>0?(n[d-1]-r*g)/a:-1/0;d<n.length&&p(d,g,1),d>0&&p(d-1,g,-1),vl(i,1,gl(b,N),f,m),u.push(Math.log(gl(b,N)),Ge(g,{log:!0}))}return i.value=se(u),i}function jh(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=Ve(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||se(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 yl=(e,t)=>e>=t-1e-12*Math.abs(t);function Nl(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=jh(o,a);if(!s&&Math.abs(r[0])===Ze&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>Ze?(Math.sign(l[0])*Ze-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let p=r.map((g,d)=>g+f*l[d]);f*l[0]!==0&&Math.abs(p[0])>=Ze*(1-1e-15)&&(p[0]=Math.sign(p[0])*Ze);let w=e(p);if(yl(w.value,o.value)){let g=ge(p.map((d,N)=>Math.abs(d-r[N])/Math.max(1,Math.abs(r[N]))));r=p,o=w,m=g>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function Ml(e,t){let n=Nl(e,[Math.max(-Ze,Math.min(Ze,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Nl(e,[Math.sign(n.theta[0])*Ze,...n.theta.slice(1)],!0);return yl(r.at.value,n.at.value)?r:n}function El(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=Ve(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 xl=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function Kh(e,t,n){De(t)&&z(t,pl,"polychor"),z(n,pl,"polychor");let r,o;if(dl(t)){let O=e,D=t;if(O.length!==D.length)throw new RangeError("'x' and 'y' must have the same length");let W=O.map((te,ne)=>!Number.isNaN(te)&&!Number.isNaN(D[ne])),F=Fe(O.filter((te,ne)=>W[ne])),L=Fe(D.filter((te,ne)=>W[ne])),$=new Map(ta(F).map((te,ne)=>[te,ne])),H=new Map(ta(L).map((te,ne)=>[te,ne]));r=Array.from({length:$.size},()=>new Array(H.size).fill(0)),F.forEach((te,ne)=>r[$.get(te)][H.get(L[ne])]++),o=n??{}}else{let O=e;if(!O.every(dl))throw new TypeError("give a table (array of rows) or two vectors");let D=O[0]?.length??0;if(O.some(W=>W.length!==D))throw new TypeError("the table must have rows of equal length");if(r=O.map(W=>Array.from(W)),r.some(W=>W.some(F=>!(F>=0&&F<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:i=!1}=o,c=[],a=0;se(r.map(O=>se(O)))<2**1e3||({groups:r,k:a}=ee(r));let l=r.map(O=>se(O)),f=l.filter(O=>O===0).length;f>0&&(c.push(hl(f,"row")),r=r.filter((O,D)=>l[D]!==0));let m=(r[0]??[]).map((O,D)=>se(r.map(W=>W[D]))),u=m.filter(O=>O===0).length;u>0&&(c.push(hl(u,"column")),r=r.map(O=>O.filter((D,W)=>m[W]!==0)));let p=r.length,w=r[0]?.length??0;if(p<2&&c.push("the table has fewer than 2 rows"),w<2&&c.push("the table has fewer than 2 columns"),p<2||w<2)return j("polychor",{rho:NaN,warnings:c});let g=[-1/0,...ea(r.map(O=>se(O))),1/0],d=[-1/0,...ea(r[0].map((O,D)=>se(r.map(W=>W[D])))),1/0],b=_a(O=>{let D=Math.max(-Ze,Math.min(Ze,O)),W=[];for(let F=0;F<p;F++)for(let L=0;L<w;L++){if(r[F][L]===0)continue;let $=Hn([g[F],d[L]],[g[F+1],d[L+1]],D);W.push(r[F][L]*Math.log($))}return-se(W)},-Ze,Ze).optimum;if(!s&&!i)return j("polychor",{rho:b,warnings:c});let v=O=>[-1/0,...O,1/0],y=g.slice(1,p),h=d.slice(1,w),E=O=>s?bl(r,v(O.slice(1,p)),v(O.slice(p)),O[0],!0):bl(r,g,d,O[0],!1),{theta:x,at:M,bounded:T}=Ml(E,s?[b,...y,...h]:[b]),R=x[0];if(T&&c.push(xl(R)),!i)return j("polychor",{rho:R,warnings:c});let S=s?v(x.slice(1,p)):g,k=s?v(x.slice(p)):d,A=se(r.map(O=>se(O))),_=[],q=[];for(let O=0;O<p;O++)for(let D=0;D<w;D++){let W=r[O][D],F=Hn([S[O],k[D]],[S[O+1],k[D+1]],R);if(W===0)_.push(A*F);else if(a===0)_.push(W*Math.log1p(1e-6/W),W*wl(A*F/W-1));else{let L=I(W,a);q.push(L*Math.log1p(1e-6/L)),_.push(W*wl(A*F/W-1))}}let C=El(M.hessian,T);return j("polychor",{rho:R,warnings:c,var:a===0?C:C.map(O=>O.map(D=>I(D,-a))),chisq:a===0?2*se(_):I(2*se(_),a)+2*se(q),df:p*w-p-w,n:I(A,a),rowCuts:S.slice(1,p),colCuts:k.slice(1,w)})}function Uh(e,t,n,r){let o=e.length,s=[Z(.25),Z(.5),Z(.75)],i=(p,w)=>w.filter(g=>g<p).length,c=na(e).map(p=>i(p,n)),a=na(t).map(p=>i(p,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));c.forEach((p,w)=>l[p][a[w]]++);let f=[-1/0,...n,1/0],m=[-1/0,...s,1/0],u=[];return l.forEach((p,w)=>p.forEach((g,d)=>{if(g===0)return;let N=Hn([f[w],m[d]],[f[w+1],m[d+1]],r);u.push(g*Math.log((g+1e-6)/(N*o)))})),2*se(u)}function Yh(e,t,n={}){if(z(n,zh,"polyserial"),e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,s=e.map((M,T)=>!Number.isNaN(M)&&!Number.isNaN(t[T])),i=e.filter((M,T)=>s[T]),c=Fe(t.filter((M,T)=>s[T])),a=i.length,l=ta(c);if(l.length<2)return j("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((M,T)=>[M,T+1])),m=c.map(M=>f.get(M)),u=l.map(()=>0);for(let M of m)u[M-1]++;let p=ea(u),w=[],g=Math.sqrt((a-1)/a)*Math.sqrt(qe(m))*La(i,m)[0]/se(p.map(M=>Ge(M)));if(Math.abs(g)>Ze){let M=Math.sign(g)*Ze;w.push(`initial correlation inadmissible, ${Nn(g)}, set to ${Nn(M)}`),g=M}if(!r&&!o)return j("polyserial",{rho:g,warnings:w});let d=na(i),N=m.map(M=>M-1),b=M=>Wh(d,N,r?M.slice(1):p,M[0],r),{theta:v,at:y,bounded:h}=Ml(b,r?[g,...p]:[g]);if(h&&w.push(xl(v[0])),!o)return j("polyserial",{rho:v[0],warnings:w});let E=r?v.slice(1):p,x=l.length;return j("polyserial",{rho:v[0],warnings:w,var:El(y.hessian,h),chisq:Uh(m,d,E,r?v[0]:g),df:4*x-x-1,n:a,cuts:E})}export{Is as Anova,nu as TukeyHSD,hh as adTest,vs as anova,Bi as ansariTest,eu as aov,$p as aovEz,ys as aovSummary,Mi as bartlettTest,wf as binomTest,Am as brunnerMunzelTest,Al as chisqTest,qf as cochranQTest,zc as cohenES,kp as cohensD,Qm as confint,Sl as corTest,Jp as coxph,Th as cronbachAlpha,dh as cvmTest,em as dscfAllPairsTest,rm as dunnTest,Zf as dunnettTest,Cp as epsilonSquared,rp as esH,op as esW1,sp as esW2,Fp as etaSquared,Ih as factanal,Vl as fisherTest,Ti as flignerTest,es as friedmanTest,tm as gamesHowellTest,sm as glhtDunnett,hu as glm,_u as glmAnova,Su as glmConfint,Eu as glmCooksDistance,As as glmHatvalues,Tu as glmPredict,vu as glmResiduals,Mu as glmRstandard,Ss as glmSummary,Ap as hedgesG,xh as icc,uh as jonckheereTest,Mh as kappa2,Eh as kappamFleiss,Xr as kruskalTest,sc as ksTest,Bf as kwAllPairsDunnTest,Qf as kwManyOneDunnTest,nh as leveneTest,gh as lillieTest,xs as lm,um as mantelhaenTest,Jf as manyOneUTest,Cu as mauchlyTest,Rf as mcnemarTest,xn as modelMatrix,Ri as moodTest,Vh as omega,Pp as omegaSquared,Di as onewayTest,Sn as pAdjust,Vm as pairwisePropTest,Cm as pairwiseTTest,Dm as pairwiseWilcoxTest,Sh as partialR,bh as pearsonTest,Ef as poissonTest,Kh as polychor,Yh as polyserial,Ku as powerAnovaTest,Wu as powerPropTest,$u as powerTTest,Zm as predict,Bo as propTest,sh as propTrendTest,dp as pwr2p2nTest,up as pwr2pTest,bp as pwrAnovaTest,wp as pwrChisqTest,Np as pwrF2Test,hp as pwrNormTest,pp as pwrPTest,gp as pwrRTest,mp as pwrT2nTest,fp as pwrTTest,xi as quadeTest,Rl as setWarningHandler,wh as sfTest,im as shapiroTest,Up as survdiff,Gp as survfit,Rn as tTest,nm as tukeyTest,yi as varTest,tr as wilcoxTest,ru as zTest};
|