@numero-uno-jpn/rstats 0.15.1 → 0.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.ja.md +8 -8
  3. package/README.md +8 -8
  4. package/dist/chunks/chunk-2CUKVVRO.js +2 -0
  5. package/dist/chunks/{chunk-3QTVXDUM.js → chunk-3WQCKGKP.js} +2 -2
  6. package/dist/chunks/{chunk-4ILI2FSB.js → chunk-4Z2XDWKV.js} +1 -1
  7. package/dist/chunks/{chunk-5XQZNKHZ.js → chunk-AJBLCNZ7.js} +2 -2
  8. package/dist/chunks/chunk-D4F7GQAY.js +2 -0
  9. package/dist/chunks/chunk-EBQMGCQZ.js +2 -0
  10. package/dist/chunks/{chunk-5OSQ76OO.js → chunk-HBMHR5SS.js} +2 -2
  11. package/dist/chunks/{chunk-7J4HW6VF.js → chunk-IBPWRQIF.js} +2 -2
  12. package/dist/chunks/chunk-JLAH3X77.js +2 -0
  13. package/dist/chunks/chunk-LG74QY5U.js +2 -0
  14. package/dist/chunks/{chunk-735NNFAS.js → chunk-PEYGUK32.js} +2 -2
  15. package/dist/chunks/{chunk-H4CHJMFU.js → chunk-PWVQVJTP.js} +1 -1
  16. package/dist/chunks/{chunk-LY2T6DA6.js → chunk-Q4PGYESP.js} +2 -2
  17. package/dist/chunks/{chunk-OLPTFKIZ.js → chunk-RCAL4VHM.js} +2 -2
  18. package/dist/chunks/{chunk-IQOVOHZG.js → chunk-SREPSVTT.js} +2 -2
  19. package/dist/chunks/chunk-UFT662QZ.js +2 -0
  20. package/dist/chunks/{chunk-Z3C3G5XI.js → chunk-VWJ74UHC.js} +2 -2
  21. package/dist/chunks/{chunk-SAZAEPZC.js → chunk-XNPU54NH.js} +1 -1
  22. package/dist/chunks/{chunk-5SQFDLNG.js → chunk-XPLDEK6D.js} +2 -2
  23. package/dist/chunks/{chunk-VOIJKNL2.js → chunk-YGT4P7EU.js} +1 -1
  24. package/dist/chunks/{chunk-GEJCBCMP.js → chunk-YVREYVI2.js} +2 -2
  25. package/dist/descriptive.js +2 -2
  26. package/dist/distributions/arcsine.js +48 -4
  27. package/dist/distributions/beta.d.ts +1 -1
  28. package/dist/distributions/beta.js +64 -1
  29. package/dist/distributions/exponential.js +11 -2
  30. package/dist/distributions/f.js +104 -21
  31. package/dist/distributions/gamma.d.ts +13 -2
  32. package/dist/distributions/gamma.js +91 -12
  33. package/dist/distributions/geometric.js +16 -3
  34. package/dist/distributions/invgamma.js +56 -10
  35. package/dist/distributions/kumaraswamy.js +98 -17
  36. package/dist/distributions/lognormal.js +28 -4
  37. package/dist/distributions/nbeta.js +540 -37
  38. package/dist/distributions/nchisq.d.ts +25 -0
  39. package/dist/distributions/nchisq.js +330 -92
  40. package/dist/distributions/negbinomial.js +20 -5
  41. package/dist/distributions/normal.js +3 -0
  42. package/dist/distributions/nt.js +263 -51
  43. package/dist/distributions/pareto.js +68 -19
  44. package/dist/distributions/t.js +48 -10
  45. package/dist/distributions/triangular.js +66 -27
  46. package/dist/distributions/tukey.js +94 -50
  47. package/dist/distributions/uniform.js +12 -9
  48. package/dist/distributions/weibull.js +43 -8
  49. package/dist/distributions.js +2 -2
  50. package/dist/internal/ddexp.d.ts +5 -0
  51. package/dist/internal/ddexp.js +28 -3
  52. package/dist/internal/discrete.js +30 -7
  53. package/dist/internal/invert.js +49 -0
  54. package/dist/internal/noncentral.d.ts +34 -0
  55. package/dist/internal/noncentral.js +115 -2
  56. package/dist/internal/poissonMixture.d.ts +7 -2
  57. package/dist/internal/poissonMixture.js +113 -32
  58. package/dist/linalg.js +2 -2
  59. package/dist/mixed.js +2 -2
  60. package/dist/numerics.js +2 -2
  61. package/dist/rstats.js +14 -14
  62. package/dist/special/dd.d.ts +1 -1
  63. package/dist/special/dd.js +4 -1
  64. package/dist/special/igam.js +93 -15
  65. package/dist/special/incbet.d.ts +10 -0
  66. package/dist/special/incbet.js +154 -48
  67. package/dist/special/incbetLargeA.js +47 -13
  68. package/dist/special/incbetPrefactor.d.ts +52 -0
  69. package/dist/special/incbetPrefactor.js +92 -0
  70. package/dist/special/incbetTemme.js +15 -3
  71. package/dist/special/loader.js +33 -7
  72. package/dist/special.js +2 -2
  73. package/dist/tests.js +2 -2
  74. package/package.json +1 -1
  75. package/dist/chunks/chunk-5YMP4WAJ.js +0 -2
  76. package/dist/chunks/chunk-BDJPSEA7.js +0 -2
  77. package/dist/chunks/chunk-KO6QQPC6.js +0 -2
  78. package/dist/chunks/chunk-TYFGVCVQ.js +0 -2
  79. package/dist/chunks/chunk-UWBYDIOH.js +0 -2
  80. package/dist/chunks/chunk-UZHY5DWN.js +0 -2
