@numero-uno-jpn/rstats 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.ja.md +14 -14
  3. package/README.md +13 -13
  4. package/THIRD_PARTY_NOTICES.md +2 -2
  5. package/dist/chunks/chunk-22SXZNR6.js +2 -0
  6. package/dist/chunks/chunk-2IAT6MT2.js +2 -0
  7. package/dist/chunks/chunk-3AKA4KEL.js +3 -0
  8. package/dist/chunks/{chunk-LEIH4U5X.js → chunk-4AQNVF3Z.js} +2 -2
  9. package/dist/chunks/chunk-4GNJEYXR.js +2 -0
  10. package/dist/chunks/chunk-AGRKZXLG.js +2 -0
  11. package/dist/chunks/{chunk-6BH65HGN.js → chunk-BJHBYEEU.js} +1 -1
  12. package/dist/chunks/chunk-BSOUJPDF.js +2 -0
  13. package/dist/chunks/{chunk-VQHEOBTM.js → chunk-CQNVEA22.js} +2 -2
  14. package/dist/chunks/{chunk-XJQKSR52.js → chunk-F44XUHWI.js} +1 -1
  15. package/dist/chunks/chunk-JGFUZKLJ.js +2 -0
  16. package/dist/chunks/{chunk-OX3SBG5X.js → chunk-MEMYFTYS.js} +1 -1
  17. package/dist/chunks/{chunk-EZ2KD5NA.js → chunk-N3FV6H3C.js} +2 -2
  18. package/dist/chunks/chunk-NRV3BH7I.js +2 -0
  19. package/dist/chunks/{chunk-UV2MZYDQ.js → chunk-RTWLZPPG.js} +2 -2
  20. package/dist/chunks/chunk-SLSDFIW2.js +2 -0
  21. package/dist/chunks/chunk-SVWU5T7C.js +2 -0
  22. package/dist/chunks/chunk-VG67L3JA.js +3 -0
  23. package/dist/chunks/chunk-VWGJYRE6.js +2 -0
  24. package/dist/data/grouping.js +11 -4
  25. package/dist/descriptive/basic.js +57 -49
  26. package/dist/descriptive/cov.js +7 -2
  27. package/dist/descriptive/factanal.d.ts +16 -0
  28. package/dist/descriptive/factanal.js +26 -0
  29. package/dist/descriptive/hist.d.ts +8 -2
  30. package/dist/descriptive/hist.js +10 -2
  31. package/dist/descriptive/quantile.d.ts +8 -3
  32. package/dist/descriptive/quantile.js +10 -6
  33. package/dist/descriptive/table.d.ts +7 -5
  34. package/dist/descriptive/table.js +39 -22
  35. package/dist/descriptive.js +2 -2
  36. package/dist/distributions/beta.js +17 -0
  37. package/dist/distributions/binomial.js +3 -0
  38. package/dist/distributions/gamma.js +7 -0
  39. package/dist/distributions.js +2 -2
  40. package/dist/entries/tests.d.ts +1 -1
  41. package/dist/htest/agreement.js +7 -2
  42. package/dist/htest/groups.js +7 -3
  43. package/dist/htest/mcnemarTest.js +6 -2
  44. package/dist/htest/pairwise.js +6 -3
  45. package/dist/htest/polycor.js +5 -3
  46. package/dist/htest/tTest.d.ts +3 -2
  47. package/dist/htest/tTest.js +6 -3
  48. package/dist/htest/trendTests.js +10 -5
  49. package/dist/internal/levels.d.ts +11 -0
  50. package/dist/internal/levels.js +48 -0
  51. package/dist/internal/summary.d.ts +19 -1
  52. package/dist/internal/summary.js +69 -8
  53. package/dist/linalg/balance.d.ts +21 -0
  54. package/dist/linalg/balance.js +191 -0
  55. package/dist/linalg/eigen.d.ts +1 -1
  56. package/dist/linalg/eigen.js +8 -2
  57. package/dist/linalg/expm.d.ts +1 -11
  58. package/dist/linalg/expm.js +2 -158
  59. package/dist/linalg/kappa.d.ts +6 -2
  60. package/dist/linalg/kappa.js +16 -7
  61. package/dist/linalg/lu.d.ts +13 -2
  62. package/dist/linalg/lu.js +17 -3
  63. package/dist/linalg/qr.d.ts +4 -1
  64. package/dist/linalg/qr.js +2 -2
  65. package/dist/linalg.js +2 -2
  66. package/dist/mixed.js +2 -2
  67. package/dist/models/aovEz.js +6 -4
  68. package/dist/models/coxph.js +5 -2
  69. package/dist/models/glm.d.ts +4 -1
  70. package/dist/models/glmFamily.d.ts +1 -1
  71. package/dist/models/glmFamily.js +24 -2
  72. package/dist/models/mauchly.js +2 -1
  73. package/dist/models/modelFrame.js +2 -1
  74. package/dist/models/survival.js +5 -1
  75. package/dist/numerics/integrate.d.ts +1 -1
  76. package/dist/numerics/integrate.js +13 -3
  77. package/dist/numerics/interp.d.ts +1 -1
  78. package/dist/numerics/interp.js +3 -0
  79. package/dist/numerics.js +2 -2
  80. package/dist/rstats.js +14 -14
  81. package/dist/special/beta.js +10 -20
  82. package/dist/special.js +2 -2
  83. package/dist/tests.js +12 -12
  84. package/package.json +1 -1
  85. package/dist/chunks/chunk-3VMBBQLV.js +0 -3
  86. package/dist/chunks/chunk-4OKAZJJG.js +0 -3
  87. package/dist/chunks/chunk-ATC4F4BX.js +0 -2
  88. package/dist/chunks/chunk-EDH53DXN.js +0 -2
  89. package/dist/chunks/chunk-GSPOO2G3.js +0 -2
  90. package/dist/chunks/chunk-K7AZ65H7.js +0 -2
  91. package/dist/chunks/chunk-SGZQDJRR.js +0 -2
  92. package/dist/chunks/chunk-UH2N3ZQW.js +0 -2
  93. package/dist/chunks/chunk-WJQZZYOI.js +0 -2
  94. package/dist/chunks/chunk-WKEFHOFR.js +0 -2
  95. package/dist/chunks/chunk-WXC25OPJ.js +0 -2
package/dist/tests.js CHANGED
@@ -1,17 +1,17 @@
1
- /*! rStats.js 0.12.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
- import{C as xt,D as at,H as Fo,I as _o,J as fr,N as Et,O as mr,T as qt,U as Vo,Y as Co,Z as kt,_ as Do,b as Lo,c as me,ca as Nn,d as lr,da as $o,ea as Kt,fa as Po,ha as yn,i as $e,ia as Mn,j as Io,l as Oo,m as Qt,n as rt,s as ae,t as ot,v as st,w as Qe,y as cr}from"./chunks/chunk-K7AZ65H7.js";import{a as An,e as be,i as mt}from"./chunks/chunk-ATC4F4BX.js";import{a as En,b as Ke,c as Jt,d as Go,e as Uo,f as hr,g as te,h as qn,i as Me,j as Qo,k as Da,m as Tn,n as en}from"./chunks/chunk-SGZQDJRR.js";import{k as we,l as Pe,n as bn,o as it}from"./chunks/chunk-EZ2KD5NA.js";import{b as Xo,c as ct,e as Sn}from"./chunks/chunk-3VMBBQLV.js";import{b as ur}from"./chunks/chunk-6BH65HGN.js";import"./chunks/chunk-XJQKSR52.js";import{a as Zt,b as se,c as re,d as jo,e as ee,f as fe,g as Ie,h as ke,i as At}from"./chunks/chunk-GSPOO2G3.js";import{a as Ho,d as Bo}from"./chunks/chunk-LEIH4U5X.js";import{b as Xt}from"./chunks/chunk-VQHEOBTM.js";import{f as ye,g as P,h as H,n as ir,p as To,s as So}from"./chunks/chunk-EDH53DXN.js";import{b as nt,c as Mt,d as B,e as Ao,f as le,g as oe,h as Te,i as ge,j as Bt,k as Ro}from"./chunks/chunk-OX3SBG5X.js";import{a as ft,b as St}from"./chunks/chunk-UV2MZYDQ.js";import{h as he,l as Ko,m as Tt,n as kn,v as Rn}from"./chunks/chunk-WJQZZYOI.js";import{a as Yt,b as Rt,c as xn,d as zo,i as Wo}from"./chunks/chunk-4OKAZJJG.js";import{a as lt}from"./chunks/chunk-WKEFHOFR.js";function Lt(e,t,n={}){if(typeof e=="string")return $a(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,i=n.paired??!1,l=n.varEqual??!1,s=n.confLevel??.95;if(Number.isNaN(a))throw new RangeError("mu must be a number");if(qn(s),i&&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(i){let[y,x]=At(c,f);c=y.map((k,q)=>k-x[q]),f=void 0}else c=ke(c),f!==void 0&&(f=ke(f));let m,u,h,p,d,g,b,w=c.length;if(w<2)throw new Error("not enough 'x' observations");let v=se(c),N=re(c);if(f===void 0){if(u=w-1,h=Math.sqrt(N/w),h<10*Number.EPSILON*Math.abs(v))throw new Error("data are essentially constant");m=(v-a)/h,g=i?"Paired t-test":"One Sample t-test",p=v,d=i?"mean difference":"mean of x",b=i?"mean difference":"mean"}else{let y=f.length;if(y<1||!l&&y<2)throw new Error("not enough 'y' observations");if(l&&w+y<3)throw new Error("not enough observations");let x=se(f),k=re(f);if(g=`${l?"":"Welch"} Two Sample t-test`,p=[v,x],d=["mean of x","mean of y"],b="difference in means",l){u=w+y-2;let q=0;w>1&&(q+=(w-1)*N),y>1&&(q+=(y-1)*k),q/=u,h=Math.sqrt(q*(1/w+1/y))}else{let q=N/w,S=k/y;h=Math.sqrt(q+S),u=(h*h)**2/(q*q/(w-1)+S*S/(y-1))}if(h<10*Number.EPSILON*Math.max(Math.abs(v),Math.abs(x)))throw new Error("data are essentially constant");m=(v-x-a)/h}let M,E;if(o==="less")M=we(m,u,!0,!1),E=[-1/0,m+Pe(s,u,!0,!1)];else if(o==="greater")M=we(m,u,!1,!1),E=[m-Pe(s,u,!0,!1),1/0];else{M=2*we(-Math.abs(m),u,!0,!1);let y=1-s,x=Pe(1-y/2,u,!0,!1);E=[m-x,m+x]}return E=[a+E[0]*h,a+E[1]*h],{method:g,alternative:o,statistic:m,statisticName:"t",parameter:u,parameterName:"df",pValue:M,confInt:E,confLevel:s,estimate:p,estimateNames:d,nullValue:a,nullValueName:b,stdErr:h}}function $a(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 _e(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 Pa(e,t,n){return Array.isArray(t)?ja(e,t,n??{}):za(e,t??{})}var Zo=(e,t)=>e<t?-1:e>t?1:0;function ja(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 i=[...new Set(o)].sort(Zo),l=[...new Set(a)].sort(Zo);if(i.length<2||l.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let s=new Map(i.map((u,h)=>[u,h])),c=new Map(l.map((u,h)=>[u,h])),f=i.map(()=>l.map(()=>0));return o.forEach((u,h)=>f[s.get(u)][c.get(a[h])]++),{...ns(f,n),rowLevels:i,colLevels:l}}function za(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?pr(r[0],t):o===1?pr(r.map(a=>a[0]),t):ns(r,t)}return pr(Array.from(e),t)}function Yo(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 Jo(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 es(e,t){return Number.isNaN(e)?NaN:me(e,t/2,2,!1,!1)}function ts(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 pr(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=_e(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=_e(o);o=o.map(u=>u/m)}else if(Math.abs(_e(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let a=o.map(m=>r*m);ts(a);let i=o.map((m,u)=>a[u]*(1-m)),l=_e(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=Yo(t);s+=Yt(m),c=NaN,f=Jo(Wa(r,o,a,m),l,m)}else f=es(l,c);return{method:s,statistic:l,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(i[u]))}}function Wa(e,t,n,r){let o=t.length,a=Po(o,{size:Math.trunc(r*e),replace:!0,prob:t}),i=Math.trunc(e);if(a.length===0)return new Float64Array(0);if(i===0)throw new RangeError("invalid 'nrow' value (too large or NA)");let l=Math.ceil(a.length/i),s=new Float64Array(l),c=new Float64Array(o);for(let f=0;f<l;f++){c.fill(0);for(let u=0;u<i;u++)c[a[(f*i+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 Ga(e,t,n,r){let o=e.length,a=t.length,i=e.reduce((f,m)=>f+m,0),l=yn(i),s=new Int32Array(o*a),c=new Float64Array(Math.trunc(r));for(let f=0;f<c.length;f++){Mn(e,t,l,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 ns(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 i=_e(a);if(!(i>0))throw new RangeError("at least one entry of 'x' must be positive");let l=e.map(w=>_e(w)),s=e[0].map((w,v)=>_e(e.map(N=>N[v]))),c=l.map(w=>s.map(v=>w*v/i));ts(c.flat());let f=e.map((w,v)=>w.map((N,M)=>N-c[v][M])),m="Pearson's Chi-squared test",u,h=(r-1)*(o-1),p,d=(t.simulatePValue??!1)&&l.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=Yo(t);m+=Yt(w),u=_e(f.flatMap((N,M)=>N.map((E,y)=>E*E/c[M][y]))),h=NaN;let v=Ga(l.map(N=>Math.trunc(N)),s.map(N=>Math.trunc(N)),c,w);p=Jo(v,u,w)}else g>0?(m+=" with Yates' continuity correction",u=_e(f.flatMap((w,v)=>w.map((N,M)=>(Math.abs(N)-g)**2/c[v][M])))):u=_e(f.flatMap((w,v)=>w.map((N,M)=>N*N/c[v][M])));let b=c.map((w,v)=>w.map((N,M)=>N*(1-l[v]/i)*(1-s[M]/i)));return{method:m,statistic:u,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p??es(u,h),observed:e,expected:c,residuals:f.map((w,v)=>w.map((N,M)=>N/Math.sqrt(c[v][M]))),stdres:f.map((w,v)=>w.map((N,M)=>N/Math.sqrt(b[v][M])))}}var Ua=Math.pow(Number.EPSILON,.25);function xe(e,t,n,r={}){let o={tol:r.tol??Ua,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),ct(e,t,n,o).root}function Ha(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(Xt)),i="Fisher's Exact Test for Count Data";if(n===2&&r===2)return Ka(a,t,i);let l=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:i+Yt(s),alternative:l,pValue:Ba(a,s)}}return{method:i,alternative:l,pValue:Za(a)}}function Ba(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,b)=>g+b,0)),i=o[0].map((d,g)=>o.reduce((b,w)=>b+w[g],0)),l=a.length,s=i.length,c=[0,0];for(let d of o.flat())c=B(c,[-Bo(d),0]);let f=c[0]/(1+64*Number.EPSILON),m=yn(a.reduce((d,g)=>d+g,0)),u=new Int32Array(l*s),h=0,p=Math.trunc(t);for(let d=0;d<p;d++){Mn(a,i,m,u);let g=0;for(let b=0;b<u.length;b++)g-=m[u[b]];g<=f&&h++}return Math.max(0,Math.min(1,(1+h)/(t+1)))}var Xa=1+1e-7,Qa=16;function dr(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 wr=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,a=1;for(let i=this.lo;i<=this.hi;i++){let l=i-this.lo;l%Qa===0||!(a>0&&a<1/0)?(o=Et(i,t,n,r,!0),a=1,this.logCentral[l]=o):this.logCentral[l]=o+Math.log(a),a*=(t-i)*(r-i)/((i+1)*(n-r+i+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),a=-1/0;for(let l=0;l<n;l++){let s=this.logCentral[l]+(this.lo+l)*r;o[l]=s,s>a&&(a=s)}for(let l=0;l<n;l++){let s=o[l]-a;o[l]=s<-746?0:Math.exp(s)}let i=dr(o,0,n-1);for(let l=0;l<n;l++)o[l]!==0&&(o[l]=o[l]/i);return o}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)n[o]!==0&&(r=B(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?mr(t-1,this.m,this.n,this.k,!1,!1):mr(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return(r?t<=i:t>=i)?1:0}let o=this.density(n),a=t-this.lo;return r?dr(o,a,o.length-1):dr(o,0,a)}};function gr(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function Ka(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[[i,l],[s,c]]=e,f=i,m=new wr(i+s,l+c,i+l),{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]*Xa,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:gr(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 b=N=>{if(f===h)return 1/0;let M=m.tail(f,1,!1);return M===N?1:gr(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:gr(E=>m.tail(f,E,!0)-N,M>N)},v;if(r==="less")v=[0,b(1-a)];else if(r==="greater")v=[w(1-a),1/0];else{let N=(1-a)/2;v=[w(N),b(N)]}return{...g,confInt:v,confLevel:a}}var Ln=[[0,0]];function Ve(e){for(let t=Ln.length;t<=e;t++)Ln.push(B(Ln[t-1],Ro([t,0])));return Ln[e]}function In(e,t,n,r){let o=e+n,a=o-e,i=e-(o-a)+(n-a)+t+r,l=o+i;return[l,i-(l-o)]}var vr=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,i=a-this.hi,l=this.hi-(a-i)+(o-i)+this.lo;this.hi=a+l,this.lo=l-(this.hi-a)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function Za(e){let t=e.filter(v=>v.some(N=>N>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,N)=>t.some(M=>M[N]>0));if(t=t.map(v=>v.filter((N,M)=>n[M])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,N)=>t.map(M=>M[N])));let r=t.length,o=t.map(v=>v.reduce((N,M)=>N+M,0)),a=t[0].map((v,N)=>t.reduce((M,E)=>M+E[N],0)).sort((v,N)=>N-v),i=a.length,l=o.reduce((v,N)=>v+N,0),s=Ao(Ve(l));for(let v of o)s=B(s,Ve(v));for(let v of a)s=B(s,Ve(v));let c=[0,0];for(let v of t.flat())c=le(c,Ve(v));let f=B(c,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),d=(v,N)=>{let M=N.join(","),E=p[v].get(M);if(E!==void 0)return E;let y=B(s,Ve(N.reduce((A,R)=>A+R,0)));for(let A of N)y=le(y,Ve(A));for(let A=v;A<i;A++)y=le(y,Ve(a[A]));let x={R:N,KZ:y,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(M,x),v===i-1){let A=[0,0];for(let R of N)A=le(A,Ve(R));return x.max=x.min=A[0],x.edges.push({to:x,hi:A[0],lo:A[1],mult:1}),x}let k=new Map,q=new Array(r+1).fill(0);for(let A=r-1;A>=0;A--)q[A]=q[A+1]+N[A];let S=new Array(r).fill(0),L=(A,R,C)=>{if(A===r-1){S[A]=R;let V=le(C,Ve(R)),z=N.map(($,D)=>$-S[D]).sort(($,D)=>$-D),J=d(v+1,z),T=`${z.join(",")}|${V[0]}`,O=k.get(T);O===void 0?k.set(T,{to:J,hi:V[0],lo:V[1],mult:1}):O.mult++;return}let F=Math.max(0,R-q[A+1]),I=Math.min(N[A],R);for(let V=F;V<=I;V++)S[A]=V,L(A+1,R-V,le(C,Ve(V)))};if(L(0,a[v],[0,0]),x.edges=[...k.values()],x.max=Math.max(...x.edges.map(A=>A.hi+A.to.max)),x.min=Math.min(...x.edges.map(A=>A.hi+A.to.min)),v===i-2){let A=x.edges.map(J=>{let T=J.to.edges[0];return{v:B([J.hi,J.lo],[T.hi,T.lo]),mult:J.mult}});A.sort((J,T)=>J.v[0]-T.v[0]||J.v[1]-T.v[1]);let R=A.length,C=A[R-1].v[0],F=new Float64Array(R),I=new Float64Array(R),V=new Float64Array(R),z=[0,0];A.forEach(({v:J,mult:T},O)=>{F[O]=J[0],I[O]=J[1];let $=le(J,[C,0]);z=B(z,[T*Math.exp($[0])*(1+$[1]),0]),V[O]=z[0]}),x.dist={hi:F,lo:I,cum:V,vmax:C,kv:B(s,[C,0])}}return x},g=d(0,[...o].sort((v,N)=>v-N)),b=new vr,w=(v,N,M,E)=>{if(N+v.max<=u-m){let[L,A]=In(v.KZ[0],v.KZ[1],N,M);return b.add(L,A,E),!0}if(N+v.min>u+m)return!0;let y=v.dist;if(y===void 0)return!1;let[x,k]=In(u,h,-N,-M),q=0,S=y.hi.length;for(;q<S;){let L=q+S>>>1,A=y.hi[L];A<x||A===x&&y.lo[L]<=k?q=L+1:S=L}if(q>0){let[L,A]=In(y.kv[0],y.kv[1],N,M);b.add(L,A,E*y.cum[q-1])}return!0};w(g,0,0,1)||(g.pastHi.push(0),g.pastLo.push(0),g.count.push(1));for(let v=0;v<i-2;v++)for(let N of p[v].values()){let{pastHi:M,pastLo:E,count:y}=N;if(y.length!==0){for(let{to:x,hi:k,lo:q,mult:S}of N.edges)for(let L=0;L<y.length;L++){let[A,R]=In(M[L],E[L],k,q),C=y[L]*S;if(w(x,A,R,C))continue;let F=x.index.get(A);F===void 0?(x.index.set(A,x.count.length),x.pastHi.push(A),x.pastLo.push(R),x.count.push(C)):x.count[F]+=C}N.pastHi=N.pastLo=N.count=[],N.index.clear()}}return Math.min(1,b.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,i=n.paired??!1,l=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(i&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[d,g]=At(Array.from(e),Array.from(r));u=ke(d.map((b,w)=>b-g[w]))}else u=ke(Array.from(e)),r!==void 0&&(h=ke(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?Ja(u,l,i,p):ni(u,h,l,p)}function On(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function os(e,t){return t==="less"?P(e):t==="greater"?P(e,{lowerTail:!1}):2*Math.min(P(e),P(e,{lowerTail:!1}))}function Ya(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 br(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 Nr=(e,t)=>e-t;function ss(e){let t=e.length,n=fe(e.map(Math.abs)),r=0;for(let i=0;i<t;i++)e[i]>0&&(r+=n[i]);let o=0;for(let i of Ie(n))o+=i*i*i-i;let a=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,a]}function Ja(e,t,n,r){let{alternative:o,mu:a,correct:i}=r,l=e.map(E=>E-a),s=l.some(E=>E===0);s&&(l=l.filter(E=>E!==0));let c=l.length,f=t??c<50,m=fe(l.map(Math.abs)),u=Ie(m).length>0,[h,p,d]=ss(l),g,b,w=f&&!u&&!s;if(w){b="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 b=`Wilcoxon signed rank test${i?" with continuity correction":""}`,g=os((p-On(p,o,i))/d,o);let v={method:b,alternative:o,statistic:h,statisticName:"V",pValue:g,nullValue:a,nullValueName:n?"location shift":"location"};if(!r.confInt)return v;let N=l.map(E=>E+a),M=w?ei(N,r):ti(N,r);return{...v,...M}}function Fn(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function rs(e,t,n){let r={confInt:Fn(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function as(e,t,n){let r=e(t==="two.sided"?n/2:n);return r===0?1:r}function is(e){if(!(e>0))throw new RangeError("tolRoot must be positive")}function ei(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(Nr);let i=a.length,l=1-r,s=as(m=>Vo(m,o,!0,!1),n,l),c=qt(s-1,o,!0,!1),f=n==="two.sided"?2*c:c;return{confInt:Fn(n,a[s-1],a[i-s]),confLevel:f-l>l/2?1-Ya(f):r,estimate:br(a),estimateNames:"(pseudo)median"}}function ti(e,t){let{alternative:n,correct:r,confLevel:o,tolRoot:a}=t,i=e.length;if(i===0)return rs(n,NaN);let l=Math.min(...e),s=Math.max(...e);if(!Number.isFinite(l)||!Number.isFinite(s)||i===1||l===s)return rs(n,(l+s)/2,"midrange");let c=(v,N)=>{let[,M,E]=ss(e.map(y=>y-v).filter(y=>y!==0));return(M-On(M,n,N))/E},f=c(l,r),m=c(s,r),u=v=>-H(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>H(v/2)):!(f<u(v/2))&&!(m>H(v/2)),p=1-o,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;is(a);let g=v=>xe(N=>c(N,r)-v,l,s,{tol:a,fLower:f-v,fUpper:m-v}),b;if(p>=1){let v=br([...e].sort(Nr));b=Fn(n,v,v)}else n==="greater"?b=[g(u(p)),1/0]:n==="less"?b=[-1/0,g(H(p))]:b=[g(u(p/2)),g(H(p/2))];let w=xe(v=>c(v,!1),l,s,{tol:a,fLower:c(l,!1),fUpper:c(s,!1)});return{confInt:b,confLevel:d?1-Math.min(1,p):o,estimate:w,estimateNames:"(pseudo)median"}}function ls(e,t){let n=e.length,r=t.length,o=fe([...e,...t]),a=0;for(let f=0;f<n;f++)a+=o[f];let i=a-n*(n+1)/2,l=0;for(let f of Ie(o))l+=f*f*f-f;let s=n+r,c=Math.sqrt(n*r/12*(s+1-l/(s*(s-1))));return[i,i-n*r/2,c]}function ni(e,t,n,r){let{alternative:o,mu:a,correct:i}=r,l=e.length,s=t.length,c=e.map(N=>N-a),f=n??(l<50&&s<50),m=Ie([...c,...t]).length>0,[u,h,p]=ls(c,t),d,g,b=f&&!m;if(b)if(g="Wilcoxon rank sum exact test",o==="less")d=kt(u,l,s,!0,!1);else if(o==="greater")d=kt(u-1,l,s,!1,!1);else{let N=u>l*s/2?kt(u-1,l,s,!1,!1):kt(u,l,s,!0,!1);d=Math.min(1,2*N)}else g=`Wilcoxon rank sum test${i?" with continuity correction":""}`,d=os((h-On(h,o,i))/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 v=b?ri(e,t,r):oi(e,t,r);return{...w,...v}}function ri(e,t,n){let{alternative:r,confLevel:o}=n,a=e.length,i=t.length,l=[];for(let h of e)for(let p of t)l.push(h-p);l.sort(Nr);let s=l.length,c=1-o,f=as(h=>Do(h,a,i,!0,!1),r,c),m=kt(f-1,a,i,!0,!1),u=r==="two.sided"?2*m:m;return{confInt:Fn(r,l[f-1],l[s-f]),confLevel:u-c>c/2?1-u:o,estimate:br(l),estimateNames:"difference in location"}}function oi(e,t,n){let{alternative:r,correct:o,confLevel:a,tolRoot:i}=n,l=Math.min(...e)-Math.max(...t),s=Math.max(...e)-Math.min(...t);if(!Number.isFinite(l)||!Number.isFinite(s))throw new Error("cannot compute the confidence interval because the differences are not finite");let c=(h,p)=>{let[,d,g]=ls(e.map(b=>b-h),t);return(d-On(d,r,p))/g},f=(h,p)=>{let d=c(l,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?l:g>=0?s:(is(i),xe(b=>c(b,p)-h,l,s,{tol:i,fLower:d,fUpper:g}))},m=1-a,u;return r==="greater"?u=[f(-H(m),o),1/0]:r==="less"?u=[-1/0,f(H(m),o)]:u=[f(-H(m/2),o),f(H(m/2),o)],{confInt:u,confLevel:a,estimate:f(0,!1),estimateNames:"difference in location"}}var cs=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function si(e){if(cs.includes(e))return e;let t=cs.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=si(n.method??"holm"),o=Array.from(e),a=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||a.push(m);let i=a.length,l=n.n??i;if(!(l>=i))throw new RangeError("n must be at least the number of non-NaN p-values");if(l<=1)return o;let s=a.map(m=>o[m]),c=ai(s,r,l),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 ai(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),i=-1/0;return a.forEach((l,s)=>{i=Math.max(i,(n-s)*e[l]),o[l]=Math.min(1,i)}),o}case"hochberg":{let a=_n(e,!0),i=1/0;return a.forEach((l,s)=>{let c=r-s;i=Math.min(i,(n-c+1)*e[l]),o[l]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let a=t==="BY"?ee(Array.from({length:Math.floor(n)},(s,c)=>1/(c+1))):1,i=_n(e,!0),l=1/0;return i.forEach((s,c)=>{let f=r-c;l=Math.min(l,a*n/f*e[s]),o[s]=Math.min(1,l)}),o}case"hommel":return ii(e,n)}}function ii(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]),i=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++)i[f]=Math.max(i[f],c);for(let f=0;f<t-s;f++)i[f]=Math.max(i[f],Math.min(s*a[f],c))}let l=new Array(n);return o.forEach((s,c)=>{s<n&&(l[s]=i[c])}),l}var li=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 ci(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=xt(e,t,n,!1)*li,a=0;if(e<r){for(let s=Math.ceil(r);s<=t;s++)xt(s,t,n,!1)<=o&&a++;let l=a===0?0:at(t-a,t,n,!1,!1);return Math.min(1,at(e,t,n,!0,!1)+l)}for(let l=0;l<=Math.floor(r);l++)xt(l,t,n,!1)<=o&&a++;let i=a===0?0:at(a-1,t,n,!0,!1);return Math.min(1,at(e-1,t,n,!1,!1)+i)}function yr(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 i=te(n.alternative),l=n.confLevel??.95;Me(l);let s;i==="less"?s=at(r,o,a,!0,!1):i==="greater"?s=at(r-1,o,a,!1,!1):s=ci(r,o,a);let c=u=>r===0?0:cr(u,r,o-r+1,!0,!1),f=u=>r===o?1:cr(1-u,r+1,o-r,!0,!1),m;if(i==="less")m=[0,f(1-l)];else if(i==="greater")m=[c(1-l),1];else{let u=(1-l)/2;m=[c(u),f(u)]}return{method:"Exact binomial test",alternative:i,statistic:r,statisticName:"number of successes",parameter:o,parameterName:"number of trials",pValue:s,confInt:m,confLevel:l,estimate:r/o,estimateNames:"probability of success",nullValue:a,nullValueName:"probability of success"}}var fi=1+1e-7;function mi(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function ui(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=Qt(e,t,!1)*fi,r=l=>Qt(l,t,!1)<=n,o=Math.floor(t);if(e<t){let l=Math.max(e+1,o),s=l;if(!r(l)){let c=l,f=1;for(s=l+1;!r(s);)c=s,f*=2,s=l+f;for(;s-c>1;){let m=c+Math.floor((s-c)/2);r(m)?s=m:c=m}}return Math.min(1,rt(e,t,!0,!1)+rt(s-1,t,!1,!1))}let a=Math.min(e-1,o),i;if(!r(0))i=-1;else if(r(a))i=a;else{i=0;let l=a;for(;l-i>1;){let s=i+Math.floor((l-i)/2);r(s)?i=s:l=s}}return Math.min(1,rt(i,t,!0,!1)+rt(e-1,t,!1,!1))}function hi(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(mi))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,a=t.T??1,i=typeof a=="number"?new Array(o).fill(a):Array.from(a);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(b=>Number.isFinite(b)&&b>=0))throw new RangeError("'T' must be nonnegative");let l=t.r??1;if(!(Number.isFinite(l)&&l>=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[b,w]=r,[v,N]=i,M=b+w,E=l*v/(l*v+N),y=yr([b,w],{p:E,alternative:s,confLevel:c}),x=k=>k/(1-k)*(N/v);return{method:"Comparison of Poisson rates",alternative:s,statistic:b,statisticName:"count1",parameter:M*l*v/(v+l*N),parameterName:"expected count1",pValue:y.pValue,confInt:[x(y.confInt[0]),x(y.confInt[1])],confLevel:c,estimate:b/v/(w/N),estimateNames:"rate ratio",nullValue:l,nullValueName:"rate ratio"}}let f=r[0],m=i[0],u=l*m,h;s==="less"?h=rt(f,u,!0,!1):s==="greater"?h=rt(f-1,u,!1,!1):h=ui(f,u);let p=b=>f===0?0:lr(b,f,1,!0,!1),d=b=>lr(1-b,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 b=(1-c)/2;g=[p(b),d(b)]}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:l,nullValueName:"event rate"}}function Mr(e,t,n={}){let r,o,a=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let q=e;if(q.some(S=>S.length!==2))throw new RangeError("'x' must have 2 columns");r=q.map(S=>S[0]),o=q.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 i=r.length,l=[];for(let q=0;q<i;q++)!Number.isNaN(r[q])&&!Number.isNaN(o[q])&&l.push(q);let s=l.length;if(s<1)throw new RangeError("not enough data");if(r=l.map(q=>r[q]),o=l.map(q=>o[q]),o.some(q=>!(q>0)))throw new RangeError("elements of 'n' must be positive");if(r.some(q=>!(q>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((q,S)=>q>o[S]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let c;if(a.p!==void 0){let q=typeof a.p=="number"?[a.p]:Array.from(a.p);if(q.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(c=l.map(S=>q[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((q,S)=>q/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,b=!1;if(g>0&&s===1){let q=Math.abs(r[0]-o[0]*c[0]);q<=g&&([g,b]=[q,!0])}else if(g>0&&s===2&&!f){let q=Math.abs(p)/d;q<=g&&([g,b]=[q,!0])}let w=r.reduce((q,S)=>q+S,0)/o.reduce((q,S)=>q+S,0),v=0;if(!b)for(let q=0;q<s;q++){let S=c?.[q]??w,L=o[q]*S,A=o[q]*(1-S);v+=(Math.abs(r[q]-L)-g)**2/L,v+=(Math.abs(o[q]-r[q]-A)-g)**2/A}let N=c!==void 0?s:s-1,M;if(u==="two.sided")M=me(v,N/2,2,!1,!1);else{let q=Math.sign(s===1?h[0]-c[0]:p);M=P(q*Math.sqrt(v),{lowerTail:u==="less"})}let E;if(s===1||s===2&&!f){let q=H(u==="two.sided"?(1+m)/2:m);if(s===1){let S=o[0],L=q*q,A=(V,z)=>(V+L/(2*S)+z*q*Math.sqrt(V*(1-V)/S+L/(4*S*S)))/(1+L/S),R=h[0]+g/S,C=h[0]-g/S,F=Math.min(1,Math.max(0,R>=1?1:A(R,1))),I=Math.min(1,Math.max(0,C<=0?0:A(C,-1)));E=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[0,F]}else{let S=Math.sqrt(h.reduce((C,F,I)=>C+F*(1-F)/o[I],0)),L=q*S+g*d,A=Math.max(-1,p-L),R=Math.min(1,p+L);E=u==="two.sided"?[A,R]:u==="greater"?[A,1]:[-1,R]}}let y=s===1?"p":l.map(q=>`prop ${q+1}`),k={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:v,statisticName:"X-squared",parameter:N,parameterName:"df",pValue:M,estimate:s===1?h[0]:h,estimateNames:y};return c!==void 0&&(k.nullValue=s===1?c[0]:c,k.nullValueName=y),E!==void 0&&(k.confInt=E,k.confLevel=m),k}function pi(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=di(e,t)}let o=n.correct??!0,a=r.length,i,l=o&&a===2&&r[0][1]!==r[1][0];if(l){let c=r[0][1],f=r[1][0];i=(Math.abs(c-f)-1)**2/(c+f)}else{i=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];i+=(m-u)**2/(m+u)}}let s=a*(a-1)/2;return{method:l?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:i,statisticName:"McNemar's chi-squared",parameter:s,parameterName:"df",pValue:me(i,s/2,2,!1,!1)}}function di(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[];for(let c=0;c<e.length;c++){let f=e[c],m=t[c];typeof f=="number"&&Number.isNaN(f)||typeof m=="number"&&Number.isNaN(m)||n.push([f,m])}let r=c=>Array.from(new Set(c)).sort((f,m)=>typeof f=="number"&&typeof m=="number"?f-m:String(f)<String(m)?-1:String(f)>String(m)?1:0),o=r(n.map(c=>c[0])),a=r(n.map(c=>c[1]));if(o.length<2||o.length!==a.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let i=new Map(o.map((c,f)=>[c,f])),l=new Map(a.map((c,f)=>[c,f])),s=o.map(()=>new Array(o.length).fill(0));for(let[c,f]of n)s[i.get(c)][l.get(f)]++;return s}function fs(e,t,n={}){if(typeof e=="string"){let g=Ke(e,t);return fs(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 i=te(n.alternative),l=n.confLevel??.95;Me(l);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(i==="less")p=u,d=[0,f/ot(1-l,s,c,!0,!1)];else if(i==="greater")p=h,d=[f/ot(l,s,c,!0,!1),1/0];else{p=Math.min(1,2*Math.min(u,h));let g=(1-l)/2;d=[f/ot(1-g,s,c,!0,!1),f/ot(g,s,c,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:m,statisticName:"F",parameter:[s,c],parameterName:["num df","denom df"],pValue:p,confInt:d,confLevel:l,estimate:f,estimateNames:"ratio of variances",nullValue:a,nullValueName:"ratio of variances"}}function ms(e,t){if(typeof e=="string"){let u=Jt(e,t);return ms(Go(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 i=0,l=0,s=0,c=0;for(let u=0;u<r;u++)i+=o[u],l+=o[u]*a[u],s+=o[u]*Math.log(a[u]),c+=1/o[u];l/=i;let f=(i*Math.log(l)-s)/(1+(c-1/i)/(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:me(f,m/2,2,!1,!1)}}function ut(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 l=i.map(f=>Array.from(f).filter(m=>!Number.isNaN(m)));if(n==="error"&&l.some(f=>f.length===0))throw new RangeError("all groups must contain data");let s=[],c=[];return l.forEach((f,m)=>{f.length!==0&&(s.push(String(m+1)),c.push(f))}),{labels:s,groups:c}}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;for(let i=0;i<r.length;i++){let l=r[i],s=t[i];if(Number.isNaN(l)||typeof s=="number"&&Number.isNaN(s))continue;let c=o.get(s);c===void 0&&o.set(s,c=[]),c.push(l)}let a=Array.from(o.keys()).sort((i,l)=>typeof i=="number"&&typeof l=="number"?i-l:String(i)<String(l)?-1:String(i)>String(l)?1:0);return{labels:a.map(String),groups:a.map(i=>o.get(i))}}function ht(e,t,n="drop"){return ut(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 xr(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 Er(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:me(n,r/2,2,!1,!1)}}function us(e){let t=0;for(let n of Ie(e))t+=n*n*n-n;return t}function Dn(e,t){if(typeof e=="string"){let c=Jt(e,t);return Dn(c.values,c.group.codes)}let n=ht(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,i=fe(o),l=0,s=0;for(let c of n){let f=0;for(let m=0;m<c.length;m++)f+=i[s+m];s+=c.length,l+=f*f/c.length}return l=(12*l/(a*(a+1))-3*(a+1))/(1-us(o)/(a*a*a-a)),Er("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",l,r-1)}function qr(e,t){if(typeof e=="string")return qr(hr(e,t));let n=xr(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),i=0;for(let c of n){let f=fe(c);for(let m=0;m<o;m++)a[m]+=f[m];i+=us(c)}let l=0;for(let c of a)l+=(c-r*(o+1)/2)**2;let s=12*l/(r*o*(o+1)-i/(o-1));return Er("Friedman rank sum test","Friedman chi-squared",s,o-1)}function gi(e){return{...qr(e),method:"Cochran's Q test",statisticName:"Q"}}function hs(e,t){if(typeof e=="string")return hs(hr(e,t));let n=xr(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=fe(n.map(p=>Math.max(...p)-Math.min(...p))),i=new Array(o).fill(0),l=0;n.forEach((p,d)=>{let g=fe(p);for(let b=0;b<o;b++){let w=a[d]*(g[b]-(o+1)/2);l+=w*w,i[b]+=w}});let s=0;for(let p of i)s+=p*p;s/=r;let c=o-1,f=(r-1)*(o-1),m,u,h=[c,f];if(l===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/(l-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 ps(e,t){if(typeof e=="string"){let u=Jt(e,t);return ps(u.values,u.group.codes)}let n=ht(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,l=fe(o).map(u=>H((1+u/(a+1))/2)),s=0;for(let u of l)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+=l[m+p];m+=u.length,f+=u.length*(h/u.length-c)**2}return f/=re(l),Er("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",f,r-1)}function ds(e,t,n={}){if(typeof e=="string"){let N=Ke(e,t);return ds(N.x,N.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),a=te(n.alternative),i=r.length,l=o.length,s=i+l;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<i;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=i*p,b=i*l/(s*(s-1))*d,w=(h-g)/Math.sqrt(b),v=a==="less"?P(w):a==="greater"?P(w,{lowerTail:!1}):2*P(-Math.abs(w));return{method:"Mood two-sample test of scale",alternative:a,statistic:w,statisticName:"Z",pValue:v}}function pt(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var wi=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],vi=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],bi=[.544,-.39978,.025054,-6714e-7],Ni=[1.3822,-.77857,.062767,-.0020322],yi=[-1.5861,-.31082,-.083751,.0038915],Mi=[-.4803,-.082676,.0030302],xi=[-2.273,.459];function Ei(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]=-H((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let a=Math.sqrt(o),i=1/Math.sqrt(e),l=r[0]/a+pt(wi,i),s=new Float64Array(n);s[0]=l;let c,f;if(e>5){let m=r[1]/a+pt(vi,i);s[1]=m,c=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*l*l-2*m*m))}else c=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*l*l));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 gs(e){let t=e.length,n=Ei(t),r=e[t-1]-e[0],o=0;for(let c of e)o+=c/r;o/=t;let a=0,i=0,l=0;for(let c=0;c<t;c++){let f=e[c]/r-o;a+=n[c]*n[c],i+=f*f,l+=n[c]*f}let s=Math.sqrt(a*i);return(s-l)*(s+l)/(a*i)}var qi=1.90985931710274,ki=1.0471975511966;function ws(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=qi*(Math.asin(Math.sqrt(1-e))-ki);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=pt(xi,t);if(n>=i)return 1e-99;let l=pt(bi,t),s=Math.exp(pt(Ni,t));return P(-Math.log(i-n),{mean:l,sd:s,lowerTail:!1})}let r=Math.log(t),o=pt(yi,r),a=Math.exp(pt(Mi,r));return P(n,{mean:o,sd:a,lowerTail:!1})}function vs(e,t,n={}){if(typeof e=="string"){let h=Uo(e,t);return vs(h.values,h.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=ht(e,r),a=n.varEqual??!1,i=o.length;if(i<2)throw new RangeError("not enough groups");let l=o.map(h=>h.length);if(l.some(h=>h<2))throw new RangeError("not enough observations");let s=o.map(se),c=o.map(re),f=l.reduce((h,p)=>h+p,0),m,u;if(a){let h=se(o.flat()),p=0,d=0;for(let g=0;g<i;g++)p+=l[g]*(s[g]-h)**2,d+=(l[g]-1)*c[g];u=f-i,m=p/(i-1)/(d/u)}else{let h=l.map((w,v)=>w/c[v]),p=h.reduce((w,v)=>w+v,0),d=0,g=0;for(let w=0;w<i;w++)d+=(1-h[w]/p)**2/(l[w]-1),g+=h[w]*s[w];d/=i*i-1,g/=p;let b=0;for(let w=0;w<i;w++)b+=h[w]*(s[w]-g)**2;m=b/((i-1)*(1+2*(i-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:[i-1,u],parameterName:["num df","denom df"],pValue:ae(m,i-1,u,!1,!1)}}function Ai(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=gs(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:ws(r,n)}}function kr(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 Ri([[e,t],[n,r]]){let o=e+n,a=t+r,i=e+t,l=Math.max(0,i-a),s=Math.min(i,o),c=new Float64Array(s-l+1);for(let f=l;f<=s;f++)c[f-l]=Et(f,o,a,i,!1);return{lo:l,d:c}}function Ti(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 $n=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let a=Ri(o);n+=a.lo,r=Ti(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 l=0;l<n;l++){let s=this.logNull[l]+(this.lo+l)*o;r[l]=s,s>a&&(a=s)}for(let l=0;l<n;l++)r[l]=Math.exp(r[l]-a);let i=kr(r,0,n);for(let l=0;l<n;l++)r[l]=r[l]/i;return r}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)r=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?kr(o,a,o.length):kr(o,0,a+1)}};function Ar(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function bs(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Ar(r=>e.mean(r)-t,n>t)}function Rr(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Ar(o=>n-e.cdf(t,o,!1),r<n)}function Tr(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Ar(o=>e.cdf(t,o,!0)-n,r>n)}function Si(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 i=te(t.alternative),l=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?Ii(f,i,c):Li(f,a,i,l,c)}return Oi(e,a,r,o)}function Li(e,t,n,r,o){let a=0,i=0,l=0,s=0,c=0,f=0,m=0,u=0;e.forEach(([[N,M],[E,y]],x)=>{let k=t[x],q=N+M,S=E+y,L=N+E,A=M+y;a+=N,i+=q*L/k,l+=q*S*L*A/(k*k*(k-1));let R=(N+y)/k,C=(M+E)/k,F=N*y/k,I=M*E/k;s+=F,c+=I,f+=R*F,m+=R*I+C*F,u+=C*I});let h=a-i,p=r&&Math.abs(h)>=.5?.5:0,d=(Math.abs(h)-p)**2/l,g=n==="two.sided"?me(d,.5,2,!1,!1):P(Math.sign(h)*Math.sqrt(d),{lowerTail:n==="less"}),b=s/c,w=Math.sqrt(f/(2*s*s)+m/(2*s*c)+u/(2*c*c)),v;if(n==="less")v=[0,b*Math.exp(H(o)*w)];else if(n==="greater")v=[b*Math.exp(H(o,{lowerTail:!1})*w),1/0];else{let N=H((1+o)/2);v=[b*Math.exp(-N*w),b*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:v,confLevel:o,estimate:b,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Ii(e,t,n){let r=new $n(e),o=e.reduce((l,s)=>l+s[0][0],0),a;if(t==="two.sided"){let l=r.density(1),s=l[o-r.lo]*(1+1e-7),c=[0,0];for(let f of l)f<=s&&(c=B(c,[f,0]));a=Math.min(1,c[0])}else a=r.cdf(o,1,t==="greater");let i;if(t==="less")i=[0,Rr(r,o,1-n)];else if(t==="greater")i=[Tr(r,o,1-n),1/0];else{let l=(1-n)/2;i=[Tr(r,o,l),Rr(r,o,l)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:o,statisticName:"S",pValue:a,confInt:i,confLevel:n,estimate:bs(r,o),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Oi(e,t,n,r){let o=(n-1)*(r-1),a=new Array(o).fill(0),i=Array.from({length:o},()=>new Array(o).fill(0));e.forEach((c,f)=>{let m=t[f],u=c.map(d=>d.reduce((g,b)=>g+b,0)),h=Array.from({length:r},(d,g)=>c.reduce((b,w)=>b+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 b=g+(n-1)*d;a[b]+=c[g][d]-u[g]*h[d]/m;for(let w=0;w<r-1;w++)for(let v=0;v<n-1;v++){let N=(g===v?m*u[g]:0)-u[g]*u[v],M=(d===w?m*h[d]:0)-h[d]*h[w];i[b][v+(n-1)*w]+=p*N*M}}});let l=be(i,a),s=0;for(let c=0;c<o;c++)s+=a[c]*l[c];return{method:"Cochran-Mantel-Haenszel test",statistic:s,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:o,parameterName:"df",pValue:me(s,o/2,2,!1,!1)}}var Ns=new Map;function Fi(e,t){let n=`${e},${t}`,r=Ns.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 i=a.reduce((u,h)=>u+h,0),l=Array.from({length:e+1},()=>new Float64Array(i+1));l[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(i+1));for(let g=0;g<=e;g++){let b=l[g],w=e-g;if(w>h)continue;let v=w/h;for(let N=0;N<=i;N++){let M=b[N];M!==0&&(w>0&&(d[g+1][N+p]+=M*v),w<h&&(d[g][N]+=M*(1-v)))}}l=d}let s=l[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 Ns.set(n,m),m}function Sr(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}=Fi(t,n),i=Math.floor(e+1e-7)-o,l=0;if(r)for(let s=0;s<=Math.min(i,a.length-1);s++)l+=a[s];else for(let s=Math.max(i+1,0);s<a.length;s++)l+=a[s];return Math.min(l,1)}var ys=new Map;function _i(e,t){let n=`${e},${t}`,r=ys.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 i=Array.from({length:e+1},()=>new Float64Array(a+1));i[0][0]=1;for(let m=0;m<o;m++){let u=Math.min(m+1,o-m),h=Array.from({length:e+1},()=>new Float64Array(a+1));for(let p=0;p<=e;p++){if(e-p>o-m)continue;let d=i[p];for(let g=0;g<=a;g++){let b=d[g];b!==0&&(p<e&&(h[p+1][g+u]+=b),e-p<o-m&&(h[p][g]+=b))}}i=h}let l=i[e],s=0;for(;l[s]===0;)s++;let c=l.length-1;for(;l[c]===0;)c--;let f={min:s,counts:l.slice(s,c+1)};return ys.set(n,f),f}function Pn(e,t,n){let{min:r,counts:o}=_i(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let a=Ho(t+n,t),i=0;for(let l=0;l<o.length;l++)if(i+=o[l]/a,i>=e)return r+l;return r+o.length-1}function Ms(e,t,n={}){if(typeof e=="string"){let v=Ke(e,t);return Ms(v.x,v.y,n)}let r=Array.from(e).filter(v=>!Number.isNaN(v)),o=Array.from(t).filter(v=>!Number.isNaN(v)),a=r.length,i=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let l=te(n.alternative),s=n.confInt??!1,c=n.confLevel??.95;s&&Me(c);let f=a+i,m=fe(r.concat(o)),u=m.map(v=>Math.min(v,f-v+1)),h=0;for(let v=0;v<a;v++)h+=u[v];let p=new Set(m).size<f,d=n.exact??(a<50&&i<50&&!p),g,b;if(d&&!p){let v=M=>Sr(M-1,a,i,!1),N=M=>Sr(M,a,i,!0);if(l==="two.sided"){let M=Math.floor((a+1)**2/4)+Math.floor(a*i/2)/2;g=Math.min(1,2*(h>M?v(h):N(h)))}else l==="less"?g=v(h):g=N(h);s&&(b=Ci(r,o,l,c))}else{let v=0;for(let x of u)v+=x*x;let N=f%2===0,M=N?a*(f+2)/4:a*(f+1)**2/(4*f),E=N?a*i*(16*v-f*(f+2)**2)/(16*f*(f-1)):a*i*(16*f*v-(f+1)**4)/(16*f*f*(f-1)),y=(h-M)/Math.sqrt(E);g=l==="two.sided"?2*Math.min(P(y),P(y,{lowerTail:!1})):l==="less"?P(y,{lowerTail:!1}):P(y),s&&(b=Di(r,o,l,c))}let w={method:"Ansari-Bradley test",alternative:l,statistic:h,statisticName:"AB",pValue:g};return b!==void 0&&(w.confInt=b.confInt,w.confLevel=c,w.estimate=b.estimate,w.estimateNames="ratio of scales"),w.nullValue=1,w.nullValueName="ratio of scales",w}function xs(e,t,n){let r=e.length,o=r+t.length,a=Vi(e.map(s=>s/n).concat(t)),i=a.map(s=>Math.min(s,o-s+1)),l=0;for(let s=0;s<r;s++)l+=i[s];return{ab:l,a:i,ties:new Set(a).size<o}}function Vi(e){let t=e.filter(a=>!Number.isNaN(a));if(t.length===e.length)return fe(e);let n=fe(t),r=0,o=t.length;return e.map(a=>Number.isNaN(a)?++o:n[r++])}function Lr(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Ci(e,t,n,r){let o=e.length,a=t.length,i=[];for(let g of e)for(let b of t)g/b>=0&&i.push(g/b);i.sort((g,b)=>g-b);let l=i.length;if(l===0)return{confInt:Lr(n,1-r,()=>[NaN,NaN]),estimate:NaN};let s=i.map((g,b)=>xs(e,t,g+(b<l-1?(i[b+1]-g)/2:g*1.01)).ab),c=(g,b,w)=>{let v=1/0;for(let M of s)(b?M-g>0:M-g>=0)&&M-g<v&&(v=M-g);let N=w?-1/0:1/0;return s.forEach((M,E)=>{M-g===v&&(N=w?Math.max(N,i[E]):Math.min(N,i[E]))}),N},f=g=>{let b=Pn(g/2,o,a),w=Pn(1-g/2,o,a);if(!s.some(M=>M-b>=0)||!s.some(M=>M-w>0))return[NaN,NaN];let v=c(b,!1,!1),N=c(w,!0,!0);return v>N?[c(w,!1,!1),c(b,!0,!0)]:[v,N]},m=Lr(n,1-r,f),u=Pn(.5,o,a),h=-1/0,p=1/0;s.forEach((g,b)=>{g-u<=0?h=Math.max(h,i[b]):p=Math.min(p,i[b])});let d=h===-1/0||p===1/0?NaN:(p+h)/2;return{confInt:m,estimate:d}}function Di(e,t,n,r){let o=e.length,a=t.length,i=o+a,l=i%2===0,s=(b,w)=>{let{ab:v,a:N,ties:M}=xs(e,t,b),E=l?v-o*(i+2)/4:v-o*(i+1)**2/(4*i),y;if(!M)y=Math.sqrt(l?o*a*(i+2)*(i-2)/(48*(i-1)):o*a*(i+1)*(3+i*i)/(48*i*i));else{let x=0;for(let k of N)x+=k*k;y=Math.sqrt(l?o*a*(16*x-i*(i+2)**2)/(16*i*(i-1)):o*a*(16*i*x-(i+1)**4)/(16*i*i*(i-1)))}return E/y-w},c=[],f=(b,w)=>{b.length===0||w.length===0||c.push(Math.min(...b)/Math.max(...w),Math.max(...b)/Math.min(...w))};if(f(e.filter(b=>b>0),t.filter(b=>b>0)),f(e.filter(b=>b<=0),t.filter(b=>b<0)),c.some(b=>!Number.isFinite(b)))return;let m=Math.min(...c),u=Math.max(...c),h=b=>xe(w=>s(w,b),m,u,{tol:1e-4}),p=b=>{let w=H(b/2),v=H(b/2,{lowerTail:!1});if(s(m,w)>0||s(u,v)<0)return[NaN,NaN];let N=h(w),M=h(v);return N<=M?[N,M]:[M,N]},d=Lr(n,1-r,p),g=s(m,0)>0||s(u,0)<0?NaN:h(0);return{confInt:d,estimate:g}}function $i(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,i=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let l=te(n.alternative),s=n.confLevel??.95;qn(s);let c=fe(r.concat(o)),f=c.slice(0,a),m=c.slice(a),u=x=>x.reduce((k,q)=>k+q,0)/x.length,h=u(f),p=u(m),d=(x,k,q)=>{let S=(x.length+1)/2,L=0;for(let A=0;A<x.length;A++)L+=(x[A]-k[A]-q+S)**2;return L/(x.length-1)},g=d(f,fe(r),h),b=d(m,fe(o),p),w=(p-(i+1)/2)/a,v=a*g+i*b,N=a*i*(p-h)/(a+i)/Math.sqrt(v),M=v**2/((a*g)**2/(a-1)+(i*b)**2/(i-1)),E;l==="greater"?E=we(N,M,!0,!1):l==="less"?E=we(N,M,!1,!1):E=2*we(-Math.abs(N),M,!0,!1);let y=Pe((1-s)/2,M,!1,!1)*Math.sqrt(g/(a*i**2)+b/(i*a**2));return{method:"Brunner-Munzel Test",alternative:l,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 Pi=[1.2337005501361697,7831619385924639e-32];function ji(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,i=0,l=e.slice(),s=0,c=n;for(;c>0;)c&1&&(a=a===void 0?l.slice():r(a,l),i+=s,i+=o(a)),c>>=1,c>0&&(l=r(l,l),s*=2,s+=o(l));return[a,i]}function Es(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]=Mt(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[i,l]=ji(a,r,e),s=i[(n-1)*r+n-1],c=l;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 Ir(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=ge(Pi,Mt(e,e)),a=o[0]+o[1],i=Math.exp(-o[0])*(1-o[1]),l=0;for(let c=2;c<100;c++){let f=Math.exp(-(2*c-1)*(2*c-1)*a);if(l+=f,f<=1e-17*(l+i))break}let s=Math.sqrt(2*Math.PI)/e*(i+l);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 qs(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]=Mt(e,t),o=Math.floor(e-n-r),a=0;for(let i=0;i<=o;i++){let l=B(nt(e-i,-n),[-r,0]);if(l[0]<=0)break;let s=B(nt(i,n),[r,0]),c=ge(s,[e,0]),f=ge(l,[e,0]);a+=So(i,e-i,c,f,!1)/(c[0]+c[1])}return Math.min(t*a,1)}function ks(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,i;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);i=new Array(a+1).fill(!1);for(let h=1;h<a;h++)i[h]=u[h]!==u[h-1]}let l=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)>=l},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);(i===void 0?p<a:i[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 As(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 Rs(e,t,n={}){if(typeof e=="string"){let w=Ke(e,t);return Rs(w.x,w.y,n)}let r=Array.from(e).filter(w=>!Number.isNaN(w)).sort((w,v)=>w-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let a=te(n.alternative),i=a==="two.sided"?"D":a==="greater"?"D^+":"D^-";if(typeof t=="function"){let w=-1/0,v=-1/0;for(let x=0;x<o;x++){let k=t(r[x]);w=Math.max(w,(x+1)/o-k),v=Math.max(v,k-x/o)}let N=a==="two.sided"?Math.max(w,v):a==="greater"?w:v,M=new Set(r).size<o,E=n.exact??(o<100&&!M),y;return a==="two.sided"?y=E?1-Es(o,N):Ir(Math.sqrt(o)*N,!1):y=E?qs(o,N):Math.exp(-2*o*N*N),{method:`${E?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:As(a,!0),exact:E,statistic:N,statisticName:i,pValue:Math.min(1,Math.max(0,y))}}let l=Array.from(t).filter(w=>!Number.isNaN(w)),s=l.length;if(s<1)throw new RangeError("not enough 'y' data");let c=r.map(w=>[w,0]).concat(l.map(w=>[w,1]));c.sort((w,v)=>w[0]-v[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 v=f/o-m/s;u=Math.max(u,v),h=Math.max(h,-v)}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,b;if(g)b=ks(p,o,s,d?{alternative:a,z:c.map(w=>w[0])}:{alternative:a});else{let w=o*s/(o+s);b=a==="two.sided"?Ir(Math.sqrt(w)*p,!1):Math.exp(-2*w*p*p)}return{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:As(a,!1),exact:g,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,b))}}function Ts(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;for(let i=0;i<r.length;i++){let l=t[i];if(typeof l=="number"&&Number.isNaN(l)||n&&Number.isNaN(r[i]))continue;let s=o.get(l);s===void 0&&o.set(l,s=[]),s.push(r[i])}let a=Array.from(o.keys()).sort((i,l)=>typeof i=="number"&&typeof l=="number"?i-l:String(i)<String(l)?-1:String(i)>String(l)?1:0);return{levels:a,groups:a.map(i=>o.get(i))}}function Or(e,t,n){let r=[],o=[];for(let l=0;l<e-1;l++)for(let s=l+1;s<e;s++)r.push(t(s,l)),o.push([s-1,l]);let a=It(r,n),i=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([l,s],c)=>i[l][s]=a[c]),i}function Fr(e,t,n,r){return{method:e,pValue:n,rowNames:t.slice(1).map(String),colNames:t.slice(0,-1).map(String),pAdjustMethod:r}}function zi(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),i=n.pAdjustMethod??"holm",{levels:l,groups:s}=Ts(e,t,!r),c=l.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(b=>b.length),h=s.map(b=>b.length>0?se(b):NaN),p=0,d=0;s.forEach(b=>{b.length>1&&(p+=re(b)*(b.length-1),d+=b.length-1)});let g=Math.sqrt(p/d);f=(b,w)=>{let v=(h[b]-h[w])/(g*Math.sqrt(1/u[b]+1/u[w]));return a==="less"?we(v,d,!0,!1):a==="greater"?we(v,d,!1,!1):2*we(-Math.abs(v),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 Fr(m,l,Or(c,f,i),i)}function Wi(e,t,n={}){let{pAdjustMethod:r,alternative:o,...a}=n,i=r??"holm",{levels:l,groups:s}=Ts(e,t,!(a.paired??!1)),c=l.length;if(c<2)throw new RangeError("there must be at least two groups");let f="",m=Or(c,(u,h)=>{let p=tn(s[u],s[h],{...a,...o===void 0?{}:{alternative:o}});return f=p.method,p.pValue},i);return Fr(f,l,m,i)}function Gi(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 i=n.pAdjustMethod??"holm",l=n.correct??!0,s=Or(a,(f,m)=>Mr([r[f],r[m]],[o[f],o[m]],{correct:l}).pValue,i),c=Array.from({length:a},(f,m)=>m+1);return Fr("Pairwise comparison of proportions",c,s,i)}var nn=[0,0];function Ss(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=le(e,Mt(t,t));return nt(t,n[0]/(2*t))}function jn(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Ss([e[r],0]))}function Ot(e,t){return je(e,t,[]).fit}function je(e,t,n){let r=t.length,o=Math.min(e.length,r),a=e.map(f=>f.slice()),i=t.slice(),l=n.map(f=>f.slice()),s=[];for(let f=0;f<o;f++){let m=a[f],u=nn;for(let b=f;b<r;b++)u=B(u,oe(m[b],m[b]));if(u[0]===0){s.push(nn);continue}let h=Ss(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=B(m[f],p);let d=oe(p,m[f]),g=b=>{let w=nn;for(let N=f;N<r;N++)w=B(w,oe(m[N],b[N]));let v=ge(w,d);for(let N=f;N<r;N++)b[N]=le(b[N],oe(v,m[N]))};for(let b=f+1;b<e.length;b++)g(a[b]);g(i);for(let b of l)g(b);s.push([-p[0],-p[1]])}let c=new Array(o).fill(nn);for(let f=o-1;f>=0;f--){let m=i[f];for(let u=f+1;u<o;u++)m=le(m,oe(a[u][f],c[u]));c[f]=ge(m,s[f])}return{fit:{qty:i,coef:c},qtv:l}}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 Ui(e,t,n,r,o){let a=t.length,i=n.flatMap((d,g)=>rn(d)[0]>1e-5?[g]:[]),l=0,s=[],c=t.slice();if(i.length>0){let d=he(Array.from({length:a},(g,b)=>i.map(w=>n[w][b][0])));l=d.rank,s=d.pivot.slice(0,l).map(g=>i[g]),c=Ot(s.map(g=>n[g]),t).qty}let f=[];s.forEach((d,g)=>{let b=r[d],w=f.find(v=>v.term===b);w===void 0&&f.push(w={term:b,df:0,ss:[0,0]}),w.df++,w.ss=B(w.ss,oe(c[g],c[g]))});let m=a-l,u=rn(c,l)[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:b,ss:w})=>{let v=w[0]/b/d;return[b,w[0],w[0]/b,v,ae(v,b,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 Ls(e,t,n,r,o,a){if(a.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let i=n??{type:"num",value:1,text:"1"},l=Wo({response:t.response,rhs:i},Object.keys(o)).variables,s=ft(t,o,{...a,dropUnusedLevels:!0,contrastVariables:l},i),c=s.y;if(c===void 0)throw new TypeError("the formula has no response");let f=ft({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?he(f.x):void 0,d=p?.rank??0;d<h&&u.push("Error() model is singular");let g=p?.pivot.slice(0,d)??[],b=y=>y.map(x=>[x,0]),w=s.colNames.map((y,x)=>b(s.x.map(k=>k[x]))),{fit:v,qtv:N}=je(g.map(y=>b(f.x.map(x=>x[y]))),b(c),w),M=[];g.forEach((y,x)=>{let k=f.assign[y],q=k===0?"(Intercept)":f.terms.termLabels[k-1],S=M.find(L=>L.name===q);S===void 0&&M.push(S={name:q,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=>Ui(y.name,y.idx.map(x=>v.qty[x]),N.map(x=>y.idx.map(k=>x[k])),s.assign,s.terms.termLabels));return{formula:e,multistratum:!0,strata:E,rows:s.frame.rows,warnings:u}}function _r(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=Is(t.x,r,t.frame.weights,t.terms.intercept,!0),a=o.result,i=[...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)&&i.push("essentially perfect fit: summary may be unreliable")}let l={...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:i};return t.frame.weights!==void 0&&(l.weights=t.frame.weights),l}function Vr(e,t,n={}){return typeof e=="string"?_r(e,ft(xn(e),t,{...n,dropUnusedLevels:!0}),!1):Hi(e,t,n)}function Hi(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 i=n.weights===void 0?void 0:Array.from(n.weights);if(i!==void 0&&i.length!==a.length)throw new RangeError("variable lengths differ (found for '(weights)')");if(i?.some(m=>m<0))throw new RangeError("missing or negative weights not allowed");let l=a.map((m,u)=>!Number.isNaN(m)&&o[u].every(h=>!Number.isNaN(h))&&(i===void 0||!Number.isNaN(i[u]))),s=o.filter((m,u)=>l[u]).map(m=>r?[1,...m]:m),c=a.filter((m,u)=>l[u]),f=i?.filter((m,u)=>l[u]);return Is(s,c,f,r).result}function Bi(e,t,n,r,o,a){let i=o.rank,l=o.pivot.slice(0,i),s=jn(n===void 0?void 0:r.map(w=>n[w]),r.length),c=l.map(w=>r.map((v,N)=>Te(s[N],e[v][w]))),{qty:f,coef:m}=Ot(c,r.map((w,v)=>Te(s[v],t[w]))),u=e[0]?.map(()=>NaN)??[];l.forEach((w,v)=>u[w]=m[v][0]);let h=e.map(w=>{let v=[0,0];return l.forEach((N,M)=>v=B(v,Te(m[M],w[N]))),v}),p=rn(f,i)[0],d=r.map(w=>n?.[w]??1),g=[0,0];if(a){let w=[0,0],v=[0,0];r.forEach((N,M)=>{w=B(w,[d[M],0]),v=B(v,Te(h[N],d[M]))}),g=ge(v,w)}let b=[0,0];return r.forEach((w,v)=>{let N=le(h[w],g);b=B(b,Te(oe(N,N),d[v]))}),{coefficients:u,fittedValues:h.map(w=>w[0]),residuals:h.map((w,v)=>le([t[v],0],w)[0]),effects:f.map(w=>w[0]),rss:p,mss:b[0]}}function Is(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),i=e.filter((D,G)=>a[G]),l=t.filter((D,G)=>a[G]),s=n?.filter((D,G)=>a[G]),c=l.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?i:i.map((D,G)=>D.map(Q=>Q*m[G])),h=m===void 0?l:l.map((D,G)=>D*m[G]),p=he(u),d=p.rank,g=o?Bi(e,t,n,f,p,r):void 0,b=g?.coefficients??Tt(p,h),w=g?.residuals??[],v=g?.fittedValues??[],N=c-d,M=s??new Array(c).fill(1),E=g?.rss??0;if(g===void 0){let D=kn(p,h),G=m===void 0?D:D.map((ne,de)=>ne/m[de]),Q=0;t.forEach((ne,de)=>{if(a[de])w.push(G[Q]),v.push(ne-G[Q]),Q++;else{let ve=e[de].reduce((Oe,Fe,Le)=>Number.isNaN(b[Le])?Oe:Oe+Fe*b[Le],0);v.push(ve),w.push(ne-ve)}});for(let ne=0;ne<c;ne++)E+=M[ne]*G[ne]**2}let y=v.filter((D,G)=>a[G]),x=E/N,k=Math.sqrt(x),q=p.qr.slice(0,d).map(D=>D.slice(0,d)),S=d>0?Rn(q):[],A=p.pivot.slice(0,d).map((D,G)=>[D,G]).sort((D,G)=>D[0]-G[0]),R=A.map(([D])=>b[D]),C=A.map(([,D])=>Math.sqrt(S[D][D]*x)),F=R.map((D,G)=>D/C[G]),I=F.map(D=>2*we(-Math.abs(D),N,!0,!1)),V={coefficients:b,residuals:w,fittedValues:v,rank:d,dfResidual:N,coefTable:{estimate:R,stdError:C,tValue:F,pValue:I},sigma:k,rSquared:0,adjRSquared:0,intercept:r,qr:p},z=r?1:0,J=d-z,T={result:V,fitRows:f,effects:g?.effects??[],rss:E};if(J===0)return T;let O=0;if(g!==void 0)O=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++)O+=M[Q]*(y[Q]-G)**2}else for(let D=0;D<c;D++)O+=M[D]*y[D]**2;V.rSquared=O/(O+E),V.adjRSquared=1-(1-V.rSquared)*((c-z)/N);let $=O/J/x;return V.fStatistic={value:$,numdf:J,dendf:N},V.fPValue=ae($,J,N,!1,!1),T}function Xi(e,t={}){let n=t.level??.95,r=Pe((1+n)/2,e.dfResidual,!0,!1),{estimate:o,stdError:a}=e.coefTable,i=e.coefficients.map(()=>NaN),l=e.coefficients.map(()=>NaN),s=0;return e.coefficients.forEach((c,f)=>{Number.isNaN(c)||(i[f]=o[s]-r*a[s],l[f]=o[s]+r*a[s],s++)}),{lower:i,upper:l}}function Qi(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,i=e.qr.pivot.slice(0,a),l=e.qr.qr.slice(0,a).map(p=>p.slice(0,a)),s=e.sigma**2,c=o.map(p=>p.reduce((d,g,b)=>Number.isNaN(e.coefficients[b])?d:d+g*e.coefficients[b],0)),f=o.map(p=>{let d=new Array(a).fill(0);for(let g=0;g<a;g++){let b=p[i[g]];for(let w=0;w<g;w++)b-=l[w][g]*d[w];d[g]=b/l[g][g]}return Math.sqrt(d.reduce((g,b)=>g+b*b,0)*s)}),m=n.interval??"none";if(m==="none")return{fit:c,seFit:f};let u=Pe((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 Os(e){let t=e.flat(),n=Zt(t),r=[0,0],o=[0,0],a=[];for(let i of e){let l=Zt(i);a.push(l[0]);let s=le(l,n);r=B(r,oe(oe(s,s),[i.length,0]));for(let c of i){let f=le([c,0],l);o=B(o,oe(f,f))}}return{ssb:r[0],ssw:o[0],means:a}}function Cr(e,t,n={}){return typeof e=="string"?Ki(e,t,n):Zi(e,t)}function Ki(e,t,n){let r=xn(e,!0),{treatment:o,errors:a}=zo(r.rhs);if(a.length>1)throw new Error(`there are ${a.length} Error terms: only 1 is allowed`);return a.length===1?Ls(e,r,o,a[0],t,n):_r(e,ft(r,t,{...n,dropUnusedLevels:!0},o),!0)}function Zi(e,t){let n=ht(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:i}=Os(n),l=r-1,s=o-r,c=a/l,f=i/s,m=c/f;return{df:[l,s],sumSq:[a,i],meanSq:[c,f],fValue:m,pValue:s>0?ae(m,l,s,!1,!1):NaN}}function Yi(e,t,n={}){let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:a}=ut(e,r),i=a.length;if(i<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let l=n.confLevel??.95,c=a.reduce((d,g)=>d+g.length,0)-i,{ssw:f,means:m}=Os(a),u=f/c,h=$o(l,i,c),p={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:[]};for(let d=0;d<i-1;d++)for(let g=d+1;g<i;g++){let b=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(b),p.lwr.push(b-h*w),p.upr.push(b+h*w),p.pAdj.push(Nn(Math.abs(b)/w,i,c,{lowerTail:!1}))}return p}function Ji(e,t,n={}){let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:a}=n,i=n.mu??0;if(o===void 0&&r===void 0)throw new RangeError("sigmaX (the known standard deviation of x) is required");if(o===void 0||r!==void 0&&a===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");if(Number.isNaN(i))throw new RangeError("mu must be a number");let l=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(v=>!Number.isNaN(v)),f=r===void 0?void 0:Array.from(r).filter(v=>!Number.isNaN(v));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=Zt(c)[0],u=f===void 0?NaN:Zt(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-i)/h,g,b;if(l==="less")g=P(d),b=[-1/0,p+H(s)*h];else if(l==="greater")g=P(d,{lowerTail:!1}),b=[p-H(s)*h,1/0];else{g=2*P(-Math.abs(d));let v=H(1-(1-s)/2)*h;b=[p-v,p+v]}let w=f!==void 0;return{method:w?"Two-sample z-Test":"One-sample z-Test",alternative:l,statistic:d,statisticName:"z",pValue:g,confInt:b,confLevel:s,estimate:w?[m,u]:m,estimateNames:w?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:w?"difference in means":"mean"}}var Ee=2**-52,el={gaussian:["identity","log","inverse"],binomial:["logit","probit","cauchit","log","cloglog"],quasibinomial:["logit","probit","cauchit","log","cloglog"],poisson:["log","identity","sqrt"],quasipoisson:["log","identity","sqrt"],Gamma:["inverse","identity","log"],"inverse.gaussian":["1/mu^2","inverse","identity","log"],quasi:["identity","logit","probit","cloglog","inverse","log","1/mu^2","sqrt"]},Dr=e=>Number.isFinite(e)&&e>0,Ft=()=>!0,Ne=class{list=[];add(t){this.list.includes(t)||this.list.push(t)}},zn;function Wn(e,t){let n=zn;zn=e;try{return t()}finally{zn=n}}function tl(e){return e<0&&zn?.add("NaNs produced"),Math.sqrt(e)}function $r(e){switch(e){case"identity":return{name:e,link:t=>t,linkinv:t=>t,muEta:()=>1,etaOk:Ft};case"log":return{name:e,link:Math.log,linkinv:t=>Math.max(Math.exp(t),Ee),muEta:t=>Math.max(Math.exp(t),Ee),etaOk:Ft};case"inverse":return{name:e,link:t=>1/t,linkinv:t=>1/t,muEta:t=>-1/(t*t),etaOk:t=>Number.isFinite(t)&&t!==0};case"sqrt":return{name:e,link:Math.sqrt,linkinv:t=>t*t,muEta:t=>2*t,etaOk:Dr};case"1/mu^2":return{name:e,link:t=>1/(t*t),linkinv:t=>1/tl(t),muEta:t=>-1/(2*t**1.5),etaOk:Dr};case"logit":return{name:e,link:t=>Math.log(t/(1-t)),linkinv:t=>{let n=t<-30?Ee:t>30?1/Ee:Math.exp(t);return n/(1+n)},muEta:t=>{if(Math.abs(t)>30)return Ee;let n=Math.exp(t);return n/((1+n)*(1+n))},etaOk:Ft};case"probit":{let t=-H(Ee);return{name:e,link:n=>H(n),linkinv:n=>P(Math.min(Math.max(n,-t),t)),muEta:n=>Math.max(ye(n),Ee),etaOk:Ft}}case"cauchit":{let t=-fr(Ee,0,1,!0,!1);return{name:e,link:n=>fr(n,0,1,!0,!1),linkinv:n=>_o(Math.min(Math.max(n,-t),t),0,1,!0,!1),muEta:n=>Math.max(Fo(n,0,1,!1),Ee),etaOk:Ft}}case"cloglog":return{name:e,link:t=>Math.log(-Math.log(1-t)),linkinv:t=>Math.min(Math.max(-Math.expm1(-Math.exp(t)),Ee),1-Ee),muEta:t=>{let n=Math.min(t,700);return Math.max(Math.exp(n)*Math.exp(-Math.exp(n)),Ee)},etaOk:Ft};default:throw new RangeError(`unknown link "${String(e)}"`)}}function nl(e){if(!Number.isFinite(e))throw new RangeError("invalid power for the power link");if(e<=0)return $r("log");if(e===1)return $r("identity");let t=1/e;return{name:`mu^${Math.round(e*1e3)/1e3}`,link:n=>n**e,linkinv:n=>Math.max(n**t,Ee),muEta:n=>Math.max(t*n**(t-1),Ee),etaOk:Dr}}function rl(e){switch(e){case"constant":return{variance:()=>1,muOk:()=>!0,dev:(t,n,r)=>r*(t-n)**2};case"mu(1-mu)":return{variance:t=>t*(1-t),muOk:t=>t>0&&t<1,dev:Fs};case"mu":return{variance:t=>t,muOk:t=>t>0,dev:(t,n,r)=>2*r*((t===0?0:t*Math.log(t/n))-(t-n))};case"mu^2":return{variance:t=>t*t,muOk:t=>t>0,dev:(t,n,r)=>Math.max(-2*r*(Math.log(t>0?t/n:1/n)-(t-n)/n),0)};case"mu^3":return{variance:t=>t*t*t,muOk:t=>t>0,dev:_s};default:throw new RangeError(`'variance' "${String(e)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`)}}function Fs(e,t,n){let r=e===0?0:e*Math.log(e/t),o=e===1?0:(1-e)*Math.log((1-e)/(1-t));return 2*n*(r+o)}function _s(e,t,n){return n*(e-t)**2/(e*t*t)}function on(e,t){let n=typeof e=="string"?{family:e}:e,r=n.family,o=el[r];if(o===void 0)throw new RangeError(`unknown family "${String(r)}"`);let a=t??n.link??o[0],i;if(typeof a=="object"){if(r!=="quasi")throw new RangeError(`link "power" not available for ${r} family`);i=nl(a.power)}else{if(!o.includes(a))throw new RangeError(`link "${a}" not available for ${r} family; available links are ${o.map(s=>`'${s}'`).join(", ")}`);i=$r(a)}if(n.variance!==void 0&&r!=="quasi")throw new TypeError("'variance' is only used by the quasi family");let l={...i,family:r,varianceName:void 0,fixedDispersion:!1};switch(r){case"gaussian":return{...l,variance:()=>1,muOk:()=>!0,devContribution:(s,c,f)=>f*(s-c)**2};case"binomial":case"quasibinomial":return{...l,fixedDispersion:r==="binomial",variance:s=>s*(1-s),muOk:s=>Number.isFinite(s)&&s>0&&s<1,devContribution:Fs};case"poisson":case"quasipoisson":return{...l,fixedDispersion:r==="poisson",variance:s=>s,muOk:s=>Number.isFinite(s)&&s>0,devContribution:(s,c,f)=>s>0?2*f*(s*Math.log(s/c)-(s-c)):2*f*c};case"Gamma":return{...l,variance:s=>s*s,muOk:s=>Number.isFinite(s)&&s>0,devContribution:(s,c,f)=>-2*f*((s===0?0:Math.log(s/c))-(s-c)/c)};case"inverse.gaussian":return{...l,variance:s=>s*s*s,muOk:()=>!0,devContribution:_s};case"quasi":{let s=n.variance??"constant",c=rl(s);return{...l,varianceName:s,variance:c.variance,muOk:c.muOk,devContribution:c.dev}}}}function Pr(e,t,n,r,o){let a=e.family,i=a==="binomial"||a==="quasibinomial";if(t.length>0&&Array.isArray(t[0])){let f=t;if(f.every(d=>d.length===1))return Pr(e,f.map(d=>d[0]),n,r,o);if(!i)throw new TypeError("y must be a vector (or, for the binomial families, a two-column matrix)");if(f.some(d=>d.length!==2))throw new TypeError(`for the '${a}' family, y must be a vector of 0 and 1's
3
- or a 2 column matrix where col 1 is no. successes and col 2 is no. failures`);a==="binomial"&&f.some(d=>Math.abs(d[0]-Math.round(d[0]))>.001||Math.abs(d[1]-Math.round(d[1]))>.001)&&o.add("non-integer counts in a binomial glm!");let m=f.map(d=>d[0]+d[1]),u=f.map((d,g)=>m[g]===0?0:d[0]/m[g]),h=n.map((d,g)=>d*m[g]),p=u.map((d,g)=>(m[g]*d+.5)/(m[g]+1));return{y:u,weights:h,mustart:p,size:m}}let s=t.slice(),c=n.slice();switch(a){case"binomial":case"quasibinomial":{for(let m=0;m<s.length;m++)c[m]===0&&(s[m]=0);if(s.some(m=>m<0||m>1))throw new RangeError("y values must be 0 <= y <= 1");let f=s.map((m,u)=>(c[u]*m+.5)/(c[u]+1));return a==="binomial"&&s.some((m,u)=>Math.abs(c[u]*m-Math.round(c[u]*m))>.001)&&o.add("non-integer #successes in a binomial glm!"),{y:s,weights:c,mustart:f,size:s.map(()=>1)}}case"poisson":case"quasipoisson":if(s.some(f=>f<0))throw new RangeError(`negative values not allowed for the '${a==="poisson"?"Poisson":"quasiPoisson"}' family`);return{y:s,weights:c,mustart:s.map(f=>f+.1),size:void 0};case"Gamma":if(s.some(f=>f<=0))throw new RangeError("non-positive values not allowed for the 'Gamma' family");return{y:s,weights:c,mustart:s.slice(),size:void 0};case"inverse.gaussian":if(s.some(f=>f<=0))throw new RangeError("positive values only are allowed for the 'inverse.gaussian' family");return{y:s,weights:c,mustart:s.slice(),size:void 0};case"gaussian":if(!r&&(e.name==="inverse"&&s.some(f=>f===0)||e.name==="log"&&s.some(f=>f<=0)))throw new Error("cannot find valid starting values: please specify some");return{y:s,weights:c,mustart:s.slice(),size:void 0};case"quasi":{let f=e.varianceName,m=f==="constant"?s.slice():f==="mu(1-mu)"?s.map(u=>Math.min(Math.max(u,.001),.999)):s.map(u=>u+(u===0?.1:0));return{y:s,weights:c,mustart:m,size:void 0}}}}function Vs(e,t,n,r,o,a,i){let l=t.length,s=0;switch(e.family){case"gaussian":{let c=0;for(let f of r)c+=Math.log(f);return l*(Math.log(2*Math.PI*a/l)+1)+2-c}case"binomial":{let c=o!==void 0&&o.some(f=>f>1)?o:r;for(let f=0;f<l;f++)c[f]>0&&(s+=r[f]/c[f]*xt(Xt(c[f]*t[f]),Xt(c[f]),n[f],!0));return-2*s}case"poisson":for(let c=0;c<l;c++)Oo(t[c])&&i.add(`non-integer x = ${t[c].toFixed(6)}`),s+=r[c]*Qt(t[c],n[c],!0);return-2*s;case"Gamma":{let c=0;for(let m of r)c+=m;let f=a/c;for(let m=0;m<l;m++)s+=r[m]*Lo(t[m],1/f,n[m]*f,!0);return-2*s+2}case"inverse.gaussian":{let c=0,f=0;for(let m=0;m<l;m++)c+=r[m],f+=r[m]*Math.log(t[m]);return c*(Math.log(2*Math.PI*a/c)+1)+3*f+2}default:return NaN}}var _t=2**-52;function ol(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function Gn(e,t,n){return e.map((r,o)=>{let a=0;for(let i=0;i<r.length;i++)a+=r[i]*t[i];return a+n[o]})}function dt(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 Cs(e,t){let{x:n,p:r,y:o,w:a,offset:i,fam:l,epsilon:s,maxit:c}=e,f=o.length;return Wn(t,()=>{let m=(L,A)=>L.every(R=>l.etaOk(R))&&A.every(R=>l.muOk(R));if(r===0){let L=i.slice();if(!L.every(F=>l.etaOk(F)))throw new Error("invalid linear predictor values in empty model");let A=L.map(F=>l.linkinv(F));if(!A.every(F=>l.muOk(F)))throw new Error("invalid fitted means in empty model");let R=L.map(F=>l.muEta(F)),C=A.map((F,I)=>Math.sqrt(a[I]*R[I]**2/l.variance(F))**2);return{coefficients:[],eta:L,mu:A,deviance:dt(l,o,A,a),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:a.map(F=>F>0),workingWeights:C,workingResiduals:o.map((F,I)=>(F-A[I])/R[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 ${ol(e.columnNames)}`);h=e.start.slice(),u=Gn(n,h,i)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(L=>l.link(L));let p=u.map(L=>l.linkinv(L));if(!m(u,p))throw new Error("cannot find valid starting values: please specify some");let d=dt(l,o,p,a),g=d,b=!1,w=!1,v=0,N=new Array(r).fill(0),M,E=a.map(()=>!1),y=[],x=Math.min(1e-7,s/1e3);for(v=1;v<=c;v++){let L=u.map(I=>l.muEta(I));for(let I=0;I<f;I++){if(!(a[I]>0))continue;let V=l.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 A=[];for(let I=0;I<f;I++)E[I]&&A.push(I);if(A.length===0){t.add(`no observations informative at iteration ${v}`),b=!1;break}y=new Array(f).fill(0);let R=[],C=[];for(let I of A){let V=Math.sqrt(a[I]*L[I]**2/l.variance(p[I]));y[I]=V,R.push((u[I]-i[I]+(o[I]-p[I])/L[I])*V),C.push(n[I].map(z=>z*V))}M=he(C,{tol:x});let F=Tt(M,R);if(F.some((I,V)=>M.pivot.indexOf(V)<M.rank&&!Number.isFinite(I))){t.add(`non-finite coefficients at iteration ${v}`),b=!1;break}if(N=F.map(I=>Number.isNaN(I)?0:I),w=!1,u=Gn(n,N,i),p=u.map(I=>l.linkinv(I)),g=dt(l,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=Gn(n,N,i),p=u.map(V=>l.linkinv(V)),g=dt(l,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=Gn(n,N,i),p=u.map(V=>l.linkinv(V))}w=!0,g=dt(l,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*_t*I)){b=!0;break}}else if(Math.abs(g-d)/(Math.abs(g)+.1)<s){b=!0;break}d=g,h=N.slice()}v>c&&(v=c);let k=r,q=N.slice();if(M!==void 0){k=M.rank;for(let L=M.rank;L<r;L++)q[M.pivot[L]]=NaN}let S=u.map(L=>l.muEta(L));return{coefficients:q,eta:u,mu:p,deviance:g,iter:v,converged:b,boundary:w,rank:k,qr:M,used:E,workingWeights:y.map(L=>L*L),workingResiduals:o.map((L,A)=>(L-p[A])/S[A])}})}function Ds(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function $s(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*_t||o<10*_t)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*_t)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function Vt(e,t,n,r={}){let o=on(e.familySpec),a="start"in r?r.start:e.start,i=Pr(o,e.yGiven,e.priorGiven,a!==void 0||r.etastart!==void 0,n),l=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=Cs({x:s,p:t.length,y:i.y,w:i.weights,offset:l,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:i.mustart,untilCoefficients:r.untilCoefficients},n);return $s(c,o,n,t.length===0),{fit:c,fam:o,y:i.y,w:i.weights,size:i.size,offset:l}}function sl(e,t,n={}){let r=n.intercept??!0,o=Ds(e),i=t.length>0&&typeof t[0]!="number"?t.map(C=>Array.from(C)):Array.from(t),l=i.length;if(o.length!==l)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!==l)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!==l)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((C,F)=>{let I=i[F];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[F]))&&!(c!==void 0&&Number.isNaN(c[F]))}),m=o.filter((C,F)=>f[F]).map(C=>r?[1,...C]:C),u=i.filter((C,F)=>f[F]),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,F)=>f[F]);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,F)=>f[F]),g=o.length>0?o[0].length:0,b=[...r?["(Intercept)"]:[],...Array.from({length:g},(C,F)=>`x${F+1}`)],w=b.length;if(m.some(C=>C.length!==w))throw new TypeError("all rows of x must have the same length");let v=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(v.link=n.link);let N={x:m,columnNames:b,yGiven:u,priorGiven:p,offset:d,hasOffset:c!==void 0,intercept:r,familySpec:v,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 Ne,E=Array.from({length:w},(C,F)=>F),{fit:y,fam:x,y:k,w:q,size:S}=Vt(N,E,M),L;if(r&&N.hasOffset){let C=new Ne,F=Cs({x:m.map(()=>[1]),p:1,y:k,w:q,offset:d,fam:x,epsilon:N.epsilon,maxit:N.maxit,columnNames:["(Intercept)"],mustart:y.mu},C);$s(F,x,C,!1),C.list.forEach(I=>M.add(I)),L=F.deviance,F.converged||M.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let C=0,F=0;for(let V=0;V<h;V++)C+=q[V],F+=q[V]*k[V];let I=F/C;L=dt(x,k,k.map(()=>I),q)}else L=dt(x,k,Wn(M,()=>d.map(C=>x.linkinv(C))),q);let A=h-q.filter(C=>C===0).length,R=Vs(x,k,y.mu,q,S,y.deviance,M)+2*y.rank;return{coefficients:y.coefficients,fittedValues:y.mu,linearPredictors:y.eta,deviance:y.deviance,nullDeviance:L,aic:R,rank:y.rank,dfResidual:A-y.rank,dfNull:A-(r?1:0),iter:y.iter,converged:y.converged,boundary:y.boundary,y:k,priorWeights:q,weights:y.workingWeights,residuals:y.workingResiduals,family:x.family,link:x.name,warnings:M.list,qr:y.qr,model:N}}function Un(e){return on(e.model.familySpec)}function Ct(e,t){if(Un(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 al(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 Rn(t)}function jr(e,t={}){let n=new Ne,r=Un(e),o=t.dispersion===void 0&&!r.fixedDispersion,a=t.dispersion??Ct(e,n),i=al(e),s=(e.qr===void 0?[]:e.qr.pivot.slice(0,e.rank)).map(h=>e.coefficients[h]),c=i.map((h,p)=>Math.sqrt(h[p]*a)),f=s.map((h,p)=>h/c[p]),m=!o,u=f.map(h=>m?2*P(-Math.abs(h)):e.dfResidual>0?2*we(-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:i,covScaled:i.map(h=>h.map(p=>p*a)),dfResidual:e.dfResidual,warnings:n.list}}function zr(e,t="deviance"){let n=Un(e),{y:r,fittedValues:o,priorWeights:a}=e;switch(t){case"response":return r.map((i,l)=>i-o[l]);case"working":return e.residuals.slice();case"pearson":return r.map((i,l)=>(i-o[l])*Math.sqrt(a[l])/Math.sqrt(n.variance(o[l])));case"deviance":return e.dfResidual<=0?r.map(()=>0):r.map((i,l)=>Math.sign(i-o[l])*Math.sqrt(Math.max(n.devContribution(i,o[l],a[l]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function Wr(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let i=n.qr.length,l=Array.from({length:i},(c,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=Ko(n,l).map(c=>c.reduce((f,m)=>f+m*m,0))}let o=0,a=e.weights.map(i=>i>0);return e.priorWeights.forEach((i,l)=>{let s=a[l]?r[o++]??0:0;i>0&&t.push(s>1-10*_t?1:s)}),t}function il(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 ll(e,t={}){let n=Wr(e),r=t.dispersion??Ct(e),o=zr(e,"deviance").filter((i,l)=>e.priorWeights[l]>0),a=100*_t*il(o.map(Math.abs));return o.map((i,l)=>{let s=(Math.abs(i)<a?0:i)/Math.sqrt(r*(1-n[l]));return Number.isFinite(s)?s:NaN})}function cl(e,t={}){let n=Wr(e),r=t.dispersion??Ct(e);return zr(e,"pearson").filter((a,i)=>e.priorWeights[i]>0).map((a,i)=>{let l=(a/(1-n[i]))**2*(n[i]/(r*e.rank));return Number.isFinite(l)?l:NaN})}function fl(e,t={}){let n=Un(e),r,o;if(t.newdata===void 0)r=e.model.x,o=e.linearPredictors.slice();else{r=Ds(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??Ct(e),i=e.rank,l=e.qr===void 0?[]:e.qr.pivot.slice(0,i),s=e.qr?.qr,c=r.map(f=>{let m=new Array(i).fill(0),u=0;for(let h=0;h<i;h++){let p=f[l[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}:Wn(new Ne,()=>({fit:o.map(f=>n.linkinv(f)),seFit:c.map((f,m)=>f*Math.abs(n.muEta(o[m])))}))}var Ps={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Gr(e,t,n,r,o=1e-11){let a=[];for(let d=0;d<t.length;d++)n[d]>0&&a.push(d);let i=0,l=0;for(let d of a)i+=n[d],l+=n[d]*t[d];let s=r?l/i: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=he(a.map((d,g)=>e[d].map(b=>b*m[g])),{tol:o}),h=kn(u,a.map((d,g)=>t[d]*m[g])),p=0;for(let d of h)p+=d*d;return c-p}function ml(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 i=e.model,l=e.coefficients.map((E,y)=>y).filter(E=>!Number.isNaN(e.coefficients[E])),s=new Ne,c=Vt(i,l,s,{...Ps,start:i.start===void 0?void 0:l.map(E=>i.start[E])}),f=new Array(o).fill(NaN);l.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=jr(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[l[E]]=u.stdError[y]);let g=i.x.map(E=>l.map(y=>E[y])),b=c.fit.eta,w=(E,y)=>{let x=l.filter(S=>S!==E),k=Vt(i,x,new Ne,{...Ps,start:void 0,etastart:b,extraOffset:i.x.map(S=>S[E]*y)}).fit,q=r==="LRT"?k.deviance-c.fit.deviance:Gr(g,k.workingResiduals,k.workingWeights,!1);return Math.sign(y-f[E])*Math.sqrt(Math.max(q/h,0))},v=[H((1-n)/2),H((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 k=x===0?-1:1,q=NaN,S=NaN;for(let V=0;V<=30;V++){let z=f[E]+k*2**V*p[E],J=w(E,z);if(Math.abs(J)>=Math.abs(v[x])){q=z,S=J;break}}if(Number.isNaN(q))continue;let L=V=>V===f[E]?-v[x]:w(E,V)-v[x],[A,R]=x===0?[q,f[E]]:[f[E],q],[C,F]=x===0?[S-v[x],-v[x]]:[-v[x],S-v[x]],I=xe(L,A,R,{tol:1e-14*p[E],maxIter:500,fLower:C,fUpper:F});x===0?N[y]=I:M[y]=I}}),{lower:N,upper:M}}var ul=(e,t)=>me(e,t/2,2,!1,!1);function hl(e,t,n){let{fit:r,w:o}=Vt(e,t,n);return{dfRes:o.filter(i=>i>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(i=>t.map(l=>i[l])),intercept:e.intercept}}var Ur=(e,t)=>Gr(t.x,e.fit.workingResiduals,e.fit.workingWeights,t.intercept);function pl(e,t={}){let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=new Ne,o=Array.isArray(e)&&n.length>1,a=n[0],i=on(a.model.familySpec),l=i.fixedDispersion,s=t.test===void 0?t.dispersion!==void 0||l?"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,k)=>c.push({name:String(k+1),dfRes:x.dfResidual,dev:x.deviance})),f=c.map((x,k)=>k===0?NaN:c[k-1].dfRes-x.dfRes),m=c.map((x,k)=>k===0?NaN:c[k-1].dev-x.dev),s==="Rao"){let x=n.map(k=>({dfRes:k.dfResidual,dev:k.deviance,fit:js(k),x:k.model.x,intercept:k.model.intercept}));u=c.map((k,q)=>q===0?NaN:f[q]<0?-Ur(x[q],x[q-1]):Ur(x[q-1],x[q]))}h=n.reduce((x,k)=>k.dfResidual<x.dfResidual?k:x)}else{let y=a,x=y.model,k=x.x.length>0?x.x[0].length-(x.intercept?1:0):0,q=x.intercept?[0]:[],S=A=>[...q,...Array.from({length:A},(R,C)=>C+q.length)],L=[];for(let A=0;A<=k;A++)A===k?L.push({dfRes:y.dfResidual,dev:y.deviance,fit:js(y),x:x.x,intercept:x.intercept}):A>0||s==="Rao"?L.push(hl(x,S(A),r)):L.push(void 0);c.push({name:"NULL",dfRes:y.dfNull,dev:y.nullDeviance});for(let A=1;A<=k;A++)c.push({name:`x${A}`,dfRes:L[A].dfRes,dev:L[A].dev});f=c.map((A,R)=>R===0?NaN:c[R-1].dfRes-A.dfRes),m=c.map((A,R)=>R===0?NaN:Math.max(c[R-1].dev-A.dev,0)),s==="Rao"&&(u=c.map((A,R)=>R===0?NaN:Ur(L[R-1],L[R]))),h=y}let p=new Ne,d=t.dispersion??Ct(h,p);p.list.forEach(y=>r.add(y));let g=l?1/0:h.dfResidual,b=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]),v=[...b],N=y=>c.map((x,k)=>{if(k===0||f[k]===0)return NaN;let q=y[k]/d*Math.sign(f[k]);return q<0||Number.isNaN(q)?NaN:ul(q,Math.abs(f[k]))}),M=[];if(s==="Chisq"||s==="LRT")v.push("Pr(>Chi)"),M=[N(m)];else if(s==="Rao")v.push("Rao","Pr(>Chi)"),M=[u,N(u)];else if(s==="F"){if(l&&r.add(`using F test with a '${i.family}' family is inappropriate`),g!==0){let y=c.map((x,k)=>{let q=m[k]/f[k]/d;return k===0||f[k]===0||q<0?NaN:q});v.push("F","Pr(>F)"),M=[y,y.map((x,k)=>Number.isNaN(x)?NaN:ae(x,Math.abs(f[k]),g,!1,!1))]}}else if(s==="Cp"){let y=h.model.x.length;v.push("Cp"),M=[c.map(x=>x.dev+2*d*(y-x.dfRes))]}let E=w.map((y,x)=>[...y,...M.map(k=>k[x])]);return{colNames:v,rowNames:c.map(y=>y.name),table:E,warnings:r.list}}function js(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 dl(e){let{pivot:t}=e.qr,n=[],r=new Map;for(let o=0;o<e.rank;o++){let a=e.assign[t[o]],i=r.get(a);i===void 0&&(i={term:a,df:0,ss:0},r.set(a,i),n.push(i)),i.df++,i.ss+=e.effects[o]**2}return{terms:n,rdf:e.dfResidual,rss:e.rss}}var zs=(e,t,n)=>ae(e,t,n,!1,!1);function gl(e){let t=dl(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 Hr(e){let{names:t,rows:n,rdf:r,rss:o,perfect:a}=gl(e),i=o/r,l=n.map(({df:s,ss:c})=>{let f=c/s/i;return[s,c,c/s,f,zs(f,s,r)]});return l.push([r,o,r===0?NaN:i,NaN,NaN]),{colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...t,"Residuals"],table:l,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function wl(e){let t=e.map(i=>i.dfResidual),n=e.map(i=>i.rss),r=0;t.forEach((i,l)=>{i<t[r]&&(r=l)});let o=n[r]/t[r],a=e.map((i,l)=>{if(l===0)return[t[0],n[0],NaN,NaN,NaN,NaN];let s=t[l-1]-t[l],c=n[l-1]-n[l],f=s===0?NaN:c/s/o;return f<0&&(f=NaN),[t[l],n[l],s,c,f,Number.isNaN(f)?NaN:zs(f,Math.abs(s),t[r])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((i,l)=>String(l+1)),table:a,warnings:[]}}function Br(e,...t){let n=[e,...t];for(let i of n)if(typeof i!="object"||i===null||!Array.isArray(i.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1)return Hr(e);if(n.some(i=>i.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=n.filter(i=>i.response===e.response),o=[];if(r.length<n.length){let i=[...new Set(n.filter(l=>l.response!==e.response).map(l=>l.response))];o.push(`models with response ${i.map(l=>`\u2018${l}\u2019`).join(", ")} removed because response differs from model 1`)}let a=r.length===1?Hr(e):wl(r);return a.warnings=[...o,...a.warnings],a}function Xr(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=Hr(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 Ws(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])),i=he(o.map((u,h)=>r.map(p=>e.x[u][p]*a[h]))),l=jn(e.weights===void 0?void 0:o.map(u=>e.weights[u]),o.length),s=i.pivot.slice(0,i.rank),{qty:c}=Ot(s.map(u=>o.map((h,p)=>Te(l[p],e.x[h][r[u]]))),o.map((u,h)=>Te(l[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 Qr(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 i=[],l=e.termLabels.length;r===2&&l===0&&e.intercept&&(i.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(b=>b!==p)));f.push(p===0?"(Intercept)":e.termLabels[p-1]),m.push({ss:Ws(e,g,d).ss,df:d.length})}}else{let p=d=>e.factors.flatMap((g,b)=>g[d-1]>0?[b]:[]);for(let d of c){if(d===0)continue;let g=p(d),b=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&&!b.includes(N)))),v=Ws(e,w,s(new Set([d])));f.push(e.termLabels[d-1]),m.push(v)}}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:i}}var Hn=[0,0];var Gs=e=>e.map(t=>t.map(n=>n[0]));function sn(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Hn);return r.forEach((a,i)=>{let l=t[i];for(let s=0;s<n;s++)o[s]=B(o[s],oe(a,l[s]))}),o})}function gt(e){let t=Hn;return e.forEach((n,r)=>t=B(t,n[r])),t}function Us(e){let t=e.length,n=e.map(i=>i.slice()),r=Array.from({length:t},(i,l)=>l),o=1,a=!1;for(let i=0;i<t;i++){let l=i;for(let s=i+1;s<t;s++)Math.abs(n[s][i][0])>Math.abs(n[l][i][0])&&(l=s);if(n[l][i][0]===0){a=!0;continue}l!==i&&([n[i],n[l]]=[n[l],n[i]],[r[i],r[l]]=[r[l],r[i]],o=-o);for(let s=i+1;s<t;s++){let c=ge(n[s][i],n[i][i]);n[s][i]=c;for(let f=i+1;f<t;f++)n[s][f]=le(n[s][f],oe(c,n[i][f]))}}return{a:n,perm:r,sign:o,singular:a}}function Hs(e){let{a:t,sign:n,singular:r}=Us(e);if(r)return Hn;let o=[n,0];return t.forEach((a,i)=>o=oe(o,a[i])),o}function Bs(e){let t=e.length,{a:n,perm:r,singular:o}=Us(e);if(o)throw new Error("singular matrix");let a=Array.from({length:t},()=>new Array(t).fill(Hn));for(let i=0;i<t;i++){let l=r.map(s=>[s===i?1:0,0]);for(let s=0;s<t;s++)for(let c=0;c<s;c++)l[s]=le(l[s],oe(n[s][c],l[c]));for(let s=t-1;s>=0;s--){for(let c=s+1;c<t;c++)l[s]=le(l[s],oe(n[s][c],l[c]));l[s]=ge(l[s],n[s][s])}for(let s=0;s<t;s++)a[s][i]=l[s]}return a}var Xs=(e,t)=>me(e,t/2,2,!1,!1);function Kr(e){let t=e.length,n=Bt(gt(e),t),r=[1,0];for(let o=0;o<t;o++)r=oe(r,n);return n=ge(Hs(e),r),n[0]}function Zr(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),i=-n*o*Math.log(e),l=t*(t+1)/2-1,s=Xs(i,l),c=Xs(i,l+4);return s+a*(c-s)}var vl=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function bl(e,t,n){let r=e.length,o=b=>b.length===0?0:he(Array.from({length:r},(w,v)=>b.map(N=>N[v]))).rank,a=n.length===0?void 0:he(Array.from({length:r},(b,w)=>n.map(v=>v[w]))),i=a===void 0?[]:a.pivot.slice(0,a.rank).map(b=>n[b]),l=i.length,s=[...i,...t],c=he(Array.from({length:r},(b,w)=>s.map(v=>v[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(b=>s[b]),m=b=>b.map(w=>[w,0]),u=new Array(r).fill([0,0]),h=je(f.map(m),u,e.map((b,w)=>e.map(v=>v[w]))).qtv,p=Array.from({length:r},(b,w)=>h.map(v=>v[w])),d=je(f.map(m),u,p).qtv,g=Array.from({length:c.rank-l},(b,w)=>l+w);return g.map(b=>g.map(w=>d[b][w]))}function Nl(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 i=a?.map(String)??n.map(()=>""),l=[...new Set(i)].sort(lt),s=n.map(w=>w.map(()=>[0,0]));for(let w of l){let v=i.flatMap((N,M)=>N===w?[M]:[]);for(let N=0;N<o;N++){let M=[0,0];for(let y of v)M=B(M,[n[y][N],0]);let E=Bt(M,v.length);for(let y of v)s[y][N]=le([n[y][N],0],E)}}let c=r-l.length,f=Array.from({length:o},(w,v)=>Array.from({length:o},(N,M)=>{let E=[0,0];for(let y of s)E=B(E,oe(y[v],y[M]));return E})),m=t.idata??{index:Array.from({length:o},(w,v)=>v+1)},u=w=>{let v=St(w,m);if(v.x.length!==o)throw new RangeError("idata must have one row per column of y");return vl(v.x,v.colNames.length)},h=t.M===void 0?Array.from({length:o},(w,v)=>Array.from({length:o},(N,M)=>M===v?1:0)):u(t.M),p=u(t.X??"~0"),d=bl(f,h,p),g=d.length,b=Kr(d);return{statistic:b,statisticName:"W",pValue:Zr(b,g,c,o),method:"Mauchly's test of sphericity"}}var Qs=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 Se(e,t){if(e.filter(j).length!==1)throw new RangeError(t)}function Y(e,t,n,r={}){return ct(e,t,n,r).root}var wt=(e,t,n,r)=>bn(e,t,{ncp:n,lowerTail:r});function Yr(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Jr="'sig.level' must be numeric in [0, 1]";function yl(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;Se([t,n,o,r,a],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Yr(a,Jr);let i=ie(e.type,["two.sample","one.sample","paired"]),l=ie(e.alternative,["two.sided","one.sided"]),s=e.strict??!1,c=e.tol??Qs,f=i==="two.sample"?2:1,m=l==="two.sided"?2:1;m===2&&!j(n)&&(n=Math.abs(n));let u=(d,g,b,w)=>{let v=Math.max(1e-7,d-1)*f,N=it(w/m,v,{lowerTail:!1}),M=Math.sqrt(d/f)*g/b,E=wt(N,v,M,!1);return s&&m===2?E+wt(-N,v,M,!0):E};if(j(r))r=u(t,n,o,a);else{let d=r;j(t)?t=Y(g=>u(g,n,o,a)-d,2,1e7,{tol:c,extendInt:"upX"}):j(o)?o=Y(g=>u(t,n,g,a)-d,n*1e-7,n*1e7,{tol:c,extendInt:"downX"}):j(n)?n=Y(g=>u(t,g,o,a)-d,o*1e-7,o*1e7,{tol:c,extendInt:"upX"}):a=Y(g=>u(t,n,o,g)-d,1e-10,1-1e-10,{tol:c,extendInt:"yes"})}let h=i==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":i==="two.sample"?"n is number in *each* group":void 0,p={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[i]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:a,power:r,alternative:l,...h===void 0?{}:{note:h},method:p}}function Ml(e={}){let{n:t,p1:n,p2:r,power:o}=e,a=e.sigLevel===void 0?.05:e.sigLevel;Se([t,n,r,o,a],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Yr(a,Jr);let i=ie(e.alternative,["two.sided","one.sided"]),l=e.strict??!1,s=e.tol??Qs,c=i==="two.sided"?2:1,f=(u,h,p,d)=>{let g=H(d/c,{lowerTail:!1});if(l&&c===2){let b=(h+p)/2,w=h*(1-h),v=p*(1-p),N=b*(1-b),M=Math.abs(h-p),E=Math.sqrt(w+v);return P((Math.sqrt(u)*M-g*Math.sqrt(2*N))/E)+P((Math.sqrt(u)*M+g*Math.sqrt(2*N))/E,{lowerTail:!1})}return P((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=Y(h=>f(h,n,r,a)-u,1,1e7,{tol:s,extendInt:"upX"}):j(n)?(n=Y(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=Y(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=Y(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:i,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m}}function xl(e={}){let{groups:t,n,betweenVar:r,withinVar:o,power:a}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,n,r,o,a,i],"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");Yr(i,Jr);let l=(s,c,f,m,u)=>{let h=(s-1)*c*f/m,p=s-1,d=(c-1)*s;return st(Qe(u,p,d,{lowerTail:!1}),p,d,{ncp:h,lowerTail:!1})};if(j(a))a=l(t,n,r,o,i);else{let s=a;j(t)?t=Y(c=>l(c,n,r,o,i)-s,2,100):j(n)?n=Y(c=>l(t,c,r,o,i)-s,2,1e5):j(o)?o=Y(c=>l(t,n,r,c,i)-s,r*1e-7,r*1e7):j(r)?r=Y(c=>l(t,n,c,o,i)-s,o*1e-7,o*1e7):i=Y(c=>l(t,n,r,o,c)-s,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:i,power:a,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation"}}var El={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 Ks(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:El[n][["small","medium","large"].indexOf(r)],method:"Conventional effect size from Cohen (1982)"}}function ql(e,t){return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function kl(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 Al(e){let t=e.map(a=>a.reduce((i,l)=>i+l,0)),n=e[0]?.length??0,r=Array.from({length:n},(a,i)=>e.reduce((l,s)=>l+s[i],0)),o=0;for(let a=0;a<n;a++)for(let i=0;i<e.length;i++){let l=t[i]*r[a];o+=(e[i][a]-l)**2/l}return Math.sqrt(o)}var Rl="\u2018sig.level\u2019 must be numeric in [0, 1]",Tl="\u2018power\u2019 must be numeric in [0, 1]";function Ze(e,t){let n=r=>!j(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(Rl);if(n(t))throw new RangeError(Tl)}function Ye(e,t){return typeof e=="string"?Ks(t,e).effectSize:e}var an=["two.sided","less","greater"];function Bn(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Zs(e,t,n,r){if(r==="less")return wt(it(n,e),e,t,!0);if(r==="greater")return wt(it(n,e,{lowerTail:!1}),e,t,!1);let o=it(n/2,e,{lowerTail:!1});return wt(o,e,t,!1)+wt(-o,e,t,!0)}function Ys(e,t,n){return n==="greater"?P(H(1-t)-e,{lowerTail:!1}):n==="less"?P(H(t)-e):P(H(1-t/2)-e,{lowerTail:!1})+P(H(t/2)-e)}function Sl(e={}){let{n:t,power:n}=e,r=Ye(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;Se([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),Ze(o,n);let a=ie(e.type,["two.sample","one.sample","paired"]),i=ie(e.alternative,an),l=a==="two.sample"?2:1;i==="two.sided"&&!j(r)&&(r=Math.abs(r));let s=(m,u,h)=>Zs((m-1)*l,Math.sqrt(m/l)*u,h,i);if(j(n))n=s(t,r,o);else{let m=n;j(t)?t=Y(u=>s(u,r,o)-m,2+1e-10,1e9):j(r)?r=Y(u=>s(t,u,o)-m,...Bn(i,[1e-7,10])):o=Y(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:i,...c===void 0?{}:{note:c},method:f}}function Ll(e={}){let{n1:t,n2:n,power:r}=e,o=Ye(e.d,"t"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,n,o,r,a],"exactly one of n1, n2, d, power, and sig.level must be NULL"),Ze(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 i=ie(e.alternative,an);i==="two.sided"&&!j(o)&&(o=Math.abs(o));let l=(s,c,f,m)=>Zs(s+c-2,f/Math.sqrt(1/s+1/c),m,i);if(j(r))r=l(t,n,o,a);else{let s=r;j(t)?t=Y(c=>l(c,n,o,a)-s,2+1e-10,1e9):j(n)?n=Y(c=>l(t,c,o,a)-s,2+1e-10,1e9):j(o)?o=Y(c=>l(t,n,c,a)-s,...Bn(i,[1e-7,10])):a=Y(c=>l(t,n,o,c)-s,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:a,power:r,alternative:i,method:"t test power calculation"}}function eo(e,t,n,r,o,a){let{n:i,power:l}=t,s=Ye(e,n==="h"?"p":"t"),c=t.sigLevel===void 0?.05:t.sigLevel;if(Se([s,i,l,c],`exactly one of ${n}, n, power, and sig.level must be NULL`),Ze(c,l),!j(i)&&i<1)throw new RangeError("number of observations in each group must be at least 1");let f=ie(t.alternative,an);f==="two.sided"&&!j(s)&&(s=Math.abs(s));let m=(u,h,p)=>Ys(u*r(h),p,f);if(j(l))l=m(s,i,c);else{let u=l;j(s)?s=Y(h=>m(h,i,c)-u,...Bn(f,[a,10])):j(i)?i=Y(h=>m(s,h,c)-u,o,1e9):c=Y(h=>m(s,i,h)-u,1e-10,1-1e-10)}return{es:s,n:i,sigLevel:c,power:l,alternative:f}}function Il(e={}){let t=eo(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 Ol(e={}){let t=eo(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 Fl(e={}){let t=eo(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 _l(e={}){let{n1:t,n2:n,power:r}=e,o=Ye(e.h,"p"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([o,t,n,r,a],"exactly one of h, n1, n2, power, and sig.level must be NULL"),Ze(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 i=ie(e.alternative,an);i==="two.sided"&&!j(o)&&(o=Math.abs(o));let l=(s,c,f,m)=>Ys(s*Math.sqrt(c*f/(c+f)),m,i);if(j(r))r=l(o,t,n,a);else{let s=r;j(o)?o=Y(c=>l(c,t,n,a)-s,...Bn(i,[1e-10,10])):j(t)?t=Y(c=>l(o,c,n,a)-s,2+1e-10,1e9):j(n)?n=Y(c=>l(o,t,c,a)-s,2+1e-10,1e9):a=Y(c=>l(o,t,n,c)-s,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:a,power:r,alternative:i,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes"}}function Vl(e={}){let{n:t,power:n}=e,r=Ye(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),Ze(o,n),!j(t)&&t<4)throw new RangeError("number of observations must be at least 4");let a=ie(e.alternative,an);a==="two.sided"&&!j(r)&&(r=Math.abs(r));let i=(s,c,f,m)=>{let u=it(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((p-d)*Math.sqrt(s-3));return m?g+P((-p-d)*Math.sqrt(s-3)):g},l=(s,c,f)=>a==="two.sided"?i(s,c,f,!0):i(s,a==="less"?-c:c,f,!1);if(j(n))n=l(t,r,o),a==="less"&&(r=-r);else{let s=n;if(j(t))t=Y(c=>l(c,r,o)-s,4+1e-10,1e9);else if(j(r)){let[c,f]=a==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=Y(m=>l(t,m,o)-s,c,f)}else o=Y(c=>l(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 Cl(e){let{N:t,power:n}=e,{df:r}=e,o=Ye(e.w,"chisq"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([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(Ze(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 i=(l,s,c)=>$e(Io(c,r,{lowerTail:!1}),r,{ncp:s*l**2,lowerTail:!1});if(j(n))n=i(o,t,a);else{let l=n;j(o)?o=Y(s=>i(s,t,a)-l,1e-10,1e9):j(t)?t=Y(s=>i(o,s,a)-l,1+1e-10,1e9):a=Y(s=>i(o,t,s)-l,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 Dl(e={}){let{k:t,n,power:r}=e,o=Ye(e.f,"anov"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,n,o,r,a],"exactly one of k, n, f, power, and sig.level must be NULL"),Ze(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 i=(l,s,c,f)=>{let m=l-1,u=(s-1)*l;return st(Qe(f,m,u,{lowerTail:!1}),m,u,{ncp:l*s*c**2,lowerTail:!1})};if(j(r))r=i(t,n,o,a);else{let l=r;j(t)?t=Y(s=>i(s,n,o,a)-l,2+1e-10,100):j(n)?n=Y(s=>i(t,s,o,a)-l,2+1e-10,1e9):j(o)?o=Y(s=>i(t,n,s,a)-l,1e-7,1e7):a=Y(s=>i(t,n,o,s)-l,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 $l(e={}){let{u:t,v:n,power:r}=e,o=Ye(e.f2,"f2"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,n,o,r,a],"exactly one of u, v, f2, power, and sig.level must be NULL"),Ze(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 i=(l,s,c,f)=>st(Qe(f,l,s,{lowerTail:!1}),l,s,{ncp:c*(l+s+1),lowerTail:!1});if(j(r))r=i(t,n,o,a);else{let l=r;j(t)?t=Y(s=>i(s,n,o,a)-l,1+1e-10,100):j(n)?n=Y(s=>i(t,s,o,a)-l,1+1e-10,1e9):j(o)?o=Y(s=>i(t,n,s,a)-l,1e-7,1e7):a=Y(s=>i(t,n,o,s)-l,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:a,power:r,method:"Multiple regression power calculation"}}var Pl="Missing values detected. NAs dropped.",na={tol:0,maxiter:5e3};function jl(e){return Math.exp(ir(e/2)-Math.log(Math.sqrt(e/2))-ir((e-1)/2))}function Js(e,t,n){return ct(r=>bn(e,t,{ncp:r})-n,e-1,e+1,{...na,extendInt:"downX"}).root}function ra(e,t,n,r){let o=n.pooledSd??!0,a=n.mu??0,i=n.paired??!1,l=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(Pl);let u,h,p,d,g;if(f===void 0||i){let x;if(f===void 0)x=ke(c);else{let[q,S]=At(c,f);x=q.map((L,A)=>L-S[A])}let k=x.length;u=se(x),h=Math.sqrt(re(x)),p=h/Math.sqrt(k),d=1/k,g=k-1}else{let x=ke(c),k=ke(f),q=x.length,S=k.length;u=se(x)-se(k);let L=Math.sqrt(re(x)),A=Math.sqrt(re(k));o?(h=Math.sqrt(((q-1)*L**2+(S-1)*A**2)/(q+S-2)),p=h*Math.sqrt(1/q+1/S),d=1/q+1/S,g=q+S-2):(h=Math.sqrt((L**2+A**2)/2),d=2*(S*L**2+q*A**2)/(q*S*(L**2+A**2)),p=Math.sqrt(L**2/q+A**2/S),g=p**4/((L**2/q)**2/(q-1)+(A**2/S)**2/(S-1)))}let b=r?jl(g):1,w=(u-a)/h*b,v=r?"Hedges_g":"Cohens_d";if(l===null)return{name:v,value:w,warnings:m};let N=s==="two.sided"?l:2*l-1,M=(u-a)/p,E=Js(M,g,(1+N)/2)*Math.sqrt(d)*b,y=Js(M,g,(1-N)/2)*Math.sqrt(d)*b;return s==="less"&&(E=-1/0),s==="greater"&&(y=1/0),{name:v,value:w,ci:l,ciLow:E,ciHigh:y,warnings:m}}function zl(e,t,n={}){return ra(e,t,n,n.adjust??!1)}function Wl(e,t,n={}){return ra(e,t,n,!0)}var Gl=e=>Array.isArray(e.effects),oa=e=>e.multistratum===!0,Ul=e=>Array.isArray(e.strata),sa=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Hl=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),to=e=>new RegExp(`\\b${Hl(e)}\\b`);function Bl(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let a=o.stratum.replace(/^Error: /,""),i=o.colNames.indexOf("Df"),l=o.colNames.indexOf("Sum Sq"),s=o.rowNames.map((f,m)=>({name:f,df:o.table[m][i],ss:o.table[m][l]})),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 Xl(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(b=>to(b).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 i=r.find(s=>s.name==="Subject");if(i===void 0)throw new Error("the result has no subject stratum");let l=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:i,order:l}}function aa(e,t){let n=Array.isArray(e)?e:[],r=n.filter(a=>!t.some(i=>to(a).test(i.name)));if(r.length>0)throw new Error(`Observed variable not in data: ${r.join(", ")}`);let o=a=>n.some(i=>to(i).test(a));return{isObserved:o,ssObserved:t.reduce((a,i)=>o(i.name)?a+i.ss:a,0)}}function ea(e,t,n,r){let o=a=>st(e,t,n,{ncp:a})-r;return o(0)>0?e===1/0?1/0:ct(o,0,Math.max(10,e*t*4),{...na,extendInt:"downX"}).root:0}function ia(e,t,n,r,o){let a=o==="two.sided"?r:2*r-1,i=Math.max(0,e),l=i/t/((1-i)/n),s=ea(l,t,n,(1+a)/2),c=ea(l,t,n,(1-a)/2);l<=Qe((1-a)/2,t,n)&&(c=0),l<=Qe((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 ta="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function no(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(ta);if(oa(e)||Ul(e)||sa(e))return Ql(e,t,n);let r=Gl(e)?Br(e):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(ta);let o=n.ci===void 0?.95:n.ci,a=ie(n.alternative??"greater",["two.sided","less","greater"]),i=r.colNames.indexOf("Df"),l=r.colNames.indexOf("Sum Sq");if(i<0||l<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][i],ss:r.table[x][l]})).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:v,ssObserved:N}=aa(g,f),M=y=>{let{ss:x,df:k}=y;if(t==="Eta2")return w==="partial"?x/(x+u):w==="plain"?x/p:x/(x+u+N-(v(y.name)?x:0));if(t==="Omega2"){let S=w==="partial"?(x-k*h)/(x+(d-k)*h):(x-k*h)/(p+h);return Math.max(0,S)}let q=w==="partial"?(x-k*h)/(x+u):(x-k*h)/p;return Math.max(0,q)},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)]:ia(M(y),y.df,m,o,a)),warnings:[]}}function Ql(e,t,n){let r=n.ci===void 0?.95:n.ci,o=ie(n.alternative??"greater",["two.sided","less","greater"]),a=sa(e),i,l,s;if(a){let N=Xl(e);({strata:i,subject:l}=N),s=N.order.map(M=>({term:M,stratum:N.strata.find(E=>E.terms.includes(M))}))}else({strata:i,subject:l}=Bl(oa(e)?Xr(e):e)),s=i.flatMap(N=>N.terms.map(M=>({term:M,stratum:N})));let c=i.flatMap(N=>N.terms),f=i.reduce((N,M)=>N+M.sse,0),m=c.reduce((N,M)=>N+M.ss,0)+f,u=l.sse/l.dfe,h=t==="Eta2"?n.generalized??!1:!1,p=h!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:g}=aa(h,c),b=(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 q=M===l?0:1,S=p==="partial"?(E-y*x)/(E+q*M.sse+l.sse+u):(E-y*x)/(m+u);return Math.max(0,S)}let k=p==="partial"?(E-y*x)/(E+M.sse):(E-y*x)/m;return Math.max(0,k)},w=`${t}${p==="plain"?"":`_${p}`}`,v={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=b(N,M);return r===null?[E]:ia(E,N.df,M.dfe,r,o)}),warnings:[]};return a||(v.groups=s.map(N=>N.stratum.name)),v}function Kl(e,t={}){return no(e,"Eta2",t)}function Zl(e,t={}){return no(e,"Omega2",t)}function Yl(e,t={}){return no(e,"Epsilon2",t)}var Xn=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Jl=Math.sqrt(2220446049250313e-31);function Qn(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 ln=e=>typeof e=="boolean"?e?"TRUE":"FALSE":String(e);function ec(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(ln):[...new Set(t.map(ln))].sort(lt)}var la=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function ca(e,t,n,r){let o=s=>r.some(c=>new RegExp(`\\b${la(c)}\\b`).test(s));for(let s of r)if(!e.some(c=>new RegExp(`\\b${la(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),i=e.reduce((s,c)=>o(c.name)?s+c.ss:s,0),l=["pes","ges"].filter(s=>n.includes(s));return{colNames:["num Df","den Df","MSE","F",...l,"Pr(>F)"],rowNames:e.map(s=>s.name),table:e.map(s=>{let c=l.map(f=>f==="pes"?s.ss/(s.ss+s.errSS):s.ss/(s.ss+a+i-(o(s.name)?s.ss:0)));return[s.numDf,s.denDf,s.errSS/s.denDf,s.f,...c,s.p]})}}function tc(e,t){let n=Xn(t.between),r=Xn(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",i=Xn(t.es??"ges"),l=Xn(t.observed),s=[],c=Qn(e,t.id).map(ln),f=Qn(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=>Qn(e,T)),u=r.map(T=>Qn(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=>ec(T)),d=m.map(T=>T.map(ln)),g=u.map(T=>T.map(ln)),b=g.map(T=>[...new Set(T)]);p.forEach((T,O)=>{if(T.length<2)throw new Error(`Factor "${n[O]}" consists of one level only. Remove factor from model?`)});let w=[...new Set(c)],v=new Map,N=new Set;if(c.forEach((T,O)=>{let $=JSON.stringify(d.map(G=>G[O])),D=v.get(T);D===void 0?v.set(T,$):D!==$&&N.add(T)}),N.size>0)throw new Error(`Following ids are in more than one between subjects condition:
4
- ${w.filter(T=>N.has(T)).join(", ")}`);let M=[[]];for(let T of b)M=M.flatMap(O=>T.map($=>[...O,$]));let E=T=>JSON.stringify(g.map(O=>O[T])),y=new Map(M.map((T,O)=>[JSON.stringify(T),O])),x=new Set(c.map((T,O)=>E(O)));if(M.some(T=>!x.has(JSON.stringify(T))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let k=M.length,q=new Map;c.forEach((T,O)=>{let $=q.get(T);$===void 0&&q.set(T,$={s:M.map(()=>[0,0]),n:M.map(()=>0)});let D=y.get(E(O));$.s[D]=B($.s[D],[f[O],0]),$.n[D]++}),[...q.values()].some(T=>T.n.some(O=>O>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=>q.get(T).n.some(O=>O===0));if(S.length>0){let T=[...S].sort(lt),O=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:
5
- ${O}`)}let L=w.filter(T=>!S.includes(T)),A=new Map;c.forEach((T,O)=>A.has(T)||A.set(T,O));let R=L.map(T=>{let O=q.get(T);return O.s.map(($,D)=>Bt($,O.n[D]))}),C={};n.forEach((T,O)=>C[T]=L.map($=>d[O][A.get($)]));let F=Object.fromEntries(n.map(T=>[T,"contr.sum"])),I=Object.fromEntries(n.map((T,O)=>[T,p[O]])),V="Rank deficient model matrix; insufficient data to estimate full model.";if(k===1)return z();return J();function z(){let T=n.includes("y")?"y_":"y",O={...C,[T]:R.map(ne=>ne[0][0])},$=Vr(`${T} ~ ${n.length>0?n.join(" * "):"1"}`,O,{contrasts:F,levels:I});if($.rank<$.coefNames.length)throw new Error(V);let D=Qr($,{type:o});s.push(...D.warnings.filter(ne=>!s.includes(ne)));let G=$.dfResidual,Q=D.rowNames.flatMap((ne,de)=>{if(ne==="(Intercept)"||ne==="Residuals")return[];let[ve,Oe,Fe,Le]=D.table[de];return[{name:ne,ss:ve,numDf:Oe,errSS:$.rss,denDf:G,f:Fe,p:Le}]});return{anova:ca(Q,[$.rss],i,l),univariate:{colNames:D.colNames,rowNames:D.rowNames,table:D.table},within:r,warnings:s}}function J(){let T=L.length,O=n.length>0?St(`~ ${n.join(" * ")}`,C,{contrasts:F,levels:I}):{colNames:["(Intercept)"],assign:[0],x:L.map(()=>[1]),termLabels:[],factors:[]},$=O.colNames.length;if(T===0||he(O.x).rank<$)throw new Error(V);let D=T-$,G=[0,...O.termLabels.map((X,Z)=>Z+1)],Q=["(Intercept)",...O.termLabels],ne=X=>O.assign.flatMap((Z,U)=>X.includes(Z)?[U]:[]),de=X=>O.factors.flatMap((Z,U)=>Z[X-1]>0?[U]:[]),ve=X=>O.x.map(Z=>[Z[X],0]),Oe=new Array(T).fill([0,0]),Fe=(X,Z,U)=>{let{qtv:pe}=je([...X,...Z].map(ve),Oe,U);return pe.map(tt=>tt.slice(X.length,X.length+Z.length))},Le=X=>{let{qtv:Z}=je(O.colNames.map((U,pe)=>ve(pe)),Oe,X);return Z.map(U=>U.slice($))},ze=Object.fromEntries(r.map((X,Z)=>[X,M.map(U=>U[Z])])),Ae=St(`~ ${r.join(" * ")}`,ze,{contrasts:Object.fromEntries(r.map(X=>[X,"contr.sum"])),levels:Object.fromEntries(r.map((X,Z)=>[X,b[Z]]))}),yt=[0,...Ae.termLabels.map((X,Z)=>Z+1)],Je=["(Intercept)",...Ae.termLabels],$t=X=>X.map(Z=>X.map(U=>{let pe=[0,0];return Z.forEach((tt,Gt)=>pe=B(pe,oe(tt,U[Gt]))),pe})),et=[],Pt=[],jt=[],pn=[],zt=[],Wt=[],_=[],W=!1;for(let X of yt){let Z=Ae.assign.flatMap((Re,ce)=>Re===X?[ce]:[]),U=Z.length,pe=Z.map(Re=>R.map(ce=>ce.reduce((Ge,Ue,qo)=>B(Ge,Te(Ue,Ae.x[qo][Re])),[0,0]))),tt=Z.map(Re=>Z.map(ce=>[Ae.x.reduce((Ge,Ue)=>Ge+Ue[Re]*Ue[ce],0),0])),Gt=Bs(tt),or=$t(Le(pe)),dn=sn(or,Gt),gn=gt(dn)[0],We=NaN,wn=NaN,sr=NaN,xo=NaN,Eo=!1;if(U>=2&&mt(Gs(or),{symmetric:!0,onlyValues:!0}).values.filter(ce=>ce>=Jl).length===U){Eo=!0;let ce=gt(dn);We=ge(oe(ce,ce),Te(gt(sn(dn,dn)),U))[0],wn=((D+1)*U*We-2)/(U*(D-U*We)),sr=Kr(sn(Gt,or)),xo=Zr(sr,U,D,k)}for(let Re=0;Re<G.length;Re++){let ce=G[Re],Ge;if(o===3)Ge=Fe(ne(G.filter(Xe=>Xe!==ce)),ne([ce]),pe);else if(ce===0)Ge=Fe([],ne([0]),pe);else{let Xe=de(ce),vn=G.filter(De=>De!==0&&De!==ce&&Xe.every(Ca=>O.factors[Ca][De-1]>0));Ge=Fe(ne(G.filter(De=>De!==ce&&!vn.includes(De))),ne([ce]),pe)}let Ue=gt(sn($t(Ge),Gt))[0],He=(ce===0?1:ne([ce]).length)*U,Be=D*U,Ut=Ue/He/(gn/Be),ko=ae(Ut,He,Be,!1,!1),ar=X===0?Q[Re]:ce===0?Je[X]:`${Q[Re]}:${Je[X]}`;et.push([Ue,He,gn,Be,Ut,ko]),Pt.push(ar),_.push(gn);let Ht={name:ar,ss:Ue,numDf:He,errSS:gn,denDf:Be,f:Ut,p:ko};if(Eo){let Xe=Math.min(1,wn),vn=ae(Ut,We*He,We*Be,!1,!1),De=ae(Ut,Xe*He,Xe*Be,!1,!1);jt.push([We,vn,wn,De]),pn.push([sr,xo]),zt.push(ar),a==="GG"?Ht={...Ht,numDf:We*He,denDf:We*Be,p:vn}:a==="HF"&&(wn>1&&(W=!0),Ht={...Ht,numDf:Xe*He,denDf:Xe*Be,p:De})}X===0&&ce===0||Wt.push(Ht)}}W&&s.push("HF eps > 1 treated as 1");let K={anova:ca(Wt,_,i,l),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:Pt,table:et},within:r,warnings:s};return jt.length>0&&(K.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:zt,table:jt},K.sphericity={colNames:["Test statistic","p-value"],rowNames:[...zt],table:pn}),K}}var ha=Math.log(2*Math.PI),fa=2e-14,ma=500;function nc(e){return Math.exp(-.5*e*e-.5*ha)}function rc(e){return P(e,{logP:!0})}function pa(e,t=-1/0){return(t===-1/0?ur(e,0,2,0,fa,ma):ur(e,t,1,0,fa,ma)).result}function ua(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return pa(i=>{let l=0;for(let s=0;s<r.length;s++){let c=(r[s]*i+e)/o[s];if(!n){l+=rc(c);continue}let f=(r[s]*i-e)/o[s],m=P(f)+P(c,{lowerTail:!1});m<.5?l+=Math.log1p(-m):l+=Math.log(c+f<0?P(c)-P(f):P(f,{lowerTail:!1})-P(c,{lowerTail:!1}))}return nc(i)*-Math.expm1(l)})}function vt(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,ua(e,t,r));let o=n/2,a=Math.log(2)+.5*Math.log(o)-.5*ha-To(o);return Math.min(1,pa(l=>{let s=a-o*(Math.expm1(2*l)-2*l);return s<-745?0:Math.exp(s)*ua(e*Math.exp(l),t,r)}))}var da="Ties are present. z-quantiles were corrected for ties.";function Ce(e,t){let{labels:n,groups:r}=ut(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let a=r.map(i=>i.length);return{labels:n,groups:r,n:a,N:a.reduce((i,l)=>i+l,0),k:o}}function ro(e){let t=e.groups.flat(),n=fe(t),r=[],o=0;for(let i of e.n){let l=0;for(let s=0;s<i;s++)l+=n[o+s];o+=i,r.push(l/i)}let a=0;for(let i of Ie(t))a+=i*i*i-i;return{R:r,C:a/(12*(e.N-1))}}function cn(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 oc(e,t){let n=e.length+1,r=[];for(let l=0;l<n-1;l++)for(let s=l+1;s<n;s++)r.push(e[s-1][l]);let o=It(r,t),a=0,i=e.map(l=>l.slice());for(let l=0;l<n-1;l++)for(let s=l+1;s<n;s++)i[s-1][l]=o[a++];return i}function Zn(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 oo(e,t,n,r,o,a=[]){return{method:t,statistic:n.map(i=>[i]),pValue:r.map(i=>[i]),rowNames:e.labels.slice(1),colNames:e.labels.slice(0,1),pAdjustMethod:o,warnings:a}}var Kn=e=>P(e,{lowerTail:!1}),so=(e,t,n)=>Nn(Math.abs(e),t,n,{lowerTail:!1});function ao(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 sc(e,t,n={}){let r=n.pAdjustMethod??"holm",o=Ce(e,t),{R:a,C:i}=ro(o),l=o.N*(o.N+1)/12-i,s=cn(o.k,(f,m)=>Math.abs(a[f]-a[m])/Math.sqrt(l*(1/o.n[f]+1/o.n[m]))),c=oc(s.map(f=>f.map(m=>2*Kn(m))),r);return Zn(o,"Dunn's all-pairs test",s,c,r,i!==0?[da]:[])}function ac(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=Ce(e,t),{R:i,C:l}=ro(a),s=a.N*(a.N+1)/12-l,c=a.n.slice(1).map((h,p)=>p+1),f=c.map(h=>(i[h]-i[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=ga(f,m,r,o);return oo(a,"Dunn's many-to-one test",f,u,o,l!==0?[da]:[])}function ga(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?vt(o,t,1/0,!0):vt(n==="greater"?o:-o,t,1/0,!1)):It(e.map(o=>n==="two.sided"?2*Kn(Math.abs(o)):Kn(n==="greater"?o:-o)),r)}function ic(e,t,n={}){let r=te(n.alternative),o=Ce(e,t),{m:a,s2:i,df:l}=ao(o),s=Math.sqrt(i),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*vt(Math.abs(h),m,l,!1)):vt(r==="greater"?h:-h,m,l,!1));return oo(o,`Dunnett's-test for multiple
6
- comparisons with one control`,f,u,"single-step")}function lc(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=Ce(e,t);if(o==="single-step"&&a.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
1
+ /*! rStats.js 0.13.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
+ import{C as qt,D as ct,H as Co,I as Do,J as hr,N as kt,O as pr,T as At,U as $o,Y as Po,Z as Rt,_ as zo,b as Fo,c as he,ca as xn,d as mr,da as jo,ea as Jt,fa as Wo,ha as En,i as je,ia as qn,j as _o,l as Vo,m as Yt,n as at,s as ae,t as it,v as lt,w as Ye,y as ur}from"./chunks/chunk-AGRKZXLG.js";import{a as Sn,e as we,j as pt}from"./chunks/chunk-22SXZNR6.js";import{a as An,b as Je,c as nn,d as Bo,e as Xo,f as gr,g as te,h as Rn,i as Ee,j as Yo,k as Pa,m as In,n as rn}from"./chunks/chunk-NRV3BH7I.js";import{k as Ne,l as We,n as Mn,o as ft}from"./chunks/chunk-N3FV6H3C.js";import{b as Zo,c as ut,e as On}from"./chunks/chunk-VG67L3JA.js";import{b as dr}from"./chunks/chunk-BJHBYEEU.js";import"./chunks/chunk-F44XUHWI.js";import{e as en,f as se,g as re,h as Go,i as ee,j as me,k as Fe,l as Re,m as Tt}from"./chunks/chunk-SLSDFIW2.js";import{a as Qo,d as Ko}from"./chunks/chunk-4AQNVF3Z.js";import{b as Zt}from"./chunks/chunk-CQNVEA22.js";import{f as xe,g as P,h as B,n as fr,p as Io,s as Oo}from"./chunks/chunk-4GNJEYXR.js";import{b as st,c as Et,d as X,e as So,f as le,g as oe,h as Le,i as be,j as Kt,k as Lo}from"./chunks/chunk-MEMYFTYS.js";import{a as ht,b as It}from"./chunks/chunk-RTWLZPPG.js";import{h as pe,l as Jo,m as Lt,n as Tn,v as Ln}from"./chunks/chunk-VWGJYRE6.js";import{a as kn,b as Uo,g as Ho}from"./chunks/chunk-BSOUJPDF.js";import{a as mt}from"./chunks/chunk-JGFUZKLJ.js";import{a as tn,b as St,c as ce,d as ge}from"./chunks/chunk-3AKA4KEL.js";function Ot(e,t,n={}){if(typeof e=="string")return za(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(Rn(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]=Tt(c,f);c=x.map((k,S)=>k-q[S]),f=void 0}else c=Re(c),f!==void 0&&(f=Re(f));let m,u,h,d,p,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",d=N,p=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),k=x>1?re(f):NaN;if(g=`${i?"":"Welch"} Two Sample t-test`,d=[N,q],p=["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)*k),S/=u,h=Math.sqrt(S*(1/w+1/x))}else{let S=M/w,L=k/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+We(s,u,!0,!1)];else if(o==="greater")E=Ne(m,u,!1,!1),y=[m-We(s,u,!0,!1),1/0];else{E=2*Ne(-Math.abs(m),u,!0,!1);let x=1-s,q=We(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:d,estimateNames:p,nullValue:a,nullValueName:v,stdErr:h}}function za(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=An(e,t,!0);if(r.kind==="one")return Ot(r.x,n);if(r.kind==="paired")return Ot(r.x,r.y,{...n,paired:!0});let[o,a]=r.levels;return{...Ot(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 Ce(e){let t=[0,0],n=0;for(let r of e)t=X(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function ja(e,t,n){return Array.isArray(t)?Wa(e,t,n??{}):Ga(e,t??{})}var es=(e,t)=>e<t?-1:e>t?1:0;function Wa(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 d=t[h];!r(u)&&!r(d)&&(o.push(u),a.push(d))});let l=[...new Set(o)].sort(es),i=[...new Set(a)].sort(es);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])]++),{...ss(f,n),rowLevels:l,colLevels:i}}function Ga(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?wr(r[0],t):o===1?wr(r.map(a=>a[0]),t):ss(r,t)}return wr(Array.from(e),t)}function ts(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 ns(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 rs(e,t){return Number.isNaN(e)?NaN:he(e,t/2,2,!1,!1)}function os(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 wr(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=Ce(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=Ce(o);o=o.map(u=>u/m)}else if(Math.abs(Ce(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let a=o.map(m=>r*m);os(a);let l=o.map((m,u)=>a[u]*(1-m)),i=Ce(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=ts(t);s+=tn(m),c=NaN,f=ns(Ua(r,o,a,m),i,m)}else f=rs(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 Ua(e,t,n,r){let o=t.length,a=Wo(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 Ha(e,t,n,r){let o=e.length,a=t.length,l=e.reduce((f,m)=>f+m,0),i=En(l),s=new Int32Array(o*a),c=new Float64Array(Math.trunc(r));for(let f=0;f<c.length;f++){qn(e,t,i,s);let m=0;for(let u=0;u<a;u++)for(let h=0;h<o;h++){let d=n[h][u];m+=(s[h+u*o]-d)**2/d}c[f]=m}return c}function ss(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=Ce(a);if(!(l>0))throw new RangeError("at least one entry of 'x' must be positive");let i=e.map(w=>Ce(w)),s=e[0].map((w,b)=>Ce(e.map(N=>N[b]))),c=i.map(w=>s.map(b=>w*b/l));os(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),d,p=(t.simulatePValue??!1)&&i.every(w=>w>0)&&s.every(w=>w>0),g=!p&&n&&r===2&&o===2?Math.min(.5,...f.flat().map(Math.abs)):0;if(p){let w=ts(t);m+=tn(w),u=Ce(f.flatMap((N,M)=>N.map((E,y)=>E*E/c[M][y]))),h=NaN;let b=Ha(i.map(N=>Math.trunc(N)),s.map(N=>Math.trunc(N)),c,w);d=ns(b,u,w)}else g>0?(m+=" with Yates' continuity correction",u=Ce(f.flatMap((w,b)=>w.map((N,M)=>(Math.abs(N)-g)**2/c[b][M])))):u=Ce(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:d??rs(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 Ba=Math.pow(Number.EPSILON,.25);function qe(e,t,n,r={}){let o={tol:r.tol??Ba,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),ut(e,t,n,o).root}function Xa(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(Zt)),l="Fisher's Exact Test for Count Data";if(n===2&&r===2)return Ya(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+tn(s),alternative:i,pValue:Qa(a,s)}}return{method:l,alternative:i,pValue:Ja(a)}}function Qa(e,t){let n=e.filter(p=>p.some(g=>g>0)),r=e[0].map((p,g)=>g).filter(p=>e.some(g=>g[p]>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(p=>r.map(g=>p[g])),a=o.map(p=>p.reduce((g,v)=>g+v,0)),l=o[0].map((p,g)=>o.reduce((v,w)=>v+w[g],0)),i=a.length,s=l.length,c=[0,0];for(let p of o.flat())c=X(c,[-Ko(p),0]);let f=c[0]/(1+64*Number.EPSILON),m=En(a.reduce((p,g)=>p+g,0)),u=new Int32Array(i*s),h=0,d=Math.trunc(t);for(let p=0;p<d;p++){qn(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 Ka=1+1e-7,Za=16;function vr(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=X(r,[e[o],0]));return r[0]}var Nr=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%Za===0||!(a>0&&a<1/0)?(o=kt(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=vr(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=X(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?pr(t-1,this.m,this.n,this.k,!1,!1):pr(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?vr(o,a,o.length-1):vr(o,0,a)}};function br(e,t){return t?qe(e,0,1):1/qe(n=>e(1/n),Number.EPSILON,1)}function Ya(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 Nr(l+s,i+c,l+i),{lo:u,hi:h}=m,d;if(r==="less")d=m.tail(f,o,!1);else if(r==="greater")d=m.tail(f,o,!0);else if(o===0)d=f===u?1:0;else if(o===1/0)d=f===h?1:0;else{let N=m.density(o),M=N[f-u]*Ka,E=[0,0];for(let y of N)y<=M&&(E=X(E,[y,0]));d=E[0]}let p;if(f===u)p=0;else if(f===h)p=1/0;else{let N=m.mean(1);p=N===f?1:br(M=>m.mean(M)-f,N>f)}let g={method:n,alternative:r,pValue:d,estimate:p,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:br(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:br(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 Fn=[[0,0]];function De(e){for(let t=Fn.length;t<=e;t++)Fn.push(X(Fn[t-1],Lo([t,0])));return Fn[e]}function _n(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 yr=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=So(De(i));for(let b of o)s=X(s,De(b));for(let b of a)s=X(s,De(b));let c=[0,0];for(let b of t.flat())c=le(c,De(b));let f=X(c,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,d=Array.from({length:l},()=>new Map),p=(b,N)=>{let M=N.join(","),E=d[b].get(M);if(E!==void 0)return E;let y=X(s,De(N.reduce((A,R)=>A+R,0)));for(let A of N)y=le(y,De(A));for(let A=b;A<l;A++)y=le(y,De(a[A]));let x={R:N,KZ:y,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(d[b].set(M,x),b===l-1){let A=[0,0];for(let R of N)A=le(A,De(R));return x.max=x.min=A[0],x.edges.push({to:x,hi:A[0],lo:A[1],mult:1}),x}let q=new Map,k=new Array(r+1).fill(0);for(let A=r-1;A>=0;A--)k[A]=k[A+1]+N[A];let S=new Array(r).fill(0),L=(A,R,C)=>{if(A===r-1){S[A]=R;let V=le(C,De(R)),j=N.map(($,D)=>$-S[D]).sort(($,D)=>$-D),Y=p(b+1,j),T=`${j.join(",")}|${V[0]}`,O=q.get(T);O===void 0?q.set(T,{to:Y,hi:V[0],lo:V[1],mult:1}):O.mult++;return}let F=Math.max(0,R-k[A+1]),I=Math.min(N[A],R);for(let V=F;V<=I;V++)S[A]=V,L(A+1,R-V,le(C,De(V)))};if(L(0,a[b],[0,0]),x.edges=[...q.values()],x.max=Math.max(...x.edges.map(A=>A.hi+A.to.max)),x.min=Math.min(...x.edges.map(A=>A.hi+A.to.min)),b===l-2){let A=x.edges.map(Y=>{let T=Y.to.edges[0];return{v:X([Y.hi,Y.lo],[T.hi,T.lo]),mult:Y.mult}});A.sort((Y,T)=>Y.v[0]-T.v[0]||Y.v[1]-T.v[1]);let R=A.length,C=A[R-1].v[0],F=new Float64Array(R),I=new Float64Array(R),V=new Float64Array(R),j=[0,0];A.forEach(({v:Y,mult:T},O)=>{F[O]=Y[0],I[O]=Y[1];let $=le(Y,[C,0]);j=X(j,[T*Math.exp($[0])*(1+$[1]),0]),V[O]=j[0]}),x.dist={hi:F,lo:I,cum:V,vmax:C,kv:X(s,[C,0])}}return x},g=p(0,[...o].sort((b,N)=>b-N)),v=new yr,w=(b,N,M,E)=>{if(N+b.max<=u-m){let[L,A]=_n(b.KZ[0],b.KZ[1],N,M);return v.add(L,A,E),!0}if(N+b.min>u+m)return!0;let y=b.dist;if(y===void 0)return!1;let[x,q]=_n(u,h,-N,-M),k=0,S=y.hi.length;for(;k<S;){let L=k+S>>>1,A=y.hi[L];A<x||A===x&&y.lo[L]<=q?k=L+1:S=L}if(k>0){let[L,A]=_n(y.kv[0],y.kv[1],N,M);v.add(L,A,E*y.cum[k-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 d[b].values()){let{pastHi:M,pastLo:E,count:y}=N;if(y.length!==0){for(let{to:x,hi:q,lo:k,mult:S}of N.edges)for(let L=0;L<y.length;L++){let[A,R]=_n(M[L],E[L],q,k),C=y[L]*S;if(w(x,A,R,C))continue;let F=x.index.get(A);F===void 0?(x.index.set(A,x.count.length),x.pastHi.push(A),x.pastLo.push(R),x.count.push(C)):x.count[F]+=C}N.pastHi=N.pastLo=N.count=[],N.index.clear()}}return Math.min(1,v.value())}function on(e,t,n={}){if(typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let p=An(e,t,!0);return p.kind==="one"?on(p.x,n):on(p.x,p.y,p.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[p,g]=Tt(Array.from(e),Array.from(r));u=Re(p.map((v,w)=>v-g[w]))}else u=Re(Array.from(e)),r!==void 0&&(h=Re(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let d={alternative:o,mu:a,correct:s,confInt:c,confLevel:f,tolRoot:m};return h===void 0?ti(u,i,l,d):oi(u,h,i,d)}function Vn(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function is(e,t){return t==="less"?P(e):t==="greater"?P(e,{lowerTail:!1}):2*Math.min(P(e),P(e,{lowerTail:!1}))}function ei(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 Mr(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 xr=(e,t)=>e-t;function ls(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 ti(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,d,p]=ls(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=At(h,c,!0,!1);else if(o==="greater")g=At(h-1,c,!1,!1);else{let y=h>E/2?At(h-1,c,!1,!1):At(h,c,!0,!1);g=Math.min(1,2*y)}}else v=`Wilcoxon signed rank test${l?" with continuity correction":""}`,g=is((d-Vn(d,o,l))/p,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?ni(N,r):ri(N,r);return{...b,...M}}function Cn(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function as(e,t,n){let r={confInt:Cn(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function cs(e,t,n){let r=e(t==="two.sided"?n/2:n);return r===0?1:r}function fs(e){if(!(e>0))throw new RangeError("tolRoot must be positive")}function ni(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(xr);let l=a.length,i=1-r,s=cs(m=>$o(m,o,!0,!1),n,i),c=At(s-1,o,!0,!1),f=n==="two.sided"?2*c:c;return{confInt:Cn(n,a[s-1],a[l-s]),confLevel:f-i>i/2?1-ei(f):r,estimate:Mr(a),estimateNames:"(pseudo)median"}}function ri(e,t){let{alternative:n,correct:r,confLevel:o,tolRoot:a}=t,l=e.length;if(l===0)return as(n,NaN);let i=Math.min(...e),s=Math.max(...e);if(!Number.isFinite(i)||!Number.isFinite(s)||l===1||i===s)return as(n,(i+s)/2,"midrange");let c=(b,N)=>{let[,M,E]=ls(e.map(y=>y-b).filter(y=>y!==0));return(M-Vn(M,n,N))/E},f=c(i,r),m=c(s,r),u=b=>-B(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>B(b/2)):!(f<u(b/2))&&!(m>B(b/2)),d=1-o,p=!1;for(;!h(d)&&d<1;)d*=2,p=!0;fs(a);let g=b=>qe(N=>c(N,r)-b,i,s,{tol:a,fLower:f-b,fUpper:m-b}),v;if(d>=1){let b=Mr([...e].sort(xr));v=Cn(n,b,b)}else n==="greater"?v=[g(u(d)),1/0]:n==="less"?v=[-1/0,g(B(d))]:v=[g(u(d/2)),g(B(d/2))];let w=qe(b=>c(b,!1),i,s,{tol:a,fLower:c(i,!1),fUpper:c(s,!1)});return{confInt:v,confLevel:p?1-Math.min(1,d):o,estimate:w,estimateNames:"(pseudo)median"}}function ms(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 oi(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,d]=ms(c,t),p,g,v=f&&!m;if(v)if(g="Wilcoxon rank sum exact test",o==="less")p=Rt(u,i,s,!0,!1);else if(o==="greater")p=Rt(u-1,i,s,!1,!1);else{let N=u>i*s/2?Rt(u-1,i,s,!1,!1):Rt(u,i,s,!0,!1);p=Math.min(1,2*N)}else g=`Wilcoxon rank sum test${l?" with continuity correction":""}`,p=is((h-Vn(h,o,l))/d,o);let w={method:g,alternative:o,statistic:u,statisticName:"W",pValue:p,nullValue:a,nullValueName:"location shift"};if(!r.confInt)return w;let b=v?si(e,t,r):ai(e,t,r);return{...w,...b}}function si(e,t,n){let{alternative:r,confLevel:o}=n,a=e.length,l=t.length,i=[];for(let h of e)for(let d of t)i.push(h-d);i.sort(xr);let s=i.length,c=1-o,f=cs(h=>zo(h,a,l,!0,!1),r,c),m=Rt(f-1,a,l,!0,!1),u=r==="two.sided"?2*m:m;return{confInt:Cn(r,i[f-1],i[s-f]),confLevel:u-c>c/2?1-u:o,estimate:Mr(i),estimateNames:"difference in location"}}function ai(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,d)=>{let[,p,g]=ms(e.map(v=>v-h),t);return(p-Vn(p,r,d))/g},f=(h,d)=>{let p=c(i,d)-h,g=c(s,d)-h;if(Number.isNaN(p)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return p<=0?i:g>=0?s:(fs(l),qe(v=>c(v,d)-h,i,s,{tol:l,fLower:p,fUpper:g}))},m=1-a,u;return r==="greater"?u=[f(-B(m),o),1/0]:r==="less"?u=[-1/0,f(B(m),o)]:u=[f(-B(m/2),o),f(B(m/2),o)],{confInt:u,confLevel:a,estimate:f(0,!1),estimateNames:"difference in location"}}var us=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function ii(e){if(us.includes(e))return e;let t=us.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Ft(e,t={}){let n=typeof t=="string"?{method:t}:t,r=ii(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=li(s,r,i),f=o.slice();return a.forEach((m,u)=>f[m]=c[u]),f}function Dn(e,t){let n=e.map((r,o)=>o);return n.sort((r,o)=>(t?e[o]-e[r]:e[r]-e[o])||r-o),n}function li(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(a=>Math.min(1,n*a));case"holm":{let a=Dn(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=Dn(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=Dn(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 ci(e,n)}}function ci(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=Dn(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 fi=1+1e-7;function $n(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function mi(e,t,n){if(n===0)return e===0?1:0;if(n===1)return e===t?1:0;let r=t*n;if(e===r)return 1;let o=qt(e,t,n,!1)*fi,a=0;if(e<r){for(let s=Math.ceil(r);s<=t;s++)qt(s,t,n,!1)<=o&&a++;let i=a===0?0:ct(t-a,t,n,!1,!1);return Math.min(1,ct(e,t,n,!0,!1)+i)}for(let i=0;i<=Math.floor(r);i++)qt(i,t,n,!1)<=o&&a++;let l=a===0?0:ct(a-1,t,n,!0,!1);return Math.min(1,ct(e-1,t,n,!1,!1)+l)}function Er(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),!$n(e[0])||!$n(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(!$n(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!($n(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;Ee(i);let s;l==="less"?s=ct(r,o,a,!0,!1):l==="greater"?s=ct(r-1,o,a,!1,!1):s=mi(r,o,a);let c=u=>r===0?0:ur(u,r,o-r+1,!0,!1),f=u=>r===o?1:ur(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 ui=1+1e-7;function hi(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function pi(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=Yt(e,t,!1)*ui,r=i=>Yt(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,at(e,t,!0,!1)+at(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,at(l,t,!0,!1)+at(e-1,t,!1,!1))}function di(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(hi))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(Ee(c),o===2){let[v,w]=r,[b,N]=l,M=v+w,E=i*b/(i*b+N),y=Er([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=at(f,u,!0,!1):s==="greater"?h=at(f-1,u,!1,!1):h=pi(f,u);let d=v=>f===0?0:mr(v,f,1,!0,!1),p=v=>mr(1-v,f+1,1,!0,!1),g;if(s==="less")g=[0,p(1-c)];else if(s==="greater")g=[d(1-c),1/0];else{let v=(1-c)/2;g=[d(v),p(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 qr(e,t,n={}){let r,o,a=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let k=e;if(k.some(S=>S.length!==2))throw new RangeError("'x' must have 2 columns");r=k.map(S=>S[0]),o=k.map(S=>S[0]+S[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(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 k=0;k<l;k++)!Number.isNaN(r[k])&&!Number.isNaN(o[k])&&i.push(k);let s=i.length;if(s<1)throw new RangeError("not enough data");if(r=i.map(k=>r[k]),o=i.map(k=>o[k]),o.some(k=>!(k>0)))throw new RangeError("elements of 'n' must be positive");if(r.some(k=>!(k>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((k,S)=>k>o[S]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let c;if(a.p!==void 0){let k=typeof a.p=="number"?[a.p]:Array.from(a.p);if(k.length!==l)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(c=i.map(S=>k[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;Ee(m);let u=te(a.alternative);(s>2||s===2&&f)&&(u="two.sided");let h=r.map((k,S)=>k/o[S]),d=s===2?h[0]-h[1]:0,p=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 k=Math.abs(r[0]-o[0]*c[0]);k<=g&&([g,v]=[k,!0])}else if(g>0&&s===2&&!f){let k=Math.abs(d)/p;k<=g&&([g,v]=[k,!0])}let w=r.reduce((k,S)=>k+S,0)/o.reduce((k,S)=>k+S,0),b=0;if(!v)for(let k=0;k<s;k++){let S=c?.[k]??w,L=o[k]*S,A=o[k]*(1-S);b+=(Math.abs(r[k]-L)-g)**2/L,b+=(Math.abs(o[k]-r[k]-A)-g)**2/A}let N=c!==void 0?s:s-1,M;if(u==="two.sided")M=he(b,N/2,2,!1,!1);else{let k=Math.sign(s===1?h[0]-c[0]:d);M=P(k*Math.sqrt(b),{lowerTail:u==="less"})}let E;if(s===1||s===2&&!f){let k=B(u==="two.sided"?(1+m)/2:m);if(s===1){let S=o[0],L=k*k,A=(V,j)=>(V+L/(2*S)+j*k*Math.sqrt(V*(1-V)/S+L/(4*S*S)))/(1+L/S),R=h[0]+g/S,C=h[0]-g/S,F=Math.min(1,Math.max(0,R>=1?1:A(R,1))),I=Math.min(1,Math.max(0,C<=0?0:A(C,-1)));E=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[0,F]}else{let S=Math.sqrt(h.reduce((C,F,I)=>C+F*(1-F)/o[I],0)),L=k*S+g*p,A=Math.max(-1,d-L),R=Math.min(1,d+L);E=u==="two.sided"?[A,R]:u==="greater"?[A,1]:[-1,R]}}let y=s===1?"p":i.map(k=>`prop ${k+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 gi(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=wi(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 wi(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 hs(e,t,n={}){if(typeof e=="string"){let g=Je(e,t);return hs(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;Ee(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),d,p;if(l==="less")d=u,p=[0,f/it(1-i,s,c,!0,!1)];else if(l==="greater")d=h,p=[f/it(i,s,c,!0,!1),1/0];else{d=Math.min(1,2*Math.min(u,h));let g=(1-i)/2;p=[f/it(1-g,s,c,!0,!1),f/it(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:d,confInt:p,confLevel:i,estimate:f,estimateNames:"ratio of variances",nullValue:a,nullValueName:"ratio of variances"}}function ps(e,t){if(typeof e=="string"){let u=nn(e,t);return ps(Bo(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 dt(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 gt(e,t,n="drop"){return dt(e,t,n).groups}function Pn(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 kr(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 Ar(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:he(n,r/2,2,!1,!1)}}function ds(e){let t=0;for(let n of Fe(e))t+=n*n*n-n;return t}function zn(e,t){if(typeof e=="string"){let c=nn(e,t);return zn(c.values,c.group.codes)}let n=gt(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-ds(o)/(a*a*a-a)),Ar("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",i,r-1)}function Rr(e,t){if(typeof e=="string")return Rr(gr(e,t));let n=kr(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+=ds(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 Ar("Friedman rank sum test","Friedman chi-squared",s,o-1)}function vi(e){return{...Rr(e),method:"Cochran's Q test",statisticName:"Q"}}function gs(e,t){if(typeof e=="string")return gs(gr(e,t));let n=kr(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(d=>Math.max(...d)-Math.min(...d))),l=new Array(o).fill(0),i=0;n.forEach((d,p)=>{let g=me(d);for(let v=0;v<o;v++){let w=a[p]*(g[v]-(o+1)/2);i+=w*w,l[v]+=w}});let s=0;for(let d of l)s+=d*d;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 d=0;for(let p=2;p<=o;p++)d+=Math.log(p);u=Math.exp((1-r)*d)}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 ws(e,t){if(typeof e=="string"){let u=nn(e,t);return ws(u.values,u.group.codes)}let n=gt(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=Pn(u);return u.map(d=>Math.abs(d-h))}),a=o.length,i=me(o).map(u=>B((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 d=0;d<u.length;d++)h+=i[m+d];m+=u.length,f+=u.length*(h/u.length-c)**2}return f/=re(i),Ar("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",f,r-1)}function vs(e,t,n={}){if(typeof e=="string"){let N=Je(e,t);return vs(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 d=0;for(let N of m)d+=N;d/=s;let p=0;for(let N of m)p+=(N-d)**2;let g=l*d,v=l*i/(s*(s-1))*p,w=(h-g)/Math.sqrt(v),b=a==="less"?P(w):a==="greater"?P(w,{lowerTail:!1}):2*P(-Math.abs(w));return{method:"Mood two-sample test of scale",alternative:a,statistic:w,statisticName:"Z",pValue:b}}function wt(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var bi=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],Ni=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],yi=[.544,-.39978,.025054,-6714e-7],Mi=[1.3822,-.77857,.062767,-.0020322],xi=[-1.5861,-.31082,-.083751,.0038915],Ei=[-.4803,-.082676,.0030302],qi=[-2.273,.459];function ki(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]=-B((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+wt(bi,l),s=new Float64Array(n);s[0]=i;let c,f;if(e>5){let m=r[1]/a+wt(Ni,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 bs(e){let t=e.length,n=ki(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 Ai=1.90985931710274,Ri=1.0471975511966;function Ns(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let l=Ai*(Math.asin(Math.sqrt(1-e))-Ri);return Math.max(l,0)}let n=Math.log(e);if(t<=11){let l=wt(qi,t);if(n>=l)return 1e-99;let i=wt(yi,t),s=Math.exp(wt(Mi,t));return P(-Math.log(l-n),{mean:i,sd:s,lowerTail:!1})}let r=Math.log(t),o=wt(xi,r),a=Math.exp(wt(Ei,r));return P(n,{mean:o,sd:a,lowerTail:!1})}function ys(e,t,n={}){if(typeof e=="string"){let h=Xo(e,t);return ys(h.values,h.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=gt(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,d)=>h+d,0),m,u;if(a){let h=se(o.flat()),d=0,p=0;for(let g=0;g<l;g++)d+=i[g]*(s[g]-h)**2,p+=(i[g]-1)*c[g];u=f-l,m=d/(l-1)/(p/u)}else{let h=i.map((w,b)=>w/c[b]),d=h.reduce((w,b)=>w+b,0),p=0,g=0;for(let w=0;w<l;w++)p+=(1-h[w]/d)**2/(i[w]-1),g+=h[w]*s[w];p/=l*l-1,g/=d;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)*p)),u=1/(3*p)}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 Ti(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=bs(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:Ns(r,n)}}function Tr(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=X(r,[e[o],0]);return r[0]}function Si([[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]=kt(f,o,a,l,!1);return{lo:i,d:c}}function Li(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 jn=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let a=Si(o);n+=a.lo,r=Li(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=Tr(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=X(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?Tr(o,a,o.length):Tr(o,0,a+1)}};function Sr(e,t){return t?qe(e,0,1):1/qe(n=>e(1/n),Number.EPSILON,1)}function Ms(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Sr(r=>e.mean(r)-t,n>t)}function Lr(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Sr(o=>n-e.cdf(t,o,!1),r<n)}function Ir(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Sr(o=>e.cdf(t,o,!0)-n,r>n)}function Ii(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,d)=>h+d,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(Ee(c),r===2&&o===2){let f=e.map(m=>[[m[0][0],m[0][1]],[m[1][0],m[1][1]]]);return s?Fi(f,l,c):Oi(f,a,l,i,c)}return _i(e,a,r,o)}function Oi(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],k=N+M,S=E+y,L=N+E,A=M+y;a+=N,l+=k*L/q,i+=k*S*L*A/(q*q*(q-1));let R=(N+y)/q,C=(M+E)/q,F=N*y/q,I=M*E/q;s+=F,c+=I,f+=R*F,m+=R*I+C*F,u+=C*I});let h=a-l,d=r&&Math.abs(h)>=.5?.5:0,p=(Math.abs(h)-d)**2/i,g=n==="two.sided"?he(p,.5,2,!1,!1):P(Math.sign(h)*Math.sqrt(p),{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(B(o)*w)];else if(n==="greater")b=[v*Math.exp(B(o,{lowerTail:!1})*w),1/0];else{let N=B((1+o)/2);b=[v*Math.exp(-N*w),v*Math.exp(N*w)]}return{method:`Mantel-Haenszel chi-squared test ${d!==0?"with":"without"} continuity correction`,alternative:n,statistic:p,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 Fi(e,t,n){let r=new jn(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=X(c,[f,0]));a=Math.min(1,c[0])}else a=r.cdf(o,1,t==="greater");let l;if(t==="less")l=[0,Lr(r,o,1-n)];else if(t==="greater")l=[Ir(r,o,1-n),1/0];else{let i=(1-n)/2;l=[Ir(r,o,i),Lr(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:Ms(r,o),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function _i(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(p=>p.reduce((g,v)=>g+v,0)),h=Array.from({length:r},(p,g)=>c.reduce((v,w)=>v+w[g],0)),d=1/(m*m*(m-1));for(let p=0;p<r-1;p++)for(let g=0;g<n-1;g++){let v=g+(n-1)*p;a[v]+=c[g][p]-u[g]*h[p]/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=(p===w?m*h[p]:0)-h[p]*h[w];l[v][b+(n-1)*w]+=d*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 xs=new Map;function Vi(e,t){let n=`${e},${t}`,r=xs.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,d=a[u],p=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&&(p[g+1][N+d]+=M*b),w<h&&(p[g][N]+=M*(1-b)))}}i=p}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 xs.set(n,m),m}function Or(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}=Vi(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 Es=new Map;function Ci(e,t){let n=`${e},${t}`,r=Es.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 d=0;d<=e;d++){if(e-d>o-m)continue;let p=l[d];for(let g=0;g<=a;g++){let v=p[g];v!==0&&(d<e&&(h[d+1][g+u]+=v),e-d<o-m&&(h[d][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 Es.set(n,f),f}function Wn(e,t,n){let{min:r,counts:o}=Ci(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let a=Qo(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 qs(e,t,n={}){if(typeof e=="string"){let b=Je(e,t);return qs(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&&Ee(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 d=new Set(m).size<f,p=n.exact??(a<50&&l<50&&!d),g,v;if(p&&!d){let b=M=>Or(M-1,a,l,!1),N=M=>Or(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=$i(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(P(y),P(y,{lowerTail:!1})):i==="less"?P(y,{lowerTail:!1}):P(y),s&&(v=Pi(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 ks(e,t,n){let r=e.length,o=r+t.length,a=Di(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 Di(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 Fr(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function $i(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:Fr(n,1-r,()=>[NaN,NaN]),estimate:NaN};let s=l.map((g,v)=>ks(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=Wn(g/2,o,a),w=Wn(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=Fr(n,1-r,f),u=Wn(.5,o,a),h=-1/0,d=1/0;s.forEach((g,v)=>{g-u<=0?h=Math.max(h,l[v]):d=Math.min(d,l[v])});let p=h===-1/0||d===1/0?NaN:(d+h)/2;return{confInt:m,estimate:p}}function Pi(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}=ks(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=>qe(w=>s(w,v),m,u,{tol:1e-4}),d=v=>{let w=B(v/2),b=B(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]},p=Fr(n,1-r,d),g=s(m,0)>0||s(u,0)<0?NaN:h(0);return{confInt:p,estimate:g}}function zi(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;Rn(s);let c=me(r.concat(o)),f=c.slice(0,a),m=c.slice(a),u=x=>x.reduce((q,k)=>q+k,0)/x.length,h=u(f),d=u(m),p=(x,q,k)=>{let S=(x.length+1)/2,L=0;for(let A=0;A<x.length;A++)L+=(x[A]-q[A]-k+S)**2;return L/(x.length-1)},g=p(f,me(r),h),v=p(m,me(o),d),w=(d-(l+1)/2)/a,b=a*g+l*v,N=a*l*(d-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=We((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 ji=[1.2337005501361697,7831619385924639e-32];function Wi(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let d=0;d<t;d++){let p=f[h*t+d];if(p!==0)for(let g=0;g<t;g++)u[h*t+g]+=p*m[d*t+g]}return u},o=f=>{let m=0;for(let d of f)m=Math.max(m,Math.abs(d));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let d=0;d<f.length;d++)f[d]*=h;return u},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 As(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]=Et(e,t),u=2*f-1+2*m,h=1,d=0;for(let p=1;p<=e;p++)h*=p/e*u,h<2**-500&&(h*=2**500,d-=500);for(;d<-1e3;)h*=2**-1e3,d+=1e3;return h*2**d}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,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 d=2;d<=u;d++)h*=d;a[f*r+m]/=h}}let[l,i]=Wi(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 _r(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(ji,Et(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 Rs(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]=Et(e,t),o=Math.floor(e-n-r),a=0;for(let l=0;l<=o;l++){let i=X(st(e-l,-n),[-r,0]);if(i[0]<=0)break;let s=X(st(l,n),[r,0]),c=be(s,[e,0]),f=be(i,[e,0]);a+=Oo(l,e-l,c,f,!1)/(c[0]+c[1])}return Math.min(t*a,1)}function Ts(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,d)=>h-d);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 d=u*n-h*t;return(o==="two.sided"?Math.abs(d):o==="greater"?d:-d)>=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 d=u+h,p=(u>0?u/d*c[h]:0)+(h>0?h/d*f[h-1]:0);(l===void 0?d<a:l[d])&&s(u,h)?(p>0&&(m+=p*kt(u,t,n,d,!1)),f[h]=0):f[h]=p}[c,f]=[f,c],f.fill(0)}return Math.min(m,1)}function Ss(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 Ls(e,t,n={}){if(typeof e=="string"){let w=Je(e,t);return Ls(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-As(o,N):_r(Math.sqrt(o)*N,!1):y=E?Rs(o,N):Math.exp(-2*o*N*N),{method:`${E?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Ss(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 d=a==="two.sided"?Math.max(u,h):a==="greater"?u:h,p=new Set(c.map(w=>w[0])).size<c.length,g=n.exact??o*s<1e4,v;if(g)v=Ts(d,o,s,p?{alternative:a,z:c.map(w=>w[0])}:{alternative:a});else{let w=o*s/(o+s);v=a==="two.sided"?_r(Math.sqrt(w)*d,!1):Math.exp(-2*w*d*d)}return{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Ss(a,!1),exact:g,statistic:d,statisticName:l,pValue:Math.min(1,Math.max(0,v))}}function Is(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 Vr(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=Ft(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 Cr(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 Gi(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}=Is(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),d=0,p=0;s.forEach(v=>{v.length>1&&(d+=re(v)*(v.length-1),p+=v.length-1)});let g=Math.sqrt(d/p);f=(v,w)=>{let b=(h[v]-h[w])/(g*Math.sqrt(1/u[v]+1/u[w]));return a==="less"?Ne(b,p,!0,!1):a==="greater"?Ne(b,p,!1,!1):2*Ne(-Math.abs(b),p,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>Ot(s[u],s[h],{paired:r,alternative:a}).pValue;return Cr(m,i,Vr(c,f,l),l)}function Ui(e,t,n={}){let{pAdjustMethod:r,alternative:o,...a}=n,l=r??"holm",{levels:i,groups:s}=Is(e,t,!(a.paired??!1)),c=i.length;if(c<2)throw new RangeError("there must be at least two groups");let f="",m=Vr(c,(u,h)=>{let d=on(s[u],s[h],{...a,...o===void 0?{}:{alternative:o}});return f=d.method,d.pValue},l);return Cr(f,i,m,l)}function Hi(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=Vr(a,(f,m)=>qr([r[f],r[m]],[o[f],o[m]],{correct:i}).pValue,l),c=Array.from({length:a},(f,m)=>m+1);return Cr("Pairwise comparison of proportions",c,s,l)}var sn=[0,0];function Os(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=le(e,Et(t,t));return st(t,n[0]/(2*t))}function Gn(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Os([e[r],0]))}function _t(e,t){return Ge(e,t,[]).fit}function Ge(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=sn;for(let v=f;v<r;v++)u=X(u,oe(m[v],m[v]));if(u[0]===0){s.push(sn);continue}let h=Os(u),d=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=X(m[f],d);let p=oe(d,m[f]),g=v=>{let w=sn;for(let N=f;N<r;N++)w=X(w,oe(m[N],v[N]));let b=be(w,p);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([-d[0],-d[1]])}let c=new Array(o).fill(sn);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 an(e,t=0,n=e.length){let r=sn;for(let o=t;o<n;o++)r=X(r,oe(e[o],e[o]));return r}function Bi(e,t,n,r,o){let a=t.length,l=n.flatMap((p,g)=>an(p)[0]>1e-5?[g]:[]),i=0,s=[],c=t.slice();if(l.length>0){let p=pe(Array.from({length:a},(g,v)=>l.map(w=>n[w][v][0])));i=p.rank,s=p.pivot.slice(0,i).map(g=>l[g]),c=_t(s.map(g=>n[g]),t).qty}let f=[];s.forEach((p,g)=>{let v=r[p],w=f.find(b=>b.term===v);w===void 0&&f.push(w={term:v,df:0,ss:[0,0]}),w.df++,w.ss=X(w.ss,oe(c[g],c[g]))});let m=a-i,u=an(c,i)[0],h=f.filter(p=>p.term!==0),d=h.map(p=>o[p.term-1]);if(m>0){let p=u/m,g=h.map(({df:v,ss:w})=>{let b=w[0]/v/p;return[v,w[0],w[0]/v,b,ae(b,v,m,!1,!1)]});return g.push([m,u,p,NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...d,"Residuals"],table:g}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:d,table:h.map(({df:p,ss:g})=>[p,g[0],g[0]/p])}}function Fs(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=Ho({response:t.response,rhs:l},Object.keys(o)).variables,s=ht(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=ht({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,d=h>0?pe(f.x):void 0,p=d?.rank??0;p<h&&u.push("Error() model is singular");let g=d?.pivot.slice(0,p)??[],v=y=>y.map(x=>[x,0]),w=s.colNames.map((y,x)=>v(s.x.map(q=>q[x]))),{fit:b,qtv:N}=Ge(g.map(y=>v(f.x.map(x=>x[y]))),v(c),w),M=[];g.forEach((y,x)=>{let q=f.assign[y],k=q===0?"(Intercept)":f.terms.termLabels[q-1],S=M.find(L=>L.name===k);S===void 0&&M.push(S={name:k,idx:[]}),S.idx.push(x)}),m>p&&M.push({name:"Within",idx:Array.from({length:m-p},(y,x)=>p+x)});let E=M.map(y=>Bi(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 Dr(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=_s(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 $r(e,t,n={}){return typeof e=="string"?Dr(e,ht(kn(e),t,{...n,dropUnusedLevels:!0}),!1):Xi(e,t,n)}function Xi(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 _s(s,c,f,r).result}function Qi(e,t,n,r,o,a){let l=o.rank,i=o.pivot.slice(0,l),s=Gn(n===void 0?void 0:r.map(w=>n[w]),r.length),c=i.map(w=>r.map((b,N)=>Le(s[N],e[b][w]))),{qty:f,coef:m}=_t(c,r.map((w,b)=>Le(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=X(b,Le(m[M],w[N]))),b}),d=an(f,l)[0],p=r.map(w=>n?.[w]??1),g=[0,0];if(a){let w=[0,0],b=[0,0];r.forEach((N,M)=>{w=X(w,[p[M],0]),b=X(b,Le(h[N],p[M]))}),g=be(b,w)}let v=[0,0];return r.forEach((w,b)=>{let N=le(h[w],g);v=X(v,Le(oe(N,N),p[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:d,mss:v[0]}}function _s(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]),d=pe(u),p=d.rank,g=o?Qi(e,t,n,f,d,r):void 0,v=g?.coefficients??Lt(d,h),w=g?.residuals??[],b=g?.fittedValues??[],N=c-p,M=s??new Array(c).fill(1),E=g?.rss??0;if(g===void 0){let D=Tn(d,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((_e,Ve,Oe)=>Number.isNaN(v[Oe])?_e:_e+Ve*v[Oe],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),k=d.qr.slice(0,p).map(D=>D.slice(0,p)),S=p>0?Ln(k):[],A=d.pivot.slice(0,p).map((D,G)=>[D,G]).sort((D,G)=>D[0]-G[0]),R=A.map(([D])=>v[D]),C=A.map(([,D])=>Math.sqrt(S[D][D]*x)),F=R.map((D,G)=>D/C[G]),I=F.map(D=>2*Ne(-Math.abs(D),N,!0,!1)),V={coefficients:v,residuals:w,fittedValues:b,rank:p,dfResidual:N,coefTable:{estimate:R,stdError:C,tValue:F,pValue:I},sigma:q,rSquared:0,adjRSquared:0,intercept:r,qr:d},j=r?1:0,Y=p-j,T={result:V,fitRows:f,effects:g?.effects??[],rss:E};if(Y===0)return T;let O=0;if(g!==void 0)O=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++)O+=M[Q]*(y[Q]-G)**2}else for(let D=0;D<c;D++)O+=M[D]*y[D]**2;V.rSquared=O/(O+E),V.adjRSquared=1-(1-V.rSquared)*((c-j)/N);let $=O/Y/x;return V.fStatistic={value:$,numdf:Y,dendf:N},V.fPValue=ae($,Y,N,!1,!1),T}function Ki(e,t={}){let n=t.level??.95,r=We((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 Zi(e,t,n={}){let o=(t.length>0&&typeof t[0]!="number"?t.map(d=>Array.from(d)):Array.from(t,d=>[d])).map(d=>e.intercept?[1,...d]:d),a=e.rank,l=e.qr.pivot.slice(0,a),i=e.qr.qr.slice(0,a).map(d=>d.slice(0,a)),s=e.sigma**2,c=o.map(d=>d.reduce((p,g,v)=>Number.isNaN(e.coefficients[v])?p:p+g*e.coefficients[v],0)),f=o.map(d=>{let p=new Array(a).fill(0);for(let g=0;g<a;g++){let v=d[l[g]];for(let w=0;w<g;w++)v-=i[w][g]*p[w];p[g]=v/i[g][g]}return Math.sqrt(p.reduce((g,v)=>g+v*v,0)*s)}),m=n.interval??"none";if(m==="none")return{fit:c,seFit:f};let u=We((1+(n.level??.95))/2,e.dfResidual,!0,!1),h=f.map(d=>u*Math.sqrt(d*d+(m==="prediction"?s:0)));return{fit:c,seFit:f,lwr:c.map((d,p)=>d-h[p]),upr:c.map((d,p)=>d+h[p])}}function Vs(e){let t=e.flat(),n=en(t),r=[0,0],o=[0,0],a=[];for(let l of e){let i=en(l);a.push(i[0]);let s=le(i,n);r=X(r,oe(oe(s,s),[l.length,0]));for(let c of l){let f=le([c,0],i);o=X(o,oe(f,f))}}return{ssb:r[0],ssw:o[0],means:a}}function Pr(e,t,n={}){return typeof e=="string"?Yi(e,t,n):Ji(e,t)}function Yi(e,t,n){let r=kn(e,!0),{treatment:o,errors:a}=Uo(r.rhs);if(a.length>1)throw new Error(`there are ${a.length} Error terms: only 1 is allowed`);return a.length===1?Fs(e,r,o,a[0],t,n):Dr(e,ht(r,t,{...n,dropUnusedLevels:!0},o),!0)}function Ji(e,t){let n=gt(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}=Vs(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 el(e,t,n={}){let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:a}=dt(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((p,g)=>p+g.length,0)-l,{ssw:f,means:m}=Vs(a),u=f/c,h=jo(i,l,c),d={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:[]};for(let p=0;p<l-1;p++)for(let g=p+1;g<l;g++){let v=m[g]-m[p],w=Math.sqrt(u/2*(1/a[g].length+1/a[p].length));d.names.push(`${o[g]}-${o[p]}`),d.i.push(g+1),d.j.push(p+1),d.diff.push(v),d.lwr.push(v-h*w),d.upr.push(v+h*w),d.pAdj.push(xn(Math.abs(v)/w,l,c,{lowerTail:!1}))}return d}function tl(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=en(c)[0],u=f===void 0?NaN:en(f)[0],h=f===void 0?o/Math.sqrt(c.length):Math.sqrt(o**2/c.length+a**2/f.length),d=f===void 0?m:m-u,p=(d-l)/h,g,v;if(i==="less")g=P(p),v=[-1/0,d+B(s)*h];else if(i==="greater")g=P(p,{lowerTail:!1}),v=[d-B(s)*h,1/0];else{g=2*P(-Math.abs(p));let b=B(1-(1-s)/2)*h;v=[d-b,d+b]}let w=f!==void 0;return{method:w?"Two-sample z-Test":"One-sample z-Test",alternative:i,statistic:p,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 ke=2**-52,nl={gaussian:["identity","log","inverse"],binomial:["logit","probit","cauchit","log","cloglog"],quasibinomial:["logit","probit","cauchit","log","cloglog"],poisson:["log","identity","sqrt"],quasipoisson:["log","identity","sqrt"],Gamma:["inverse","identity","log"],"inverse.gaussian":["1/mu^2","inverse","identity","log"],quasi:["identity","logit","probit","cloglog","inverse","log","1/mu^2","sqrt"]},zr=e=>Number.isFinite(e)&&e>0,Vt=()=>!0,Me=class{list=[];add(t){this.list.includes(t)||this.list.push(t)}},Un;function Hn(e,t){let n=Un;Un=e;try{return t()}finally{Un=n}}function rl(e){return e<0&&Un?.add("NaNs produced"),Math.sqrt(e)}function ol(e){if(!Number.isFinite(e))return Number.isNaN(e)?"nan":e>0?"inf":"-inf";let t=e.toExponential(5),n=Number(t.slice(t.indexOf("e")+1));if(n>=-4&&n<6)return e.toFixed(5-n).replace(/\.?0+$/,"");let[r,o]=t.split("e");return`${r.replace(/\.?0+$/,"")}e${n<0?"-":"+"}${String(Math.abs(Number(o))).padStart(2,"0")}`}function sl(e){if(e<0||e>1)throw new RangeError(`Value ${ol(e)} out of range (0, 1)`);return Math.log(e/(1-e))}var al=["logit","probit","cauchit","cloglog","identity","log","sqrt","1/mu^2","inverse"];function jr(e){switch(e){case"identity":return{name:e,link:t=>t,linkinv:t=>t,muEta:()=>1,etaOk:Vt};case"log":return{name:e,link:Math.log,linkinv:t=>Math.max(Math.exp(t),ke),muEta:t=>Math.max(Math.exp(t),ke),etaOk:Vt};case"inverse":return{name:e,link:t=>1/t,linkinv:t=>1/t,muEta:t=>-1/(t*t),etaOk:t=>Number.isFinite(t)&&t!==0};case"sqrt":return{name:e,link:Math.sqrt,linkinv:t=>t*t,muEta:t=>2*t,etaOk:zr};case"1/mu^2":return{name:e,link:t=>1/(t*t),linkinv:t=>1/rl(t),muEta:t=>-1/(2*t**1.5),etaOk:zr};case"logit":return{name:e,link:sl,linkinv:t=>{let n=t<-30?ke:t>30?1/ke:Math.exp(t);return n/(1+n)},muEta:t=>{if(Math.abs(t)>30)return ke;let n=Math.exp(t);return n/((1+n)*(1+n))},etaOk:Vt};case"probit":{let t=-B(ke);return{name:e,link:n=>B(n),linkinv:n=>P(Math.min(Math.max(n,-t),t)),muEta:n=>Math.max(xe(n),ke),etaOk:Vt}}case"cauchit":{let t=-hr(ke,0,1,!0,!1);return{name:e,link:n=>hr(n,0,1,!0,!1),linkinv:n=>Do(Math.min(Math.max(n,-t),t),0,1,!0,!1),muEta:n=>Math.max(Co(n,0,1,!1),ke),etaOk:Vt}}case"cloglog":return{name:e,link:t=>Math.log(-Math.log(1-t)),linkinv:t=>Math.min(Math.max(-Math.expm1(-Math.exp(t)),ke),1-ke),muEta:t=>{let n=Math.min(t,700);return Math.max(Math.exp(n)*Math.exp(-Math.exp(n)),ke)},etaOk:Vt};default:throw new RangeError(`unknown link "${String(e)}"`)}}function il(e){if(!Number.isFinite(e))throw new RangeError("invalid power for the power link");if(e<=0)return jr("log");if(e===1)return jr("identity");let t=1/e;return{name:`mu^${Math.round(e*1e3)/1e3}`,link:n=>n**e,linkinv:n=>Math.max(n**t,ke),muEta:n=>Math.max(t*n**(t-1),ke),etaOk:zr}}function ll(e){switch(e){case"constant":return{variance:()=>1,muOk:()=>!0,dev:(t,n,r)=>r*(t-n)**2};case"mu(1-mu)":return{variance:t=>t*(1-t),muOk:t=>t>0&&t<1,dev:Cs};case"mu":return{variance:t=>t,muOk:t=>t>0,dev:(t,n,r)=>2*r*((t===0?0:t*Math.log(t/n))-(t-n))};case"mu^2":return{variance:t=>t*t,muOk:t=>t>0,dev:(t,n,r)=>Math.max(-2*r*(Math.log(t>0?t/n:1/n)-(t-n)/n),0)};case"mu^3":return{variance:t=>t*t*t,muOk:t=>t>0,dev:Ds};default:throw new RangeError(`'variance' "${String(e)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`)}}function Cs(e,t,n){let r=e===0?0:e*Math.log(e/t),o=e===1?0:(1-e)*Math.log((1-e)/(1-t));return 2*n*(r+o)}function Ds(e,t,n){return n*(e-t)**2/(e*t*t)}function ln(e,t){let n=typeof e=="string"?{family:e}:e,r=n.family,o=nl[r];if(o===void 0)throw new RangeError(`unknown family "${String(r)}"`);let a=t??n.link??o[0],l;if(typeof a=="object"){if(r!=="quasi")throw new RangeError(`link "power" not available for ${r} family`);l=il(a.power)}else{if(r==="quasi"&&!o.includes(a))throw new RangeError(`link "${a}" not available for ${r} family; available links are ${o.map(s=>`'${s}'`).join(", ")}`);if(!al.includes(a))throw new RangeError(`\u2018${a}\u2019 link not recognised`);l=jr(a)}if(n.variance!==void 0&&r!=="quasi")throw new TypeError("'variance' is only used by the quasi family");let i={...l,family:r,varianceName:void 0,fixedDispersion:!1};switch(r){case"gaussian":return{...i,variance:()=>1,muOk:()=>!0,devContribution:(s,c,f)=>f*(s-c)**2};case"binomial":case"quasibinomial":return{...i,fixedDispersion:r==="binomial",variance:s=>s*(1-s),muOk:s=>Number.isFinite(s)&&s>0&&s<1,devContribution:Cs};case"poisson":case"quasipoisson":return{...i,fixedDispersion:r==="poisson",variance:s=>s,muOk:s=>Number.isFinite(s)&&s>0,devContribution:(s,c,f)=>s>0?2*f*(s*Math.log(s/c)-(s-c)):2*f*c};case"Gamma":return{...i,variance:s=>s*s,muOk:s=>Number.isFinite(s)&&s>0,devContribution:(s,c,f)=>-2*f*((s===0?0:Math.log(s/c))-(s-c)/c)};case"inverse.gaussian":return{...i,variance:s=>s*s*s,muOk:()=>!0,devContribution:Ds};case"quasi":{let s=n.variance??"constant",c=ll(s);return{...i,varianceName:s,variance:c.variance,muOk:c.muOk,devContribution:c.dev}}}}function Wr(e,t,n,r,o){let a=e.family,l=a==="binomial"||a==="quasibinomial";if(t.length>0&&Array.isArray(t[0])){let f=t;if(f.every(p=>p.length===1))return Wr(e,f.map(p=>p[0]),n,r,o);if(!l)throw new TypeError("y must be a vector (or, for the binomial families, a two-column matrix)");if(f.some(p=>p.length!==2))throw new TypeError(`for the '${a}' family, y must be a vector of 0 and 1's
3
+ or a 2 column matrix where col 1 is no. successes and col 2 is no. failures`);a==="binomial"&&f.some(p=>Math.abs(p[0]-Math.round(p[0]))>.001||Math.abs(p[1]-Math.round(p[1]))>.001)&&o.add("non-integer counts in a binomial glm!");let m=f.map(p=>p[0]+p[1]),u=f.map((p,g)=>m[g]===0?0:p[0]/m[g]),h=n.map((p,g)=>p*m[g]),d=u.map((p,g)=>(m[g]*p+.5)/(m[g]+1));return{y:u,weights:h,mustart:d,size:m}}let s=t.slice(),c=n.slice();switch(a){case"binomial":case"quasibinomial":{for(let m=0;m<s.length;m++)c[m]===0&&(s[m]=0);if(s.some(m=>m<0||m>1))throw new RangeError("y values must be 0 <= y <= 1");let f=s.map((m,u)=>(c[u]*m+.5)/(c[u]+1));return a==="binomial"&&s.some((m,u)=>Math.abs(c[u]*m-Math.round(c[u]*m))>.001)&&o.add("non-integer #successes in a binomial glm!"),{y:s,weights:c,mustart:f,size:s.map(()=>1)}}case"poisson":case"quasipoisson":if(s.some(f=>f<0))throw new RangeError(`negative values not allowed for the '${a==="poisson"?"Poisson":"quasiPoisson"}' family`);return{y:s,weights:c,mustart:s.map(f=>f+.1),size:void 0};case"Gamma":if(s.some(f=>f<=0))throw new RangeError("non-positive values not allowed for the 'Gamma' family");return{y:s,weights:c,mustart:s.slice(),size:void 0};case"inverse.gaussian":if(s.some(f=>f<=0))throw new RangeError("positive values only are allowed for the 'inverse.gaussian' family");return{y:s,weights:c,mustart:s.slice(),size:void 0};case"gaussian":if(!r&&(e.name==="inverse"&&s.some(f=>f===0)||e.name==="log"&&s.some(f=>f<=0)))throw new Error("cannot find valid starting values: please specify some");return{y:s,weights:c,mustart:s.slice(),size:void 0};case"quasi":{let f=e.varianceName,m=f==="constant"?s.slice():f==="mu(1-mu)"?s.map(u=>Math.min(Math.max(u,.001),.999)):s.map(u=>u+(u===0?.1:0));return{y:s,weights:c,mustart:m,size:void 0}}}}function $s(e,t,n,r,o,a,l){let i=t.length,s=0;switch(e.family){case"gaussian":{let c=0;for(let f of r)c+=Math.log(f);return i*(Math.log(2*Math.PI*a/i)+1)+2-c}case"binomial":{let c=o!==void 0&&o.some(f=>f>1)?o:r;for(let f=0;f<i;f++)c[f]>0&&(s+=r[f]/c[f]*qt(Zt(c[f]*t[f]),Zt(c[f]),n[f],!0));return-2*s}case"poisson":for(let c=0;c<i;c++)Vo(t[c])&&l.add(`non-integer x = ${t[c].toFixed(6)}`),s+=r[c]*Yt(t[c],n[c],!0);return-2*s;case"Gamma":{let c=0;for(let m of r)c+=m;let f=a/c;for(let m=0;m<i;m++)s+=r[m]*Fo(t[m],1/f,n[m]*f,!0);return-2*s+2}case"inverse.gaussian":{let c=0,f=0;for(let m=0;m<i;m++)c+=r[m],f+=r[m]*Math.log(t[m]);return c*(Math.log(2*Math.PI*a/c)+1)+3*f+2}default:return NaN}}var Ct=2**-52;function cl(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function Bn(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 vt(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 Ps(e,t){let{x:n,p:r,y:o,w:a,offset:l,fam:i,epsilon:s,maxit:c}=e,f=o.length;return Hn(t,()=>{let m=(L,A)=>L.every(R=>i.etaOk(R))&&A.every(R=>i.muOk(R));if(r===0){let L=l.slice();if(!L.every(F=>i.etaOk(F)))throw new Error("invalid linear predictor values in empty model");let A=L.map(F=>i.linkinv(F));if(!A.every(F=>i.muOk(F)))throw new Error("invalid fitted means in empty model");let R=L.map(F=>i.muEta(F)),C=A.map((F,I)=>Math.sqrt(a[I]*R[I]**2/i.variance(F))**2);return{coefficients:[],eta:L,mu:A,deviance:vt(i,o,A,a),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:a.map(F=>F>0),workingWeights:C,workingResiduals:o.map((F,I)=>(F-A[I])/R[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 ${cl(e.columnNames)}`);h=e.start.slice(),u=Bn(n,h,l)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(L=>i.link(L));let d=u.map(L=>i.linkinv(L));if(!m(u,d))throw new Error("cannot find valid starting values: please specify some");let p=vt(i,o,d,a),g=p,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(d[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 A=[];for(let I=0;I<f;I++)E[I]&&A.push(I);if(A.length===0){t.add(`no observations informative at iteration ${b}`),v=!1;break}y=new Array(f).fill(0);let R=[],C=[];for(let I of A){let V=Math.sqrt(a[I]*L[I]**2/i.variance(d[I]));y[I]=V,R.push((u[I]-l[I]+(o[I]-d[I])/L[I])*V),C.push(n[I].map(j=>j*V))}M=pe(C,{tol:x});let F=Lt(M,R);if(F.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=F.map(I=>Number.isNaN(I)?0:I),w=!1,u=Bn(n,N,l),d=u.map(I=>i.linkinv(I)),g=vt(i,o,d,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,j)=>(V+h[j])/2),u=Bn(n,N,l),d=u.map(V=>i.linkinv(V)),g=vt(i,o,d,a)}w=!0}if(!m(u,d)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated: out of bounds");let I=0;for(;!m(u,d);){if(I>=c)throw new Error("inner loop 2; cannot correct step size");I++,N=N.map((V,j)=>(V+h[j])/2),u=Bn(n,N,l),d=u.map(V=>i.linkinv(V))}w=!0,g=vt(i,o,d,a)}if(e.untilCoefficients){let I=Math.max(...N.map(Math.abs));if(h!==void 0&&N.every((V,j)=>Math.abs(V-h[j])<=8*Ct*I)){v=!0;break}}else if(Math.abs(g-p)/(Math.abs(g)+.1)<s){v=!0;break}p=g,h=N.slice()}b>c&&(b=c);let q=r,k=N.slice();if(M!==void 0){q=M.rank;for(let L=M.rank;L<r;L++)k[M.pivot[L]]=NaN}let S=u.map(L=>i.muEta(L));return{coefficients:k,eta:u,mu:d,deviance:g,iter:b,converged:v,boundary:w,rank:q,qr:M,used:E,workingWeights:y.map(L=>L*L),workingResiduals:o.map((L,A)=>(L-d[A])/S[A])}})}function zs(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function js(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*Ct||o<10*Ct)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*Ct)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function Dt(e,t,n,r={}){let o=ln(e.familySpec),a="start"in r?r.start:e.start,l=Wr(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=Ps({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 js(c,o,n,t.length===0),{fit:c,fam:o,y:l.y,w:l.weights,size:l.size,offset:i}}function fl(e,t,n={}){let r=n.intercept??!0,o=zs(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,F)=>{let I=l[F];return!(Array.isArray(I)?I.some(j=>Number.isNaN(j)):Number.isNaN(I))&&C.every(j=>!Number.isNaN(j))&&!(s!==void 0&&Number.isNaN(s[F]))&&!(c!==void 0&&Number.isNaN(c[F]))}),m=o.filter((C,F)=>f[F]).map(C=>r?[1,...C]:C),u=l.filter((C,F)=>f[F]),h=u.length;if(h===0)throw new RangeError("0 (non-NA) cases");let d=s===void 0?new Array(h).fill(1):s.filter((C,F)=>f[F]);if(d.some(C=>C<0))throw new RangeError("negative weights not allowed");let p=c===void 0?new Array(h).fill(0):c.filter((C,F)=>f[F]),g=o.length>0?o[0].length:0,v=[...r?["(Intercept)"]:[],...Array.from({length:g},(C,F)=>`x${F+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:d,offset:p,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 Me,E=Array.from({length:w},(C,F)=>F),{fit:y,fam:x,y:q,w:k,size:S}=Dt(N,E,M),L;if(r&&N.hasOffset){let C=new Me,F=Ps({x:m.map(()=>[1]),p:1,y:q,w:k,offset:p,fam:x,epsilon:N.epsilon,maxit:N.maxit,columnNames:["(Intercept)"],mustart:y.mu},C);js(F,x,C,!1),C.list.forEach(I=>M.add(I)),L=F.deviance,F.converged||M.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let C=0,F=0;for(let V=0;V<h;V++)C+=k[V],F+=k[V]*q[V];let I=F/C;L=vt(x,q,q.map(()=>I),k)}else L=vt(x,q,Hn(M,()=>p.map(C=>x.linkinv(C))),k);let A=h-k.filter(C=>C===0).length,R=$s(x,q,y.mu,k,S,y.deviance,M)+2*y.rank;return{coefficients:y.coefficients,fittedValues:y.mu,linearPredictors:y.eta,deviance:y.deviance,nullDeviance:L,aic:R,rank:y.rank,dfResidual:A-y.rank,dfNull:A-(r?1:0),iter:y.iter,converged:y.converged,boundary:y.boundary,y:q,priorWeights:k,weights:y.workingWeights,residuals:y.workingResiduals,family:x.family,link:x.name,warnings:M.list,qr:y.qr,model:N}}function Xn(e){return ln(e.model.familySpec)}function $t(e,t){if(Xn(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 ml(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 Ln(t)}function Gr(e,t={}){let n=new Me,r=Xn(e),o=t.dispersion===void 0&&!r.fixedDispersion,a=t.dispersion??$t(e,n),l=ml(e),s=(e.qr===void 0?[]:e.qr.pivot.slice(0,e.rank)).map(h=>e.coefficients[h]),c=l.map((h,d)=>Math.sqrt(h[d]*a)),f=s.map((h,d)=>h/c[d]),m=!o,u=f.map(h=>m?2*P(-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(d=>d*a)),dfResidual:e.dfResidual,warnings:n.list}}function Ur(e,t="deviance"){let n=Xn(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 Hr(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=Jo(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*Ct?1:s)}),t}function ul(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 hl(e,t={}){let n=Hr(e),r=t.dispersion??$t(e),o=Ur(e,"deviance").filter((l,i)=>e.priorWeights[i]>0),a=100*Ct*ul(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 pl(e,t={}){let n=Hr(e),r=t.dispersion??$t(e);return Ur(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 dl(e,t={}){let n=Xn(e),r,o;if(t.newdata===void 0)r=e.model.x,o=e.linearPredictors.slice();else{r=zs(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,d,p)=>Number.isNaN(e.coefficients[p])?h:h+d*e.coefficients[p],0)+f[u])}let a=t.dispersion??$t(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 d=f[i[h]];for(let p=0;p<h;p++)d-=s[p][h]*m[p];m[h]=d/s[h][h],u+=m[h]*m[h]}return Math.sqrt(u)*Math.sqrt(a)});return(t.type??"link")==="link"?{fit:o,seFit:c}:Hn(new Me,()=>({fit:o.map(f=>n.linkinv(f)),seFit:c.map((f,m)=>f*Math.abs(n.muEta(o[m])))}))}var Ws={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Br(e,t,n,r,o=1e-11){let a=[];for(let p=0;p<t.length;p++)n[p]>0&&a.push(p);let l=0,i=0;for(let p of a)l+=n[p],i+=n[p]*t[p];let s=r?i/l:0,c=0;for(let p of a)c+=n[p]*(t[p]-s)**2;if((e.length>0?e[0].length:0)===0||a.length===0)return 0;let m=a.map(p=>Math.sqrt(n[p])),u=pe(a.map((p,g)=>e[p].map(v=>v*m[g])),{tol:o}),h=Tn(u,a.map((p,g)=>t[p]*m[g])),d=0;for(let p of h)d+=p*p;return c-d}function gl(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 Me,c=Dt(l,i,s,{...Ws,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=Gr(m),h=u.dispersion,d=new Array(o).fill(NaN);(c.fit.qr===void 0?[]:c.fit.qr.pivot.slice(0,c.fit.rank)).forEach((E,y)=>d[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=Dt(l,x,new Me,{...Ws,start:void 0,etastart:v,extraOffset:l.x.map(S=>S[E]*y)}).fit,k=r==="LRT"?q.deviance-c.fit.deviance:Br(g,q.workingResiduals,q.workingWeights,!1);return Math.sign(y-f[E])*Math.sqrt(Math.max(k/h,0))},b=[B((1-n)/2),B((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,k=NaN,S=NaN;for(let V=0;V<=30;V++){let j=f[E]+q*2**V*d[E],Y=w(E,j);if(Math.abs(Y)>=Math.abs(b[x])){k=j,S=Y;break}}if(Number.isNaN(k))continue;let L=V=>V===f[E]?-b[x]:w(E,V)-b[x],[A,R]=x===0?[k,f[E]]:[f[E],k],[C,F]=x===0?[S-b[x],-b[x]]:[-b[x],S-b[x]],I=qe(L,A,R,{tol:1e-14*d[E],maxIter:500,fLower:C,fUpper:F});x===0?N[y]=I:M[y]=I}}),{lower:N,upper:M}}var wl=(e,t)=>he(e,t/2,2,!1,!1);function vl(e,t,n){let{fit:r,w:o}=Dt(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 Xr=(e,t)=>Br(t.x,e.fit.workingResiduals,e.fit.workingWeights,t.intercept);function bl(e,t={}){let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=new Me,o=Array.isArray(e)&&n.length>1,a=n[0],l=ln(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:Gs(q),x:q.model.x,intercept:q.model.intercept}));u=c.map((q,k)=>k===0?NaN:f[k]<0?-Xr(x[k],x[k-1]):Xr(x[k-1],x[k]))}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,k=x.intercept?[0]:[],S=A=>[...k,...Array.from({length:A},(R,C)=>C+k.length)],L=[];for(let A=0;A<=q;A++)A===q?L.push({dfRes:y.dfResidual,dev:y.deviance,fit:Gs(y),x:x.x,intercept:x.intercept}):A>0||s==="Rao"?L.push(vl(x,S(A),r)):L.push(void 0);c.push({name:"NULL",dfRes:y.dfNull,dev:y.nullDeviance});for(let A=1;A<=q;A++)c.push({name:`x${A}`,dfRes:L[A].dfRes,dev:L[A].dev});f=c.map((A,R)=>R===0?NaN:c[R-1].dfRes-A.dfRes),m=c.map((A,R)=>R===0?NaN:Math.max(c[R-1].dev-A.dev,0)),s==="Rao"&&(u=c.map((A,R)=>R===0?NaN:Xr(L[R-1],L[R]))),h=y}let d=new Me,p=t.dispersion??$t(h,d);d.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 k=y[q]/p*Math.sign(f[q]);return k<0||Number.isNaN(k)?NaN:wl(k,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 k=m[q]/f[q]/p;return q===0||f[q]===0||k<0?NaN:k});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*p*(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 Gs(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 Nl(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 Us=(e,t,n)=>ae(e,t,n,!1,!1);function yl(e){let t=Nl(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 Qr(e){let{names:t,rows:n,rdf:r,rss:o,perfect:a}=yl(e),l=o/r,i=n.map(({df:s,ss:c})=>{let f=c/s/l;return[s,c,c/s,f,Us(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 Ml(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:Us(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 Kr(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 Qr(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?Qr(e):Ml(r);return a.warnings=[...o,...a.warnings],a}function Zr(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=Qr(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 Hs(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(d=>e.x[u][d]*a[h]))),i=Gn(e.weights===void 0?void 0:o.map(u=>e.weights[u]),o.length),s=l.pivot.slice(0,l.rank),{qty:c}=_t(s.map(u=>o.map((h,d)=>Le(i[d],e.x[h][r[u]]))),o.map((u,h)=>Le(i[h],e.y[u]))),f=[0,0],m=0;return s.forEach((u,h)=>{u>=t.length&&(f=X(f,oe(c[h],c[h])),m++)}),{ss:f[0],df:m}}function Yr(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=d=>e.assign.flatMap((p,g)=>d.has(p)?[g]:[]),c=[...new Set(e.assign)],f=[],m=[];if(r===3){if(e.coefficients.some(d=>Number.isNaN(d)))throw new Error("there are aliased coefficients in the model");for(let d of c){let p=s(new Set([d])),g=s(new Set(c.filter(v=>v!==d)));f.push(d===0?"(Intercept)":e.termLabels[d-1]),m.push({ss:Hs(e,g,p).ss,df:p.length})}}else{let d=p=>e.factors.flatMap((g,v)=>g[p-1]>0?[v]:[]);for(let p of c){if(p===0)continue;let g=d(p),v=c.filter(N=>N!==0&&N!==p&&g.every(M=>e.factors[M][N-1]>0)),w=s(new Set(c.filter(N=>N!==p&&!v.includes(N)))),b=Hs(e,w,s(new Set([p])));f.push(e.termLabels[p-1]),m.push(b)}}let u=a/o,h=m.map(({ss:d,df:p})=>{let g=d/p/u;return[d,p,g,ae(g,p,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 Qn=[0,0];var Bs=e=>e.map(t=>t.map(n=>n[0]));function cn(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Qn);return r.forEach((a,l)=>{let i=t[l];for(let s=0;s<n;s++)o[s]=X(o[s],oe(a,i[s]))}),o})}function bt(e){let t=Qn;return e.forEach((n,r)=>t=X(t,n[r])),t}function Xs(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 Qs(e){let{a:t,sign:n,singular:r}=Xs(e);if(r)return Qn;let o=[n,0];return t.forEach((a,l)=>o=oe(o,a[l])),o}function Ks(e){let t=e.length,{a:n,perm:r,singular:o}=Xs(e);if(o)throw new Error("singular matrix");let a=Array.from({length:t},()=>new Array(t).fill(Qn));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 Zs=(e,t)=>he(e,t/2,2,!1,!1);function Jr(e){let t=e.length,n=Kt(bt(e),t),r=[1,0];for(let o=0;o<t;o++)r=oe(r,n);return n=be(Qs(e),r),n[0]}function eo(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=Zs(l,i),c=Zs(l,i+4);return s+a*(c-s)}var xl=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function El(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=Ge(f.map(m),u,e.map((v,w)=>e.map(b=>b[w]))).qtv,d=Array.from({length:r},(v,w)=>h.map(b=>b[w])),p=Ge(f.map(m),u,d).qtv,g=Array.from({length:c.rank-i},(v,w)=>i+w);return g.map(v=>g.map(w=>p[v][w]))}function ql(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(mt),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=X(M,[n[y][N],0]);let E=Kt(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=X(E,oe(y[b],y[M]));return E})),m=t.idata??{index:Array.from({length:o},(w,b)=>b+1)},u=w=>{let b=It(w,m);if(b.x.length!==o)throw new RangeError("idata must have one row per column of y");return xl(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),d=u(t.X??"~0"),p=El(f,h,d),g=p.length,v=Jr(p);return{statistic:v,statisticName:"W",pValue:eo(v,g,c,o),method:"Mauchly's test of sphericity"}}var Ys=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 z=e=>e==null;function Ie(e,t){if(e.filter(z).length!==1)throw new RangeError(t)}function Z(e,t,n,r={}){return ut(e,t,n,r).root}var Nt=(e,t,n,r)=>Mn(e,t,{ncp:n,lowerTail:r});function to(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var no="'sig.level' must be numeric in [0, 1]";function kl(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;Ie([t,n,o,r,a],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),to(a,no);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??Ys,f=l==="two.sample"?2:1,m=i==="two.sided"?2:1;m===2&&!z(n)&&(n=Math.abs(n));let u=(p,g,v,w)=>{let b=Math.max(1e-7,p-1)*f,N=ft(w/m,b,{lowerTail:!1}),M=Math.sqrt(p/f)*g/v,E=Nt(N,b,M,!1);return s&&m===2?E+Nt(-N,b,M,!0):E};if(z(r))r=u(t,n,o,a);else{let p=r;z(t)?t=Z(g=>u(g,n,o,a)-p,2,1e7,{tol:c,extendInt:"upX"}):z(o)?o=Z(g=>u(t,n,g,a)-p,n*1e-7,n*1e7,{tol:c,extendInt:"downX"}):z(n)?n=Z(g=>u(t,g,o,a)-p,o*1e-7,o*1e7,{tol:c,extendInt:"upX"}):a=Z(g=>u(t,n,o,g)-p,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,d={"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:d}}function Al(e={}){let{n:t,p1:n,p2:r,power:o}=e,a=e.sigLevel===void 0?.05:e.sigLevel;Ie([t,n,r,o,a],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),to(a,no);let l=ie(e.alternative,["two.sided","one.sided"]),i=e.strict??!1,s=e.tol??Ys,c=l==="two.sided"?2:1,f=(u,h,d,p)=>{let g=B(p/c,{lowerTail:!1});if(i&&c===2){let v=(h+d)/2,w=h*(1-h),b=d*(1-d),N=v*(1-v),M=Math.abs(h-d),E=Math.sqrt(w+b);return P((Math.sqrt(u)*M-g*Math.sqrt(2*N))/E)+P((Math.sqrt(u)*M+g*Math.sqrt(2*N))/E,{lowerTail:!1})}return P((Math.sqrt(u)*Math.abs(h-d)-g*Math.sqrt((h+d)*(1-(h+d)/2)))/Math.sqrt(h*(1-h)+d*(1-d)))},m=[];if(z(o))o=f(t,n,r,a);else{let u=o;z(t)?t=Z(h=>f(h,n,r,a)-u,1,1e7,{tol:s,extendInt:"upX"}):z(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")):z(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 Rl(e={}){let{groups:t,n,betweenVar:r,withinVar:o,power:a}=e,l=e.sigLevel===void 0?.05:e.sigLevel;if(Ie([t,n,r,o,a,l],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!z(t)&&t<2)throw new RangeError("number of groups must be at least 2");if(!z(n)&&n<2)throw new RangeError("number of observations in each group must be at least 2");to(l,no);let i=(s,c,f,m,u)=>{let h=(s-1)*c*f/m,d=s-1,p=(c-1)*s;return lt(Ye(u,d,p,{lowerTail:!1}),d,p,{ncp:h,lowerTail:!1})};if(z(a))a=i(t,n,r,o,l);else{let s=a;z(t)?t=Z(c=>i(c,n,r,o,l)-s,2,100):z(n)?n=Z(c=>i(t,c,r,o,l)-s,2,1e5):z(o)?o=Z(c=>i(t,n,r,c,l)-s,r*1e-7,r*1e7):z(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 Tl={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:Tl[n][["small","medium","large"].indexOf(r)],method:"Conventional effect size from Cohen (1982)"}}function Sl(e,t){return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function Ll(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 Il(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 Ol="\u2018sig.level\u2019 must be numeric in [0, 1]",Fl="\u2018power\u2019 must be numeric in [0, 1]";function et(e,t){let n=r=>!z(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(Ol);if(n(t))throw new RangeError(Fl)}function tt(e,t){return typeof e=="string"?Js(t,e).effectSize:e}var fn=["two.sided","less","greater"];function Kn(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function ea(e,t,n,r){if(r==="less")return Nt(ft(n,e),e,t,!0);if(r==="greater")return Nt(ft(n,e,{lowerTail:!1}),e,t,!1);let o=ft(n/2,e,{lowerTail:!1});return Nt(o,e,t,!1)+Nt(-o,e,t,!0)}function ta(e,t,n){return n==="greater"?P(B(1-t)-e,{lowerTail:!1}):n==="less"?P(B(t)-e):P(B(1-t/2)-e,{lowerTail:!1})+P(B(t/2)-e)}function _l(e={}){let{n:t,power:n}=e,r=tt(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;Ie([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),et(o,n);let a=ie(e.type,["two.sample","one.sample","paired"]),l=ie(e.alternative,fn),i=a==="two.sample"?2:1;l==="two.sided"&&!z(r)&&(r=Math.abs(r));let s=(m,u,h)=>ea((m-1)*i,Math.sqrt(m/i)*u,h,l);if(z(n))n=s(t,r,o);else{let m=n;z(t)?t=Z(u=>s(u,r,o)-m,2+1e-10,1e9):z(r)?r=Z(u=>s(t,u,o)-m,...Kn(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 Vl(e={}){let{n1:t,n2:n,power:r}=e,o=tt(e.d,"t"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Ie([t,n,o,r,a],"exactly one of n1, n2, d, power, and sig.level must be NULL"),et(a,r),!z(t)&&t<2)throw new RangeError("number of observations in the first group must be at least 2");if(!z(n)&&n<2)throw new RangeError("number of observations in the second group must be at least 2");let l=ie(e.alternative,fn);l==="two.sided"&&!z(o)&&(o=Math.abs(o));let i=(s,c,f,m)=>ea(s+c-2,f/Math.sqrt(1/s+1/c),m,l);if(z(r))r=i(t,n,o,a);else{let s=r;z(t)?t=Z(c=>i(c,n,o,a)-s,2+1e-10,1e9):z(n)?n=Z(c=>i(t,c,o,a)-s,2+1e-10,1e9):z(o)?o=Z(c=>i(t,n,c,a)-s,...Kn(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 ro(e,t,n,r,o,a){let{n:l,power:i}=t,s=tt(e,n==="h"?"p":"t"),c=t.sigLevel===void 0?.05:t.sigLevel;if(Ie([s,l,i,c],`exactly one of ${n}, n, power, and sig.level must be NULL`),et(c,i),!z(l)&&l<1)throw new RangeError("number of observations in each group must be at least 1");let f=ie(t.alternative,fn);f==="two.sided"&&!z(s)&&(s=Math.abs(s));let m=(u,h,d)=>ta(u*r(h),d,f);if(z(i))i=m(s,l,c);else{let u=i;z(s)?s=Z(h=>m(h,l,c)-u,...Kn(f,[a,10])):z(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 Cl(e={}){let t=ro(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 Dl(e={}){let t=ro(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 $l(e={}){let t=ro(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 Pl(e={}){let{n1:t,n2:n,power:r}=e,o=tt(e.h,"p"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Ie([o,t,n,r,a],"exactly one of h, n1, n2, power, and sig.level must be NULL"),et(a,r),!z(t)&&t<2)throw new RangeError("number of observations in the first group must be at least 2");if(!z(n)&&n<2)throw new RangeError("number of observations in the second group must be at least 2");let l=ie(e.alternative,fn);l==="two.sided"&&!z(o)&&(o=Math.abs(o));let i=(s,c,f,m)=>ta(s*Math.sqrt(c*f/(c+f)),m,l);if(z(r))r=i(o,t,n,a);else{let s=r;z(o)?o=Z(c=>i(c,t,n,a)-s,...Kn(l,[1e-10,10])):z(t)?t=Z(c=>i(o,c,n,a)-s,2+1e-10,1e9):z(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 zl(e={}){let{n:t,power:n}=e,r=tt(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Ie([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),et(o,n),!z(t)&&t<4)throw new RangeError("number of observations must be at least 4");let a=ie(e.alternative,fn);a==="two.sided"&&!z(r)&&(r=Math.abs(r));let l=(s,c,f,m)=>{let u=ft(m?f/2:f,s-2,{lowerTail:!1}),h=Math.sqrt(u**2/(u**2+s-2)),d=Math.atanh(c)+c/(2*(s-1)),p=Math.atanh(h),g=P((d-p)*Math.sqrt(s-3));return m?g+P((-d-p)*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(z(n))n=i(t,r,o),a==="less"&&(r=-r);else{let s=n;if(z(t))t=Z(c=>i(c,r,o)-s,4+1e-10,1e9);else if(z(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 jl(e){let{N:t,power:n}=e,{df:r}=e,o=tt(e.w,"chisq"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Ie([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(et(a,n),!z(o)&&o<0)throw new RangeError("w must be positive");if(!z(t)&&t<1)throw new RangeError("number of observations must be at least 1");let l=(i,s,c)=>je(_o(c,r,{lowerTail:!1}),r,{ncp:s*i**2,lowerTail:!1});if(z(n))n=l(o,t,a);else{let i=n;z(o)?o=Z(s=>l(s,t,a)-i,1e-10,1e9):z(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 Wl(e={}){let{k:t,n,power:r}=e,o=tt(e.f,"anov"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Ie([t,n,o,r,a],"exactly one of k, n, f, power, and sig.level must be NULL"),et(a,r),!z(o)&&o<0)throw new RangeError("f must be positive");if(!z(t)&&t<2)throw new RangeError("number of groups must be at least 2");if(!z(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 lt(Ye(f,m,u,{lowerTail:!1}),m,u,{ncp:i*s*c**2,lowerTail:!1})};if(z(r))r=l(t,n,o,a);else{let i=r;z(t)?t=Z(s=>l(s,n,o,a)-i,2+1e-10,100):z(n)?n=Z(s=>l(t,s,o,a)-i,2+1e-10,1e9):z(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 Gl(e={}){let{u:t,v:n,power:r}=e,o=tt(e.f2,"f2"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Ie([t,n,o,r,a],"exactly one of u, v, f2, power, and sig.level must be NULL"),et(a,r),!z(o)&&o<0)throw new RangeError("f2 must be positive");if(!z(t)&&t<1)throw new RangeError("degree of freedom u for numerator must be at least 1");if(!z(n)&&n<1)throw new RangeError("degree of freedom v for denominator must be at least 1");let l=(i,s,c,f)=>lt(Ye(f,i,s,{lowerTail:!1}),i,s,{ncp:c*(i+s+1),lowerTail:!1});if(z(r))r=l(t,n,o,a);else{let i=r;z(t)?t=Z(s=>l(s,n,o,a)-i,1+1e-10,100):z(n)?n=Z(s=>l(t,s,o,a)-i,1+1e-10,1e9):z(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 Ul="Missing values detected. NAs dropped.",sa={tol:0,maxiter:5e3};function Hl(e){return Math.exp(fr(e/2)-Math.log(Math.sqrt(e/2))-fr((e-1)/2))}function na(e,t,n){return ut(r=>Mn(e,t,{ncp:r})-n,e-1,e+1,{...sa,extendInt:"downX"}).root}function aa(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(Ul);let u,h,d,p,g;if(f===void 0||l){let x;if(f===void 0)x=Re(c);else{let[k,S]=Tt(c,f);x=k.map((L,A)=>L-S[A])}let q=x.length;u=se(x),h=Math.sqrt(re(x)),d=h/Math.sqrt(q),p=1/q,g=q-1}else{let x=Re(c),q=Re(f),k=x.length,S=q.length;u=se(x)-se(q);let L=Math.sqrt(re(x)),A=Math.sqrt(re(q));o?(h=Math.sqrt(((k-1)*L**2+(S-1)*A**2)/(k+S-2)),d=h*Math.sqrt(1/k+1/S),p=1/k+1/S,g=k+S-2):(h=Math.sqrt((L**2+A**2)/2),p=2*(S*L**2+k*A**2)/(k*S*(L**2+A**2)),d=Math.sqrt(L**2/k+A**2/S),g=d**4/((L**2/k)**2/(k-1)+(A**2/S)**2/(S-1)))}let v=r?Hl(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)/d,E=na(M,g,(1+N)/2)*Math.sqrt(p)*v,y=na(M,g,(1-N)/2)*Math.sqrt(p)*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 Bl(e,t,n={}){return aa(e,t,n,n.adjust??!1)}function Xl(e,t,n={}){return aa(e,t,n,!0)}var Ql=e=>Array.isArray(e.effects),ia=e=>e.multistratum===!0,Kl=e=>Array.isArray(e.strata),la=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Zl=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),oo=e=>new RegExp(`\\b${Zl(e)}\\b`);function Yl(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 Jl(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],d=e.within.filter(v=>oo(v).test(m)),p=o(d.length===0?"Subject":`Subject:${d.join(":")}`,h[c],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[s],ss:h[n]};p.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 ca(e,t){let n=Array.isArray(e)?e:[],r=n.filter(a=>!t.some(l=>oo(a).test(l.name)));if(r.length>0)throw new Error(`Observed variable not in data: ${r.join(", ")}`);let o=a=>n.some(l=>oo(l).test(a));return{isObserved:o,ssObserved:t.reduce((a,l)=>o(l.name)?a+l.ss:a,0)}}function ra(e,t,n,r){let o=a=>lt(e,t,n,{ncp:a})-r;return o(0)>0?e===1/0?1/0:ut(o,0,Math.max(10,e*t*4),{...sa,extendInt:"downX"}).root:0}function fa(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=ra(i,t,n,(1+a)/2),c=ra(i,t,n,(1-a)/2);i<=Ye((1-a)/2,t,n)&&(c=0),i<=Ye((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 oa="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function so(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(oa);if(ia(e)||Kl(e)||la(e))return ec(e,t,n);let r=Ql(e)?Kr(e):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(oa);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,d=s.reduce((y,x)=>y+x.ss,0),p=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}=ca(g,f),M=y=>{let{ss:x,df:q}=y;if(t==="Eta2")return w==="partial"?x/(x+u):w==="plain"?x/d:x/(x+u+N-(b(y.name)?x:0));if(t==="Omega2"){let S=w==="partial"?(x-q*h)/(x+(p-q)*h):(x-q*h)/(d+h);return Math.max(0,S)}let k=w==="partial"?(x-q*h)/(x+u):(x-q*h)/d;return Math.max(0,k)},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)]:fa(M(y),y.df,m,o,a)),warnings:[]}}function ec(e,t,n){let r=n.ci===void 0?.95:n.ci,o=ie(n.alternative??"greater",["two.sided","less","greater"]),a=la(e),l,i,s;if(a){let N=Jl(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}=Yl(ia(e)?Zr(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,d=h!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:p,ssObserved:g}=ca(h,c),v=(N,M)=>{let{ss:E,df:y}=N,x=M.sse/M.dfe;if(t==="Eta2")return d==="partial"?E/(E+M.sse):d==="plain"?E/m:E/(E+f+g-(p(N.name)?E:0));if(t==="Omega2"){let k=M===i?0:1,S=d==="partial"?(E-y*x)/(E+k*M.sse+i.sse+u):(E-y*x)/(m+u);return Math.max(0,S)}let q=d==="partial"?(E-y*x)/(E+M.sse):(E-y*x)/m;return Math.max(0,q)},w=`${t}${d==="plain"?"":`_${d}`}`,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]:fa(E,N.df,M.dfe,r,o)}),warnings:[]};return a||(b.groups=s.map(N=>N.stratum.name)),b}function tc(e,t={}){return so(e,"Eta2",t)}function nc(e,t={}){return so(e,"Omega2",t)}function rc(e,t={}){return so(e,"Epsilon2",t)}var Zn=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],oc=Math.sqrt(2220446049250313e-31);function Yn(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 mn=e=>typeof e=="boolean"?e?"TRUE":"FALSE":ge(e);function sc(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(mn):[...new Set(t.map(mn))].sort(mt)}var ma=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function ua(e,t,n,r){let o=s=>r.some(c=>new RegExp(`\\b${ma(c)}\\b`).test(s));for(let s of r)if(!e.some(c=>new RegExp(`\\b${ma(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 ac(e,t){let n=Zn(t.between),r=Zn(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=Zn(t.es??"ges"),i=Zn(t.observed),s=[],c=ce(Yn(e,t.id)).map(mn),f=Yn(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(Yn(e,T))),u=r.map(T=>ce(Yn(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 d=m.map(T=>sc(T)),p=m.map(T=>T.map(mn)),g=u.map(T=>T.map(mn)),v=g.map(T=>[...new Set(T)]);d.forEach((T,O)=>{if(T.length<2)throw new Error(`Factor "${n[O]}" consists of one level only. Remove factor from model?`)});let w=[...new Set(c)],b=new Map,N=new Set;if(c.forEach((T,O)=>{let $=JSON.stringify(p.map(G=>G[O])),D=b.get(T);D===void 0?b.set(T,$):D!==$&&N.add(T)}),N.size>0)throw new Error(`Following ids are in more than one between subjects condition:
4
+ ${w.filter(T=>N.has(T)).join(", ")}`);let M=[[]];for(let T of v)M=M.flatMap(O=>T.map($=>[...O,$]));let E=T=>JSON.stringify(g.map(O=>O[T])),y=new Map(M.map((T,O)=>[JSON.stringify(T),O])),x=new Set(c.map((T,O)=>E(O)));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,k=new Map;c.forEach((T,O)=>{let $=k.get(T);$===void 0&&k.set(T,$={s:M.map(()=>[0,0]),n:M.map(()=>0)});let D=y.get(E(O));$.s[D]=X($.s[D],[f[O],0]),$.n[D]++}),[...k.values()].some(T=>T.n.some(O=>O>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=>k.get(T).n.some(O=>O===0));if(S.length>0){let T=[...S].sort(mt),O=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:
5
+ ${O}`)}let L=w.filter(T=>!S.includes(T)),A=new Map;c.forEach((T,O)=>A.has(T)||A.set(T,O));let R=L.map(T=>{let O=k.get(T);return O.s.map(($,D)=>Kt($,O.n[D]))}),C={};n.forEach((T,O)=>C[T]=L.map($=>p[O][A.get($)]));let F=Object.fromEntries(n.map(T=>[T,"contr.sum"])),I=Object.fromEntries(n.map((T,O)=>[T,d[O]])),V="Rank deficient model matrix; insufficient data to estimate full model.";if(q===1)return j();return Y();function j(){let T=n.includes("y")?"y_":"y",O={...C,[T]:R.map(ne=>ne[0][0])},$=$r(`${T} ~ ${n.length>0?n.join(" * "):"1"}`,O,{contrasts:F,levels:I});if($.rank<$.coefNames.length)throw new Error(V);let D=Yr($,{type:o});s.push(...D.warnings.filter(ne=>!s.includes(ne)));let G=$.dfResidual,Q=D.rowNames.flatMap((ne,ve)=>{if(ne==="(Intercept)"||ne==="Residuals")return[];let[ye,_e,Ve,Oe]=D.table[ve];return[{name:ne,ss:ye,numDf:_e,errSS:$.rss,denDf:G,f:Ve,p:Oe}]});return{anova:ua(Q,[$.rss],l,i),univariate:{colNames:D.colNames,rowNames:D.rowNames,table:D.table},within:r,warnings:s}}function Y(){let T=L.length,O=n.length>0?It(`~ ${n.join(" * ")}`,C,{contrasts:F,levels:I}):{colNames:["(Intercept)"],assign:[0],x:L.map(()=>[1]),termLabels:[],factors:[]},$=O.colNames.length;if(T===0||pe(O.x).rank<$)throw new Error(V);let D=T-$,G=[0,...O.termLabels.map((H,K)=>K+1)],Q=["(Intercept)",...O.termLabels],ne=H=>O.assign.flatMap((K,U)=>H.includes(K)?[U]:[]),ve=H=>O.factors.flatMap((K,U)=>K[H-1]>0?[U]:[]),ye=H=>O.x.map(K=>[K[H],0]),_e=new Array(T).fill([0,0]),Ve=(H,K,U)=>{let{qtv:de}=Ge([...H,...K].map(ye),_e,U);return de.map(ot=>ot.slice(H.length,H.length+K.length))},Oe=H=>{let{qtv:K}=Ge(O.colNames.map((U,de)=>ye(de)),_e,H);return K.map(U=>U.slice($))},Ue=Object.fromEntries(r.map((H,K)=>[H,M.map(U=>U[K])])),Te=It(`~ ${r.join(" * ")}`,Ue,{contrasts:Object.fromEntries(r.map(H=>[H,"contr.sum"])),levels:Object.fromEntries(r.map((H,K)=>[H,v[K]]))}),xt=[0,...Te.termLabels.map((H,K)=>K+1)],nt=["(Intercept)",...Te.termLabels],jt=H=>H.map(K=>H.map(U=>{let de=[0,0];return K.forEach((ot,Bt)=>de=X(de,oe(ot,U[Bt]))),de})),rt=[],Wt=[],Gt=[],wn=[],Ut=[],Ht=[],_=[],W=!1;for(let H of xt){let K=Te.assign.flatMap((Se,fe)=>Se===H?[fe]:[]),U=K.length,de=K.map(Se=>R.map(fe=>fe.reduce((Be,Xe,Ro)=>X(Be,Le(Xe,Te.x[Ro][Se])),[0,0]))),ot=K.map(Se=>K.map(fe=>[Te.x.reduce((Be,Xe)=>Be+Xe[Se]*Xe[fe],0),0])),Bt=Ks(ot),ir=jt(Oe(de)),vn=cn(ir,Bt),bn=bt(vn)[0],He=NaN,Nn=NaN,lr=NaN,ko=NaN,Ao=!1;if(U>=2&&pt(Bs(ir),{symmetric:!0,onlyValues:!0}).values.filter(fe=>fe>=oc).length===U){Ao=!0;let fe=bt(vn);He=be(oe(fe,fe),Le(bt(cn(vn,vn)),U))[0],Nn=((D+1)*U*He-2)/(U*(D-U*He)),lr=Jr(cn(Bt,ir)),ko=eo(lr,U,D,q)}for(let Se=0;Se<G.length;Se++){let fe=G[Se],Be;if(o===3)Be=Ve(ne(G.filter(Ze=>Ze!==fe)),ne([fe]),de);else if(fe===0)Be=Ve([],ne([0]),de);else{let Ze=ve(fe),yn=G.filter(ze=>ze!==0&&ze!==fe&&Ze.every($a=>O.factors[$a][ze-1]>0));Be=Ve(ne(G.filter(ze=>ze!==fe&&!yn.includes(ze))),ne([fe]),de)}let Xe=bt(cn(jt(Be),Bt))[0],Qe=(fe===0?1:ne([fe]).length)*U,Ke=D*U,Xt=Xe/Qe/(bn/Ke),To=ae(Xt,Qe,Ke,!1,!1),cr=H===0?Q[Se]:fe===0?nt[H]:`${Q[Se]}:${nt[H]}`;rt.push([Xe,Qe,bn,Ke,Xt,To]),Wt.push(cr),_.push(bn);let Qt={name:cr,ss:Xe,numDf:Qe,errSS:bn,denDf:Ke,f:Xt,p:To};if(Ao){let Ze=Math.min(1,Nn),yn=ae(Xt,He*Qe,He*Ke,!1,!1),ze=ae(Xt,Ze*Qe,Ze*Ke,!1,!1);Gt.push([He,yn,Nn,ze]),wn.push([lr,ko]),Ut.push(cr),a==="GG"?Qt={...Qt,numDf:He*Qe,denDf:He*Ke,p:yn}:a==="HF"&&(Nn>1&&(W=!0),Qt={...Qt,numDf:Ze*Qe,denDf:Ze*Ke,p:ze})}H===0&&fe===0||Ht.push(Qt)}}W&&s.push("HF eps > 1 treated as 1");let J={anova:ua(Ht,_,l,i),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:Wt,table:rt},within:r,warnings:s};return Gt.length>0&&(J.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:Ut,table:Gt},J.sphericity={colNames:["Test statistic","p-value"],rowNames:[...Ut],table:wn}),J}}var ga=Math.log(2*Math.PI),ha=2e-14,pa=500;function ic(e){return Math.exp(-.5*e*e-.5*ga)}function lc(e){return P(e,{logP:!0})}function wa(e,t=-1/0){return(t===-1/0?dr(e,0,2,0,ha,pa):dr(e,t,1,0,ha,pa)).result}function da(e,t,n){let r=t.map(Math.sqrt),o=t.map(l=>Math.sqrt(1-l));return wa(l=>{let i=0;for(let s=0;s<r.length;s++){let c=(r[s]*l+e)/o[s];if(!n){i+=lc(c);continue}let f=(r[s]*l-e)/o[s],m=P(f)+P(c,{lowerTail:!1});m<.5?i+=Math.log1p(-m):i+=Math.log(c+f<0?P(c)-P(f):P(f,{lowerTail:!1})-P(c,{lowerTail:!1}))}return ic(l)*-Math.expm1(i)})}function yt(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,da(e,t,r));let o=n/2,a=Math.log(2)+.5*Math.log(o)-.5*ga-Io(o);return Math.min(1,wa(i=>{let s=a-o*(Math.expm1(2*i)-2*i);return s<-745?0:Math.exp(s)*da(e*Math.exp(i),t,r)}))}var va="Ties are present. z-quantiles were corrected for ties.";function $e(e,t){let{labels:n,groups:r}=dt(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 ao(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 un(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 cc(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=Ft(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 er(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 io(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 Jn=e=>P(e,{lowerTail:!1}),lo=(e,t,n)=>xn(Math.abs(e),t,n,{lowerTail:!1});function co(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 fc(e,t,n={}){let r=n.pAdjustMethod??"holm",o=$e(e,t),{R:a,C:l}=ao(o),i=o.N*(o.N+1)/12-l,s=un(o.k,(f,m)=>Math.abs(a[f]-a[m])/Math.sqrt(i*(1/o.n[f]+1/o.n[m]))),c=cc(s.map(f=>f.map(m=>2*Jn(m))),r);return er(o,"Dunn's all-pairs test",s,c,r,l!==0?[va]:[])}function mc(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=$e(e,t),{R:l,C:i}=ao(a),s=a.N*(a.N+1)/12-i,c=a.n.slice(1).map((h,d)=>d+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=ba(f,m,r,o);return io(a,"Dunn's many-to-one test",f,u,o,i!==0?[va]:[])}function ba(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?yt(o,t,1/0,!0):yt(n==="greater"?o:-o,t,1/0,!1)):Ft(e.map(o=>n==="two.sided"?2*Jn(Math.abs(o)):Jn(n==="greater"?o:-o)),r)}function uc(e,t,n={}){let r=te(n.alternative),o=$e(e,t),{m:a,s2:l,df:i}=co(o),s=Math.sqrt(l),c=o.n.slice(1).map((h,d)=>d+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*yt(Math.abs(h),m,i,!1)):yt(r==="greater"?h:-h,m,i,!1));return io(o,`Dunnett's-test for multiple
6
+ comparisons with one control`,f,u,"single-step")}function hc(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=$e(e,t);if(o==="single-step"&&a.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
7
7
 
8
- k = 3 groups are required.`);let i=a.n[0],l=a.n.slice(1).map((m,u)=>u+1),s=l.map(m=>{let u=a.n[m],h=[...a.groups[0],...a.groups[m]],p=fe(h),d=0;for(let y=0;y<i;y++)d+=p[y];let g=i+u,b=g*(g+1)/2-d,w=i*u+i*(i+1)/2-d,v=i*u+u*(u+1)/2-b,N=0;for(let y of Ie(h))N+=y*y*y-y;let M=i*u/(g*(g-1))*((g*g*g-g)/12-N/12),E=(Math.min(w,v)-i*u/2)/Math.sqrt(M);return d<b?-E:E}),c=l.map(m=>a.n[m]/(a.n[m]+i)),f=ga(s,c,r,o);return oo(a,`Wilcoxon, Mann, Whittney U-test
9
- for multiple comparisons with one control`,s,f,o)}function cc(e,t){let n=Ce(e,t),r=cn(n.k,(a,i)=>{let l=n.n[a],s=n.n[i],c=[...n.groups[a],...n.groups[i]],f=fe(c),m=0;for(let b=0;b<l;b++)m+=f[b];let u=m-l*(l+1)/2,h=Math.min(u,l*s-u),p=l+s,d=0;for(let b of Ie(c))d+=b*b*b-b;let g=l*s/(p*(p-1))*((p*p*p-p)/12-d/12);return Math.SQRT2*(h-l*s/2)/Math.sqrt(g)}),o=r.map(a=>a.map(i=>Number.isNaN(i)?NaN:so(i,n.k,1/0)));return Zn(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step")}function fc(e,t){let n=Ce(e,t),r=n.groups.map(se),o=n.groups.map((l,s)=>re(l)/n.n[s]),a=cn(n.k,(l,s)=>(r[l]-r[s])/Math.sqrt(o[l]+o[s])*Math.SQRT2),i=cn(n.k,(l,s)=>{let c=(o[l]+o[s])**2/(o[l]**2/(n.n[l]-1)+o[s]**2/(n.n[s]-1));return so(a[l-1][s],n.k,c)});return Zn(n,"Games-Howell test",a,i,"none")}function mc(e,t){let n=Ce(e,t),{m:r,s2:o,df:a}=ao(n),i=cn(n.k,(s,c)=>(r[s]-r[c])/Math.sqrt(o*(1/n.n[s]+1/n.n[c])/2)),l=i.map(s=>s.map(c=>Number.isNaN(c)?NaN:so(c,n.k,a)));return Zn(n,"Tukey's test",i,l,"single-step")}function uc(e,t,n={}){if((n.alternative===void 0?"greater":te(n.alternative))==="two.sided")throw new RangeError("'arg' should be one of \u201Cgreater\u201D, \u201Cless\u201D");let o=Ce(e,t);throw o.n.some(a=>a!==o.n[0])?new RangeError("Steel's test is only valid for balanced designs."):new Error("'data' must be of a vector type, was 'NULL'")}function hc(e,t,n={}){let r=n.method??"none",o=n.altp??!1,a=Ce(e,t),i=Dn(e,t).statistic,{R:l,C:s}=ro(a),c=a.N*(a.N+1)/12-s,f=[],m=[];for(let h=1;h<a.k;h++)for(let p=0;p<h;p++)f.push((l[p]-l[h])/Math.sqrt(c*(1/a.n[p]+1/a.n[h]))),m.push(`${a.labels[p]} - ${a.labels[h]}`);let u=f.map(h=>(o?2:1)*Kn(Math.abs(h)));return{chi2:i,Z:f,P:u,PAdjusted:pc(u,r),comparisons:m}}function pc(e,t){let n=e.length,r=(a,i)=>Math.min(1,-Math.expm1(i*Math.log1p(-a))),o=a=>{let i=e.map((s,c)=>c).sort((s,c)=>(a?e[c]-e[s]:e[s]-e[c])||s-c),l=new Array(n);return i.forEach((s,c)=>l[s]=c+1),l};switch(t){case"none":return e.slice();case"bonferroni":return e.map(a=>Math.min(1,n*a));case"sidak":return e.map(a=>r(a,n));case"holm":{let a=o(!1);return e.map((i,l)=>Math.min(1,(n+1-a[l])*i))}case"hs":{let a=o(!1);return e.map((i,l)=>r(i,n+1-a[l]))}case"hochberg":{let a=o(!0);return e.map((i,l)=>Math.min(1,a[l]*i))}case"bh":{let a=o(!0);return e.map((i,l)=>Math.min(1,n/(n+1-a[l])*i))}case"by":{let a=o(!0),i=0;for(let l=1;l<=n;l++)i+=1/l;return e.map((l,s)=>Math.min(1,n/(n+1-a[s])*i*l))}default:throw new RangeError(`invalid method "${String(t)}"`)}}function dc(e,t,n={}){let r=te(n.alternative),o=Ce(e,t),{m:a,s2:i,df:l}=ao(o),s=Math.sqrt(i),c=o.n.slice(1).map((d,g)=>g+1),f=c.map(d=>a[d]-a[0]),m=c.map(d=>s*Math.sqrt(1/o.n[d]+1/o.n[0])),u=f.map((d,g)=>d/m[g]),h=c.map(d=>o.n[d]/(o.n[d]+o.n[0])),p=u.map(d=>r==="two.sided"?vt(d,h,l,!0):vt(r==="greater"?d:-d,h,l,!1));return{names:c.map(d=>`${o.labels[d]} - ${o.labels[0]}`),estimate:f,se:m,t:u,p,df:l}}var gc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function wa(e,t,n){if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n!==void 0&&n.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");let r=[];for(let l=0;l<e.length;l++){if(Number.isNaN(e[l])||Number.isNaN(t[l])||n!==void 0&&gc(n[l]))continue;let s=t[l];if(s!==0&&s!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");r.push(l)}if(r.length===0)throw new RangeError("no observations without missing values");let o=[],a=new Map;n!==void 0&&(o=Array.from(new Set(r.map(l=>n[l]))).sort((l,s)=>typeof l=="number"&&typeof s=="number"?l-s:String(l)<String(s)?-1:String(l)>String(s)?1:0),o.forEach((l,s)=>a.set(l,s)));let i=r.map(l=>({time:e[l],event:t[l]===1,group:n===void 0?0:a.get(n[l])}));return i.sort((l,s)=>l.time-s.time),{obs:i,labels:o.map(l=>`group=${String(l)}`)}}function wc(e,t,n,r={}){let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:a,labels:i}=wa(e,t,n),l=i.length,s=new Array(l).fill(0);for(let w of a)s[w.group]++;let c=new Array(l).fill(0),f=new Array(l).fill(0),m=Array.from({length:l},()=>new Array(l).fill(0)),u=s.slice(),h=a.length,p=1;for(let w=0;w<a.length;){let v=a[w].time,N=w,M=new Array(l).fill(0),E=new Array(l).fill(0),y=0;for(;N<a.length&&a[N].time===v;){let x=a[N];E[x.group]++,x.event&&(M[x.group]++,y++),N++}if(y>0){let x=o===0?1:p**o;for(let k=0;k<l;k++)c[k]+=x*M[k],f[k]+=x*y*u[k]/h;if(h>1){let k=x*x*y*(h-y)/(h*(h-1));for(let q=0;q<l;q++)for(let S=0;S<l;S++)m[q][S]+=k*u[q]*((q===S?1:0)-u[S]/h)}p*=1-y/h}for(let x=0;x<l;x++)u[x]-=E[x];h-=N-w,w=N}let d=f.map((w,v)=>w>0?v:-1).filter(w=>w>=0),g=0;if(d.length>=2){let w=d.slice(1),v=w.map(M=>c[M]-f[M]),N=be(w.map(M=>w.map(E=>m[M][E])),v);g=v.reduce((M,E,y)=>M+E*N[y],0)}let b=$e(g,d.length-1,{lowerTail:!1});return{n:s,groups:i,obs:c,exp:f,var:m,chisq:g,pvalue:b}}function io(e,t,n){let r=Math.sqrt(Number.EPSILON);for(let o=0;o<n.length;o++){let a=n[o];if(a<.5+r){if(a>.5-r){for(let i=o+1;i<n.length;i++)if(t[i]>0)return(e[o]+e[i])/2;return e[o]}return e[o]}}return NaN}function vc(e,t,n={}){let{group:r,confType:o="log",confInt:a=.95}=n;if(o!=="log"&&o!=="plain"&&o!=="log-log")throw new RangeError(`invalid 'confType': ${String(o)}`);if(!(a>0&&a<1))throw new RangeError("'confInt' must be between 0 and 1");let{obs:i,labels:l}=wa(e,t,r),s=r===void 0?["all"]:l,c=H(1-(1-a)/2),f={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:[],lower:[],upper:[],median:[],medianRows:s};return s.forEach((m,u)=>{let h=i.filter(N=>N.group===u),p=f.time.length,d=h.length,g=1,b=0;for(let N=0;N<h.length;){let M=h[N].time,E=N,y=0;for(;E<h.length&&h[E].time===M;)h[E].event&&y++,E++;let x=E-N-y;y>0&&(g*=1-y/d,b+=y<d?y/(d*(d-y)):1/0);let k=Math.sqrt(b),q,S;if(o==="log")q=g===0?NaN:g*Math.exp(-c*k),S=g===0?NaN:Math.min(1,g*Math.exp(c*k));else if(o==="plain"){let L=c*k*g;q=Number.isNaN(L)?NaN:Math.max(0,g-L),S=Number.isNaN(L)?NaN:Math.min(1,g+L)}else if(g===0||g===1)q=NaN,S=NaN;else{let L=Math.log(g),A=Math.log(-L);q=Math.exp(-Math.exp(A-c*k/L)),S=Math.exp(-Math.exp(A+c*k/L))}f.strata.push(m),f.time.push(M),f.nRisk.push(d),f.nEvent.push(y),f.nCensor.push(x),f.surv.push(g),f.stdErr.push(g*k),f.lower.push(q),f.upper.push(S),d-=E-N,N=E}let w=f.time.slice(p),v=f.nEvent.slice(p);f.median.push([io(w,v,f.surv.slice(p)),io(w,v,f.lower.slice(p)),io(w,v,f.upper.slice(p))])}),f}var bc=e=>e==null||typeof e=="number"&&Number.isNaN(e),Nc=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0;function yc(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(a=>Number.isFinite(a)))).sort((a,i)=>a-i);if(n.length<2)return e.slice();let r=0;for(let a of n)r+=Math.abs(a);r/=n.length;let o=[n[0]];for(let a=0;a+1<n.length;a++){let i=n[a+1]-n[a];i<=t||i/r<=t||o.push(n[a+1])}return e.map(a=>{if(!Number.isFinite(a))return a;let i=0,l=o.length-1;for(;i<l;){let s=i+l+1>>1;o[s]<=a?i=s:l=s-1}return o[i]})}function lo(e,t,n){let{time:r,event:o,weight:a,x:i,stratumEnd:l}=e,s=r.length,c=t.length,f=()=>new Array(c).fill(0),m=()=>Array.from({length:c},f),u=f(),h=m(),p=0,d=0,g=f(),b=m(),w=f(),v=m();for(let N=s-1;N>=0;){l[N]&&(d=0,g=f(),b=m());let M=r[N],E=0,y=0,x=0,k=N;do{let q=i[k],S=0;for(let R=0;R<c;R++)S+=t[R]*q[R];let L=a[k],A=Math.exp(S)*L;if(o[k]){E++,y+=L,x+=A,p+=L*S;for(let R=0;R<c;R++){u[R]+=L*q[R],w[R]+=A*q[R];let C=v[R];for(let F=0;F<=R;F++)C[F]+=A*q[R]*q[F]}}else{d+=A;for(let R=0;R<c;R++){g[R]+=A*q[R];let C=b[R];for(let F=0;F<=R;F++)C[F]+=A*q[R]*q[F]}}k--}while(k>=0&&r[k]===M&&!l[k]);if(E>0){let q=n?E:1,S=n?E:1,L=y/q;for(let A=0;A<q;A++){d+=n?x/S:x,p-=L*Math.log(d);for(let R=0;R<c;R++){g[R]+=n?w[R]/S:w[R];let C=g[R]/d;u[R]-=L*C;let F=b[R],I=v[R],V=h[R];for(let z=0;z<=R;z++)F[z]+=n?I[z]/S:I[z],V[z]+=L*(F[z]-C*g[z])/d}}w.fill(0);for(let A of v)A.fill(0)}N=k}for(let N=0;N<c;N++)for(let M=0;M<N;M++)h[M][N]=h[N][M];return{loglik:p,score:u,info:h}}function co(e,t){let n=e.length,r=0,o=!1;for(let s=0;s<n;s++){let c=e[s][s];c>0&&((!o||c>r)&&(r=c),o=!0)}let a=o?r*t:t,i=0,l=!0;for(let s=0;s<n;s++){let c=e[s][s];if(!Number.isFinite(c)||c<a){e[s][s]=0,c<-8*a&&(l=!1);continue}i++;for(let f=s+1;f<n;f++){let m=e[f][s]/c;e[f][s]=m,e[f][f]-=m*m*c;for(let u=f+1;u<n;u++)e[u][f]-=m*e[u][s]}}return l?i:-i}function fo(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let a=r[o];for(let i=0;i<o;i++)a-=r[i]*e[o][i];r[o]=a}for(let o=n-1;o>=0;o--){if(e[o][o]===0){r[o]=0;continue}let a=r[o]/e[o][o];for(let i=o+1;i<n;i++)a-=r[i]*e[i][o];r[o]=a}return r}function mo(e){let t=e.length;for(let n=0;n<t;n++)if(e[n][n]>0){e[n][n]=1/e[n][n];for(let r=n+1;r<t;r++){e[r][n]=-e[r][n];for(let o=0;o<n;o++)e[r][o]+=e[r][n]*e[n][o]}}for(let n=0;n<t;n++){if(e[n][n]===0){for(let r=0;r<t;r++)e[n][r]=0,e[r][n]=0;continue}for(let r=n+1;r<t;r++){let o=e[r][n]*e[r][r];e[n][r]=o;for(let a=n;a<r;a++)e[n][a]+=o*e[r][a]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}var uo=(e,t)=>({statistic:e,df:t,pValue:$e(e,t,{lowerTail:!1})});function Mc(e,t,n,r={}){let{ties:o="efron",weights:a,strata:i,init:l,control:s={},nocenter:c=[-1,0,1],confLevel:f=.95}=r,{eps:m=1e-9,iterMax:u=20,tolerChol:h=2**-39}=s,p=s.tolerInf??Math.sqrt(m);if(o!=="efron"&&o!=="breslow")throw new RangeError(`invalid 'ties': ${String(o)}`);if(!(m>0))throw new RangeError("'eps' must be positive");if(!(Number.isInteger(u)&&u>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(f>0&&f<1))throw new RangeError("'confLevel' must be between 0 and 1");let d=e.length;if(t.length!==d||n.length!==d)throw new RangeError("'time', 'status' and 'x' must have the same length");if(a!==void 0&&a.length!==d)throw new RangeError("'weights' must have the same length as 'time'");if(i!==void 0&&i.length!==d)throw new RangeError("'strata' must have the same length as 'time'");let g=d===0?0:n[0].length;if(g<1)throw new RangeError("'x' must have at least one column");if(n.some(_=>_.length!==g))throw new RangeError("the rows of 'x' must have the same length");if(l!==void 0&&l.length!==g)throw new RangeError("'init' must have one value per column of 'x'");let b=[];for(let _=0;_<d;_++){if(Number.isNaN(e[_])||Number.isNaN(t[_])||n[_].some(K=>Number.isNaN(K))||a!==void 0&&Number.isNaN(a[_])||i!==void 0&&bc(i[_]))continue;let W=t[_];if(W!==0&&W!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");b.push(_)}let w=b.length;if(w===0)throw new RangeError("no observations without missing values");let v=b.map(_=>a===void 0?1:a[_]);if(v.some(_=>!(_>0)))throw new RangeError("Invalid weights, must be >0");if(v.some(_=>!Number.isInteger(_)))throw new RangeError("non-integer weights (robust variance) are not supported yet");let N=0;for(let _ of b)N+=t[_];let M=yc(b.map(_=>e[_])),E=new Array(w).fill(0);if(i!==void 0){let _=Array.from(new Set(b.map(K=>i[K]))).sort(Nc),W=new Map;_.forEach((K,X)=>W.set(K,X)),E=b.map(K=>W.get(i[K]))}let y=Array.from({length:w},(_,W)=>W).sort((_,W)=>E[_]-E[W]||M[_]-M[W]||_-W),x=0;for(let _ of v)x+=_;let k=y.map(_=>v[_]),q=y.map(_=>n[b[_]].slice()),S=new Array(g).fill(0),L=new Array(g).fill(1),A=new Set(c);for(let _=0;_<g;_++){if(q.every(U=>A.has(U[_])))continue;let W=0;for(let U=0;U<w;U++)W+=k[U]*q[U][_];let K=W/x,X=0;for(let U=0;U<w;U++)q[U][_]-=K,X+=k[U]*Math.abs(q[U][_]);let Z=X===0?1:x/X;for(let U=0;U<w;U++)q[U][_]*=Z;S[_]=K,L[_]=Z}let R={time:y.map(_=>M[_]),event:y.map(_=>t[b[_]]===1),weight:k,x:q,stratumEnd:y.map((_,W)=>W===w-1||E[y[W+1]]!==E[_])},C=o==="efron",F=l===void 0?new Array(g).fill(0):l.map((_,W)=>_/L[W]),I=lo(R,F,C),V=[I.loglik,I.loglik],z=I.info.map(_=>_.slice()),J=co(z,h),T=fo(z,I.score),O=0;for(let _=0;_<g;_++)O+=I.score[_]*T[_];let $=0,D,G=!1;if(u===0||!Number.isFinite(I.loglik))D=mo(z);else{let _=I.loglik,W=F.map((Z,U)=>Z+T[U]),K=0,X=!1;for($=1;$<=u;$++){I=lo(R,W,C),z=I.info.map(U=>U.slice()),J=co(z,h);let Z=Number.isFinite(I.loglik)&&I.score.every(Number.isFinite)&&I.info.every(U=>U.every(Number.isFinite));if(Z&&Math.abs(1-_/I.loglik)<=m){X=!0,V[1]=I.loglik,F=W;break}!Z||I.loglik<_?(K++,W=W.map((U,pe)=>(U+K*F[pe])/(K+1))):(K=0,_=I.loglik,V[1]=_,T=fo(z,I.score),F=W,W=W.map((U,pe)=>U+T[pe]))}X?D=mo(z):($=u+1,G=!0,u>1?(I=lo(R,F,C),V[1]=I.loglik,D=I.info.map((Z,U)=>Z.map((pe,tt)=>U===tt?Z[U]>0?1/Z[U]:0:-0))):D=mo(z))}let Q=F.map((_,W)=>_*L[W]),ne=I.score.map((_,W)=>_/L[W]),de=D.map((_,W)=>_.map((K,X)=>K*L[W]*L[X])),ve=Q.slice();if(u>0&&Math.abs(J)<g)for(let _=0;_<g;_++)de[_][_]===0&&(ve[_]=NaN);let Oe=0;for(let _=0;_<g;_++)Oe+=Q[_]*S[_];let Fe=b.map(_=>{let W=0;for(let K=0;K<g;K++)W+=n[_][K]*Q[K];return W-Oe}),Le=new Ne;if(u>1){let _=de.map((W,K)=>{let X=0;for(let Z=0;Z<g;Z++)X+=ne[Z]*de[Z][K];return Math.abs(X)});if(G)Le.add("Ran out of iterations and did not converge"),(Math.max(...Fe)>500||_.some(W=>!Number.isFinite(W)))&&Le.add("one or more coefficients may be infinite");else{let W=[];for(let K=0;K<g;K++){let X=_[K];(!Number.isFinite(ne[K])||X>m&&X>p*Math.abs(Q[K]))&&W.push(K+1)}W.length>0&&Le.add(`Loglik converged before variable ${W.join(",")} ; coefficient may be infinite. `)}}let ze=ve.map((_,W)=>Number.isNaN(_)?-1:W).filter(_=>_>=0),Ae=ze.length,yt=ze.map(_=>ve[_]-(l===void 0?0:l[_])),Je=0;if(Ae===1)Je=yt[0]*yt[0]/de[ze[0]][ze[0]];else if(Ae>1){let _=ze.map(K=>ze.map(X=>de[K][X]));co(_,h);let W=fo(_,yt);for(let K=0;K<Ae;K++)Je+=yt[K]*W[K]}let $t=H((1+f)/2),et=de.map((_,W)=>Math.sqrt(_[W])),Pt=ve.map((_,W)=>_/et[W]),jt=Pt.map(_=>$e(_*_,1,{lowerTail:!1})),pn=ve.map((_,W)=>Math.exp(_-$t*et[W])),zt=ve.map((_,W)=>Math.exp(_+$t*et[W])),Wt=-2*(V[0]-V[1]);return{coefficients:ve,var:de,loglik:V,score:O,wald:Je,iter:$,means:S,n:w,nevent:N,linearPredictors:Fe,se:et,z:Pt,p:jt,lower:pn,upper:zt,logtest:uo(Wt,Ae),sctest:uo(O,Ae),waldtest:uo(Je,Ae),rsq:{rsq:1-Math.exp(-Wt/w),maxrsq:1-Math.exp(2*V[0]/w)},warnings:Le.list}}function xc(e,t,n={}){let r=ie(n.center??"median",["median","mean"]),{groups:o}=ut(e,t);if(o.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let a=o.map(l=>{let s=r==="median"?Cn(l):se(l);return l.map(c=>Math.abs(c-s))}),i=Cr(a);return{colNames:["Df","F value","Pr(>F)"],rowNames:["group",""],table:[[i.df[0],i.fValue,i.pValue],[i.df[1],NaN,NaN]]}}function Ec(e,t,n){let r=Array.from(e),o=Array.from(t),a=r.length,i=n===void 0?r.map((g,b)=>b+1):Array.from(n);if(o.length!==a||i.length!==a)throw new RangeError("'x', 'n' and 'score' must have the same length");let l=r.reduce((g,b)=>g+b,0)/o.reduce((g,b)=>g+b,0),s=o.map(g=>g/(l*(1-l))),c=r.map((g,b)=>g/o[b]),f=s.reduce((g,b)=>g+b,0),m=s.reduce((g,b,w)=>g+b*i[w],0)/f,u=s.reduce((g,b,w)=>g+b*c[w],0)/f,h=0,p=0;s.forEach((g,b)=>{let w=i[b]-m;h+=g*w*(c[b]-u),p+=g*w*w});let d=h*h/p;return{method:"Chi-squared Test for Trend in Proportions",statistic:d,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:me(d,.5,2,!1,!1)}}var qc=`Sample size > 100 or data with ties
10
- p-value based on normal approximation. Specify nperm for permutation p-value`,kc=`Sample data with ties
11
- p-value based on normal approximation. Specify nperm for permutation p-value.`;function Ac(e,t,n){let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=r.flatMap((s,c)=>Number.isNaN(s)||typeof t[c]=="number"&&Number.isNaN(t[c])?[]:[c]),a;if(n!==void 0)a=n.map(String);else{let s=[...new Set(o.map(c=>t[c]))];a=s.every(c=>typeof c=="number")?s.sort((c,f)=>c-f).map(String):[...new Set(s.map(String))].sort(lt)}let i=a.map(()=>[]),l=new Map(a.map((s,c)=>[s,c]));for(let s of o){let c=l.get(String(t[s]));if(c===void 0)throw new RangeError(`the group '${String(t[s])}' is not among 'levels'`);i[c].push(r[s])}return i}function Rc(e,t,n={}){let r=ie(n.alternative,["two.sided","increasing","decreasing"]),o=n.variant??"DescTools";if(o!=="DescTools"&&o!=="clinfun")throw new RangeError(`invalid variant "${String(o)}"`);if(o==="clinfun"&&n.exact!==void 0)throw new TypeError("clinfun's jonckheere.test has no option 'exact'");let a=Ac(e,t,n.levels),i=a.flat(),l=i.length,s=new Set(i).size<l,c=0,f=[],m=l;a.forEach((w,v)=>{m-=w.length,f.push([w.length,m]);for(let N=v+1;N<a.length;N++)for(let M of w)for(let E of a[N])c+=M<E?1:M===E?.5:0});let u=[],h=!s&&l<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&s&&(h=!1,u.push(kc))):h||u.push(qc);let p,d;if(h){let w=[1];for(let[v,N]of f){if(v===0||N===0)continue;let M=Array.from({length:v*N+1},(y,x)=>Co(x,v,N,!1)),E=new Array(w.length+M.length-1).fill(0);w.forEach((y,x)=>M.forEach((k,q)=>E[x+q]+=y*k)),w=E}p=0,d=0,w.forEach((v,N)=>{N<=c&&(p+=v),N>=c&&(d+=v)})}else{let w=f.reduce((M,[E,y])=>M+E*y,0)/2,v=f.reduce((M,[E,y])=>M+E*y*(E+y+1)/12,0),N=(c-w)/Math.sqrt(v);p=P(N),d=1-p}let b=r==="increasing"?d:r==="decreasing"?p:2*Math.min(d,p,o==="clinfun"?.5:1);return{method:"Jonckheere-Terpstra test",alternative:r,statistic:c,statisticName:"JT",pValue:b,warnings:u}}function fn(e){return ke(Array.from(e)).sort((t,n)=>t-n)}function Yn(e){let t=se(e),n=Math.sqrt(re(e));return e.map(r=>(r-t)/n)}function Tc(e){let t=fn(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Yn(t),o=0;for(let s=1;s<=n;s++)o+=(2*s-1)*(P(r[s-1],{logP:!0})+P(r[n-s],{lowerTail:!1,logP:!0}));let a=-n-o/n,i=(1+.75/n+2.25/n**2)*a,l;return i<.2?l=1-Math.exp(-13.436+101.14*i-223.73*i**2):i<.34?l=1-Math.exp(-8.318+42.796*i-59.938*i**2):i<.6?l=Math.exp(.9177-4.279*i-1.38*i**2):i<10?l=Math.exp(1.2937-5.709*i+.0186*i**2):l=37e-25,{method:"Anderson-Darling normality test",statistic:a,statisticName:"A",pValue:l}}function Sc(e){let t=fn(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Yn(t),o=1/(12*n);r.forEach((s,c)=>{o+=(P(s)-(2*(c+1)-1)/(2*n))**2});let a=(1+.5/n)*o,i=[],l;return a<.0275?l=1-Math.exp(-13.953+775.5*a-12542.61*a**2):a<.051?l=1-Math.exp(-5.903+179.546*a-1515.29*a**2):a<.092?l=Math.exp(.886-31.62*a+10.897*a**2):a<1.1?l=Math.exp(1.111-34.242*a+12.832*a**2):(l=737e-12,i.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:l,warnings:i}}function Lc(e){let t=fn(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=Yn(t).map(f=>P(f)),o=-1/0,a=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),a=Math.max(a,f-m/n)});let i=Math.max(o,a),l=n<=100?i:i*(n/100)**.49,s=n<=100?n:100,c=Math.exp(-7.01256*l**2*(s+2.78019)+2.99587*l*Math.sqrt(s+2.78019)-.122119+.974598/Math.sqrt(s)+1.67997/s);if(c>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*i;f<=.302?c=1:f<=.5?c=2.76773-19.828315*f+80.709644*f**2-138.55152*f**3+81.218052*f**4:f<=.9?c=-4.901232+40.662806*f-97.490286*f**2+94.029866*f**3-32.355711*f**4:f<=1.31?c=6.198765-19.558097*f+23.186922*f**2-12.234627*f**3+2.423045*f**4:c=0}return{method:"Lilliefors (Kolmogorov-Smirnov) normality test",statistic:i,statisticName:"D",pValue:c}}function Ic(e){let t=fn(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");let r=t.map((f,m)=>H((m+1-3/8)/(n+1/4))),o=jo(t,r)**2/(re(t)*re(r)),a=Math.log(n),i=Math.log(a),l=-1.2725+1.0521*(i-a),s=1.0308-.26758*(i+2/a),c=P((Math.log(1-o)-l)/s,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:o,statisticName:"W",pValue:c}}function Oc(e,t={}){let n=fn(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),a=t.adjust??!0,i=new Array(o).fill(0);for(let f of Yn(n)){let m=Math.floor(1+o*P(f));m>=1&&m<=o&&i[m-1]++}let l=r/o,s=i.reduce((f,m)=>f+(m-l)**2/l,0),c=o-1-(a?2:0);return{method:"Pearson chi-square normality test",statistic:s,statisticName:"P",pValue:me(s,c/2,2,!1,!1)}}function Jn(e,t){let n=e[0]?.length??0;if(e.some(r=>r.length!==n))throw new TypeError(`'${t}' must be a matrix (rows of equal length)`);return Array.from({length:n},(r,o)=>e.map(a=>a[o]))}function er(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`)}var ba=e=>2*P(-Math.abs(e));function Fc(e,t={}){let n=ie(t.weight,["unweighted","equal","squared"]);if(e.some(y=>y.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(y=>!y.some(Number.isNaN)),o=r.length,a=r.map(y=>Rt(y[0])),i=r.map(y=>Rt(y[1])),l=[...new Set([...a,...i])].sort((y,x)=>y<x?-1:y>x?1:0),s=l.length,c=new Map(l.map((y,x)=>[y,x])),f=Array.from({length:s},()=>new Array(s).fill(0));for(let y=0;y<o;y++)f[c.get(a[y])][c.get(i[y])]++;let m=y=>n==="unweighted"?y===0?1:0:n==="equal"?1-y/(s-1):1-y*y/((s-1)*(s-1)),u=Array.from({length:s},(y,x)=>Array.from({length:s},(k,q)=>m(Math.abs(x-q)))),h=f.map(y=>ee(y)/o),p=Array.from({length:s},(y,x)=>ee(f.map(k=>k[x]))/o),d=y=>{let x=[];for(let k=0;k<s;k++)for(let q=0;q<s;q++)x.push(y(k,q));return ee(x)},g=d((y,x)=>f[y][x]*u[y][x])/o,b=d((y,x)=>h[y]*p[x]*u[y][x]),w=(g-b)/(1-b),v=Array.from({length:s},(y,x)=>ee(p.map((k,q)=>k*u[x][q]))),N=Array.from({length:s},(y,x)=>ee(h.map((k,q)=>k*u[q][x]))),M=(d((y,x)=>h[y]*p[x]*(u[y][x]-(v[y]+N[x]))**2)-b*b)/(o*(1-b)**2),E=w/Math.sqrt(M);return{value:w,statistic:E,pValue:ba(E)}}function _c(e,t={}){let n=t.exact??!1,r=Jn(e,"ratings");er(e,"ratings");let o=e.length,a=r.length,i=[...new Set(e.flat())].sort((w,v)=>w-v),l=new Map(i.map((w,v)=>[w,v])),s=i.length,c=e.map(w=>{let v=new Array(s).fill(0);for(let N of w)v[l.get(N)]++;return v}),f=ee(c.map(w=>(ee(w.map(v=>v*v))-a)/(a*(a-1))))/o,m=Array.from({length:s},(w,v)=>ee(c.map(N=>N[v]))/(o*a)),u=ee(m.map(w=>w*w));if(n){let w=r.map(N=>{let M=new Array(s).fill(0);for(let E of N)M[l.get(E)]++;return M.map(E=>E/o)}),v=Array.from({length:s},(N,M)=>re(w.map(E=>E[M]))*(a-1)/a);u-=ee(v)/(a-1)}let h=(f-u)/(1-u);if(n)return{value:h};let p=ee(m.map(w=>w*(1-w))),d=ee(m.map(w=>w*(1-w)*(1-w-w))),g=2/(p*p*o*a*(a-1))*(p*p-d),b=h/Math.sqrt(g);return{value:h,statistic:b,pValue:ba(b)}}function Vc(e,t={}){let n=ie(t.model,["oneway","twoway"]),r=ie(t.type,["consistency","agreement"]),o=ie(t.unit,["single","average"]),a=t.r0??0,i=t.confLevel??.95;Me(i);let l=Jn(e,"ratings");er(e,"ratings");let s=e.length,c=l.length,f=1-i,m=re(e.map(V=>se(V)))*c,u=re(l.map(V=>se(V)))*s,h=se(e.map(V=>re(V))),d=(re(e.flat())*(s*c-1)-m*(s-1)-u*(c-1))/((s-1)*(c-1)),g=(V,z,J)=>ae(V,z,J,!1,!1),b=(V,z)=>ot(1-f/2,V,z,!0,!1),w=s-1;if(n==="oneway"||r==="consistency"){let V=n==="oneway"?h:d,z=n==="oneway"?s*(c-1):(s-1)*(c-1),J=m/V,T=J/b(w,z),O=J*b(z,w),[$,D,G]=o==="single"?[(m-V)/(m+(c-1)*V),J*(1-a)/(1+(c-1)*a),Q=>(Q-1)/(Q+c-1)]:[(m-V)/m,J*(1-a),Q=>1-1/Q];return{value:$,Fvalue:D,df1:w,df2:z,pValue:g(D,w,z),lbound:G(T),ubound:G(O)}}let v=o==="single"?c:1,N=V=>[v*V/(s*(1-V)),1+v*V*(s-1)/(s*(1-V))],M=(V,z)=>(V*u+z*d)**2/((V*u)**2/(c-1)+(z*d)**2/((s-1)*(c-1))),E=o==="single"?(m-d)/(m+(c-1)*d+c/s*(u-d)):(m-d)/(m+(u-d)/s),[y,x]=N(a),k=m/(y*u+x*d),q=M(y,x),[S,L]=[c*E/(s*(1-E)),1+c*E*(s-1)/(s*(1-E))],A=M(S,L),R=b(w,A),C=b(A,w),[F,I]=o==="single"?[s*(m-R*d)/(R*(c*u+(c*s-c-s)*d)+s*m),s*(C*m-d)/(c*u+(c*s-c-s)*d+s*C*m)]:[s*(m-R*d)/(R*(u-d)+s*m),s*(C*m-d)/(u-d+s*C*m)];return{value:E,Fvalue:k,df1:w,df2:q,pValue:g(k,w,q),lbound:F,ubound:I}}function va(e,t){let n=t.length,r=ee(t.flatMap(a=>t.map(i=>e[a][i])));return(1-ee(t.map(a=>e[a][a]))/r)*n/(n-1)}function Cc(e){let t=Jn(e,"x").length;if(er(e,"x"),t<2)throw new RangeError("'x' needs at least two items");let n=Tn(e),r=en(e),o=Array.from({length:t},(s,c)=>c),a=ee(r.flat()),i=be(r),l=o.map(s=>1-1/i[s][s]);return{rawAlpha:va(n,o),stdAlpha:(1-t/a)*t/(t-1),G6:1-(t-ee(l))/a,averageR:(a-t)/(t*(t-1)),alphaDrop:o.map(s=>va(n,o.filter(c=>c!==s)))}}function Dc(e,t,n){let r=Jn(e,"x").length;er(e,"x");for(let s of[t,n])if(!Number.isInteger(s)||s<0||s>=r)throw new RangeError(`column index ${s} out of range (0 to ${r-1})`);if(t===n)throw new RangeError("'i' and 'j' must be different columns");let o=en(e),a=[t,n],i=Array.from({length:r},(s,c)=>c).filter(s=>s!==t&&s!==n),l=a.map(s=>a.map(c=>o[s][c]));if(i.length>0){let s=i.map(m=>i.map(u=>o[m][u])),c=i.map(m=>a.map(u=>o[m][u])),f=be(s,c);for(let m=0;m<2;m++)for(let u=0;u<2;u++)l[m][u]-=ee(i.map((h,p)=>o[a[m]][h]*f[p][u]))}return l[0][1]/Math.sqrt(l[0][0]*l[1][1])}function ue(e){let t=0,n=0;for(let r of e){let[o,a]=nt(t,r);t=o,n+=a}return t+n}var Na=(e,t)=>e[0].map((n,r)=>ue(e.map(o=>t(o[r]))));function xa(e,t){let n=t.map(r=>1/Math.sqrt(r));return e.map((r,o)=>r.map((a,i)=>n[o]*a*n[i]))}function ya(e,t,n){let r=mt(xa(e,t),{symmetric:!0}),o=r.values.slice(0,n).map(a=>Math.sqrt(Math.max(a-1,0)));return r.vectors.map((a,i)=>a.slice(0,n).map((l,s)=>Math.sqrt(t[i])*(l*o[s])))}function Ma(e,t){let n=Na(e,l=>l*l),r=n.map((l,s)=>s).sort((l,s)=>n[s]-n[l]||l-s),o=e.map(l=>r.map(s=>l[s])),a=t?.map(l=>r.map(s=>l[s])),i=Na(o,l=>l).map(l=>l<0?-1:1);return o=o.map(l=>l.map((s,c)=>i[c]<0?-s:s)),a=a?.map(l=>l.map((s,c)=>i[c]<0?-s:s)),a===void 0?{L:o}:{L:o,rot:a}}var bt=(e,t)=>e.map(n=>t[0].map((r,o)=>{let a=0;for(let i=0;i<n.length;i++)a+=n[i]*t[i][o];return a})),ho=e=>e[0].map((t,n)=>e.map(r=>r[n]));function tr(e){let t=e[0].length,n=e.length,r=e.map(c=>Math.sqrt(ue(c.map(f=>f*f)))),o=e.map((c,f)=>c.map(m=>m/r[f])),a=ho(o),i=Array.from({length:t},(c,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),l=0;for(let c=0;c<1e3;c++){let f=bt(o,i),m=f[0].map((g,b)=>{let w=0;for(let v=0;v<n;v++)w+=f[v][b]*f[v][b];return w}),u=f.map(g=>g.map((b,w)=>b**3-b*m[w]/n)),h=bt(a,u),p=An(h);i=bt(p.u,ho(p.v));let d=l;if(l=ue(p.d),l<d*(1+1e-5))break}return{loadings:bt(o,i).map((c,f)=>c.map(m=>m*r[f])),rotmat:i}}function $c(e){let t=tr(e),n=t.loadings,r=n.map(l=>l.map(s=>s*Math.abs(s)**3)),o=Tt(he(n),r),a=be(bt(ho(o),o)).map((l,s)=>l[s]),i=o.map(l=>l.map((s,c)=>s*Math.sqrt(a[c])));return{loadings:bt(n,i),rotmat:bt(t.rotmat,i)}}function Pc(e,t,n={}){let r,o;if(n.covmat!==void 0)r=n.covmat.map(E=>E.slice()),o=n.nObs??NaN;else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Tn(e),o=e.length}let a=r.length;if(a<3)throw new RangeError("factor analysis requires at least three variables");let i=t;if(!Number.isInteger(i)||i<1)throw new RangeError("'factors' must be a positive integer");let l=((a-i)**2-a-i)/2;if(l<0)throw new RangeError(`${i} ${i===1?"factor is":"factors are"} too many for ${a} variables`);let s=r.map((E,y)=>Math.sqrt(E[y])),c=r.map((E,y)=>E.map((x,k)=>x/(s[y]*s[k]))),f=n.control?.lower??.005,m=n.control?.start?.slice()??be(c).map((E,y)=>(1-i/(2*a))/E[y]),p=Sn(m,E=>{let y=mt(xa(c,E),{symmetric:!0,onlyValues:!0}).values.slice(i);return-(ue(y.map(x=>Math.log(x)-x))-i+a)},{gr:E=>{let y=ya(c,E,i);return E.map((x,k)=>{let q=0;for(let S=0;S<i;S++)q+=y[k][S]*y[k][S];return(q+x-c[k][k])/(x*x)})},method:"L-BFGS-B",lower:f,upper:1,control:{fnscale:1,parscale:new Array(a).fill(.01)}});if(p.convergence!==0)throw new Error("unable to optimize from this starting value");let d=p.par,g=Ma(ya(c,d,i)).L,b,w=n.rotation??"varimax";if(w!=="none"&&i>=2){let E=w==="varimax"?tr(g):$c(g),y=Ma(E.loadings,E.rotmat);g=y.L,b=y.rot}else if(w!=="varimax"&&w!=="promax"&&w!=="none")throw new RangeError(`unknown rotation '${String(w)}'`);let v=NaN,N=NaN;!Number.isNaN(o)&&l>0&&(v=(o-1-(2*a+5)/6-2*i/3)*p.value,N=$e(v,l,{lowerTail:!1}));let M={uniquenesses:d,loadings:g,STATISTIC:v,dof:l,PVAL:N,converged:!0,objective:p.value,counts:p.counts};return b!==void 0&&(M.rotmat=b),M}var wo=2**-52,jc=`
8
+ k = 3 groups are required.`);let l=a.n[0],i=a.n.slice(1).map((m,u)=>u+1),s=i.map(m=>{let u=a.n[m],h=[...a.groups[0],...a.groups[m]],d=me(h),p=0;for(let y=0;y<l;y++)p+=d[y];let g=l+u,v=g*(g+1)/2-p,w=l*u+l*(l+1)/2-p,b=l*u+u*(u+1)/2-v,N=0;for(let y of Fe(h))N+=y*y*y-y;let M=l*u/(g*(g-1))*((g*g*g-g)/12-N/12),E=(Math.min(w,b)-l*u/2)/Math.sqrt(M);return p<v?-E:E}),c=i.map(m=>a.n[m]/(a.n[m]+l)),f=ba(s,c,r,o);return io(a,`Wilcoxon, Mann, Whittney U-test
9
+ for multiple comparisons with one control`,s,f,o)}function pc(e,t){let n=$e(e,t),r=un(n.k,(a,l)=>{let i=n.n[a],s=n.n[l],c=[...n.groups[a],...n.groups[l]],f=me(c),m=0;for(let v=0;v<i;v++)m+=f[v];let u=m-i*(i+1)/2,h=Math.min(u,i*s-u),d=i+s,p=0;for(let v of Fe(c))p+=v*v*v-v;let g=i*s/(d*(d-1))*((d*d*d-d)/12-p/12);return Math.SQRT2*(h-i*s/2)/Math.sqrt(g)}),o=r.map(a=>a.map(l=>Number.isNaN(l)?NaN:lo(l,n.k,1/0)));return er(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step")}function dc(e,t){let n=$e(e,t),r=n.groups.map(se),o=n.groups.map((i,s)=>re(i)/n.n[s]),a=un(n.k,(i,s)=>(r[i]-r[s])/Math.sqrt(o[i]+o[s])*Math.SQRT2),l=un(n.k,(i,s)=>{let c=(o[i]+o[s])**2/(o[i]**2/(n.n[i]-1)+o[s]**2/(n.n[s]-1));return lo(a[i-1][s],n.k,c)});return er(n,"Games-Howell test",a,l,"none")}function gc(e,t){let n=$e(e,t),{m:r,s2:o,df:a}=co(n),l=un(n.k,(s,c)=>(r[s]-r[c])/Math.sqrt(o*(1/n.n[s]+1/n.n[c])/2)),i=l.map(s=>s.map(c=>Number.isNaN(c)?NaN:lo(c,n.k,a)));return er(n,"Tukey's test",l,i,"single-step")}function wc(e,t,n={}){if((n.alternative===void 0?"greater":te(n.alternative))==="two.sided")throw new RangeError("'arg' should be one of \u201Cgreater\u201D, \u201Cless\u201D");let o=$e(e,t);throw o.n.some(a=>a!==o.n[0])?new RangeError("Steel's test is only valid for balanced designs."):new Error("'data' must be of a vector type, was 'NULL'")}function vc(e,t,n={}){let r=n.method??"none",o=n.altp??!1,a=$e(e,t),l=zn(e,t).statistic,{R:i,C:s}=ao(a),c=a.N*(a.N+1)/12-s,f=[],m=[];for(let h=1;h<a.k;h++)for(let d=0;d<h;d++)f.push((i[d]-i[h])/Math.sqrt(c*(1/a.n[d]+1/a.n[h]))),m.push(`${a.labels[d]} - ${a.labels[h]}`);let u=f.map(h=>(o?2:1)*Jn(Math.abs(h)));return{chi2:l,Z:f,P:u,PAdjusted:bc(u,r),comparisons:m}}function bc(e,t){let n=e.length,r=(a,l)=>Math.min(1,-Math.expm1(l*Math.log1p(-a))),o=a=>{let l=e.map((s,c)=>c).sort((s,c)=>(a?e[c]-e[s]:e[s]-e[c])||s-c),i=new Array(n);return l.forEach((s,c)=>i[s]=c+1),i};switch(t){case"none":return e.slice();case"bonferroni":return e.map(a=>Math.min(1,n*a));case"sidak":return e.map(a=>r(a,n));case"holm":{let a=o(!1);return e.map((l,i)=>Math.min(1,(n+1-a[i])*l))}case"hs":{let a=o(!1);return e.map((l,i)=>r(l,n+1-a[i]))}case"hochberg":{let a=o(!0);return e.map((l,i)=>Math.min(1,a[i]*l))}case"bh":{let a=o(!0);return e.map((l,i)=>Math.min(1,n/(n+1-a[i])*l))}case"by":{let a=o(!0),l=0;for(let i=1;i<=n;i++)l+=1/i;return e.map((i,s)=>Math.min(1,n/(n+1-a[s])*l*i))}default:throw new RangeError(`invalid method "${String(t)}"`)}}function Nc(e,t,n={}){let r=te(n.alternative),o=$e(e,t),{m:a,s2:l,df:i}=co(o),s=Math.sqrt(l),c=o.n.slice(1).map((p,g)=>g+1),f=c.map(p=>a[p]-a[0]),m=c.map(p=>s*Math.sqrt(1/o.n[p]+1/o.n[0])),u=f.map((p,g)=>p/m[g]),h=c.map(p=>o.n[p]/(o.n[p]+o.n[0])),d=u.map(p=>r==="two.sided"?yt(p,h,i,!0):yt(r==="greater"?p:-p,h,i,!1));return{names:c.map(p=>`${o.labels[p]} - ${o.labels[0]}`),estimate:f,se:m,t:u,p:d,df:i}}var yc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function Na(e,t,n){if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n!==void 0&&n.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");let r=[];for(let i=0;i<e.length;i++){if(Number.isNaN(e[i])||Number.isNaN(t[i])||n!==void 0&&yc(n[i]))continue;let s=t[i];if(s!==0&&s!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");r.push(i)}if(r.length===0)throw new RangeError("no observations without missing values");let o=[],a=new Map;n!==void 0&&(n=ce(n)),n!==void 0&&(o=Array.from(new Set(r.map(i=>n[i]))).sort((i,s)=>typeof i=="number"&&typeof s=="number"?i-s:String(i)<String(s)?-1:String(i)>String(s)?1:0),o.forEach((i,s)=>a.set(i,s)));let l=r.map(i=>({time:e[i],event:t[i]===1,group:n===void 0?0:a.get(n[i])}));return l.sort((i,s)=>i.time-s.time),{obs:l,labels:o.map(i=>`group=${ge(i)}`)}}function Mc(e,t,n,r={}){let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:a,labels:l}=Na(e,t,n),i=l.length,s=new Array(i).fill(0);for(let w of a)s[w.group]++;let c=new Array(i).fill(0),f=new Array(i).fill(0),m=Array.from({length:i},()=>new Array(i).fill(0)),u=s.slice(),h=a.length,d=1;for(let w=0;w<a.length;){let b=a[w].time,N=w,M=new Array(i).fill(0),E=new Array(i).fill(0),y=0;for(;N<a.length&&a[N].time===b;){let x=a[N];E[x.group]++,x.event&&(M[x.group]++,y++),N++}if(y>0){let x=o===0?1:d**o;for(let q=0;q<i;q++)c[q]+=x*M[q],f[q]+=x*y*u[q]/h;if(h>1){let q=x*x*y*(h-y)/(h*(h-1));for(let k=0;k<i;k++)for(let S=0;S<i;S++)m[k][S]+=q*u[k]*((k===S?1:0)-u[S]/h)}d*=1-y/h}for(let x=0;x<i;x++)u[x]-=E[x];h-=N-w,w=N}let p=f.map((w,b)=>w>0?b:-1).filter(w=>w>=0),g=0;if(p.length>=2){let w=p.slice(1),b=w.map(M=>c[M]-f[M]),N=we(w.map(M=>w.map(E=>m[M][E])),b);g=b.reduce((M,E,y)=>M+E*N[y],0)}let v=je(g,p.length-1,{lowerTail:!1});return{n:s,groups:l,obs:c,exp:f,var:m,chisq:g,pvalue:v}}function fo(e,t,n){let r=Math.sqrt(Number.EPSILON);for(let o=0;o<n.length;o++){let a=n[o];if(a<.5+r){if(a>.5-r){for(let l=o+1;l<n.length;l++)if(t[l]>0)return(e[o]+e[l])/2;return e[o]}return e[o]}}return NaN}function xc(e,t,n={}){let{group:r,confType:o="log",confInt:a=.95}=n;if(o!=="log"&&o!=="plain"&&o!=="log-log")throw new RangeError(`invalid 'confType': ${String(o)}`);if(!(a>0&&a<1))throw new RangeError("'confInt' must be between 0 and 1");let{obs:l,labels:i}=Na(e,t,r),s=r===void 0?["all"]:i,c=B(1-(1-a)/2),f={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:[],lower:[],upper:[],median:[],medianRows:s};return s.forEach((m,u)=>{let h=l.filter(N=>N.group===u),d=f.time.length,p=h.length,g=1,v=0;for(let N=0;N<h.length;){let M=h[N].time,E=N,y=0;for(;E<h.length&&h[E].time===M;)h[E].event&&y++,E++;let x=E-N-y;y>0&&(g*=1-y/p,v+=y<p?y/(p*(p-y)):1/0);let q=Math.sqrt(v),k,S;if(o==="log")k=g===0?NaN:g*Math.exp(-c*q),S=g===0?NaN:Math.min(1,g*Math.exp(c*q));else if(o==="plain"){let L=c*q*g;k=Number.isNaN(L)?NaN:Math.max(0,g-L),S=Number.isNaN(L)?NaN:Math.min(1,g+L)}else if(g===0||g===1)k=NaN,S=NaN;else{let L=Math.log(g),A=Math.log(-L);k=Math.exp(-Math.exp(A-c*q/L)),S=Math.exp(-Math.exp(A+c*q/L))}f.strata.push(m),f.time.push(M),f.nRisk.push(p),f.nEvent.push(y),f.nCensor.push(x),f.surv.push(g),f.stdErr.push(g*q),f.lower.push(k),f.upper.push(S),p-=E-N,N=E}let w=f.time.slice(d),b=f.nEvent.slice(d);f.median.push([fo(w,b,f.surv.slice(d)),fo(w,b,f.lower.slice(d)),fo(w,b,f.upper.slice(d))])}),f}var Ec=e=>e==null||typeof e=="number"&&Number.isNaN(e),qc=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0;function kc(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(a=>Number.isFinite(a)))).sort((a,l)=>a-l);if(n.length<2)return e.slice();let r=0;for(let a of n)r+=Math.abs(a);r/=n.length;let o=[n[0]];for(let a=0;a+1<n.length;a++){let l=n[a+1]-n[a];l<=t||l/r<=t||o.push(n[a+1])}return e.map(a=>{if(!Number.isFinite(a))return a;let l=0,i=o.length-1;for(;l<i;){let s=l+i+1>>1;o[s]<=a?l=s:i=s-1}return o[l]})}function mo(e,t,n){let{time:r,event:o,weight:a,x:l,stratumEnd:i}=e,s=r.length,c=t.length,f=()=>new Array(c).fill(0),m=()=>Array.from({length:c},f),u=f(),h=m(),d=0,p=0,g=f(),v=m(),w=f(),b=m();for(let N=s-1;N>=0;){i[N]&&(p=0,g=f(),v=m());let M=r[N],E=0,y=0,x=0,q=N;do{let k=l[q],S=0;for(let R=0;R<c;R++)S+=t[R]*k[R];let L=a[q],A=Math.exp(S)*L;if(o[q]){E++,y+=L,x+=A,d+=L*S;for(let R=0;R<c;R++){u[R]+=L*k[R],w[R]+=A*k[R];let C=b[R];for(let F=0;F<=R;F++)C[F]+=A*k[R]*k[F]}}else{p+=A;for(let R=0;R<c;R++){g[R]+=A*k[R];let C=v[R];for(let F=0;F<=R;F++)C[F]+=A*k[R]*k[F]}}q--}while(q>=0&&r[q]===M&&!i[q]);if(E>0){let k=n?E:1,S=n?E:1,L=y/k;for(let A=0;A<k;A++){p+=n?x/S:x,d-=L*Math.log(p);for(let R=0;R<c;R++){g[R]+=n?w[R]/S:w[R];let C=g[R]/p;u[R]-=L*C;let F=v[R],I=b[R],V=h[R];for(let j=0;j<=R;j++)F[j]+=n?I[j]/S:I[j],V[j]+=L*(F[j]-C*g[j])/p}}w.fill(0);for(let A of b)A.fill(0)}N=q}for(let N=0;N<c;N++)for(let M=0;M<N;M++)h[M][N]=h[N][M];return{loglik:d,score:u,info:h}}function uo(e,t){let n=e.length,r=0,o=!1;for(let s=0;s<n;s++){let c=e[s][s];c>0&&((!o||c>r)&&(r=c),o=!0)}let a=o?r*t:t,l=0,i=!0;for(let s=0;s<n;s++){let c=e[s][s];if(!Number.isFinite(c)||c<a){e[s][s]=0,c<-8*a&&(i=!1);continue}l++;for(let f=s+1;f<n;f++){let m=e[f][s]/c;e[f][s]=m,e[f][f]-=m*m*c;for(let u=f+1;u<n;u++)e[u][f]-=m*e[u][s]}}return i?l:-l}function ho(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let a=r[o];for(let l=0;l<o;l++)a-=r[l]*e[o][l];r[o]=a}for(let o=n-1;o>=0;o--){if(e[o][o]===0){r[o]=0;continue}let a=r[o]/e[o][o];for(let l=o+1;l<n;l++)a-=r[l]*e[l][o];r[o]=a}return r}function po(e){let t=e.length;for(let n=0;n<t;n++)if(e[n][n]>0){e[n][n]=1/e[n][n];for(let r=n+1;r<t;r++){e[r][n]=-e[r][n];for(let o=0;o<n;o++)e[r][o]+=e[r][n]*e[n][o]}}for(let n=0;n<t;n++){if(e[n][n]===0){for(let r=0;r<t;r++)e[n][r]=0,e[r][n]=0;continue}for(let r=n+1;r<t;r++){let o=e[r][n]*e[r][r];e[n][r]=o;for(let a=n;a<r;a++)e[n][a]+=o*e[r][a]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}var go=(e,t)=>({statistic:e,df:t,pValue:je(e,t,{lowerTail:!1})});function Ac(e,t,n,r={}){let{ties:o="efron",weights:a,strata:l,init:i,control:s={},nocenter:c=[-1,0,1],confLevel:f=.95}=r,{eps:m=1e-9,iterMax:u=20,tolerChol:h=2**-39}=s,d=s.tolerInf??Math.sqrt(m);if(o!=="efron"&&o!=="breslow")throw new RangeError(`invalid 'ties': ${String(o)}`);if(!(m>0))throw new RangeError("'eps' must be positive");if(!(Number.isInteger(u)&&u>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(f>0&&f<1))throw new RangeError("'confLevel' must be between 0 and 1");let p=e.length;if(t.length!==p||n.length!==p)throw new RangeError("'time', 'status' and 'x' must have the same length");if(a!==void 0&&a.length!==p)throw new RangeError("'weights' must have the same length as 'time'");if(l!==void 0&&l.length!==p)throw new RangeError("'strata' must have the same length as 'time'");let g=p===0?0:n[0].length;if(g<1)throw new RangeError("'x' must have at least one column");if(n.some(_=>_.length!==g))throw new RangeError("the rows of 'x' must have the same length");if(i!==void 0&&i.length!==g)throw new RangeError("'init' must have one value per column of 'x'");let v=[];for(let _=0;_<p;_++){if(Number.isNaN(e[_])||Number.isNaN(t[_])||n[_].some(J=>Number.isNaN(J))||a!==void 0&&Number.isNaN(a[_])||l!==void 0&&Ec(l[_]))continue;let W=t[_];if(W!==0&&W!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");v.push(_)}let w=v.length;if(w===0)throw new RangeError("no observations without missing values");let b=v.map(_=>a===void 0?1:a[_]);if(b.some(_=>!(_>0)))throw new RangeError("Invalid weights, must be >0");if(b.some(_=>!Number.isInteger(_)))throw new RangeError("non-integer weights (robust variance) are not supported yet");let N=0;for(let _ of v)N+=t[_];let M=kc(v.map(_=>e[_])),E=new Array(w).fill(0);if(l!==void 0){let _=ce(l),W=Array.from(new Set(v.map(H=>_[H]))).sort(qc),J=new Map;W.forEach((H,K)=>J.set(H,K)),E=v.map(H=>J.get(_[H]))}let y=Array.from({length:w},(_,W)=>W).sort((_,W)=>E[_]-E[W]||M[_]-M[W]||_-W),x=0;for(let _ of b)x+=_;let q=y.map(_=>b[_]),k=y.map(_=>n[v[_]].slice()),S=new Array(g).fill(0),L=new Array(g).fill(1),A=new Set(c);for(let _=0;_<g;_++){if(k.every(U=>A.has(U[_])))continue;let W=0;for(let U=0;U<w;U++)W+=q[U]*k[U][_];let J=W/x,H=0;for(let U=0;U<w;U++)k[U][_]-=J,H+=q[U]*Math.abs(k[U][_]);let K=H===0?1:x/H;for(let U=0;U<w;U++)k[U][_]*=K;S[_]=J,L[_]=K}let R={time:y.map(_=>M[_]),event:y.map(_=>t[v[_]]===1),weight:q,x:k,stratumEnd:y.map((_,W)=>W===w-1||E[y[W+1]]!==E[_])},C=o==="efron",F=i===void 0?new Array(g).fill(0):i.map((_,W)=>_/L[W]),I=mo(R,F,C),V=[I.loglik,I.loglik],j=I.info.map(_=>_.slice()),Y=uo(j,h),T=ho(j,I.score),O=0;for(let _=0;_<g;_++)O+=I.score[_]*T[_];let $=0,D,G=!1;if(u===0||!Number.isFinite(I.loglik))D=po(j);else{let _=I.loglik,W=F.map((K,U)=>K+T[U]),J=0,H=!1;for($=1;$<=u;$++){I=mo(R,W,C),j=I.info.map(U=>U.slice()),Y=uo(j,h);let K=Number.isFinite(I.loglik)&&I.score.every(Number.isFinite)&&I.info.every(U=>U.every(Number.isFinite));if(K&&Math.abs(1-_/I.loglik)<=m){H=!0,V[1]=I.loglik,F=W;break}!K||I.loglik<_?(J++,W=W.map((U,de)=>(U+J*F[de])/(J+1))):(J=0,_=I.loglik,V[1]=_,T=ho(j,I.score),F=W,W=W.map((U,de)=>U+T[de]))}H?D=po(j):($=u+1,G=!0,u>1?(I=mo(R,F,C),V[1]=I.loglik,D=I.info.map((K,U)=>K.map((de,ot)=>U===ot?K[U]>0?1/K[U]:0:-0))):D=po(j))}let Q=F.map((_,W)=>_*L[W]),ne=I.score.map((_,W)=>_/L[W]),ve=D.map((_,W)=>_.map((J,H)=>J*L[W]*L[H])),ye=Q.slice();if(u>0&&Math.abs(Y)<g)for(let _=0;_<g;_++)ve[_][_]===0&&(ye[_]=NaN);let _e=0;for(let _=0;_<g;_++)_e+=Q[_]*S[_];let Ve=v.map(_=>{let W=0;for(let J=0;J<g;J++)W+=n[_][J]*Q[J];return W-_e}),Oe=new Me;if(u>1){let _=ve.map((W,J)=>{let H=0;for(let K=0;K<g;K++)H+=ne[K]*ve[K][J];return Math.abs(H)});if(G)Oe.add("Ran out of iterations and did not converge"),(Math.max(...Ve)>500||_.some(W=>!Number.isFinite(W)))&&Oe.add("one or more coefficients may be infinite");else{let W=[];for(let J=0;J<g;J++){let H=_[J];(!Number.isFinite(ne[J])||H>m&&H>d*Math.abs(Q[J]))&&W.push(J+1)}W.length>0&&Oe.add(`Loglik converged before variable ${W.join(",")} ; coefficient may be infinite. `)}}let Ue=ye.map((_,W)=>Number.isNaN(_)?-1:W).filter(_=>_>=0),Te=Ue.length,xt=Ue.map(_=>ye[_]-(i===void 0?0:i[_])),nt=0;if(Te===1)nt=xt[0]*xt[0]/ve[Ue[0]][Ue[0]];else if(Te>1){let _=Ue.map(J=>Ue.map(H=>ve[J][H]));uo(_,h);let W=ho(_,xt);for(let J=0;J<Te;J++)nt+=xt[J]*W[J]}let jt=B((1+f)/2),rt=ve.map((_,W)=>Math.sqrt(_[W])),Wt=ye.map((_,W)=>_/rt[W]),Gt=Wt.map(_=>je(_*_,1,{lowerTail:!1})),wn=ye.map((_,W)=>Math.exp(_-jt*rt[W])),Ut=ye.map((_,W)=>Math.exp(_+jt*rt[W])),Ht=-2*(V[0]-V[1]);return{coefficients:ye,var:ve,loglik:V,score:O,wald:nt,iter:$,means:S,n:w,nevent:N,linearPredictors:Ve,se:rt,z:Wt,p:Gt,lower:wn,upper:Ut,logtest:go(Ht,Te),sctest:go(O,Te),waldtest:go(nt,Te),rsq:{rsq:1-Math.exp(-Ht/w),maxrsq:1-Math.exp(2*V[0]/w)},warnings:Oe.list}}function Rc(e,t,n={}){let r=ie(n.center??"median",["median","mean"]),{groups:o}=dt(e,t);if(o.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let a=o.map(i=>{let s=r==="median"?Pn(i):se(i);return i.map(c=>Math.abs(c-s))}),l=Pr(a);return{colNames:["Df","F value","Pr(>F)"],rowNames:["group",""],table:[[l.df[0],l.fValue,l.pValue],[l.df[1],NaN,NaN]]}}function Tc(e,t,n){let r=Array.from(e),o=Array.from(t),a=r.length,l=n===void 0?r.map((g,v)=>v+1):Array.from(n);if(o.length!==a||l.length!==a)throw new RangeError("'x', 'n' and 'score' must have the same length");let i=r.reduce((g,v)=>g+v,0)/o.reduce((g,v)=>g+v,0),s=o.map(g=>g/(i*(1-i))),c=r.map((g,v)=>g/o[v]),f=s.reduce((g,v)=>g+v,0),m=s.reduce((g,v,w)=>g+v*l[w],0)/f,u=s.reduce((g,v,w)=>g+v*c[w],0)/f,h=0,d=0;s.forEach((g,v)=>{let w=l[v]-m;h+=g*w*(c[v]-u),d+=g*w*w});let p=h*h/d;return{method:"Chi-squared Test for Trend in Proportions",statistic:p,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:he(p,.5,2,!1,!1)}}var Sc=`Sample size > 100 or data with ties
10
+ p-value based on normal approximation. Specify nperm for permutation p-value`,Lc=`Sample data with ties
11
+ p-value based on normal approximation. Specify nperm for permutation p-value.`;function Ic(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=r.flatMap((s,c)=>Number.isNaN(s)||typeof t[c]=="number"&&Number.isNaN(t[c])?[]:[c]);n===void 0&&(t=ce(t));let a;if(n!==void 0)a=n.map(ge);else{let s=[...new Set(o.map(c=>t[c]))];a=s.every(c=>typeof c=="number")?s.sort((c,f)=>c-f).map(ge):[...new Set(s.map(ge))].sort(mt)}let l=a.map(()=>[]),i=new Map(a.map((s,c)=>[s,c]));for(let s of o){let c=i.get(ge(t[s]));if(c===void 0)throw new RangeError(`the group '${ge(t[s])}' is not among 'levels'`);l[c].push(r[s])}return l}function Oc(e,t,n={}){let r=ie(n.alternative,["two.sided","increasing","decreasing"]),o=n.variant??"DescTools";if(o!=="DescTools"&&o!=="clinfun")throw new RangeError(`invalid variant "${String(o)}"`);if(o==="clinfun"&&n.exact!==void 0)throw new TypeError("clinfun's jonckheere.test has no option 'exact'");let a=Ic(e,t,n.levels),l=a.flat(),i=l.length,s=new Set(l).size<i,c=0,f=[],m=i;a.forEach((w,b)=>{m-=w.length,f.push([w.length,m]);for(let N=b+1;N<a.length;N++)for(let M of w)for(let E of a[N])c+=M<E?1:M===E?.5:0});let u=[],h=!s&&i<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&s&&(h=!1,u.push(Lc))):h||u.push(Sc);let d,p;if(h){let w=[1];for(let[b,N]of f){if(b===0||N===0)continue;let M=Array.from({length:b*N+1},(y,x)=>Po(x,b,N,!1)),E=new Array(w.length+M.length-1).fill(0);w.forEach((y,x)=>M.forEach((q,k)=>E[x+k]+=y*q)),w=E}d=0,p=0,w.forEach((b,N)=>{N<=c&&(d+=b),N>=c&&(p+=b)})}else{let w=f.reduce((M,[E,y])=>M+E*y,0)/2,b=f.reduce((M,[E,y])=>M+E*y*(E+y+1)/12,0),N=(c-w)/Math.sqrt(b);d=P(N),p=1-d}let v=r==="increasing"?p:r==="decreasing"?d:2*Math.min(p,d,o==="clinfun"?.5:1);return{method:"Jonckheere-Terpstra test",alternative:r,statistic:c,statisticName:"JT",pValue:v,warnings:u}}function hn(e){return Re(Array.from(e)).sort((t,n)=>t-n)}function tr(e){let t=se(e),n=Math.sqrt(re(e));return e.map(r=>(r-t)/n)}function Fc(e){let t=hn(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=tr(t),o=0;for(let s=1;s<=n;s++)o+=(2*s-1)*(P(r[s-1],{logP:!0})+P(r[n-s],{lowerTail:!1,logP:!0}));let a=-n-o/n,l=(1+.75/n+2.25/n**2)*a,i;return l<.2?i=1-Math.exp(-13.436+101.14*l-223.73*l**2):l<.34?i=1-Math.exp(-8.318+42.796*l-59.938*l**2):l<.6?i=Math.exp(.9177-4.279*l-1.38*l**2):l<10?i=Math.exp(1.2937-5.709*l+.0186*l**2):i=37e-25,{method:"Anderson-Darling normality test",statistic:a,statisticName:"A",pValue:i}}function _c(e){let t=hn(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=tr(t),o=1/(12*n);r.forEach((s,c)=>{o+=(P(s)-(2*(c+1)-1)/(2*n))**2});let a=(1+.5/n)*o,l=[],i;return a<.0275?i=1-Math.exp(-13.953+775.5*a-12542.61*a**2):a<.051?i=1-Math.exp(-5.903+179.546*a-1515.29*a**2):a<.092?i=Math.exp(.886-31.62*a+10.897*a**2):a<1.1?i=Math.exp(1.111-34.242*a+12.832*a**2):(i=737e-12,l.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:i,warnings:l}}function Vc(e){let t=hn(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=tr(t).map(f=>P(f)),o=-1/0,a=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),a=Math.max(a,f-m/n)});let l=Math.max(o,a),i=n<=100?l:l*(n/100)**.49,s=n<=100?n:100,c=Math.exp(-7.01256*i**2*(s+2.78019)+2.99587*i*Math.sqrt(s+2.78019)-.122119+.974598/Math.sqrt(s)+1.67997/s);if(c>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*l;f<=.302?c=1:f<=.5?c=2.76773-19.828315*f+80.709644*f**2-138.55152*f**3+81.218052*f**4:f<=.9?c=-4.901232+40.662806*f-97.490286*f**2+94.029866*f**3-32.355711*f**4:f<=1.31?c=6.198765-19.558097*f+23.186922*f**2-12.234627*f**3+2.423045*f**4:c=0}return{method:"Lilliefors (Kolmogorov-Smirnov) normality test",statistic:l,statisticName:"D",pValue:c}}function Cc(e){let t=hn(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");let r=t.map((f,m)=>B((m+1-3/8)/(n+1/4))),o=Go(t,r)**2/(re(t)*re(r)),a=Math.log(n),l=Math.log(a),i=-1.2725+1.0521*(l-a),s=1.0308-.26758*(l+2/a),c=P((Math.log(1-o)-i)/s,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:o,statisticName:"W",pValue:c}}function Dc(e,t={}){let n=hn(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),a=t.adjust??!0,l=new Array(o).fill(0);for(let f of tr(n)){let m=Math.floor(1+o*P(f));m>=1&&m<=o&&l[m-1]++}let i=r/o,s=l.reduce((f,m)=>f+(m-i)**2/i,0),c=o-1-(a?2:0);return{method:"Pearson chi-square normality test",statistic:s,statisticName:"P",pValue:he(s,c/2,2,!1,!1)}}function nr(e,t){let n=e[0]?.length??0;if(e.some(r=>r.length!==n))throw new TypeError(`'${t}' must be a matrix (rows of equal length)`);return Array.from({length:n},(r,o)=>e.map(a=>a[o]))}function rr(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`)}var Ma=e=>2*P(-Math.abs(e));function $c(e,t={}){let n=ie(t.weight,["unweighted","equal","squared"]);if(e.some(y=>y.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(y=>!y.some(Number.isNaN)),o=r.length,a=r.map(y=>St(y[0])),l=r.map(y=>St(y[1])),i=[...new Set([...a,...l])].sort((y,x)=>y<x?-1:y>x?1:0),s=i.length,c=new Map(i.map((y,x)=>[y,x])),f=Array.from({length:s},()=>new Array(s).fill(0));for(let y=0;y<o;y++)f[c.get(a[y])][c.get(l[y])]++;let m=y=>n==="unweighted"?y===0?1:0:n==="equal"?1-y/(s-1):1-y*y/((s-1)*(s-1)),u=Array.from({length:s},(y,x)=>Array.from({length:s},(q,k)=>m(Math.abs(x-k)))),h=f.map(y=>ee(y)/o),d=Array.from({length:s},(y,x)=>ee(f.map(q=>q[x]))/o),p=y=>{let x=[];for(let q=0;q<s;q++)for(let k=0;k<s;k++)x.push(y(q,k));return ee(x)},g=p((y,x)=>f[y][x]*u[y][x])/o,v=p((y,x)=>h[y]*d[x]*u[y][x]),w=(g-v)/(1-v),b=Array.from({length:s},(y,x)=>ee(d.map((q,k)=>q*u[x][k]))),N=Array.from({length:s},(y,x)=>ee(h.map((q,k)=>q*u[k][x]))),M=(p((y,x)=>h[y]*d[x]*(u[y][x]-(b[y]+N[x]))**2)-v*v)/(o*(1-v)**2),E=w/Math.sqrt(M);return{value:w,statistic:E,pValue:Ma(E)}}function Pc(e,t={}){let n=t.exact??!1,r=nr(e,"ratings");rr(e,"ratings");let o=e.length,a=r.length,l=e.flat(),i=ce(l),s=[...new Set(i)].sort((M,E)=>M-E),c=new Map(s.map((M,E)=>[M,E])),f=new Map(l.map((M,E)=>[M,c.get(i[E])])),m=s.length,u=e.map(M=>{let E=new Array(m).fill(0);for(let y of M)E[f.get(y)]++;return E}),h=ee(u.map(M=>(ee(M.map(E=>E*E))-a)/(a*(a-1))))/o,d=Array.from({length:m},(M,E)=>ee(u.map(y=>y[E]))/(o*a)),p=ee(d.map(M=>M*M));if(n){let M=r.map(y=>{let x=new Array(m).fill(0);for(let q of y)x[f.get(q)]++;return x.map(q=>q/o)}),E=Array.from({length:m},(y,x)=>re(M.map(q=>q[x]))*(a-1)/a);p-=ee(E)/(a-1)}let g=(h-p)/(1-p);if(n)return{value:g};let v=ee(d.map(M=>M*(1-M))),w=ee(d.map(M=>M*(1-M)*(1-M-M))),b=2/(v*v*o*a*(a-1))*(v*v-w),N=g/Math.sqrt(b);return{value:g,statistic:N,pValue:Ma(N)}}function zc(e,t={}){let n=ie(t.model,["oneway","twoway"]),r=ie(t.type,["consistency","agreement"]),o=ie(t.unit,["single","average"]),a=t.r0??0,l=t.confLevel??.95;Ee(l);let i=nr(e,"ratings");rr(e,"ratings");let s=e.length,c=i.length,f=1-l,m=re(e.map(V=>se(V)))*c,u=re(i.map(V=>se(V)))*s,h=se(e.map(V=>re(V))),p=(re(e.flat())*(s*c-1)-m*(s-1)-u*(c-1))/((s-1)*(c-1)),g=(V,j,Y)=>ae(V,j,Y,!1,!1),v=(V,j)=>it(1-f/2,V,j,!0,!1),w=s-1;if(n==="oneway"||r==="consistency"){let V=n==="oneway"?h:p,j=n==="oneway"?s*(c-1):(s-1)*(c-1),Y=m/V,T=Y/v(w,j),O=Y*v(j,w),[$,D,G]=o==="single"?[(m-V)/(m+(c-1)*V),Y*(1-a)/(1+(c-1)*a),Q=>(Q-1)/(Q+c-1)]:[(m-V)/m,Y*(1-a),Q=>1-1/Q];return{value:$,Fvalue:D,df1:w,df2:j,pValue:g(D,w,j),lbound:G(T),ubound:G(O)}}let b=o==="single"?c:1,N=V=>[b*V/(s*(1-V)),1+b*V*(s-1)/(s*(1-V))],M=(V,j)=>(V*u+j*p)**2/((V*u)**2/(c-1)+(j*p)**2/((s-1)*(c-1))),E=o==="single"?(m-p)/(m+(c-1)*p+c/s*(u-p)):(m-p)/(m+(u-p)/s),[y,x]=N(a),q=m/(y*u+x*p),k=M(y,x),[S,L]=[c*E/(s*(1-E)),1+c*E*(s-1)/(s*(1-E))],A=M(S,L),R=v(w,A),C=v(A,w),[F,I]=o==="single"?[s*(m-R*p)/(R*(c*u+(c*s-c-s)*p)+s*m),s*(C*m-p)/(c*u+(c*s-c-s)*p+s*C*m)]:[s*(m-R*p)/(R*(u-p)+s*m),s*(C*m-p)/(u-p+s*C*m)];return{value:E,Fvalue:q,df1:w,df2:k,pValue:g(q,w,k),lbound:F,ubound:I}}function ya(e,t){let n=t.length,r=ee(t.flatMap(a=>t.map(l=>e[a][l])));return(1-ee(t.map(a=>e[a][a]))/r)*n/(n-1)}function jc(e){let t=nr(e,"x").length;if(rr(e,"x"),t<2)throw new RangeError("'x' needs at least two items");let n=In(e),r=rn(e),o=Array.from({length:t},(s,c)=>c),a=ee(r.flat()),l=we(r),i=o.map(s=>1-1/l[s][s]);return{rawAlpha:ya(n,o),stdAlpha:(1-t/a)*t/(t-1),G6:1-(t-ee(i))/a,averageR:(a-t)/(t*(t-1)),alphaDrop:o.map(s=>ya(n,o.filter(c=>c!==s)))}}function Wc(e,t,n){let r=nr(e,"x").length;rr(e,"x");for(let s of[t,n])if(!Number.isInteger(s)||s<0||s>=r)throw new RangeError(`column index ${s} out of range (0 to ${r-1})`);if(t===n)throw new RangeError("'i' and 'j' must be different columns");let o=rn(e),a=[t,n],l=Array.from({length:r},(s,c)=>c).filter(s=>s!==t&&s!==n),i=a.map(s=>a.map(c=>o[s][c]));if(l.length>0){let s=l.map(m=>l.map(u=>o[m][u])),c=l.map(m=>a.map(u=>o[m][u])),f=we(s,c);for(let m=0;m<2;m++)for(let u=0;u<2;u++)i[m][u]-=ee(l.map((h,d)=>o[a[m]][h]*f[d][u]))}return i[0][1]/Math.sqrt(i[0][0]*i[1][1])}function ue(e){let t=0,n=0;for(let r of e){let[o,a]=st(t,r);t=o,n+=a}return t+n}var wo=(e,t)=>e[0].map((n,r)=>ue(e.map(o=>t(o[r]))));function qa(e,t){let n=t.map(r=>1/Math.sqrt(r));return e.map((r,o)=>r.map((a,l)=>n[o]*a*n[l]))}function xa(e,t,n){let r=pt(qa(e,t),{symmetric:!0}),o=r.values.slice(0,n).map(a=>Math.sqrt(Math.max(a-1,0)));return r.vectors.map((a,l)=>a.slice(0,n).map((i,s)=>Math.sqrt(t[l])*(i*o[s])))}function Ea(e,t){let n=wo(e,i=>i*i),r=n.map((i,s)=>s).sort((i,s)=>n[s]-n[i]||i-s),o=e.map(i=>r.map(s=>i[s])),a=t?.map(i=>r.map(s=>i[s])),l=wo(o,i=>i).map(i=>i<0?-1:1);return o=o.map(i=>i.map((s,c)=>l[c]<0?-s:s)),a=a?.map(i=>i.map((s,c)=>l[c]<0?-s:s)),a===void 0?{L:o}:{L:o,rot:a}}var Pe=(e,t)=>e.map(n=>t[0].map((r,o)=>{let a=0;for(let l=0;l<n.length;l++)a+=n[l]*t[l][o];return a})),Pt=e=>e[0].map((t,n)=>e.map(r=>r[n]));function or(e){let t=e[0].length,n=e.length,r=e.map(c=>Math.sqrt(ue(c.map(f=>f*f)))),o=e.map((c,f)=>c.map(m=>m/r[f])),a=Pt(o),l=Array.from({length:t},(c,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),i=0;for(let c=0;c<1e3;c++){let f=Pe(o,l),m=f[0].map((g,v)=>{let w=0;for(let b=0;b<n;b++)w+=f[b][v]*f[b][v];return w}),u=f.map(g=>g.map((v,w)=>v**3-v*m[w]/n)),h=Pe(a,u),d=Sn(h);l=Pe(d.u,Pt(d.v));let p=i;if(i=ue(d.d),i<p*(1+1e-5))break}return{loadings:Pe(o,l).map((c,f)=>c.map(m=>m*r[f])),rotmat:l}}function Gc(e){let t=or(e),n=t.loadings,r=n.map(i=>i.map(s=>s*Math.abs(s)**3)),o=Lt(pe(n),r),a=we(Pe(Pt(o),o)).map((i,s)=>i[s]),l=o.map(i=>i.map((s,c)=>s*Math.sqrt(a[c])));return{loadings:Pe(n,l),rotmat:Pe(t.rotmat,l)}}function Uc(e,t,n={}){let r=n.scores??"none";if(r!=="none"&&r!=="regression"&&r!=="Bartlett")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Cregression\u201D, \u201CBartlett\u201D");let o,a;if(n.covmat!==void 0){if(r!=="none")throw new RangeError("requested scores without an 'x' matrix");o=n.covmat.map(y=>y.slice()),a=n.nObs??NaN}else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");o=In(e),a=e.length}let l=o.length;if(l<3)throw new RangeError("factor analysis requires at least three variables");let i=t;if(!Number.isInteger(i)||i<1)throw new RangeError("'factors' must be a positive integer");let s=((l-i)**2-l-i)/2;if(s<0)throw new RangeError(`${i} ${i===1?"factor is":"factors are"} too many for ${l} variables`);let c=o.map((y,x)=>Math.sqrt(y[x])),f=o.map((y,x)=>y.map((q,k)=>q/(c[x]*c[k]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??we(f).map((y,x)=>(1-i/(2*l))/y[x]),p=On(u,y=>{let x=pt(qa(f,y),{symmetric:!0,onlyValues:!0}).values.slice(i);return-(ue(x.map(q=>Math.log(q)-q))-i+l)},{gr:y=>{let x=xa(f,y,i);return y.map((q,k)=>{let S=0;for(let L=0;L<i;L++)S+=x[k][L]*x[k][L];return(S+q-f[k][k])/(q*q)})},method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(l).fill(.01)}});if(p.convergence!==0)throw new Error("unable to optimize from this starting value");let g=p.par,v=Ea(xa(f,g,i)).L,w,b=n.rotation??"varimax";if(b!=="none"&&i>=2){let y=b==="varimax"?or(v):Gc(v),x=Ea(y.loadings,y.rotmat);v=x.L,w=x.rot}else if(b!=="varimax"&&b!=="promax"&&b!=="none")throw new RangeError(`unknown rotation '${String(b)}'`);let N=NaN,M=NaN;!Number.isNaN(a)&&s>0&&(N=(a-1-(2*l+5)/6-2*i/3)*p.value,M=je(N,s,{lowerTail:!1}));let E={uniquenesses:g,loadings:v,STATISTIC:N,dof:s,PVAL:M,converged:!0,objective:p.value,counts:p.counts};return r!=="none"&&(E.scores=Hc(e,f,g,v,r)),w!==void 0&&(E.rotmat=w),E}function Hc(e,t,n,r,o){let a=e.length,l=e[0].map((m,u)=>ue(e.map(h=>h[u]))/a),i=e.map(m=>m.map((u,h)=>u-l[h])),s=wo(i,m=>m*m).map(m=>Math.sqrt(m/(a-1))),c=i.map(m=>m.map((u,h)=>u/s[h]));if(o==="regression")return Pe(c,we(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return Pe(c,Pt(we(Pe(Pt(r),f),Pt(f))))}var No=2**-52,Bc=`
12
12
  Three factors are required for identification -- general factor loadings set to be equal.
13
13
  Proceed with caution.
14
14
  Think about redoing the analysis with alternative values of the 'option' setting.
15
15
 
16
- `,mn=(e,t)=>e.map(n=>t[0].map((r,o)=>{let a=0;for(let i=0;i<n.length;i++)a+=n[i]*t[i][o];return a})),un=e=>e[0].map((t,n)=>e.map(r=>r[n])),hn=(e,t=n=>n)=>e[0].map((n,r)=>ue(e.map(o=>t(o[r])))),qa=(e,t=n=>n)=>e.map(n=>ue(n.map(t))),Dt=(e,t=n=>n)=>ue(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),nr=e=>e<0?-1:1;function vo(e){let t=e.map((n,r)=>Math.sqrt(n[r]));return e.map((n,r)=>n.map((o,a)=>r===a?1:o/(t[r]*t[a])))}function ka(e,t){let n=vo(e),{d:r,u:o,v:a}=An(n),i=r.map((s,c)=>c).filter(s=>r[s]>Math.sqrt(wo)*r[0]),l=n.map((s,c)=>{let f=0;for(let m of i)f+=a[c][m]*(1/r[m])*o[c][m];return 1-1/f});return l.some(s=>s>1)&&Nt(t,"In smc, smcs > 1 were set to 1.0"),l.some(s=>s<0)&&Nt(t,"In smc, smcs < 0 were set to .0"),l.map(s=>s>1?1:s<0?0:s)}var Nt=(e,t)=>{e.includes(t)||e.push(t)};function po(e,t,n,r){let o=e.map((l,s)=>l.map((c,f)=>s===f?t[s]:c)),a=mt(o,{symmetric:!0}),i=a.values.slice(0,n).map(l=>Math.sqrt(r(l)));return a.vectors.map(l=>l.slice(0,n).map((s,c)=>s*i[c]))}var zc=e=>mn(e,un(e));function Ea(e,t,n,r){let o=vo(e),a=o.length,i=ka(o,r),l=u=>{let h=po(o,u.map(g=>1-g),t,g=>g<wo?100*wo:g),p=zc(h),d=[];for(let g=0;g<a;g++)for(let b=g+1;b<a;b++)d.push((o[b][g]-p[b][g])**2);return ue(d)},s=u=>{let h=po(o,u.map((p,d)=>o[d][d]-p),t,p=>Math.max(p,0));return u.map((p,d)=>{let g=0;for(let b=0;b<t;b++)g+=h[d][b]*h[d][b];return g+p-o[d][d]})},f=Sn(i.map(u=>1-u),l,{gr:s,method:"L-BFGS-B",lower:.005,upper:Math.max(Math.max(...i),1),control:{fnscale:1,parscale:new Array(a).fill(.01)}}).par,m=po(o,f.map((u,h)=>o[h][h]-u),t,u=>Math.max(u,0));if(m=go(m,t),m=m.map(u=>u.map(h=>h===0?1e-15:h)),qa(m,u=>u*u).some(u=>u>1)&&Nt(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){m=go(tr(m).loadings,t);let u=hn(m,p=>p*p),h=u.map((p,d)=>d).sort((p,d)=>u[d]-u[p]||p-d);m=m.map(p=>h.map(d=>p[d]))}else m=go(m,t);return m}function go(e,t){let n=hn(e).map(nr);return t===1?n[0]<0?e.map(r=>r.map(o=>-o)):e:e.map(r=>r.map((o,a)=>n[a]*o))}function Wc(e){let t=e[0].length,n=e.map(o=>o.map(a=>a*a)),r=n.map(o=>o.map((a,i)=>{let l=0;for(let s=0;s<t;s++)s!==i&&(l+=o[s]);return l}));return{f:Dt(n.map((o,a)=>o.map((i,l)=>i*r[a][l])))/4,Gq:e.map((o,a)=>o.map((i,l)=>i*r[a][l]))}}function Gc(e){let t=e[0].length,n=c=>{let f=be(c),m=mn(e,un(f)),{f:u,Gq:h}=Wc(m),p=un(mn(mn(un(m),h),f)).map(d=>d.map(g=>-g));return{L:m,f:u,G:p}},r=Array.from({length:t},(c,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),o=1,a=n(r),i,l=[],s=!1;for(let c=0;c<=2e3;c++){let f=hn(r.map((g,b)=>g.map((w,v)=>w*a.G[b][v]))),m=a.G.map((g,b)=>g.map((w,v)=>w-r[b][v]*f[v])),u=Math.sqrt(Dt(m,g=>g*g));if(l.push(a.f),u<1e-5){s=!0;break}if(i!==void 0){let g=i,b=r.map((v,N)=>v.map((M,E)=>M-g.T[N][E])),w=m.map((v,N)=>v.map((M,E)=>M-g.Gp[N][E]));Dt(w,v=>v*v)>0&&(o=Dt(b,v=>v*v)/Math.abs(Dt(b.map((v,N)=>v.map((M,E)=>M*w[N][E])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=Math.max(...l.slice(-10)),p=a,d=r;for(let g=0;g<=10;g++){let b=r.map((v,N)=>v.map((M,E)=>M-o*m[N][E])),w=hn(b,v=>v*v).map(v=>1/Math.sqrt(v));if(d=b.map(v=>v.map((N,M)=>N*w[M])),p=n(d),h-p.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=d,a=p}return{loadings:a.L,phi:mn(un(r),r),converged:s}}function Uc(e,t={}){let n=t.nfactors??3;if(!Number.isInteger(n)||n<1)throw new RangeError("'nfactors' must be a positive integer");let r;if(t.covmat!==void 0)r=vo(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=en(e,void 0,{use:"pairwise.complete.obs"})}let o=r.length,a=[],i=[],l=Ea(r,n,a,i),s,c,f;if(n===1)s=l.map(T=>T.slice()),f=[1],Nt(i,`Omega_h for 1 factor is not meaningful, just omega_t
17
- `),Nt(a,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let T=Gc(l);if(T.converged||Nt(a,"oblimin did not converge in 2000 iterations"),s=T.loadings,c=T.phi,n===2){let O=c[0][1],$=Math.sqrt(Math.abs(O));f=[$,nr(O)*$],Nt(i,jc)}else f=Ea(c,1,a,i).map(O=>O[0])}let m=s.map(T=>{let O=0;for(let $=0;$<n;$++)O+=T[$]*f[$];return O}),u=qa(l,T=>T*T),h=u.map((T,O)=>r[O][O]-T),p=s.map((T,O)=>T.map(($,D)=>{if(n===1)return 0;let G=r[O][O],Q=G-(G-$*$)-($*f[D])**2;return Q<0&&(Q=0),nr($)*Math.sqrt(Q)})),d=m.map((T,O)=>T*T/u[O]),g=m.map((T,O)=>{let $=[T,...p[O]];return ue($.map(D=>D*D))**2/ue($.map(D=>D**4))}),b=m.map(nr);b.some(T=>T<0)&&(r=r.map((T,O)=>T.map(($,D)=>b[O]*$*b[D])));let w=m.map((T,O)=>b[O]*T),v=p.map((T,O)=>T.map($=>b[O]*$)),N=w.map((T,O)=>[T,...v[O],u[O],h[O],d[O],g[O]]),M=Dt(r),E=ue(r.map((T,O)=>T[O])),y=ue(w)**2,x=ue(h),k=y/M,q=(M-x)/M,S=y/(M-x),L=(M-E)/M*o/(o-1),A=(M+ue(ka(r,i))-E)/M,R=hn(N.map(T=>T.slice(0,n+1)),T=>T*T),C=R[0]/ue(R),F=v.map(T=>{let O=0;for(let $=1;$<n;$++)Math.abs(T[$])>Math.abs(T[O])&&(O=$);return O}),I=[],V=[];for(let T=0;T<n;T++){let O=F.map((Q,ne)=>ne).filter(Q=>F[Q]===T),$=ue(O.flatMap(Q=>O.map(ne=>r[ne][Q]))),D=ue(O.map(Q=>v[Q][T])),G=ue(O.map(Q=>w[Q]));V.push(D*D),I.push([(D*D+G*G)/$,G*G/$,D*D/$])}let z=[[q,ue(w)**2/M,ue(V)/M],...I],J={omegaH:k,omegaLim:S,alpha:L,omegaTot:q,G6:A,ECV:C,sl:N,slNames:["g",...Array.from({length:n},(T,O)=>`F${O+1}*`),"h2","u2","p2","com"],orthog:l,oblique:s,gloading:f,key:b,omegaGroup:z,warnings:a,messages:i};return c!==void 0&&(J.phi=c),J}var qe=.9999;function No(e){let t=ee(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(H(r/t));return n}var yo=e=>[...new Set(e)].sort((t,n)=>t-n),Aa=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Ra=e=>Array.isArray(e)||ArrayBuffer.isView(e),Ta=(e,t)=>e>0?P(e,{lowerTail:!1})-P(t,{lowerTail:!1}):P(t)-P(e);function Mo(e){let t=se(e),n=e.map(o=>o-t),r=Math.sqrt(re(n));return n.map(o=>o/r)}function Hc(e){if(Math.abs(e)>=.1)return e-Math.log1p(e);let t=e,n=0;for(let r=2;r<40;r++){t*=-e;let o=t/r;if(n+=o,Math.abs(o)<=1e-17*Math.abs(n))break}return-n}var rr=e=>Array.from({length:e},()=>new Array(e).fill(0));function Ia(e,t,n,r,o){e.value+=t*Math.log(n);let a=r.length;for(let i=0;i<a;i++){e.gradient[i]+=t*r[i]/n;for(let l=0;l<a;l++)e.hessian[i][l]+=t*(o[i][l]/n-r[i]*r[l]/(n*n))}}var bo={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function Bc(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return bo;if(e===1/0)return{...bo,k:ye(t),kk:-t*ye(t)};if(t===1/0)return{...bo,h:ye(e),hh:-e*ye(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),a=e*e-2*n*e*t+t*t,i=Math.exp(-a/(2*r))/(2*Math.PI*o),l=(t-n*e)/o,s=(e-n*t)/o,c=ye(e),f=ye(t);return{r:i,h:c*P(l),k:f*P(s),rr:i*(n/r+(e*t*r-n*a)/(r*r)),rh:-i*(e-n*t)/r,rk:-i*(t-n*e)/r,hh:-e*c*P(l)-n/o*c*ye(l),hk:i,kk:-t*f*P(s)-n/o*f*ye(s)}}function Sa(e,t,n,r,o){let a=e.length,i=e[0].length,l=o?a+i-1:1,s={value:0,gradient:new Array(l).fill(0),hessian:rr(l)},c=h=>o&&h>=1&&h<=a-1?h:-1,f=h=>o&&h>=1&&h<=i-1?a-1+h:-1,m=t.map(h=>n.map(p=>Bc(h,p,r))),u=[];for(let h=0;h<a;h++)for(let p=0;p<i;p++){let d=e[h][p];if(d===0)continue;let g=Kt([t[h],n[p]],[t[h+1],n[p+1]],r),b=new Array(l).fill(0),w=rr(l);for(let[v,N,M]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let E=m[v][N],y=c(v),x=f(N);b[0]+=M*E.r,w[0][0]+=M*E.rr,y>=0&&(b[y]+=M*E.h,w[0][y]+=M*E.rh,w[y][0]+=M*E.rh,w[y][y]+=M*E.hh),x>=0&&(b[x]+=M*E.k,w[0][x]+=M*E.rk,w[x][0]+=M*E.rk,w[x][x]+=M*E.kk),y>=0&&x>=0&&(w[y][x]+=M*E.hk,w[x][y]+=M*E.hk)}Ia(s,d,g,b,w),u.push(d*Math.log(g))}return s.value=ee(u),s}function Xc(e,t,n,r,o){let a=o?n.length+1:1,i={value:0,gradient:new Array(a).fill(0),hessian:rr(a)},l=(1-r)*(1+r),s=Math.sqrt(l),c=l*s,f=new Array(a),m=rr(a),u=[],h=(p,d,g)=>{let b=n[p],w=(b-r*d)/s,v=ye(w),N=(r*b-d)/c,M=1/s,E=b/c+3*r*(r*b-d)/(c*l),y=r/c;if(f[0]+=g*v*N,m[0][0]+=g*v*(E-w*N*N),o){let x=p+1;f[x]+=g*v*M;let k=g*v*(y-w*N*M);m[0][x]+=k,m[x][0]+=k,m[x][x]+=g*v*(-w*M*M)}};for(let p=0;p<e.length;p++){let d=e[p],g=t[p];f.fill(0);for(let v of m)v.fill(0);let b=g<n.length?(n[g]-r*d)/s:1/0,w=g>0?(n[g-1]-r*d)/s:-1/0;g<n.length&&h(g,d,1),g>0&&h(g-1,d,-1),Ia(i,1,Ta(w,b),f,m),u.push(Math.log(Ta(w,b)),ye(d,{log:!0}))}return i.value=ee(u),i}function Qc(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(i=>e.gradient[i]),o=t.map(i=>t.map(l=>-e.hessian[i][l])),a;try{a=be(o,r)}catch{a=void 0}return(a===void 0||!a.every(Number.isFinite)||ee(a.map((i,l)=>i*r[l]))<=0)&&(a=r.map((i,l)=>i/Math.max(Math.abs(o[l][l]),1))),t.forEach((i,l)=>n[i]=a[l]),n}var Oa=(e,t)=>e>=t-1e-12*Math.abs(t);function La(e,t,n){let r=[...t],o=e(r),a=n,i=r.map((l,s)=>s);for(let l=0;l<200;l++){let s=a?i.slice(1):i;if(s.length===0)break;let c=Qc(o,s);if(!a&&Math.abs(r[0])===qe&&c[0]*r[0]>0){a=!0;continue}let f=Math.abs(r[0]+c[0])>qe?(Math.sign(c[0])*qe-r[0])/c[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((d,g)=>d+f*c[g]);f*c[0]!==0&&Math.abs(h[0])>=qe*(1-1e-15)&&(h[0]=Math.sign(h[0])*qe);let p=e(h);if(Oa(p.value,o.value)){let d=Math.max(...h.map((g,b)=>Math.abs(g-r[b])/Math.max(1,Math.abs(r[b]))));r=h,o=p,m=d>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:a}}function Fa(e,t){let n=La(e,[Math.max(-qe,Math.min(qe,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=La(e,[Math.sign(n.theta[0])*qe,...n.theta.slice(1)],!0);return Oa(r.at.value,n.at.value)?r:n}function _a(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=be(e.map(o=>o.map(a=>-a)))}catch{return n()}return r.map((o,a)=>o.map((i,l)=>(i+r[l][a])/2))}var Va=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function Kc(e,t,n){let r,o;if(Ra(t)){let R=e,C=t;if(R.length!==C.length)throw new RangeError("'x' and 'y' must have the same length");let F=R.map((T,O)=>!Number.isNaN(T)&&!Number.isNaN(C[O])),I=R.filter((T,O)=>F[O]),V=C.filter((T,O)=>F[O]),z=new Map(yo(I).map((T,O)=>[T,O])),J=new Map(yo(V).map((T,O)=>[T,O]));r=Array.from({length:z.size},()=>new Array(J.size).fill(0)),I.forEach((T,O)=>r[z.get(T)][J.get(V[O])]++),o=n??{}}else{let R=e;if(!R.every(Ra))throw new TypeError("give a table (array of rows) or two vectors");let C=R[0]?.length??0;if(R.some(F=>F.length!==C))throw new TypeError("the table must have rows of equal length");r=R.map(F=>Array.from(F)),o=t??{}}let{ML:a=!1,stdErr:i=!1}=o,l=[],s=r.map(R=>ee(R)),c=s.filter(R=>R===0).length;c>0&&(l.push(Aa(c,"row")),r=r.filter((R,C)=>s[C]!==0));let f=(r[0]??[]).map((R,C)=>ee(r.map(F=>F[C]))),m=f.filter(R=>R===0).length;m>0&&(l.push(Aa(m,"column")),r=r.map(R=>R.filter((C,F)=>f[F]!==0)));let u=r.length,h=r[0]?.length??0;if(u<2&&l.push("the table has fewer than 2 rows"),h<2&&l.push("the table has fewer than 2 columns"),u<2||h<2)return{rho:NaN,warnings:l};let p=[-1/0,...No(r.map(R=>ee(R))),1/0],d=[-1/0,...No(r[0].map((R,C)=>ee(r.map(F=>F[C])))),1/0],b=Xo(R=>{let C=Math.max(-qe,Math.min(qe,R)),F=[];for(let I=0;I<u;I++)for(let V=0;V<h;V++){if(r[I][V]===0)continue;let z=Kt([p[I],d[V]],[p[I+1],d[V+1]],C);F.push(r[I][V]*Math.log(z))}return-ee(F)},-qe,qe).optimum;if(!a&&!i)return{rho:b,warnings:l};let w=R=>[-1/0,...R,1/0],v=p.slice(1,u),N=d.slice(1,h),M=R=>a?Sa(r,w(R.slice(1,u)),w(R.slice(u)),R[0],!0):Sa(r,p,d,R[0],!1),{theta:E,at:y,bounded:x}=Fa(M,a?[b,...v,...N]:[b]),k=E[0];if(x&&l.push(Va(k)),!i)return{rho:k,warnings:l};let q=a?w(E.slice(1,u)):p,S=a?w(E.slice(u)):d,L=ee(r.map(R=>ee(R))),A=[];for(let R=0;R<u;R++)for(let C=0;C<h;C++){let F=r[R][C],I=Kt([q[R],S[C]],[q[R+1],S[C+1]],k);F===0?A.push(L*I):A.push(F*Math.log1p(1e-6/F),F*Hc(L*I/F-1))}return{rho:k,warnings:l,var:_a(y.hessian,x),chisq:2*ee(A),df:u*h-u-h,n:L,rowCuts:q.slice(1,u),colCuts:S.slice(1,h)}}function Zc(e,t,n,r){let o=e.length,a=[H(.25),H(.5),H(.75)],i=(h,p)=>p.filter(d=>d<h).length,l=Mo(e).map(h=>i(h,n)),s=Mo(t).map(h=>i(h,a)),c=Array.from({length:n.length+1},()=>new Array(4).fill(0));l.forEach((h,p)=>c[h][s[p]]++);let f=[-1/0,...n,1/0],m=[-1/0,...a,1/0],u=[];return c.forEach((h,p)=>h.forEach((d,g)=>{if(d===0)return;let b=Kt([f[p],m[g]],[f[p+1],m[g+1]],r);u.push(d*Math.log((d+1e-6)/(b*o)))})),2*ee(u)}function Yc(e,t,n={}){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,a=e.map((x,k)=>!Number.isNaN(x)&&!Number.isNaN(t[k])),i=e.filter((x,k)=>a[k]),l=t.filter((x,k)=>a[k]),s=i.length,c=yo(l);if(c.length<2)return{rho:NaN,warnings:["y has fewer than 2 levels"]};let f=new Map(c.map((x,k)=>[x,k+1])),m=l.map(x=>f.get(x)),u=c.map(()=>0);for(let x of m)u[x-1]++;let h=No(u),p=[],d=Math.sqrt((s-1)/s)*Math.sqrt(re(m))*Qo(i,m)[0]/ee(h.map(x=>ye(x)));if(Math.abs(d)>qe){let x=Math.sign(d)*qe;p.push(`initial correlation inadmissible, ${Rt(d)}, set to ${Rt(x)}`),d=x}if(!r&&!o)return{rho:d,warnings:p};let g=Mo(i),b=m.map(x=>x-1),w=x=>Xc(g,b,r?x.slice(1):h,x[0],r),{theta:v,at:N,bounded:M}=Fa(w,r?[d,...h]:[d]);if(M&&p.push(Va(v[0])),!o)return{rho:v[0],warnings:p};let E=r?v.slice(1):h,y=c.length;return{rho:v[0],warnings:p,var:_a(N.hessian,M),chisq:Zc(m,g,E,r?v[0]:d),df:4*y-y-1,n:s,cuts:E}}export{Qr as Anova,Yi as TukeyHSD,Tc as adTest,Br as anova,Ms as ansariTest,Cr as aov,tc as aovEz,Xr as aovSummary,ms as bartlettTest,yr as binomTest,$i as brunnerMunzelTest,Pa as chisqTest,gi as cochranQTest,Ks as cohenES,zl as cohensD,Xi as confint,Da as corTest,Mc as coxph,Cc as cronbachAlpha,Sc as cvmTest,cc as dscfAllPairsTest,hc as dunnTest,ic as dunnettTest,Yl as epsilonSquared,ql as esH,kl as esW1,Al as esW2,Kl as etaSquared,Pc as factanal,Ha as fisherTest,ps as flignerTest,qr as friedmanTest,fc as gamesHowellTest,dc as glhtDunnett,sl as glm,pl as glmAnova,ml as glmConfint,cl as glmCooksDistance,Wr as glmHatvalues,fl as glmPredict,zr as glmResiduals,ll as glmRstandard,jr as glmSummary,Wl as hedgesG,Vc as icc,Rc as jonckheereTest,Fc as kappa2,_c as kappamFleiss,Dn as kruskalTest,Rs as ksTest,sc as kwAllPairsDunnTest,ac as kwManyOneDunnTest,xc as leveneTest,Lc as lillieTest,Vr as lm,Si as mantelhaenTest,lc as manyOneUTest,Nl as mauchlyTest,pi as mcnemarTest,St as modelMatrix,ds as moodTest,Uc as omega,Zl as omegaSquared,vs as onewayTest,It as pAdjust,Gi as pairwisePropTest,zi as pairwiseTTest,Wi as pairwiseWilcoxTest,Dc as partialR,Oc as pearsonTest,hi as poissonTest,Kc as polychor,Yc as polyserial,xl as powerAnovaTest,Ml as powerPropTest,yl as powerTTest,Qi as predict,Mr as propTest,Ec as propTrendTest,_l as pwr2p2nTest,Il as pwr2pTest,Dl as pwrAnovaTest,Cl as pwrChisqTest,$l as pwrF2Test,Fl as pwrNormTest,Ol as pwrPTest,Vl as pwrRTest,Ll as pwrT2nTest,Sl as pwrTTest,hs as quadeTest,Ic as sfTest,Ai as shapiroTest,uc as steelTest,wc as survdiff,vc as survfit,Lt as tTest,mc as tukeyTest,fs as varTest,tn as wilcoxTest,Ji as zTest};
16
+ `,pn=(e,t)=>e.map(n=>t[0].map((r,o)=>{let a=0;for(let l=0;l<n.length;l++)a+=n[l]*t[l][o];return a})),dn=e=>e[0].map((t,n)=>e.map(r=>r[n])),gn=(e,t=n=>n)=>e[0].map((n,r)=>ue(e.map(o=>t(o[r])))),Aa=(e,t=n=>n)=>e.map(n=>ue(n.map(t))),zt=(e,t=n=>n)=>ue(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),sr=e=>e<0?-1:1;function yo(e){let t=e.map((n,r)=>Math.sqrt(n[r]));return e.map((n,r)=>n.map((o,a)=>r===a?1:o/(t[r]*t[a])))}function Ra(e,t){let n=yo(e),{d:r,u:o,v:a}=Sn(n),l=r.map((s,c)=>c).filter(s=>r[s]>Math.sqrt(No)*r[0]),i=n.map((s,c)=>{let f=0;for(let m of l)f+=a[c][m]*(1/r[m])*o[c][m];return 1-1/f});return i.some(s=>s>1)&&Mt(t,"In smc, smcs > 1 were set to 1.0"),i.some(s=>s<0)&&Mt(t,"In smc, smcs < 0 were set to .0"),i.map(s=>s>1?1:s<0?0:s)}var Mt=(e,t)=>{e.includes(t)||e.push(t)};function vo(e,t,n,r){let o=e.map((i,s)=>i.map((c,f)=>s===f?t[s]:c)),a=pt(o,{symmetric:!0}),l=a.values.slice(0,n).map(i=>Math.sqrt(r(i)));return a.vectors.map(i=>i.slice(0,n).map((s,c)=>s*l[c]))}var Xc=e=>pn(e,dn(e));function ka(e,t,n,r){let o=yo(e),a=o.length,l=Ra(o,r),i=u=>{let h=vo(o,u.map(g=>1-g),t,g=>g<No?100*No:g),d=Xc(h),p=[];for(let g=0;g<a;g++)for(let v=g+1;v<a;v++)p.push((o[v][g]-d[v][g])**2);return ue(p)},s=u=>{let h=vo(o,u.map((d,p)=>o[p][p]-d),t,d=>Math.max(d,0));return u.map((d,p)=>{let g=0;for(let v=0;v<t;v++)g+=h[p][v]*h[p][v];return g+d-o[p][p]})},f=On(l.map(u=>1-u),i,{gr:s,method:"L-BFGS-B",lower:.005,upper:Math.max(Math.max(...l),1),control:{fnscale:1,parscale:new Array(a).fill(.01)}}).par,m=vo(o,f.map((u,h)=>o[h][h]-u),t,u=>Math.max(u,0));if(m=bo(m,t),m=m.map(u=>u.map(h=>h===0?1e-15:h)),Aa(m,u=>u*u).some(u=>u>1)&&Mt(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){m=bo(or(m).loadings,t);let u=gn(m,d=>d*d),h=u.map((d,p)=>p).sort((d,p)=>u[p]-u[d]||d-p);m=m.map(d=>h.map(p=>d[p]))}else m=bo(m,t);return m}function bo(e,t){let n=gn(e).map(sr);return t===1?n[0]<0?e.map(r=>r.map(o=>-o)):e:e.map(r=>r.map((o,a)=>n[a]*o))}function Qc(e){let t=e[0].length,n=e.map(o=>o.map(a=>a*a)),r=n.map(o=>o.map((a,l)=>{let i=0;for(let s=0;s<t;s++)s!==l&&(i+=o[s]);return i}));return{f:zt(n.map((o,a)=>o.map((l,i)=>l*r[a][i])))/4,Gq:e.map((o,a)=>o.map((l,i)=>l*r[a][i]))}}function Kc(e){let t=e[0].length,n=c=>{let f=we(c),m=pn(e,dn(f)),{f:u,Gq:h}=Qc(m),d=dn(pn(pn(dn(m),h),f)).map(p=>p.map(g=>-g));return{L:m,f:u,G:d}},r=Array.from({length:t},(c,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),o=1,a=n(r),l,i=[],s=!1;for(let c=0;c<=2e3;c++){let f=gn(r.map((g,v)=>g.map((w,b)=>w*a.G[v][b]))),m=a.G.map((g,v)=>g.map((w,b)=>w-r[v][b]*f[b])),u=Math.sqrt(zt(m,g=>g*g));if(i.push(a.f),u<1e-5){s=!0;break}if(l!==void 0){let g=l,v=r.map((b,N)=>b.map((M,E)=>M-g.T[N][E])),w=m.map((b,N)=>b.map((M,E)=>M-g.Gp[N][E]));zt(w,b=>b*b)>0&&(o=zt(v,b=>b*b)/Math.abs(zt(v.map((b,N)=>b.map((M,E)=>M*w[N][E])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=Math.max(...i.slice(-10)),d=a,p=r;for(let g=0;g<=10;g++){let v=r.map((b,N)=>b.map((M,E)=>M-o*m[N][E])),w=gn(v,b=>b*b).map(b=>1/Math.sqrt(b));if(p=v.map(b=>b.map((N,M)=>N*w[M])),d=n(p),h-d.f>.5*u*u*o)break;o=o/2}l={T:r,Gp:m},r=p,a=d}return{loadings:a.L,phi:pn(dn(r),r),converged:s}}function Zc(e,t={}){let n=t.nfactors??3;if(!Number.isInteger(n)||n<1)throw new RangeError("'nfactors' must be a positive integer");let r;if(t.covmat!==void 0)r=yo(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=rn(e,void 0,{use:"pairwise.complete.obs"})}let o=r.length,a=[],l=[],i=ka(r,n,a,l),s,c,f;if(n===1)s=i.map(T=>T.slice()),f=[1],Mt(l,`Omega_h for 1 factor is not meaningful, just omega_t
17
+ `),Mt(a,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let T=Kc(i);if(T.converged||Mt(a,"oblimin did not converge in 2000 iterations"),s=T.loadings,c=T.phi,n===2){let O=c[0][1],$=Math.sqrt(Math.abs(O));f=[$,sr(O)*$],Mt(l,Bc)}else f=ka(c,1,a,l).map(O=>O[0])}let m=s.map(T=>{let O=0;for(let $=0;$<n;$++)O+=T[$]*f[$];return O}),u=Aa(i,T=>T*T),h=u.map((T,O)=>r[O][O]-T),d=s.map((T,O)=>T.map(($,D)=>{if(n===1)return 0;let G=r[O][O],Q=G-(G-$*$)-($*f[D])**2;return Q<0&&(Q=0),sr($)*Math.sqrt(Q)})),p=m.map((T,O)=>T*T/u[O]),g=m.map((T,O)=>{let $=[T,...d[O]];return ue($.map(D=>D*D))**2/ue($.map(D=>D**4))}),v=m.map(sr);v.some(T=>T<0)&&(r=r.map((T,O)=>T.map(($,D)=>v[O]*$*v[D])));let w=m.map((T,O)=>v[O]*T),b=d.map((T,O)=>T.map($=>v[O]*$)),N=w.map((T,O)=>[T,...b[O],u[O],h[O],p[O],g[O]]),M=zt(r),E=ue(r.map((T,O)=>T[O])),y=ue(w)**2,x=ue(h),q=y/M,k=(M-x)/M,S=y/(M-x),L=(M-E)/M*o/(o-1),A=(M+ue(Ra(r,l))-E)/M,R=gn(N.map(T=>T.slice(0,n+1)),T=>T*T),C=R[0]/ue(R),F=b.map(T=>{let O=0;for(let $=1;$<n;$++)Math.abs(T[$])>Math.abs(T[O])&&(O=$);return O}),I=[],V=[];for(let T=0;T<n;T++){let O=F.map((Q,ne)=>ne).filter(Q=>F[Q]===T),$=ue(O.flatMap(Q=>O.map(ne=>r[ne][Q]))),D=ue(O.map(Q=>b[Q][T])),G=ue(O.map(Q=>w[Q]));V.push(D*D),I.push([(D*D+G*G)/$,G*G/$,D*D/$])}let j=[[k,ue(w)**2/M,ue(V)/M],...I],Y={omegaH:q,omegaLim:S,alpha:L,omegaTot:k,G6:A,ECV:C,sl:N,slNames:["g",...Array.from({length:n},(T,O)=>`F${O+1}*`),"h2","u2","p2","com"],orthog:i,oblique:s,gloading:f,key:v,omegaGroup:j,warnings:a,messages:l};return c!==void 0&&(Y.phi=c),Y}var Ae=.9999;function xo(e){let t=ee(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(B(r/t));return n}var Eo=e=>[...new Set(e)].sort((t,n)=>t-n),Ta=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Sa=e=>Array.isArray(e)||ArrayBuffer.isView(e),La=(e,t)=>e>0?P(e,{lowerTail:!1})-P(t,{lowerTail:!1}):P(t)-P(e);function qo(e){let t=se(e),n=e.map(o=>o-t),r=Math.sqrt(re(n));return n.map(o=>o/r)}function Yc(e){if(Math.abs(e)>=.1)return e-Math.log1p(e);let t=e,n=0;for(let r=2;r<40;r++){t*=-e;let o=t/r;if(n+=o,Math.abs(o)<=1e-17*Math.abs(n))break}return-n}var ar=e=>Array.from({length:e},()=>new Array(e).fill(0));function Fa(e,t,n,r,o){e.value+=t*Math.log(n);let a=r.length;for(let l=0;l<a;l++){e.gradient[l]+=t*r[l]/n;for(let i=0;i<a;i++)e.hessian[l][i]+=t*(o[l][i]/n-r[l]*r[i]/(n*n))}}var Mo={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function Jc(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Mo;if(e===1/0)return{...Mo,k:xe(t),kk:-t*xe(t)};if(t===1/0)return{...Mo,h:xe(e),hh:-e*xe(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),a=e*e-2*n*e*t+t*t,l=Math.exp(-a/(2*r))/(2*Math.PI*o),i=(t-n*e)/o,s=(e-n*t)/o,c=xe(e),f=xe(t);return{r:l,h:c*P(i),k:f*P(s),rr:l*(n/r+(e*t*r-n*a)/(r*r)),rh:-l*(e-n*t)/r,rk:-l*(t-n*e)/r,hh:-e*c*P(i)-n/o*c*xe(i),hk:l,kk:-t*f*P(s)-n/o*f*xe(s)}}function Ia(e,t,n,r,o){let a=e.length,l=e[0].length,i=o?a+l-1:1,s={value:0,gradient:new Array(i).fill(0),hessian:ar(i)},c=h=>o&&h>=1&&h<=a-1?h:-1,f=h=>o&&h>=1&&h<=l-1?a-1+h:-1,m=t.map(h=>n.map(d=>Jc(h,d,r))),u=[];for(let h=0;h<a;h++)for(let d=0;d<l;d++){let p=e[h][d];if(p===0)continue;let g=Jt([t[h],n[d]],[t[h+1],n[d+1]],r),v=new Array(i).fill(0),w=ar(i);for(let[b,N,M]of[[h+1,d+1,1],[h,d+1,-1],[h+1,d,-1],[h,d,1]]){let E=m[b][N],y=c(b),x=f(N);v[0]+=M*E.r,w[0][0]+=M*E.rr,y>=0&&(v[y]+=M*E.h,w[0][y]+=M*E.rh,w[y][0]+=M*E.rh,w[y][y]+=M*E.hh),x>=0&&(v[x]+=M*E.k,w[0][x]+=M*E.rk,w[x][0]+=M*E.rk,w[x][x]+=M*E.kk),y>=0&&x>=0&&(w[y][x]+=M*E.hk,w[x][y]+=M*E.hk)}Fa(s,p,g,v,w),u.push(p*Math.log(g))}return s.value=ee(u),s}function ef(e,t,n,r,o){let a=o?n.length+1:1,l={value:0,gradient:new Array(a).fill(0),hessian:ar(a)},i=(1-r)*(1+r),s=Math.sqrt(i),c=i*s,f=new Array(a),m=ar(a),u=[],h=(d,p,g)=>{let v=n[d],w=(v-r*p)/s,b=xe(w),N=(r*v-p)/c,M=1/s,E=v/c+3*r*(r*v-p)/(c*i),y=r/c;if(f[0]+=g*b*N,m[0][0]+=g*b*(E-w*N*N),o){let x=d+1;f[x]+=g*b*M;let q=g*b*(y-w*N*M);m[0][x]+=q,m[x][0]+=q,m[x][x]+=g*b*(-w*M*M)}};for(let d=0;d<e.length;d++){let p=e[d],g=t[d];f.fill(0);for(let b of m)b.fill(0);let v=g<n.length?(n[g]-r*p)/s:1/0,w=g>0?(n[g-1]-r*p)/s:-1/0;g<n.length&&h(g,p,1),g>0&&h(g-1,p,-1),Fa(l,1,La(w,v),f,m),u.push(Math.log(La(w,v)),xe(p,{log:!0}))}return l.value=ee(u),l}function tf(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(l=>e.gradient[l]),o=t.map(l=>t.map(i=>-e.hessian[l][i])),a;try{a=we(o,r)}catch{a=void 0}return(a===void 0||!a.every(Number.isFinite)||ee(a.map((l,i)=>l*r[i]))<=0)&&(a=r.map((l,i)=>l/Math.max(Math.abs(o[i][i]),1))),t.forEach((l,i)=>n[l]=a[i]),n}var _a=(e,t)=>e>=t-1e-12*Math.abs(t);function Oa(e,t,n){let r=[...t],o=e(r),a=n,l=r.map((i,s)=>s);for(let i=0;i<200;i++){let s=a?l.slice(1):l;if(s.length===0)break;let c=tf(o,s);if(!a&&Math.abs(r[0])===Ae&&c[0]*r[0]>0){a=!0;continue}let f=Math.abs(r[0]+c[0])>Ae?(Math.sign(c[0])*Ae-r[0])/c[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((p,g)=>p+f*c[g]);f*c[0]!==0&&Math.abs(h[0])>=Ae*(1-1e-15)&&(h[0]=Math.sign(h[0])*Ae);let d=e(h);if(_a(d.value,o.value)){let p=Math.max(...h.map((g,v)=>Math.abs(g-r[v])/Math.max(1,Math.abs(r[v]))));r=h,o=d,m=p>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:a}}function Va(e,t){let n=Oa(e,[Math.max(-Ae,Math.min(Ae,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Oa(e,[Math.sign(n.theta[0])*Ae,...n.theta.slice(1)],!0);return _a(r.at.value,n.at.value)?r:n}function Ca(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=we(e.map(o=>o.map(a=>-a)))}catch{return n()}return r.map((o,a)=>o.map((l,i)=>(l+r[i][a])/2))}var Da=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function nf(e,t,n){let r,o;if(Sa(t)){let R=e,C=t;if(R.length!==C.length)throw new RangeError("'x' and 'y' must have the same length");let F=R.map((T,O)=>!Number.isNaN(T)&&!Number.isNaN(C[O])),I=ce(R.filter((T,O)=>F[O])),V=ce(C.filter((T,O)=>F[O])),j=new Map(Eo(I).map((T,O)=>[T,O])),Y=new Map(Eo(V).map((T,O)=>[T,O]));r=Array.from({length:j.size},()=>new Array(Y.size).fill(0)),I.forEach((T,O)=>r[j.get(T)][Y.get(V[O])]++),o=n??{}}else{let R=e;if(!R.every(Sa))throw new TypeError("give a table (array of rows) or two vectors");let C=R[0]?.length??0;if(R.some(F=>F.length!==C))throw new TypeError("the table must have rows of equal length");r=R.map(F=>Array.from(F)),o=t??{}}let{ML:a=!1,stdErr:l=!1}=o,i=[],s=r.map(R=>ee(R)),c=s.filter(R=>R===0).length;c>0&&(i.push(Ta(c,"row")),r=r.filter((R,C)=>s[C]!==0));let f=(r[0]??[]).map((R,C)=>ee(r.map(F=>F[C]))),m=f.filter(R=>R===0).length;m>0&&(i.push(Ta(m,"column")),r=r.map(R=>R.filter((C,F)=>f[F]!==0)));let u=r.length,h=r[0]?.length??0;if(u<2&&i.push("the table has fewer than 2 rows"),h<2&&i.push("the table has fewer than 2 columns"),u<2||h<2)return{rho:NaN,warnings:i};let d=[-1/0,...xo(r.map(R=>ee(R))),1/0],p=[-1/0,...xo(r[0].map((R,C)=>ee(r.map(F=>F[C])))),1/0],v=Zo(R=>{let C=Math.max(-Ae,Math.min(Ae,R)),F=[];for(let I=0;I<u;I++)for(let V=0;V<h;V++){if(r[I][V]===0)continue;let j=Jt([d[I],p[V]],[d[I+1],p[V+1]],C);F.push(r[I][V]*Math.log(j))}return-ee(F)},-Ae,Ae).optimum;if(!a&&!l)return{rho:v,warnings:i};let w=R=>[-1/0,...R,1/0],b=d.slice(1,u),N=p.slice(1,h),M=R=>a?Ia(r,w(R.slice(1,u)),w(R.slice(u)),R[0],!0):Ia(r,d,p,R[0],!1),{theta:E,at:y,bounded:x}=Va(M,a?[v,...b,...N]:[v]),q=E[0];if(x&&i.push(Da(q)),!l)return{rho:q,warnings:i};let k=a?w(E.slice(1,u)):d,S=a?w(E.slice(u)):p,L=ee(r.map(R=>ee(R))),A=[];for(let R=0;R<u;R++)for(let C=0;C<h;C++){let F=r[R][C],I=Jt([k[R],S[C]],[k[R+1],S[C+1]],q);F===0?A.push(L*I):A.push(F*Math.log1p(1e-6/F),F*Yc(L*I/F-1))}return{rho:q,warnings:i,var:Ca(y.hessian,x),chisq:2*ee(A),df:u*h-u-h,n:L,rowCuts:k.slice(1,u),colCuts:S.slice(1,h)}}function rf(e,t,n,r){let o=e.length,a=[B(.25),B(.5),B(.75)],l=(h,d)=>d.filter(p=>p<h).length,i=qo(e).map(h=>l(h,n)),s=qo(t).map(h=>l(h,a)),c=Array.from({length:n.length+1},()=>new Array(4).fill(0));i.forEach((h,d)=>c[h][s[d]]++);let f=[-1/0,...n,1/0],m=[-1/0,...a,1/0],u=[];return c.forEach((h,d)=>h.forEach((p,g)=>{if(p===0)return;let v=Jt([f[d],m[g]],[f[d+1],m[g+1]],r);u.push(p*Math.log((p+1e-6)/(v*o)))})),2*ee(u)}function of(e,t,n={}){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,a=e.map((x,q)=>!Number.isNaN(x)&&!Number.isNaN(t[q])),l=e.filter((x,q)=>a[q]),i=ce(t.filter((x,q)=>a[q])),s=l.length,c=Eo(i);if(c.length<2)return{rho:NaN,warnings:["y has fewer than 2 levels"]};let f=new Map(c.map((x,q)=>[x,q+1])),m=i.map(x=>f.get(x)),u=c.map(()=>0);for(let x of m)u[x-1]++;let h=xo(u),d=[],p=Math.sqrt((s-1)/s)*Math.sqrt(re(m))*Yo(l,m)[0]/ee(h.map(x=>xe(x)));if(Math.abs(p)>Ae){let x=Math.sign(p)*Ae;d.push(`initial correlation inadmissible, ${St(p)}, set to ${St(x)}`),p=x}if(!r&&!o)return{rho:p,warnings:d};let g=qo(l),v=m.map(x=>x-1),w=x=>ef(g,v,r?x.slice(1):h,x[0],r),{theta:b,at:N,bounded:M}=Va(w,r?[p,...h]:[p]);if(M&&d.push(Da(b[0])),!o)return{rho:b[0],warnings:d};let E=r?b.slice(1):h,y=c.length;return{rho:b[0],warnings:d,var:Ca(N.hessian,M),chisq:rf(m,g,E,r?b[0]:p),df:4*y-y-1,n:s,cuts:E}}export{Yr as Anova,el as TukeyHSD,Fc as adTest,Kr as anova,qs as ansariTest,Pr as aov,ac as aovEz,Zr as aovSummary,ps as bartlettTest,Er as binomTest,zi as brunnerMunzelTest,ja as chisqTest,vi as cochranQTest,Js as cohenES,Bl as cohensD,Ki as confint,Pa as corTest,Ac as coxph,jc as cronbachAlpha,_c as cvmTest,pc as dscfAllPairsTest,vc as dunnTest,uc as dunnettTest,rc as epsilonSquared,Sl as esH,Ll as esW1,Il as esW2,tc as etaSquared,Uc as factanal,Xa as fisherTest,ws as flignerTest,Rr as friedmanTest,dc as gamesHowellTest,Nc as glhtDunnett,fl as glm,bl as glmAnova,gl as glmConfint,pl as glmCooksDistance,Hr as glmHatvalues,dl as glmPredict,Ur as glmResiduals,hl as glmRstandard,Gr as glmSummary,Xl as hedgesG,zc as icc,Oc as jonckheereTest,$c as kappa2,Pc as kappamFleiss,zn as kruskalTest,Ls as ksTest,fc as kwAllPairsDunnTest,mc as kwManyOneDunnTest,Rc as leveneTest,Vc as lillieTest,$r as lm,Ii as mantelhaenTest,hc as manyOneUTest,ql as mauchlyTest,gi as mcnemarTest,It as modelMatrix,vs as moodTest,Zc as omega,nc as omegaSquared,ys as onewayTest,Ft as pAdjust,Hi as pairwisePropTest,Gi as pairwiseTTest,Ui as pairwiseWilcoxTest,Wc as partialR,Dc as pearsonTest,di as poissonTest,nf as polychor,of as polyserial,Rl as powerAnovaTest,Al as powerPropTest,kl as powerTTest,Zi as predict,qr as propTest,Tc as propTrendTest,Pl as pwr2p2nTest,Cl as pwr2pTest,Wl as pwrAnovaTest,jl as pwrChisqTest,Gl as pwrF2Test,$l as pwrNormTest,Dl as pwrPTest,zl as pwrRTest,Vl as pwrT2nTest,_l as pwrTTest,gs as quadeTest,Cc as sfTest,Ti as shapiroTest,wc as steelTest,Mc as survdiff,xc as survfit,Ot as tTest,gc as tukeyTest,hs as varTest,on as wilcoxTest,tl as zTest};