package/dist/tests.js CHANGED
@@ -1,5 +1,5 @@
1
- /*! rStats.js 0.15.1 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
- import{A as Mn,b as je,c as Ro,d as Et,e as cr,j as qt,k as ko,o as To,p as At,q as So,u as Nn,v as Lo,w as Xt,x as Io,z as yn}from"./chunks/chunk-KO6QQPC6.js";import{a as En,b as Ye,c as Zt,d as Vo,e as Co,f as mr,g as te,h as qn,i as Me,j as jo,k as ka,m as Tn,n as en}from"./chunks/chunk-Z3C3G5XI.js";import"./chunks/chunk-3QTVXDUM.js";import{b as $o,c as mt,e as Sn}from"./chunks/chunk-5XQZNKHZ.js";import{b as fr}from"./chunks/chunk-VOIJKNL2.js";import"./chunks/chunk-4ILI2FSB.js";import{e as Qt,f as se,g as re,h as Fo,i as ee,j as me,k as Fe,l as qe,m as Rt}from"./chunks/chunk-7J4HW6VF.js";import{a as Do,d as Po}from"./chunks/chunk-5SQFDLNG.js";import{a as ut,b as St,c as Ae,d as Yt,e as Jt,f as Wo,g as Go}from"./chunks/chunk-OLPTFKIZ.js";import{C as vn,D as lt,c as he,d as ar,m as ir,n as st,s as ae,t as at,v as it,w as Ze,y as lr}from"./chunks/chunk-5YMP4WAJ.js";import{B as kn,E as ht,h as pe,j as zo,k as Tt,l as An,r as Rn,v as we}from"./chunks/chunk-SAZAEPZC.js";import{a as xn,b as Oo,g as _o}from"./chunks/chunk-735NNFAS.js";import{k as Ne,l as ze,n as bn,o as ct}from"./chunks/chunk-TYFGVCVQ.js";import{a as ft}from"./chunks/chunk-5OSQ76OO.js";import{a as Kt,b as kt,c as ce,d as ge}from"./chunks/chunk-H4CHJMFU.js";import{b as Ao}from"./chunks/chunk-BDJPSEA7.js";import{f as Se,g as $,h as X,n as sr,p as Eo,s as qo}from"./chunks/chunk-UZHY5DWN.js";import{b as ot,c as xt,d as B,e as Mo,f as le,g as oe,h as Te,i as be,j as Bt,k as xo}from"./chunks/chunk-UWBYDIOH.js";function Lt(e,t,n={}){if(typeof e=="string")return Ta(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=te(n.alternative),a=n.mu??0,l=n.paired??!1,i=n.varEqual??!1,s=n.confLevel??.95;if(Number.isNaN(a))throw new RangeError("mu must be a number");if(qn(s),l&&r===void 0)throw new TypeError("y is required when paired is true");let c=Array.from(e),f=r===void 0?void 0:Array.from(r);if(l){let[x,q]=Rt(c,f);c=x.map((A,S)=>A-q[S]),f=void 0}else c=qe(c),f!==void 0&&(f=qe(f));let m,u,h,p,d,g,v,w=c.length,b=f!==void 0&&i;if(w<1||!b&&w<2)throw new Error("not enough 'x' observations");let N=se(c),M=w>1?re(c):NaN;if(f===void 0){if(u=w-1,h=Math.sqrt(M/w),h<10*Number.EPSILON*Math.abs(N))throw new Error("data are essentially constant");m=(N-a)/h,g=l?"Paired t-test":"One Sample t-test",p=N,d=l?"mean difference":"mean of x",v=l?"mean difference":"mean"}else{let x=f.length;if(x<1||!i&&x<2)throw new Error("not enough 'y' observations");if(i&&w+x<3)throw new Error("not enough observations");let q=se(f),A=x>1?re(f):NaN;if(g=`${i?"":"Welch"} Two Sample t-test`,p=[N,q],d=["mean of x","mean of y"],v="difference in means",i){u=w+x-2;let S=0;w>1&&(S+=(w-1)*M),x>1&&(S+=(x-1)*A),S/=u,h=Math.sqrt(S*(1/w+1/x))}else{let S=M/w,L=A/x;h=Math.sqrt(S+L),u=(h*h)**2/(S*S/(w-1)+L*L/(x-1))}if(h<10*Number.EPSILON*Math.max(Math.abs(N),Math.abs(q)))throw new Error("data are essentially constant");m=(N-q-a)/h}let E,y;if(o==="less")E=Ne(m,u,!0,!1),y=[-1/0,m+ze(s,u,!0,!1)];else if(o==="greater")E=Ne(m,u,!1,!1),y=[m-ze(s,u,!0,!1),1/0];else{E=2*Ne(-Math.abs(m),u,!0,!1);let x=1-s,q=ze(1-x/2,u,!0,!1);y=[m-q,m+q]}return y=[a+y[0]*h,a+y[1]*h],{method:g,alternative:o,statistic:m,statisticName:"t",parameter:u,parameterName:"df",pValue:E,confInt:y,confLevel:s,estimate:p,estimateNames:d,nullValue:a,nullValueName:v,stdErr:h}}function Ta(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=En(e,t,!0);if(r.kind==="one")return Lt(r.x,n);if(r.kind==="paired")return Lt(r.x,r.y,{...n,paired:!0});let[o,a]=r.levels;return{...Lt(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${a}`],nullValueName:`difference in means between group ${o} and group ${a}`}}function Ve(e){let t=[0,0],n=0;for(let r of e)t=B(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Sa(e,t,n){return Array.isArray(t)?La(e,t,n??{}):Ia(e,t??{})}var Uo=(e,t)=>e<t?-1:e>t?1:0;function La(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=[],a=[];e.forEach((u,h)=>{let p=t[h];!r(u)&&!r(p)&&(o.push(u),a.push(p))});let l=[...new Set(o)].sort(Uo),i=[...new Set(a)].sort(Uo);if(l.length<2||i.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let s=new Map(l.map((u,h)=>[u,h])),c=new Map(i.map((u,h)=>[u,h])),f=l.map(()=>i.map(()=>0));return o.forEach((u,h)=>f[s.get(u)][c.get(a[h])]++),{...Ko(f,n),rowLevels:l,colLevels:i}}function Ia(e,t){if(e.length>0&&typeof e[0]!="number"){let r=e.map(a=>Array.from(a)),o=r[0].length;if(r.some(a=>a.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?ur(r[0],t):o===1?ur(r.map(a=>a[0]),t):Ko(r,t)}return ur(Array.from(e),t)}function Ho(e){let t=e.B??2e3;if(!(t>=0&&t<2**31))throw new RangeError("'B' must be a non-negative number");return t}function Bo(e,t,n){let r=(1-64*Number.EPSILON)*t,o=0;for(let a=0;a<e.length;a++)e[a]>=r&&o++;return(1+o)/(n+1)}function Xo(e,t){return Number.isNaN(e)?NaN:he(e,t/2,2,!1,!1)}function Qo(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 ur(e,t){let n=e.length;if(e.some(m=>Number.isNaN(m)||m<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=Ve(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(m=>m<0))throw new RangeError("probabilities must be non-negative");if(t.rescaleP??!1){let m=Ve(o);o=o.map(u=>u/m)}else if(Math.abs(Ve(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let a=o.map(m=>r*m);Qo(a);let l=o.map((m,u)=>a[u]*(1-m)),i=Ve(e.map((m,u)=>(m-a[u])**2/a[u])),s="Chi-squared test for given probabilities",c=n-1,f;if(t.simulatePValue??!1){let m=Ho(t);s+=Kt(m),c=NaN,f=Bo(Fa(r,o,a,m),i,m)}else f=Xo(i,c);return{method:s,statistic:i,statisticName:"X-squared",parameter:c,parameterName:"df",pValue:f,observed:e,expected:a,residuals:e.map((m,u)=>(m-a[u])/Math.sqrt(a[u])),stdres:e.map((m,u)=>(m-a[u])/Math.sqrt(l[u]))}}function Fa(e,t,n,r){let o=t.length,a=Io(o,{size:Math.trunc(r*e),replace:!0,prob:t}),l=Math.trunc(e);if(a.length===0)return new Float64Array(0);if(l===0)throw new RangeError("invalid 'nrow' value (too large or NA)");let i=Math.ceil(a.length/l),s=new Float64Array(i),c=new Float64Array(o);for(let f=0;f<i;f++){c.fill(0);for(let u=0;u<l;u++)c[a[(f*l+u)%a.length]]++;let m=0;for(let u=0;u<o;u++)m+=(c[u]-n[u])**2/n[u];s[f]=m}return s}function Oa(e,t,n,r){let o=e.length,a=t.length,l=e.reduce((f,m)=>f+m,0),i=yn(l),s=new Int32Array(o*a),c=new Float64Array(Math.trunc(r));for(let f=0;f<c.length;f++){Mn(e,t,i,s);let m=0;for(let u=0;u<a;u++)for(let h=0;h<o;h++){let p=n[h][u];m+=(s[h+u*o]-p)**2/p}c[f]=m}return c}function Ko(e,t){let n=t.correct??!0,r=e.length,o=e[0].length,a=e.flat();if(a.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let l=Ve(a);if(!(l>0))throw new RangeError("at least one entry of 'x' must be positive");let i=e.map(w=>Ve(w)),s=e[0].map((w,b)=>Ve(e.map(N=>N[b]))),c=i.map(w=>s.map(b=>w*b/l));Qo(c.flat());let f=e.map((w,b)=>w.map((N,M)=>N-c[b][M])),m="Pearson's Chi-squared test",u,h=(r-1)*(o-1),p,d=(t.simulatePValue??!1)&&i.every(w=>w>0)&&s.every(w=>w>0),g=!d&&n&&r===2&&o===2?Math.min(.5,...f.flat().map(Math.abs)):0;if(d){let w=Ho(t);m+=Kt(w),u=Ve(f.flatMap((N,M)=>N.map((E,y)=>E*E/c[M][y]))),h=NaN;let b=Oa(i.map(N=>Math.trunc(N)),s.map(N=>Math.trunc(N)),c,w);p=Bo(b,u,w)}else g>0?(m+=" with Yates' continuity correction",u=Ve(f.flatMap((w,b)=>w.map((N,M)=>(Math.abs(N)-g)**2/c[b][M])))):u=Ve(f.flatMap((w,b)=>w.map((N,M)=>N*N/c[b][M])));let v=c.map((w,b)=>w.map((N,M)=>N*(1-i[b]/l)*(1-s[M]/l)));return{method:m,statistic:u,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p??Xo(u,h),observed:e,expected:c,residuals:f.map((w,b)=>w.map((N,M)=>N/Math.sqrt(c[b][M]))),stdres:f.map((w,b)=>w.map((N,M)=>N/Math.sqrt(v[b][M])))}}var _a=Math.pow(Number.EPSILON,.25);function xe(e,t,n,r={}){let o={tol:r.tol??_a,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),mt(e,t,n,o).root}function Va(e,t={}){let n=e.length,r=n===0?0:e[0].length;if(e.some(s=>s.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(s=>Array.from(s));if(o.flat().some(s=>Number.isNaN(s)||s<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(s=>s===1/0))throw new RangeError("'x' has entries too large to be integer");let a=o.map(s=>s.map(Ao)),l="Fisher's Exact Test for Count Data";if(n===2&&r===2)return $a(a,t,l);let i=t.alternative??"two.sided";if(t.simulatePValue??!1){let s=t.B??2e3;if(!(s>=0&&s<2**31))throw new RangeError("'B' must be a non-negative number");return{method:l+Kt(s),alternative:i,pValue:Ca(a,s)}}return{method:l,alternative:i,pValue:ja(a)}}function Ca(e,t){let n=e.filter(d=>d.some(g=>g>0)),r=e[0].map((d,g)=>g).filter(d=>e.some(g=>g[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(g=>d[g])),a=o.map(d=>d.reduce((g,v)=>g+v,0)),l=o[0].map((d,g)=>o.reduce((v,w)=>v+w[g],0)),i=a.length,s=l.length,c=[0,0];for(let d of o.flat())c=B(c,[-Po(d),0]);let f=c[0]/(1+64*Number.EPSILON),m=yn(a.reduce((d,g)=>d+g,0)),u=new Int32Array(i*s),h=0,p=Math.trunc(t);for(let d=0;d<p;d++){Mn(a,l,m,u);let g=0;for(let v=0;v<u.length;v++)g-=m[u[v]];g<=f&&h++}return Math.max(0,Math.min(1,(1+h)/(t+1)))}var Da=1+1e-7,Pa=16;function hr(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=B(r,[e[o],0]));return r[0]}var dr=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,a=1;for(let l=this.lo;l<=this.hi;l++){let i=l-this.lo;i%Pa===0||!(a>0&&a<1/0)?(o=Et(l,t,n,r,!0),a=1,this.logCentral[i]=o):this.logCentral[i]=o+Math.log(a),a*=(t-l)*(r-l)/((l+1)*(n-r+l+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),a=-1/0;for(let i=0;i<n;i++){let s=this.logCentral[i]+(this.lo+i)*r;o[i]=s,s>a&&(a=s)}for(let i=0;i<n;i++){let s=o[i]-a;o[i]=s<-746?0:Math.exp(s)}let l=hr(o,0,n-1);for(let i=0;i<n;i++)o[i]!==0&&(o[i]=o[i]/l);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=B(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?cr(t-1,this.m,this.n,this.k,!1,!1):cr(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let l=n===0?this.lo:this.hi;return(r?t<=l:t>=l)?1:0}let o=this.density(n),a=t-this.lo;return r?hr(o,a,o.length-1):hr(o,0,a)}};function pr(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function $a(e,t,n){let r=te(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let a=t.confLevel??.95;if(!(a>0&&a<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[l,i],[s,c]]=e,f=l,m=new dr(l+s,i+c,l+i),{lo:u,hi:h}=m,p;if(r==="less")p=m.tail(f,o,!1);else if(r==="greater")p=m.tail(f,o,!0);else if(o===0)p=f===u?1:0;else if(o===1/0)p=f===h?1:0;else{let N=m.density(o),M=N[f-u]*Da,E=[0,0];for(let y of N)y<=M&&(E=B(E,[y,0]));p=E[0]}let d;if(f===u)d=0;else if(f===h)d=1/0;else{let N=m.mean(1);d=N===f?1:pr(M=>m.mean(M)-f,N>f)}let g={method:n,alternative:r,pValue:p,estimate:d,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio"};if(!(t.confInt??!0))return g;let v=N=>{if(f===h)return 1/0;let M=m.tail(f,1,!1);return M===N?1:pr(E=>m.tail(f,E,!1)-N,M<N)},w=N=>{if(f===u)return 0;let M=m.tail(f,1,!0);return M===N?1:pr(E=>m.tail(f,E,!0)-N,M>N)},b;if(r==="less")b=[0,v(1-a)];else if(r==="greater")b=[w(1-a),1/0];else{let N=(1-a)/2;b=[w(N),v(N)]}return{...g,confInt:b,confLevel:a}}var Ln=[[0,0]];function Ce(e){for(let t=Ln.length;t<=e;t++)Ln.push(B(Ln[t-1],xo([t,0])));return Ln[e]}function In(e,t,n,r){let o=e+n,a=o-e,l=e-(o-a)+(n-a)+t+r,i=o+l;return[i,l-(i-o)]}var gr=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let s=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=s,this.lo*=s,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),a=this.hi+o,l=a-this.hi,i=this.hi-(a-l)+(o-l)+this.lo;this.hi=a+i,this.lo=i-(this.hi-a)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function ja(e){let t=e.filter(b=>b.some(N=>N>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((b,N)=>t.some(M=>M[N]>0));if(t=t.map(b=>b.filter((N,M)=>n[M])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((b,N)=>t.map(M=>M[N])));let r=t.length,o=t.map(b=>b.reduce((N,M)=>N+M,0)),a=t[0].map((b,N)=>t.reduce((M,E)=>M+E[N],0)).sort((b,N)=>N-b),l=a.length,i=o.reduce((b,N)=>b+N,0),s=Mo(Ce(i));for(let b of o)s=B(s,Ce(b));for(let b of a)s=B(s,Ce(b));let c=[0,0];for(let b of t.flat())c=le(c,Ce(b));let f=B(c,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:l},()=>new Map),d=(b,N)=>{let M=N.join(","),E=p[b].get(M);if(E!==void 0)return E;let y=B(s,Ce(N.reduce((R,k)=>R+k,0)));for(let R of N)y=le(y,Ce(R));for(let R=b;R<l;R++)y=le(y,Ce(a[R]));let x={R:N,KZ:y,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(M,x),b===l-1){let R=[0,0];for(let k of N)R=le(R,Ce(k));return x.max=x.min=R[0],x.edges.push({to:x,hi:R[0],lo:R[1],mult:1}),x}let q=new Map,A=new Array(r+1).fill(0);for(let R=r-1;R>=0;R--)A[R]=A[R+1]+N[R];let S=new Array(r).fill(0),L=(R,k,C)=>{if(R===r-1){S[R]=k;let V=le(C,Ce(k)),z=N.map((P,D)=>P-S[D]).sort((P,D)=>P-D),Y=d(b+1,z),T=`${z.join(",")}|${V[0]}`,F=q.get(T);F===void 0?q.set(T,{to:Y,hi:V[0],lo:V[1],mult:1}):F.mult++;return}let O=Math.max(0,k-A[R+1]),I=Math.min(N[R],k);for(let V=O;V<=I;V++)S[R]=V,L(R+1,k-V,le(C,Ce(V)))};if(L(0,a[b],[0,0]),x.edges=[...q.values()],x.max=Math.max(...x.edges.map(R=>R.hi+R.to.max)),x.min=Math.min(...x.edges.map(R=>R.hi+R.to.min)),b===l-2){let R=x.edges.map(Y=>{let T=Y.to.edges[0];return{v:B([Y.hi,Y.lo],[T.hi,T.lo]),mult:Y.mult}});R.sort((Y,T)=>Y.v[0]-T.v[0]||Y.v[1]-T.v[1]);let k=R.length,C=R[k-1].v[0],O=new Float64Array(k),I=new Float64Array(k),V=new Float64Array(k),z=[0,0];R.forEach(({v:Y,mult:T},F)=>{O[F]=Y[0],I[F]=Y[1];let P=le(Y,[C,0]);z=B(z,[T*Math.exp(P[0])*(1+P[1]),0]),V[F]=z[0]}),x.dist={hi:O,lo:I,cum:V,vmax:C,kv:B(s,[C,0])}}return x},g=d(0,[...o].sort((b,N)=>b-N)),v=new gr,w=(b,N,M,E)=>{if(N+b.max<=u-m){let[L,R]=In(b.KZ[0],b.KZ[1],N,M);return v.add(L,R,E),!0}if(N+b.min>u+m)return!0;let y=b.dist;if(y===void 0)return!1;let[x,q]=In(u,h,-N,-M),A=0,S=y.hi.length;for(;A<S;){let L=A+S>>>1,R=y.hi[L];R<x||R===x&&y.lo[L]<=q?A=L+1:S=L}if(A>0){let[L,R]=In(y.kv[0],y.kv[1],N,M);v.add(L,R,E*y.cum[A-1])}return!0};w(g,0,0,1)||(g.pastHi.push(0),g.pastLo.push(0),g.count.push(1));for(let b=0;b<l-2;b++)for(let N of p[b].values()){let{pastHi:M,pastLo:E,count:y}=N;if(y.length!==0){for(let{to:x,hi:q,lo:A,mult:S}of N.edges)for(let L=0;L<y.length;L++){let[R,k]=In(M[L],E[L],q,A),C=y[L]*S;if(w(x,R,k,C))continue;let O=x.index.get(R);O===void 0?(x.index.set(R,x.count.length),x.pastHi.push(R),x.pastLo.push(k),x.count.push(C)):x.count[O]+=C}N.pastHi=N.pastLo=N.count=[],N.index.clear()}}return Math.min(1,v.value())}function tn(e,t,n={}){if(typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let d=En(e,t,!0);return d.kind==="one"?tn(d.x,n):tn(d.x,d.y,d.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=te(n.alternative),a=n.mu??0,l=n.paired??!1,i=n.exact??null,s=n.correct??!0,c=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(a))throw new RangeError("mu must be a single finite number");if(c&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(l&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(l){let[d,g]=Rt(Array.from(e),Array.from(r));u=qe(d.map((v,w)=>v-g[w]))}else u=qe(Array.from(e)),r!==void 0&&(h=qe(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let p={alternative:o,mu:a,correct:s,confInt:c,confLevel:f,tolRoot:m};return h===void 0?Wa(u,i,l,p):Ha(u,h,i,p)}function Fn(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function Yo(e,t){return t==="less"?$(e):t==="greater"?$(e,{lowerTail:!1}):2*Math.min($(e),$(e,{lowerTail:!1}))}function za(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 wr(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:se([e[n-1],e[n]])}var vr=(e,t)=>e-t;function Jo(e){let t=e.length,n=me(e.map(Math.abs)),r=0;for(let l=0;l<t;l++)e[l]>0&&(r+=n[l]);let o=0;for(let l of Fe(n))o+=l*l*l-l;let a=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,a]}function Wa(e,t,n,r){let{alternative:o,mu:a,correct:l}=r,i=e.map(E=>E-a),s=i.some(E=>E===0);s&&(i=i.filter(E=>E!==0));let c=i.length,f=t??c<50,m=me(i.map(Math.abs)),u=Fe(m).length>0,[h,p,d]=Jo(i),g,v,w=f&&!u&&!s;if(w){v="Wilcoxon signed rank exact test";let E=c*(c+1)/2;if(o==="less")g=qt(h,c,!0,!1);else if(o==="greater")g=qt(h-1,c,!1,!1);else{let y=h>E/2?qt(h-1,c,!1,!1):qt(h,c,!0,!1);g=Math.min(1,2*y)}}else v=`Wilcoxon signed rank test${l?" with continuity correction":""}`,g=Yo((p-Fn(p,o,l))/d,o);let b={method:v,alternative:o,statistic:h,statisticName:"V",pValue:g,nullValue:a,nullValueName:n?"location shift":"location"};if(!r.confInt)return b;let N=i.map(E=>E+a),M=w?Ga(N,r):Ua(N,r);return{...b,...M}}function On(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Zo(e,t,n){let r={confInt:On(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function es(e,t,n){let r=e(t==="two.sided"?n/2:n);return r===0?1:r}function ts(e){if(!(e>0))throw new RangeError("tolRoot must be positive")}function Ga(e,t){let{alternative:n,confLevel:r}=t,o=e.length,a=[];for(let m=0;m<o;m++)for(let u=m;u<o;u++)a.push((e[m]+e[u])/2);a.sort(vr);let l=a.length,i=1-r,s=es(m=>ko(m,o,!0,!1),n,i),c=qt(s-1,o,!0,!1),f=n==="two.sided"?2*c:c;return{confInt:On(n,a[s-1],a[l-s]),confLevel:f-i>i/2?1-za(f):r,estimate:wr(a),estimateNames:"(pseudo)median"}}function Ua(e,t){let{alternative:n,correct:r,confLevel:o,tolRoot:a}=t,l=e.length;if(l===0)return Zo(n,NaN);let i=Math.min(...e),s=Math.max(...e);if(!Number.isFinite(i)||!Number.isFinite(s)||l===1||i===s)return Zo(n,(i+s)/2,"midrange");let c=(b,N)=>{let[,M,E]=Jo(e.map(y=>y-b).filter(y=>y!==0));return(M-Fn(M,n,N))/E},f=c(i,r),m=c(s,r),u=b=>-X(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>X(b/2)):!(f<u(b/2))&&!(m>X(b/2)),p=1-o,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;ts(a);let g=b=>xe(N=>c(N,r)-b,i,s,{tol:a,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=wr([...e].sort(vr));v=On(n,b,b)}else n==="greater"?v=[g(u(p)),1/0]:n==="less"?v=[-1/0,g(X(p))]:v=[g(u(p/2)),g(X(p/2))];let w=xe(b=>c(b,!1),i,s,{tol:a,fLower:c(i,!1),fUpper:c(s,!1)});return{confInt:v,confLevel:d?1-Math.min(1,p):o,estimate:w,estimateNames:"(pseudo)median"}}function ns(e,t){let n=e.length,r=t.length,o=me([...e,...t]),a=0;for(let f=0;f<n;f++)a+=o[f];let l=a-n*(n+1)/2,i=0;for(let f of Fe(o))i+=f*f*f-f;let s=n+r,c=Math.sqrt(n*r/12*(s+1-i/(s*(s-1))));return[l,l-n*r/2,c]}function Ha(e,t,n,r){let{alternative:o,mu:a,correct:l}=r,i=e.length,s=t.length,c=e.map(N=>N-a),f=n??(i<50&&s<50),m=Fe([...c,...t]).length>0,[u,h,p]=ns(c,t),d,g,v=f&&!m;if(v)if(g="Wilcoxon rank sum exact test",o==="less")d=At(u,i,s,!0,!1);else if(o==="greater")d=At(u-1,i,s,!1,!1);else{let N=u>i*s/2?At(u-1,i,s,!1,!1):At(u,i,s,!0,!1);d=Math.min(1,2*N)}else g=`Wilcoxon rank sum test${l?" with continuity correction":""}`,d=Yo((h-Fn(h,o,l))/p,o);let w={method:g,alternative:o,statistic:u,statisticName:"W",pValue:d,nullValue:a,nullValueName:"location shift"};if(!r.confInt)return w;let b=v?Ba(e,t,r):Xa(e,t,r);return{...w,...b}}function Ba(e,t,n){let{alternative:r,confLevel:o}=n,a=e.length,l=t.length,i=[];for(let h of e)for(let p of t)i.push(h-p);i.sort(vr);let s=i.length,c=1-o,f=es(h=>So(h,a,l,!0,!1),r,c),m=At(f-1,a,l,!0,!1),u=r==="two.sided"?2*m:m;return{confInt:On(r,i[f-1],i[s-f]),confLevel:u-c>c/2?1-u:o,estimate:wr(i),estimateNames:"difference in location"}}function Xa(e,t,n){let{alternative:r,correct:o,confLevel:a,tolRoot:l}=n,i=Math.min(...e)-Math.max(...t),s=Math.max(...e)-Math.min(...t);if(!Number.isFinite(i)||!Number.isFinite(s))throw new Error("cannot compute the confidence interval because the differences are not finite");let c=(h,p)=>{let[,d,g]=ns(e.map(v=>v-h),t);return(d-Fn(d,r,p))/g},f=(h,p)=>{let d=c(i,p)-h,g=c(s,p)-h;if(Number.isNaN(d)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return d<=0?i:g>=0?s:(ts(l),xe(v=>c(v,p)-h,i,s,{tol:l,fLower:d,fUpper:g}))},m=1-a,u;return r==="greater"?u=[f(-X(m),o),1/0]:r==="less"?u=[-1/0,f(X(m),o)]:u=[f(-X(m/2),o),f(X(m/2),o)],{confInt:u,confLevel:a,estimate:f(0,!1),estimateNames:"difference in location"}}var rs=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Qa(e){if(rs.includes(e))return e;let t=rs.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function It(e,t={}){let n=typeof t=="string"?{method:t}:t,r=Qa(n.method??"holm"),o=Array.from(e),a=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||a.push(m);let l=a.length,i=n.n??l;if(!(i>=l))throw new RangeError("n must be at least the number of non-NaN p-values");if(i<=1)return o;let s=a.map(m=>o[m]),c=Ka(s,r,i),f=o.slice();return a.forEach((m,u)=>f[m]=c[u]),f}function _n(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 Ka(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(a=>Math.min(1,n*a));case"holm":{let a=_n(e,!1),l=-1/0;return a.forEach((i,s)=>{l=Math.max(l,(n-s)*e[i]),o[i]=Math.min(1,l)}),o}case"hochberg":{let a=_n(e,!0),l=1/0;return a.forEach((i,s)=>{let c=r-s;l=Math.min(l,(n-c+1)*e[i]),o[i]=Math.min(1,l)}),o}case"BH":case"fdr":case"BY":{let a=t==="BY"?ee(Array.from({length:Math.floor(n)},(s,c)=>1/(c+1))):1,l=_n(e,!0),i=1/0;return l.forEach((s,c)=>{let f=r-c;i=Math.min(i,a*n/f*e[s]),o[s]=Math.min(1,i)}),o}case"hommel":return Za(e,n)}}function Za(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=_n(r,!1),a=o.map(s=>r[s]),l=a.slice();for(let s=t;s>=2;s--){let c=1/0;for(let f=1;f<=s;f++)c=Math.min(c,s*a[t-s+f-1]/f);for(let f=t-s;f<t;f++)l[f]=Math.max(l[f],c);for(let f=0;f<t-s;f++)l[f]=Math.max(l[f],Math.min(s*a[f],c))}let i=new Array(n);return o.forEach((s,c)=>{s<n&&(i[s]=l[c])}),i}var Ya=1+1e-7;function Vn(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function Ja(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=vn(e,t,n,!1)*Ya,a=0;if(e<r){for(let s=Math.ceil(r);s<=t;s++)vn(s,t,n,!1)<=o&&a++;let i=a===0?0:lt(t-a,t,n,!1,!1);return Math.min(1,lt(e,t,n,!0,!1)+i)}for(let i=0;i<=Math.floor(r);i++)vn(i,t,n,!1)<=o&&a++;let l=a===0?0:lt(a-1,t,n,!0,!1);return Math.min(1,lt(e-1,t,n,!1,!1)+l)}function br(e,t,n={}){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),!Vn(e[0])||!Vn(e[1]))throw new RangeError("'x' must be nonnegative and integer");r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1])}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Vn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Vn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}let a=n.p??.5;if(!(a>=0&&a<=1))throw new RangeError("'p' must be a single number between 0 and 1");let l=te(n.alternative),i=n.confLevel??.95;Me(i);let s;l==="less"?s=lt(r,o,a,!0,!1):l==="greater"?s=lt(r-1,o,a,!1,!1):s=Ja(r,o,a);let c=u=>r===0?0:lr(u,r,o-r+1,!0,!1),f=u=>r===o?1:lr(1-u,r+1,o-r,!0,!1),m;if(l==="less")m=[0,f(1-i)];else if(l==="greater")m=[c(1-i),1];else{let u=(1-i)/2;m=[c(u),f(u)]}return{method:"Exact binomial test",alternative:l,statistic:r,statisticName:"number of successes",parameter:o,parameterName:"number of trials",pValue:s,confInt:m,confLevel:i,estimate:r/o,estimateNames:"probability of success",nullValue:a,nullValueName:"probability of success"}}var ei=1+1e-7;function ti(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function ni(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=ir(e,t,!1)*ei,r=i=>ir(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=Math.max(e+1,o),s=i;if(!r(i)){let c=i,f=1;for(s=i+1;!r(s);)c=s,f*=2,s=i+f;for(;s-c>1;){let m=c+Math.floor((s-c)/2);r(m)?s=m:c=m}}return Math.min(1,st(e,t,!0,!1)+st(s-1,t,!1,!1))}let a=Math.min(e-1,o),l;if(!r(0))l=-1;else if(r(a))l=a;else{l=0;let i=a;for(;i-l>1;){let s=l+Math.floor((i-l)/2);r(s)?l=s:i=s}}return Math.min(1,st(l,t,!0,!1)+st(e-1,t,!1,!1))}function ri(e,t={}){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(ti))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,a=t.T??1,l=typeof a=="number"?new Array(o).fill(a):Array.from(a);if(l.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!l.every(v=>Number.isFinite(v)&&v>=0))throw new RangeError("'T' must be nonnegative");let i=t.r??1;if(!(Number.isFinite(i)&&i>=0))throw new RangeError("'r' must be a single positive number");let s=te(t.alternative),c=t.confLevel??.95;if(Me(c),o===2){let[v,w]=r,[b,N]=l,M=v+w,E=i*b/(i*b+N),y=br([v,w],{p:E,alternative:s,confLevel:c}),x=q=>q/(1-q)*(N/b);return{method:"Comparison of Poisson rates",alternative:s,statistic:v,statisticName:"count1",parameter:M*i*b/(b+i*N),parameterName:"expected count1",pValue:y.pValue,confInt:[x(y.confInt[0]),x(y.confInt[1])],confLevel:c,estimate:v/b/(w/N),estimateNames:"rate ratio",nullValue:i,nullValueName:"rate ratio"}}let f=r[0],m=l[0],u=i*m,h;s==="less"?h=st(f,u,!0,!1):s==="greater"?h=st(f-1,u,!1,!1):h=ni(f,u);let p=v=>f===0?0:ar(v,f,1,!0,!1),d=v=>ar(1-v,f+1,1,!0,!1),g;if(s==="less")g=[0,d(1-c)];else if(s==="greater")g=[p(1-c),1/0];else{let v=(1-c)/2;g=[p(v),d(v)]}return{method:"Exact Poisson test",alternative:s,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[g[0]/m,g[1]/m],confLevel:c,estimate:f/m,estimateNames:"event rate",nullValue:i,nullValueName:"event rate"}}function Nr(e,t,n={}){let r,o,a=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let A=e;if(A.some(S=>S.length!==2))throw new RangeError("'x' must have 2 columns");r=A.map(S=>S[0]),o=A.map(S=>S[0]+S[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(a=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 l=r.length,i=[];for(let A=0;A<l;A++)!Number.isNaN(r[A])&&!Number.isNaN(o[A])&&i.push(A);let s=i.length;if(s<1)throw new RangeError("not enough data");if(r=i.map(A=>r[A]),o=i.map(A=>o[A]),o.some(A=>!(A>0)))throw new RangeError("elements of 'n' must be positive");if(r.some(A=>!(A>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((A,S)=>A>o[S]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let c;if(a.p!==void 0){let A=typeof a.p=="number"?[a.p]:Array.from(a.p);if(A.length!==l)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(c=i.map(S=>A[S]),c.some(S=>!(S>0&&S<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=c!==void 0;s===1&&c===void 0&&(c=[.5]);let m=a.confLevel??.95;Me(m);let u=te(a.alternative);(s>2||s===2&&f)&&(u="two.sided");let h=r.map((A,S)=>A/o[S]),p=s===2?h[0]-h[1]:0,d=s===2?1/o[0]+1/o[1]:0,g=(a.correct??!0)&&s<=2?.5:0,v=!1;if(g>0&&s===1){let A=Math.abs(r[0]-o[0]*c[0]);A<=g&&([g,v]=[A,!0])}else if(g>0&&s===2&&!f){let A=Math.abs(p)/d;A<=g&&([g,v]=[A,!0])}let w=r.reduce((A,S)=>A+S,0)/o.reduce((A,S)=>A+S,0),b=0;if(!v)for(let A=0;A<s;A++){let S=c?.[A]??w,L=o[A]*S,R=o[A]*(1-S);b+=(Math.abs(r[A]-L)-g)**2/L,b+=(Math.abs(o[A]-r[A]-R)-g)**2/R}let N=c!==void 0?s:s-1,M;if(u==="two.sided")M=he(b,N/2,2,!1,!1);else{let A=Math.sign(s===1?h[0]-c[0]:p);M=$(A*Math.sqrt(b),{lowerTail:u==="less"})}let E;if(s===1||s===2&&!f){let A=X(u==="two.sided"?(1+m)/2:m);if(s===1){let S=o[0],L=A*A,R=(V,z)=>(V+L/(2*S)+z*A*Math.sqrt(V*(1-V)/S+L/(4*S*S)))/(1+L/S),k=h[0]+g/S,C=h[0]-g/S,O=Math.min(1,Math.max(0,k>=1?1:R(k,1))),I=Math.min(1,Math.max(0,C<=0?0:R(C,-1)));E=u==="two.sided"?[I,O]:u==="greater"?[I,1]:[0,O]}else{let S=Math.sqrt(h.reduce((C,O,I)=>C+O*(1-O)/o[I],0)),L=A*S+g*d,R=Math.max(-1,p-L),k=Math.min(1,p+L);E=u==="two.sided"?[R,k]:u==="greater"?[R,1]:[-1,k]}}let y=s===1?"p":i.map(A=>`prop ${A+1}`),q={method:`${s===1?"1-sample proportions test":`${s}-sample test for ${f?"given proportions":"equality of proportions"}`} ${g!==0?"with":"without"} continuity correction`,alternative:u,statistic:b,statisticName:"X-squared",parameter:N,parameterName:"df",pValue:M,estimate:s===1?h[0]:h,estimateNames:y};return c!==void 0&&(q.nullValue=s===1?c[0]:c,q.nullValueName=y),E!==void 0&&(q.confInt=E,q.confLevel=m),q}function oi(e,t,n={}){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 c=r.length;if(c<2||r.some(f=>f.length!==c))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=si(e,t)}let o=n.correct??!0,a=r.length,l,i=o&&a===2&&r[0][1]!==r[1][0];if(i){let c=r[0][1],f=r[1][0];l=(Math.abs(c-f)-1)**2/(c+f)}else{l=0;for(let c=0;c<a;c++)for(let f=c+1;f<a;f++){let m=r[c][f],u=r[f][c];l+=(m-u)**2/(m+u)}}let s=a*(a-1)/2;return{method:i?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:l,statisticName:"McNemar's chi-squared",parameter:s,parameterName:"df",pValue:he(l,s/2,2,!1,!1)}}function si(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=ce(e),o=ce(t);for(let m=0;m<e.length;m++){let u=r[m],h=o[m];typeof u=="number"&&Number.isNaN(u)||typeof h=="number"&&Number.isNaN(h)||n.push([u,h])}let a=m=>Array.from(new Set(m)).sort((u,h)=>typeof u=="number"&&typeof h=="number"?u-h:String(u)<String(h)?-1:String(u)>String(h)?1:0),l=a(n.map(m=>m[0])),i=a(n.map(m=>m[1]));if(l.length<2||l.length!==i.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let s=new Map(l.map((m,u)=>[m,u])),c=new Map(i.map((m,u)=>[m,u])),f=l.map(()=>new Array(l.length).fill(0));for(let[m,u]of n)f[s.get(m)][c.get(u)]++;return f}function os(e,t,n={}){if(typeof e=="string"){let g=Ye(e,t);return os(g.x,g.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),a=n.ratio??1;if(!(Number.isFinite(a)&&a>0))throw new RangeError("'ratio' must be a single positive number");let l=te(n.alternative),i=n.confLevel??.95;Me(i);let s=r.length-1,c=o.length-1;if(s<1)throw new Error("not enough 'x' observations");if(c<1)throw new Error("not enough 'y' observations");let f=re(r)/re(o),m=f/a,u=ae(m,s,c,!0,!1),h=ae(m,s,c,!1,!1),p,d;if(l==="less")p=u,d=[0,f/at(1-i,s,c,!0,!1)];else if(l==="greater")p=h,d=[f/at(i,s,c,!0,!1),1/0];else{p=Math.min(1,2*Math.min(u,h));let g=(1-i)/2;d=[f/at(1-g,s,c,!0,!1),f/at(g,s,c,!0,!1)]}return{method:"F test to compare two variances",alternative:l,statistic:m,statisticName:"F",parameter:[s,c],parameterName:["num df","denom df"],pValue:p,confInt:d,confLevel:i,estimate:f,estimateNames:"ratio of variances",nullValue:a,nullValueName:"ratio of variances"}}function ss(e,t){if(typeof e=="string"){let u=Zt(e,t);return ss(Vo(u.values,u.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=[],a=[];for(let u of n){let h=Array.from(u).filter(Number.isFinite);if(h.length<2)throw new RangeError("there must be at least 2 observations in each group");o.push(h.length-1),a.push(re(h))}let l=0,i=0,s=0,c=0;for(let u=0;u<r;u++)l+=o[u],i+=o[u]*a[u],s+=o[u]*Math.log(a[u]),c+=1/o[u];i/=l;let f=(l*Math.log(i)-s)/(1+(c-1/l)/(3*(r-1))),m=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:f,statisticName:"Bartlett's K-squared",parameter:m,parameterName:"df",pValue:he(f,m/2,2,!1,!1)}}function pt(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 s=i.map(m=>Array.from(m).filter(u=>!Number.isNaN(u)));if(n==="error"&&s.some(m=>m.length===0))throw new RangeError("all groups must contain data");let c=[],f=[];return s.forEach((m,u)=>{m.length!==0&&(c.push(String(u+1)),f.push(m))}),{labels:c,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=ce(t),a=new Map;for(let i=0;i<r.length;i++){let s=r[i],c=o[i];if(Number.isNaN(s)||typeof c=="number"&&Number.isNaN(c))continue;let f=a.get(c);f===void 0&&a.set(c,f=[]),f.push(s)}let l=Array.from(a.keys()).sort((i,s)=>typeof i=="number"&&typeof s=="number"?i-s:String(i)<String(s)?-1:String(i)>String(s)?1:0);return{labels:l.map(ge),groups:l.map(i=>a.get(i))}}function dt(e,t,n="drop"){return pt(e,t,n).groups}function Cn(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:(t[n/2-1]+t[n/2])/2}function yr(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))}function Mr(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:he(n,r/2,2,!1,!1)}}function as(e){let t=0;for(let n of Fe(e))t+=n*n*n-n;return t}function Dn(e,t){if(typeof e=="string"){let c=Zt(e,t);return Dn(c.values,c.group.codes)}let n=dt(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),a=o.length,l=me(o),i=0,s=0;for(let c of n){let f=0;for(let m=0;m<c.length;m++)f+=l[s+m];s+=c.length,i+=f*f/c.length}return i=(12*i/(a*(a+1))-3*(a+1))/(1-as(o)/(a*a*a-a)),Mr("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",i,r-1)}function xr(e,t){if(typeof e=="string")return xr(mr(e,t));let n=yr(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 a=new Array(o).fill(0),l=0;for(let c of n){let f=me(c);for(let m=0;m<o;m++)a[m]+=f[m];l+=as(c)}let i=0;for(let c of a)i+=(c-r*(o+1)/2)**2;let s=12*i/(r*o*(o+1)-l/(o-1));return Mr("Friedman rank sum test","Friedman chi-squared",s,o-1)}function ai(e){return{...xr(e),method:"Cochran's Q test",statisticName:"Q"}}function is(e,t){if(typeof e=="string")return is(mr(e,t));let n=yr(e),r=e.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 a=me(n.map(p=>Math.max(...p)-Math.min(...p))),l=new Array(o).fill(0),i=0;n.forEach((p,d)=>{let g=me(p);for(let v=0;v<o;v++){let w=a[d]*(g[v]-(o+1)/2);i+=w*w,l[v]+=w}});let s=0;for(let p of l)s+=p*p;s/=r;let c=o-1,f=(r-1)*(o-1),m,u,h=[c,f];if(i===s){m=NaN,h=[NaN,NaN];let p=0;for(let d=2;d<=o;d++)p+=Math.log(d);u=Math.exp((1-r)*p)}else m=(r-1)*s/(i-s),u=ae(m,c,f,!1,!1);return{method:"Quade test",statistic:m,statisticName:"Quade F",parameter:h,parameterName:["num df","denom df"],pValue:u}}function ls(e,t){if(typeof e=="string"){let u=Zt(e,t);return ls(u.values,u.group.codes)}let n=dt(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flatMap(u=>{let h=Cn(u);return u.map(p=>Math.abs(p-h))}),a=o.length,i=me(o).map(u=>X((1+u/(a+1))/2)),s=0;for(let u of i)s+=u;let c=s/a,f=0,m=0;for(let u of n){let h=0;for(let p=0;p<u.length;p++)h+=i[m+p];m+=u.length,f+=u.length*(h/u.length-c)**2}return f/=re(i),Mr("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",f,r-1)}function cs(e,t,n={}){if(typeof e=="string"){let N=Ye(e,t);return cs(N.x,N.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),a=te(n.alternative),l=r.length,i=o.length,s=l+i;if(s<3)throw new RangeError("not enough observations");let c=r.concat(o),f=Array.from({length:s},(N,M)=>M).sort((N,M)=>c[N]-c[M]),m=new Array(s),u=0;for(;u<s;){let N=u;for(;N+1<s&&c[f[N+1]]===c[f[u]];)N++;let M=0;for(let E=u;E<=N;E++)M+=(E+1-(s+1)/2)**2;M/=N-u+1;for(let E=u;E<=N;E++)m[f[E]]=M;u=N+1}let h=0;for(let N=0;N<l;N++)h+=m[N];let p=0;for(let N of m)p+=N;p/=s;let d=0;for(let N of m)d+=(N-p)**2;let g=l*p,v=l*i/(s*(s-1))*d,w=(h-g)/Math.sqrt(v),b=a==="less"?$(w):a==="greater"?$(w,{lowerTail:!1}):2*$(-Math.abs(w));return{method:"Mood two-sample test of scale",alternative:a,statistic:w,statisticName:"Z",pValue:b}}function gt(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var ii=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],li=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],ci=[.544,-.39978,.025054,-6714e-7],fi=[1.3822,-.77857,.062767,-.0020322],mi=[-1.5861,-.31082,-.083751,.0038915],ui=[-.4803,-.082676,.0030302],hi=[-2.273,.459];function pi(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]=-X((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let a=Math.sqrt(o),l=1/Math.sqrt(e),i=r[0]/a+gt(ii,l),s=new Float64Array(n);s[0]=i;let c,f;if(e>5){let m=r[1]/a+gt(li,l);s[1]=m,c=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*i*i-2*m*m))}else c=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*i*i));for(let m=c;m<n;m++)s[m]=r[m]/f;for(let m=0;m<n;m++)t[e-1-m]=s[m],t[m]=-s[m];return t}function fs(e){let t=e.length,n=pi(t),r=e[t-1]-e[0],o=0;for(let c of e)o+=c/r;o/=t;let a=0,l=0,i=0;for(let c=0;c<t;c++){let f=e[c]/r-o;a+=n[c]*n[c],l+=f*f,i+=n[c]*f}let s=Math.sqrt(a*l);return(s-i)*(s+i)/(a*l)}var di=1.90985931710274,gi=1.0471975511966;function ms(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let l=di*(Math.asin(Math.sqrt(1-e))-gi);return Math.max(l,0)}let n=Math.log(e);if(t<=11){let l=gt(hi,t);if(n>=l)return 1e-99;let i=gt(ci,t),s=Math.exp(gt(fi,t));return $(-Math.log(l-n),{mean:i,sd:s,lowerTail:!1})}let r=Math.log(t),o=gt(mi,r),a=Math.exp(gt(ui,r));return $(n,{mean:o,sd:a,lowerTail:!1})}function us(e,t,n={}){if(typeof e=="string"){let h=Co(e,t);return us(h.values,h.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=dt(e,r),a=n.varEqual??!1,l=o.length;if(l<2)throw new RangeError("not enough groups");let i=o.map(h=>h.length);if(i.some(h=>h<2))throw new RangeError("not enough observations");let s=o.map(se),c=o.map(re),f=i.reduce((h,p)=>h+p,0),m,u;if(a){let h=se(o.flat()),p=0,d=0;for(let g=0;g<l;g++)p+=i[g]*(s[g]-h)**2,d+=(i[g]-1)*c[g];u=f-l,m=p/(l-1)/(d/u)}else{let h=i.map((w,b)=>w/c[b]),p=h.reduce((w,b)=>w+b,0),d=0,g=0;for(let w=0;w<l;w++)d+=(1-h[w]/p)**2/(i[w]-1),g+=h[w]*s[w];d/=l*l-1,g/=p;let v=0;for(let w=0;w<l;w++)v+=h[w]*(s[w]-g)**2;m=v/((l-1)*(1+2*(l-2)*d)),u=1/(3*d)}return{method:a?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:m,statisticName:"F",parameter:[l-1,u],parameterName:["num df","denom df"],pValue:ae(m,l-1,u,!1,!1)}}function wi(e){let t=Array.from(e).filter(o=>!Number.isNaN(o)).sort((o,a)=>o-a),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=fs(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:ms(r,n)}}function Er(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=B(r,[e[o],0]);return r[0]}function vi([[e,t],[n,r]]){let o=e+n,a=t+r,l=e+t,i=Math.max(0,l-a),s=Math.min(l,o),c=new Float64Array(s-i+1);for(let f=i;f<=s;f++)c[f-i]=Et(f,o,a,l,!1);return{lo:i,d:c}}function bi(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 a=0;a<t.length;a++)n[r+a]+=o*t[a]}return n}var Pn=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let a=vi(o);n+=a.lo,r=bi(r,a.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),a=-1/0;for(let i=0;i<n;i++){let s=this.logNull[i]+(this.lo+i)*o;r[i]=s,s>a&&(a=s)}for(let i=0;i<n;i++)r[i]=Math.exp(r[i]-a);let l=Er(r,0,n);for(let i=0;i<n;i++)r[i]=r[i]/l;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=B(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(t,n,r){let o=this.density(n),a=t-this.lo;return r?Er(o,a,o.length):Er(o,0,a+1)}};function qr(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function hs(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:qr(r=>e.mean(r)-t,n>t)}function Ar(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:qr(o=>n-e.cdf(t,o,!1),r<n)}function Rr(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:qr(o=>e.cdf(t,o,!0)-n,r>n)}function Ni(e,t={}){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 a=e.map(f=>f.reduce((m,u)=>m+u.reduce((h,p)=>h+p,0),0));if(a.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let l=te(t.alternative),i=t.correct??!0,s=t.exact??!1,c=t.confLevel??.95;if(Me(c),r===2&&o===2){let f=e.map(m=>[[m[0][0],m[0][1]],[m[1][0],m[1][1]]]);return s?Mi(f,l,c):yi(f,a,l,i,c)}return xi(e,a,r,o)}function yi(e,t,n,r,o){let a=0,l=0,i=0,s=0,c=0,f=0,m=0,u=0;e.forEach(([[N,M],[E,y]],x)=>{let q=t[x],A=N+M,S=E+y,L=N+E,R=M+y;a+=N,l+=A*L/q,i+=A*S*L*R/(q*q*(q-1));let k=(N+y)/q,C=(M+E)/q,O=N*y/q,I=M*E/q;s+=O,c+=I,f+=k*O,m+=k*I+C*O,u+=C*I});let h=a-l,p=r&&Math.abs(h)>=.5?.5:0,d=(Math.abs(h)-p)**2/i,g=n==="two.sided"?he(d,.5,2,!1,!1):$(Math.sign(h)*Math.sqrt(d),{lowerTail:n==="less"}),v=s/c,w=Math.sqrt(f/(2*s*s)+m/(2*s*c)+u/(2*c*c)),b;if(n==="less")b=[0,v*Math.exp(X(o)*w)];else if(n==="greater")b=[v*Math.exp(X(o,{lowerTail:!1})*w),1/0];else{let N=X((1+o)/2);b=[v*Math.exp(-N*w),v*Math.exp(N*w)]}return{method:`Mantel-Haenszel chi-squared test ${p!==0?"with":"without"} continuity correction`,alternative:n,statistic:d,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:g,confInt:b,confLevel:o,estimate:v,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Mi(e,t,n){let r=new Pn(e),o=e.reduce((i,s)=>i+s[0][0],0),a;if(t==="two.sided"){let i=r.density(1),s=i[o-r.lo]*(1+1e-7),c=[0,0];for(let f of i)f<=s&&(c=B(c,[f,0]));a=Math.min(1,c[0])}else a=r.cdf(o,1,t==="greater");let l;if(t==="less")l=[0,Ar(r,o,1-n)];else if(t==="greater")l=[Rr(r,o,1-n),1/0];else{let i=(1-n)/2;l=[Rr(r,o,i),Ar(r,o,i)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:o,statisticName:"S",pValue:a,confInt:l,confLevel:n,estimate:hs(r,o),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function xi(e,t,n,r){let o=(n-1)*(r-1),a=new Array(o).fill(0),l=Array.from({length:o},()=>new Array(o).fill(0));e.forEach((c,f)=>{let m=t[f],u=c.map(d=>d.reduce((g,v)=>g+v,0)),h=Array.from({length:r},(d,g)=>c.reduce((v,w)=>v+w[g],0)),p=1/(m*m*(m-1));for(let d=0;d<r-1;d++)for(let g=0;g<n-1;g++){let v=g+(n-1)*d;a[v]+=c[g][d]-u[g]*h[d]/m;for(let w=0;w<r-1;w++)for(let b=0;b<n-1;b++){let N=(g===b?m*u[g]:0)-u[g]*u[b],M=(d===w?m*h[d]:0)-h[d]*h[w];l[v][b+(n-1)*w]+=p*N*M}}});let i=we(l,a),s=0;for(let c=0;c<o;c++)s+=a[c]*i[c];return{method:"Cochran-Mantel-Haenszel test",statistic:s,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:o,parameterName:"df",pValue:he(s,o/2,2,!1,!1)}}var ps=new Map;function Ei(e,t){let n=`${e},${t}`,r=ps.get(n);if(r!==void 0)return r;let o=e+t,a=[];for(let u=1;u<=o;u++)a.push(Math.min(u,o+1-u));let l=a.reduce((u,h)=>u+h,0),i=Array.from({length:e+1},()=>new Float64Array(l+1));i[0][0]=1;for(let u=0;u<o;u++){let h=o-u,p=a[u],d=Array.from({length:e+1},()=>new Float64Array(l+1));for(let g=0;g<=e;g++){let v=i[g],w=e-g;if(w>h)continue;let b=w/h;for(let N=0;N<=l;N++){let M=v[N];M!==0&&(w>0&&(d[g+1][N+p]+=M*b),w<h&&(d[g][N]+=M*(1-b)))}}i=d}let s=i[e],c=0;for(;c<s.length&&s[c]===0;)c++;let f=s.length-1;for(;f>c&&s[f]===0;)f--;let m={min:c,probs:s.slice(c,f+1)};return ps.set(n,m),m}function kr(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:a}=Ei(t,n),l=Math.floor(e+1e-7)-o,i=0;if(r)for(let s=0;s<=Math.min(l,a.length-1);s++)i+=a[s];else for(let s=Math.max(l+1,0);s<a.length;s++)i+=a[s];return Math.min(i,1)}var ds=new Map;function qi(e,t){let n=`${e},${t}`,r=ds.get(n);if(r!==void 0)return r;let o=e+t,a=0;for(let m=1;m<=o;m++)a+=Math.min(m,o+1-m);let l=Array.from({length:e+1},()=>new Float64Array(a+1));l[0][0]=1;for(let m=0;m<o;m++){let u=Math.min(m+1,o-m),h=Array.from({length:e+1},()=>new Float64Array(a+1));for(let p=0;p<=e;p++){if(e-p>o-m)continue;let d=l[p];for(let g=0;g<=a;g++){let v=d[g];v!==0&&(p<e&&(h[p+1][g+u]+=v),e-p<o-m&&(h[p][g]+=v))}}l=h}let i=l[e],s=0;for(;i[s]===0;)s++;let c=i.length-1;for(;i[c]===0;)c--;let f={min:s,counts:i.slice(s,c+1)};return ds.set(n,f),f}function $n(e,t,n){let{min:r,counts:o}=qi(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let a=Do(t+n,t),l=0;for(let i=0;i<o.length;i++)if(l+=o[i]/a,l>=e)return r+i;return r+o.length-1}function gs(e,t,n={}){if(typeof e=="string"){let b=Ye(e,t);return gs(b.x,b.y,n)}let r=Array.from(e).filter(b=>!Number.isNaN(b)),o=Array.from(t).filter(b=>!Number.isNaN(b)),a=r.length,l=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(l<1)throw new RangeError("not enough 'y' observations");let i=te(n.alternative),s=n.confInt??!1,c=n.confLevel??.95;s&&Me(c);let f=a+l,m=me(r.concat(o)),u=m.map(b=>Math.min(b,f-b+1)),h=0;for(let b=0;b<a;b++)h+=u[b];let p=new Set(m).size<f,d=n.exact??(a<50&&l<50&&!p),g,v;if(d&&!p){let b=M=>kr(M-1,a,l,!1),N=M=>kr(M,a,l,!0);if(i==="two.sided"){let M=Math.floor((a+1)**2/4)+Math.floor(a*l/2)/2;g=Math.min(1,2*(h>M?b(h):N(h)))}else i==="less"?g=b(h):g=N(h);s&&(v=Ri(r,o,i,c))}else{let b=0;for(let x of u)b+=x*x;let N=f%2===0,M=N?a*(f+2)/4:a*(f+1)**2/(4*f),E=N?a*l*(16*b-f*(f+2)**2)/(16*f*(f-1)):a*l*(16*f*b-(f+1)**4)/(16*f*f*(f-1)),y=(h-M)/Math.sqrt(E);g=i==="two.sided"?2*Math.min($(y),$(y,{lowerTail:!1})):i==="less"?$(y,{lowerTail:!1}):$(y),s&&(v=ki(r,o,i,c))}let w={method:"Ansari-Bradley test",alternative:i,statistic:h,statisticName:"AB",pValue:g};return v!==void 0&&(w.confInt=v.confInt,w.confLevel=c,w.estimate=v.estimate,w.estimateNames="ratio of scales"),w.nullValue=1,w.nullValueName="ratio of scales",w}function ws(e,t,n){let r=e.length,o=r+t.length,a=Ai(e.map(s=>s/n).concat(t)),l=a.map(s=>Math.min(s,o-s+1)),i=0;for(let s=0;s<r;s++)i+=l[s];return{ab:i,a:l,ties:new Set(a).size<o}}function Ai(e){let t=e.filter(a=>!Number.isNaN(a));if(t.length===e.length)return me(e);let n=me(t),r=0,o=t.length;return e.map(a=>Number.isNaN(a)?++o:n[r++])}function Tr(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Ri(e,t,n,r){let o=e.length,a=t.length,l=[];for(let g of e)for(let v of t)g/v>=0&&l.push(g/v);l.sort((g,v)=>g-v);let i=l.length;if(i===0)return{confInt:Tr(n,1-r,()=>[NaN,NaN]),estimate:NaN};let s=l.map((g,v)=>ws(e,t,g+(v<i-1?(l[v+1]-g)/2:g*1.01)).ab),c=(g,v,w)=>{let b=1/0;for(let M of s)(v?M-g>0:M-g>=0)&&M-g<b&&(b=M-g);let N=w?-1/0:1/0;return s.forEach((M,E)=>{M-g===b&&(N=w?Math.max(N,l[E]):Math.min(N,l[E]))}),N},f=g=>{let v=$n(g/2,o,a),w=$n(1-g/2,o,a);if(!s.some(M=>M-v>=0)||!s.some(M=>M-w>0))return[NaN,NaN];let b=c(v,!1,!1),N=c(w,!0,!0);return b>N?[c(w,!1,!1),c(v,!0,!0)]:[b,N]},m=Tr(n,1-r,f),u=$n(.5,o,a),h=-1/0,p=1/0;s.forEach((g,v)=>{g-u<=0?h=Math.max(h,l[v]):p=Math.min(p,l[v])});let d=h===-1/0||p===1/0?NaN:(p+h)/2;return{confInt:m,estimate:d}}function ki(e,t,n,r){let o=e.length,a=t.length,l=o+a,i=l%2===0,s=(v,w)=>{let{ab:b,a:N,ties:M}=ws(e,t,v),E=i?b-o*(l+2)/4:b-o*(l+1)**2/(4*l),y;if(!M)y=Math.sqrt(i?o*a*(l+2)*(l-2)/(48*(l-1)):o*a*(l+1)*(3+l*l)/(48*l*l));else{let x=0;for(let q of N)x+=q*q;y=Math.sqrt(i?o*a*(16*x-l*(l+2)**2)/(16*l*(l-1)):o*a*(16*l*x-(l+1)**4)/(16*l*l*(l-1)))}return E/y-w},c=[],f=(v,w)=>{v.length===0||w.length===0||c.push(Math.min(...v)/Math.max(...w),Math.max(...v)/Math.min(...w))};if(f(e.filter(v=>v>0),t.filter(v=>v>0)),f(e.filter(v=>v<=0),t.filter(v=>v<0)),c.some(v=>!Number.isFinite(v)))return;let m=Math.min(...c),u=Math.max(...c),h=v=>xe(w=>s(w,v),m,u,{tol:1e-4}),p=v=>{let w=X(v/2),b=X(v/2,{lowerTail:!1});if(s(m,w)>0||s(u,b)<0)return[NaN,NaN];let N=h(w),M=h(b);return N<=M?[N,M]:[M,N]},d=Tr(n,1-r,p),g=s(m,0)>0||s(u,0)<0?NaN:h(0);return{confInt:d,estimate:g}}function Ti(e,t,n={}){let r=Array.from(e).filter(x=>!Number.isNaN(x)),o=Array.from(t).filter(x=>!Number.isNaN(x)),a=r.length,l=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(l<1)throw new RangeError("not enough 'y' observations");let i=te(n.alternative),s=n.confLevel??.95;qn(s);let c=me(r.concat(o)),f=c.slice(0,a),m=c.slice(a),u=x=>x.reduce((q,A)=>q+A,0)/x.length,h=u(f),p=u(m),d=(x,q,A)=>{let S=(x.length+1)/2,L=0;for(let R=0;R<x.length;R++)L+=(x[R]-q[R]-A+S)**2;return L/(x.length-1)},g=d(f,me(r),h),v=d(m,me(o),p),w=(p-(l+1)/2)/a,b=a*g+l*v,N=a*l*(p-h)/(a+l)/Math.sqrt(b),M=b**2/((a*g)**2/(a-1)+(l*v)**2/(l-1)),E;i==="greater"?E=Ne(N,M,!0,!1):i==="less"?E=Ne(N,M,!1,!1):E=2*Ne(-Math.abs(N),M,!0,!1);let y=ze((1-s)/2,M,!1,!1)*Math.sqrt(g/(a*l**2)+v/(l*a**2));return{method:"Brunner-Munzel Test",alternative:i,statistic:N,statisticName:"Brunner-Munzel Test Statistic",parameter:M,parameterName:"df",pValue:E,confInt:[w-y,w+y],confLevel:s,estimate:w,estimateNames:"P(X<Y)+.5*P(X=Y)"}}var Si=[1.2337005501361697,7831619385924639e-32];function Li(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let d=f[h*t+p];if(d!==0)for(let g=0;g<t;g++)u[h*t+g]+=d*m[p*t+g]}return u},o=f=>{let m=0;for(let p of f)m=Math.max(m,Math.abs(p));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let p=0;p<f.length;p++)f[p]*=h;return u},a,l=0,i=e.slice(),s=0,c=n;for(;c>0;)c&1&&(a=a===void 0?i.slice():r(a,i),l+=s,l+=o(a)),c>>=1,c>0&&(i=r(i,i),s*=2,s+=o(i));return[a,l]}function vs(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=1/(2*e))return 0;if(t>=1)return 1;if(e*t<1){let[f,m]=xt(e,t),u=2*f-1+2*m,h=1,p=0;for(let d=1;d<=e;d++)h*=d/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,a=new Float64Array(r*r);for(let f=0;f<r;f++)for(let m=0;m<r;m++)a[f*r+m]=f-m+1>=0?1:0;for(let f=0;f<r;f++)a[f*r]-=Math.pow(o,f+1),a[(r-1)*r+f]-=Math.pow(o,r-f);2*o-1>0&&(a[(r-1)*r]+=Math.pow(2*o-1,r));for(let f=0;f<r;f++)for(let m=0;m<r;m++){let u=f-m+1;if(u>0){let h=1;for(let p=2;p<=u;p++)h*=p;a[f*r+m]/=h}}let[l,i]=Li(a,r,e),s=l[(n-1)*r+n-1],c=i;for(let f=1;f<=e;f++)s=s*f/e,Math.abs(s)<2**-500&&(s*=2**500,c-=500);for(;c>1e3;)s*=2**1e3,c-=1e3;for(;c<-1e3;)s*=2**-1e3,c+=1e3;return s*2**c}function Sr(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=be(Si,xt(e,e)),a=o[0]+o[1],l=Math.exp(-o[0])*(1-o[1]),i=0;for(let c=2;c<100;c++){let f=Math.exp(-(2*c-1)*(2*c-1)*a);if(i+=f,f<=1e-17*(i+l))break}let s=Math.sqrt(2*Math.PI)/e*(l+i);return t?s:1-s}let n=0;for(let o=1;o<100;o++){let a=Math.exp(-2*o*o*e*e);if(n+=o%2===1?a:-a,a<=1e-17*Math.abs(n))break}let r=2*n;return t?1-r:r}function bs(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=0)return 1;if(t>1)return 0;let[n,r]=xt(e,t),o=Math.floor(e-n-r),a=0;for(let l=0;l<=o;l++){let i=B(ot(e-l,-n),[-r,0]);if(i[0]<=0)break;let s=B(ot(l,n),[r,0]),c=be(s,[e,0]),f=be(i,[e,0]);a+=qo(l,e-l,c,f,!1)/(c[0]+c[1])}return Math.min(t*a,1)}function Ns(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 a=t+n,l;if(r.z!==void 0){if(r.z.length!==a)throw new RangeError("z must have length m + n");let u=[...r.z].sort((h,p)=>h-p);l=new Array(a+1).fill(!1);for(let h=1;h<a;h++)l[h]=u[h]!==u[h-1]}let i=e*t*n*(1-1e-10),s=(u,h)=>{let p=u*n-h*t;return(o==="two.sided"?Math.abs(p):o==="greater"?p:-p)>=i},c=new Float64Array(n+1),f=new Float64Array(n+1),m=0;for(let u=0;u<=t;u++){for(let h=0;h<=n;h++){if(u===0&&h===0){f[0]=1;continue}let p=u+h,d=(u>0?u/p*c[h]:0)+(h>0?h/p*f[h-1]:0);(l===void 0?p<a:l[p])&&s(u,h)?(d>0&&(m+=d*Et(u,t,n,p,!1)),f[h]=0):f[h]=d}[c,f]=[f,c],f.fill(0)}return Math.min(m,1)}function ys(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 Ms(e,t,n={}){if(typeof e=="string"){let w=Ye(e,t);return Ms(w.x,w.y,n)}let r=Array.from(e).filter(w=>!Number.isNaN(w)).sort((w,b)=>w-b),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let a=te(n.alternative),l=a==="two.sided"?"D":a==="greater"?"D^+":"D^-";if(typeof t=="function"){let w=-1/0,b=-1/0;for(let x=0;x<o;x++){let q=t(r[x]);w=Math.max(w,(x+1)/o-q),b=Math.max(b,q-x/o)}let N=a==="two.sided"?Math.max(w,b):a==="greater"?w:b,M=new Set(r).size<o,E=n.exact??(o<100&&!M),y;return a==="two.sided"?y=E?1-vs(o,N):Sr(Math.sqrt(o)*N,!1):y=E?bs(o,N):Math.exp(-2*o*N*N),{method:`${E?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:ys(a,!0),exact:E,statistic:N,statisticName:l,pValue:Math.min(1,Math.max(0,y))}}let i=Array.from(t).filter(w=>!Number.isNaN(w)),s=i.length;if(s<1)throw new RangeError("not enough 'y' data");let c=r.map(w=>[w,0]).concat(i.map(w=>[w,1]));c.sort((w,b)=>w[0]-b[0]);let f=0,m=0,u=0,h=0;for(let w=0;w<c.length;w++){if(c[w][1]===0?f++:m++,w+1<c.length&&c[w+1][0]===c[w][0])continue;let b=f/o-m/s;u=Math.max(u,b),h=Math.max(h,-b)}let p=a==="two.sided"?Math.max(u,h):a==="greater"?u:h,d=new Set(c.map(w=>w[0])).size<c.length,g=n.exact??o*s<1e4,v;if(g)v=Ns(p,o,s,d?{alternative:a,z:c.map(w=>w[0])}:{alternative:a});else{let w=o*s/(o+s);v=a==="two.sided"?Sr(Math.sqrt(w)*p,!1):Math.exp(-2*w*p*p)}return{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:ys(a,!1),exact:g,statistic:p,statisticName:l,pValue:Math.min(1,Math.max(0,v))}}function xs(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,a=ce(t);for(let i=0;i<r.length;i++){let s=a[i];if(typeof s=="number"&&Number.isNaN(s)||n&&Number.isNaN(r[i]))continue;let c=o.get(s);c===void 0&&o.set(s,c=[]),c.push(r[i])}let l=Array.from(o.keys()).sort((i,s)=>typeof i=="number"&&typeof s=="number"?i-s:String(i)<String(s)?-1:String(i)>String(s)?1:0);return{levels:l,groups:l.map(i=>o.get(i))}}function Lr(e,t,n){let r=[],o=[];for(let i=0;i<e-1;i++)for(let s=i+1;s<e;s++)r.push(t(s,i)),o.push([s-1,i]);let a=It(r,n),l=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([i,s],c)=>l[i][s]=a[c]),l}function Ir(e,t,n,r){return{method:e,pValue:n,rowNames:t.slice(1).map(ge),colNames:t.slice(0,-1).map(ge),pAdjustMethod:r}}function Ii(e,t,n={}){let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let a=te(n.alternative),l=n.pAdjustMethod??"holm",{levels:i,groups:s}=xs(e,t,!r),c=i.length;if(c<2)throw new RangeError("there must be at least two groups");let f,m;if(o){m="t tests with pooled SD";let u=s.map(v=>v.length),h=s.map(v=>v.length>0?se(v):NaN),p=0,d=0;s.forEach(v=>{v.length>1&&(p+=re(v)*(v.length-1),d+=v.length-1)});let g=Math.sqrt(p/d);f=(v,w)=>{let b=(h[v]-h[w])/(g*Math.sqrt(1/u[v]+1/u[w]));return a==="less"?Ne(b,d,!0,!1):a==="greater"?Ne(b,d,!1,!1):2*Ne(-Math.abs(b),d,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>Lt(s[u],s[h],{paired:r,alternative:a}).pValue;return Ir(m,i,Lr(c,f,l),l)}function Fi(e,t,n={}){let{pAdjustMethod:r,alternative:o,...a}=n,l=r??"holm",{levels:i,groups:s}=xs(e,t,!(a.paired??!1)),c=i.length;if(c<2)throw new RangeError("there must be at least two groups");let f="",m=Lr(c,(u,h)=>{let p=tn(s[u],s[h],{...a,...o===void 0?{}:{alternative:o}});return f=p.method,p.pValue},l);return Ir(f,i,m,l)}function Oi(e,t,n={}){if(e.length!==t.length)throw new RangeError("'x' and 'n' must have the same length");let r=[],o=[];e.forEach((f,m)=>{Number.isNaN(f)||Number.isNaN(t[m])||(r.push(f),o.push(t[m]))});let a=r.length;if(a<2)throw new RangeError("too few groups");let l=n.pAdjustMethod??"holm",i=n.correct??!0,s=Lr(a,(f,m)=>Nr([r[f],r[m]],[o[f],o[m]],{correct:i}).pValue,l),c=Array.from({length:a},(f,m)=>m+1);return Ir("Pairwise comparison of proportions",c,s,l)}var nn=[0,0];function Es(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=le(e,xt(t,t));return ot(t,n[0]/(2*t))}function jn(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Es([e[r],0]))}function Ft(e,t){return We(e,t,[]).fit}function We(e,t,n){let r=t.length,o=Math.min(e.length,r),a=e.map(f=>f.slice()),l=t.slice(),i=n.map(f=>f.slice()),s=[];for(let f=0;f<o;f++){let m=a[f],u=nn;for(let v=f;v<r;v++)u=B(u,oe(m[v],m[v]));if(u[0]===0){s.push(nn);continue}let h=Es(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=B(m[f],p);let d=oe(p,m[f]),g=v=>{let w=nn;for(let N=f;N<r;N++)w=B(w,oe(m[N],v[N]));let b=be(w,d);for(let N=f;N<r;N++)v[N]=le(v[N],oe(b,m[N]))};for(let v=f+1;v<e.length;v++)g(a[v]);g(l);for(let v of i)g(v);s.push([-p[0],-p[1]])}let c=new Array(o).fill(nn);for(let f=o-1;f>=0;f--){let m=l[f];for(let u=f+1;u<o;u++)m=le(m,oe(a[u][f],c[u]));c[f]=be(m,s[f])}return{fit:{qty:l,coef:c},qtv:i}}function rn(e,t=0,n=e.length){let r=nn;for(let o=t;o<n;o++)r=B(r,oe(e[o],e[o]));return r}function _i(e,t,n,r,o){let a=t.length,l=n.flatMap((d,g)=>rn(d)[0]>1e-5?[g]:[]),i=0,s=[],c=t.slice();if(l.length>0){let d=pe(Array.from({length:a},(g,v)=>l.map(w=>n[w][v][0])));i=d.rank,s=d.pivot.slice(0,i).map(g=>l[g]),c=Ft(s.map(g=>n[g]),t).qty}let f=[];s.forEach((d,g)=>{let v=r[d],w=f.find(b=>b.term===v);w===void 0&&f.push(w={term:v,df:0,ss:[0,0]}),w.df++,w.ss=B(w.ss,oe(c[g],c[g]))});let m=a-i,u=rn(c,i)[0],h=f.filter(d=>d.term!==0),p=h.map(d=>o[d.term-1]);if(m>0){let d=u/m,g=h.map(({df:v,ss:w})=>{let b=w[0]/v/d;return[v,w[0],w[0]/v,b,ae(b,v,m,!1,!1)]});return g.push([m,u,d,NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...p,"Residuals"],table:g}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:p,table:h.map(({df:d,ss:g})=>[d,g[0],g[0]/d])}}function qs(e,t,n,r,o,a){if(a.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let l=n??{type:"num",value:1,text:"1"},i=_o({response:t.response,rhs:l},Object.keys(o)).variables,s=ut(t,o,{...a,dropUnusedLevels:!0,contrastVariables:i},l),c=s.y;if(c===void 0)throw new TypeError("the formula has no response");let f=ut({response:t.response,rhs:r},o,{...a.levels===void 0?{}:{levels:a.levels},allContrasts:"contr.helmert",intercept:s.terms.intercept,extraVariables:s.terms.variables,dropUnusedLevels:!0}),m=c.length,u=[...s.warnings,...f.warnings.filter(y=>!s.warnings.includes(y))],h=f.colNames.length,p=h>0?pe(f.x):void 0,d=p?.rank??0;d<h&&u.push("Error() model is singular");let g=p?.pivot.slice(0,d)??[],v=y=>y.map(x=>[x,0]),w=s.colNames.map((y,x)=>v(s.x.map(q=>q[x]))),{fit:b,qtv:N}=We(g.map(y=>v(f.x.map(x=>x[y]))),v(c),w),M=[];g.forEach((y,x)=>{let q=f.assign[y],A=q===0?"(Intercept)":f.terms.termLabels[q-1],S=M.find(L=>L.name===A);S===void 0&&M.push(S={name:A,idx:[]}),S.idx.push(x)}),m>d&&M.push({name:"Within",idx:Array.from({length:m-d},(y,x)=>d+x)});let E=M.map(y=>_i(y.name,y.idx.map(x=>b.qty[x]),N.map(x=>y.idx.map(q=>x[q])),s.assign,s.terms.termLabels));return{formula:e,multistratum:!0,strata:E,rows:s.frame.rows,warnings:u}}function Fr(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=As(t.x,r,t.frame.weights,t.terms.intercept,!0),a=o.result,l=[...t.warnings];if(!n){let s=o.fitRows.map(u=>a.fittedValues[u]),c=s.reduce((u,h)=>u+h,0)/s.length,f=s.length>1?s.reduce((u,h)=>u+(h-c)**2,0)/(s.length-1):NaN,m=o.rss/a.dfResidual;Number.isFinite(m)&&m<1e-30*(c*c+f)&&l.push("essentially perfect fit: summary may be unreliable")}let i={...a,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:l};return t.frame.weights!==void 0&&(i.weights=t.frame.weights),i}function Or(e,t,n={}){return typeof e=="string"?Fr(e,ut(xn(e),t,{...n,dropUnusedLevels:!0}),!1):Vi(e,t,n)}function Vi(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]),a=Array.from(t);if(o.length!==a.length)throw new RangeError("variable lengths differ (found for 'x')");let l=n.weights===void 0?void 0:Array.from(n.weights);if(l!==void 0&&l.length!==a.length)throw new RangeError("variable lengths differ (found for '(weights)')");if(l?.some(m=>m<0))throw new RangeError("missing or negative weights not allowed");let i=a.map((m,u)=>!Number.isNaN(m)&&o[u].every(h=>!Number.isNaN(h))&&(l===void 0||!Number.isNaN(l[u]))),s=o.filter((m,u)=>i[u]).map(m=>r?[1,...m]:m),c=a.filter((m,u)=>i[u]),f=l?.filter((m,u)=>i[u]);return As(s,c,f,r).result}function Ci(e,t,n,r,o,a){let l=o.rank,i=o.pivot.slice(0,l),s=jn(n===void 0?void 0:r.map(w=>n[w]),r.length),c=i.map(w=>r.map((b,N)=>Te(s[N],e[b][w]))),{qty:f,coef:m}=Ft(c,r.map((w,b)=>Te(s[b],t[w]))),u=e[0]?.map(()=>NaN)??[];i.forEach((w,b)=>u[w]=m[b][0]);let h=e.map(w=>{let b=[0,0];return i.forEach((N,M)=>b=B(b,Te(m[M],w[N]))),b}),p=rn(f,l)[0],d=r.map(w=>n?.[w]??1),g=[0,0];if(a){let w=[0,0],b=[0,0];r.forEach((N,M)=>{w=B(w,[d[M],0]),b=B(b,Te(h[N],d[M]))}),g=be(b,w)}let v=[0,0];return r.forEach((w,b)=>{let N=le(h[w],g);v=B(v,Te(oe(N,N),d[b]))}),{coefficients:u,fittedValues:h.map(w=>w[0]),residuals:h.map((w,b)=>le([t[b],0],w)[0]),effects:f.map(w=>w[0]),rss:p,mss:v[0]}}function As(e,t,n,r,o=!1){if(t.length===0)throw new RangeError("0 (non-NA) cases");let a=t.map((D,G)=>n===void 0||n[G]!==0),l=e.filter((D,G)=>a[G]),i=t.filter((D,G)=>a[G]),s=n?.filter((D,G)=>a[G]),c=i.length;if(c===0)throw new RangeError("all weights are zero");let f=t.flatMap((D,G)=>a[G]?[G]:[]),m=s?.map(Math.sqrt),u=m===void 0?l:l.map((D,G)=>D.map(Q=>Q*m[G])),h=m===void 0?i:i.map((D,G)=>D*m[G]),p=pe(u),d=p.rank,g=o?Ci(e,t,n,f,p,r):void 0,v=g?.coefficients??Tt(p,h),w=g?.residuals??[],b=g?.fittedValues??[],N=c-d,M=s??new Array(c).fill(1),E=g?.rss??0;if(g===void 0){let D=An(p,h),G=m===void 0?D:D.map((ne,ve)=>ne/m[ve]),Q=0;t.forEach((ne,ve)=>{if(a[ve])w.push(G[Q]),b.push(ne-G[Q]),Q++;else{let ye=e[ve].reduce((Oe,_e,Ie)=>Number.isNaN(v[Ie])?Oe:Oe+_e*v[Ie],0);b.push(ye),w.push(ne-ye)}});for(let ne=0;ne<c;ne++)E+=M[ne]*G[ne]**2}let y=b.filter((D,G)=>a[G]),x=E/N,q=Math.sqrt(x),A=p.qr.slice(0,d).map(D=>D.slice(0,d)),S=d>0?kn(A):[],R=p.pivot.slice(0,d).map((D,G)=>[D,G]).sort((D,G)=>D[0]-G[0]),k=R.map(([D])=>v[D]),C=R.map(([,D])=>Math.sqrt(S[D][D]*x)),O=k.map((D,G)=>D/C[G]),I=O.map(D=>2*Ne(-Math.abs(D),N,!0,!1)),V={coefficients:v,residuals:w,fittedValues:b,rank:d,dfResidual:N,coefTable:{estimate:k,stdError:C,tValue:O,pValue:I},sigma:q,rSquared:0,adjRSquared:0,intercept:r,qr:p},z=r?1:0,Y=d-z,T={result:V,fitRows:f,effects:g?.effects??[],rss:E};if(Y===0)return T;let F=0;if(g!==void 0)F=g.mss;else if(r){let D=M.reduce((Q,ne)=>Q+ne,0),G=0;for(let Q=0;Q<c;Q++)G+=M[Q]*y[Q]/D;for(let Q=0;Q<c;Q++)F+=M[Q]*(y[Q]-G)**2}else for(let D=0;D<c;D++)F+=M[D]*y[D]**2;V.rSquared=F/(F+E),V.adjRSquared=1-(1-V.rSquared)*((c-z)/N);let P=F/Y/x;return V.fStatistic={value:P,numdf:Y,dendf:N},V.fPValue=ae(P,Y,N,!1,!1),T}function Di(e,t={}){let n=t.level??.95,r=ze((1+n)/2,e.dfResidual,!0,!1),{estimate:o,stdError:a}=e.coefTable,l=e.coefficients.map(()=>NaN),i=e.coefficients.map(()=>NaN),s=0;return e.coefficients.forEach((c,f)=>{Number.isNaN(c)||(l[f]=o[s]-r*a[s],i[f]=o[s]+r*a[s],s++)}),{lower:l,upper:i}}function Pi(e,t,n={}){let o=(t.length>0&&typeof t[0]!="number"?t.map(p=>Array.from(p)):Array.from(t,p=>[p])).map(p=>e.intercept?[1,...p]:p),a=e.rank,l=e.qr.pivot.slice(0,a),i=e.qr.qr.slice(0,a).map(p=>p.slice(0,a)),s=e.sigma**2,c=o.map(p=>p.reduce((d,g,v)=>Number.isNaN(e.coefficients[v])?d:d+g*e.coefficients[v],0)),f=o.map(p=>{let d=new Array(a).fill(0);for(let g=0;g<a;g++){let v=p[l[g]];for(let w=0;w<g;w++)v-=i[w][g]*d[w];d[g]=v/i[g][g]}return Math.sqrt(d.reduce((g,v)=>g+v*v,0)*s)}),m=n.interval??"none";if(m==="none")return{fit:c,seFit:f};let u=ze((1+(n.level??.95))/2,e.dfResidual,!0,!1),h=f.map(p=>u*Math.sqrt(p*p+(m==="prediction"?s:0)));return{fit:c,seFit:f,lwr:c.map((p,d)=>p-h[d]),upr:c.map((p,d)=>p+h[d])}}function Rs(e){let t=e.flat(),n=Qt(t),r=[0,0],o=[0,0],a=[];for(let l of e){let i=Qt(l);a.push(i[0]);let s=le(i,n);r=B(r,oe(oe(s,s),[l.length,0]));for(let c of l){let f=le([c,0],i);o=B(o,oe(f,f))}}return{ssb:r[0],ssw:o[0],means:a}}function _r(e,t,n={}){return typeof e=="string"?$i(e,t,n):ji(e,t)}function $i(e,t,n){let r=xn(e,!0),{treatment:o,errors:a}=Oo(r.rhs);if(a.length>1)throw new Error(`there are ${a.length} Error terms: only 1 is allowed`);return a.length===1?qs(e,r,o,a[0],t,n):Fr(e,ut(r,t,{...n,dropUnusedLevels:!0},o),!0)}function ji(e,t){let n=dt(e,t),r=n.length;if(r<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=n.reduce((u,h)=>u+h.length,0),{ssb:a,ssw:l}=Rs(n),i=r-1,s=o-r,c=a/i,f=l/s,m=c/f;return{df:[i,s],sumSq:[a,l],meanSq:[c,f],fValue:m,pValue:s>0?ae(m,i,s,!1,!1):NaN}}function zi(e,t,n={}){let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:a}=pt(e,r),l=a.length;if(l<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let i=n.confLevel??.95,c=a.reduce((d,g)=>d+g.length,0)-l,{ssw:f,means:m}=Rs(a),u=f/c,h=Lo(i,l,c),p={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:[]};for(let d=0;d<l-1;d++)for(let g=d+1;g<l;g++){let v=m[g]-m[d],w=Math.sqrt(u/2*(1/a[g].length+1/a[d].length));p.names.push(`${o[g]}-${o[d]}`),p.i.push(g+1),p.j.push(d+1),p.diff.push(v),p.lwr.push(v-h*w),p.upr.push(v+h*w),p.pAdj.push(Nn(Math.abs(v)/w,l,c,{lowerTail:!1}))}return p}function Wi(e,t,n={}){let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:a}=n,l=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&&a===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");if(Number.isNaN(l))throw new RangeError("mu must be a number");let i=te(n.alternative),s=n.confLevel??.95;if(!(s>=0&&s<=1))throw new RangeError("confLevel must be between 0 and 1");let c=Array.from(e).filter(b=>!Number.isNaN(b)),f=r===void 0?void 0:Array.from(r).filter(b=>!Number.isNaN(b));if(c.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=Qt(c)[0],u=f===void 0?NaN:Qt(f)[0],h=f===void 0?o/Math.sqrt(c.length):Math.sqrt(o**2/c.length+a**2/f.length),p=f===void 0?m:m-u,d=(p-l)/h,g,v;if(i==="less")g=$(d),v=[-1/0,p+X(s)*h];else if(i==="greater")g=$(d,{lowerTail:!1}),v=[p-X(s)*h,1/0];else{g=2*$(-Math.abs(d));let b=X(1-(1-s)/2)*h;v=[p-b,p+b]}let w=f!==void 0;return{method:w?"Two-sample z-Test":"One-sample z-Test",alternative:i,statistic:d,statisticName:"z",pValue:g,confInt:v,confLevel:s,estimate:w?[m,u]:m,estimateNames:w?["mean of x","mean of y"]:"mean of x",nullValue:l,nullValueName:w?"difference in means":"mean"}}var Ot=2**-52;function Gi(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function zn(e,t,n){return e.map((r,o)=>{let a=0;for(let l=0;l<r.length;l++)a+=r[l]*t[l];return a+n[o]})}function wt(e,t,n,r){let o=0;for(let a=0;a<t.length;a++)o+=e.devContribution(t[a],n[a],r[a]);return o}function ks(e,t){let{x:n,p:r,y:o,w:a,offset:l,fam:i,epsilon:s,maxit:c}=e,f=o.length;return Yt(t,()=>{let m=(L,R)=>L.every(k=>i.etaOk(k))&&R.every(k=>i.muOk(k));if(r===0){let L=l.slice();if(!L.every(O=>i.etaOk(O)))throw new Error("invalid linear predictor values in empty model");let R=L.map(O=>i.linkinv(O));if(!R.every(O=>i.muOk(O)))throw new Error("invalid fitted means in empty model");let k=L.map(O=>i.muEta(O)),C=R.map((O,I)=>Math.sqrt(a[I]*k[I]**2/i.variance(O))**2);return{coefficients:[],eta:L,mu:R,deviance:wt(i,o,R,a),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:a.map(O=>O>0),workingWeights:C,workingResiduals:o.map((O,I)=>(O-R[I])/k[I])}}let u,h;if(e.start!==void 0){if(e.start.length!==r)throw new RangeError(`length of 'start' should equal ${r} and correspond to initial coefs for ${Gi(e.columnNames)}`);h=e.start.slice(),u=zn(n,h,l)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(L=>i.link(L));let p=u.map(L=>i.linkinv(L));if(!m(u,p))throw new Error("cannot find valid starting values: please specify some");let d=wt(i,o,p,a),g=d,v=!1,w=!1,b=0,N=new Array(r).fill(0),M,E=a.map(()=>!1),y=[],x=Math.min(1e-7,s/1e3);for(b=1;b<=c;b++){let L=u.map(I=>i.muEta(I));for(let I=0;I<f;I++){if(!(a[I]>0))continue;let V=i.variance(p[I]);if(Number.isNaN(V))throw new Error("NAs in V(mu)");if(V===0)throw new Error("0s in V(mu)");if(Number.isNaN(L[I]))throw new Error("NAs in d(mu)/d(eta)")}E=a.map((I,V)=>I>0&&L[V]!==0);let R=[];for(let I=0;I<f;I++)E[I]&&R.push(I);if(R.length===0){t.add(`no observations informative at iteration ${b}`),v=!1;break}y=new Array(f).fill(0);let k=[],C=[];for(let I of R){let V=Math.sqrt(a[I]*L[I]**2/i.variance(p[I]));y[I]=V,k.push((u[I]-l[I]+(o[I]-p[I])/L[I])*V),C.push(n[I].map(z=>z*V))}M=pe(C,{tol:x});let O=Tt(M,k);if(O.some((I,V)=>M.pivot.indexOf(V)<M.rank&&!Number.isFinite(I))){t.add(`non-finite coefficients at iteration ${b}`),v=!1;break}if(N=O.map(I=>Number.isNaN(I)?0:I),w=!1,u=zn(n,N,l),p=u.map(I=>i.linkinv(I)),g=wt(i,o,p,a),!Number.isFinite(g)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated due to divergence");let I=0;for(;!Number.isFinite(g);){if(I>=c)throw new Error("inner loop 1; cannot correct step size");I++,N=N.map((V,z)=>(V+h[z])/2),u=zn(n,N,l),p=u.map(V=>i.linkinv(V)),g=wt(i,o,p,a)}w=!0}if(!m(u,p)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated: out of bounds");let I=0;for(;!m(u,p);){if(I>=c)throw new Error("inner loop 2; cannot correct step size");I++,N=N.map((V,z)=>(V+h[z])/2),u=zn(n,N,l),p=u.map(V=>i.linkinv(V))}w=!0,g=wt(i,o,p,a)}if(e.untilCoefficients){let I=Math.max(...N.map(Math.abs));if(h!==void 0&&N.every((V,z)=>Math.abs(V-h[z])<=8*Ot*I)){v=!0;break}}else if(Math.abs(g-d)/(Math.abs(g)+.1)<s){v=!0;break}d=g,h=N.slice()}b>c&&(b=c);let q=r,A=N.slice();if(M!==void 0){q=M.rank;for(let L=M.rank;L<r;L++)A[M.pivot[L]]=NaN}let S=u.map(L=>i.muEta(L));return{coefficients:A,eta:u,mu:p,deviance:g,iter:b,converged:v,boundary:w,rank:q,qr:M,used:E,workingWeights:y.map(L=>L*L),workingResiduals:o.map((L,R)=>(L-p[R])/S[R])}})}function Ts(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function Ss(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*Ot||o<10*Ot)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*Ot)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function _t(e,t,n,r={}){let o=Jt(e.familySpec),a="start"in r?r.start:e.start,l=Wo(o,e.yGiven,e.priorGiven,a!==void 0||r.etastart!==void 0,n),i=r.extraOffset===void 0?e.offset:e.offset.map((f,m)=>f+r.extraOffset[m]),s=e.x.map(f=>t.map(m=>f[m])),c=ks({x:s,p:t.length,y:l.y,w:l.weights,offset:i,fam:o,epsilon:r.epsilon??e.epsilon,maxit:r.maxit??e.maxit,columnNames:t.map(f=>e.columnNames[f]),start:a,etastart:r.etastart,mustart:l.mustart,untilCoefficients:r.untilCoefficients},n);return Ss(c,o,n,t.length===0),{fit:c,fam:o,y:l.y,w:l.weights,size:l.size,offset:i}}function Ui(e,t,n={}){let r=n.intercept??!0,o=Ts(e),l=t.length>0&&typeof t[0]!="number"?t.map(C=>Array.from(C)):Array.from(t),i=l.length;if(o.length!==i)throw new RangeError("variable lengths differ (found for 'x1')");let s=n.weights===void 0?void 0:Array.from(n.weights);if(s!==void 0&&s.length!==i)throw new RangeError("variable lengths differ (found for '(weights)')");let c=n.offset===void 0?void 0:Array.from(n.offset);if(c!==void 0&&c.length!==i)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((C,O)=>{let I=l[O];return!(Array.isArray(I)?I.some(z=>Number.isNaN(z)):Number.isNaN(I))&&C.every(z=>!Number.isNaN(z))&&!(s!==void 0&&Number.isNaN(s[O]))&&!(c!==void 0&&Number.isNaN(c[O]))}),m=o.filter((C,O)=>f[O]).map(C=>r?[1,...C]:C),u=l.filter((C,O)=>f[O]),h=u.length;if(h===0)throw new RangeError("0 (non-NA) cases");let p=s===void 0?new Array(h).fill(1):s.filter((C,O)=>f[O]);if(p.some(C=>C<0))throw new RangeError("negative weights not allowed");let d=c===void 0?new Array(h).fill(0):c.filter((C,O)=>f[O]),g=o.length>0?o[0].length:0,v=[...r?["(Intercept)"]:[],...Array.from({length:g},(C,O)=>`x${O+1}`)],w=v.length;if(m.some(C=>C.length!==w))throw new TypeError("all rows of x must have the same length");let b=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(b.link=n.link);let N={x:m,columnNames:v,yGiven:u,priorGiven:p,offset:d,hasOffset:c!==void 0,intercept:r,familySpec:b,epsilon:n.epsilon??1e-8,maxit:n.maxit??25,start:n.start===void 0?void 0:Array.from(n.start)};if(!(N.epsilon>0))throw new RangeError("value of 'epsilon' must be > 0");if(!(N.maxit>0))throw new RangeError("maximum number of iterations must be > 0");let M=new Ae,E=Array.from({length:w},(C,O)=>O),{fit:y,fam:x,y:q,w:A,size:S}=_t(N,E,M),L;if(r&&N.hasOffset){let C=new Ae,O=ks({x:m.map(()=>[1]),p:1,y:q,w:A,offset:d,fam:x,epsilon:N.epsilon,maxit:N.maxit,columnNames:["(Intercept)"],mustart:y.mu},C);Ss(O,x,C,!1),C.list.forEach(I=>M.add(I)),L=O.deviance,O.converged||M.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let C=0,O=0;for(let V=0;V<h;V++)C+=A[V],O+=A[V]*q[V];let I=O/C;L=Yt(M,()=>wt(x,q,q.map(()=>I),A))}else L=Yt(M,()=>wt(x,q,d.map(C=>x.linkinv(C)),A));let R=h-A.filter(C=>C===0).length,k=Go(x,q,y.mu,A,S,y.deviance,M)+2*y.rank;return{coefficients:y.coefficients,fittedValues:y.mu,linearPredictors:y.eta,deviance:y.deviance,nullDeviance:L,aic:k,rank:y.rank,dfResidual:R-y.rank,dfNull:R-(r?1:0),iter:y.iter,converged:y.converged,boundary:y.boundary,y:q,priorWeights:A,weights:y.workingWeights,residuals:y.workingResiduals,family:x.family,link:x.name,warnings:M.list,qr:y.qr,model:N}}function Wn(e){return Jt(e.model.familySpec)}function Vt(e,t){if(Wn(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/e.dfResidual}function Hi(e){if(e.qr===void 0||e.rank===0)return[];let t=e.qr.qr.slice(0,e.rank).map(n=>n.slice(0,e.rank));return kn(t)}function Vr(e,t={}){let n=new Ae,r=Wn(e),o=t.dispersion===void 0&&!r.fixedDispersion,a=t.dispersion??Vt(e,n),l=Hi(e),s=(e.qr===void 0?[]:e.qr.pivot.slice(0,e.rank)).map(h=>e.coefficients[h]),c=l.map((h,p)=>Math.sqrt(h[p]*a)),f=s.map((h,p)=>h/c[p]),m=!o,u=f.map(h=>m?2*$(-Math.abs(h)):e.dfResidual>0?2*Ne(-Math.abs(h),e.dfResidual,!0,!1):NaN);return o&&e.dfResidual===0&&(c.fill(NaN),f.fill(NaN)),{dispersion:a,estimate:s,stdError:c,statistic:f,pValue:u,statisticName:r.fixedDispersion?"z value":"t value",covUnscaled:l,covScaled:l.map(h=>h.map(p=>p*a)),dfResidual:e.dfResidual,warnings:n.list}}function Cr(e,t="deviance"){let n=Wn(e),{y:r,fittedValues:o,priorWeights:a}=e;switch(t){case"response":return r.map((l,i)=>l-o[i]);case"working":return e.residuals.slice();case"pearson":return r.map((l,i)=>(l-o[i])*Math.sqrt(a[i])/Math.sqrt(n.variance(o[i])));case"deviance":return e.dfResidual<=0?r.map(()=>0):r.map((l,i)=>Math.sign(l-o[i])*Math.sqrt(Math.max(n.devContribution(l,o[i],a[i]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function Dr(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let l=n.qr.length,i=Array.from({length:l},(c,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=zo(n,i).map(c=>c.reduce((f,m)=>f+m*m,0))}let o=0,a=e.weights.map(l=>l>0);return e.priorWeights.forEach((l,i)=>{let s=a[i]?r[o++]??0:0;l>0&&t.push(s>1-10*Ot?1:s)}),t}function Bi(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 Xi(e,t={}){let n=Dr(e),r=t.dispersion??Vt(e),o=Cr(e,"deviance").filter((l,i)=>e.priorWeights[i]>0),a=100*Ot*Bi(o.map(Math.abs));return o.map((l,i)=>{let s=(Math.abs(l)<a?0:l)/Math.sqrt(r*(1-n[i]));return Number.isFinite(s)?s:NaN})}function Qi(e,t={}){let n=Dr(e),r=t.dispersion??Vt(e);return Cr(e,"pearson").filter((a,l)=>e.priorWeights[l]>0).map((a,l)=>{let i=(a/(1-n[l]))**2*(n[l]/(r*e.rank));return Number.isFinite(i)?i:NaN})}function Ki(e,t={}){let n=Wn(e),r,o;if(t.newdata===void 0)r=e.model.x,o=e.linearPredictors.slice();else{r=Ts(t.newdata).map(m=>e.model.intercept?[1,...m]:m);let f=t.newoffset===void 0?r.map(()=>0):Array.from(t.newoffset);if(f.length!==r.length)throw new RangeError("newoffset must have one value per row of newdata");o=r.map((m,u)=>m.reduce((h,p,d)=>Number.isNaN(e.coefficients[d])?h:h+p*e.coefficients[d],0)+f[u])}let a=t.dispersion??Vt(e),l=e.rank,i=e.qr===void 0?[]:e.qr.pivot.slice(0,l),s=e.qr?.qr,c=r.map(f=>{let m=new Array(l).fill(0),u=0;for(let h=0;h<l;h++){let p=f[i[h]];for(let d=0;d<h;d++)p-=s[d][h]*m[d];m[h]=p/s[h][h],u+=m[h]*m[h]}return Math.sqrt(u)*Math.sqrt(a)});return(t.type??"link")==="link"?{fit:o,seFit:c}:Yt(new Ae,()=>({fit:o.map(f=>n.linkinv(f)),seFit:c.map((f,m)=>f*Math.abs(n.muEta(o[m])))}))}var Ls={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Pr(e,t,n,r,o=1e-11){let a=[];for(let d=0;d<t.length;d++)n[d]>0&&a.push(d);let l=0,i=0;for(let d of a)l+=n[d],i+=n[d]*t[d];let s=r?i/l:0,c=0;for(let d of a)c+=n[d]*(t[d]-s)**2;if((e.length>0?e[0].length:0)===0||a.length===0)return 0;let m=a.map(d=>Math.sqrt(n[d])),u=pe(a.map((d,g)=>e[d].map(v=>v*m[g])),{tol:o}),h=An(u,a.map((d,g)=>t[d]*m[g])),p=0;for(let d of h)p+=d*d;return c-p}function Zi(e,t={}){let n=t.level??.95,r=t.test??"LRT";if(!(n>0&&n<1))throw new RangeError("'level' must be in (0, 1)");let o=e.coefficients.length,a=t.parm??Array.from({length:o},(E,y)=>y);for(let E of a)if(!Number.isInteger(E)||E<0||E>=o)throw new RangeError(`invalid coefficient index ${E}`);let l=e.model,i=e.coefficients.map((E,y)=>y).filter(E=>!Number.isNaN(e.coefficients[E])),s=new Ae,c=_t(l,i,s,{...Ls,start:l.start===void 0?void 0:i.map(E=>l.start[E])}),f=new Array(o).fill(NaN);i.forEach((E,y)=>f[E]=c.fit.coefficients[y]);let m={...e,coefficients:f,weights:c.fit.workingWeights,residuals:c.fit.workingResiduals,qr:c.fit.qr,rank:c.fit.rank},u=Vr(m),h=u.dispersion,p=new Array(o).fill(NaN);(c.fit.qr===void 0?[]:c.fit.qr.pivot.slice(0,c.fit.rank)).forEach((E,y)=>p[i[E]]=u.stdError[y]);let g=l.x.map(E=>i.map(y=>E[y])),v=c.fit.eta,w=(E,y)=>{let x=i.filter(S=>S!==E),q=_t(l,x,new Ae,{...Ls,start:void 0,etastart:v,extraOffset:l.x.map(S=>S[E]*y)}).fit,A=r==="LRT"?q.deviance-c.fit.deviance:Pr(g,q.workingResiduals,q.workingWeights,!1);return Math.sign(y-f[E])*Math.sqrt(Math.max(A/h,0))},b=[X((1-n)/2),X((1+n)/2)],N=a.map(()=>NaN),M=a.map(()=>NaN);return a.forEach((E,y)=>{if(!Number.isNaN(f[E]))for(let x of[0,1]){let q=x===0?-1:1,A=NaN,S=NaN;for(let V=0;V<=30;V++){let z=f[E]+q*2**V*p[E],Y=w(E,z);if(Math.abs(Y)>=Math.abs(b[x])){A=z,S=Y;break}}if(Number.isNaN(A))continue;let L=V=>V===f[E]?-b[x]:w(E,V)-b[x],[R,k]=x===0?[A,f[E]]:[f[E],A],[C,O]=x===0?[S-b[x],-b[x]]:[-b[x],S-b[x]],I=xe(L,R,k,{tol:1e-14*p[E],maxIter:500,fLower:C,fUpper:O});x===0?N[y]=I:M[y]=I}}),{lower:N,upper:M}}var Yi=(e,t)=>he(e,t/2,2,!1,!1);function Ji(e,t,n){let{fit:r,w:o}=_t(e,t,n);return{dfRes:o.filter(l=>l>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(l=>t.map(i=>l[i])),intercept:e.intercept}}var $r=(e,t)=>Pr(t.x,e.fit.workingResiduals,e.fit.workingWeights,t.intercept);function el(e,t={}){let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=new Ae,o=Array.isArray(e)&&n.length>1,a=n[0],l=Jt(a.model.familySpec),i=l.fixedDispersion,s=t.test===void 0?t.dispersion!==void 0||i?"Chisq":"F":t.test,c=[],f,m,u=[],h;if(o){let y=n[0].model.x.length;if(n.some(x=>x.model.x.length!==y))throw new RangeError("models were not all fitted to the same size of dataset");if(n.forEach((x,q)=>c.push({name:String(q+1),dfRes:x.dfResidual,dev:x.deviance})),f=c.map((x,q)=>q===0?NaN:c[q-1].dfRes-x.dfRes),m=c.map((x,q)=>q===0?NaN:c[q-1].dev-x.dev),s==="Rao"){let x=n.map(q=>({dfRes:q.dfResidual,dev:q.deviance,fit:Is(q),x:q.model.x,intercept:q.model.intercept}));u=c.map((q,A)=>A===0?NaN:f[A]<0?-$r(x[A],x[A-1]):$r(x[A-1],x[A]))}h=n.reduce((x,q)=>q.dfResidual<x.dfResidual?q:x)}else{let y=a,x=y.model,q=x.x.length>0?x.x[0].length-(x.intercept?1:0):0,A=x.intercept?[0]:[],S=R=>[...A,...Array.from({length:R},(k,C)=>C+A.length)],L=[];for(let R=0;R<=q;R++)R===q?L.push({dfRes:y.dfResidual,dev:y.deviance,fit:Is(y),x:x.x,intercept:x.intercept}):R>0||s==="Rao"?L.push(Ji(x,S(R),r)):L.push(void 0);c.push({name:"NULL",dfRes:y.dfNull,dev:y.nullDeviance});for(let R=1;R<=q;R++)c.push({name:`x${R}`,dfRes:L[R].dfRes,dev:L[R].dev});f=c.map((R,k)=>k===0?NaN:c[k-1].dfRes-R.dfRes),m=c.map((R,k)=>k===0?NaN:Math.max(c[k-1].dev-R.dev,0)),s==="Rao"&&(u=c.map((R,k)=>k===0?NaN:$r(L[k-1],L[k]))),h=y}let p=new Ae,d=t.dispersion??Vt(h,p);p.list.forEach(y=>r.add(y));let g=i?1/0:h.dfResidual,v=o?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],w=c.map((y,x)=>o?[y.dfRes,y.dev,f[x],m[x]]:[f[x],m[x],y.dfRes,y.dev]),b=[...v],N=y=>c.map((x,q)=>{if(q===0||f[q]===0)return NaN;let A=y[q]/d*Math.sign(f[q]);return A<0||Number.isNaN(A)?NaN:Yi(A,Math.abs(f[q]))}),M=[];if(s==="Chisq"||s==="LRT")b.push("Pr(>Chi)"),M=[N(m)];else if(s==="Rao")b.push("Rao","Pr(>Chi)"),M=[u,N(u)];else if(s==="F"){if(i&&r.add(`using F test with a '${l.family}' family is inappropriate`),g!==0){let y=c.map((x,q)=>{let A=m[q]/f[q]/d;return q===0||f[q]===0||A<0?NaN:A});b.push("F","Pr(>F)"),M=[y,y.map((x,q)=>Number.isNaN(x)?NaN:ae(x,Math.abs(f[q]),g,!1,!1))]}}else if(s==="Cp"){let y=h.model.x.length;b.push("Cp"),M=[c.map(x=>x.dev+2*d*(y-x.dfRes))]}let E=w.map((y,x)=>[...y,...M.map(q=>q[x])]);return{colNames:b,rowNames:c.map(y=>y.name),table:E,warnings:r.list}}function Is(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}}function tl(e){let{pivot:t}=e.qr,n=[],r=new Map;for(let o=0;o<e.rank;o++){let a=e.assign[t[o]],l=r.get(a);l===void 0&&(l={term:a,df:0,ss:0},r.set(a,l),n.push(l)),l.df++,l.ss+=e.effects[o]**2}return{terms:n,rdf:e.dfResidual,rss:e.rss}}var Fs=(e,t,n)=>ae(e,t,n,!1,!1);function nl(e){let t=tl(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,a)=>r+=(e.weights?.[a]??1)*o*o),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,perfect:t.rss<1e-10*r}}function jr(e){let{names:t,rows:n,rdf:r,rss:o,perfect:a}=nl(e),l=o/r,i=n.map(({df:s,ss:c})=>{let f=c/s/l;return[s,c,c/s,f,Fs(f,s,r)]});return i.push([r,o,r===0?NaN:l,NaN,NaN]),{colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...t,"Residuals"],table:i,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function rl(e){let t=e.map(l=>l.dfResidual),n=e.map(l=>l.rss),r=0;t.forEach((l,i)=>{l<t[r]&&(r=i)});let o=n[r]/t[r],a=e.map((l,i)=>{if(i===0)return[t[0],n[0],NaN,NaN,NaN,NaN];let s=t[i-1]-t[i],c=n[i-1]-n[i],f=s===0?NaN:c/s/o;return f<0&&(f=NaN),[t[i],n[i],s,c,f,Number.isNaN(f)?NaN:Fs(f,Math.abs(s),t[r])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((l,i)=>String(i+1)),table:a,warnings:[]}}function zr(e,...t){let n=[e,...t];for(let l of n)if(typeof l!="object"||l===null||!Array.isArray(l.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1)return jr(e);if(n.some(l=>l.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=n.filter(l=>l.response===e.response),o=[];if(r.length<n.length){let l=[...new Set(n.filter(i=>i.response!==e.response).map(i=>i.response))];o.push(`models with response ${l.map(i=>`\u2018${i}\u2019`).join(", ")} removed because response differs from model 1`)}let a=r.length===1?jr(e):rl(r);return a.warnings=[...o,...a.warnings],a}function Wr(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0)return{strata:e.strata.filter(r=>r.stratum!=="Error: (Intercept)").map(r=>({...r})),warnings:[]};if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=jr(e);return e.dfResidual>0?{...t,warnings:[]}:{colNames:t.colNames.slice(0,3),rowNames:t.rowNames.slice(0,-1),table:t.table.slice(0,-1).map(n=>n.slice(0,3)),warnings:[]}}function Os(e,t,n){let r=[...t,...n];if(r.length===0)return{ss:0,df:0};let o=e.y.flatMap((u,h)=>e.weights===void 0||e.weights[h]!==0?[h]:[]),a=o.map(u=>e.weights===void 0?1:Math.sqrt(e.weights[u])),l=pe(o.map((u,h)=>r.map(p=>e.x[u][p]*a[h]))),i=jn(e.weights===void 0?void 0:o.map(u=>e.weights[u]),o.length),s=l.pivot.slice(0,l.rank),{qty:c}=Ft(s.map(u=>o.map((h,p)=>Te(i[p],e.x[h][r[u]]))),o.map((u,h)=>Te(i[h],e.y[u]))),f=[0,0],m=0;return s.forEach((u,h)=>{u>=t.length&&(f=B(f,oe(c[h],c[h])),m++)}),{ss:f[0],df:m}}function Gr(e,t={}){if(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,a=e.rss;if(o===0)throw new Error("residual df = 0");if(a<Math.sqrt(2220446049250313e-31))throw new Error("residual sum of squares is 0 (within rounding error)");let l=[],i=e.termLabels.length;r===2&&i===0&&e.intercept&&(l.push("the model contains only an intercept: Type III test substituted"),r=3);let s=p=>e.assign.flatMap((d,g)=>p.has(d)?[g]:[]),c=[...new Set(e.assign)],f=[],m=[];if(r===3){if(e.coefficients.some(p=>Number.isNaN(p)))throw new Error("there are aliased coefficients in the model");for(let p of c){let d=s(new Set([p])),g=s(new Set(c.filter(v=>v!==p)));f.push(p===0?"(Intercept)":e.termLabels[p-1]),m.push({ss:Os(e,g,d).ss,df:d.length})}}else{let p=d=>e.factors.flatMap((g,v)=>g[d-1]>0?[v]:[]);for(let d of c){if(d===0)continue;let g=p(d),v=c.filter(N=>N!==0&&N!==d&&g.every(M=>e.factors[M][N-1]>0)),w=s(new Set(c.filter(N=>N!==d&&!v.includes(N)))),b=Os(e,w,s(new Set([d])));f.push(e.termLabels[d-1]),m.push(b)}}let u=a/o,h=m.map(({ss:p,df:d})=>{let g=p/d/u;return[p,d,g,ae(g,d,o,!1,!1)]});return h.push([a,o,NaN,NaN]),{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...f,"Residuals"],table:h,warnings:l}}var Gn=[0,0];var _s=e=>e.map(t=>t.map(n=>n[0]));function on(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Gn);return r.forEach((a,l)=>{let i=t[l];for(let s=0;s<n;s++)o[s]=B(o[s],oe(a,i[s]))}),o})}function vt(e){let t=Gn;return e.forEach((n,r)=>t=B(t,n[r])),t}function Vs(e){let t=e.length,n=e.map(l=>l.slice()),r=Array.from({length:t},(l,i)=>i),o=1,a=!1;for(let l=0;l<t;l++){let i=l;for(let s=l+1;s<t;s++)Math.abs(n[s][l][0])>Math.abs(n[i][l][0])&&(i=s);if(n[i][l][0]===0){a=!0;continue}i!==l&&([n[l],n[i]]=[n[i],n[l]],[r[l],r[i]]=[r[i],r[l]],o=-o);for(let s=l+1;s<t;s++){let c=be(n[s][l],n[l][l]);n[s][l]=c;for(let f=l+1;f<t;f++)n[s][f]=le(n[s][f],oe(c,n[l][f]))}}return{a:n,perm:r,sign:o,singular:a}}function Cs(e){let{a:t,sign:n,singular:r}=Vs(e);if(r)return Gn;let o=[n,0];return t.forEach((a,l)=>o=oe(o,a[l])),o}function Ds(e){let t=e.length,{a:n,perm:r,singular:o}=Vs(e);if(o)throw new Error("singular matrix");let a=Array.from({length:t},()=>new Array(t).fill(Gn));for(let l=0;l<t;l++){let i=r.map(s=>[s===l?1:0,0]);for(let s=0;s<t;s++)for(let c=0;c<s;c++)i[s]=le(i[s],oe(n[s][c],i[c]));for(let s=t-1;s>=0;s--){for(let c=s+1;c<t;c++)i[s]=le(i[s],oe(n[s][c],i[c]));i[s]=be(i[s],n[s][s])}for(let s=0;s<t;s++)a[s][l]=i[s]}return a}var Ps=(e,t)=>he(e,t/2,2,!1,!1);function Ur(e){let t=e.length,n=Bt(vt(e),t),r=[1,0];for(let o=0;o<t;o++)r=oe(r,n);return n=be(Cs(e),r),n[0]}function Hr(e,t,n,r){let o=1-(2*t*t+t+2)/(6*t*n),a=(t+2)*(t-1)*(t-2)*(2*t**3+6*t*t+3*r+2)/(288*(n*t*o)**2),l=-n*o*Math.log(e),i=t*(t+1)/2-1,s=Ps(l,i),c=Ps(l,i+4);return s+a*(c-s)}var ol=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function sl(e,t,n){let r=e.length,o=v=>v.length===0?0:pe(Array.from({length:r},(w,b)=>v.map(N=>N[b]))).rank,a=n.length===0?void 0:pe(Array.from({length:r},(v,w)=>n.map(b=>b[w]))),l=a===void 0?[]:a.pivot.slice(0,a.rank).map(v=>n[v]),i=l.length,s=[...l,...t],c=pe(Array.from({length:r},(v,w)=>s.map(b=>b[w])));if(c.rank>o(t))throw new Error("X does not define a subspace of M");let f=c.pivot.slice(0,c.rank).map(v=>s[v]),m=v=>v.map(w=>[w,0]),u=new Array(r).fill([0,0]),h=We(f.map(m),u,e.map((v,w)=>e.map(b=>b[w]))).qtv,p=Array.from({length:r},(v,w)=>h.map(b=>b[w])),d=We(f.map(m),u,p).qtv,g=Array.from({length:c.rank-i},(v,w)=>i+w);return g.map(v=>g.map(w=>d[v][w]))}function al(e,t={}){let n=e.map(w=>Array.from(w)),r=n.length,o=n[0]?.length??0;if(o===0||n.some(w=>w.length!==o))throw new TypeError("y must be a matrix with at least one column");let a=t.group;if(a!==void 0&&a.length!==r)throw new RangeError("'group' must have one label per row of y");let l=a?.map(ge)??n.map(()=>""),i=[...new Set(l)].sort(ft),s=n.map(w=>w.map(()=>[0,0]));for(let w of i){let b=l.flatMap((N,M)=>N===w?[M]:[]);for(let N=0;N<o;N++){let M=[0,0];for(let y of b)M=B(M,[n[y][N],0]);let E=Bt(M,b.length);for(let y of b)s[y][N]=le([n[y][N],0],E)}}let c=r-i.length,f=Array.from({length:o},(w,b)=>Array.from({length:o},(N,M)=>{let E=[0,0];for(let y of s)E=B(E,oe(y[b],y[M]));return E})),m=t.idata??{index:Array.from({length:o},(w,b)=>b+1)},u=w=>{let b=St(w,m);if(b.x.length!==o)throw new RangeError("idata must have one row per column of y");return ol(b.x,b.colNames.length)},h=t.M===void 0?Array.from({length:o},(w,b)=>Array.from({length:o},(N,M)=>M===b?1:0)):u(t.M),p=u(t.X??"~0"),d=sl(f,h,p),g=d.length,v=Ur(d);return{statistic:v,statisticName:"W",pValue:Hr(v,g,c,o),method:"Mauchly's test of sphericity"}}var $s=Number.EPSILON**.25;function ie(e,t){if(e===void 0)return t[0];let n=t.find(o=>o===e);if(n!==void 0)return n;let r=t.filter(o=>o.startsWith(e));if(e.length===0||r.length!==1)throw new RangeError(`'arg' should be one of ${t.map(o=>`\u201C${o}\u201D`).join(", ")}`);return r[0]}var j=e=>e==null;function Le(e,t){if(e.filter(j).length!==1)throw new RangeError(t)}function Z(e,t,n,r={}){return mt(e,t,n,r).root}var bt=(e,t,n,r)=>bn(e,t,{ncp:n,lowerTail:r});function Br(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Xr="'sig.level' must be numeric in [0, 1]";function il(e={}){let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,a=e.sigLevel===void 0?.05:e.sigLevel;Le([t,n,o,r,a],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Br(a,Xr);let l=ie(e.type,["two.sample","one.sample","paired"]),i=ie(e.alternative,["two.sided","one.sided"]),s=e.strict??!1,c=e.tol??$s,f=l==="two.sample"?2:1,m=i==="two.sided"?2:1;m===2&&!j(n)&&(n=Math.abs(n));let u=(d,g,v,w)=>{let b=Math.max(1e-7,d-1)*f,N=ct(w/m,b,{lowerTail:!1}),M=Math.sqrt(d/f)*g/v,E=bt(N,b,M,!1);return s&&m===2?E+bt(-N,b,M,!0):E};if(j(r))r=u(t,n,o,a);else{let d=r;j(t)?t=Z(g=>u(g,n,o,a)-d,2,1e7,{tol:c,extendInt:"upX"}):j(o)?o=Z(g=>u(t,n,g,a)-d,n*1e-7,n*1e7,{tol:c,extendInt:"downX"}):j(n)?n=Z(g=>u(t,g,o,a)-d,o*1e-7,o*1e7,{tol:c,extendInt:"upX"}):a=Z(g=>u(t,n,o,g)-d,1e-10,1-1e-10,{tol:c,extendInt:"yes"})}let h=l==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":l==="two.sample"?"n is number in *each* group":void 0,p={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[l]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:a,power:r,alternative:i,...h===void 0?{}:{note:h},method:p}}function ll(e={}){let{n:t,p1:n,p2:r,power:o}=e,a=e.sigLevel===void 0?.05:e.sigLevel;Le([t,n,r,o,a],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Br(a,Xr);let l=ie(e.alternative,["two.sided","one.sided"]),i=e.strict??!1,s=e.tol??$s,c=l==="two.sided"?2:1,f=(u,h,p,d)=>{let g=X(d/c,{lowerTail:!1});if(i&&c===2){let v=(h+p)/2,w=h*(1-h),b=p*(1-p),N=v*(1-v),M=Math.abs(h-p),E=Math.sqrt(w+b);return $((Math.sqrt(u)*M-g*Math.sqrt(2*N))/E)+$((Math.sqrt(u)*M+g*Math.sqrt(2*N))/E,{lowerTail:!1})}return $((Math.sqrt(u)*Math.abs(h-p)-g*Math.sqrt((h+p)*(1-(h+p)/2)))/Math.sqrt(h*(1-h)+p*(1-p)))},m=[];if(j(o))o=f(t,n,r,a);else{let u=o;j(t)?t=Z(h=>f(h,n,r,a)-u,1,1e7,{tol:s,extendInt:"upX"}):j(n)?(n=Z(h=>f(t,h,r,a)-u,0,r,{tol:s,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")):j(r)?(r=Z(h=>f(t,n,h,a)-u,n,1,{tol:s,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")):(a=Z(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:s,extendInt:"upX"}),(a<0||a>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power"))}return{n:t,p1:n,p2:r,sigLevel:a,power:o,alternative:l,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m}}function cl(e={}){let{groups:t,n,betweenVar:r,withinVar:o,power:a}=e,l=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,r,o,a,l],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!j(t)&&t<2)throw new RangeError("number of groups must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in each group must be at least 2");Br(l,Xr);let i=(s,c,f,m,u)=>{let h=(s-1)*c*f/m,p=s-1,d=(c-1)*s;return it(Ze(u,p,d,{lowerTail:!1}),p,d,{ncp:h,lowerTail:!1})};if(j(a))a=i(t,n,r,o,l);else{let s=a;j(t)?t=Z(c=>i(c,n,r,o,l)-s,2,100):j(n)?n=Z(c=>i(t,c,r,o,l)-s,2,1e5):j(o)?o=Z(c=>i(t,n,r,c,l)-s,r*1e-7,r*1e7):j(r)?r=Z(c=>i(t,n,c,o,l)-s,o*1e-7,o*1e7):l=Z(c=>i(t,n,r,o,c)-s,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:l,power:a,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation"}}var fl={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 js(e="p",t="small"){let n=ie(e,["p","t","r","anov","chisq","f2"]),r=ie(t,["small","medium","large"]);return{test:n,size:r,effectSize:fl[n][["small","medium","large"].indexOf(r)],method:"Conventional effect size from Cohen (1982)"}}function ml(e,t){return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function ul(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");let n=0;for(let r=0;r<e.length;r++)n+=(t[r]-e[r])**2/e[r];return Math.sqrt(n)}function hl(e){let t=e.map(a=>a.reduce((l,i)=>l+i,0)),n=e[0]?.length??0,r=Array.from({length:n},(a,l)=>e.reduce((i,s)=>i+s[l],0)),o=0;for(let a=0;a<n;a++)for(let l=0;l<e.length;l++){let i=t[l]*r[a];o+=(e[l][a]-i)**2/i}return Math.sqrt(o)}var pl="\u2018sig.level\u2019 must be numeric in [0, 1]",dl="\u2018power\u2019 must be numeric in [0, 1]";function Je(e,t){let n=r=>!j(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(pl);if(n(t))throw new RangeError(dl)}function et(e,t){return typeof e=="string"?js(t,e).effectSize:e}var sn=["two.sided","less","greater"];function Un(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function zs(e,t,n,r){if(r==="less")return bt(ct(n,e),e,t,!0);if(r==="greater")return bt(ct(n,e,{lowerTail:!1}),e,t,!1);let o=ct(n/2,e,{lowerTail:!1});return bt(o,e,t,!1)+bt(-o,e,t,!0)}function Ws(e,t,n){return n==="greater"?$(X(1-t)-e,{lowerTail:!1}):n==="less"?$(X(t)-e):$(X(1-t/2)-e,{lowerTail:!1})+$(X(t/2)-e)}function gl(e={}){let{n:t,power:n}=e,r=et(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;Le([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),Je(o,n);let a=ie(e.type,["two.sample","one.sample","paired"]),l=ie(e.alternative,sn),i=a==="two.sample"?2:1;l==="two.sided"&&!j(r)&&(r=Math.abs(r));let s=(m,u,h)=>zs((m-1)*i,Math.sqrt(m/i)*u,h,l);if(j(n))n=s(t,r,o);else{let m=n;j(t)?t=Z(u=>s(u,r,o)-m,2+1e-10,1e9):j(r)?r=Z(u=>s(t,u,o)-m,...Un(l,[1e-7,10])):o=Z(u=>s(t,r,u)-m,1e-10,1-1e-10)}let c=a==="paired"?"n is number of *pairs*":a==="two.sample"?"n is number in *each* group":void 0,f={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[a]+" t test power calculation";return{n:t,d:r,sigLevel:o,power:n,alternative:l,...c===void 0?{}:{note:c},method:f}}function wl(e={}){let{n1:t,n2:n,power:r}=e,o=et(e.d,"t"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,o,r,a],"exactly one of n1, n2, d, power, and sig.level must be NULL"),Je(a,r),!j(t)&&t<2)throw new RangeError("number of observations in the first group must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in the second group must be at least 2");let l=ie(e.alternative,sn);l==="two.sided"&&!j(o)&&(o=Math.abs(o));let i=(s,c,f,m)=>zs(s+c-2,f/Math.sqrt(1/s+1/c),m,l);if(j(r))r=i(t,n,o,a);else{let s=r;j(t)?t=Z(c=>i(c,n,o,a)-s,2+1e-10,1e9):j(n)?n=Z(c=>i(t,c,o,a)-s,2+1e-10,1e9):j(o)?o=Z(c=>i(t,n,c,a)-s,...Un(l,[1e-7,10])):a=Z(c=>i(t,n,o,c)-s,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:a,power:r,alternative:l,method:"t test power calculation"}}function Qr(e,t,n,r,o,a){let{n:l,power:i}=t,s=et(e,n==="h"?"p":"t"),c=t.sigLevel===void 0?.05:t.sigLevel;if(Le([s,l,i,c],`exactly one of ${n}, n, power, and sig.level must be NULL`),Je(c,i),!j(l)&&l<1)throw new RangeError("number of observations in each group must be at least 1");let f=ie(t.alternative,sn);f==="two.sided"&&!j(s)&&(s=Math.abs(s));let m=(u,h,p)=>Ws(u*r(h),p,f);if(j(i))i=m(s,l,c);else{let u=i;j(s)?s=Z(h=>m(h,l,c)-u,...Un(f,[a,10])):j(l)?l=Z(h=>m(s,h,c)-u,o,1e9):c=Z(h=>m(s,l,h)-u,1e-10,1-1e-10)}return{es:s,n:l,sigLevel:c,power:i,alternative:f}}function vl(e={}){let t=Qr(e.h,e,"h",n=>Math.sqrt(n/2),2.0000000001,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"}}function bl(e={}){let t=Qr(e.h,e,"h",n=>Math.sqrt(n),2.0000000001,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)"}}function Nl(e={}){let t=Qr(e.d,e,"d",n=>Math.sqrt(n),1.0000000001,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"}}function yl(e={}){let{n1:t,n2:n,power:r}=e,o=et(e.h,"p"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([o,t,n,r,a],"exactly one of h, n1, n2, power, and sig.level must be NULL"),Je(a,r),!j(t)&&t<2)throw new RangeError("number of observations in the first group must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in the second group must be at least 2");let l=ie(e.alternative,sn);l==="two.sided"&&!j(o)&&(o=Math.abs(o));let i=(s,c,f,m)=>Ws(s*Math.sqrt(c*f/(c+f)),m,l);if(j(r))r=i(o,t,n,a);else{let s=r;j(o)?o=Z(c=>i(c,t,n,a)-s,...Un(l,[1e-10,10])):j(t)?t=Z(c=>i(o,c,n,a)-s,2+1e-10,1e9):j(n)?n=Z(c=>i(o,t,c,a)-s,2+1e-10,1e9):a=Z(c=>i(o,t,n,c)-s,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:a,power:r,alternative:l,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes"}}function Ml(e={}){let{n:t,power:n}=e,r=et(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),Je(o,n),!j(t)&&t<4)throw new RangeError("number of observations must be at least 4");let a=ie(e.alternative,sn);a==="two.sided"&&!j(r)&&(r=Math.abs(r));let l=(s,c,f,m)=>{let u=ct(m?f/2:f,s-2,{lowerTail:!1}),h=Math.sqrt(u**2/(u**2+s-2)),p=Math.atanh(c)+c/(2*(s-1)),d=Math.atanh(h),g=$((p-d)*Math.sqrt(s-3));return m?g+$((-p-d)*Math.sqrt(s-3)):g},i=(s,c,f)=>a==="two.sided"?l(s,c,f,!0):l(s,a==="less"?-c:c,f,!1);if(j(n))n=i(t,r,o),a==="less"&&(r=-r);else{let s=n;if(j(t))t=Z(c=>i(c,r,o)-s,4+1e-10,1e9);else if(j(r)){let[c,f]=a==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=Z(m=>i(t,m,o)-s,c,f)}else o=Z(c=>i(t,r,c)-s,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:a,method:"approximate correlation power calculation (arctangh transformation)"}}function xl(e){let{N:t,power:n}=e,{df:r}=e,o=et(e.w,"chisq"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([o,t,n,a],"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(Je(a,n),!j(o)&&o<0)throw new RangeError("w must be positive");if(!j(t)&&t<1)throw new RangeError("number of observations must be at least 1");let l=(i,s,c)=>je(Ro(c,r,{lowerTail:!1}),r,{ncp:s*i**2,lowerTail:!1});if(j(n))n=l(o,t,a);else{let i=n;j(o)?o=Z(s=>l(s,t,a)-i,1e-10,1e9):j(t)?t=Z(s=>l(o,s,a)-i,1+1e-10,1e9):a=Z(s=>l(o,t,s)-i,1e-10,1-1e-10)}return{w:o,N:t,df:r,sigLevel:a,power:n,method:"Chi squared power calculation",note:"N is the number of observations"}}function El(e={}){let{k:t,n,power:r}=e,o=et(e.f,"anov"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,o,r,a],"exactly one of k, n, f, power, and sig.level must be NULL"),Je(a,r),!j(o)&&o<0)throw new RangeError("f must be positive");if(!j(t)&&t<2)throw new RangeError("number of groups must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in each group must be at least 2");let l=(i,s,c,f)=>{let m=i-1,u=(s-1)*i;return it(Ze(f,m,u,{lowerTail:!1}),m,u,{ncp:i*s*c**2,lowerTail:!1})};if(j(r))r=l(t,n,o,a);else{let i=r;j(t)?t=Z(s=>l(s,n,o,a)-i,2+1e-10,100):j(n)?n=Z(s=>l(t,s,o,a)-i,2+1e-10,1e9):j(o)?o=Z(s=>l(t,n,s,a)-i,1e-7,1e7):a=Z(s=>l(t,n,o,s)-i,1e-10,1-1e-10)}return{k:t,n,f:o,sigLevel:a,power:r,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation"}}function ql(e={}){let{u:t,v:n,power:r}=e,o=et(e.f2,"f2"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,o,r,a],"exactly one of u, v, f2, power, and sig.level must be NULL"),Je(a,r),!j(o)&&o<0)throw new RangeError("f2 must be positive");if(!j(t)&&t<1)throw new RangeError("degree of freedom u for numerator must be at least 1");if(!j(n)&&n<1)throw new RangeError("degree of freedom v for denominator must be at least 1");let l=(i,s,c,f)=>it(Ze(f,i,s,{lowerTail:!1}),i,s,{ncp:c*(i+s+1),lowerTail:!1});if(j(r))r=l(t,n,o,a);else{let i=r;j(t)?t=Z(s=>l(s,n,o,a)-i,1+1e-10,100):j(n)?n=Z(s=>l(t,s,o,a)-i,1+1e-10,1e9):j(o)?o=Z(s=>l(t,n,s,a)-i,1e-7,1e7):a=Z(s=>l(t,n,o,s)-i,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:a,power:r,method:"Multiple regression power calculation"}}var Al="Missing values detected. NAs dropped.",Bs={tol:0,maxiter:5e3};function Rl(e){return Math.exp(sr(e/2)-Math.log(Math.sqrt(e/2))-sr((e-1)/2))}function Gs(e,t,n){return mt(r=>bn(e,t,{ncp:r})-n,e-1,e+1,{...Bs,extendInt:"downX"}).root}function Xs(e,t,n,r){let o=n.pooledSd??!0,a=n.mu??0,l=n.paired??!1,i=n.ci===void 0?.95:n.ci,s=ie(n.alternative,["two.sided","less","greater"]),c=Array.from(e),f=t===void 0?void 0:Array.from(t),m=[];(c.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(Al);let u,h,p,d,g;if(f===void 0||l){let x;if(f===void 0)x=qe(c);else{let[A,S]=Rt(c,f);x=A.map((L,R)=>L-S[R])}let q=x.length;u=se(x),h=Math.sqrt(re(x)),p=h/Math.sqrt(q),d=1/q,g=q-1}else{let x=qe(c),q=qe(f),A=x.length,S=q.length;u=se(x)-se(q);let L=Math.sqrt(re(x)),R=Math.sqrt(re(q));o?(h=Math.sqrt(((A-1)*L**2+(S-1)*R**2)/(A+S-2)),p=h*Math.sqrt(1/A+1/S),d=1/A+1/S,g=A+S-2):(h=Math.sqrt((L**2+R**2)/2),d=2*(S*L**2+A*R**2)/(A*S*(L**2+R**2)),p=Math.sqrt(L**2/A+R**2/S),g=p**4/((L**2/A)**2/(A-1)+(R**2/S)**2/(S-1)))}let v=r?Rl(g):1,w=(u-a)/h*v,b=r?"Hedges_g":"Cohens_d";if(i===null)return{name:b,value:w,warnings:m};let N=s==="two.sided"?i:2*i-1,M=(u-a)/p,E=Gs(M,g,(1+N)/2)*Math.sqrt(d)*v,y=Gs(M,g,(1-N)/2)*Math.sqrt(d)*v;return s==="less"&&(E=-1/0),s==="greater"&&(y=1/0),{name:b,value:w,ci:i,ciLow:E,ciHigh:y,warnings:m}}function kl(e,t,n={}){return Xs(e,t,n,n.adjust??!1)}function Tl(e,t,n={}){return Xs(e,t,n,!0)}var Sl=e=>Array.isArray(e.effects),Qs=e=>e.multistratum===!0,Ll=e=>Array.isArray(e.strata),Ks=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Il=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),Kr=e=>new RegExp(`\\b${Il(e)}\\b`);function Fl(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let a=o.stratum.replace(/^Error: /,""),l=o.colNames.indexOf("Df"),i=o.colNames.indexOf("Sum Sq"),s=o.rowNames.map((f,m)=>({name:f,df:o.table[m][l],ss:o.table[m][i]})),c=s.find(f=>f.name==="Residuals");if(c===void 0)throw new Error(`the error stratum '${a}' has no residual degrees of freedom`);return{name:a,terms:s.filter(f=>f!==c),sse:c.ss,dfe:c.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 Ol(e){let t=e.univariate,n=t.colNames.indexOf("Sum Sq"),r=[],o=(s,c,f)=>{let m=r.find(u=>u.name===s);return m===void 0&&r.push(m={name:s,terms:[],sse:c,dfe:f}),m},a=new Map;if(t.colNames.includes("Error SS")){let s=t.colNames.indexOf("num Df"),c=t.colNames.indexOf("Error SS"),f=t.colNames.indexOf("den Df");t.rowNames.forEach((m,u)=>{let h=t.table[u],p=e.within.filter(v=>Kr(v).test(m)),d=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[c],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[s],ss:h[n]};d.terms.push(g),a.set(m,g)})}else{let s=t.colNames.indexOf("Df"),c=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[c][n],t.table[c][s]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][s],ss:t.table[u][n]};f.terms.push(h),a.set(m,h)})}let l=r.find(s=>s.name==="Subject");if(l===void 0)throw new Error("the result has no subject stratum");let i=e.anova.rowNames.map(s=>{let c=a.get(s);if(c===void 0)throw new Error(`there is no univariate test of '${s}'`);return c});return{strata:r,subject:l,order:i}}function Zs(e,t){let n=Array.isArray(e)?e:[],r=n.filter(a=>!t.some(l=>Kr(a).test(l.name)));if(r.length>0)throw new Error(`Observed variable not in data: ${r.join(", ")}`);let o=a=>n.some(l=>Kr(l).test(a));return{isObserved:o,ssObserved:t.reduce((a,l)=>o(l.name)?a+l.ss:a,0)}}function Us(e,t,n,r){let o=a=>it(e,t,n,{ncp:a})-r;return o(0)>0?e===1/0?1/0:mt(o,0,Math.max(10,e*t*4),{...Bs,extendInt:"downX"}).root:0}function Ys(e,t,n,r,o){let a=o==="two.sided"?r:2*r-1,l=Math.max(0,e),i=l/t/((1-l)/n),s=Us(i,t,n,(1+a)/2),c=Us(i,t,n,(1-a)/2);i<=Ze((1-a)/2,t,n)&&(c=0),i<=Ze((1+a)/2,t,n)&&(s=0);let f=s===1/0?1:s/(s+n),m=c===1/0?1:c/(c+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}var Hs="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Zr(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Hs);if(Qs(e)||Ll(e)||Ks(e))return _l(e,t,n);let r=Sl(e)?zr(e):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Hs);let o=n.ci===void 0?.95:n.ci,a=ie(n.alternative??"greater",["two.sided","less","greater"]),l=r.colNames.indexOf("Df"),i=r.colNames.indexOf("Sum Sq");if(l<0||i<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let s=r.rowNames.map((y,x)=>({name:y,df:r.table[x][l],ss:r.table[x][i]})).filter(y=>y.name!=="(Intercept)"),c=s.find(y=>y.name==="Residuals");if(c===void 0)throw new TypeError("the table has no row 'Residuals'");let f=s.filter(y=>y!==c),m=c.df,u=c.ss,h=u/m,p=s.reduce((y,x)=>y+x.ss,0),d=s.reduce((y,x)=>y+x.df,0)+1,g=t==="Eta2"?n.generalized??!1:!1,w=f.length===1?"plain":g!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:b,ssObserved:N}=Zs(g,f),M=y=>{let{ss:x,df:q}=y;if(t==="Eta2")return w==="partial"?x/(x+u):w==="plain"?x/p:x/(x+u+N-(b(y.name)?x:0));if(t==="Omega2"){let S=w==="partial"?(x-q*h)/(x+(d-q)*h):(x-q*h)/(p+h);return Math.max(0,S)}let A=w==="partial"?(x-q*h)/(x+u):(x-q*h)/p;return Math.max(0,A)},E=`${t}${w==="plain"?"":`_${w}`}`;return{colNames:o===null?[E]:[E,"CI","CI_low","CI_high"],rowNames:f.map(y=>y.name),table:f.map(y=>o===null?[M(y)]:Ys(M(y),y.df,m,o,a)),warnings:[]}}function _l(e,t,n){let r=n.ci===void 0?.95:n.ci,o=ie(n.alternative??"greater",["two.sided","less","greater"]),a=Ks(e),l,i,s;if(a){let N=Ol(e);({strata:l,subject:i}=N),s=N.order.map(M=>({term:M,stratum:N.strata.find(E=>E.terms.includes(M))}))}else({strata:l,subject:i}=Fl(Qs(e)?Wr(e):e)),s=l.flatMap(N=>N.terms.map(M=>({term:M,stratum:N})));let c=l.flatMap(N=>N.terms),f=l.reduce((N,M)=>N+M.sse,0),m=c.reduce((N,M)=>N+M.ss,0)+f,u=i.sse/i.dfe,h=t==="Eta2"?n.generalized??!1:!1,p=h!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:g}=Zs(h,c),v=(N,M)=>{let{ss:E,df:y}=N,x=M.sse/M.dfe;if(t==="Eta2")return p==="partial"?E/(E+M.sse):p==="plain"?E/m:E/(E+f+g-(d(N.name)?E:0));if(t==="Omega2"){let A=M===i?0:1,S=p==="partial"?(E-y*x)/(E+A*M.sse+i.sse+u):(E-y*x)/(m+u);return Math.max(0,S)}let q=p==="partial"?(E-y*x)/(E+M.sse):(E-y*x)/m;return Math.max(0,q)},w=`${t}${p==="plain"?"":`_${p}`}`,b={colNames:r===null?[w]:[w,"CI","CI_low","CI_high"],rowNames:s.map(N=>N.term.name),table:s.map(({term:N,stratum:M})=>{let E=v(N,M);return r===null?[E]:Ys(E,N.df,M.dfe,r,o)}),warnings:[]};return a||(b.groups=s.map(N=>N.stratum.name)),b}function Vl(e,t={}){return Zr(e,"Eta2",t)}function Cl(e,t={}){return Zr(e,"Omega2",t)}function Dl(e,t={}){return Zr(e,"Epsilon2",t)}var Hn=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Pl=Math.sqrt(2220446049250313e-31);function Bn(e,t){let n=Object.prototype.hasOwnProperty.call(e,t)?e[t]:void 0;if(n===void 0)throw new Error(`object '${t}' not found`);return Array.from(n)}var an=e=>typeof e=="boolean"?e?"TRUE":"FALSE":ge(e);function $l(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(an):[...new Set(t.map(an))].sort(ft)}var Js=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function ea(e,t,n,r){let o=s=>r.some(c=>new RegExp(`\\b${Js(c)}\\b`).test(s));for(let s of r)if(!e.some(c=>new RegExp(`\\b${Js(s)}\\b`).test(c.name)))throw new Error(`Observed variable not in data: ${s}`);let a=[...new Set(t)].reduce((s,c)=>s+c,0),l=e.reduce((s,c)=>o(c.name)?s+c.ss:s,0),i=["pes","ges"].filter(s=>n.includes(s));return{colNames:["num Df","den Df","MSE","F",...i,"Pr(>F)"],rowNames:e.map(s=>s.name),table:e.map(s=>{let c=i.map(f=>f==="pes"?s.ss/(s.ss+s.errSS):s.ss/(s.ss+a+l-(o(s.name)?s.ss:0)));return[s.numDf,s.denDf,s.errSS/s.denDf,s.f,...c,s.p]})}}function jl(e,t){let n=Hn(t.between),r=Hn(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");let o=t.type??3;if(o!==2&&o!==3)throw new RangeError(`invalid type (${String(o)}): use 2 or 3`);let a=t.correction??"GG",l=Hn(t.es??"ges"),i=Hn(t.observed),s=[],c=ce(Bn(e,t.id)).map(an),f=Bn(e,t.dv).map(T=>{if(typeof T!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);return T}),m=n.map(T=>ce(Bn(e,T))),u=r.map(T=>ce(Bn(e,T))),h=c.length;for(let T of[f,...m,...u])if(T.length!==h)throw new RangeError("the columns must have the same length");let p=m.map(T=>$l(T)),d=m.map(T=>T.map(an)),g=u.map(T=>T.map(an)),v=g.map(T=>[...new Set(T)]);p.forEach((T,F)=>{if(T.length<2)throw new Error(`Factor "${n[F]}" consists of one level only. Remove factor from model?`)});let w=[...new Set(c)],b=new Map,N=new Set;if(c.forEach((T,F)=>{let P=JSON.stringify(d.map(G=>G[F])),D=b.get(T);D===void 0?b.set(T,P):D!==P&&N.add(T)}),N.size>0)throw new Error(`Following ids are in more than one between subjects condition:
1
+ /*! rStats.js 0.15.2 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
+ import{A as Mn,b as je,c as Ro,d as Et,e as cr,j as qt,k as ko,o as To,p as At,q as So,u as Nn,v as Lo,w as Xt,x as Io,z as yn}from"./chunks/chunk-JLAH3X77.js";import{a as En,b as Ye,c as Zt,d as Vo,e as Co,f as mr,g as te,h as qn,i as Me,j as jo,k as ka,m as Tn,n as en}from"./chunks/chunk-VWJ74UHC.js";import"./chunks/chunk-3WQCKGKP.js";import{b as $o,c as mt,e as Sn}from"./chunks/chunk-AJBLCNZ7.js";import{b as fr}from"./chunks/chunk-YGT4P7EU.js";import"./chunks/chunk-4Z2XDWKV.js";import{e as Qt,f as se,g as re,h as Fo,i as ee,j as me,k as Fe,l as qe,m as Rt}from"./chunks/chunk-IBPWRQIF.js";import{a as Do,d as Po}from"./chunks/chunk-XPLDEK6D.js";import{a as ut,b as St,c as Ae,d as Yt,e as Jt,f as Wo,g as Go}from"./chunks/chunk-RCAL4VHM.js";import{D as vn,E as lt,c as he,e as ar,n as ir,o as st,t as ae,u as at,w as it,x as Ze,z as lr}from"./chunks/chunk-EBQMGCQZ.js";import{B as kn,E as ht,h as pe,j as zo,k as Tt,l as An,r as Rn,v as we}from"./chunks/chunk-XNPU54NH.js";import{a as xn,b as Oo,g as _o}from"./chunks/chunk-PEYGUK32.js";import{o as Ne,p as ze,r as bn,s as ct}from"./chunks/chunk-UFT662QZ.js";import{a as ft}from"./chunks/chunk-HBMHR5SS.js";import{a as Kt,b as kt,c as ce,d as ge}from"./chunks/chunk-PWVQVJTP.js";import{j as Ao}from"./chunks/chunk-D4F7GQAY.js";import{f as Se,g as $,h as X,n as sr,p as Eo,s as qo}from"./chunks/chunk-2CUKVVRO.js";import{b as ot,c as xt,d as B,e as Mo,f as le,g as oe,h as Te,i as be,j as Bt,k as xo}from"./chunks/chunk-LG74QY5U.js";function Lt(e,t,n={}){if(typeof e=="string")return Ta(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=te(n.alternative),a=n.mu??0,l=n.paired??!1,i=n.varEqual??!1,s=n.confLevel??.95;if(Number.isNaN(a))throw new RangeError("mu must be a number");if(qn(s),l&&r===void 0)throw new TypeError("y is required when paired is true");let c=Array.from(e),f=r===void 0?void 0:Array.from(r);if(l){let[x,q]=Rt(c,f);c=x.map((A,S)=>A-q[S]),f=void 0}else c=qe(c),f!==void 0&&(f=qe(f));let m,u,h,p,d,g,v,w=c.length,b=f!==void 0&&i;if(w<1||!b&&w<2)throw new Error("not enough 'x' observations");let N=se(c),M=w>1?re(c):NaN;if(f===void 0){if(u=w-1,h=Math.sqrt(M/w),h<10*Number.EPSILON*Math.abs(N))throw new Error("data are essentially constant");m=(N-a)/h,g=l?"Paired t-test":"One Sample t-test",p=N,d=l?"mean difference":"mean of x",v=l?"mean difference":"mean"}else{let x=f.length;if(x<1||!i&&x<2)throw new Error("not enough 'y' observations");if(i&&w+x<3)throw new Error("not enough observations");let q=se(f),A=x>1?re(f):NaN;if(g=`${i?"":"Welch"} Two Sample t-test`,p=[N,q],d=["mean of x","mean of y"],v="difference in means",i){u=w+x-2;let S=0;w>1&&(S+=(w-1)*M),x>1&&(S+=(x-1)*A),S/=u,h=Math.sqrt(S*(1/w+1/x))}else{let S=M/w,L=A/x;h=Math.sqrt(S+L),u=(h*h)**2/(S*S/(w-1)+L*L/(x-1))}if(h<10*Number.EPSILON*Math.max(Math.abs(N),Math.abs(q)))throw new Error("data are essentially constant");m=(N-q-a)/h}let E,y;if(o==="less")E=Ne(m,u,!0,!1),y=[-1/0,m+ze(s,u,!0,!1)];else if(o==="greater")E=Ne(m,u,!1,!1),y=[m-ze(s,u,!0,!1),1/0];else{E=2*Ne(-Math.abs(m),u,!0,!1);let x=1-s,q=ze(1-x/2,u,!0,!1);y=[m-q,m+q]}return y=[a+y[0]*h,a+y[1]*h],{method:g,alternative:o,statistic:m,statisticName:"t",parameter:u,parameterName:"df",pValue:E,confInt:y,confLevel:s,estimate:p,estimateNames:d,nullValue:a,nullValueName:v,stdErr:h}}function Ta(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=En(e,t,!0);if(r.kind==="one")return Lt(r.x,n);if(r.kind==="paired")return Lt(r.x,r.y,{...n,paired:!0});let[o,a]=r.levels;return{...Lt(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${a}`],nullValueName:`difference in means between group ${o} and group ${a}`}}function Ve(e){let t=[0,0],n=0;for(let r of e)t=B(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Sa(e,t,n){return Array.isArray(t)?La(e,t,n??{}):Ia(e,t??{})}var Uo=(e,t)=>e<t?-1:e>t?1:0;function La(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=[],a=[];e.forEach((u,h)=>{let p=t[h];!r(u)&&!r(p)&&(o.push(u),a.push(p))});let l=[...new Set(o)].sort(Uo),i=[...new Set(a)].sort(Uo);if(l.length<2||i.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let s=new Map(l.map((u,h)=>[u,h])),c=new Map(i.map((u,h)=>[u,h])),f=l.map(()=>i.map(()=>0));return o.forEach((u,h)=>f[s.get(u)][c.get(a[h])]++),{...Ko(f,n),rowLevels:l,colLevels:i}}function Ia(e,t){if(e.length>0&&typeof e[0]!="number"){let r=e.map(a=>Array.from(a)),o=r[0].length;if(r.some(a=>a.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?ur(r[0],t):o===1?ur(r.map(a=>a[0]),t):Ko(r,t)}return ur(Array.from(e),t)}function Ho(e){let t=e.B??2e3;if(!(t>=0&&t<2**31))throw new RangeError("'B' must be a non-negative number");return t}function Bo(e,t,n){let r=(1-64*Number.EPSILON)*t,o=0;for(let a=0;a<e.length;a++)e[a]>=r&&o++;return(1+o)/(n+1)}function Xo(e,t){return Number.isNaN(e)?NaN:he(e,t/2,2,!1,!1)}function Qo(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 ur(e,t){let n=e.length;if(e.some(m=>Number.isNaN(m)||m<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=Ve(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(m=>m<0))throw new RangeError("probabilities must be non-negative");if(t.rescaleP??!1){let m=Ve(o);o=o.map(u=>u/m)}else if(Math.abs(Ve(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let a=o.map(m=>r*m);Qo(a);let l=o.map((m,u)=>a[u]*(1-m)),i=Ve(e.map((m,u)=>(m-a[u])**2/a[u])),s="Chi-squared test for given probabilities",c=n-1,f;if(t.simulatePValue??!1){let m=Ho(t);s+=Kt(m),c=NaN,f=Bo(Fa(r,o,a,m),i,m)}else f=Xo(i,c);return{method:s,statistic:i,statisticName:"X-squared",parameter:c,parameterName:"df",pValue:f,observed:e,expected:a,residuals:e.map((m,u)=>(m-a[u])/Math.sqrt(a[u])),stdres:e.map((m,u)=>(m-a[u])/Math.sqrt(l[u]))}}function Fa(e,t,n,r){let o=t.length,a=Io(o,{size:Math.trunc(r*e),replace:!0,prob:t}),l=Math.trunc(e);if(a.length===0)return new Float64Array(0);if(l===0)throw new RangeError("invalid 'nrow' value (too large or NA)");let i=Math.ceil(a.length/l),s=new Float64Array(i),c=new Float64Array(o);for(let f=0;f<i;f++){c.fill(0);for(let u=0;u<l;u++)c[a[(f*l+u)%a.length]]++;let m=0;for(let u=0;u<o;u++)m+=(c[u]-n[u])**2/n[u];s[f]=m}return s}function Oa(e,t,n,r){let o=e.length,a=t.length,l=e.reduce((f,m)=>f+m,0),i=yn(l),s=new Int32Array(o*a),c=new Float64Array(Math.trunc(r));for(let f=0;f<c.length;f++){Mn(e,t,i,s);let m=0;for(let u=0;u<a;u++)for(let h=0;h<o;h++){let p=n[h][u];m+=(s[h+u*o]-p)**2/p}c[f]=m}return c}function Ko(e,t){let n=t.correct??!0,r=e.length,o=e[0].length,a=e.flat();if(a.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let l=Ve(a);if(!(l>0))throw new RangeError("at least one entry of 'x' must be positive");let i=e.map(w=>Ve(w)),s=e[0].map((w,b)=>Ve(e.map(N=>N[b]))),c=i.map(w=>s.map(b=>w*b/l));Qo(c.flat());let f=e.map((w,b)=>w.map((N,M)=>N-c[b][M])),m="Pearson's Chi-squared test",u,h=(r-1)*(o-1),p,d=(t.simulatePValue??!1)&&i.every(w=>w>0)&&s.every(w=>w>0),g=!d&&n&&r===2&&o===2?Math.min(.5,...f.flat().map(Math.abs)):0;if(d){let w=Ho(t);m+=Kt(w),u=Ve(f.flatMap((N,M)=>N.map((E,y)=>E*E/c[M][y]))),h=NaN;let b=Oa(i.map(N=>Math.trunc(N)),s.map(N=>Math.trunc(N)),c,w);p=Bo(b,u,w)}else g>0?(m+=" with Yates' continuity correction",u=Ve(f.flatMap((w,b)=>w.map((N,M)=>(Math.abs(N)-g)**2/c[b][M])))):u=Ve(f.flatMap((w,b)=>w.map((N,M)=>N*N/c[b][M])));let v=c.map((w,b)=>w.map((N,M)=>N*(1-i[b]/l)*(1-s[M]/l)));return{method:m,statistic:u,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p??Xo(u,h),observed:e,expected:c,residuals:f.map((w,b)=>w.map((N,M)=>N/Math.sqrt(c[b][M]))),stdres:f.map((w,b)=>w.map((N,M)=>N/Math.sqrt(v[b][M])))}}var _a=Math.pow(Number.EPSILON,.25);function xe(e,t,n,r={}){let o={tol:r.tol??_a,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),mt(e,t,n,o).root}function Va(e,t={}){let n=e.length,r=n===0?0:e[0].length;if(e.some(s=>s.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(s=>Array.from(s));if(o.flat().some(s=>Number.isNaN(s)||s<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(s=>s===1/0))throw new RangeError("'x' has entries too large to be integer");let a=o.map(s=>s.map(Ao)),l="Fisher's Exact Test for Count Data";if(n===2&&r===2)return $a(a,t,l);let i=t.alternative??"two.sided";if(t.simulatePValue??!1){let s=t.B??2e3;if(!(s>=0&&s<2**31))throw new RangeError("'B' must be a non-negative number");return{method:l+Kt(s),alternative:i,pValue:Ca(a,s)}}return{method:l,alternative:i,pValue:ja(a)}}function Ca(e,t){let n=e.filter(d=>d.some(g=>g>0)),r=e[0].map((d,g)=>g).filter(d=>e.some(g=>g[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(g=>d[g])),a=o.map(d=>d.reduce((g,v)=>g+v,0)),l=o[0].map((d,g)=>o.reduce((v,w)=>v+w[g],0)),i=a.length,s=l.length,c=[0,0];for(let d of o.flat())c=B(c,[-Po(d),0]);let f=c[0]/(1+64*Number.EPSILON),m=yn(a.reduce((d,g)=>d+g,0)),u=new Int32Array(i*s),h=0,p=Math.trunc(t);for(let d=0;d<p;d++){Mn(a,l,m,u);let g=0;for(let v=0;v<u.length;v++)g-=m[u[v]];g<=f&&h++}return Math.max(0,Math.min(1,(1+h)/(t+1)))}var Da=1+1e-7,Pa=16;function hr(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=B(r,[e[o],0]));return r[0]}var dr=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,a=1;for(let l=this.lo;l<=this.hi;l++){let i=l-this.lo;i%Pa===0||!(a>0&&a<1/0)?(o=Et(l,t,n,r,!0),a=1,this.logCentral[i]=o):this.logCentral[i]=o+Math.log(a),a*=(t-l)*(r-l)/((l+1)*(n-r+l+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),a=-1/0;for(let i=0;i<n;i++){let s=this.logCentral[i]+(this.lo+i)*r;o[i]=s,s>a&&(a=s)}for(let i=0;i<n;i++){let s=o[i]-a;o[i]=s<-746?0:Math.exp(s)}let l=hr(o,0,n-1);for(let i=0;i<n;i++)o[i]!==0&&(o[i]=o[i]/l);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=B(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?cr(t-1,this.m,this.n,this.k,!1,!1):cr(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let l=n===0?this.lo:this.hi;return(r?t<=l:t>=l)?1:0}let o=this.density(n),a=t-this.lo;return r?hr(o,a,o.length-1):hr(o,0,a)}};function pr(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function $a(e,t,n){let r=te(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let a=t.confLevel??.95;if(!(a>0&&a<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[l,i],[s,c]]=e,f=l,m=new dr(l+s,i+c,l+i),{lo:u,hi:h}=m,p;if(r==="less")p=m.tail(f,o,!1);else if(r==="greater")p=m.tail(f,o,!0);else if(o===0)p=f===u?1:0;else if(o===1/0)p=f===h?1:0;else{let N=m.density(o),M=N[f-u]*Da,E=[0,0];for(let y of N)y<=M&&(E=B(E,[y,0]));p=E[0]}let d;if(f===u)d=0;else if(f===h)d=1/0;else{let N=m.mean(1);d=N===f?1:pr(M=>m.mean(M)-f,N>f)}let g={method:n,alternative:r,pValue:p,estimate:d,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio"};if(!(t.confInt??!0))return g;let v=N=>{if(f===h)return 1/0;let M=m.tail(f,1,!1);return M===N?1:pr(E=>m.tail(f,E,!1)-N,M<N)},w=N=>{if(f===u)return 0;let M=m.tail(f,1,!0);return M===N?1:pr(E=>m.tail(f,E,!0)-N,M>N)},b;if(r==="less")b=[0,v(1-a)];else if(r==="greater")b=[w(1-a),1/0];else{let N=(1-a)/2;b=[w(N),v(N)]}return{...g,confInt:b,confLevel:a}}var Ln=[[0,0]];function Ce(e){for(let t=Ln.length;t<=e;t++)Ln.push(B(Ln[t-1],xo([t,0])));return Ln[e]}function In(e,t,n,r){let o=e+n,a=o-e,l=e-(o-a)+(n-a)+t+r,i=o+l;return[i,l-(i-o)]}var gr=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let s=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=s,this.lo*=s,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),a=this.hi+o,l=a-this.hi,i=this.hi-(a-l)+(o-l)+this.lo;this.hi=a+i,this.lo=i-(this.hi-a)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function ja(e){let t=e.filter(b=>b.some(N=>N>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((b,N)=>t.some(M=>M[N]>0));if(t=t.map(b=>b.filter((N,M)=>n[M])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((b,N)=>t.map(M=>M[N])));let r=t.length,o=t.map(b=>b.reduce((N,M)=>N+M,0)),a=t[0].map((b,N)=>t.reduce((M,E)=>M+E[N],0)).sort((b,N)=>N-b),l=a.length,i=o.reduce((b,N)=>b+N,0),s=Mo(Ce(i));for(let b of o)s=B(s,Ce(b));for(let b of a)s=B(s,Ce(b));let c=[0,0];for(let b of t.flat())c=le(c,Ce(b));let f=B(c,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:l},()=>new Map),d=(b,N)=>{let M=N.join(","),E=p[b].get(M);if(E!==void 0)return E;let y=B(s,Ce(N.reduce((R,k)=>R+k,0)));for(let R of N)y=le(y,Ce(R));for(let R=b;R<l;R++)y=le(y,Ce(a[R]));let x={R:N,KZ:y,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(M,x),b===l-1){let R=[0,0];for(let k of N)R=le(R,Ce(k));return x.max=x.min=R[0],x.edges.push({to:x,hi:R[0],lo:R[1],mult:1}),x}let q=new Map,A=new Array(r+1).fill(0);for(let R=r-1;R>=0;R--)A[R]=A[R+1]+N[R];let S=new Array(r).fill(0),L=(R,k,C)=>{if(R===r-1){S[R]=k;let V=le(C,Ce(k)),z=N.map((P,D)=>P-S[D]).sort((P,D)=>P-D),Y=d(b+1,z),T=`${z.join(",")}|${V[0]}`,F=q.get(T);F===void 0?q.set(T,{to:Y,hi:V[0],lo:V[1],mult:1}):F.mult++;return}let O=Math.max(0,k-A[R+1]),I=Math.min(N[R],k);for(let V=O;V<=I;V++)S[R]=V,L(R+1,k-V,le(C,Ce(V)))};if(L(0,a[b],[0,0]),x.edges=[...q.values()],x.max=Math.max(...x.edges.map(R=>R.hi+R.to.max)),x.min=Math.min(...x.edges.map(R=>R.hi+R.to.min)),b===l-2){let R=x.edges.map(Y=>{let T=Y.to.edges[0];return{v:B([Y.hi,Y.lo],[T.hi,T.lo]),mult:Y.mult}});R.sort((Y,T)=>Y.v[0]-T.v[0]||Y.v[1]-T.v[1]);let k=R.length,C=R[k-1].v[0],O=new Float64Array(k),I=new Float64Array(k),V=new Float64Array(k),z=[0,0];R.forEach(({v:Y,mult:T},F)=>{O[F]=Y[0],I[F]=Y[1];let P=le(Y,[C,0]);z=B(z,[T*Math.exp(P[0])*(1+P[1]),0]),V[F]=z[0]}),x.dist={hi:O,lo:I,cum:V,vmax:C,kv:B(s,[C,0])}}return x},g=d(0,[...o].sort((b,N)=>b-N)),v=new gr,w=(b,N,M,E)=>{if(N+b.max<=u-m){let[L,R]=In(b.KZ[0],b.KZ[1],N,M);return v.add(L,R,E),!0}if(N+b.min>u+m)return!0;let y=b.dist;if(y===void 0)return!1;let[x,q]=In(u,h,-N,-M),A=0,S=y.hi.length;for(;A<S;){let L=A+S>>>1,R=y.hi[L];R<x||R===x&&y.lo[L]<=q?A=L+1:S=L}if(A>0){let[L,R]=In(y.kv[0],y.kv[1],N,M);v.add(L,R,E*y.cum[A-1])}return!0};w(g,0,0,1)||(g.pastHi.push(0),g.pastLo.push(0),g.count.push(1));for(let b=0;b<l-2;b++)for(let N of p[b].values()){let{pastHi:M,pastLo:E,count:y}=N;if(y.length!==0){for(let{to:x,hi:q,lo:A,mult:S}of N.edges)for(let L=0;L<y.length;L++){let[R,k]=In(M[L],E[L],q,A),C=y[L]*S;if(w(x,R,k,C))continue;let O=x.index.get(R);O===void 0?(x.index.set(R,x.count.length),x.pastHi.push(R),x.pastLo.push(k),x.count.push(C)):x.count[O]+=C}N.pastHi=N.pastLo=N.count=[],N.index.clear()}}return Math.min(1,v.value())}function tn(e,t,n={}){if(typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let d=En(e,t,!0);return d.kind==="one"?tn(d.x,n):tn(d.x,d.y,d.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=te(n.alternative),a=n.mu??0,l=n.paired??!1,i=n.exact??null,s=n.correct??!0,c=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(a))throw new RangeError("mu must be a single finite number");if(c&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(l&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(l){let[d,g]=Rt(Array.from(e),Array.from(r));u=qe(d.map((v,w)=>v-g[w]))}else u=qe(Array.from(e)),r!==void 0&&(h=qe(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let p={alternative:o,mu:a,correct:s,confInt:c,confLevel:f,tolRoot:m};return h===void 0?Wa(u,i,l,p):Ha(u,h,i,p)}function Fn(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function Yo(e,t){return t==="less"?$(e):t==="greater"?$(e,{lowerTail:!1}):2*Math.min($(e),$(e,{lowerTail:!1}))}function za(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 wr(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:se([e[n-1],e[n]])}var vr=(e,t)=>e-t;function Jo(e){let t=e.length,n=me(e.map(Math.abs)),r=0;for(let l=0;l<t;l++)e[l]>0&&(r+=n[l]);let o=0;for(let l of Fe(n))o+=l*l*l-l;let a=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,a]}function Wa(e,t,n,r){let{alternative:o,mu:a,correct:l}=r,i=e.map(E=>E-a),s=i.some(E=>E===0);s&&(i=i.filter(E=>E!==0));let c=i.length,f=t??c<50,m=me(i.map(Math.abs)),u=Fe(m).length>0,[h,p,d]=Jo(i),g,v,w=f&&!u&&!s;if(w){v="Wilcoxon signed rank exact test";let E=c*(c+1)/2;if(o==="less")g=qt(h,c,!0,!1);else if(o==="greater")g=qt(h-1,c,!1,!1);else{let y=h>E/2?qt(h-1,c,!1,!1):qt(h,c,!0,!1);g=Math.min(1,2*y)}}else v=`Wilcoxon signed rank test${l?" with continuity correction":""}`,g=Yo((p-Fn(p,o,l))/d,o);let b={method:v,alternative:o,statistic:h,statisticName:"V",pValue:g,nullValue:a,nullValueName:n?"location shift":"location"};if(!r.confInt)return b;let N=i.map(E=>E+a),M=w?Ga(N,r):Ua(N,r);return{...b,...M}}function On(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Zo(e,t,n){let r={confInt:On(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function es(e,t,n){let r=e(t==="two.sided"?n/2:n);return r===0?1:r}function ts(e){if(!(e>0))throw new RangeError("tolRoot must be positive")}function Ga(e,t){let{alternative:n,confLevel:r}=t,o=e.length,a=[];for(let m=0;m<o;m++)for(let u=m;u<o;u++)a.push((e[m]+e[u])/2);a.sort(vr);let l=a.length,i=1-r,s=es(m=>ko(m,o,!0,!1),n,i),c=qt(s-1,o,!0,!1),f=n==="two.sided"?2*c:c;return{confInt:On(n,a[s-1],a[l-s]),confLevel:f-i>i/2?1-za(f):r,estimate:wr(a),estimateNames:"(pseudo)median"}}function Ua(e,t){let{alternative:n,correct:r,confLevel:o,tolRoot:a}=t,l=e.length;if(l===0)return Zo(n,NaN);let i=Math.min(...e),s=Math.max(...e);if(!Number.isFinite(i)||!Number.isFinite(s)||l===1||i===s)return Zo(n,(i+s)/2,"midrange");let c=(b,N)=>{let[,M,E]=Jo(e.map(y=>y-b).filter(y=>y!==0));return(M-Fn(M,n,N))/E},f=c(i,r),m=c(s,r),u=b=>-X(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>X(b/2)):!(f<u(b/2))&&!(m>X(b/2)),p=1-o,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;ts(a);let g=b=>xe(N=>c(N,r)-b,i,s,{tol:a,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=wr([...e].sort(vr));v=On(n,b,b)}else n==="greater"?v=[g(u(p)),1/0]:n==="less"?v=[-1/0,g(X(p))]:v=[g(u(p/2)),g(X(p/2))];let w=xe(b=>c(b,!1),i,s,{tol:a,fLower:c(i,!1),fUpper:c(s,!1)});return{confInt:v,confLevel:d?1-Math.min(1,p):o,estimate:w,estimateNames:"(pseudo)median"}}function ns(e,t){let n=e.length,r=t.length,o=me([...e,...t]),a=0;for(let f=0;f<n;f++)a+=o[f];let l=a-n*(n+1)/2,i=0;for(let f of Fe(o))i+=f*f*f-f;let s=n+r,c=Math.sqrt(n*r/12*(s+1-i/(s*(s-1))));return[l,l-n*r/2,c]}function Ha(e,t,n,r){let{alternative:o,mu:a,correct:l}=r,i=e.length,s=t.length,c=e.map(N=>N-a),f=n??(i<50&&s<50),m=Fe([...c,...t]).length>0,[u,h,p]=ns(c,t),d,g,v=f&&!m;if(v)if(g="Wilcoxon rank sum exact test",o==="less")d=At(u,i,s,!0,!1);else if(o==="greater")d=At(u-1,i,s,!1,!1);else{let N=u>i*s/2?At(u-1,i,s,!1,!1):At(u,i,s,!0,!1);d=Math.min(1,2*N)}else g=`Wilcoxon rank sum test${l?" with continuity correction":""}`,d=Yo((h-Fn(h,o,l))/p,o);let w={method:g,alternative:o,statistic:u,statisticName:"W",pValue:d,nullValue:a,nullValueName:"location shift"};if(!r.confInt)return w;let b=v?Ba(e,t,r):Xa(e,t,r);return{...w,...b}}function Ba(e,t,n){let{alternative:r,confLevel:o}=n,a=e.length,l=t.length,i=[];for(let h of e)for(let p of t)i.push(h-p);i.sort(vr);let s=i.length,c=1-o,f=es(h=>So(h,a,l,!0,!1),r,c),m=At(f-1,a,l,!0,!1),u=r==="two.sided"?2*m:m;return{confInt:On(r,i[f-1],i[s-f]),confLevel:u-c>c/2?1-u:o,estimate:wr(i),estimateNames:"difference in location"}}function Xa(e,t,n){let{alternative:r,correct:o,confLevel:a,tolRoot:l}=n,i=Math.min(...e)-Math.max(...t),s=Math.max(...e)-Math.min(...t);if(!Number.isFinite(i)||!Number.isFinite(s))throw new Error("cannot compute the confidence interval because the differences are not finite");let c=(h,p)=>{let[,d,g]=ns(e.map(v=>v-h),t);return(d-Fn(d,r,p))/g},f=(h,p)=>{let d=c(i,p)-h,g=c(s,p)-h;if(Number.isNaN(d)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return d<=0?i:g>=0?s:(ts(l),xe(v=>c(v,p)-h,i,s,{tol:l,fLower:d,fUpper:g}))},m=1-a,u;return r==="greater"?u=[f(-X(m),o),1/0]:r==="less"?u=[-1/0,f(X(m),o)]:u=[f(-X(m/2),o),f(X(m/2),o)],{confInt:u,confLevel:a,estimate:f(0,!1),estimateNames:"difference in location"}}var rs=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Qa(e){if(rs.includes(e))return e;let t=rs.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function It(e,t={}){let n=typeof t=="string"?{method:t}:t,r=Qa(n.method??"holm"),o=Array.from(e),a=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||a.push(m);let l=a.length,i=n.n??l;if(!(i>=l))throw new RangeError("n must be at least the number of non-NaN p-values");if(i<=1)return o;let s=a.map(m=>o[m]),c=Ka(s,r,i),f=o.slice();return a.forEach((m,u)=>f[m]=c[u]),f}function _n(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 Ka(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(a=>Math.min(1,n*a));case"holm":{let a=_n(e,!1),l=-1/0;return a.forEach((i,s)=>{l=Math.max(l,(n-s)*e[i]),o[i]=Math.min(1,l)}),o}case"hochberg":{let a=_n(e,!0),l=1/0;return a.forEach((i,s)=>{let c=r-s;l=Math.min(l,(n-c+1)*e[i]),o[i]=Math.min(1,l)}),o}case"BH":case"fdr":case"BY":{let a=t==="BY"?ee(Array.from({length:Math.floor(n)},(s,c)=>1/(c+1))):1,l=_n(e,!0),i=1/0;return l.forEach((s,c)=>{let f=r-c;i=Math.min(i,a*n/f*e[s]),o[s]=Math.min(1,i)}),o}case"hommel":return Za(e,n)}}function Za(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=_n(r,!1),a=o.map(s=>r[s]),l=a.slice();for(let s=t;s>=2;s--){let c=1/0;for(let f=1;f<=s;f++)c=Math.min(c,s*a[t-s+f-1]/f);for(let f=t-s;f<t;f++)l[f]=Math.max(l[f],c);for(let f=0;f<t-s;f++)l[f]=Math.max(l[f],Math.min(s*a[f],c))}let i=new Array(n);return o.forEach((s,c)=>{s<n&&(i[s]=l[c])}),i}var Ya=1+1e-7;function Vn(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function Ja(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=vn(e,t,n,!1)*Ya,a=0;if(e<r){for(let s=Math.ceil(r);s<=t;s++)vn(s,t,n,!1)<=o&&a++;let i=a===0?0:lt(t-a,t,n,!1,!1);return Math.min(1,lt(e,t,n,!0,!1)+i)}for(let i=0;i<=Math.floor(r);i++)vn(i,t,n,!1)<=o&&a++;let l=a===0?0:lt(a-1,t,n,!0,!1);return Math.min(1,lt(e-1,t,n,!1,!1)+l)}function br(e,t,n={}){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),!Vn(e[0])||!Vn(e[1]))throw new RangeError("'x' must be nonnegative and integer");r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1])}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Vn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Vn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}let a=n.p??.5;if(!(a>=0&&a<=1))throw new RangeError("'p' must be a single number between 0 and 1");let l=te(n.alternative),i=n.confLevel??.95;Me(i);let s;l==="less"?s=lt(r,o,a,!0,!1):l==="greater"?s=lt(r-1,o,a,!1,!1):s=Ja(r,o,a);let c=u=>r===0?0:lr(u,r,o-r+1,!0,!1),f=u=>r===o?1:lr(1-u,r+1,o-r,!0,!1),m;if(l==="less")m=[0,f(1-i)];else if(l==="greater")m=[c(1-i),1];else{let u=(1-i)/2;m=[c(u),f(u)]}return{method:"Exact binomial test",alternative:l,statistic:r,statisticName:"number of successes",parameter:o,parameterName:"number of trials",pValue:s,confInt:m,confLevel:i,estimate:r/o,estimateNames:"probability of success",nullValue:a,nullValueName:"probability of success"}}var ei=1+1e-7;function ti(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function ni(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=ir(e,t,!1)*ei,r=i=>ir(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=Math.max(e+1,o),s=i;if(!r(i)){let c=i,f=1;for(s=i+1;!r(s);)c=s,f*=2,s=i+f;for(;s-c>1;){let m=c+Math.floor((s-c)/2);r(m)?s=m:c=m}}return Math.min(1,st(e,t,!0,!1)+st(s-1,t,!1,!1))}let a=Math.min(e-1,o),l;if(!r(0))l=-1;else if(r(a))l=a;else{l=0;let i=a;for(;i-l>1;){let s=l+Math.floor((i-l)/2);r(s)?l=s:i=s}}return Math.min(1,st(l,t,!0,!1)+st(e-1,t,!1,!1))}function ri(e,t={}){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(ti))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,a=t.T??1,l=typeof a=="number"?new Array(o).fill(a):Array.from(a);if(l.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!l.every(v=>Number.isFinite(v)&&v>=0))throw new RangeError("'T' must be nonnegative");let i=t.r??1;if(!(Number.isFinite(i)&&i>=0))throw new RangeError("'r' must be a single positive number");let s=te(t.alternative),c=t.confLevel??.95;if(Me(c),o===2){let[v,w]=r,[b,N]=l,M=v+w,E=i*b/(i*b+N),y=br([v,w],{p:E,alternative:s,confLevel:c}),x=q=>q/(1-q)*(N/b);return{method:"Comparison of Poisson rates",alternative:s,statistic:v,statisticName:"count1",parameter:M*i*b/(b+i*N),parameterName:"expected count1",pValue:y.pValue,confInt:[x(y.confInt[0]),x(y.confInt[1])],confLevel:c,estimate:v/b/(w/N),estimateNames:"rate ratio",nullValue:i,nullValueName:"rate ratio"}}let f=r[0],m=l[0],u=i*m,h;s==="less"?h=st(f,u,!0,!1):s==="greater"?h=st(f-1,u,!1,!1):h=ni(f,u);let p=v=>f===0?0:ar(v,f,1,!0,!1),d=v=>ar(1-v,f+1,1,!0,!1),g;if(s==="less")g=[0,d(1-c)];else if(s==="greater")g=[p(1-c),1/0];else{let v=(1-c)/2;g=[p(v),d(v)]}return{method:"Exact Poisson test",alternative:s,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[g[0]/m,g[1]/m],confLevel:c,estimate:f/m,estimateNames:"event rate",nullValue:i,nullValueName:"event rate"}}function Nr(e,t,n={}){let r,o,a=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let A=e;if(A.some(S=>S.length!==2))throw new RangeError("'x' must have 2 columns");r=A.map(S=>S[0]),o=A.map(S=>S[0]+S[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(a=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 l=r.length,i=[];for(let A=0;A<l;A++)!Number.isNaN(r[A])&&!Number.isNaN(o[A])&&i.push(A);let s=i.length;if(s<1)throw new RangeError("not enough data");if(r=i.map(A=>r[A]),o=i.map(A=>o[A]),o.some(A=>!(A>0)))throw new RangeError("elements of 'n' must be positive");if(r.some(A=>!(A>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((A,S)=>A>o[S]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let c;if(a.p!==void 0){let A=typeof a.p=="number"?[a.p]:Array.from(a.p);if(A.length!==l)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(c=i.map(S=>A[S]),c.some(S=>!(S>0&&S<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=c!==void 0;s===1&&c===void 0&&(c=[.5]);let m=a.confLevel??.95;Me(m);let u=te(a.alternative);(s>2||s===2&&f)&&(u="two.sided");let h=r.map((A,S)=>A/o[S]),p=s===2?h[0]-h[1]:0,d=s===2?1/o[0]+1/o[1]:0,g=(a.correct??!0)&&s<=2?.5:0,v=!1;if(g>0&&s===1){let A=Math.abs(r[0]-o[0]*c[0]);A<=g&&([g,v]=[A,!0])}else if(g>0&&s===2&&!f){let A=Math.abs(p)/d;A<=g&&([g,v]=[A,!0])}let w=r.reduce((A,S)=>A+S,0)/o.reduce((A,S)=>A+S,0),b=0;if(!v)for(let A=0;A<s;A++){let S=c?.[A]??w,L=o[A]*S,R=o[A]*(1-S);b+=(Math.abs(r[A]-L)-g)**2/L,b+=(Math.abs(o[A]-r[A]-R)-g)**2/R}let N=c!==void 0?s:s-1,M;if(u==="two.sided")M=he(b,N/2,2,!1,!1);else{let A=Math.sign(s===1?h[0]-c[0]:p);M=$(A*Math.sqrt(b),{lowerTail:u==="less"})}let E;if(s===1||s===2&&!f){let A=X(u==="two.sided"?(1+m)/2:m);if(s===1){let S=o[0],L=A*A,R=(V,z)=>(V+L/(2*S)+z*A*Math.sqrt(V*(1-V)/S+L/(4*S*S)))/(1+L/S),k=h[0]+g/S,C=h[0]-g/S,O=Math.min(1,Math.max(0,k>=1?1:R(k,1))),I=Math.min(1,Math.max(0,C<=0?0:R(C,-1)));E=u==="two.sided"?[I,O]:u==="greater"?[I,1]:[0,O]}else{let S=Math.sqrt(h.reduce((C,O,I)=>C+O*(1-O)/o[I],0)),L=A*S+g*d,R=Math.max(-1,p-L),k=Math.min(1,p+L);E=u==="two.sided"?[R,k]:u==="greater"?[R,1]:[-1,k]}}let y=s===1?"p":i.map(A=>`prop ${A+1}`),q={method:`${s===1?"1-sample proportions test":`${s}-sample test for ${f?"given proportions":"equality of proportions"}`} ${g!==0?"with":"without"} continuity correction`,alternative:u,statistic:b,statisticName:"X-squared",parameter:N,parameterName:"df",pValue:M,estimate:s===1?h[0]:h,estimateNames:y};return c!==void 0&&(q.nullValue=s===1?c[0]:c,q.nullValueName=y),E!==void 0&&(q.confInt=E,q.confLevel=m),q}function oi(e,t,n={}){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 c=r.length;if(c<2||r.some(f=>f.length!==c))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=si(e,t)}let o=n.correct??!0,a=r.length,l,i=o&&a===2&&r[0][1]!==r[1][0];if(i){let c=r[0][1],f=r[1][0];l=(Math.abs(c-f)-1)**2/(c+f)}else{l=0;for(let c=0;c<a;c++)for(let f=c+1;f<a;f++){let m=r[c][f],u=r[f][c];l+=(m-u)**2/(m+u)}}let s=a*(a-1)/2;return{method:i?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:l,statisticName:"McNemar's chi-squared",parameter:s,parameterName:"df",pValue:he(l,s/2,2,!1,!1)}}function si(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=ce(e),o=ce(t);for(let m=0;m<e.length;m++){let u=r[m],h=o[m];typeof u=="number"&&Number.isNaN(u)||typeof h=="number"&&Number.isNaN(h)||n.push([u,h])}let a=m=>Array.from(new Set(m)).sort((u,h)=>typeof u=="number"&&typeof h=="number"?u-h:String(u)<String(h)?-1:String(u)>String(h)?1:0),l=a(n.map(m=>m[0])),i=a(n.map(m=>m[1]));if(l.length<2||l.length!==i.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let s=new Map(l.map((m,u)=>[m,u])),c=new Map(i.map((m,u)=>[m,u])),f=l.map(()=>new Array(l.length).fill(0));for(let[m,u]of n)f[s.get(m)][c.get(u)]++;return f}function os(e,t,n={}){if(typeof e=="string"){let g=Ye(e,t);return os(g.x,g.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),a=n.ratio??1;if(!(Number.isFinite(a)&&a>0))throw new RangeError("'ratio' must be a single positive number");let l=te(n.alternative),i=n.confLevel??.95;Me(i);let s=r.length-1,c=o.length-1;if(s<1)throw new Error("not enough 'x' observations");if(c<1)throw new Error("not enough 'y' observations");let f=re(r)/re(o),m=f/a,u=ae(m,s,c,!0,!1),h=ae(m,s,c,!1,!1),p,d;if(l==="less")p=u,d=[0,f/at(1-i,s,c,!0,!1)];else if(l==="greater")p=h,d=[f/at(i,s,c,!0,!1),1/0];else{p=Math.min(1,2*Math.min(u,h));let g=(1-i)/2;d=[f/at(1-g,s,c,!0,!1),f/at(g,s,c,!0,!1)]}return{method:"F test to compare two variances",alternative:l,statistic:m,statisticName:"F",parameter:[s,c],parameterName:["num df","denom df"],pValue:p,confInt:d,confLevel:i,estimate:f,estimateNames:"ratio of variances",nullValue:a,nullValueName:"ratio of variances"}}function ss(e,t){if(typeof e=="string"){let u=Zt(e,t);return ss(Vo(u.values,u.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=[],a=[];for(let u of n){let h=Array.from(u).filter(Number.isFinite);if(h.length<2)throw new RangeError("there must be at least 2 observations in each group");o.push(h.length-1),a.push(re(h))}let l=0,i=0,s=0,c=0;for(let u=0;u<r;u++)l+=o[u],i+=o[u]*a[u],s+=o[u]*Math.log(a[u]),c+=1/o[u];i/=l;let f=(l*Math.log(i)-s)/(1+(c-1/l)/(3*(r-1))),m=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:f,statisticName:"Bartlett's K-squared",parameter:m,parameterName:"df",pValue:he(f,m/2,2,!1,!1)}}function pt(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 s=i.map(m=>Array.from(m).filter(u=>!Number.isNaN(u)));if(n==="error"&&s.some(m=>m.length===0))throw new RangeError("all groups must contain data");let c=[],f=[];return s.forEach((m,u)=>{m.length!==0&&(c.push(String(u+1)),f.push(m))}),{labels:c,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=ce(t),a=new Map;for(let i=0;i<r.length;i++){let s=r[i],c=o[i];if(Number.isNaN(s)||typeof c=="number"&&Number.isNaN(c))continue;let f=a.get(c);f===void 0&&a.set(c,f=[]),f.push(s)}let l=Array.from(a.keys()).sort((i,s)=>typeof i=="number"&&typeof s=="number"?i-s:String(i)<String(s)?-1:String(i)>String(s)?1:0);return{labels:l.map(ge),groups:l.map(i=>a.get(i))}}function dt(e,t,n="drop"){return pt(e,t,n).groups}function Cn(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:(t[n/2-1]+t[n/2])/2}function yr(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))}function Mr(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:he(n,r/2,2,!1,!1)}}function as(e){let t=0;for(let n of Fe(e))t+=n*n*n-n;return t}function Dn(e,t){if(typeof e=="string"){let c=Zt(e,t);return Dn(c.values,c.group.codes)}let n=dt(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),a=o.length,l=me(o),i=0,s=0;for(let c of n){let f=0;for(let m=0;m<c.length;m++)f+=l[s+m];s+=c.length,i+=f*f/c.length}return i=(12*i/(a*(a+1))-3*(a+1))/(1-as(o)/(a*a*a-a)),Mr("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",i,r-1)}function xr(e,t){if(typeof e=="string")return xr(mr(e,t));let n=yr(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 a=new Array(o).fill(0),l=0;for(let c of n){let f=me(c);for(let m=0;m<o;m++)a[m]+=f[m];l+=as(c)}let i=0;for(let c of a)i+=(c-r*(o+1)/2)**2;let s=12*i/(r*o*(o+1)-l/(o-1));return Mr("Friedman rank sum test","Friedman chi-squared",s,o-1)}function ai(e){return{...xr(e),method:"Cochran's Q test",statisticName:"Q"}}function is(e,t){if(typeof e=="string")return is(mr(e,t));let n=yr(e),r=e.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 a=me(n.map(p=>Math.max(...p)-Math.min(...p))),l=new Array(o).fill(0),i=0;n.forEach((p,d)=>{let g=me(p);for(let v=0;v<o;v++){let w=a[d]*(g[v]-(o+1)/2);i+=w*w,l[v]+=w}});let s=0;for(let p of l)s+=p*p;s/=r;let c=o-1,f=(r-1)*(o-1),m,u,h=[c,f];if(i===s){m=NaN,h=[NaN,NaN];let p=0;for(let d=2;d<=o;d++)p+=Math.log(d);u=Math.exp((1-r)*p)}else m=(r-1)*s/(i-s),u=ae(m,c,f,!1,!1);return{method:"Quade test",statistic:m,statisticName:"Quade F",parameter:h,parameterName:["num df","denom df"],pValue:u}}function ls(e,t){if(typeof e=="string"){let u=Zt(e,t);return ls(u.values,u.group.codes)}let n=dt(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flatMap(u=>{let h=Cn(u);return u.map(p=>Math.abs(p-h))}),a=o.length,i=me(o).map(u=>X((1+u/(a+1))/2)),s=0;for(let u of i)s+=u;let c=s/a,f=0,m=0;for(let u of n){let h=0;for(let p=0;p<u.length;p++)h+=i[m+p];m+=u.length,f+=u.length*(h/u.length-c)**2}return f/=re(i),Mr("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",f,r-1)}function cs(e,t,n={}){if(typeof e=="string"){let N=Ye(e,t);return cs(N.x,N.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),a=te(n.alternative),l=r.length,i=o.length,s=l+i;if(s<3)throw new RangeError("not enough observations");let c=r.concat(o),f=Array.from({length:s},(N,M)=>M).sort((N,M)=>c[N]-c[M]),m=new Array(s),u=0;for(;u<s;){let N=u;for(;N+1<s&&c[f[N+1]]===c[f[u]];)N++;let M=0;for(let E=u;E<=N;E++)M+=(E+1-(s+1)/2)**2;M/=N-u+1;for(let E=u;E<=N;E++)m[f[E]]=M;u=N+1}let h=0;for(let N=0;N<l;N++)h+=m[N];let p=0;for(let N of m)p+=N;p/=s;let d=0;for(let N of m)d+=(N-p)**2;let g=l*p,v=l*i/(s*(s-1))*d,w=(h-g)/Math.sqrt(v),b=a==="less"?$(w):a==="greater"?$(w,{lowerTail:!1}):2*$(-Math.abs(w));return{method:"Mood two-sample test of scale",alternative:a,statistic:w,statisticName:"Z",pValue:b}}function gt(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var ii=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],li=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],ci=[.544,-.39978,.025054,-6714e-7],fi=[1.3822,-.77857,.062767,-.0020322],mi=[-1.5861,-.31082,-.083751,.0038915],ui=[-.4803,-.082676,.0030302],hi=[-2.273,.459];function pi(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]=-X((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let a=Math.sqrt(o),l=1/Math.sqrt(e),i=r[0]/a+gt(ii,l),s=new Float64Array(n);s[0]=i;let c,f;if(e>5){let m=r[1]/a+gt(li,l);s[1]=m,c=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*i*i-2*m*m))}else c=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*i*i));for(let m=c;m<n;m++)s[m]=r[m]/f;for(let m=0;m<n;m++)t[e-1-m]=s[m],t[m]=-s[m];return t}function fs(e){let t=e.length,n=pi(t),r=e[t-1]-e[0],o=0;for(let c of e)o+=c/r;o/=t;let a=0,l=0,i=0;for(let c=0;c<t;c++){let f=e[c]/r-o;a+=n[c]*n[c],l+=f*f,i+=n[c]*f}let s=Math.sqrt(a*l);return(s-i)*(s+i)/(a*l)}var di=1.90985931710274,gi=1.0471975511966;function ms(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let l=di*(Math.asin(Math.sqrt(1-e))-gi);return Math.max(l,0)}let n=Math.log(e);if(t<=11){let l=gt(hi,t);if(n>=l)return 1e-99;let i=gt(ci,t),s=Math.exp(gt(fi,t));return $(-Math.log(l-n),{mean:i,sd:s,lowerTail:!1})}let r=Math.log(t),o=gt(mi,r),a=Math.exp(gt(ui,r));return $(n,{mean:o,sd:a,lowerTail:!1})}function us(e,t,n={}){if(typeof e=="string"){let h=Co(e,t);return us(h.values,h.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=dt(e,r),a=n.varEqual??!1,l=o.length;if(l<2)throw new RangeError("not enough groups");let i=o.map(h=>h.length);if(i.some(h=>h<2))throw new RangeError("not enough observations");let s=o.map(se),c=o.map(re),f=i.reduce((h,p)=>h+p,0),m,u;if(a){let h=se(o.flat()),p=0,d=0;for(let g=0;g<l;g++)p+=i[g]*(s[g]-h)**2,d+=(i[g]-1)*c[g];u=f-l,m=p/(l-1)/(d/u)}else{let h=i.map((w,b)=>w/c[b]),p=h.reduce((w,b)=>w+b,0),d=0,g=0;for(let w=0;w<l;w++)d+=(1-h[w]/p)**2/(i[w]-1),g+=h[w]*s[w];d/=l*l-1,g/=p;let v=0;for(let w=0;w<l;w++)v+=h[w]*(s[w]-g)**2;m=v/((l-1)*(1+2*(l-2)*d)),u=1/(3*d)}return{method:a?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:m,statisticName:"F",parameter:[l-1,u],parameterName:["num df","denom df"],pValue:ae(m,l-1,u,!1,!1)}}function wi(e){let t=Array.from(e).filter(o=>!Number.isNaN(o)).sort((o,a)=>o-a),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=fs(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:ms(r,n)}}function Er(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=B(r,[e[o],0]);return r[0]}function vi([[e,t],[n,r]]){let o=e+n,a=t+r,l=e+t,i=Math.max(0,l-a),s=Math.min(l,o),c=new Float64Array(s-i+1);for(let f=i;f<=s;f++)c[f-i]=Et(f,o,a,l,!1);return{lo:i,d:c}}function bi(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 a=0;a<t.length;a++)n[r+a]+=o*t[a]}return n}var Pn=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let a=vi(o);n+=a.lo,r=bi(r,a.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),a=-1/0;for(let i=0;i<n;i++){let s=this.logNull[i]+(this.lo+i)*o;r[i]=s,s>a&&(a=s)}for(let i=0;i<n;i++)r[i]=Math.exp(r[i]-a);let l=Er(r,0,n);for(let i=0;i<n;i++)r[i]=r[i]/l;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=B(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(t,n,r){let o=this.density(n),a=t-this.lo;return r?Er(o,a,o.length):Er(o,0,a+1)}};function qr(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function hs(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:qr(r=>e.mean(r)-t,n>t)}function Ar(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:qr(o=>n-e.cdf(t,o,!1),r<n)}function Rr(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:qr(o=>e.cdf(t,o,!0)-n,r>n)}function Ni(e,t={}){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 a=e.map(f=>f.reduce((m,u)=>m+u.reduce((h,p)=>h+p,0),0));if(a.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let l=te(t.alternative),i=t.correct??!0,s=t.exact??!1,c=t.confLevel??.95;if(Me(c),r===2&&o===2){let f=e.map(m=>[[m[0][0],m[0][1]],[m[1][0],m[1][1]]]);return s?Mi(f,l,c):yi(f,a,l,i,c)}return xi(e,a,r,o)}function yi(e,t,n,r,o){let a=0,l=0,i=0,s=0,c=0,f=0,m=0,u=0;e.forEach(([[N,M],[E,y]],x)=>{let q=t[x],A=N+M,S=E+y,L=N+E,R=M+y;a+=N,l+=A*L/q,i+=A*S*L*R/(q*q*(q-1));let k=(N+y)/q,C=(M+E)/q,O=N*y/q,I=M*E/q;s+=O,c+=I,f+=k*O,m+=k*I+C*O,u+=C*I});let h=a-l,p=r&&Math.abs(h)>=.5?.5:0,d=(Math.abs(h)-p)**2/i,g=n==="two.sided"?he(d,.5,2,!1,!1):$(Math.sign(h)*Math.sqrt(d),{lowerTail:n==="less"}),v=s/c,w=Math.sqrt(f/(2*s*s)+m/(2*s*c)+u/(2*c*c)),b;if(n==="less")b=[0,v*Math.exp(X(o)*w)];else if(n==="greater")b=[v*Math.exp(X(o,{lowerTail:!1})*w),1/0];else{let N=X((1+o)/2);b=[v*Math.exp(-N*w),v*Math.exp(N*w)]}return{method:`Mantel-Haenszel chi-squared test ${p!==0?"with":"without"} continuity correction`,alternative:n,statistic:d,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:g,confInt:b,confLevel:o,estimate:v,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Mi(e,t,n){let r=new Pn(e),o=e.reduce((i,s)=>i+s[0][0],0),a;if(t==="two.sided"){let i=r.density(1),s=i[o-r.lo]*(1+1e-7),c=[0,0];for(let f of i)f<=s&&(c=B(c,[f,0]));a=Math.min(1,c[0])}else a=r.cdf(o,1,t==="greater");let l;if(t==="less")l=[0,Ar(r,o,1-n)];else if(t==="greater")l=[Rr(r,o,1-n),1/0];else{let i=(1-n)/2;l=[Rr(r,o,i),Ar(r,o,i)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:o,statisticName:"S",pValue:a,confInt:l,confLevel:n,estimate:hs(r,o),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function xi(e,t,n,r){let o=(n-1)*(r-1),a=new Array(o).fill(0),l=Array.from({length:o},()=>new Array(o).fill(0));e.forEach((c,f)=>{let m=t[f],u=c.map(d=>d.reduce((g,v)=>g+v,0)),h=Array.from({length:r},(d,g)=>c.reduce((v,w)=>v+w[g],0)),p=1/(m*m*(m-1));for(let d=0;d<r-1;d++)for(let g=0;g<n-1;g++){let v=g+(n-1)*d;a[v]+=c[g][d]-u[g]*h[d]/m;for(let w=0;w<r-1;w++)for(let b=0;b<n-1;b++){let N=(g===b?m*u[g]:0)-u[g]*u[b],M=(d===w?m*h[d]:0)-h[d]*h[w];l[v][b+(n-1)*w]+=p*N*M}}});let i=we(l,a),s=0;for(let c=0;c<o;c++)s+=a[c]*i[c];return{method:"Cochran-Mantel-Haenszel test",statistic:s,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:o,parameterName:"df",pValue:he(s,o/2,2,!1,!1)}}var ps=new Map;function Ei(e,t){let n=`${e},${t}`,r=ps.get(n);if(r!==void 0)return r;let o=e+t,a=[];for(let u=1;u<=o;u++)a.push(Math.min(u,o+1-u));let l=a.reduce((u,h)=>u+h,0),i=Array.from({length:e+1},()=>new Float64Array(l+1));i[0][0]=1;for(let u=0;u<o;u++){let h=o-u,p=a[u],d=Array.from({length:e+1},()=>new Float64Array(l+1));for(let g=0;g<=e;g++){let v=i[g],w=e-g;if(w>h)continue;let b=w/h;for(let N=0;N<=l;N++){let M=v[N];M!==0&&(w>0&&(d[g+1][N+p]+=M*b),w<h&&(d[g][N]+=M*(1-b)))}}i=d}let s=i[e],c=0;for(;c<s.length&&s[c]===0;)c++;let f=s.length-1;for(;f>c&&s[f]===0;)f--;let m={min:c,probs:s.slice(c,f+1)};return ps.set(n,m),m}function kr(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:a}=Ei(t,n),l=Math.floor(e+1e-7)-o,i=0;if(r)for(let s=0;s<=Math.min(l,a.length-1);s++)i+=a[s];else for(let s=Math.max(l+1,0);s<a.length;s++)i+=a[s];return Math.min(i,1)}var ds=new Map;function qi(e,t){let n=`${e},${t}`,r=ds.get(n);if(r!==void 0)return r;let o=e+t,a=0;for(let m=1;m<=o;m++)a+=Math.min(m,o+1-m);let l=Array.from({length:e+1},()=>new Float64Array(a+1));l[0][0]=1;for(let m=0;m<o;m++){let u=Math.min(m+1,o-m),h=Array.from({length:e+1},()=>new Float64Array(a+1));for(let p=0;p<=e;p++){if(e-p>o-m)continue;let d=l[p];for(let g=0;g<=a;g++){let v=d[g];v!==0&&(p<e&&(h[p+1][g+u]+=v),e-p<o-m&&(h[p][g]+=v))}}l=h}let i=l[e],s=0;for(;i[s]===0;)s++;let c=i.length-1;for(;i[c]===0;)c--;let f={min:s,counts:i.slice(s,c+1)};return ds.set(n,f),f}function $n(e,t,n){let{min:r,counts:o}=qi(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let a=Do(t+n,t),l=0;for(let i=0;i<o.length;i++)if(l+=o[i]/a,l>=e)return r+i;return r+o.length-1}function gs(e,t,n={}){if(typeof e=="string"){let b=Ye(e,t);return gs(b.x,b.y,n)}let r=Array.from(e).filter(b=>!Number.isNaN(b)),o=Array.from(t).filter(b=>!Number.isNaN(b)),a=r.length,l=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(l<1)throw new RangeError("not enough 'y' observations");let i=te(n.alternative),s=n.confInt??!1,c=n.confLevel??.95;s&&Me(c);let f=a+l,m=me(r.concat(o)),u=m.map(b=>Math.min(b,f-b+1)),h=0;for(let b=0;b<a;b++)h+=u[b];let p=new Set(m).size<f,d=n.exact??(a<50&&l<50&&!p),g,v;if(d&&!p){let b=M=>kr(M-1,a,l,!1),N=M=>kr(M,a,l,!0);if(i==="two.sided"){let M=Math.floor((a+1)**2/4)+Math.floor(a*l/2)/2;g=Math.min(1,2*(h>M?b(h):N(h)))}else i==="less"?g=b(h):g=N(h);s&&(v=Ri(r,o,i,c))}else{let b=0;for(let x of u)b+=x*x;let N=f%2===0,M=N?a*(f+2)/4:a*(f+1)**2/(4*f),E=N?a*l*(16*b-f*(f+2)**2)/(16*f*(f-1)):a*l*(16*f*b-(f+1)**4)/(16*f*f*(f-1)),y=(h-M)/Math.sqrt(E);g=i==="two.sided"?2*Math.min($(y),$(y,{lowerTail:!1})):i==="less"?$(y,{lowerTail:!1}):$(y),s&&(v=ki(r,o,i,c))}let w={method:"Ansari-Bradley test",alternative:i,statistic:h,statisticName:"AB",pValue:g};return v!==void 0&&(w.confInt=v.confInt,w.confLevel=c,w.estimate=v.estimate,w.estimateNames="ratio of scales"),w.nullValue=1,w.nullValueName="ratio of scales",w}function ws(e,t,n){let r=e.length,o=r+t.length,a=Ai(e.map(s=>s/n).concat(t)),l=a.map(s=>Math.min(s,o-s+1)),i=0;for(let s=0;s<r;s++)i+=l[s];return{ab:i,a:l,ties:new Set(a).size<o}}function Ai(e){let t=e.filter(a=>!Number.isNaN(a));if(t.length===e.length)return me(e);let n=me(t),r=0,o=t.length;return e.map(a=>Number.isNaN(a)?++o:n[r++])}function Tr(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Ri(e,t,n,r){let o=e.length,a=t.length,l=[];for(let g of e)for(let v of t)g/v>=0&&l.push(g/v);l.sort((g,v)=>g-v);let i=l.length;if(i===0)return{confInt:Tr(n,1-r,()=>[NaN,NaN]),estimate:NaN};let s=l.map((g,v)=>ws(e,t,g+(v<i-1?(l[v+1]-g)/2:g*1.01)).ab),c=(g,v,w)=>{let b=1/0;for(let M of s)(v?M-g>0:M-g>=0)&&M-g<b&&(b=M-g);let N=w?-1/0:1/0;return s.forEach((M,E)=>{M-g===b&&(N=w?Math.max(N,l[E]):Math.min(N,l[E]))}),N},f=g=>{let v=$n(g/2,o,a),w=$n(1-g/2,o,a);if(!s.some(M=>M-v>=0)||!s.some(M=>M-w>0))return[NaN,NaN];let b=c(v,!1,!1),N=c(w,!0,!0);return b>N?[c(w,!1,!1),c(v,!0,!0)]:[b,N]},m=Tr(n,1-r,f),u=$n(.5,o,a),h=-1/0,p=1/0;s.forEach((g,v)=>{g-u<=0?h=Math.max(h,l[v]):p=Math.min(p,l[v])});let d=h===-1/0||p===1/0?NaN:(p+h)/2;return{confInt:m,estimate:d}}function ki(e,t,n,r){let o=e.length,a=t.length,l=o+a,i=l%2===0,s=(v,w)=>{let{ab:b,a:N,ties:M}=ws(e,t,v),E=i?b-o*(l+2)/4:b-o*(l+1)**2/(4*l),y;if(!M)y=Math.sqrt(i?o*a*(l+2)*(l-2)/(48*(l-1)):o*a*(l+1)*(3+l*l)/(48*l*l));else{let x=0;for(let q of N)x+=q*q;y=Math.sqrt(i?o*a*(16*x-l*(l+2)**2)/(16*l*(l-1)):o*a*(16*l*x-(l+1)**4)/(16*l*l*(l-1)))}return E/y-w},c=[],f=(v,w)=>{v.length===0||w.length===0||c.push(Math.min(...v)/Math.max(...w),Math.max(...v)/Math.min(...w))};if(f(e.filter(v=>v>0),t.filter(v=>v>0)),f(e.filter(v=>v<=0),t.filter(v=>v<0)),c.some(v=>!Number.isFinite(v)))return;let m=Math.min(...c),u=Math.max(...c),h=v=>xe(w=>s(w,v),m,u,{tol:1e-4}),p=v=>{let w=X(v/2),b=X(v/2,{lowerTail:!1});if(s(m,w)>0||s(u,b)<0)return[NaN,NaN];let N=h(w),M=h(b);return N<=M?[N,M]:[M,N]},d=Tr(n,1-r,p),g=s(m,0)>0||s(u,0)<0?NaN:h(0);return{confInt:d,estimate:g}}function Ti(e,t,n={}){let r=Array.from(e).filter(x=>!Number.isNaN(x)),o=Array.from(t).filter(x=>!Number.isNaN(x)),a=r.length,l=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(l<1)throw new RangeError("not enough 'y' observations");let i=te(n.alternative),s=n.confLevel??.95;qn(s);let c=me(r.concat(o)),f=c.slice(0,a),m=c.slice(a),u=x=>x.reduce((q,A)=>q+A,0)/x.length,h=u(f),p=u(m),d=(x,q,A)=>{let S=(x.length+1)/2,L=0;for(let R=0;R<x.length;R++)L+=(x[R]-q[R]-A+S)**2;return L/(x.length-1)},g=d(f,me(r),h),v=d(m,me(o),p),w=(p-(l+1)/2)/a,b=a*g+l*v,N=a*l*(p-h)/(a+l)/Math.sqrt(b),M=b**2/((a*g)**2/(a-1)+(l*v)**2/(l-1)),E;i==="greater"?E=Ne(N,M,!0,!1):i==="less"?E=Ne(N,M,!1,!1):E=2*Ne(-Math.abs(N),M,!0,!1);let y=ze((1-s)/2,M,!1,!1)*Math.sqrt(g/(a*l**2)+v/(l*a**2));return{method:"Brunner-Munzel Test",alternative:i,statistic:N,statisticName:"Brunner-Munzel Test Statistic",parameter:M,parameterName:"df",pValue:E,confInt:[w-y,w+y],confLevel:s,estimate:w,estimateNames:"P(X<Y)+.5*P(X=Y)"}}var Si=[1.2337005501361697,7831619385924639e-32];function Li(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let d=f[h*t+p];if(d!==0)for(let g=0;g<t;g++)u[h*t+g]+=d*m[p*t+g]}return u},o=f=>{let m=0;for(let p of f)m=Math.max(m,Math.abs(p));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let p=0;p<f.length;p++)f[p]*=h;return u},a,l=0,i=e.slice(),s=0,c=n;for(;c>0;)c&1&&(a=a===void 0?i.slice():r(a,i),l+=s,l+=o(a)),c>>=1,c>0&&(i=r(i,i),s*=2,s+=o(i));return[a,l]}function vs(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=1/(2*e))return 0;if(t>=1)return 1;if(e*t<1){let[f,m]=xt(e,t),u=2*f-1+2*m,h=1,p=0;for(let d=1;d<=e;d++)h*=d/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,a=new Float64Array(r*r);for(let f=0;f<r;f++)for(let m=0;m<r;m++)a[f*r+m]=f-m+1>=0?1:0;for(let f=0;f<r;f++)a[f*r]-=Math.pow(o,f+1),a[(r-1)*r+f]-=Math.pow(o,r-f);2*o-1>0&&(a[(r-1)*r]+=Math.pow(2*o-1,r));for(let f=0;f<r;f++)for(let m=0;m<r;m++){let u=f-m+1;if(u>0){let h=1;for(let p=2;p<=u;p++)h*=p;a[f*r+m]/=h}}let[l,i]=Li(a,r,e),s=l[(n-1)*r+n-1],c=i;for(let f=1;f<=e;f++)s=s*f/e,Math.abs(s)<2**-500&&(s*=2**500,c-=500);for(;c>1e3;)s*=2**1e3,c-=1e3;for(;c<-1e3;)s*=2**-1e3,c+=1e3;return s*2**c}function Sr(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=be(Si,xt(e,e)),a=o[0]+o[1],l=Math.exp(-o[0])*(1-o[1]),i=0;for(let c=2;c<100;c++){let f=Math.exp(-(2*c-1)*(2*c-1)*a);if(i+=f,f<=1e-17*(i+l))break}let s=Math.sqrt(2*Math.PI)/e*(l+i);return t?s:1-s}let n=0;for(let o=1;o<100;o++){let a=Math.exp(-2*o*o*e*e);if(n+=o%2===1?a:-a,a<=1e-17*Math.abs(n))break}let r=2*n;return t?1-r:r}function bs(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=0)return 1;if(t>1)return 0;let[n,r]=xt(e,t),o=Math.floor(e-n-r),a=0;for(let l=0;l<=o;l++){let i=B(ot(e-l,-n),[-r,0]);if(i[0]<=0)break;let s=B(ot(l,n),[r,0]),c=be(s,[e,0]),f=be(i,[e,0]);a+=qo(l,e-l,c,f,!1)/(c[0]+c[1])}return Math.min(t*a,1)}function Ns(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 a=t+n,l;if(r.z!==void 0){if(r.z.length!==a)throw new RangeError("z must have length m + n");let u=[...r.z].sort((h,p)=>h-p);l=new Array(a+1).fill(!1);for(let h=1;h<a;h++)l[h]=u[h]!==u[h-1]}let i=e*t*n*(1-1e-10),s=(u,h)=>{let p=u*n-h*t;return(o==="two.sided"?Math.abs(p):o==="greater"?p:-p)>=i},c=new Float64Array(n+1),f=new Float64Array(n+1),m=0;for(let u=0;u<=t;u++){for(let h=0;h<=n;h++){if(u===0&&h===0){f[0]=1;continue}let p=u+h,d=(u>0?u/p*c[h]:0)+(h>0?h/p*f[h-1]:0);(l===void 0?p<a:l[p])&&s(u,h)?(d>0&&(m+=d*Et(u,t,n,p,!1)),f[h]=0):f[h]=d}[c,f]=[f,c],f.fill(0)}return Math.min(m,1)}function ys(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 Ms(e,t,n={}){if(typeof e=="string"){let w=Ye(e,t);return Ms(w.x,w.y,n)}let r=Array.from(e).filter(w=>!Number.isNaN(w)).sort((w,b)=>w-b),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let a=te(n.alternative),l=a==="two.sided"?"D":a==="greater"?"D^+":"D^-";if(typeof t=="function"){let w=-1/0,b=-1/0;for(let x=0;x<o;x++){let q=t(r[x]);w=Math.max(w,(x+1)/o-q),b=Math.max(b,q-x/o)}let N=a==="two.sided"?Math.max(w,b):a==="greater"?w:b,M=new Set(r).size<o,E=n.exact??(o<100&&!M),y;return a==="two.sided"?y=E?1-vs(o,N):Sr(Math.sqrt(o)*N,!1):y=E?bs(o,N):Math.exp(-2*o*N*N),{method:`${E?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:ys(a,!0),exact:E,statistic:N,statisticName:l,pValue:Math.min(1,Math.max(0,y))}}let i=Array.from(t).filter(w=>!Number.isNaN(w)),s=i.length;if(s<1)throw new RangeError("not enough 'y' data");let c=r.map(w=>[w,0]).concat(i.map(w=>[w,1]));c.sort((w,b)=>w[0]-b[0]);let f=0,m=0,u=0,h=0;for(let w=0;w<c.length;w++){if(c[w][1]===0?f++:m++,w+1<c.length&&c[w+1][0]===c[w][0])continue;let b=f/o-m/s;u=Math.max(u,b),h=Math.max(h,-b)}let p=a==="two.sided"?Math.max(u,h):a==="greater"?u:h,d=new Set(c.map(w=>w[0])).size<c.length,g=n.exact??o*s<1e4,v;if(g)v=Ns(p,o,s,d?{alternative:a,z:c.map(w=>w[0])}:{alternative:a});else{let w=o*s/(o+s);v=a==="two.sided"?Sr(Math.sqrt(w)*p,!1):Math.exp(-2*w*p*p)}return{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:ys(a,!1),exact:g,statistic:p,statisticName:l,pValue:Math.min(1,Math.max(0,v))}}function xs(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,a=ce(t);for(let i=0;i<r.length;i++){let s=a[i];if(typeof s=="number"&&Number.isNaN(s)||n&&Number.isNaN(r[i]))continue;let c=o.get(s);c===void 0&&o.set(s,c=[]),c.push(r[i])}let l=Array.from(o.keys()).sort((i,s)=>typeof i=="number"&&typeof s=="number"?i-s:String(i)<String(s)?-1:String(i)>String(s)?1:0);return{levels:l,groups:l.map(i=>o.get(i))}}function Lr(e,t,n){let r=[],o=[];for(let i=0;i<e-1;i++)for(let s=i+1;s<e;s++)r.push(t(s,i)),o.push([s-1,i]);let a=It(r,n),l=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([i,s],c)=>l[i][s]=a[c]),l}function Ir(e,t,n,r){return{method:e,pValue:n,rowNames:t.slice(1).map(ge),colNames:t.slice(0,-1).map(ge),pAdjustMethod:r}}function Ii(e,t,n={}){let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let a=te(n.alternative),l=n.pAdjustMethod??"holm",{levels:i,groups:s}=xs(e,t,!r),c=i.length;if(c<2)throw new RangeError("there must be at least two groups");let f,m;if(o){m="t tests with pooled SD";let u=s.map(v=>v.length),h=s.map(v=>v.length>0?se(v):NaN),p=0,d=0;s.forEach(v=>{v.length>1&&(p+=re(v)*(v.length-1),d+=v.length-1)});let g=Math.sqrt(p/d);f=(v,w)=>{let b=(h[v]-h[w])/(g*Math.sqrt(1/u[v]+1/u[w]));return a==="less"?Ne(b,d,!0,!1):a==="greater"?Ne(b,d,!1,!1):2*Ne(-Math.abs(b),d,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>Lt(s[u],s[h],{paired:r,alternative:a}).pValue;return Ir(m,i,Lr(c,f,l),l)}function Fi(e,t,n={}){let{pAdjustMethod:r,alternative:o,...a}=n,l=r??"holm",{levels:i,groups:s}=xs(e,t,!(a.paired??!1)),c=i.length;if(c<2)throw new RangeError("there must be at least two groups");let f="",m=Lr(c,(u,h)=>{let p=tn(s[u],s[h],{...a,...o===void 0?{}:{alternative:o}});return f=p.method,p.pValue},l);return Ir(f,i,m,l)}function Oi(e,t,n={}){if(e.length!==t.length)throw new RangeError("'x' and 'n' must have the same length");let r=[],o=[];e.forEach((f,m)=>{Number.isNaN(f)||Number.isNaN(t[m])||(r.push(f),o.push(t[m]))});let a=r.length;if(a<2)throw new RangeError("too few groups");let l=n.pAdjustMethod??"holm",i=n.correct??!0,s=Lr(a,(f,m)=>Nr([r[f],r[m]],[o[f],o[m]],{correct:i}).pValue,l),c=Array.from({length:a},(f,m)=>m+1);return Ir("Pairwise comparison of proportions",c,s,l)}var nn=[0,0];function Es(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=le(e,xt(t,t));return ot(t,n[0]/(2*t))}function jn(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Es([e[r],0]))}function Ft(e,t){return We(e,t,[]).fit}function We(e,t,n){let r=t.length,o=Math.min(e.length,r),a=e.map(f=>f.slice()),l=t.slice(),i=n.map(f=>f.slice()),s=[];for(let f=0;f<o;f++){let m=a[f],u=nn;for(let v=f;v<r;v++)u=B(u,oe(m[v],m[v]));if(u[0]===0){s.push(nn);continue}let h=Es(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=B(m[f],p);let d=oe(p,m[f]),g=v=>{let w=nn;for(let N=f;N<r;N++)w=B(w,oe(m[N],v[N]));let b=be(w,d);for(let N=f;N<r;N++)v[N]=le(v[N],oe(b,m[N]))};for(let v=f+1;v<e.length;v++)g(a[v]);g(l);for(let v of i)g(v);s.push([-p[0],-p[1]])}let c=new Array(o).fill(nn);for(let f=o-1;f>=0;f--){let m=l[f];for(let u=f+1;u<o;u++)m=le(m,oe(a[u][f],c[u]));c[f]=be(m,s[f])}return{fit:{qty:l,coef:c},qtv:i}}function rn(e,t=0,n=e.length){let r=nn;for(let o=t;o<n;o++)r=B(r,oe(e[o],e[o]));return r}function _i(e,t,n,r,o){let a=t.length,l=n.flatMap((d,g)=>rn(d)[0]>1e-5?[g]:[]),i=0,s=[],c=t.slice();if(l.length>0){let d=pe(Array.from({length:a},(g,v)=>l.map(w=>n[w][v][0])));i=d.rank,s=d.pivot.slice(0,i).map(g=>l[g]),c=Ft(s.map(g=>n[g]),t).qty}let f=[];s.forEach((d,g)=>{let v=r[d],w=f.find(b=>b.term===v);w===void 0&&f.push(w={term:v,df:0,ss:[0,0]}),w.df++,w.ss=B(w.ss,oe(c[g],c[g]))});let m=a-i,u=rn(c,i)[0],h=f.filter(d=>d.term!==0),p=h.map(d=>o[d.term-1]);if(m>0){let d=u/m,g=h.map(({df:v,ss:w})=>{let b=w[0]/v/d;return[v,w[0],w[0]/v,b,ae(b,v,m,!1,!1)]});return g.push([m,u,d,NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...p,"Residuals"],table:g}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:p,table:h.map(({df:d,ss:g})=>[d,g[0],g[0]/d])}}function qs(e,t,n,r,o,a){if(a.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let l=n??{type:"num",value:1,text:"1"},i=_o({response:t.response,rhs:l},Object.keys(o)).variables,s=ut(t,o,{...a,dropUnusedLevels:!0,contrastVariables:i},l),c=s.y;if(c===void 0)throw new TypeError("the formula has no response");let f=ut({response:t.response,rhs:r},o,{...a.levels===void 0?{}:{levels:a.levels},allContrasts:"contr.helmert",intercept:s.terms.intercept,extraVariables:s.terms.variables,dropUnusedLevels:!0}),m=c.length,u=[...s.warnings,...f.warnings.filter(y=>!s.warnings.includes(y))],h=f.colNames.length,p=h>0?pe(f.x):void 0,d=p?.rank??0;d<h&&u.push("Error() model is singular");let g=p?.pivot.slice(0,d)??[],v=y=>y.map(x=>[x,0]),w=s.colNames.map((y,x)=>v(s.x.map(q=>q[x]))),{fit:b,qtv:N}=We(g.map(y=>v(f.x.map(x=>x[y]))),v(c),w),M=[];g.forEach((y,x)=>{let q=f.assign[y],A=q===0?"(Intercept)":f.terms.termLabels[q-1],S=M.find(L=>L.name===A);S===void 0&&M.push(S={name:A,idx:[]}),S.idx.push(x)}),m>d&&M.push({name:"Within",idx:Array.from({length:m-d},(y,x)=>d+x)});let E=M.map(y=>_i(y.name,y.idx.map(x=>b.qty[x]),N.map(x=>y.idx.map(q=>x[q])),s.assign,s.terms.termLabels));return{formula:e,multistratum:!0,strata:E,rows:s.frame.rows,warnings:u}}function Fr(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=As(t.x,r,t.frame.weights,t.terms.intercept,!0),a=o.result,l=[...t.warnings];if(!n){let s=o.fitRows.map(u=>a.fittedValues[u]),c=s.reduce((u,h)=>u+h,0)/s.length,f=s.length>1?s.reduce((u,h)=>u+(h-c)**2,0)/(s.length-1):NaN,m=o.rss/a.dfResidual;Number.isFinite(m)&&m<1e-30*(c*c+f)&&l.push("essentially perfect fit: summary may be unreliable")}let i={...a,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:l};return t.frame.weights!==void 0&&(i.weights=t.frame.weights),i}function Or(e,t,n={}){return typeof e=="string"?Fr(e,ut(xn(e),t,{...n,dropUnusedLevels:!0}),!1):Vi(e,t,n)}function Vi(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]),a=Array.from(t);if(o.length!==a.length)throw new RangeError("variable lengths differ (found for 'x')");let l=n.weights===void 0?void 0:Array.from(n.weights);if(l!==void 0&&l.length!==a.length)throw new RangeError("variable lengths differ (found for '(weights)')");if(l?.some(m=>m<0))throw new RangeError("missing or negative weights not allowed");let i=a.map((m,u)=>!Number.isNaN(m)&&o[u].every(h=>!Number.isNaN(h))&&(l===void 0||!Number.isNaN(l[u]))),s=o.filter((m,u)=>i[u]).map(m=>r?[1,...m]:m),c=a.filter((m,u)=>i[u]),f=l?.filter((m,u)=>i[u]);return As(s,c,f,r).result}function Ci(e,t,n,r,o,a){let l=o.rank,i=o.pivot.slice(0,l),s=jn(n===void 0?void 0:r.map(w=>n[w]),r.length),c=i.map(w=>r.map((b,N)=>Te(s[N],e[b][w]))),{qty:f,coef:m}=Ft(c,r.map((w,b)=>Te(s[b],t[w]))),u=e[0]?.map(()=>NaN)??[];i.forEach((w,b)=>u[w]=m[b][0]);let h=e.map(w=>{let b=[0,0];return i.forEach((N,M)=>b=B(b,Te(m[M],w[N]))),b}),p=rn(f,l)[0],d=r.map(w=>n?.[w]??1),g=[0,0];if(a){let w=[0,0],b=[0,0];r.forEach((N,M)=>{w=B(w,[d[M],0]),b=B(b,Te(h[N],d[M]))}),g=be(b,w)}let v=[0,0];return r.forEach((w,b)=>{let N=le(h[w],g);v=B(v,Te(oe(N,N),d[b]))}),{coefficients:u,fittedValues:h.map(w=>w[0]),residuals:h.map((w,b)=>le([t[b],0],w)[0]),effects:f.map(w=>w[0]),rss:p,mss:v[0]}}function As(e,t,n,r,o=!1){if(t.length===0)throw new RangeError("0 (non-NA) cases");let a=t.map((D,G)=>n===void 0||n[G]!==0),l=e.filter((D,G)=>a[G]),i=t.filter((D,G)=>a[G]),s=n?.filter((D,G)=>a[G]),c=i.length;if(c===0)throw new RangeError("all weights are zero");let f=t.flatMap((D,G)=>a[G]?[G]:[]),m=s?.map(Math.sqrt),u=m===void 0?l:l.map((D,G)=>D.map(Q=>Q*m[G])),h=m===void 0?i:i.map((D,G)=>D*m[G]),p=pe(u),d=p.rank,g=o?Ci(e,t,n,f,p,r):void 0,v=g?.coefficients??Tt(p,h),w=g?.residuals??[],b=g?.fittedValues??[],N=c-d,M=s??new Array(c).fill(1),E=g?.rss??0;if(g===void 0){let D=An(p,h),G=m===void 0?D:D.map((ne,ve)=>ne/m[ve]),Q=0;t.forEach((ne,ve)=>{if(a[ve])w.push(G[Q]),b.push(ne-G[Q]),Q++;else{let ye=e[ve].reduce((Oe,_e,Ie)=>Number.isNaN(v[Ie])?Oe:Oe+_e*v[Ie],0);b.push(ye),w.push(ne-ye)}});for(let ne=0;ne<c;ne++)E+=M[ne]*G[ne]**2}let y=b.filter((D,G)=>a[G]),x=E/N,q=Math.sqrt(x),A=p.qr.slice(0,d).map(D=>D.slice(0,d)),S=d>0?kn(A):[],R=p.pivot.slice(0,d).map((D,G)=>[D,G]).sort((D,G)=>D[0]-G[0]),k=R.map(([D])=>v[D]),C=R.map(([,D])=>Math.sqrt(S[D][D]*x)),O=k.map((D,G)=>D/C[G]),I=O.map(D=>2*Ne(-Math.abs(D),N,!0,!1)),V={coefficients:v,residuals:w,fittedValues:b,rank:d,dfResidual:N,coefTable:{estimate:k,stdError:C,tValue:O,pValue:I},sigma:q,rSquared:0,adjRSquared:0,intercept:r,qr:p},z=r?1:0,Y=d-z,T={result:V,fitRows:f,effects:g?.effects??[],rss:E};if(Y===0)return T;let F=0;if(g!==void 0)F=g.mss;else if(r){let D=M.reduce((Q,ne)=>Q+ne,0),G=0;for(let Q=0;Q<c;Q++)G+=M[Q]*y[Q]/D;for(let Q=0;Q<c;Q++)F+=M[Q]*(y[Q]-G)**2}else for(let D=0;D<c;D++)F+=M[D]*y[D]**2;V.rSquared=F/(F+E),V.adjRSquared=1-(1-V.rSquared)*((c-z)/N);let P=F/Y/x;return V.fStatistic={value:P,numdf:Y,dendf:N},V.fPValue=ae(P,Y,N,!1,!1),T}function Di(e,t={}){let n=t.level??.95,r=ze((1+n)/2,e.dfResidual,!0,!1),{estimate:o,stdError:a}=e.coefTable,l=e.coefficients.map(()=>NaN),i=e.coefficients.map(()=>NaN),s=0;return e.coefficients.forEach((c,f)=>{Number.isNaN(c)||(l[f]=o[s]-r*a[s],i[f]=o[s]+r*a[s],s++)}),{lower:l,upper:i}}function Pi(e,t,n={}){let o=(t.length>0&&typeof t[0]!="number"?t.map(p=>Array.from(p)):Array.from(t,p=>[p])).map(p=>e.intercept?[1,...p]:p),a=e.rank,l=e.qr.pivot.slice(0,a),i=e.qr.qr.slice(0,a).map(p=>p.slice(0,a)),s=e.sigma**2,c=o.map(p=>p.reduce((d,g,v)=>Number.isNaN(e.coefficients[v])?d:d+g*e.coefficients[v],0)),f=o.map(p=>{let d=new Array(a).fill(0);for(let g=0;g<a;g++){let v=p[l[g]];for(let w=0;w<g;w++)v-=i[w][g]*d[w];d[g]=v/i[g][g]}return Math.sqrt(d.reduce((g,v)=>g+v*v,0)*s)}),m=n.interval??"none";if(m==="none")return{fit:c,seFit:f};let u=ze((1+(n.level??.95))/2,e.dfResidual,!0,!1),h=f.map(p=>u*Math.sqrt(p*p+(m==="prediction"?s:0)));return{fit:c,seFit:f,lwr:c.map((p,d)=>p-h[d]),upr:c.map((p,d)=>p+h[d])}}function Rs(e){let t=e.flat(),n=Qt(t),r=[0,0],o=[0,0],a=[];for(let l of e){let i=Qt(l);a.push(i[0]);let s=le(i,n);r=B(r,oe(oe(s,s),[l.length,0]));for(let c of l){let f=le([c,0],i);o=B(o,oe(f,f))}}return{ssb:r[0],ssw:o[0],means:a}}function _r(e,t,n={}){return typeof e=="string"?$i(e,t,n):ji(e,t)}function $i(e,t,n){let r=xn(e,!0),{treatment:o,errors:a}=Oo(r.rhs);if(a.length>1)throw new Error(`there are ${a.length} Error terms: only 1 is allowed`);return a.length===1?qs(e,r,o,a[0],t,n):Fr(e,ut(r,t,{...n,dropUnusedLevels:!0},o),!0)}function ji(e,t){let n=dt(e,t),r=n.length;if(r<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=n.reduce((u,h)=>u+h.length,0),{ssb:a,ssw:l}=Rs(n),i=r-1,s=o-r,c=a/i,f=l/s,m=c/f;return{df:[i,s],sumSq:[a,l],meanSq:[c,f],fValue:m,pValue:s>0?ae(m,i,s,!1,!1):NaN}}function zi(e,t,n={}){let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:a}=pt(e,r),l=a.length;if(l<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let i=n.confLevel??.95,c=a.reduce((d,g)=>d+g.length,0)-l,{ssw:f,means:m}=Rs(a),u=f/c,h=Lo(i,l,c),p={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:[]};for(let d=0;d<l-1;d++)for(let g=d+1;g<l;g++){let v=m[g]-m[d],w=Math.sqrt(u/2*(1/a[g].length+1/a[d].length));p.names.push(`${o[g]}-${o[d]}`),p.i.push(g+1),p.j.push(d+1),p.diff.push(v),p.lwr.push(v-h*w),p.upr.push(v+h*w),p.pAdj.push(Nn(Math.abs(v)/w,l,c,{lowerTail:!1}))}return p}function Wi(e,t,n={}){let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:a}=n,l=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&&a===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");if(Number.isNaN(l))throw new RangeError("mu must be a number");let i=te(n.alternative),s=n.confLevel??.95;if(!(s>=0&&s<=1))throw new RangeError("confLevel must be between 0 and 1");let c=Array.from(e).filter(b=>!Number.isNaN(b)),f=r===void 0?void 0:Array.from(r).filter(b=>!Number.isNaN(b));if(c.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=Qt(c)[0],u=f===void 0?NaN:Qt(f)[0],h=f===void 0?o/Math.sqrt(c.length):Math.sqrt(o**2/c.length+a**2/f.length),p=f===void 0?m:m-u,d=(p-l)/h,g,v;if(i==="less")g=$(d),v=[-1/0,p+X(s)*h];else if(i==="greater")g=$(d,{lowerTail:!1}),v=[p-X(s)*h,1/0];else{g=2*$(-Math.abs(d));let b=X(1-(1-s)/2)*h;v=[p-b,p+b]}let w=f!==void 0;return{method:w?"Two-sample z-Test":"One-sample z-Test",alternative:i,statistic:d,statisticName:"z",pValue:g,confInt:v,confLevel:s,estimate:w?[m,u]:m,estimateNames:w?["mean of x","mean of y"]:"mean of x",nullValue:l,nullValueName:w?"difference in means":"mean"}}var Ot=2**-52;function Gi(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function zn(e,t,n){return e.map((r,o)=>{let a=0;for(let l=0;l<r.length;l++)a+=r[l]*t[l];return a+n[o]})}function wt(e,t,n,r){let o=0;for(let a=0;a<t.length;a++)o+=e.devContribution(t[a],n[a],r[a]);return o}function ks(e,t){let{x:n,p:r,y:o,w:a,offset:l,fam:i,epsilon:s,maxit:c}=e,f=o.length;return Yt(t,()=>{let m=(L,R)=>L.every(k=>i.etaOk(k))&&R.every(k=>i.muOk(k));if(r===0){let L=l.slice();if(!L.every(O=>i.etaOk(O)))throw new Error("invalid linear predictor values in empty model");let R=L.map(O=>i.linkinv(O));if(!R.every(O=>i.muOk(O)))throw new Error("invalid fitted means in empty model");let k=L.map(O=>i.muEta(O)),C=R.map((O,I)=>Math.sqrt(a[I]*k[I]**2/i.variance(O))**2);return{coefficients:[],eta:L,mu:R,deviance:wt(i,o,R,a),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:a.map(O=>O>0),workingWeights:C,workingResiduals:o.map((O,I)=>(O-R[I])/k[I])}}let u,h;if(e.start!==void 0){if(e.start.length!==r)throw new RangeError(`length of 'start' should equal ${r} and correspond to initial coefs for ${Gi(e.columnNames)}`);h=e.start.slice(),u=zn(n,h,l)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(L=>i.link(L));let p=u.map(L=>i.linkinv(L));if(!m(u,p))throw new Error("cannot find valid starting values: please specify some");let d=wt(i,o,p,a),g=d,v=!1,w=!1,b=0,N=new Array(r).fill(0),M,E=a.map(()=>!1),y=[],x=Math.min(1e-7,s/1e3);for(b=1;b<=c;b++){let L=u.map(I=>i.muEta(I));for(let I=0;I<f;I++){if(!(a[I]>0))continue;let V=i.variance(p[I]);if(Number.isNaN(V))throw new Error("NAs in V(mu)");if(V===0)throw new Error("0s in V(mu)");if(Number.isNaN(L[I]))throw new Error("NAs in d(mu)/d(eta)")}E=a.map((I,V)=>I>0&&L[V]!==0);let R=[];for(let I=0;I<f;I++)E[I]&&R.push(I);if(R.length===0){t.add(`no observations informative at iteration ${b}`),v=!1;break}y=new Array(f).fill(0);let k=[],C=[];for(let I of R){let V=Math.sqrt(a[I]*L[I]**2/i.variance(p[I]));y[I]=V,k.push((u[I]-l[I]+(o[I]-p[I])/L[I])*V),C.push(n[I].map(z=>z*V))}M=pe(C,{tol:x});let O=Tt(M,k);if(O.some((I,V)=>M.pivot.indexOf(V)<M.rank&&!Number.isFinite(I))){t.add(`non-finite coefficients at iteration ${b}`),v=!1;break}if(N=O.map(I=>Number.isNaN(I)?0:I),w=!1,u=zn(n,N,l),p=u.map(I=>i.linkinv(I)),g=wt(i,o,p,a),!Number.isFinite(g)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated due to divergence");let I=0;for(;!Number.isFinite(g);){if(I>=c)throw new Error("inner loop 1; cannot correct step size");I++,N=N.map((V,z)=>(V+h[z])/2),u=zn(n,N,l),p=u.map(V=>i.linkinv(V)),g=wt(i,o,p,a)}w=!0}if(!m(u,p)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated: out of bounds");let I=0;for(;!m(u,p);){if(I>=c)throw new Error("inner loop 2; cannot correct step size");I++,N=N.map((V,z)=>(V+h[z])/2),u=zn(n,N,l),p=u.map(V=>i.linkinv(V))}w=!0,g=wt(i,o,p,a)}if(e.untilCoefficients){let I=Math.max(...N.map(Math.abs));if(h!==void 0&&N.every((V,z)=>Math.abs(V-h[z])<=8*Ot*I)){v=!0;break}}else if(Math.abs(g-d)/(Math.abs(g)+.1)<s){v=!0;break}d=g,h=N.slice()}b>c&&(b=c);let q=r,A=N.slice();if(M!==void 0){q=M.rank;for(let L=M.rank;L<r;L++)A[M.pivot[L]]=NaN}let S=u.map(L=>i.muEta(L));return{coefficients:A,eta:u,mu:p,deviance:g,iter:b,converged:v,boundary:w,rank:q,qr:M,used:E,workingWeights:y.map(L=>L*L),workingResiduals:o.map((L,R)=>(L-p[R])/S[R])}})}function Ts(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function Ss(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*Ot||o<10*Ot)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*Ot)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function _t(e,t,n,r={}){let o=Jt(e.familySpec),a="start"in r?r.start:e.start,l=Wo(o,e.yGiven,e.priorGiven,a!==void 0||r.etastart!==void 0,n),i=r.extraOffset===void 0?e.offset:e.offset.map((f,m)=>f+r.extraOffset[m]),s=e.x.map(f=>t.map(m=>f[m])),c=ks({x:s,p:t.length,y:l.y,w:l.weights,offset:i,fam:o,epsilon:r.epsilon??e.epsilon,maxit:r.maxit??e.maxit,columnNames:t.map(f=>e.columnNames[f]),start:a,etastart:r.etastart,mustart:l.mustart,untilCoefficients:r.untilCoefficients},n);return Ss(c,o,n,t.length===0),{fit:c,fam:o,y:l.y,w:l.weights,size:l.size,offset:i}}function Ui(e,t,n={}){let r=n.intercept??!0,o=Ts(e),l=t.length>0&&typeof t[0]!="number"?t.map(C=>Array.from(C)):Array.from(t),i=l.length;if(o.length!==i)throw new RangeError("variable lengths differ (found for 'x1')");let s=n.weights===void 0?void 0:Array.from(n.weights);if(s!==void 0&&s.length!==i)throw new RangeError("variable lengths differ (found for '(weights)')");let c=n.offset===void 0?void 0:Array.from(n.offset);if(c!==void 0&&c.length!==i)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((C,O)=>{let I=l[O];return!(Array.isArray(I)?I.some(z=>Number.isNaN(z)):Number.isNaN(I))&&C.every(z=>!Number.isNaN(z))&&!(s!==void 0&&Number.isNaN(s[O]))&&!(c!==void 0&&Number.isNaN(c[O]))}),m=o.filter((C,O)=>f[O]).map(C=>r?[1,...C]:C),u=l.filter((C,O)=>f[O]),h=u.length;if(h===0)throw new RangeError("0 (non-NA) cases");let p=s===void 0?new Array(h).fill(1):s.filter((C,O)=>f[O]);if(p.some(C=>C<0))throw new RangeError("negative weights not allowed");let d=c===void 0?new Array(h).fill(0):c.filter((C,O)=>f[O]),g=o.length>0?o[0].length:0,v=[...r?["(Intercept)"]:[],...Array.from({length:g},(C,O)=>`x${O+1}`)],w=v.length;if(m.some(C=>C.length!==w))throw new TypeError("all rows of x must have the same length");let b=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(b.link=n.link);let N={x:m,columnNames:v,yGiven:u,priorGiven:p,offset:d,hasOffset:c!==void 0,intercept:r,familySpec:b,epsilon:n.epsilon??1e-8,maxit:n.maxit??25,start:n.start===void 0?void 0:Array.from(n.start)};if(!(N.epsilon>0))throw new RangeError("value of 'epsilon' must be > 0");if(!(N.maxit>0))throw new RangeError("maximum number of iterations must be > 0");let M=new Ae,E=Array.from({length:w},(C,O)=>O),{fit:y,fam:x,y:q,w:A,size:S}=_t(N,E,M),L;if(r&&N.hasOffset){let C=new Ae,O=ks({x:m.map(()=>[1]),p:1,y:q,w:A,offset:d,fam:x,epsilon:N.epsilon,maxit:N.maxit,columnNames:["(Intercept)"],mustart:y.mu},C);Ss(O,x,C,!1),C.list.forEach(I=>M.add(I)),L=O.deviance,O.converged||M.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let C=0,O=0;for(let V=0;V<h;V++)C+=A[V],O+=A[V]*q[V];let I=O/C;L=Yt(M,()=>wt(x,q,q.map(()=>I),A))}else L=Yt(M,()=>wt(x,q,d.map(C=>x.linkinv(C)),A));let R=h-A.filter(C=>C===0).length,k=Go(x,q,y.mu,A,S,y.deviance,M)+2*y.rank;return{coefficients:y.coefficients,fittedValues:y.mu,linearPredictors:y.eta,deviance:y.deviance,nullDeviance:L,aic:k,rank:y.rank,dfResidual:R-y.rank,dfNull:R-(r?1:0),iter:y.iter,converged:y.converged,boundary:y.boundary,y:q,priorWeights:A,weights:y.workingWeights,residuals:y.workingResiduals,family:x.family,link:x.name,warnings:M.list,qr:y.qr,model:N}}function Wn(e){return Jt(e.model.familySpec)}function Vt(e,t){if(Wn(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/e.dfResidual}function Hi(e){if(e.qr===void 0||e.rank===0)return[];let t=e.qr.qr.slice(0,e.rank).map(n=>n.slice(0,e.rank));return kn(t)}function Vr(e,t={}){let n=new Ae,r=Wn(e),o=t.dispersion===void 0&&!r.fixedDispersion,a=t.dispersion??Vt(e,n),l=Hi(e),s=(e.qr===void 0?[]:e.qr.pivot.slice(0,e.rank)).map(h=>e.coefficients[h]),c=l.map((h,p)=>Math.sqrt(h[p]*a)),f=s.map((h,p)=>h/c[p]),m=!o,u=f.map(h=>m?2*$(-Math.abs(h)):e.dfResidual>0?2*Ne(-Math.abs(h),e.dfResidual,!0,!1):NaN);return o&&e.dfResidual===0&&(c.fill(NaN),f.fill(NaN)),{dispersion:a,estimate:s,stdError:c,statistic:f,pValue:u,statisticName:r.fixedDispersion?"z value":"t value",covUnscaled:l,covScaled:l.map(h=>h.map(p=>p*a)),dfResidual:e.dfResidual,warnings:n.list}}function Cr(e,t="deviance"){let n=Wn(e),{y:r,fittedValues:o,priorWeights:a}=e;switch(t){case"response":return r.map((l,i)=>l-o[i]);case"working":return e.residuals.slice();case"pearson":return r.map((l,i)=>(l-o[i])*Math.sqrt(a[i])/Math.sqrt(n.variance(o[i])));case"deviance":return e.dfResidual<=0?r.map(()=>0):r.map((l,i)=>Math.sign(l-o[i])*Math.sqrt(Math.max(n.devContribution(l,o[i],a[i]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function Dr(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let l=n.qr.length,i=Array.from({length:l},(c,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=zo(n,i).map(c=>c.reduce((f,m)=>f+m*m,0))}let o=0,a=e.weights.map(l=>l>0);return e.priorWeights.forEach((l,i)=>{let s=a[i]?r[o++]??0:0;l>0&&t.push(s>1-10*Ot?1:s)}),t}function Bi(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 Xi(e,t={}){let n=Dr(e),r=t.dispersion??Vt(e),o=Cr(e,"deviance").filter((l,i)=>e.priorWeights[i]>0),a=100*Ot*Bi(o.map(Math.abs));return o.map((l,i)=>{let s=(Math.abs(l)<a?0:l)/Math.sqrt(r*(1-n[i]));return Number.isFinite(s)?s:NaN})}function Qi(e,t={}){let n=Dr(e),r=t.dispersion??Vt(e);return Cr(e,"pearson").filter((a,l)=>e.priorWeights[l]>0).map((a,l)=>{let i=(a/(1-n[l]))**2*(n[l]/(r*e.rank));return Number.isFinite(i)?i:NaN})}function Ki(e,t={}){let n=Wn(e),r,o;if(t.newdata===void 0)r=e.model.x,o=e.linearPredictors.slice();else{r=Ts(t.newdata).map(m=>e.model.intercept?[1,...m]:m);let f=t.newoffset===void 0?r.map(()=>0):Array.from(t.newoffset);if(f.length!==r.length)throw new RangeError("newoffset must have one value per row of newdata");o=r.map((m,u)=>m.reduce((h,p,d)=>Number.isNaN(e.coefficients[d])?h:h+p*e.coefficients[d],0)+f[u])}let a=t.dispersion??Vt(e),l=e.rank,i=e.qr===void 0?[]:e.qr.pivot.slice(0,l),s=e.qr?.qr,c=r.map(f=>{let m=new Array(l).fill(0),u=0;for(let h=0;h<l;h++){let p=f[i[h]];for(let d=0;d<h;d++)p-=s[d][h]*m[d];m[h]=p/s[h][h],u+=m[h]*m[h]}return Math.sqrt(u)*Math.sqrt(a)});return(t.type??"link")==="link"?{fit:o,seFit:c}:Yt(new Ae,()=>({fit:o.map(f=>n.linkinv(f)),seFit:c.map((f,m)=>f*Math.abs(n.muEta(o[m])))}))}var Ls={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Pr(e,t,n,r,o=1e-11){let a=[];for(let d=0;d<t.length;d++)n[d]>0&&a.push(d);let l=0,i=0;for(let d of a)l+=n[d],i+=n[d]*t[d];let s=r?i/l:0,c=0;for(let d of a)c+=n[d]*(t[d]-s)**2;if((e.length>0?e[0].length:0)===0||a.length===0)return 0;let m=a.map(d=>Math.sqrt(n[d])),u=pe(a.map((d,g)=>e[d].map(v=>v*m[g])),{tol:o}),h=An(u,a.map((d,g)=>t[d]*m[g])),p=0;for(let d of h)p+=d*d;return c-p}function Zi(e,t={}){let n=t.level??.95,r=t.test??"LRT";if(!(n>0&&n<1))throw new RangeError("'level' must be in (0, 1)");let o=e.coefficients.length,a=t.parm??Array.from({length:o},(E,y)=>y);for(let E of a)if(!Number.isInteger(E)||E<0||E>=o)throw new RangeError(`invalid coefficient index ${E}`);let l=e.model,i=e.coefficients.map((E,y)=>y).filter(E=>!Number.isNaN(e.coefficients[E])),s=new Ae,c=_t(l,i,s,{...Ls,start:l.start===void 0?void 0:i.map(E=>l.start[E])}),f=new Array(o).fill(NaN);i.forEach((E,y)=>f[E]=c.fit.coefficients[y]);let m={...e,coefficients:f,weights:c.fit.workingWeights,residuals:c.fit.workingResiduals,qr:c.fit.qr,rank:c.fit.rank},u=Vr(m),h=u.dispersion,p=new Array(o).fill(NaN);(c.fit.qr===void 0?[]:c.fit.qr.pivot.slice(0,c.fit.rank)).forEach((E,y)=>p[i[E]]=u.stdError[y]);let g=l.x.map(E=>i.map(y=>E[y])),v=c.fit.eta,w=(E,y)=>{let x=i.filter(S=>S!==E),q=_t(l,x,new Ae,{...Ls,start:void 0,etastart:v,extraOffset:l.x.map(S=>S[E]*y)}).fit,A=r==="LRT"?q.deviance-c.fit.deviance:Pr(g,q.workingResiduals,q.workingWeights,!1);return Math.sign(y-f[E])*Math.sqrt(Math.max(A/h,0))},b=[X((1-n)/2),X((1+n)/2)],N=a.map(()=>NaN),M=a.map(()=>NaN);return a.forEach((E,y)=>{if(!Number.isNaN(f[E]))for(let x of[0,1]){let q=x===0?-1:1,A=NaN,S=NaN;for(let V=0;V<=30;V++){let z=f[E]+q*2**V*p[E],Y=w(E,z);if(Math.abs(Y)>=Math.abs(b[x])){A=z,S=Y;break}}if(Number.isNaN(A))continue;let L=V=>V===f[E]?-b[x]:w(E,V)-b[x],[R,k]=x===0?[A,f[E]]:[f[E],A],[C,O]=x===0?[S-b[x],-b[x]]:[-b[x],S-b[x]],I=xe(L,R,k,{tol:1e-14*p[E],maxIter:500,fLower:C,fUpper:O});x===0?N[y]=I:M[y]=I}}),{lower:N,upper:M}}var Yi=(e,t)=>he(e,t/2,2,!1,!1);function Ji(e,t,n){let{fit:r,w:o}=_t(e,t,n);return{dfRes:o.filter(l=>l>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(l=>t.map(i=>l[i])),intercept:e.intercept}}var $r=(e,t)=>Pr(t.x,e.fit.workingResiduals,e.fit.workingWeights,t.intercept);function el(e,t={}){let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=new Ae,o=Array.isArray(e)&&n.length>1,a=n[0],l=Jt(a.model.familySpec),i=l.fixedDispersion,s=t.test===void 0?t.dispersion!==void 0||i?"Chisq":"F":t.test,c=[],f,m,u=[],h;if(o){let y=n[0].model.x.length;if(n.some(x=>x.model.x.length!==y))throw new RangeError("models were not all fitted to the same size of dataset");if(n.forEach((x,q)=>c.push({name:String(q+1),dfRes:x.dfResidual,dev:x.deviance})),f=c.map((x,q)=>q===0?NaN:c[q-1].dfRes-x.dfRes),m=c.map((x,q)=>q===0?NaN:c[q-1].dev-x.dev),s==="Rao"){let x=n.map(q=>({dfRes:q.dfResidual,dev:q.deviance,fit:Is(q),x:q.model.x,intercept:q.model.intercept}));u=c.map((q,A)=>A===0?NaN:f[A]<0?-$r(x[A],x[A-1]):$r(x[A-1],x[A]))}h=n.reduce((x,q)=>q.dfResidual<x.dfResidual?q:x)}else{let y=a,x=y.model,q=x.x.length>0?x.x[0].length-(x.intercept?1:0):0,A=x.intercept?[0]:[],S=R=>[...A,...Array.from({length:R},(k,C)=>C+A.length)],L=[];for(let R=0;R<=q;R++)R===q?L.push({dfRes:y.dfResidual,dev:y.deviance,fit:Is(y),x:x.x,intercept:x.intercept}):R>0||s==="Rao"?L.push(Ji(x,S(R),r)):L.push(void 0);c.push({name:"NULL",dfRes:y.dfNull,dev:y.nullDeviance});for(let R=1;R<=q;R++)c.push({name:`x${R}`,dfRes:L[R].dfRes,dev:L[R].dev});f=c.map((R,k)=>k===0?NaN:c[k-1].dfRes-R.dfRes),m=c.map((R,k)=>k===0?NaN:Math.max(c[k-1].dev-R.dev,0)),s==="Rao"&&(u=c.map((R,k)=>k===0?NaN:$r(L[k-1],L[k]))),h=y}let p=new Ae,d=t.dispersion??Vt(h,p);p.list.forEach(y=>r.add(y));let g=i?1/0:h.dfResidual,v=o?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],w=c.map((y,x)=>o?[y.dfRes,y.dev,f[x],m[x]]:[f[x],m[x],y.dfRes,y.dev]),b=[...v],N=y=>c.map((x,q)=>{if(q===0||f[q]===0)return NaN;let A=y[q]/d*Math.sign(f[q]);return A<0||Number.isNaN(A)?NaN:Yi(A,Math.abs(f[q]))}),M=[];if(s==="Chisq"||s==="LRT")b.push("Pr(>Chi)"),M=[N(m)];else if(s==="Rao")b.push("Rao","Pr(>Chi)"),M=[u,N(u)];else if(s==="F"){if(i&&r.add(`using F test with a '${l.family}' family is inappropriate`),g!==0){let y=c.map((x,q)=>{let A=m[q]/f[q]/d;return q===0||f[q]===0||A<0?NaN:A});b.push("F","Pr(>F)"),M=[y,y.map((x,q)=>Number.isNaN(x)?NaN:ae(x,Math.abs(f[q]),g,!1,!1))]}}else if(s==="Cp"){let y=h.model.x.length;b.push("Cp"),M=[c.map(x=>x.dev+2*d*(y-x.dfRes))]}let E=w.map((y,x)=>[...y,...M.map(q=>q[x])]);return{colNames:b,rowNames:c.map(y=>y.name),table:E,warnings:r.list}}function Is(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}}function tl(e){let{pivot:t}=e.qr,n=[],r=new Map;for(let o=0;o<e.rank;o++){let a=e.assign[t[o]],l=r.get(a);l===void 0&&(l={term:a,df:0,ss:0},r.set(a,l),n.push(l)),l.df++,l.ss+=e.effects[o]**2}return{terms:n,rdf:e.dfResidual,rss:e.rss}}var Fs=(e,t,n)=>ae(e,t,n,!1,!1);function nl(e){let t=tl(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,a)=>r+=(e.weights?.[a]??1)*o*o),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,perfect:t.rss<1e-10*r}}function jr(e){let{names:t,rows:n,rdf:r,rss:o,perfect:a}=nl(e),l=o/r,i=n.map(({df:s,ss:c})=>{let f=c/s/l;return[s,c,c/s,f,Fs(f,s,r)]});return i.push([r,o,r===0?NaN:l,NaN,NaN]),{colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...t,"Residuals"],table:i,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function rl(e){let t=e.map(l=>l.dfResidual),n=e.map(l=>l.rss),r=0;t.forEach((l,i)=>{l<t[r]&&(r=i)});let o=n[r]/t[r],a=e.map((l,i)=>{if(i===0)return[t[0],n[0],NaN,NaN,NaN,NaN];let s=t[i-1]-t[i],c=n[i-1]-n[i],f=s===0?NaN:c/s/o;return f<0&&(f=NaN),[t[i],n[i],s,c,f,Number.isNaN(f)?NaN:Fs(f,Math.abs(s),t[r])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((l,i)=>String(i+1)),table:a,warnings:[]}}function zr(e,...t){let n=[e,...t];for(let l of n)if(typeof l!="object"||l===null||!Array.isArray(l.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1)return jr(e);if(n.some(l=>l.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=n.filter(l=>l.response===e.response),o=[];if(r.length<n.length){let l=[...new Set(n.filter(i=>i.response!==e.response).map(i=>i.response))];o.push(`models with response ${l.map(i=>`\u2018${i}\u2019`).join(", ")} removed because response differs from model 1`)}let a=r.length===1?jr(e):rl(r);return a.warnings=[...o,...a.warnings],a}function Wr(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0)return{strata:e.strata.filter(r=>r.stratum!=="Error: (Intercept)").map(r=>({...r})),warnings:[]};if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=jr(e);return e.dfResidual>0?{...t,warnings:[]}:{colNames:t.colNames.slice(0,3),rowNames:t.rowNames.slice(0,-1),table:t.table.slice(0,-1).map(n=>n.slice(0,3)),warnings:[]}}function Os(e,t,n){let r=[...t,...n];if(r.length===0)return{ss:0,df:0};let o=e.y.flatMap((u,h)=>e.weights===void 0||e.weights[h]!==0?[h]:[]),a=o.map(u=>e.weights===void 0?1:Math.sqrt(e.weights[u])),l=pe(o.map((u,h)=>r.map(p=>e.x[u][p]*a[h]))),i=jn(e.weights===void 0?void 0:o.map(u=>e.weights[u]),o.length),s=l.pivot.slice(0,l.rank),{qty:c}=Ft(s.map(u=>o.map((h,p)=>Te(i[p],e.x[h][r[u]]))),o.map((u,h)=>Te(i[h],e.y[u]))),f=[0,0],m=0;return s.forEach((u,h)=>{u>=t.length&&(f=B(f,oe(c[h],c[h])),m++)}),{ss:f[0],df:m}}function Gr(e,t={}){if(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,a=e.rss;if(o===0)throw new Error("residual df = 0");if(a<Math.sqrt(2220446049250313e-31))throw new Error("residual sum of squares is 0 (within rounding error)");let l=[],i=e.termLabels.length;r===2&&i===0&&e.intercept&&(l.push("the model contains only an intercept: Type III test substituted"),r=3);let s=p=>e.assign.flatMap((d,g)=>p.has(d)?[g]:[]),c=[...new Set(e.assign)],f=[],m=[];if(r===3){if(e.coefficients.some(p=>Number.isNaN(p)))throw new Error("there are aliased coefficients in the model");for(let p of c){let d=s(new Set([p])),g=s(new Set(c.filter(v=>v!==p)));f.push(p===0?"(Intercept)":e.termLabels[p-1]),m.push({ss:Os(e,g,d).ss,df:d.length})}}else{let p=d=>e.factors.flatMap((g,v)=>g[d-1]>0?[v]:[]);for(let d of c){if(d===0)continue;let g=p(d),v=c.filter(N=>N!==0&&N!==d&&g.every(M=>e.factors[M][N-1]>0)),w=s(new Set(c.filter(N=>N!==d&&!v.includes(N)))),b=Os(e,w,s(new Set([d])));f.push(e.termLabels[d-1]),m.push(b)}}let u=a/o,h=m.map(({ss:p,df:d})=>{let g=p/d/u;return[p,d,g,ae(g,d,o,!1,!1)]});return h.push([a,o,NaN,NaN]),{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...f,"Residuals"],table:h,warnings:l}}var Gn=[0,0];var _s=e=>e.map(t=>t.map(n=>n[0]));function on(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Gn);return r.forEach((a,l)=>{let i=t[l];for(let s=0;s<n;s++)o[s]=B(o[s],oe(a,i[s]))}),o})}function vt(e){let t=Gn;return e.forEach((n,r)=>t=B(t,n[r])),t}function Vs(e){let t=e.length,n=e.map(l=>l.slice()),r=Array.from({length:t},(l,i)=>i),o=1,a=!1;for(let l=0;l<t;l++){let i=l;for(let s=l+1;s<t;s++)Math.abs(n[s][l][0])>Math.abs(n[i][l][0])&&(i=s);if(n[i][l][0]===0){a=!0;continue}i!==l&&([n[l],n[i]]=[n[i],n[l]],[r[l],r[i]]=[r[i],r[l]],o=-o);for(let s=l+1;s<t;s++){let c=be(n[s][l],n[l][l]);n[s][l]=c;for(let f=l+1;f<t;f++)n[s][f]=le(n[s][f],oe(c,n[l][f]))}}return{a:n,perm:r,sign:o,singular:a}}function Cs(e){let{a:t,sign:n,singular:r}=Vs(e);if(r)return Gn;let o=[n,0];return t.forEach((a,l)=>o=oe(o,a[l])),o}function Ds(e){let t=e.length,{a:n,perm:r,singular:o}=Vs(e);if(o)throw new Error("singular matrix");let a=Array.from({length:t},()=>new Array(t).fill(Gn));for(let l=0;l<t;l++){let i=r.map(s=>[s===l?1:0,0]);for(let s=0;s<t;s++)for(let c=0;c<s;c++)i[s]=le(i[s],oe(n[s][c],i[c]));for(let s=t-1;s>=0;s--){for(let c=s+1;c<t;c++)i[s]=le(i[s],oe(n[s][c],i[c]));i[s]=be(i[s],n[s][s])}for(let s=0;s<t;s++)a[s][l]=i[s]}return a}var Ps=(e,t)=>he(e,t/2,2,!1,!1);function Ur(e){let t=e.length,n=Bt(vt(e),t),r=[1,0];for(let o=0;o<t;o++)r=oe(r,n);return n=be(Cs(e),r),n[0]}function Hr(e,t,n,r){let o=1-(2*t*t+t+2)/(6*t*n),a=(t+2)*(t-1)*(t-2)*(2*t**3+6*t*t+3*r+2)/(288*(n*t*o)**2),l=-n*o*Math.log(e),i=t*(t+1)/2-1,s=Ps(l,i),c=Ps(l,i+4);return s+a*(c-s)}var ol=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function sl(e,t,n){let r=e.length,o=v=>v.length===0?0:pe(Array.from({length:r},(w,b)=>v.map(N=>N[b]))).rank,a=n.length===0?void 0:pe(Array.from({length:r},(v,w)=>n.map(b=>b[w]))),l=a===void 0?[]:a.pivot.slice(0,a.rank).map(v=>n[v]),i=l.length,s=[...l,...t],c=pe(Array.from({length:r},(v,w)=>s.map(b=>b[w])));if(c.rank>o(t))throw new Error("X does not define a subspace of M");let f=c.pivot.slice(0,c.rank).map(v=>s[v]),m=v=>v.map(w=>[w,0]),u=new Array(r).fill([0,0]),h=We(f.map(m),u,e.map((v,w)=>e.map(b=>b[w]))).qtv,p=Array.from({length:r},(v,w)=>h.map(b=>b[w])),d=We(f.map(m),u,p).qtv,g=Array.from({length:c.rank-i},(v,w)=>i+w);return g.map(v=>g.map(w=>d[v][w]))}function al(e,t={}){let n=e.map(w=>Array.from(w)),r=n.length,o=n[0]?.length??0;if(o===0||n.some(w=>w.length!==o))throw new TypeError("y must be a matrix with at least one column");let a=t.group;if(a!==void 0&&a.length!==r)throw new RangeError("'group' must have one label per row of y");let l=a?.map(ge)??n.map(()=>""),i=[...new Set(l)].sort(ft),s=n.map(w=>w.map(()=>[0,0]));for(let w of i){let b=l.flatMap((N,M)=>N===w?[M]:[]);for(let N=0;N<o;N++){let M=[0,0];for(let y of b)M=B(M,[n[y][N],0]);let E=Bt(M,b.length);for(let y of b)s[y][N]=le([n[y][N],0],E)}}let c=r-i.length,f=Array.from({length:o},(w,b)=>Array.from({length:o},(N,M)=>{let E=[0,0];for(let y of s)E=B(E,oe(y[b],y[M]));return E})),m=t.idata??{index:Array.from({length:o},(w,b)=>b+1)},u=w=>{let b=St(w,m);if(b.x.length!==o)throw new RangeError("idata must have one row per column of y");return ol(b.x,b.colNames.length)},h=t.M===void 0?Array.from({length:o},(w,b)=>Array.from({length:o},(N,M)=>M===b?1:0)):u(t.M),p=u(t.X??"~0"),d=sl(f,h,p),g=d.length,v=Ur(d);return{statistic:v,statisticName:"W",pValue:Hr(v,g,c,o),method:"Mauchly's test of sphericity"}}var $s=Number.EPSILON**.25;function ie(e,t){if(e===void 0)return t[0];let n=t.find(o=>o===e);if(n!==void 0)return n;let r=t.filter(o=>o.startsWith(e));if(e.length===0||r.length!==1)throw new RangeError(`'arg' should be one of ${t.map(o=>`\u201C${o}\u201D`).join(", ")}`);return r[0]}var j=e=>e==null;function Le(e,t){if(e.filter(j).length!==1)throw new RangeError(t)}function Z(e,t,n,r={}){return mt(e,t,n,r).root}var bt=(e,t,n,r)=>bn(e,t,{ncp:n,lowerTail:r});function Br(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Xr="'sig.level' must be numeric in [0, 1]";function il(e={}){let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,a=e.sigLevel===void 0?.05:e.sigLevel;Le([t,n,o,r,a],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Br(a,Xr);let l=ie(e.type,["two.sample","one.sample","paired"]),i=ie(e.alternative,["two.sided","one.sided"]),s=e.strict??!1,c=e.tol??$s,f=l==="two.sample"?2:1,m=i==="two.sided"?2:1;m===2&&!j(n)&&(n=Math.abs(n));let u=(d,g,v,w)=>{let b=Math.max(1e-7,d-1)*f,N=ct(w/m,b,{lowerTail:!1}),M=Math.sqrt(d/f)*g/v,E=bt(N,b,M,!1);return s&&m===2?E+bt(-N,b,M,!0):E};if(j(r))r=u(t,n,o,a);else{let d=r;j(t)?t=Z(g=>u(g,n,o,a)-d,2,1e7,{tol:c,extendInt:"upX"}):j(o)?o=Z(g=>u(t,n,g,a)-d,n*1e-7,n*1e7,{tol:c,extendInt:"downX"}):j(n)?n=Z(g=>u(t,g,o,a)-d,o*1e-7,o*1e7,{tol:c,extendInt:"upX"}):a=Z(g=>u(t,n,o,g)-d,1e-10,1-1e-10,{tol:c,extendInt:"yes"})}let h=l==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":l==="two.sample"?"n is number in *each* group":void 0,p={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[l]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:a,power:r,alternative:i,...h===void 0?{}:{note:h},method:p}}function ll(e={}){let{n:t,p1:n,p2:r,power:o}=e,a=e.sigLevel===void 0?.05:e.sigLevel;Le([t,n,r,o,a],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Br(a,Xr);let l=ie(e.alternative,["two.sided","one.sided"]),i=e.strict??!1,s=e.tol??$s,c=l==="two.sided"?2:1,f=(u,h,p,d)=>{let g=X(d/c,{lowerTail:!1});if(i&&c===2){let v=(h+p)/2,w=h*(1-h),b=p*(1-p),N=v*(1-v),M=Math.abs(h-p),E=Math.sqrt(w+b);return $((Math.sqrt(u)*M-g*Math.sqrt(2*N))/E)+$((Math.sqrt(u)*M+g*Math.sqrt(2*N))/E,{lowerTail:!1})}return $((Math.sqrt(u)*Math.abs(h-p)-g*Math.sqrt((h+p)*(1-(h+p)/2)))/Math.sqrt(h*(1-h)+p*(1-p)))},m=[];if(j(o))o=f(t,n,r,a);else{let u=o;j(t)?t=Z(h=>f(h,n,r,a)-u,1,1e7,{tol:s,extendInt:"upX"}):j(n)?(n=Z(h=>f(t,h,r,a)-u,0,r,{tol:s,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")):j(r)?(r=Z(h=>f(t,n,h,a)-u,n,1,{tol:s,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")):(a=Z(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:s,extendInt:"upX"}),(a<0||a>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power"))}return{n:t,p1:n,p2:r,sigLevel:a,power:o,alternative:l,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m}}function cl(e={}){let{groups:t,n,betweenVar:r,withinVar:o,power:a}=e,l=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,r,o,a,l],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!j(t)&&t<2)throw new RangeError("number of groups must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in each group must be at least 2");Br(l,Xr);let i=(s,c,f,m,u)=>{let h=(s-1)*c*f/m,p=s-1,d=(c-1)*s;return it(Ze(u,p,d,{lowerTail:!1}),p,d,{ncp:h,lowerTail:!1})};if(j(a))a=i(t,n,r,o,l);else{let s=a;j(t)?t=Z(c=>i(c,n,r,o,l)-s,2,100):j(n)?n=Z(c=>i(t,c,r,o,l)-s,2,1e5):j(o)?o=Z(c=>i(t,n,r,c,l)-s,r*1e-7,r*1e7):j(r)?r=Z(c=>i(t,n,c,o,l)-s,o*1e-7,o*1e7):l=Z(c=>i(t,n,r,o,c)-s,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:l,power:a,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation"}}var fl={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 js(e="p",t="small"){let n=ie(e,["p","t","r","anov","chisq","f2"]),r=ie(t,["small","medium","large"]);return{test:n,size:r,effectSize:fl[n][["small","medium","large"].indexOf(r)],method:"Conventional effect size from Cohen (1982)"}}function ml(e,t){return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function ul(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");let n=0;for(let r=0;r<e.length;r++)n+=(t[r]-e[r])**2/e[r];return Math.sqrt(n)}function hl(e){let t=e.map(a=>a.reduce((l,i)=>l+i,0)),n=e[0]?.length??0,r=Array.from({length:n},(a,l)=>e.reduce((i,s)=>i+s[l],0)),o=0;for(let a=0;a<n;a++)for(let l=0;l<e.length;l++){let i=t[l]*r[a];o+=(e[l][a]-i)**2/i}return Math.sqrt(o)}var pl="\u2018sig.level\u2019 must be numeric in [0, 1]",dl="\u2018power\u2019 must be numeric in [0, 1]";function Je(e,t){let n=r=>!j(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(pl);if(n(t))throw new RangeError(dl)}function et(e,t){return typeof e=="string"?js(t,e).effectSize:e}var sn=["two.sided","less","greater"];function Un(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function zs(e,t,n,r){if(r==="less")return bt(ct(n,e),e,t,!0);if(r==="greater")return bt(ct(n,e,{lowerTail:!1}),e,t,!1);let o=ct(n/2,e,{lowerTail:!1});return bt(o,e,t,!1)+bt(-o,e,t,!0)}function Ws(e,t,n){return n==="greater"?$(X(1-t)-e,{lowerTail:!1}):n==="less"?$(X(t)-e):$(X(1-t/2)-e,{lowerTail:!1})+$(X(t/2)-e)}function gl(e={}){let{n:t,power:n}=e,r=et(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;Le([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),Je(o,n);let a=ie(e.type,["two.sample","one.sample","paired"]),l=ie(e.alternative,sn),i=a==="two.sample"?2:1;l==="two.sided"&&!j(r)&&(r=Math.abs(r));let s=(m,u,h)=>zs((m-1)*i,Math.sqrt(m/i)*u,h,l);if(j(n))n=s(t,r,o);else{let m=n;j(t)?t=Z(u=>s(u,r,o)-m,2+1e-10,1e9):j(r)?r=Z(u=>s(t,u,o)-m,...Un(l,[1e-7,10])):o=Z(u=>s(t,r,u)-m,1e-10,1-1e-10)}let c=a==="paired"?"n is number of *pairs*":a==="two.sample"?"n is number in *each* group":void 0,f={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[a]+" t test power calculation";return{n:t,d:r,sigLevel:o,power:n,alternative:l,...c===void 0?{}:{note:c},method:f}}function wl(e={}){let{n1:t,n2:n,power:r}=e,o=et(e.d,"t"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,o,r,a],"exactly one of n1, n2, d, power, and sig.level must be NULL"),Je(a,r),!j(t)&&t<2)throw new RangeError("number of observations in the first group must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in the second group must be at least 2");let l=ie(e.alternative,sn);l==="two.sided"&&!j(o)&&(o=Math.abs(o));let i=(s,c,f,m)=>zs(s+c-2,f/Math.sqrt(1/s+1/c),m,l);if(j(r))r=i(t,n,o,a);else{let s=r;j(t)?t=Z(c=>i(c,n,o,a)-s,2+1e-10,1e9):j(n)?n=Z(c=>i(t,c,o,a)-s,2+1e-10,1e9):j(o)?o=Z(c=>i(t,n,c,a)-s,...Un(l,[1e-7,10])):a=Z(c=>i(t,n,o,c)-s,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:a,power:r,alternative:l,method:"t test power calculation"}}function Qr(e,t,n,r,o,a){let{n:l,power:i}=t,s=et(e,n==="h"?"p":"t"),c=t.sigLevel===void 0?.05:t.sigLevel;if(Le([s,l,i,c],`exactly one of ${n}, n, power, and sig.level must be NULL`),Je(c,i),!j(l)&&l<1)throw new RangeError("number of observations in each group must be at least 1");let f=ie(t.alternative,sn);f==="two.sided"&&!j(s)&&(s=Math.abs(s));let m=(u,h,p)=>Ws(u*r(h),p,f);if(j(i))i=m(s,l,c);else{let u=i;j(s)?s=Z(h=>m(h,l,c)-u,...Un(f,[a,10])):j(l)?l=Z(h=>m(s,h,c)-u,o,1e9):c=Z(h=>m(s,l,h)-u,1e-10,1-1e-10)}return{es:s,n:l,sigLevel:c,power:i,alternative:f}}function vl(e={}){let t=Qr(e.h,e,"h",n=>Math.sqrt(n/2),2.0000000001,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"}}function bl(e={}){let t=Qr(e.h,e,"h",n=>Math.sqrt(n),2.0000000001,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)"}}function Nl(e={}){let t=Qr(e.d,e,"d",n=>Math.sqrt(n),1.0000000001,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"}}function yl(e={}){let{n1:t,n2:n,power:r}=e,o=et(e.h,"p"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([o,t,n,r,a],"exactly one of h, n1, n2, power, and sig.level must be NULL"),Je(a,r),!j(t)&&t<2)throw new RangeError("number of observations in the first group must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in the second group must be at least 2");let l=ie(e.alternative,sn);l==="two.sided"&&!j(o)&&(o=Math.abs(o));let i=(s,c,f,m)=>Ws(s*Math.sqrt(c*f/(c+f)),m,l);if(j(r))r=i(o,t,n,a);else{let s=r;j(o)?o=Z(c=>i(c,t,n,a)-s,...Un(l,[1e-10,10])):j(t)?t=Z(c=>i(o,c,n,a)-s,2+1e-10,1e9):j(n)?n=Z(c=>i(o,t,c,a)-s,2+1e-10,1e9):a=Z(c=>i(o,t,n,c)-s,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:a,power:r,alternative:l,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes"}}function Ml(e={}){let{n:t,power:n}=e,r=et(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),Je(o,n),!j(t)&&t<4)throw new RangeError("number of observations must be at least 4");let a=ie(e.alternative,sn);a==="two.sided"&&!j(r)&&(r=Math.abs(r));let l=(s,c,f,m)=>{let u=ct(m?f/2:f,s-2,{lowerTail:!1}),h=Math.sqrt(u**2/(u**2+s-2)),p=Math.atanh(c)+c/(2*(s-1)),d=Math.atanh(h),g=$((p-d)*Math.sqrt(s-3));return m?g+$((-p-d)*Math.sqrt(s-3)):g},i=(s,c,f)=>a==="two.sided"?l(s,c,f,!0):l(s,a==="less"?-c:c,f,!1);if(j(n))n=i(t,r,o),a==="less"&&(r=-r);else{let s=n;if(j(t))t=Z(c=>i(c,r,o)-s,4+1e-10,1e9);else if(j(r)){let[c,f]=a==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=Z(m=>i(t,m,o)-s,c,f)}else o=Z(c=>i(t,r,c)-s,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:a,method:"approximate correlation power calculation (arctangh transformation)"}}function xl(e){let{N:t,power:n}=e,{df:r}=e,o=et(e.w,"chisq"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([o,t,n,a],"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(Je(a,n),!j(o)&&o<0)throw new RangeError("w must be positive");if(!j(t)&&t<1)throw new RangeError("number of observations must be at least 1");let l=(i,s,c)=>je(Ro(c,r,{lowerTail:!1}),r,{ncp:s*i**2,lowerTail:!1});if(j(n))n=l(o,t,a);else{let i=n;j(o)?o=Z(s=>l(s,t,a)-i,1e-10,1e9):j(t)?t=Z(s=>l(o,s,a)-i,1+1e-10,1e9):a=Z(s=>l(o,t,s)-i,1e-10,1-1e-10)}return{w:o,N:t,df:r,sigLevel:a,power:n,method:"Chi squared power calculation",note:"N is the number of observations"}}function El(e={}){let{k:t,n,power:r}=e,o=et(e.f,"anov"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,o,r,a],"exactly one of k, n, f, power, and sig.level must be NULL"),Je(a,r),!j(o)&&o<0)throw new RangeError("f must be positive");if(!j(t)&&t<2)throw new RangeError("number of groups must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in each group must be at least 2");let l=(i,s,c,f)=>{let m=i-1,u=(s-1)*i;return it(Ze(f,m,u,{lowerTail:!1}),m,u,{ncp:i*s*c**2,lowerTail:!1})};if(j(r))r=l(t,n,o,a);else{let i=r;j(t)?t=Z(s=>l(s,n,o,a)-i,2+1e-10,100):j(n)?n=Z(s=>l(t,s,o,a)-i,2+1e-10,1e9):j(o)?o=Z(s=>l(t,n,s,a)-i,1e-7,1e7):a=Z(s=>l(t,n,o,s)-i,1e-10,1-1e-10)}return{k:t,n,f:o,sigLevel:a,power:r,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation"}}function ql(e={}){let{u:t,v:n,power:r}=e,o=et(e.f2,"f2"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,o,r,a],"exactly one of u, v, f2, power, and sig.level must be NULL"),Je(a,r),!j(o)&&o<0)throw new RangeError("f2 must be positive");if(!j(t)&&t<1)throw new RangeError("degree of freedom u for numerator must be at least 1");if(!j(n)&&n<1)throw new RangeError("degree of freedom v for denominator must be at least 1");let l=(i,s,c,f)=>it(Ze(f,i,s,{lowerTail:!1}),i,s,{ncp:c*(i+s+1),lowerTail:!1});if(j(r))r=l(t,n,o,a);else{let i=r;j(t)?t=Z(s=>l(s,n,o,a)-i,1+1e-10,100):j(n)?n=Z(s=>l(t,s,o,a)-i,1+1e-10,1e9):j(o)?o=Z(s=>l(t,n,s,a)-i,1e-7,1e7):a=Z(s=>l(t,n,o,s)-i,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:a,power:r,method:"Multiple regression power calculation"}}var Al="Missing values detected. NAs dropped.",Bs={tol:0,maxiter:5e3};function Rl(e){return Math.exp(sr(e/2)-Math.log(Math.sqrt(e/2))-sr((e-1)/2))}function Gs(e,t,n){return mt(r=>bn(e,t,{ncp:r})-n,e-1,e+1,{...Bs,extendInt:"downX"}).root}function Xs(e,t,n,r){let o=n.pooledSd??!0,a=n.mu??0,l=n.paired??!1,i=n.ci===void 0?.95:n.ci,s=ie(n.alternative,["two.sided","less","greater"]),c=Array.from(e),f=t===void 0?void 0:Array.from(t),m=[];(c.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(Al);let u,h,p,d,g;if(f===void 0||l){let x;if(f===void 0)x=qe(c);else{let[A,S]=Rt(c,f);x=A.map((L,R)=>L-S[R])}let q=x.length;u=se(x),h=Math.sqrt(re(x)),p=h/Math.sqrt(q),d=1/q,g=q-1}else{let x=qe(c),q=qe(f),A=x.length,S=q.length;u=se(x)-se(q);let L=Math.sqrt(re(x)),R=Math.sqrt(re(q));o?(h=Math.sqrt(((A-1)*L**2+(S-1)*R**2)/(A+S-2)),p=h*Math.sqrt(1/A+1/S),d=1/A+1/S,g=A+S-2):(h=Math.sqrt((L**2+R**2)/2),d=2*(S*L**2+A*R**2)/(A*S*(L**2+R**2)),p=Math.sqrt(L**2/A+R**2/S),g=p**4/((L**2/A)**2/(A-1)+(R**2/S)**2/(S-1)))}let v=r?Rl(g):1,w=(u-a)/h*v,b=r?"Hedges_g":"Cohens_d";if(i===null)return{name:b,value:w,warnings:m};let N=s==="two.sided"?i:2*i-1,M=(u-a)/p,E=Gs(M,g,(1+N)/2)*Math.sqrt(d)*v,y=Gs(M,g,(1-N)/2)*Math.sqrt(d)*v;return s==="less"&&(E=-1/0),s==="greater"&&(y=1/0),{name:b,value:w,ci:i,ciLow:E,ciHigh:y,warnings:m}}function kl(e,t,n={}){return Xs(e,t,n,n.adjust??!1)}function Tl(e,t,n={}){return Xs(e,t,n,!0)}var Sl=e=>Array.isArray(e.effects),Qs=e=>e.multistratum===!0,Ll=e=>Array.isArray(e.strata),Ks=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Il=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),Kr=e=>new RegExp(`\\b${Il(e)}\\b`);function Fl(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let a=o.stratum.replace(/^Error: /,""),l=o.colNames.indexOf("Df"),i=o.colNames.indexOf("Sum Sq"),s=o.rowNames.map((f,m)=>({name:f,df:o.table[m][l],ss:o.table[m][i]})),c=s.find(f=>f.name==="Residuals");if(c===void 0)throw new Error(`the error stratum '${a}' has no residual degrees of freedom`);return{name:a,terms:s.filter(f=>f!==c),sse:c.ss,dfe:c.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 Ol(e){let t=e.univariate,n=t.colNames.indexOf("Sum Sq"),r=[],o=(s,c,f)=>{let m=r.find(u=>u.name===s);return m===void 0&&r.push(m={name:s,terms:[],sse:c,dfe:f}),m},a=new Map;if(t.colNames.includes("Error SS")){let s=t.colNames.indexOf("num Df"),c=t.colNames.indexOf("Error SS"),f=t.colNames.indexOf("den Df");t.rowNames.forEach((m,u)=>{let h=t.table[u],p=e.within.filter(v=>Kr(v).test(m)),d=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[c],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[s],ss:h[n]};d.terms.push(g),a.set(m,g)})}else{let s=t.colNames.indexOf("Df"),c=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[c][n],t.table[c][s]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][s],ss:t.table[u][n]};f.terms.push(h),a.set(m,h)})}let l=r.find(s=>s.name==="Subject");if(l===void 0)throw new Error("the result has no subject stratum");let i=e.anova.rowNames.map(s=>{let c=a.get(s);if(c===void 0)throw new Error(`there is no univariate test of '${s}'`);return c});return{strata:r,subject:l,order:i}}function Zs(e,t){let n=Array.isArray(e)?e:[],r=n.filter(a=>!t.some(l=>Kr(a).test(l.name)));if(r.length>0)throw new Error(`Observed variable not in data: ${r.join(", ")}`);let o=a=>n.some(l=>Kr(l).test(a));return{isObserved:o,ssObserved:t.reduce((a,l)=>o(l.name)?a+l.ss:a,0)}}function Us(e,t,n,r){let o=a=>it(e,t,n,{ncp:a})-r;return o(0)>0?e===1/0?1/0:mt(o,0,Math.max(10,e*t*4),{...Bs,extendInt:"downX"}).root:0}function Ys(e,t,n,r,o){let a=o==="two.sided"?r:2*r-1,l=Math.max(0,e),i=l/t/((1-l)/n),s=Us(i,t,n,(1+a)/2),c=Us(i,t,n,(1-a)/2);i<=Ze((1-a)/2,t,n)&&(c=0),i<=Ze((1+a)/2,t,n)&&(s=0);let f=s===1/0?1:s/(s+n),m=c===1/0?1:c/(c+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}var Hs="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Zr(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Hs);if(Qs(e)||Ll(e)||Ks(e))return _l(e,t,n);let r=Sl(e)?zr(e):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Hs);let o=n.ci===void 0?.95:n.ci,a=ie(n.alternative??"greater",["two.sided","less","greater"]),l=r.colNames.indexOf("Df"),i=r.colNames.indexOf("Sum Sq");if(l<0||i<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let s=r.rowNames.map((y,x)=>({name:y,df:r.table[x][l],ss:r.table[x][i]})).filter(y=>y.name!=="(Intercept)"),c=s.find(y=>y.name==="Residuals");if(c===void 0)throw new TypeError("the table has no row 'Residuals'");let f=s.filter(y=>y!==c),m=c.df,u=c.ss,h=u/m,p=s.reduce((y,x)=>y+x.ss,0),d=s.reduce((y,x)=>y+x.df,0)+1,g=t==="Eta2"?n.generalized??!1:!1,w=f.length===1?"plain":g!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:b,ssObserved:N}=Zs(g,f),M=y=>{let{ss:x,df:q}=y;if(t==="Eta2")return w==="partial"?x/(x+u):w==="plain"?x/p:x/(x+u+N-(b(y.name)?x:0));if(t==="Omega2"){let S=w==="partial"?(x-q*h)/(x+(d-q)*h):(x-q*h)/(p+h);return Math.max(0,S)}let A=w==="partial"?(x-q*h)/(x+u):(x-q*h)/p;return Math.max(0,A)},E=`${t}${w==="plain"?"":`_${w}`}`;return{colNames:o===null?[E]:[E,"CI","CI_low","CI_high"],rowNames:f.map(y=>y.name),table:f.map(y=>o===null?[M(y)]:Ys(M(y),y.df,m,o,a)),warnings:[]}}function _l(e,t,n){let r=n.ci===void 0?.95:n.ci,o=ie(n.alternative??"greater",["two.sided","less","greater"]),a=Ks(e),l,i,s;if(a){let N=Ol(e);({strata:l,subject:i}=N),s=N.order.map(M=>({term:M,stratum:N.strata.find(E=>E.terms.includes(M))}))}else({strata:l,subject:i}=Fl(Qs(e)?Wr(e):e)),s=l.flatMap(N=>N.terms.map(M=>({term:M,stratum:N})));let c=l.flatMap(N=>N.terms),f=l.reduce((N,M)=>N+M.sse,0),m=c.reduce((N,M)=>N+M.ss,0)+f,u=i.sse/i.dfe,h=t==="Eta2"?n.generalized??!1:!1,p=h!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:g}=Zs(h,c),v=(N,M)=>{let{ss:E,df:y}=N,x=M.sse/M.dfe;if(t==="Eta2")return p==="partial"?E/(E+M.sse):p==="plain"?E/m:E/(E+f+g-(d(N.name)?E:0));if(t==="Omega2"){let A=M===i?0:1,S=p==="partial"?(E-y*x)/(E+A*M.sse+i.sse+u):(E-y*x)/(m+u);return Math.max(0,S)}let q=p==="partial"?(E-y*x)/(E+M.sse):(E-y*x)/m;return Math.max(0,q)},w=`${t}${p==="plain"?"":`_${p}`}`,b={colNames:r===null?[w]:[w,"CI","CI_low","CI_high"],rowNames:s.map(N=>N.term.name),table:s.map(({term:N,stratum:M})=>{let E=v(N,M);return r===null?[E]:Ys(E,N.df,M.dfe,r,o)}),warnings:[]};return a||(b.groups=s.map(N=>N.stratum.name)),b}function Vl(e,t={}){return Zr(e,"Eta2",t)}function Cl(e,t={}){return Zr(e,"Omega2",t)}function Dl(e,t={}){return Zr(e,"Epsilon2",t)}var Hn=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Pl=Math.sqrt(2220446049250313e-31);function Bn(e,t){let n=Object.prototype.hasOwnProperty.call(e,t)?e[t]:void 0;if(n===void 0)throw new Error(`object '${t}' not found`);return Array.from(n)}var an=e=>typeof e=="boolean"?e?"TRUE":"FALSE":ge(e);function $l(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(an):[...new Set(t.map(an))].sort(ft)}var Js=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function ea(e,t,n,r){let o=s=>r.some(c=>new RegExp(`\\b${Js(c)}\\b`).test(s));for(let s of r)if(!e.some(c=>new RegExp(`\\b${Js(s)}\\b`).test(c.name)))throw new Error(`Observed variable not in data: ${s}`);let a=[...new Set(t)].reduce((s,c)=>s+c,0),l=e.reduce((s,c)=>o(c.name)?s+c.ss:s,0),i=["pes","ges"].filter(s=>n.includes(s));return{colNames:["num Df","den Df","MSE","F",...i,"Pr(>F)"],rowNames:e.map(s=>s.name),table:e.map(s=>{let c=i.map(f=>f==="pes"?s.ss/(s.ss+s.errSS):s.ss/(s.ss+a+l-(o(s.name)?s.ss:0)));return[s.numDf,s.denDf,s.errSS/s.denDf,s.f,...c,s.p]})}}function jl(e,t){let n=Hn(t.between),r=Hn(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");let o=t.type??3;if(o!==2&&o!==3)throw new RangeError(`invalid type (${String(o)}): use 2 or 3`);let a=t.correction??"GG",l=Hn(t.es??"ges"),i=Hn(t.observed),s=[],c=ce(Bn(e,t.id)).map(an),f=Bn(e,t.dv).map(T=>{if(typeof T!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);return T}),m=n.map(T=>ce(Bn(e,T))),u=r.map(T=>ce(Bn(e,T))),h=c.length;for(let T of[f,...m,...u])if(T.length!==h)throw new RangeError("the columns must have the same length");let p=m.map(T=>$l(T)),d=m.map(T=>T.map(an)),g=u.map(T=>T.map(an)),v=g.map(T=>[...new Set(T)]);p.forEach((T,F)=>{if(T.length<2)throw new Error(`Factor "${n[F]}" consists of one level only. Remove factor from model?`)});let w=[...new Set(c)],b=new Map,N=new Set;if(c.forEach((T,F)=>{let P=JSON.stringify(d.map(G=>G[F])),D=b.get(T);D===void 0?b.set(T,P):D!==P&&N.add(T)}),N.size>0)throw new Error(`Following ids are in more than one between subjects condition:
3
3
  ${w.filter(T=>N.has(T)).join(", ")}`);let M=[[]];for(let T of v)M=M.flatMap(F=>T.map(P=>[...F,P]));let E=T=>JSON.stringify(g.map(F=>F[T])),y=new Map(M.map((T,F)=>[JSON.stringify(T),F])),x=new Set(c.map((T,F)=>E(F)));if(M.some(T=>!x.has(JSON.stringify(T))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let q=M.length,A=new Map;c.forEach((T,F)=>{let P=A.get(T);P===void 0&&A.set(T,P={s:M.map(()=>[0,0]),n:M.map(()=>0)});let D=y.get(E(F));P.s[D]=B(P.s[D],[f[F],0]),P.n[D]++}),[...A.values()].some(T=>T.n.some(F=>F>1))&&s.push("More than one observation per design cell, aggregating data using `fun_aggregate = mean`.\nTo turn off this warning, pass `fun_aggregate = mean` explicitly.");let S=w.filter(T=>A.get(T).n.some(F=>F===0));if(S.length>0){let T=[...S].sort(ft),F=T.length>=10?`${T.slice(0,10).join(", ")}, ... [showing first 10 only]`:T.join(", ");s.push(`Missing values for ${S.length} ID(s), which were removed before analysis:
4
4
  ${F}`)}let L=w.filter(T=>!S.includes(T)),R=new Map;c.forEach((T,F)=>R.has(T)||R.set(T,F));let k=L.map(T=>{let F=A.get(T);return F.s.map((P,D)=>Bt(P,F.n[D]))}),C={};n.forEach((T,F)=>C[T]=L.map(P=>d[F][R.get(P)]));let O=Object.fromEntries(n.map(T=>[T,"contr.sum"])),I=Object.fromEntries(n.map((T,F)=>[T,p[F]])),V="Rank deficient model matrix; insufficient data to estimate full model.";if(q===1)return z();return Y();function z(){let T=n.includes("y")?"y_":"y",F={...C,[T]:k.map(ne=>ne[0][0])},P=Or(`${T} ~ ${n.length>0?n.join(" * "):"1"}`,F,{contrasts:O,levels:I});if(P.rank<P.coefNames.length)throw new Error(V);let D=Gr(P,{type:o});s.push(...D.warnings.filter(ne=>!s.includes(ne)));let G=P.dfResidual,Q=D.rowNames.flatMap((ne,ve)=>{if(ne==="(Intercept)"||ne==="Residuals")return[];let[ye,Oe,_e,Ie]=D.table[ve];return[{name:ne,ss:ye,numDf:Oe,errSS:P.rss,denDf:G,f:_e,p:Ie}]});return{anova:ea(Q,[P.rss],l,i),univariate:{colNames:D.colNames,rowNames:D.rowNames,table:D.table},within:r,warnings:s}}function Y(){let T=L.length,F=n.length>0?St(`~ ${n.join(" * ")}`,C,{contrasts:O,levels:I}):{colNames:["(Intercept)"],assign:[0],x:L.map(()=>[1]),termLabels:[],factors:[]},P=F.colNames.length;if(T===0||pe(F.x).rank<P)throw new Error(V);let D=T-P,G=[0,...F.termLabels.map((H,K)=>K+1)],Q=["(Intercept)",...F.termLabels],ne=H=>F.assign.flatMap((K,U)=>H.includes(K)?[U]:[]),ve=H=>F.factors.flatMap((K,U)=>K[H-1]>0?[U]:[]),ye=H=>F.x.map(K=>[K[H],0]),Oe=new Array(T).fill([0,0]),_e=(H,K,U)=>{let{qtv:de}=We([...H,...K].map(ye),Oe,U);return de.map(rt=>rt.slice(H.length,H.length+K.length))},Ie=H=>{let{qtv:K}=We(F.colNames.map((U,de)=>ye(de)),Oe,H);return K.map(U=>U.slice(P))},Ge=Object.fromEntries(r.map((H,K)=>[H,M.map(U=>U[K])])),Re=St(`~ ${r.join(" * ")}`,Ge,{contrasts:Object.fromEntries(r.map(H=>[H,"contr.sum"])),levels:Object.fromEntries(r.map((H,K)=>[H,v[K]]))}),Mt=[0,...Re.termLabels.map((H,K)=>K+1)],tt=["(Intercept)",...Re.termLabels],Pt=H=>H.map(K=>H.map(U=>{let de=[0,0];return K.forEach((rt,Gt)=>de=B(de,oe(rt,U[Gt]))),de})),nt=[],$t=[],jt=[],hn=[],zt=[],Wt=[],_=[],W=!1;for(let H of Mt){let K=Re.assign.flatMap((ke,fe)=>ke===H?[fe]:[]),U=K.length,de=K.map(ke=>k.map(fe=>fe.reduce((He,Be,No)=>B(He,Te(Be,Re.x[No][ke])),[0,0]))),rt=K.map(ke=>K.map(fe=>[Re.x.reduce((He,Be)=>He+Be[ke]*Be[fe],0),0])),Gt=Ds(rt),nr=Pt(Ie(de)),pn=on(nr,Gt),dn=vt(pn)[0],Ue=NaN,gn=NaN,rr=NaN,vo=NaN,bo=!1;if(U>=2&&ht(_s(nr),{symmetric:!0,onlyValues:!0}).values.filter(fe=>fe>=Pl).length===U){bo=!0;let fe=vt(pn);Ue=be(oe(fe,fe),Te(vt(on(pn,pn)),U))[0],gn=((D+1)*U*Ue-2)/(U*(D-U*Ue)),rr=Ur(on(Gt,nr)),vo=Hr(rr,U,D,q)}for(let ke=0;ke<G.length;ke++){let fe=G[ke],He;if(o===3)He=_e(ne(G.filter(Ke=>Ke!==fe)),ne([fe]),de);else if(fe===0)He=_e([],ne([0]),de);else{let Ke=ve(fe),wn=G.filter($e=>$e!==0&&$e!==fe&&Ke.every(Ra=>F.factors[Ra][$e-1]>0));He=_e(ne(G.filter($e=>$e!==fe&&!wn.includes($e))),ne([fe]),de)}let Be=vt(on(Pt(He),Gt))[0],Xe=(fe===0?1:ne([fe]).length)*U,Qe=D*U,Ut=Be/Xe/(dn/Qe),yo=ae(Ut,Xe,Qe,!1,!1),or=H===0?Q[ke]:fe===0?tt[H]:`${Q[ke]}:${tt[H]}`;nt.push([Be,Xe,dn,Qe,Ut,yo]),$t.push(or),_.push(dn);let Ht={name:or,ss:Be,numDf:Xe,errSS:dn,denDf:Qe,f:Ut,p:yo};if(bo){let Ke=Math.min(1,gn),wn=ae(Ut,Ue*Xe,Ue*Qe,!1,!1),$e=ae(Ut,Ke*Xe,Ke*Qe,!1,!1);jt.push([Ue,wn,gn,$e]),hn.push([rr,vo]),zt.push(or),a==="GG"?Ht={...Ht,numDf:Ue*Xe,denDf:Ue*Qe,p:wn}:a==="HF"&&(gn>1&&(W=!0),Ht={...Ht,numDf:Ke*Xe,denDf:Ke*Qe,p:$e})}H===0&&fe===0||Wt.push(Ht)}}W&&s.push("HF eps > 1 treated as 1");let J={anova:ea(Wt,_,l,i),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:$t,table:nt},within:r,warnings:s};return jt.length>0&&(J.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:zt,table:jt},J.sphericity={colNames:["Test statistic","p-value"],rowNames:[...zt],table:hn}),J}}var oa=Math.log(2*Math.PI),ta=2e-14,na=500;function zl(e){return Math.exp(-.5*e*e-.5*oa)}function Wl(e){return $(e,{logP:!0})}function sa(e,t=-1/0){return(t===-1/0?fr(e,0,2,0,ta,na):fr(e,t,1,0,ta,na)).result}function ra(e,t,n){let r=t.map(Math.sqrt),o=t.map(l=>Math.sqrt(1-l));return sa(l=>{let i=0;for(let s=0;s<r.length;s++){let c=(r[s]*l+e)/o[s];if(!n){i+=Wl(c);continue}let f=(r[s]*l-e)/o[s],m=$(f)+$(c,{lowerTail:!1});m<.5?i+=Math.log1p(-m):i+=Math.log(c+f<0?$(c)-$(f):$(f,{lowerTail:!1})-$(c,{lowerTail:!1}))}return zl(l)*-Math.expm1(i)})}function Nt(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,ra(e,t,r));let o=n/2,a=Math.log(2)+.5*Math.log(o)-.5*oa-Eo(o);return Math.min(1,sa(i=>{let s=a-o*(Math.expm1(2*i)-2*i);return s<-745?0:Math.exp(s)*ra(e*Math.exp(i),t,r)}))}var aa="Ties are present. z-quantiles were corrected for ties.";function De(e,t){let{labels:n,groups:r}=pt(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let a=r.map(l=>l.length);return{labels:n,groups:r,n:a,N:a.reduce((l,i)=>l+i,0),k:o}}function Yr(e){let t=e.groups.flat(),n=me(t),r=[],o=0;for(let l of e.n){let i=0;for(let s=0;s<l;s++)i+=n[o+s];o+=l,r.push(i/l)}let a=0;for(let l of Fe(t))a+=l*l*l-l;return{R:r,C:a/(12*(e.N-1))}}function ln(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 Gl(e,t){let n=e.length+1,r=[];for(let i=0;i<n-1;i++)for(let s=i+1;s<n;s++)r.push(e[s-1][i]);let o=It(r,t),a=0,l=e.map(i=>i.slice());for(let i=0;i<n-1;i++)for(let s=i+1;s<n;s++)l[s-1][i]=o[a++];return l}function Qn(e,t,n,r,o,a=[]){return{method:t,statistic:n,pValue:r,rowNames:e.labels.slice(1),colNames:e.labels.slice(0,-1),pAdjustMethod:o,warnings:a}}function Jr(e,t,n,r,o,a=[]){return{method:t,statistic:n.map(l=>[l]),pValue:r.map(l=>[l]),rowNames:e.labels.slice(1),colNames:e.labels.slice(0,1),pAdjustMethod:o,warnings:a}}var Xn=e=>$(e,{lowerTail:!1}),eo=(e,t,n)=>Nn(Math.abs(e),t,n,{lowerTail:!1});function to(e){let t=0,n=0;for(let r of e.groups)r.length<2||(t+=(r.length-1)*re(r),n+=r.length-1);return{m:e.groups.map(se),s2:t/n,df:e.N-e.k}}function Ul(e,t,n={}){let r=n.pAdjustMethod??"holm",o=De(e,t),{R:a,C:l}=Yr(o),i=o.N*(o.N+1)/12-l,s=ln(o.k,(f,m)=>Math.abs(a[f]-a[m])/Math.sqrt(i*(1/o.n[f]+1/o.n[m]))),c=Gl(s.map(f=>f.map(m=>2*Xn(m))),r);return Qn(o,"Dunn's all-pairs test",s,c,r,l!==0?[aa]:[])}function Hl(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=De(e,t),{R:l,C:i}=Yr(a),s=a.N*(a.N+1)/12-i,c=a.n.slice(1).map((h,p)=>p+1),f=c.map(h=>(l[h]-l[0])/Math.sqrt(s*(1/a.n[0]+1/a.n[h]))),m=c.map(h=>a.n[h]/(a.n[h]+a.n[0])),u=ia(f,m,r,o);return Jr(a,"Dunn's many-to-one test",f,u,o,i!==0?[aa]:[])}function ia(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?Nt(o,t,1/0,!0):Nt(n==="greater"?o:-o,t,1/0,!1)):It(e.map(o=>n==="two.sided"?2*Xn(Math.abs(o)):Xn(n==="greater"?o:-o)),r)}function Bl(e,t,n={}){let r=te(n.alternative),o=De(e,t),{m:a,s2:l,df:i}=to(o),s=Math.sqrt(l),c=o.n.slice(1).map((h,p)=>p+1),f=c.map(h=>(a[h]-a[0])/(s*Math.sqrt(1/o.n[h]+1/o.n[0]))),m=c.map(h=>o.n[h]/(o.n[h]+o.n[0])),u=f.map(h=>r==="two.sided"?Math.min(1,2*Nt(Math.abs(h),m,i,!1)):Nt(r==="greater"?h:-h,m,i,!1));return Jr(o,`Dunnett's-test for multiple
5
5
  comparisons with one control`,f,u,"single-step")}function Xl(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=De(e,t);if(o==="single-step"&&a.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least