@numero-uno-jpn/rstats 1.1.0 → 1.3.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 (97) hide show
  1. package/CHANGELOG.md +142 -0
  2. package/README.ja.md +33 -15
  3. package/README.md +32 -14
  4. package/dist/chunks/{chunk-4KCSBTR5.js → chunk-2VZW4XPK.js} +2 -2
  5. package/dist/chunks/chunk-4OOTEXZU.js +2 -0
  6. package/dist/chunks/{chunk-Z2HGLFZN.js → chunk-6TZVS6WC.js} +2 -2
  7. package/dist/chunks/{chunk-U7BRXOUS.js → chunk-6X2ZMPF5.js} +2 -2
  8. package/dist/chunks/{chunk-GJEKNJP7.js → chunk-AJZV2SVY.js} +2 -2
  9. package/dist/chunks/{chunk-GRUQRNX7.js → chunk-BNTBZ5W3.js} +3 -3
  10. package/dist/chunks/{chunk-DR4GQ7PW.js → chunk-EBRTF7RM.js} +1 -1
  11. package/dist/chunks/{chunk-OG7UORIX.js → chunk-FFNHG5SB.js} +1 -1
  12. package/dist/chunks/{chunk-N5BDSMXS.js → chunk-GUM57VVM.js} +1 -1
  13. package/dist/chunks/{chunk-GGJA3JRO.js → chunk-HBRSUHSU.js} +2 -2
  14. package/dist/chunks/{chunk-HRA7PCQT.js → chunk-I5NWGQOQ.js} +2 -2
  15. package/dist/chunks/chunk-IJV43Y7Q.js +2 -0
  16. package/dist/chunks/{chunk-YN5MLKDL.js → chunk-IOGQEMDE.js} +2 -2
  17. package/dist/chunks/{chunk-GYGQUYEP.js → chunk-JJRZ2TTF.js} +2 -2
  18. package/dist/chunks/{chunk-X4J6UJHN.js → chunk-LA4H6ZSC.js} +2 -2
  19. package/dist/chunks/{chunk-DTJG2IL2.js → chunk-LMNU4NHZ.js} +2 -2
  20. package/dist/chunks/{chunk-4VRMAYPY.js → chunk-LNUNKFIH.js} +3 -3
  21. package/dist/chunks/chunk-MJXETGT2.js +2 -0
  22. package/dist/chunks/chunk-P7ZHH2SF.js +2 -0
  23. package/dist/chunks/{chunk-HUWOSLKE.js → chunk-PDHBRDTF.js} +2 -2
  24. package/dist/chunks/{chunk-7KJ24LYK.js → chunk-RCGW2DCK.js} +2 -2
  25. package/dist/chunks/{chunk-QUTJEYDX.js → chunk-TZTVDVP4.js} +2 -2
  26. package/dist/chunks/{chunk-HZLSNJQM.js → chunk-UJYRU37Z.js} +2 -2
  27. package/dist/chunks/{chunk-EJYCDE4Z.js → chunk-UMDXXT3M.js} +1 -1
  28. package/dist/chunks/{chunk-7IJCUSA5.js → chunk-UOAFEB64.js} +2 -2
  29. package/dist/chunks/{chunk-JNWWLNE3.js → chunk-Z74ZOTUN.js} +2 -2
  30. package/dist/descriptive.js +2 -2
  31. package/dist/distributions.js +2 -2
  32. package/dist/entries/tests.d.ts +14 -0
  33. package/dist/entries/tests.js +14 -0
  34. package/dist/htest/agreement.d.ts +2 -2
  35. package/dist/htest/binomCI.d.ts +116 -0
  36. package/dist/htest/binomCI.js +252 -0
  37. package/dist/htest/blandAltman.d.ts +100 -0
  38. package/dist/htest/blandAltman.js +159 -0
  39. package/dist/htest/boschlooTest.d.ts +65 -0
  40. package/dist/htest/boschlooTest.js +394 -0
  41. package/dist/htest/chisqTest.d.ts +19 -0
  42. package/dist/htest/chisqTest.js +30 -2
  43. package/dist/htest/core/twoProportions.d.ts +48 -0
  44. package/dist/htest/core/twoProportions.js +476 -0
  45. package/dist/htest/diagnosticTests.d.ts +91 -0
  46. package/dist/htest/diagnosticTests.js +116 -0
  47. package/dist/htest/equivalence.d.ts +172 -0
  48. package/dist/htest/equivalence.js +189 -0
  49. package/dist/htest/fisherTest.d.ts +49 -0
  50. package/dist/htest/fisherTest.js +136 -13
  51. package/dist/htest/iccPsych.d.ts +106 -0
  52. package/dist/htest/iccPsych.js +332 -0
  53. package/dist/htest/ksTest.d.ts +1 -1
  54. package/dist/htest/mcreg.d.ts +209 -0
  55. package/dist/htest/mcreg.js +735 -0
  56. package/dist/htest/roc.d.ts +287 -0
  57. package/dist/htest/roc.js +635 -0
  58. package/dist/htest/rocTest.d.ts +139 -0
  59. package/dist/htest/rocTest.js +444 -0
  60. package/dist/htest/tTOST.d.ts +202 -0
  61. package/dist/htest/tTOST.js +558 -0
  62. package/dist/htest/tableTests.d.ts +216 -0
  63. package/dist/htest/tableTests.js +650 -0
  64. package/dist/htest/trendTests.d.ts +1 -1
  65. package/dist/htest/trendTests.js +1 -1
  66. package/dist/htest/twoByTwo.d.ts +203 -0
  67. package/dist/htest/twoByTwo.js +385 -0
  68. package/dist/internal/summary.js +5 -0
  69. package/dist/linalg/qr.d.ts +3 -0
  70. package/dist/linalg/qr.js +3 -0
  71. package/dist/linalg.js +2 -2
  72. package/dist/mixed.js +2 -2
  73. package/dist/models/coeftest.d.ts +139 -0
  74. package/dist/models/coeftest.js +237 -0
  75. package/dist/models/coxph.d.ts +60 -9
  76. package/dist/models/coxph.js +367 -38
  77. package/dist/models/glm.d.ts +11 -1
  78. package/dist/models/glm.js +19 -2
  79. package/dist/models/inputRows.d.ts +10 -0
  80. package/dist/models/inputRows.js +23 -0
  81. package/dist/models/lm.d.ts +8 -0
  82. package/dist/models/lm.js +14 -2
  83. package/dist/models/sandwich.d.ts +177 -0
  84. package/dist/models/sandwich.js +923 -0
  85. package/dist/numerics.js +2 -2
  86. package/dist/random/rng.d.ts +8 -1
  87. package/dist/random/rng.js +14 -1
  88. package/dist/rstats.js +16 -16
  89. package/dist/special/igam.js +140 -23
  90. package/dist/special/loader.js +8 -0
  91. package/dist/special.js +2 -2
  92. package/dist/tests.js +13 -13
  93. package/llms.txt +25 -0
  94. package/package.json +1 -1
  95. package/dist/chunks/chunk-NDTJ23A6.js +0 -2
  96. package/dist/chunks/chunk-UMLMY4XS.js +0 -2
  97. package/dist/chunks/chunk-Y4YWLSWU.js +0 -2
package/dist/linalg.js CHANGED
@@ -1,2 +1,2 @@
1
- /*! rStats.js 1.1.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
- import{F as B,h as lt,i as ut}from"./chunks/chunk-UMLMY4XS.js";import{b as et,c as ot}from"./chunks/chunk-7IJCUSA5.js";import{a as Qt,b as Xt}from"./chunks/chunk-YN5MLKDL.js";import{A as Lt,B as Ut,C as Yt,E as Ht,G as Y,H as Vt,J as ct,K as ft,a as A,b as at,c as O,d as _,e as N,g as U,i as st,j as Rt,k as St,l as Ft,m as Ot,n as It,o as _t,p as Tt,q as Pt,r as jt,s as $t,t as j,v as Dt,w as $,x as it,y as X,z as Kt}from"./chunks/chunk-GYGQUYEP.js";import"./chunks/chunk-GJEKNJP7.js";import"./chunks/chunk-EJYCDE4Z.js";import{a as L}from"./chunks/chunk-OG7UORIX.js";import{l as q,p as T,q as P,u as k}from"./chunks/chunk-HUWOSLKE.js";var Bt=["k","upperTri","transpose"],Ct=["k","upperTri","transpose"];function zt(n,r){let t=_(n),e=r===void 0?t:_(r);if(t.nrow!==e.nrow)throw new TypeError(`non-conformable arguments (${t.nrow} and ${e.nrow} rows)`);let l=t.nrow,i=A(t.ncol,e.ncol),f=dt(t),a=r===void 0?f:dt(e);for(let c=0;c<e.ncol;c++)for(let s=0;s<t.ncol;s++){if(r===void 0&&s>c)continue;let o=0;for(let u=0;u<l;u++)o+=t.data[u+s*l]*e.data[u+c*l];i.data[s+c*t.ncol]=T(o,f[s]*a[c])?P(mt(t,s),mt(e,c),o):o}return r===void 0&&gt(i),N(i)}function Gt(n,r){let t=_(n),e=r===void 0?t:_(r);if(t.ncol!==e.ncol)throw new TypeError(`non-conformable arguments (${t.ncol} and ${e.ncol} columns)`);let l=A(t.nrow,e.nrow),i=pt(t),f=r===void 0?i:pt(e);for(let a=0;a<e.nrow;a++)for(let c=0;c<t.nrow;c++){if(r===void 0&&c>a)continue;let s=0;for(let o=0;o<t.ncol;o++)s+=t.data[c+o*t.nrow]*e.data[a+o*e.nrow];l.data[c+a*t.nrow]=T(s,i[c]*f[a])?P(ht(t,c),ht(e,a),s):s}return r===void 0&&gt(l),N(l)}function mt(n,r){return Array.from(n.data.subarray(r*n.nrow,(r+1)*n.nrow))}function ht(n,r){return Array.from({length:n.ncol},(t,e)=>n.data[r+e*n.nrow])}function gt(n){let r=n.nrow;for(let t=0;t<r;t++)for(let e=t+1;e<r;e++)n.data[e+t*r]=n.data[t+e*r]}function Wt(n,r,t={}){return q(t,Bt,"backsolve"),wt(n,r,t.k,t.upperTri??!0,t.transpose??!1)}function Jt(n,r,t={}){return q(t,Ct,"forwardsolve"),wt(n,r,t.k,t.upperTri??!1,t.transpose??!1)}function wt(n,r,t,e,l){let i=O(n),f=_(r),a=t??i.ncol;if(!Number.isInteger(a)||a<1||a>i.nrow||a>i.ncol||a>f.nrow)throw new RangeError(`invalid k (got ${a})`);for(let s=0;s<a;s++)if(i.data[s+s*i.nrow]===0)throw new Error(`singular matrix: diagonal element ${s+1} is zero`);let c=A(a,f.ncol);for(let s=0;s<f.ncol;s++)for(let o=0;o<a;o++)c.data[o+s*a]=f.data[o+s*f.nrow];return Zt(i,a,c,e,l),at(r)?N(c):Array.from(c.data)}function Zt(n,r,t,e,l){let i=n.nrow,f=n.data,a=t.nrow,c=t.data;for(let s=0;s<t.ncol;s++){let o=s*a;if(!l&&e){for(let u=r-1;u>=0;u--)if(c[o+u]!==0){c[o+u]/=f[u+u*i];let h=c[o+u];for(let p=0;p<u;p++)f[p+u*i]!==0&&(c[o+p]-=h*f[p+u*i])}}else if(l)if(e)for(let u=0;u<r;u++){let h=c[o+u];for(let p=0;p<u;p++)f[p+u*i]!==0&&(h-=f[p+u*i]*c[o+p]);c[o+u]=h/f[u+u*i]}else for(let u=r-1;u>=0;u--){let h=c[o+u];for(let p=u+1;p<r;p++)f[p+u*i]!==0&&(h-=f[p+u*i]*c[o+p]);c[o+u]=h/f[u+u*i]}else for(let u=0;u<r;u++)if(c[o+u]!==0){c[o+u]/=f[u+u*i];let h=c[o+u];for(let p=u+1;p<r;p++)f[p+u*i]!==0&&(c[o+p]-=h*f[p+u*i])}}}function dt(n){let r=[];for(let t=0;t<n.ncol;t++){let e=1/0;for(let l=0;l<n.nrow;l++){let i=Math.abs(n.data[l+t*n.nrow]);i!==0&&i<e&&(e=i)}r.push(e)}return r}function pt(n){let r=new Array(n.nrow).fill(1/0);for(let t=0;t<n.ncol;t++)for(let e=0;e<n.nrow;e++){let l=Math.abs(n.data[e+t*n.nrow]);l!==0&&l<r[e]&&(r[e]=l)}return r}function tn(n){let r=O(n);U(r);let t=r.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let m of r.data)if(!Number.isFinite(m))throw new RangeError("the matrix contains NaN or infinite values");let e=st(r.data),l={nrow:t,ncol:t,data:e===0?r.data.slice():r.data.map(m=>k(m,-e))},i=A(t,t),f=new Float64Array(t),a=new Float64Array(t);ct(l,i),ft(l,i,f,a,!0);let c=l.data,s=i.data,o=(m,d)=>m+d*t,u=new Uint8Array(t);for(let m=0;m+1<t;m++)a[m]>0&&a[m+1]<0&&(u[m++]=1);for(let m=0;m<t;m++)for(let d=m+1;d<t;d++)d===m+1&&u[m]||(c[o(d,m)]=0);let h=new Array(t),p=new Array(t);for(let m=0;m<t;m++){if(!u[m]){h[m]=c[o(m,m)],p[m]=0;continue}let d=nn(c[o(m,m)],c[o(m,m+1)],c[o(m+1,m)],c[o(m+1,m+1)]);c[o(m,m)]=d.a,c[o(m,m+1)]=d.b,c[o(m+1,m)]=d.c,c[o(m+1,m+1)]=d.d;let{cs:b,sn:M}=d;for(let w=m+2;w<t;w++){let y=c[o(m,w)],E=c[o(m+1,w)];c[o(m,w)]=b*y+M*E,c[o(m+1,w)]=b*E-M*y}for(let w=0;w<m;w++){let y=c[o(w,m)],E=c[o(w,m+1)];c[o(w,m)]=b*y+M*E,c[o(w,m+1)]=b*E-M*y}for(let w=0;w<t;w++){let y=s[o(w,m)],E=s[o(w,m+1)];s[o(w,m)]=b*y+M*E,s[o(w,m+1)]=b*E-M*y}h[m]=d.rt1r,p[m]=d.rt1i,h[m+1]=d.rt2r,p[m+1]=d.rt2i,m++}if(e===0)return{T:N(l),Q:N(i),valuesRe:h,valuesIm:p};let g=m=>k(m,e);return{T:N(l).map(m=>m.map(g)),Q:N(i),valuesRe:h.map(g),valuesIm:p.map(g)}}var F=(n,r)=>r>=0&&!Object.is(r,-0)?Math.abs(n):-Math.abs(n);function nn(n,r,t,e){let l=22250738585072014e-324,i=Number.EPSILON,f=4,a=2**Math.trunc(Math.log(l/i)/Math.log(2)/2),c=1/a,s,o;if(t===0)s=1,o=0;else if(r===0){s=0,o=1;let h=e;e=n,n=h,r=-t,t=0}else if(n-e===0&&F(1,r)!==F(1,t))s=1,o=0;else{let h=n-e,p=.5*h,g=Math.max(Math.abs(r),Math.abs(t)),m=Math.min(Math.abs(r),Math.abs(t))*F(1,r)*F(1,t),d=Math.max(Math.abs(p),g),b=p/d*p+g/d*m;if(b>=f*i){b=p+F(Math.sqrt(d)*Math.sqrt(b),p),n=e+b,e=e-g/b*m;let M=Math.hypot(t,b);s=b/M,o=t/M,r=r-t,t=0}else{let M=0,w=r+t;for(;M++,d=Math.max(Math.abs(h),Math.abs(w)),!(!(d>=c&&(w*=a,h*=a,M<=20))&&!(d<=a&&(w*=c,h*=c,M<=20))););p=.5*h;let y=Math.hypot(w,h);s=Math.sqrt(.5*(1+Math.abs(w)/y)),o=-(p/(y*s))*F(1,w);let E=n*s+r*o,I=-n*o+r*s,W=t*s+e*o,J=-t*o+e*s;if(n=E*s+W*o,r=I*s+J*o,t=-E*o+W*s,e=-I*o+J*s,h=.5*(n+e),n=h,e=h,t!==0)if(r!==0){if(F(1,r)===F(1,t)){let Z=Math.sqrt(Math.abs(r)),tt=Math.sqrt(Math.abs(t));p=F(Z*tt,t),y=1/Math.sqrt(Math.abs(r+t)),n=h+p,e=h-p,r=r-t,t=0;let nt=Z*y,rt=tt*y;h=s*nt-o*rt,o=s*rt+o*nt,s=h}}else r=-t,t=0,h=s,s=-o,o=h}}let u=t===0?0:Math.sqrt(Math.abs(r))*Math.sqrt(Math.abs(t));return{a:n,b:r,c:t,d:e,cs:s,sn:o,rt1r:n,rt1i:u,rt2r:e,rt2i:-u}}var rn=["method","order"],en=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function on(n,r={}){q(r,rn,"expm");let t=r.method??"Higham08.b";if(!en.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let e=r.order??8,l=t==="Pade"||t==="PadeO"||t==="Taylor"||t==="TaylorO";if(l&&!(Number.isInteger(e)&&e>=1&&e<=2147483647))throw new RangeError("order must be a positive integer at most 2147483647");let i=O(n);U(i);let f=i.nrow;if(f===0)return[];if(f===1)return[[Math.exp(i.data[0])]];if(l&&e*(f**3+64)>2**31)throw new RangeError(`order = ${e} is too large for a ${f} x ${f} matrix: order * (n^3 + 64) must be at most 2^31`);for(let h of i.data)if(!Number.isFinite(h))throw new RangeError("the matrix contains NaN or infinite values");if(t==="Taylor"||t==="TaylorO")return N(un(i,e));if(t==="Pade"||t==="PadeO")return N(mn(i,e));let a,c;t==="Higham08.b"?(a=Y("P",i),c=Y("S",i)):fn(i)&&(c=Y("S",i));let s=ln(i),o=s.data,u=(h,p)=>h+p*f;if(c){let h=c.scale;for(let p=0;p<f;p++)for(let g=0;g<f;g++)o[u(g,p)]=o[u(g,p)]*h[g]/h[p]}if(a){let h=a.scale,p=g=>{let m=h[g];if(m!==g){for(let d=0;d<f;d++)[o[u(d,g)],o[u(d,m)]]=[o[u(d,m)],o[u(d,g)]];for(let d=0;d<f;d++)[o[u(g,d)],o[u(m,d)]]=[o[u(m,d)],o[u(g,d)]]}};for(let g=a.ilo-1;g>=0;g--)p(g);for(let g=a.ihi+1;g<f;g++)p(g)}return N(s)}var an={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},sn=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function cn(n,r){if(r<1/0)return Math.log2(r);let t=0;for(let l of n.data)t=Math.max(t,Math.abs(l));let e=Math.floor(Math.log2(t));return Math.log2($({...n,data:n.data.map(l=>k(l,-e))}))+e}function bt(n,r){return Math.max(0,Math.ceil(cn(n,r)-Math.log2(5.4)))}function fn(n){let r=$(n);if(r<=2.1)return!1;let t=bt(n,r);return t>0&&n.data.some(e=>e!==0&&Math.abs(k(e,-t))<2**-1022)}function ln(n){let r=n.nrow,t=$(n),e=C(r),l,i,f=0;if(t<=2.1){let o=t<=.015?3:t<=.25?5:t<=.95?7:9,u=an[o],h=R(n,n),p=H([[u[1],e]]),g=H([[u[0],e]]),m=h;for(let d=1;d<=(o-1)/2;d++)d>1&&(m=R(m,h)),D(p,u[2*d+1],m),D(g,u[2*d],m);l=R(n,p),i=g}else{f=bt(n,t);let o={...n,data:n.data.map(M=>k(M,-f))},u=R(o,o),h=R(u,u),p=R(u,h),g=sn,m=H([[g[13],p],[g[11],h],[g[9],u]]),d=R(p,m);for(let[M,w]of[[7,p],[5,h],[3,u],[1,e]])D(d,g[M],w);l=R(o,d);let b=H([[g[12],p],[g[10],h],[g[8],u]]);i=R(p,b);for(let[M,w]of[[6,p],[4,h],[2,u],[0,e]])D(i,g[M],w)}let a=A(r,r),c=A(r,r);for(let o=0;o<r*r;o++)a.data[o]=i.data[o]-l.data[o],c.data[o]=i.data[o]+l.data[o];let s=O(X(N(a),N(c)));for(let o=0;o<f;o++)s=Nt(s,s);return s}function yt(n){let r=$(n),t=it(n),e=r*t<1/0?Math.sqrt(r*t):Math.sqrt(r)*Math.sqrt(t),l=1;for(;2**l<e;)l++;let i=l+4;if(!n.data.some(a=>a!==0&&Math.abs(k(a,-i))<2**-1022))return i;if(e===0||r*t<2**-1e3)return Math.max(0,Math.ceil((Math.log2(r)+Math.log2(t))/2)+4);let f=l;for(;f>-4&&2**(f-1)>=e;)f--;return Math.max(0,f+4)}function vt(n,r){let t=Math.max(0,r-1e3);return{A:t===0?n:K(n,2**-t),N:2**(r-t)}}function Et(n,r){for(let t=0;t<r;t++)n=Nt(n,n);return n}function kt(n){let r=n.nrow;for(let t=0;t<r;t++)n.data[t+t*r]=n.data[t+t*r]+1}function un(n,r){let t=yt(n),{A:e,N:l}=vt(n,t),i=C(n.nrow);for(let f=r;f>=1;f--)i=K(R(i,e),1/(f*l)),kt(i);return Et(i,t)}function Mt(n,r,t,e){let l=e===1?n:K(n,-1),i=C(n.nrow);for(let f=r;f>=1;f--){let a=(r-f+1)/(f*(2*r-f+1)*t);i=K(R(i,l),a),kt(i)}return i}function mn(n,r){let t=n.nrow,e=yt(n),{A:l,N:i}=vt(n,e),f=Mt(l,r,i,1),a=Mt(l,r,i,-1),c=A(t,t);for(let s=0;s<t;s++)c.data.set(hn(a,f.data.subarray(s*t,(s+1)*t)),s*t);return Et(c,e)}function hn(n,r){let t=n.nrow,e=1e-100,l=g=>{let m=new Float64Array(t);for(let d=0;d<t;d++){let b=0;for(let M=0;M<t;M++)b+=n.data[d+M*t]*g[M];m[d]=b}return m},i=(g,m)=>{let d=0;for(let b=0;b<t;b++)d+=g[b]*m[b];return d},f=(g,m,d)=>{let b=new Float64Array(t);for(let M=0;M<t;M++)b[M]=g[M]+m*d[M];return b},a=new Float64Array(t),c=Float64Array.from(r),s=Float64Array.from(r),o=Float64Array.from(r),u=Float64Array.from(r),h=Math.sqrt(i(c,c)),p=i(u,r);if(Math.abs(p)<=e)return a;for(let g=0;g<t;g++){let m=l(s),d=i(u,m);if(Math.abs(d)<=e)return a;let b=p/d;if(Math.abs(b)<=e)return a;let M=f(o,-b,m);m=f(o,1,M);let w=l(m);c=f(c,-b,w);let y=Math.sqrt(i(c,c));if(a=f(a,b,m),Math.abs(y/h)<=1e-30)return a;let E=i(u,c);if(Math.abs(E)<=e)return a;let I=E/p;p=E,o=f(c,I,M),w=f(M,I,s),s=f(o,I,w)}return a}function C(n){let r=A(n,n);for(let t=0;t<n;t++)r.data[t+t*n]=1;return r}function Nt(n,r){let t=n.nrow,e=n.ncol,l=r.ncol,i=A(t,l);for(let f=0;f<l;f++)for(let a=0;a<t;a++){let c=0;for(let s=0;s<e;s++){let o=n.data[a+s*t],u=r.data[s+f*e];o===0&&Math.abs(u)===1/0||u===0&&Math.abs(o)===1/0||(c+=o*u)}i.data[a+f*t]=c}return i}function R(n,r){let t=n.nrow,e=n.ncol,l=r.ncol,i=A(t,l);for(let f=0;f<l;f++)for(let a=0;a<t;a++){let c=0;for(let s=0;s<e;s++)c+=n.data[a+s*t]*r.data[s+f*e];i.data[a+f*t]=c}return i}function K(n,r){return{nrow:n.nrow,ncol:n.ncol,data:n.data.map(t=>t*r)}}function D(n,r,t){for(let e=0;e<n.data.length;e++)n.data[e]=n.data[e]+r*t.data[e]}function H(n){let[r,t]=n[0],e=K(t,r);for(let l=1;l<n.length;l++)D(e,n[l][0],n[l][1]);return e}var dn=["fun"],pn=["nrow","ncol"],gn=["nrow","ncol","byrow"],wn=["by","lengthOut"];function x(n){return typeof n!="number"&&n.length>0&&Array.isArray(n[0])}function v(n){return typeof n=="number"?[n]:Array.from(n)}function S(n){let r=n[0].length;if(n.some(t=>t.length!==r))throw new TypeError("all rows of the matrix must have the same length");return r}function z(n,r){let t,e;if(x(n)&&x(r))t=n.map(a=>[...a]),e=r.map(a=>[...a]);else if(x(n)){let a=v(r),c=S(n);t=n.map(s=>[...s]),e=c===a.length?a.map(s=>[s]):c===1?[a]:[]}else if(x(r)){let a=v(n);e=r.map(c=>[...c]),t=r.length===a.length?[a]:r.length===1?a.map(c=>[c]):[]}else{let a=v(n),c=v(r);if(a.length===c.length)t=[a],e=c.map(s=>[s]);else if(a.length===1)t=[a],e=[c];else if(c.length===1)t=a.map(s=>[s]),e=[c];else throw new RangeError("non-conformable arguments")}let l=t[0]?.length??0;if(t.length===0||e.length!==l)throw new RangeError("non-conformable arguments");S(t);let i=e.length>0?S(e):1,f=new Array(i).fill(1/0);for(let a of e)for(let c=0;c<i;c++){let s=Math.abs(a[c]);s!==0&&s<f[c]&&(f[c]=s)}return t.map(a=>{let c=1/0;for(let o of a){let u=Math.abs(o);u!==0&&u<c&&(c=u)}let s=new Array(i).fill(0);for(let o=0;o<i;o++){let u=0;for(let h=0;h<l;h++)u+=a[h]*e[h][o];s[o]=T(u,c*f[o])?P(a,e.map(h=>h[o]),u):u}return s})}function Q(n,r,t){let e=x(n),l=x(r);if(e&&l){let c=S(n);if(n.length!==r.length||c!==S(r))throw new RangeError("non-conformable arrays");return n.map((s,o)=>s.map((u,h)=>t(u,r[o][h])))}if(e||l){let c=e?n:r,s=v(e?r:n),o=c.length,u=S(c);if(s.length===0)return c.map(h=>h.map(()=>NaN));if(s.length>o*u)throw new RangeError("dims [product "+o*u+"] do not match the length of object ["+s.length+"]");return c.map((h,p)=>h.map((g,m)=>e?t(g,s[(p+m*o)%s.length]):t(s[(p+m*o)%s.length],g)))}let i=v(n),f=v(r);if(i.length===0||f.length===0)return[];let a=Math.max(i.length,f.length);return Array.from({length:a},(c,s)=>t(i[s%i.length],f[s%f.length]))}function Mn(n,r){return Q(n,r,(t,e)=>t+e)}function bn(n,r){return Q(n,r,(t,e)=>t-e)}function yn(n,r){return Q(n,r,(t,e)=>t*e)}function vn(n,r){return Q(n,r,(t,e)=>t/e)}var En={"*":(n,r)=>n*r,"+":(n,r)=>n+r,"-":(n,r)=>n-r,"/":(n,r)=>n/r,"^":(n,r)=>n**r};function kn(n,r,...t){q(ot(t),dn,"outer");let e=et(["fun"],t).fun??"*",l=typeof e=="function"?e:En[e];if(l===void 0)throw new RangeError(`invalid fun "${String(e)}"`);let i=Array.from(n),f=Array.from(r);return i.map(a=>f.map(c=>l(a,c)))}function G(n){if(!x(n))return[v(n)];let r=S(n);return Array.from({length:r},(t,e)=>n.map(l=>l[e]))}function Nn(n,r={}){if(q(r,pn,"diag"),x(n)){let a=S(n);return Array.from({length:Math.min(n.length,a)},(c,s)=>n[s][s])}let t=v(n),e,l;if(t.length===1&&r.nrow===void 0&&r.ncol===void 0){if(l=Math.trunc(t[0]),!(l>=0))throw new RangeError("invalid 'nrow' value (< 0)");e=[1]}else e=t,l=V(r.nrow,"nrow")??t.length;let i=V(r.ncol,"ncol")??l;if(e.length===0&&Math.min(l,i)>0)throw new RangeError("'x' must have positive length");let f=Array.from({length:l},()=>new Array(i).fill(0));for(let a=0;a<Math.min(l,i);a++)f[a][a]=e[a%e.length];return l===0?[]:f}function At(n,r){let t=a=>x(a)||v(a).length>0,e=n.some(t)?n.filter(t):n,l=e.map(a=>x(a)?r?G(a):a.map(c=>[...c]):[v(a)]),i=e.filter(x),f=i.length>0?r?i[0].length:S(i[0]):L(l.map(a=>a[0].length));return i.forEach((a,c)=>{if((r?a.length:S(a))!==f)throw new RangeError(`number of ${r?"rows":"columns"} of matrices must match (see arg ${c+2})`)}),l.flatMap(a=>a.map(c=>Array.from({length:f},(s,o)=>c[o%c.length])))}function xn(n){return typeof n=="object"&&!Array.isArray(n)&&!ArrayBuffer.isView(n)}function qt(n){let r=n.filter(xn);if(r.length!==0){if(r.length<n.length)throw new TypeError("cannot combine data frames with vectors or matrices");return r}}function An(...n){let r=qt(n);return r===void 0?G(At(n,!0)):ut(r)}function qn(...n){let r=qt(n);return r===void 0?At(n,!1):lt(r)}function V(n,r){if(n!==void 0){if(!Number.isFinite(n)||n<=-1)throw new RangeError(`invalid '${r}' value`);return Math.trunc(n)+0}}function Rn(n,r={}){q(r,gn,"matrix");let t=v(n),e=t.length,l=V(r.nrow,"nrow"),i=V(r.ncol,"ncol");l===void 0&&i===void 0?(l=e,i=1):l===void 0?l=Math.ceil(e/i):i===void 0&&(i=Math.ceil(e/l));let f=r.byrow??!1;return Array.from({length:l},(a,c)=>Array.from({length:i},(s,o)=>e===0?NaN:t[(f?c*i+o:c+o*l)%e]))}function Sn(n,r){if(!x(n)&&!x(r)){let i=v(n),f=v(r);return i.flatMap(a=>f.map(c=>a*c))}let t=x(n)?n:v(n).map(i=>[i]),e=x(r)?r:v(r).map(i=>[i]),l=[];for(let i of t)for(let f of e)l.push(i.flatMap(a=>f.map(c=>a*c)));return l}function Fn(n,r,t={}){q(t,wn,"seq");let{by:e,lengthOut:l}=t;if(r===void 0&&e===void 0&&l===void 0&&n!==void 0){if(!Number.isFinite(n))throw new RangeError("'from' must be a finite number");return xt(1,n)}if(n!==void 0&&!Number.isFinite(n))throw new RangeError("'from' must be a finite number");if(r!==void 0&&!Number.isFinite(r))throw new RangeError("'to' must be a finite number");if(l===void 0){if(e===void 0)return xt(n??1,r??1);let f=n??1,a=r??1,c=!Number.isFinite(a-f),s=c?.5:1,o=a*s-f*s;if(o===0&&a===0)return[a];let u=o/(e*s);if(!Number.isFinite(u)){if(e===0&&o===0)return[f];throw new RangeError("invalid '(to - from)/by'")}if(u<0||!Number.isFinite(e)&&o!==0&&o>0!=e>0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(o)/Math.max(Math.abs(a*s),Math.abs(f*s))<100*Number.EPSILON)return[f];let h=Math.trunc(u+1e-10),p=c?g=>2*(f/2+g*(e/2)):g=>f+g*e;return Array.from({length:h+1},(g,m)=>m===0?f:e>0?Math.min(p(m),a):Math.max(p(m),a))}if(!Number.isFinite(l)||l<=-1)throw new RangeError("'length.out' must be a non-negative number");let i=Math.ceil(l);if(i===0)return[];if(e===void 0){let f=r??(n??1)+(i-1),a=n??f-(i-1);if(i<=2)return[a,f].slice(0,i);if(a===f)return new Array(i).fill(a);if(!Number.isFinite(f-a)){let s=a/2,o=(f/2-s)/(i-1);return Array.from({length:i},(u,h)=>h===0?a:h===i-1?f:2*(s+h*o))}let c=(f-a)/(i-1);return Array.from({length:i},(s,o)=>o===0?a:o===i-1?f:a+o*c)}if(r===void 0){let f=n??1;return Array.from({length:i},(a,c)=>f+c*e)}if(n===void 0)return Array.from({length:i},(f,a)=>r-(i-1-a)*e);throw new RangeError("too many arguments")}function xt(n,r){let t=Math.trunc(Math.abs(r-n)+1+11920928955078125e-23),e=n<=r?1:-1;return Array.from({length:t},(l,i)=>n+e*i)}var On=["center","scale","rank","tol"],In=22250738585072014e-324;function _n(n){let r=n.length,t=r===0?0:n[0].length,e=n.map(s=>{if(s.length!==t)throw new TypeError("all rows of the matrix must have the same length");if(!s.every(Number.isFinite))throw new RangeError("the matrix contains NaN or infinite values");return Array.from(s)}),l=Array.from({length:r},(s,o)=>o),i=Math.min(r,t);for(let s=0;s<i;s++){let o=s,u=Math.abs(e[s][s]);for(let g=s+1;g<r;g++){let m=Math.abs(e[g][s]);(m>u||Number.isNaN(u))&&(u=m,o=g)}o!==s&&([e[o],e[s]]=[e[s],e[o]],[l[o],l[s]]=[l[s],l[o]]);let h=e[s][s];if(h===0)continue;let p=Math.abs(h)>=In?1/h:NaN;for(let g=s+1;g<r;g++){let m=Number.isNaN(p)?e[g][s]/h:e[g][s]*p;e[g][s]=m;for(let d=s+1;d<t;d++)e[g][d]=e[g][d]-m*e[s][d]}}let f=Array.from({length:r},(s,o)=>Array.from({length:i},(u,h)=>o===h?1:o>h?e[o][h]:0)),a=Array.from({length:i},(s,o)=>Array.from({length:t},(u,h)=>h>=o?e[o][h]:0)),c=Array.from({length:r},()=>new Array(r).fill(0));return l.forEach((s,o)=>c[s][o]=1),{P:c,L:f,U:a}}function Tn(n,r={}){q(r,On,"prcomp");let{rank:t,tol:e}=r;if(t!==void 0){if(!Number.isFinite(t))throw new RangeError("is.finite(rank.) is not TRUE");if(!(Math.trunc(t)>0&&Math.abs(t)<2**31))throw new RangeError("as.integer(rank.) > 0 is not TRUE")}let l=n.map(d=>Array.from(d)),i=r.center??!0,f=r.scale??!1,a=B(l,{center:i,scale:f});if(a.scale?.some(d=>d===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let c=l.length,s=d=>d.every(b=>b.every(Number.isFinite)),o=s(l)&&s(a.scaled)?j(a.scaled,{nu:0}):void 0,u=0;if(s(l)&&(o===void 0||o.d.some(d=>d===1/0))){let d=Math.floor(Math.log2(L(l.flat().map(Math.abs)))),b=w=>typeof w=="boolean"?w:w.map(y=>k(y,-d)),M=B(l.map(w=>w.map(y=>k(y,-d))),{center:b(i),scale:b(f)});o=j(M.scaled,{nu:0}),f===!1&&(u=d),a={scaled:M.scaled},M.center!==void 0&&(a.center=M.center.map(w=>k(w,d))),M.scale!==void 0&&(a.scale=M.scale.map(w=>k(w,d)))}o??=j(a.scaled,{nu:0});let h=o.d.map(d=>k(d/Math.sqrt(Math.max(1,c-1)),u)),p=t===void 0?h.length:Math.min(Math.trunc(t),h.length);if(Number.isNaN(e))throw new RangeError("'tol' must not be NaN");e!==void 0&&(p=Math.min(p,h.filter(d=>d>h[0]*e).length));let g=o.v.map(d=>d.slice(0,p)),m={sdev:h,rotation:g,x:z(a.scaled,o.v).map(d=>d.slice(0,p).map(b=>k(b,u)))};return a.center!==void 0&&(m.center=a.center),a.scale!==void 0&&(m.scale=a.scale),m}export{Mn as add,Wt as backsolve,An as cbind,Yt as chol,Ht as chol2inv,zt as crossprod,Lt as det,Kt as determinant,Nn as diag,vn as divide,Vt as eigen,on as expm,Jt as forwardsolve,Xt as ginv,Qt as kappa,Sn as kronecker,_n as lu,z as matMul,Rn as matrix,yn as multiply,Dt as norm,kn as outer,Tn as prcomp,Rt as qr,Ot as qrCoef,_t as qrFitted,Tt as qrQ,St as qrQty,Ft as qrQy,Pt as qrR,It as qrResid,$t as qrSolve,jt as qrX,qn as rbind,Ut as rcond,tn as schur,Fn as seq,X as solve,bn as subtract,j as svd,G as t,Gt as tcrossprod};
1
+ /*! rStats.js 1.3.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{h as lt,i as ut,r as B}from"./chunks/chunk-MJXETGT2.js";import"./chunks/chunk-4OOTEXZU.js";import{b as et,c as ot}from"./chunks/chunk-UOAFEB64.js";import{a as Qt,b as Xt}from"./chunks/chunk-IOGQEMDE.js";import{A as Lt,B as Ut,C as Yt,E as Ht,G as Y,H as Vt,J as ct,K as ft,a as A,b as at,c as O,d as _,e as N,g as U,i as st,j as Rt,k as St,l as Ft,m as Ot,n as It,o as _t,p as Tt,q as Pt,r as jt,s as $t,t as j,v as Dt,w as $,x as it,y as X,z as Kt}from"./chunks/chunk-JJRZ2TTF.js";import"./chunks/chunk-AJZV2SVY.js";import"./chunks/chunk-UMDXXT3M.js";import{a as L}from"./chunks/chunk-FFNHG5SB.js";import{l as q,p as T,q as P,u as k}from"./chunks/chunk-PDHBRDTF.js";var Bt=["k","upperTri","transpose"],Ct=["k","upperTri","transpose"];function zt(n,r){let t=_(n),e=r===void 0?t:_(r);if(t.nrow!==e.nrow)throw new TypeError(`non-conformable arguments (${t.nrow} and ${e.nrow} rows)`);let l=t.nrow,i=A(t.ncol,e.ncol),f=dt(t),a=r===void 0?f:dt(e);for(let c=0;c<e.ncol;c++)for(let s=0;s<t.ncol;s++){if(r===void 0&&s>c)continue;let o=0;for(let u=0;u<l;u++)o+=t.data[u+s*l]*e.data[u+c*l];i.data[s+c*t.ncol]=T(o,f[s]*a[c])?P(mt(t,s),mt(e,c),o):o}return r===void 0&&gt(i),N(i)}function Gt(n,r){let t=_(n),e=r===void 0?t:_(r);if(t.ncol!==e.ncol)throw new TypeError(`non-conformable arguments (${t.ncol} and ${e.ncol} columns)`);let l=A(t.nrow,e.nrow),i=pt(t),f=r===void 0?i:pt(e);for(let a=0;a<e.nrow;a++)for(let c=0;c<t.nrow;c++){if(r===void 0&&c>a)continue;let s=0;for(let o=0;o<t.ncol;o++)s+=t.data[c+o*t.nrow]*e.data[a+o*e.nrow];l.data[c+a*t.nrow]=T(s,i[c]*f[a])?P(ht(t,c),ht(e,a),s):s}return r===void 0&&gt(l),N(l)}function mt(n,r){return Array.from(n.data.subarray(r*n.nrow,(r+1)*n.nrow))}function ht(n,r){return Array.from({length:n.ncol},(t,e)=>n.data[r+e*n.nrow])}function gt(n){let r=n.nrow;for(let t=0;t<r;t++)for(let e=t+1;e<r;e++)n.data[e+t*r]=n.data[t+e*r]}function Wt(n,r,t={}){return q(t,Bt,"backsolve"),wt(n,r,t.k,t.upperTri??!0,t.transpose??!1)}function Jt(n,r,t={}){return q(t,Ct,"forwardsolve"),wt(n,r,t.k,t.upperTri??!1,t.transpose??!1)}function wt(n,r,t,e,l){let i=O(n),f=_(r),a=t??i.ncol;if(!Number.isInteger(a)||a<1||a>i.nrow||a>i.ncol||a>f.nrow)throw new RangeError(`invalid k (got ${a})`);for(let s=0;s<a;s++)if(i.data[s+s*i.nrow]===0)throw new Error(`singular matrix: diagonal element ${s+1} is zero`);let c=A(a,f.ncol);for(let s=0;s<f.ncol;s++)for(let o=0;o<a;o++)c.data[o+s*a]=f.data[o+s*f.nrow];return Zt(i,a,c,e,l),at(r)?N(c):Array.from(c.data)}function Zt(n,r,t,e,l){let i=n.nrow,f=n.data,a=t.nrow,c=t.data;for(let s=0;s<t.ncol;s++){let o=s*a;if(!l&&e){for(let u=r-1;u>=0;u--)if(c[o+u]!==0){c[o+u]/=f[u+u*i];let h=c[o+u];for(let p=0;p<u;p++)f[p+u*i]!==0&&(c[o+p]-=h*f[p+u*i])}}else if(l)if(e)for(let u=0;u<r;u++){let h=c[o+u];for(let p=0;p<u;p++)f[p+u*i]!==0&&(h-=f[p+u*i]*c[o+p]);c[o+u]=h/f[u+u*i]}else for(let u=r-1;u>=0;u--){let h=c[o+u];for(let p=u+1;p<r;p++)f[p+u*i]!==0&&(h-=f[p+u*i]*c[o+p]);c[o+u]=h/f[u+u*i]}else for(let u=0;u<r;u++)if(c[o+u]!==0){c[o+u]/=f[u+u*i];let h=c[o+u];for(let p=u+1;p<r;p++)f[p+u*i]!==0&&(c[o+p]-=h*f[p+u*i])}}}function dt(n){let r=[];for(let t=0;t<n.ncol;t++){let e=1/0;for(let l=0;l<n.nrow;l++){let i=Math.abs(n.data[l+t*n.nrow]);i!==0&&i<e&&(e=i)}r.push(e)}return r}function pt(n){let r=new Array(n.nrow).fill(1/0);for(let t=0;t<n.ncol;t++)for(let e=0;e<n.nrow;e++){let l=Math.abs(n.data[e+t*n.nrow]);l!==0&&l<r[e]&&(r[e]=l)}return r}function tn(n){let r=O(n);U(r);let t=r.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let m of r.data)if(!Number.isFinite(m))throw new RangeError("the matrix contains NaN or infinite values");let e=st(r.data),l={nrow:t,ncol:t,data:e===0?r.data.slice():r.data.map(m=>k(m,-e))},i=A(t,t),f=new Float64Array(t),a=new Float64Array(t);ct(l,i),ft(l,i,f,a,!0);let c=l.data,s=i.data,o=(m,d)=>m+d*t,u=new Uint8Array(t);for(let m=0;m+1<t;m++)a[m]>0&&a[m+1]<0&&(u[m++]=1);for(let m=0;m<t;m++)for(let d=m+1;d<t;d++)d===m+1&&u[m]||(c[o(d,m)]=0);let h=new Array(t),p=new Array(t);for(let m=0;m<t;m++){if(!u[m]){h[m]=c[o(m,m)],p[m]=0;continue}let d=nn(c[o(m,m)],c[o(m,m+1)],c[o(m+1,m)],c[o(m+1,m+1)]);c[o(m,m)]=d.a,c[o(m,m+1)]=d.b,c[o(m+1,m)]=d.c,c[o(m+1,m+1)]=d.d;let{cs:b,sn:M}=d;for(let w=m+2;w<t;w++){let y=c[o(m,w)],E=c[o(m+1,w)];c[o(m,w)]=b*y+M*E,c[o(m+1,w)]=b*E-M*y}for(let w=0;w<m;w++){let y=c[o(w,m)],E=c[o(w,m+1)];c[o(w,m)]=b*y+M*E,c[o(w,m+1)]=b*E-M*y}for(let w=0;w<t;w++){let y=s[o(w,m)],E=s[o(w,m+1)];s[o(w,m)]=b*y+M*E,s[o(w,m+1)]=b*E-M*y}h[m]=d.rt1r,p[m]=d.rt1i,h[m+1]=d.rt2r,p[m+1]=d.rt2i,m++}if(e===0)return{T:N(l),Q:N(i),valuesRe:h,valuesIm:p};let g=m=>k(m,e);return{T:N(l).map(m=>m.map(g)),Q:N(i),valuesRe:h.map(g),valuesIm:p.map(g)}}var F=(n,r)=>r>=0&&!Object.is(r,-0)?Math.abs(n):-Math.abs(n);function nn(n,r,t,e){let l=22250738585072014e-324,i=Number.EPSILON,f=4,a=2**Math.trunc(Math.log(l/i)/Math.log(2)/2),c=1/a,s,o;if(t===0)s=1,o=0;else if(r===0){s=0,o=1;let h=e;e=n,n=h,r=-t,t=0}else if(n-e===0&&F(1,r)!==F(1,t))s=1,o=0;else{let h=n-e,p=.5*h,g=Math.max(Math.abs(r),Math.abs(t)),m=Math.min(Math.abs(r),Math.abs(t))*F(1,r)*F(1,t),d=Math.max(Math.abs(p),g),b=p/d*p+g/d*m;if(b>=f*i){b=p+F(Math.sqrt(d)*Math.sqrt(b),p),n=e+b,e=e-g/b*m;let M=Math.hypot(t,b);s=b/M,o=t/M,r=r-t,t=0}else{let M=0,w=r+t;for(;M++,d=Math.max(Math.abs(h),Math.abs(w)),!(!(d>=c&&(w*=a,h*=a,M<=20))&&!(d<=a&&(w*=c,h*=c,M<=20))););p=.5*h;let y=Math.hypot(w,h);s=Math.sqrt(.5*(1+Math.abs(w)/y)),o=-(p/(y*s))*F(1,w);let E=n*s+r*o,I=-n*o+r*s,W=t*s+e*o,J=-t*o+e*s;if(n=E*s+W*o,r=I*s+J*o,t=-E*o+W*s,e=-I*o+J*s,h=.5*(n+e),n=h,e=h,t!==0)if(r!==0){if(F(1,r)===F(1,t)){let Z=Math.sqrt(Math.abs(r)),tt=Math.sqrt(Math.abs(t));p=F(Z*tt,t),y=1/Math.sqrt(Math.abs(r+t)),n=h+p,e=h-p,r=r-t,t=0;let nt=Z*y,rt=tt*y;h=s*nt-o*rt,o=s*rt+o*nt,s=h}}else r=-t,t=0,h=s,s=-o,o=h}}let u=t===0?0:Math.sqrt(Math.abs(r))*Math.sqrt(Math.abs(t));return{a:n,b:r,c:t,d:e,cs:s,sn:o,rt1r:n,rt1i:u,rt2r:e,rt2i:-u}}var rn=["method","order"],en=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function on(n,r={}){q(r,rn,"expm");let t=r.method??"Higham08.b";if(!en.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let e=r.order??8,l=t==="Pade"||t==="PadeO"||t==="Taylor"||t==="TaylorO";if(l&&!(Number.isInteger(e)&&e>=1&&e<=2147483647))throw new RangeError("order must be a positive integer at most 2147483647");let i=O(n);U(i);let f=i.nrow;if(f===0)return[];if(f===1)return[[Math.exp(i.data[0])]];if(l&&e*(f**3+64)>2**31)throw new RangeError(`order = ${e} is too large for a ${f} x ${f} matrix: order * (n^3 + 64) must be at most 2^31`);for(let h of i.data)if(!Number.isFinite(h))throw new RangeError("the matrix contains NaN or infinite values");if(t==="Taylor"||t==="TaylorO")return N(un(i,e));if(t==="Pade"||t==="PadeO")return N(mn(i,e));let a,c;t==="Higham08.b"?(a=Y("P",i),c=Y("S",i)):fn(i)&&(c=Y("S",i));let s=ln(i),o=s.data,u=(h,p)=>h+p*f;if(c){let h=c.scale;for(let p=0;p<f;p++)for(let g=0;g<f;g++)o[u(g,p)]=o[u(g,p)]*h[g]/h[p]}if(a){let h=a.scale,p=g=>{let m=h[g];if(m!==g){for(let d=0;d<f;d++)[o[u(d,g)],o[u(d,m)]]=[o[u(d,m)],o[u(d,g)]];for(let d=0;d<f;d++)[o[u(g,d)],o[u(m,d)]]=[o[u(m,d)],o[u(g,d)]]}};for(let g=a.ilo-1;g>=0;g--)p(g);for(let g=a.ihi+1;g<f;g++)p(g)}return N(s)}var an={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},sn=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function cn(n,r){if(r<1/0)return Math.log2(r);let t=0;for(let l of n.data)t=Math.max(t,Math.abs(l));let e=Math.floor(Math.log2(t));return Math.log2($({...n,data:n.data.map(l=>k(l,-e))}))+e}function bt(n,r){return Math.max(0,Math.ceil(cn(n,r)-Math.log2(5.4)))}function fn(n){let r=$(n);if(r<=2.1)return!1;let t=bt(n,r);return t>0&&n.data.some(e=>e!==0&&Math.abs(k(e,-t))<2**-1022)}function ln(n){let r=n.nrow,t=$(n),e=C(r),l,i,f=0;if(t<=2.1){let o=t<=.015?3:t<=.25?5:t<=.95?7:9,u=an[o],h=R(n,n),p=H([[u[1],e]]),g=H([[u[0],e]]),m=h;for(let d=1;d<=(o-1)/2;d++)d>1&&(m=R(m,h)),D(p,u[2*d+1],m),D(g,u[2*d],m);l=R(n,p),i=g}else{f=bt(n,t);let o={...n,data:n.data.map(M=>k(M,-f))},u=R(o,o),h=R(u,u),p=R(u,h),g=sn,m=H([[g[13],p],[g[11],h],[g[9],u]]),d=R(p,m);for(let[M,w]of[[7,p],[5,h],[3,u],[1,e]])D(d,g[M],w);l=R(o,d);let b=H([[g[12],p],[g[10],h],[g[8],u]]);i=R(p,b);for(let[M,w]of[[6,p],[4,h],[2,u],[0,e]])D(i,g[M],w)}let a=A(r,r),c=A(r,r);for(let o=0;o<r*r;o++)a.data[o]=i.data[o]-l.data[o],c.data[o]=i.data[o]+l.data[o];let s=O(X(N(a),N(c)));for(let o=0;o<f;o++)s=Nt(s,s);return s}function yt(n){let r=$(n),t=it(n),e=r*t<1/0?Math.sqrt(r*t):Math.sqrt(r)*Math.sqrt(t),l=1;for(;2**l<e;)l++;let i=l+4;if(!n.data.some(a=>a!==0&&Math.abs(k(a,-i))<2**-1022))return i;if(e===0||r*t<2**-1e3)return Math.max(0,Math.ceil((Math.log2(r)+Math.log2(t))/2)+4);let f=l;for(;f>-4&&2**(f-1)>=e;)f--;return Math.max(0,f+4)}function vt(n,r){let t=Math.max(0,r-1e3);return{A:t===0?n:K(n,2**-t),N:2**(r-t)}}function Et(n,r){for(let t=0;t<r;t++)n=Nt(n,n);return n}function kt(n){let r=n.nrow;for(let t=0;t<r;t++)n.data[t+t*r]=n.data[t+t*r]+1}function un(n,r){let t=yt(n),{A:e,N:l}=vt(n,t),i=C(n.nrow);for(let f=r;f>=1;f--)i=K(R(i,e),1/(f*l)),kt(i);return Et(i,t)}function Mt(n,r,t,e){let l=e===1?n:K(n,-1),i=C(n.nrow);for(let f=r;f>=1;f--){let a=(r-f+1)/(f*(2*r-f+1)*t);i=K(R(i,l),a),kt(i)}return i}function mn(n,r){let t=n.nrow,e=yt(n),{A:l,N:i}=vt(n,e),f=Mt(l,r,i,1),a=Mt(l,r,i,-1),c=A(t,t);for(let s=0;s<t;s++)c.data.set(hn(a,f.data.subarray(s*t,(s+1)*t)),s*t);return Et(c,e)}function hn(n,r){let t=n.nrow,e=1e-100,l=g=>{let m=new Float64Array(t);for(let d=0;d<t;d++){let b=0;for(let M=0;M<t;M++)b+=n.data[d+M*t]*g[M];m[d]=b}return m},i=(g,m)=>{let d=0;for(let b=0;b<t;b++)d+=g[b]*m[b];return d},f=(g,m,d)=>{let b=new Float64Array(t);for(let M=0;M<t;M++)b[M]=g[M]+m*d[M];return b},a=new Float64Array(t),c=Float64Array.from(r),s=Float64Array.from(r),o=Float64Array.from(r),u=Float64Array.from(r),h=Math.sqrt(i(c,c)),p=i(u,r);if(Math.abs(p)<=e)return a;for(let g=0;g<t;g++){let m=l(s),d=i(u,m);if(Math.abs(d)<=e)return a;let b=p/d;if(Math.abs(b)<=e)return a;let M=f(o,-b,m);m=f(o,1,M);let w=l(m);c=f(c,-b,w);let y=Math.sqrt(i(c,c));if(a=f(a,b,m),Math.abs(y/h)<=1e-30)return a;let E=i(u,c);if(Math.abs(E)<=e)return a;let I=E/p;p=E,o=f(c,I,M),w=f(M,I,s),s=f(o,I,w)}return a}function C(n){let r=A(n,n);for(let t=0;t<n;t++)r.data[t+t*n]=1;return r}function Nt(n,r){let t=n.nrow,e=n.ncol,l=r.ncol,i=A(t,l);for(let f=0;f<l;f++)for(let a=0;a<t;a++){let c=0;for(let s=0;s<e;s++){let o=n.data[a+s*t],u=r.data[s+f*e];o===0&&Math.abs(u)===1/0||u===0&&Math.abs(o)===1/0||(c+=o*u)}i.data[a+f*t]=c}return i}function R(n,r){let t=n.nrow,e=n.ncol,l=r.ncol,i=A(t,l);for(let f=0;f<l;f++)for(let a=0;a<t;a++){let c=0;for(let s=0;s<e;s++)c+=n.data[a+s*t]*r.data[s+f*e];i.data[a+f*t]=c}return i}function K(n,r){return{nrow:n.nrow,ncol:n.ncol,data:n.data.map(t=>t*r)}}function D(n,r,t){for(let e=0;e<n.data.length;e++)n.data[e]=n.data[e]+r*t.data[e]}function H(n){let[r,t]=n[0],e=K(t,r);for(let l=1;l<n.length;l++)D(e,n[l][0],n[l][1]);return e}var dn=["fun"],pn=["nrow","ncol"],gn=["nrow","ncol","byrow"],wn=["by","lengthOut"];function x(n){return typeof n!="number"&&n.length>0&&Array.isArray(n[0])}function v(n){return typeof n=="number"?[n]:Array.from(n)}function S(n){let r=n[0].length;if(n.some(t=>t.length!==r))throw new TypeError("all rows of the matrix must have the same length");return r}function z(n,r){let t,e;if(x(n)&&x(r))t=n.map(a=>[...a]),e=r.map(a=>[...a]);else if(x(n)){let a=v(r),c=S(n);t=n.map(s=>[...s]),e=c===a.length?a.map(s=>[s]):c===1?[a]:[]}else if(x(r)){let a=v(n);e=r.map(c=>[...c]),t=r.length===a.length?[a]:r.length===1?a.map(c=>[c]):[]}else{let a=v(n),c=v(r);if(a.length===c.length)t=[a],e=c.map(s=>[s]);else if(a.length===1)t=[a],e=[c];else if(c.length===1)t=a.map(s=>[s]),e=[c];else throw new RangeError("non-conformable arguments")}let l=t[0]?.length??0;if(t.length===0||e.length!==l)throw new RangeError("non-conformable arguments");S(t);let i=e.length>0?S(e):1,f=new Array(i).fill(1/0);for(let a of e)for(let c=0;c<i;c++){let s=Math.abs(a[c]);s!==0&&s<f[c]&&(f[c]=s)}return t.map(a=>{let c=1/0;for(let o of a){let u=Math.abs(o);u!==0&&u<c&&(c=u)}let s=new Array(i).fill(0);for(let o=0;o<i;o++){let u=0;for(let h=0;h<l;h++)u+=a[h]*e[h][o];s[o]=T(u,c*f[o])?P(a,e.map(h=>h[o]),u):u}return s})}function Q(n,r,t){let e=x(n),l=x(r);if(e&&l){let c=S(n);if(n.length!==r.length||c!==S(r))throw new RangeError("non-conformable arrays");return n.map((s,o)=>s.map((u,h)=>t(u,r[o][h])))}if(e||l){let c=e?n:r,s=v(e?r:n),o=c.length,u=S(c);if(s.length===0)return c.map(h=>h.map(()=>NaN));if(s.length>o*u)throw new RangeError("dims [product "+o*u+"] do not match the length of object ["+s.length+"]");return c.map((h,p)=>h.map((g,m)=>e?t(g,s[(p+m*o)%s.length]):t(s[(p+m*o)%s.length],g)))}let i=v(n),f=v(r);if(i.length===0||f.length===0)return[];let a=Math.max(i.length,f.length);return Array.from({length:a},(c,s)=>t(i[s%i.length],f[s%f.length]))}function Mn(n,r){return Q(n,r,(t,e)=>t+e)}function bn(n,r){return Q(n,r,(t,e)=>t-e)}function yn(n,r){return Q(n,r,(t,e)=>t*e)}function vn(n,r){return Q(n,r,(t,e)=>t/e)}var En={"*":(n,r)=>n*r,"+":(n,r)=>n+r,"-":(n,r)=>n-r,"/":(n,r)=>n/r,"^":(n,r)=>n**r};function kn(n,r,...t){q(ot(t),dn,"outer");let e=et(["fun"],t).fun??"*",l=typeof e=="function"?e:En[e];if(l===void 0)throw new RangeError(`invalid fun "${String(e)}"`);let i=Array.from(n),f=Array.from(r);return i.map(a=>f.map(c=>l(a,c)))}function G(n){if(!x(n))return[v(n)];let r=S(n);return Array.from({length:r},(t,e)=>n.map(l=>l[e]))}function Nn(n,r={}){if(q(r,pn,"diag"),x(n)){let a=S(n);return Array.from({length:Math.min(n.length,a)},(c,s)=>n[s][s])}let t=v(n),e,l;if(t.length===1&&r.nrow===void 0&&r.ncol===void 0){if(l=Math.trunc(t[0]),!(l>=0))throw new RangeError("invalid 'nrow' value (< 0)");e=[1]}else e=t,l=V(r.nrow,"nrow")??t.length;let i=V(r.ncol,"ncol")??l;if(e.length===0&&Math.min(l,i)>0)throw new RangeError("'x' must have positive length");let f=Array.from({length:l},()=>new Array(i).fill(0));for(let a=0;a<Math.min(l,i);a++)f[a][a]=e[a%e.length];return l===0?[]:f}function At(n,r){let t=a=>x(a)||v(a).length>0,e=n.some(t)?n.filter(t):n,l=e.map(a=>x(a)?r?G(a):a.map(c=>[...c]):[v(a)]),i=e.filter(x),f=i.length>0?r?i[0].length:S(i[0]):L(l.map(a=>a[0].length));return i.forEach((a,c)=>{if((r?a.length:S(a))!==f)throw new RangeError(`number of ${r?"rows":"columns"} of matrices must match (see arg ${c+2})`)}),l.flatMap(a=>a.map(c=>Array.from({length:f},(s,o)=>c[o%c.length])))}function xn(n){return typeof n=="object"&&!Array.isArray(n)&&!ArrayBuffer.isView(n)}function qt(n){let r=n.filter(xn);if(r.length!==0){if(r.length<n.length)throw new TypeError("cannot combine data frames with vectors or matrices");return r}}function An(...n){let r=qt(n);return r===void 0?G(At(n,!0)):ut(r)}function qn(...n){let r=qt(n);return r===void 0?At(n,!1):lt(r)}function V(n,r){if(n!==void 0){if(!Number.isFinite(n)||n<=-1)throw new RangeError(`invalid '${r}' value`);return Math.trunc(n)+0}}function Rn(n,r={}){q(r,gn,"matrix");let t=v(n),e=t.length,l=V(r.nrow,"nrow"),i=V(r.ncol,"ncol");l===void 0&&i===void 0?(l=e,i=1):l===void 0?l=Math.ceil(e/i):i===void 0&&(i=Math.ceil(e/l));let f=r.byrow??!1;return Array.from({length:l},(a,c)=>Array.from({length:i},(s,o)=>e===0?NaN:t[(f?c*i+o:c+o*l)%e]))}function Sn(n,r){if(!x(n)&&!x(r)){let i=v(n),f=v(r);return i.flatMap(a=>f.map(c=>a*c))}let t=x(n)?n:v(n).map(i=>[i]),e=x(r)?r:v(r).map(i=>[i]),l=[];for(let i of t)for(let f of e)l.push(i.flatMap(a=>f.map(c=>a*c)));return l}function Fn(n,r,t={}){q(t,wn,"seq");let{by:e,lengthOut:l}=t;if(r===void 0&&e===void 0&&l===void 0&&n!==void 0){if(!Number.isFinite(n))throw new RangeError("'from' must be a finite number");return xt(1,n)}if(n!==void 0&&!Number.isFinite(n))throw new RangeError("'from' must be a finite number");if(r!==void 0&&!Number.isFinite(r))throw new RangeError("'to' must be a finite number");if(l===void 0){if(e===void 0)return xt(n??1,r??1);let f=n??1,a=r??1,c=!Number.isFinite(a-f),s=c?.5:1,o=a*s-f*s;if(o===0&&a===0)return[a];let u=o/(e*s);if(!Number.isFinite(u)){if(e===0&&o===0)return[f];throw new RangeError("invalid '(to - from)/by'")}if(u<0||!Number.isFinite(e)&&o!==0&&o>0!=e>0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(o)/Math.max(Math.abs(a*s),Math.abs(f*s))<100*Number.EPSILON)return[f];let h=Math.trunc(u+1e-10),p=c?g=>2*(f/2+g*(e/2)):g=>f+g*e;return Array.from({length:h+1},(g,m)=>m===0?f:e>0?Math.min(p(m),a):Math.max(p(m),a))}if(!Number.isFinite(l)||l<=-1)throw new RangeError("'length.out' must be a non-negative number");let i=Math.ceil(l);if(i===0)return[];if(e===void 0){let f=r??(n??1)+(i-1),a=n??f-(i-1);if(i<=2)return[a,f].slice(0,i);if(a===f)return new Array(i).fill(a);if(!Number.isFinite(f-a)){let s=a/2,o=(f/2-s)/(i-1);return Array.from({length:i},(u,h)=>h===0?a:h===i-1?f:2*(s+h*o))}let c=(f-a)/(i-1);return Array.from({length:i},(s,o)=>o===0?a:o===i-1?f:a+o*c)}if(r===void 0){let f=n??1;return Array.from({length:i},(a,c)=>f+c*e)}if(n===void 0)return Array.from({length:i},(f,a)=>r-(i-1-a)*e);throw new RangeError("too many arguments")}function xt(n,r){let t=Math.trunc(Math.abs(r-n)+1+11920928955078125e-23),e=n<=r?1:-1;return Array.from({length:t},(l,i)=>n+e*i)}var On=["center","scale","rank","tol"],In=22250738585072014e-324;function _n(n){let r=n.length,t=r===0?0:n[0].length,e=n.map(s=>{if(s.length!==t)throw new TypeError("all rows of the matrix must have the same length");if(!s.every(Number.isFinite))throw new RangeError("the matrix contains NaN or infinite values");return Array.from(s)}),l=Array.from({length:r},(s,o)=>o),i=Math.min(r,t);for(let s=0;s<i;s++){let o=s,u=Math.abs(e[s][s]);for(let g=s+1;g<r;g++){let m=Math.abs(e[g][s]);(m>u||Number.isNaN(u))&&(u=m,o=g)}o!==s&&([e[o],e[s]]=[e[s],e[o]],[l[o],l[s]]=[l[s],l[o]]);let h=e[s][s];if(h===0)continue;let p=Math.abs(h)>=In?1/h:NaN;for(let g=s+1;g<r;g++){let m=Number.isNaN(p)?e[g][s]/h:e[g][s]*p;e[g][s]=m;for(let d=s+1;d<t;d++)e[g][d]=e[g][d]-m*e[s][d]}}let f=Array.from({length:r},(s,o)=>Array.from({length:i},(u,h)=>o===h?1:o>h?e[o][h]:0)),a=Array.from({length:i},(s,o)=>Array.from({length:t},(u,h)=>h>=o?e[o][h]:0)),c=Array.from({length:r},()=>new Array(r).fill(0));return l.forEach((s,o)=>c[s][o]=1),{P:c,L:f,U:a}}function Tn(n,r={}){q(r,On,"prcomp");let{rank:t,tol:e}=r;if(t!==void 0){if(!Number.isFinite(t))throw new RangeError("is.finite(rank.) is not TRUE");if(!(Math.trunc(t)>0&&Math.abs(t)<2**31))throw new RangeError("as.integer(rank.) > 0 is not TRUE")}let l=n.map(d=>Array.from(d)),i=r.center??!0,f=r.scale??!1,a=B(l,{center:i,scale:f});if(a.scale?.some(d=>d===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let c=l.length,s=d=>d.every(b=>b.every(Number.isFinite)),o=s(l)&&s(a.scaled)?j(a.scaled,{nu:0}):void 0,u=0;if(s(l)&&(o===void 0||o.d.some(d=>d===1/0))){let d=Math.floor(Math.log2(L(l.flat().map(Math.abs)))),b=w=>typeof w=="boolean"?w:w.map(y=>k(y,-d)),M=B(l.map(w=>w.map(y=>k(y,-d))),{center:b(i),scale:b(f)});o=j(M.scaled,{nu:0}),f===!1&&(u=d),a={scaled:M.scaled},M.center!==void 0&&(a.center=M.center.map(w=>k(w,d))),M.scale!==void 0&&(a.scale=M.scale.map(w=>k(w,d)))}o??=j(a.scaled,{nu:0});let h=o.d.map(d=>k(d/Math.sqrt(Math.max(1,c-1)),u)),p=t===void 0?h.length:Math.min(Math.trunc(t),h.length);if(Number.isNaN(e))throw new RangeError("'tol' must not be NaN");e!==void 0&&(p=Math.min(p,h.filter(d=>d>h[0]*e).length));let g=o.v.map(d=>d.slice(0,p)),m={sdev:h,rotation:g,x:z(a.scaled,o.v).map(d=>d.slice(0,p).map(b=>k(b,u)))};return a.center!==void 0&&(m.center=a.center),a.scale!==void 0&&(m.scale=a.scale),m}export{Mn as add,Wt as backsolve,An as cbind,Yt as chol,Ht as chol2inv,zt as crossprod,Lt as det,Kt as determinant,Nn as diag,vn as divide,Vt as eigen,on as expm,Jt as forwardsolve,Xt as ginv,Qt as kappa,Sn as kronecker,_n as lu,z as matMul,Rn as matrix,yn as multiply,Dt as norm,kn as outer,Tn as prcomp,Rt as qr,Ot as qrCoef,_t as qrFitted,Tt as qrQ,St as qrQty,Ft as qrQy,Pt as qrR,It as qrResid,$t as qrSolve,jt as qrX,qn as rbind,Ut as rcond,tn as schur,Fn as seq,X as solve,bn as subtract,j as svd,G as t,Gt as tcrossprod};
package/dist/mixed.js CHANGED
@@ -1,5 +1,5 @@
1
- /*! rStats.js 1.1.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
- import{a as en,b as Gn}from"./chunks/chunk-HRA7PCQT.js";import{a as Pe,e as Yn,f as Zn,g as Jn,h as Ct}from"./chunks/chunk-4VRMAYPY.js";import{F as Un,G as Wn,i as qn,p as zn}from"./chunks/chunk-DTJG2IL2.js";import{b as qt}from"./chunks/chunk-YN5MLKDL.js";import{D as Te,F as _t,H as on,h as rn,j as He,m as Xn,n as Kn}from"./chunks/chunk-GYGQUYEP.js";import{a as We,c as ft,d as Be,f as Bn,g as yt}from"./chunks/chunk-U7BRXOUS.js";import{i as _n,x as Pn,y as Dn}from"./chunks/chunk-GGJA3JRO.js";import{b as Qn}from"./chunks/chunk-GJEKNJP7.js";import{a as Dr,c as ge,e as Fe,f as jt}from"./chunks/chunk-N5BDSMXS.js";import{b as nn,e as Hn}from"./chunks/chunk-EJYCDE4Z.js";import{a as Je,c as tn}from"./chunks/chunk-OG7UORIX.js";import"./chunks/chunk-4KCSBTR5.js";import{c as Zt,d as Cn}from"./chunks/chunk-X4J6UJHN.js";import{q as Yt,r as Lt}from"./chunks/chunk-NDTJ23A6.js";import{Q as Jt,l as de,m as jn,p as Vn,q as $n,u as oe}from"./chunks/chunk-HUWOSLKE.js";var er=1,tr=new WeakMap;function bt(e){let t=tr.get(e);if(t!==void 0)return t;let n=e.y.length,o=e.xColNames.length,r=e.q,s=e.terms.length,a=e.terms.reduce((R,_)=>R+_.nLevels,0),l=new Int32Array(a+1),f=new Int32Array(a),c=[],i=[],p=0;e.terms.forEach((R,_)=>{let G=R.columns.length;c.push(p),i.push(G);for(let N=0;N<R.nLevels;N++,p++)l[p]=R.zOffset+N*G,f[p]=_}),l[a]=r;let y=e.terms.map((R,_)=>{let G=i[_],N=new Int32Array(G*G).fill(-1);for(let{row:q,col:W,theta:re}of e.lambda)W>=R.zOffset&&W<R.zOffset+G&&(N[q-R.zOffset+(W-R.zOffset)*G]=re);return N}),m=e.terms.map(R=>e.groups[R.group].codes),u=new Int32Array(n+1);for(let R=0;R<n;R++){let _=0;for(let G of e.terms)for(let N of G.matrix[R])N!==0&&_++;u[R+1]=u[R]+_}let h=u,d=h[n],b=new Int32Array(d),E=new Float64Array(d);{let R=0;for(let _=0;_<n;_++)e.terms.forEach((G,N)=>{let q=G.zOffset+m[N][_]*i[N];G.matrix[_].forEach((W,re)=>{W!==0&&(b[R]=q+re,E[R++]=W)})})}let A=new Map,v=[],S=[],x=(R,_)=>{let G=_*a+R,N=A.get(G);return N===void 0&&(N=v.length,A.set(G,N),v.push(R),S.push(_)),N};for(let R=0;R<a;R++)x(R,R);let g=new Int32Array(s),w=s*(s+1)/2,k=new Int32Array(n*w);for(let R=0;R<n;R++){for(let _=0;_<s;_++)g[_]=c[_]+m[_][R];for(let _=0;_<s;_++)for(let G=0;G<=_;G++)k[R*w+_*(_+1)/2+G]=x(g[_],g[G])}let L=v.length,D=Array.from({length:L},(R,_)=>_).sort((R,_)=>S[R]-S[_]||v[R]-v[_]),M=new Int32Array(L);D.forEach((R,_)=>M[R]=_);let F=new Int32Array(a+1),U=new Int32Array(L),J=new Int32Array(L+1);D.forEach((R,_)=>{U[_]=v[R],F[S[R]+1]++;let G=i[f[v[R]]]*i[f[S[R]]];J[_+1]=J[_]+G});for(let R=0;R<a;R++)F[R+1]+=F[R];let $=k.map(R=>M[R]),B=nr(e,i,J,$,void 0),P=new Float64Array(r*o),Q=new Float64Array(r),ne=new Float64Array(o);for(let R=0;R<n;R++){let _=e.x[R],G=e.y[R];for(let N=h[R];N<h[R+1];N++){let q=b[N],W=E[N];for(let re=0;re<o;re++)P[q+re*r]+=W*_[re];Q[q]+=W*G}for(let N=0;N<o;N++)ne[N]+=_[N]*G}let ee=new Array(a),I=Array.from({length:a},()=>[]),O=new Int32Array(a).fill(-1);for(let R=0;R<a;R++){let _=[],G=q=>{q!==R&&O[q]!==R&&(O[q]=R,_.push(q))};for(let q=F[R]+1;q<F[R+1];q++)G(U[q]);for(let q of I[R])for(let W of ee[q])G(W);let N=Int32Array.from(_).sort();ee[R]=N,N.length>0&&I[N[0]].push(R)}let T=new Int32Array(L);{let R=new Int32Array(a);for(let _=0;_<a;_++){let G=0;for(let N of ee[_])R[N]=G,G+=l[N+1]-l[N];for(let N=F[_]+1;N<F[_+1];N++)T[N]=R[U[N]]}}let C=new Int32Array(r+1);for(let R=0;R<a;R++){let _=0;for(let G of ee[R])_+=l[G+1]-l[G];for(let G=l[R];G<l[R+1];G++)C[G+1]=C[G]+(l[R+1]-G)+_}let K=new Int32Array(C[r]),Y=new Int32Array(r+1);for(let R=0;R<a;R++)for(let _=l[R];_<l[R+1];_++){let G=C[_];for(let N=_;N<l[R+1];N++)K[G++]=N;for(let N of ee[R])for(let q=l[N];q<l[N+1];q++)K[G++]=q}for(let R=0;R<r;R++)for(let _=C[R]+1;_<C[R+1];_++)Y[K[_]+1]++;for(let R=0;R<r;R++)Y[R+1]+=Y[R];let j=Y.slice(),V=Y.slice(0,r),z=new Int32Array(j[r]),Z=new Int32Array(j[r]);for(let R=0;R<r;R++)for(let _=C[R]+1;_<C[R+1];_++){let G=V[K[_]]++;z[G]=R,Z[G]=_}let X={n,p:o,q:r,nGroups:a,groupStart:l,groupTerm:f,termNc:i,termTheta:y,blockStart:F,blockRow:U,blockValueStart:J,zz:B,zx:P,zy:Q,xy:ne,rowStart:h,rowCols:b,rowValues:E,colStart:C,colRows:K,rowListStart:j,rowListCol:z,rowListPos:Z,blockPatternOffset:T,rowBlocks:$};return tr.set(e,X),X}function nr(e,t,n,o,r){let s=e.y.length,a=e.terms.length,l=a*(a+1)/2,f=new Float64Array(n[n.length-1]);for(let c=0;c<s;c++){let i=r===void 0?1:r[c];if(i!==0)for(let p=0;p<a;p++){let y=e.terms[p].matrix[c],m=t[p];for(let u=0;u<=p;u++){let h=e.terms[u].matrix[c],d=t[u],b=n[o[c*l+p*(p+1)/2+u]];for(let E=0;E<d;E++){let A=r===void 0?h[E]:i*h[E];if(A!==0)for(let v=0;v<m;v++)f[b+v+E*m]+=y[v]*A}}}}return f}function et(e,t,n){return nr(t,e.termNc,e.blockValueStart,e.rowBlocks,n)}function mt(e,t){let n=new Float64Array(e.n);for(let o=0;o<e.n;o++){let r=0;for(let s=e.rowStart[o];s<e.rowStart[o+1];s++)r+=e.rowValues[s]*t[e.rowCols[s]];n[o]=r}return n}function je(e,t){let n=new Float64Array(e.q);for(let o=0;o<e.n;o++){let r=t[o];for(let s=e.rowStart[o];s<e.rowStart[o+1];s++)n[e.rowCols[s]]+=e.rowValues[s]*r}return n}function xt(e,t){return e.termTheta.map(n=>Float64Array.from(n,o=>o<0?0:t[o]))}function Ie(e,t,n){let o=new Float64Array(e.q);for(let r=0;r<e.nGroups;r++){let s=e.groupTerm[r],a=e.termNc[s],l=t[s],f=e.groupStart[r];for(let c=0;c<a;c++){let i=n[f+c];if(i!==0)for(let p=c;p<a;p++)o[f+p]+=l[p+c*a]*i}}return o}function we(e,t,n,o=1){let r=e.q,s=new Float64Array(r*o);for(let a=0;a<e.nGroups;a++){let l=e.groupTerm[a],f=e.termNc[l],c=t[l],i=e.groupStart[a];for(let p=0;p<o;p++){let y=i+p*r;for(let m=0;m<f;m++){let u=0;for(let h=m;h<f;h++)u+=c[h+m*f]*n[y+h];s[y+m]=u}}}return s}function tt(e,t,n=e.zz){let o=new Float64Array(n.length),r=new Float64Array(64);for(let s=0;s<e.nGroups;s++){let a=e.groupTerm[s],l=e.termNc[a],f=t[a];for(let c=e.blockStart[s];c<e.blockStart[s+1];c++){let i=e.groupTerm[e.blockRow[c]],p=e.termNc[i],y=t[i],m=e.blockValueStart[c];if(p===1&&l===1)o[m]=y[0]*n[m]*f[0];else{let u=r.length>=p*l?r:new Float64Array(p*l);for(let h=0;h<l;h++)for(let d=0;d<p;d++){let b=0;for(let E=h;E<l;E++)b+=n[m+d+E*p]*f[E+h*l];u[d+h*p]=b}for(let h=0;h<l;h++)for(let d=0;d<p;d++){let b=0;for(let E=d;E<p;E++)b+=y[E+d*p]*u[E+h*p];o[m+d+h*p]=b}}if(c===e.blockStart[s])for(let u=0;u<l;u++)o[m+u+u*l]+=1}}return o}function nt(e,t){let{q:n,colStart:o,colRows:r,rowListStart:s,rowListCol:a,rowListPos:l}=e,f=new Float64Array(r.length),c=new Float64Array(n);for(let i=0;i<e.nGroups;i++){let p=e.termNc[e.groupTerm[i]],y=e.groupStart[i];for(let m=0;m<p;m++){let u=y+m;for(let E=e.blockStart[i];E<e.blockStart[i+1];E++){let A=e.blockRow[E],v=e.termNc[e.groupTerm[A]],S=e.blockValueStart[E]+m*v,x=e.groupStart[A];for(let g=A===i?m:0;g<v;g++)c[x+g]=t[S+g]}for(let E=s[u];E<s[u+1];E++){let A=a[E],v=l[E],S=f[v];if(S===0)continue;let x=o[A+1];for(let g=v;g<x;g++)c[r[g]]-=f[g]*S}let h=c[u];if(!(h>0))throw new Error("the penalised least-squares matrix is not positive definite");let d=Math.sqrt(h),b=o[u];f[b]=d,c[u]=0;for(let E=b+1;E<o[u+1];E++){let A=r[E];f[E]=c[A]/d,c[A]=0}}}return f}function ye(e,t,n,o=0){let{q:r,colStart:s,colRows:a}=e;for(let l=0;l<r;l++){let f=s[l],c=n[o+l]/=t[f];if(c!==0)for(let i=f+1;i<s[l+1];i++)n[o+a[i]]-=t[i]*c}}function Ce(e,t,n,o=0){let{q:r,colStart:s,colRows:a}=e;for(let l=r-1;l>=0;l--){let f=s[l],c=n[o+l];for(let i=f+1;i<s[l+1];i++)c-=t[i]*n[o+a[i]];n[o+l]=c/t[f]}}function De(e,t,n){for(let o=0;o<t;o++){let r=o*t,s=n[o];for(let a=0;a<o;a++)s-=e[a+r]*n[a];n[o]=s/e[o+r]}}function Qe(e,t,n){for(let o=t-1;o>=0;o--){let r=n[o]/=e[o+o*t],s=o*t;for(let a=0;a<o;a++)n[a]-=e[a+s]*r}}function sn(e,t){let n=e.slice();for(let o=0;o<t;o++)for(let r=o+1;r<t;r++)n[r+o*t]=0;_t({nrow:t,ncol:t,data:n});for(let o=0;o<t;o++)for(let r=o+1;r<t;r++)n[r+o*t]=n[o+r*t];return n}function an(e,t,n,o={}){let r=At(e,t),{sys:s,t:a,l,rx:f,ws:c,xResiduals:i,res:p,uv:y}=r,{n:m,p:u,q:h}=s,d=Array.from(r.beta),b=Array.from(y),E=Array.from(r.b),A=0;for(let M of p)A+=M*M;let v=0;for(let M of b)v+=M*M;let S=A+v,x=0;for(let M=0;M<h;M++)x+=Math.log(l[s.colStart[M]]);x*=2;let g=0;for(let M=0;M<u;M++)g+=Math.log(f[M+M*u]);g*=2;let w=n?m-u:m,L={deviance:(n?x+g:x)+w*(1+Math.log(2*Math.PI*S/w)),beta:d,u:b,b:E,rss:A,ussq:v,prss:S,logDetL:x,logDetRX:g},D=o.gradient===!0||o.rxInverse===!0?sn(f,u):void 0;return o.rxInverse===!0&&(L.rxInverse=Array.from({length:u},(M,F)=>Array.from({length:u},(U,J)=>D[F+J*u]))),o.gradient===!0&&(L.gradient=Gr(e,s,a,l,t,n,{res:p,uv:y,prss:S,ws:c,xResiduals:i,sInverse:D})),L}function At(e,t){if(t.length!==e.theta.length)throw new RangeError(`theta must have length ${e.theta.length}, not ${t.length}`);if(t.some(x=>!Number.isFinite(x)))throw new RangeError("theta must be finite");let n=bt(e),{n:o,p:r,q:s}=n,a=xt(n,t),l=nt(n,tt(n,a)),f=x=>mt(n,x),c=x=>je(n,x),i=x=>{ye(n,l,x),Ce(n,l,x)},p=we(n,a,n.zx,r);for(let x=0;x<r;x++)ye(n,l,p,x*s);let y=[],m=[];for(let x=0;x<r;x++){let g=p.slice(x*s,(x+1)*s);Ce(n,l,g);let w=Float64Array.from(e.x,D=>D[x]),k=()=>{let D=f(Ie(n,a,g));return w.map((M,F)=>M-D[F])},L=k();for(let D=0;D<er;D++){let M=we(n,a,c(L));for(let F=0;F<s;F++)M[F]-=g[F];i(M);for(let F=0;F<s;F++)g[F]+=M[F];L=k()}y.push(L),m.push(g)}let u=new Float64Array(r*r);for(let x=0;x<r;x++)for(let g=x;g<r;g++){let w=0,k=y[x],L=y[g];for(let F=0;F<o;F++)w+=k[F]*L[F];let D=m[x],M=m[g];for(let F=0;F<s;F++)w+=D[F]*M[F];u[x+g*r]=w,u[g+x*r]=w}let h={nrow:r,ncol:r,data:u};if(r>0&&Te(h)>0)throw new Error("the fixed-effects matrix of the penalised least squares is not positive definite");let d=(x,g)=>{ye(n,l,x);for(let w=0;w<r;w++){let k=g[w];for(let L=0;L<s;L++)k-=p[L+w*s]*x[L];g[w]=k}De(u,r,g),Qe(u,r,g);for(let w=0;w<r;w++){let k=g[w];for(let L=0;L<s;L++)x[L]-=p[L+w*s]*k}Ce(n,l,x)},b=(x,g)=>{let w=f(g),k=new Float64Array(o);for(let L=0;L<o;L++){let D=e.x[L],M=w[L];for(let F=0;F<r;F++)M+=D[F]*x[F];k[L]=e.y[L]-M}return k},E=we(n,a,n.zy),A=Float64Array.from(n.xy);d(E,A);let v=Ie(n,a,E),S=b(A,v);for(let x=0;x<er;x++){let g=we(n,a,c(S));for(let k=0;k<s;k++)g[k]-=E[k];let w=new Float64Array(r);for(let k=0;k<o;k++){let L=e.x[k],D=S[k];for(let M=0;M<r;M++)w[M]+=L[M]*D}d(g,w);for(let k=0;k<s;k++)E[k]+=g[k];for(let k=0;k<r;k++)A[k]+=w[k];v=Ie(n,a,E),S=b(A,v)}return{sys:n,t:a,l,cx:p,rx:u,ws:m,xResiduals:y,res:S,uv:E,beta:A,b:v,solveNormal:d}}function Et(e,t){let{q:n,colStart:o,colRows:r}=e,s=new Float64Array(t.length),a=new Int32Array(n).fill(-1),l=new Float64Array(n),f=new Float64Array(n);for(let c=n-1;c>=0;c--){let i=o[c],p=o[c+1];for(let u=i+1;u<p;u++){let h=r[u];a[h]=c,l[h]=t[u],f[h]=0}for(let u=i+1;u<p;u++){let h=r[u],d=t[u],b=o[h],E=s[b]*d;for(let A=b+1;A<o[h+1];A++){let v=r[A];if(a[v]!==c)continue;let S=s[A];f[v]+=S*d,E+=S*l[v]}f[h]+=E}let y=t[i],m=1/y;for(let u=i+1;u<p;u++){let h=-f[r[u]]/y;s[u]=h,m-=t[u]*h}s[i]=m/y}return s}function rr(e,t,n){let{q:o,colStart:r,colRows:s,rowListStart:a,rowListCol:l,rowListPos:f}=e,c=new Float64Array(t.length),i=new Float64Array(o);for(let p=0;p<e.nGroups;p++){let y=e.termNc[e.groupTerm[p]],m=e.groupStart[p];for(let u=0;u<y;u++){let h=m+u;for(let A=e.blockStart[p];A<e.blockStart[p+1];A++){let v=e.blockRow[A],S=e.termNc[e.groupTerm[v]],x=e.blockValueStart[A]+u*S,g=e.groupStart[v];for(let w=v===p?u:0;w<S;w++)i[g+w]=n[x+w]}for(let A=a[h];A<a[h+1];A++){let v=l[A],S=f[A],x=t[S],g=c[S];if(x===0&&g===0)continue;let w=r[v+1];for(let k=S;k<w;k++)i[s[k]]-=c[k]*x+t[k]*g}let d=r[h],b=t[d],E=i[h]/(2*b);c[d]=E,i[h]=0;for(let A=d+1;A<r[h+1];A++){let v=s[A];c[A]=(i[v]-t[A]*E)/b,i[v]=0}}}return c}function or(e,t,n,o){let{q:r,colStart:s,colRows:a}=e,l=new Float64Array(t.length),f=new Int32Array(r).fill(-1),c=new Float64Array(r),i=new Float64Array(r),p=new Float64Array(r),y=new Float64Array(r);for(let m=r-1;m>=0;m--){let u=s[m],h=s[m+1];for(let v=u+1;v<h;v++){let S=a[v];f[S]=m,c[S]=t[v],i[S]=n[v],p[S]=0,y[S]=0}for(let v=u+1;v<h;v++){let S=a[v],x=t[v],g=n[v],w=s[S],k=o[w]*x,L=l[w]*x+o[w]*g;for(let D=w+1;D<s[S+1];D++){let M=a[D];if(f[M]!==m)continue;let F=o[D],U=l[D];p[M]+=F*x,y[M]+=U*x+F*g,k+=F*c[M],L+=U*c[M]+F*i[M]}p[S]+=k,y[S]+=L}let d=t[u],b=n[u],E=1/d,A=-b/(d*d);for(let v=u+1;v<h;v++){let S=a[v],x=o[v],g=-y[S]/d+p[S]*b/(d*d);l[v]=g,E-=t[v]*x,A-=n[v]*x+t[v]*g}l[u]=A/d-E*b/(d*d)}return l}function Gr(e,t,n,o,r,s,{res:a,uv:l,prss:f,ws:c,xResiduals:i,sInverse:p}){let{n:y,p:m,q:u}=t,h=rt(t,Et(t,o),n),d=new Array(r.length).fill(0);t.termTheta.forEach((x,g)=>{let w=t.termNc[g];for(let k=0;k<w;k++)for(let L=k;L<w;L++)d[x[L+k*w]]+=2*h[g][k+L*w]});let b=new Float64Array(u);for(let x=0;x<y;x++){let g=a[x];for(let w=t.rowStart[x];w<t.rowStart[x+1];w++)b[t.rowCols[w]]+=t.rowValues[w]*g}let E,A;if(s&&m>0){E=new Float64Array(u*m);for(let x=0;x<m;x++)for(let g=0;g<m;g++){let w=p[g+x*m],k=c[g];for(let L=0;L<u;L++)E[L+x*u]+=k[L]*w}A=new Float64Array(m*u);for(let x=0;x<y;x++)for(let g=t.rowStart[x];g<t.rowStart[x+1];g++){let w=t.rowCols[g],k=t.rowValues[g];for(let L=0;L<m;L++)A[L+w*m]+=i[L][x]*k}}let S=2*(s?y-m:y)/f;for(let{row:x,col:g,theta:w}of e.lambda){let k=0;if(E!==void 0&&A!==void 0)for(let L=0;L<m;L++)k+=E[g+L*u]*A[L+x*m];d[w]-=2*k+S*b[x]*l[g]}return d}function rt(e,t,n,o=e.zz){let{colStart:r}=e,s=e.termNc.map(f=>new Float64Array(f*f)),a=new Float64Array(16),l=new Float64Array(16);for(let f=0;f<e.nGroups;f++){let c=e.groupTerm[f],i=e.termNc[c],p=n[c],y=e.groupStart[f];for(let m=e.blockStart[f];m<e.blockStart[f+1];m++){let u=e.blockRow[m],h=e.groupTerm[u],d=e.termNc[h],b=n[h],E=e.blockValueStart[m];if(i===1&&d===1){let x=u===f?r[y]:r[y]+1+e.blockPatternOffset[m],g=t[x]*o[E];s[c][0]+=g*b[0],u!==f&&(s[h][0]+=g*p[0]);continue}let A=d*i;a.length<A&&(a=new Float64Array(A),l=new Float64Array(Math.max(A,d*d,i*i)));for(let x=0;x<i;x++){let g=r[y+x];if(u===f)for(let w=x;w<d;w++){let k=t[g+w-x];a[w+x*d]=k,a[x+w*d]=k}else{let w=g+(i-x)+e.blockPatternOffset[m];for(let k=0;k<d;k++)a[k+x*d]=t[w+k]}}for(let x=0;x<i;x++)for(let g=0;g<d;g++){let w=0;for(let k=g;k<d;k++)w+=b[k+g*d]*o[E+k+x*d];l[g+x*d]=w}let v=s[c];for(let x=0;x<i;x++)for(let g=0;g<i;g++){let w=0;for(let k=0;k<d;k++)w+=a[k+x*d]*l[k+g*d];v[x+g*i]+=w}if(u===f)continue;for(let x=0;x<d;x++)for(let g=0;g<i;g++){let w=0;for(let k=g;k<i;k++)w+=p[k+g*i]*o[E+x+k*d];l[g+x*i]=w}let S=s[h];for(let x=0;x<d;x++)for(let g=0;g<d;g++){let w=0;for(let k=0;k<i;k++)w+=a[x+k*d]*l[k+g*i];S[x+g*d]+=w}}}return s}function Ur(e){let t=[],n=l=>{l.type==="var"?t.includes(l.name)||t.push(l.name):l.type==="binary"?(n(l.left),l.op!=="^"&&n(l.right)):(l.type==="unary"||l.type==="paren"||l.type==="Error")&&n(l.arg)};n(e);let{terms:o}=Bn(e,l=>t.indexOf(l),[]);if(o.length===0)throw new SyntaxError(`invalid formula: no grouping factor in '${Be(e)}'`);let r=o.map((l,f)=>[l,f]).sort((l,f)=>l[0].length-f[0].length||l[1]-f[1]).map(([l])=>l.map(f=>t[f])),s=!1,a=l=>{l.type==="binary"?(l.op==="/"&&(s=!0),a(l.left),a(l.right)):(l.type==="unary"||l.type==="paren"||l.type==="Error")&&a(l.arg)};return a(e),s&&(r=r.map(l=>[...l].reverse()).reverse()),r}function Wr(e){if(e.kind==="factor")return Qn(e);let t=[...new Set(e.values)].sort((r,s)=>r-s),n=[],o=new Map;for(let r of t){let s=nn(r);o.has(s)||(o.set(s,n.length),n.push(s))}return{levels:n,codes:e.values.map(r=>o.get(nn(r)))}}function ln(e,t,n={},o=!1){let r=We(e,!1,!0,o);if(r.response===void 0)throw new TypeError("the formula has no response");let{fixed:s,bars:a}=ft(r.rhs);if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let l={...n.levels===void 0?{}:{levels:n.levels},dropUnusedLevels:!0},f=s??{type:"num",value:1,text:"1"},c={...r,rhs:f},i=yt(r,Object.keys(t),f).variables,p=n.contrasts===void 0?{}:{contrasts:n.contrasts},y=o?{anyResponse:!0}:{},m=Pe(c,t,{...l,...p,...y,extraVariables:i},f),u=[...m.terms.variables,...i.filter(h=>!m.terms.variables.includes(h))];return{parsed:r,bars:a,fixedDesign:m,variables:u,frameOptions:l,responseOptions:y}}function zt(e,t,n){let o=[],r;return n.forEach((s,a)=>{let l=[s.lhs];if(s.double){let c=yt(e,t,s.lhs);l=c.intercept?[{type:"num",value:1,text:"1"}]:[];for(let i of c.terms){let p=i.map(y=>({type:"var",name:c.variables[y]})).reduce((y,m)=>({type:"binary",op:":",left:y,right:m}));l.push({type:"binary",op:"+",left:{type:"num",value:0,text:"0"},right:p})}l.length===0&&(r??=a)}let f=Ur(s.group);for(let c of l)for(let i of f)o.push({lhs:c,groupVariables:i,bar:a})}),{expanded:o,noColumns:r}}function Vt(e,t,n={},o=!1){let{parsed:r,bars:s,fixedDesign:a,variables:l,frameOptions:f,responseOptions:c}=ln(e,t,n,o),{frame:i}=a,p=i.rows.length;if(p===0&&!o)throw new RangeError("0 (non-NA) cases");if(s.length===0)throw new Error("No random effects terms specified in formula");let{expanded:y,noColumns:m}=zt(r,Object.keys(t),s),u=m;if(p===0)throw new RangeError(`Invalid grouping factor specification, ${y[0]?.groupVariables.join(":")??""}`);let h=new Map,d=I=>{let O=I.join(":"),T=h.get(O);if(T!==void 0)return T;let C=I.map(Y=>Wr(i.variables[l.indexOf(Y)])),K;if(C.length===1)K=C[0];else{let Y=new Map;for(let z=0;z<p;z++){let Z=C.map(X=>X.codes[z]);Y.set(Z.join(","),Z)}let j=[...Y.values()].sort((z,Z)=>{for(let X=0;X<z.length;X++)if(z[X]!==Z[X])return z[X]-Z[X];return 0}),V=new Map(j.map((z,Z)=>[z.join(","),Z]));K={levels:j.map(z=>z.map((Z,X)=>C[X].levels[Z]).join(":")),codes:Array.from({length:p},(z,Z)=>V.get(C.map(X=>X.codes[Z]).join(",")))}}return h.set(O,K),K},b=y.map(I=>({...I,name:I.groupVariables.join(":"),factor:d(I.groupVariables)})),E=b;b.some((I,O)=>O>0&&I.factor.levels.length>b[O-1].factor.levels.length)&&(E=b.map((I,O)=>[I,O]).sort((I,O)=>I[0].factor.levels.length-O[0].factor.levels.length||I[1]-O[1]).map(([I])=>I).reverse());let A=[],v=[],S=0,x=0;for(let I of E){let O=A.findIndex(Y=>Y.name===I.name);O<0&&(O=A.length,A.push({name:I.name,levels:I.factor.levels,codes:I.factor.codes}));let T=Pe(r,t,{...f,...c},I.lhs),C=T.colNames.length;C===0&&(u===void 0||I.bar<u)&&(u=I.bar);let K=I.factor.levels.length;v.push({label:`${Be(I.lhs)} | ${I.name}`,group:O,columns:T.colNames,matrix:T.x,nLevels:K,zOffset:S,thetaOffset:x}),S+=K*C,x+=C*(C+1)/2}if(u!==void 0){let I=s[u];throw new Error(`lmer: the random-effects term '${Be(I.lhs)} ${I.double?"||":"|"} ${Be(I.group)}' has no columns`)}if(A.some(I=>I.levels.length<2))throw new RangeError("grouping factors must have > 1 sampled level");let g=o?[]:A.filter(I=>I.levels.length>=p).map(I=>I.name);if(g.length>0)throw new RangeError(`number of levels of each grouping factor must be < number of observations (problems: ${g.join(", ")})`);for(let I of v){let O=I.nLevels*I.columns.length;if(o&&p<O)throw new RangeError(`number of observations (=${p}) < number of random effects (=${O}) for term (${I.label}); the random-effects parameters are probably unidentifiable`);if(!o&&p<=O)throw new RangeError(`number of observations (=${p}) <= number of random effects (=${O}) for term (${I.label}); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable`)}for(let I of v)if(I.matrix.some(O=>O.some(T=>!Number.isFinite(T))))throw new RangeError(`NA/NaN/Inf in the random-effects term (${I.label})`);let w=[],k=[],L=[],D=[],M=[];for(let I of v){let{levels:O,codes:T}=A[I.group],C=I.columns.length,K=Array.from({length:O.length*C},()=>({rows:[],values:[]}));for(let V=0;V<p;V++){let z=I.matrix[V],Z=T[V]*C;for(let X=0;X<C;X++){let R=z[X];if(R===0)continue;let _=K[Z+X];_.rows.push(V),_.values.push(R)}}for(let V=0;V<O.length;V++)for(let z=0;z<C;z++)w.push(K[V*C+z]),k.push(O[V]);let Y=Array.from({length:C},()=>new Array(C).fill(-1)),j=I.thetaOffset;for(let V=0;V<C;V++)for(let z=V;z<C;z++)Y[z][V]=j++,L.push(z===V?1:0),D.push(z===V?0:-1/0);for(let V=0;V<I.nLevels;V++){let z=I.zOffset+V*C;for(let Z=0;Z<C;Z++)for(let X=Z;X<C;X++)M.push({row:z+X,col:z+Z,theta:Y[X][Z]})}}let F=[],U=a.x,J=a.colNames,$=a.assign;if(J.length>0){let I=He(U),O=new Set(I.pivot.slice(I.rank));if(O.size>0){let T=J.map((C,K)=>K).filter(C=>!O.has(C));U=U.map(C=>T.map(K=>C[K])),J=T.map(C=>J[C]),$=T.map(C=>$[C]),F.push(`fixed-effect model matrix is rank deficient so dropping ${O.size} ${O.size===1?"column / coefficient":"columns / coefficients"}`)}}let B=[...a.warnings],P=[];for(let I=0;I<J.length;I++){let O=U.map(K=>K[I]);if(O.every(K=>K===0||K===1))continue;let T=O.reduce((K,Y)=>K+Y,0)/p,C=Math.sqrt(O.reduce((K,Y)=>K+(Y-T)**2,0)/(p-1));P.push(C)}let Q=Math.log(1e3);P.some((I,O)=>Math.abs(Math.log(I))>Q||P.some((T,C)=>C!==O&&Math.abs(Math.log(I/T))>Q))&&B.push(`Some predictor variables are on very different scales: consider rescaling.
1
+ /*! rStats.js 1.3.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
+ import{a as en,b as Gn}from"./chunks/chunk-I5NWGQOQ.js";import{c as Yn,d as Pe,f as Zn,g as Jn,h as Ct}from"./chunks/chunk-LNUNKFIH.js";import{F as Un,G as Wn,i as qn,p as zn}from"./chunks/chunk-LMNU4NHZ.js";import{b as qt}from"./chunks/chunk-IOGQEMDE.js";import{D as Te,F as _t,H as on,h as rn,j as He,m as Xn,n as Kn}from"./chunks/chunk-JJRZ2TTF.js";import{a as We,c as ft,d as Be,f as Bn,g as yt}from"./chunks/chunk-6X2ZMPF5.js";import{i as _n,y as Pn,z as Dn}from"./chunks/chunk-HBRSUHSU.js";import{b as Qn}from"./chunks/chunk-AJZV2SVY.js";import{a as Dr,c as ge,e as Fe,f as jt}from"./chunks/chunk-GUM57VVM.js";import{b as nn,e as Hn}from"./chunks/chunk-UMDXXT3M.js";import{a as Je,c as tn}from"./chunks/chunk-FFNHG5SB.js";import"./chunks/chunk-2VZW4XPK.js";import{c as Zt,d as Cn}from"./chunks/chunk-LA4H6ZSC.js";import{q as Yt,r as Lt}from"./chunks/chunk-P7ZHH2SF.js";import{Q as Jt,l as de,m as jn,p as Vn,q as $n,u as oe}from"./chunks/chunk-PDHBRDTF.js";var er=1,tr=new WeakMap;function bt(e){let t=tr.get(e);if(t!==void 0)return t;let n=e.y.length,o=e.xColNames.length,r=e.q,s=e.terms.length,a=e.terms.reduce((R,_)=>R+_.nLevels,0),l=new Int32Array(a+1),f=new Int32Array(a),c=[],i=[],p=0;e.terms.forEach((R,_)=>{let G=R.columns.length;c.push(p),i.push(G);for(let N=0;N<R.nLevels;N++,p++)l[p]=R.zOffset+N*G,f[p]=_}),l[a]=r;let y=e.terms.map((R,_)=>{let G=i[_],N=new Int32Array(G*G).fill(-1);for(let{row:q,col:W,theta:re}of e.lambda)W>=R.zOffset&&W<R.zOffset+G&&(N[q-R.zOffset+(W-R.zOffset)*G]=re);return N}),m=e.terms.map(R=>e.groups[R.group].codes),u=new Int32Array(n+1);for(let R=0;R<n;R++){let _=0;for(let G of e.terms)for(let N of G.matrix[R])N!==0&&_++;u[R+1]=u[R]+_}let h=u,d=h[n],b=new Int32Array(d),E=new Float64Array(d);{let R=0;for(let _=0;_<n;_++)e.terms.forEach((G,N)=>{let q=G.zOffset+m[N][_]*i[N];G.matrix[_].forEach((W,re)=>{W!==0&&(b[R]=q+re,E[R++]=W)})})}let A=new Map,v=[],S=[],x=(R,_)=>{let G=_*a+R,N=A.get(G);return N===void 0&&(N=v.length,A.set(G,N),v.push(R),S.push(_)),N};for(let R=0;R<a;R++)x(R,R);let g=new Int32Array(s),w=s*(s+1)/2,k=new Int32Array(n*w);for(let R=0;R<n;R++){for(let _=0;_<s;_++)g[_]=c[_]+m[_][R];for(let _=0;_<s;_++)for(let G=0;G<=_;G++)k[R*w+_*(_+1)/2+G]=x(g[_],g[G])}let L=v.length,D=Array.from({length:L},(R,_)=>_).sort((R,_)=>S[R]-S[_]||v[R]-v[_]),M=new Int32Array(L);D.forEach((R,_)=>M[R]=_);let F=new Int32Array(a+1),U=new Int32Array(L),J=new Int32Array(L+1);D.forEach((R,_)=>{U[_]=v[R],F[S[R]+1]++;let G=i[f[v[R]]]*i[f[S[R]]];J[_+1]=J[_]+G});for(let R=0;R<a;R++)F[R+1]+=F[R];let $=k.map(R=>M[R]),B=nr(e,i,J,$,void 0),P=new Float64Array(r*o),Q=new Float64Array(r),ne=new Float64Array(o);for(let R=0;R<n;R++){let _=e.x[R],G=e.y[R];for(let N=h[R];N<h[R+1];N++){let q=b[N],W=E[N];for(let re=0;re<o;re++)P[q+re*r]+=W*_[re];Q[q]+=W*G}for(let N=0;N<o;N++)ne[N]+=_[N]*G}let ee=new Array(a),I=Array.from({length:a},()=>[]),O=new Int32Array(a).fill(-1);for(let R=0;R<a;R++){let _=[],G=q=>{q!==R&&O[q]!==R&&(O[q]=R,_.push(q))};for(let q=F[R]+1;q<F[R+1];q++)G(U[q]);for(let q of I[R])for(let W of ee[q])G(W);let N=Int32Array.from(_).sort();ee[R]=N,N.length>0&&I[N[0]].push(R)}let T=new Int32Array(L);{let R=new Int32Array(a);for(let _=0;_<a;_++){let G=0;for(let N of ee[_])R[N]=G,G+=l[N+1]-l[N];for(let N=F[_]+1;N<F[_+1];N++)T[N]=R[U[N]]}}let C=new Int32Array(r+1);for(let R=0;R<a;R++){let _=0;for(let G of ee[R])_+=l[G+1]-l[G];for(let G=l[R];G<l[R+1];G++)C[G+1]=C[G]+(l[R+1]-G)+_}let K=new Int32Array(C[r]),Y=new Int32Array(r+1);for(let R=0;R<a;R++)for(let _=l[R];_<l[R+1];_++){let G=C[_];for(let N=_;N<l[R+1];N++)K[G++]=N;for(let N of ee[R])for(let q=l[N];q<l[N+1];q++)K[G++]=q}for(let R=0;R<r;R++)for(let _=C[R]+1;_<C[R+1];_++)Y[K[_]+1]++;for(let R=0;R<r;R++)Y[R+1]+=Y[R];let j=Y.slice(),V=Y.slice(0,r),z=new Int32Array(j[r]),Z=new Int32Array(j[r]);for(let R=0;R<r;R++)for(let _=C[R]+1;_<C[R+1];_++){let G=V[K[_]]++;z[G]=R,Z[G]=_}let X={n,p:o,q:r,nGroups:a,groupStart:l,groupTerm:f,termNc:i,termTheta:y,blockStart:F,blockRow:U,blockValueStart:J,zz:B,zx:P,zy:Q,xy:ne,rowStart:h,rowCols:b,rowValues:E,colStart:C,colRows:K,rowListStart:j,rowListCol:z,rowListPos:Z,blockPatternOffset:T,rowBlocks:$};return tr.set(e,X),X}function nr(e,t,n,o,r){let s=e.y.length,a=e.terms.length,l=a*(a+1)/2,f=new Float64Array(n[n.length-1]);for(let c=0;c<s;c++){let i=r===void 0?1:r[c];if(i!==0)for(let p=0;p<a;p++){let y=e.terms[p].matrix[c],m=t[p];for(let u=0;u<=p;u++){let h=e.terms[u].matrix[c],d=t[u],b=n[o[c*l+p*(p+1)/2+u]];for(let E=0;E<d;E++){let A=r===void 0?h[E]:i*h[E];if(A!==0)for(let v=0;v<m;v++)f[b+v+E*m]+=y[v]*A}}}}return f}function et(e,t,n){return nr(t,e.termNc,e.blockValueStart,e.rowBlocks,n)}function mt(e,t){let n=new Float64Array(e.n);for(let o=0;o<e.n;o++){let r=0;for(let s=e.rowStart[o];s<e.rowStart[o+1];s++)r+=e.rowValues[s]*t[e.rowCols[s]];n[o]=r}return n}function je(e,t){let n=new Float64Array(e.q);for(let o=0;o<e.n;o++){let r=t[o];for(let s=e.rowStart[o];s<e.rowStart[o+1];s++)n[e.rowCols[s]]+=e.rowValues[s]*r}return n}function xt(e,t){return e.termTheta.map(n=>Float64Array.from(n,o=>o<0?0:t[o]))}function Ie(e,t,n){let o=new Float64Array(e.q);for(let r=0;r<e.nGroups;r++){let s=e.groupTerm[r],a=e.termNc[s],l=t[s],f=e.groupStart[r];for(let c=0;c<a;c++){let i=n[f+c];if(i!==0)for(let p=c;p<a;p++)o[f+p]+=l[p+c*a]*i}}return o}function we(e,t,n,o=1){let r=e.q,s=new Float64Array(r*o);for(let a=0;a<e.nGroups;a++){let l=e.groupTerm[a],f=e.termNc[l],c=t[l],i=e.groupStart[a];for(let p=0;p<o;p++){let y=i+p*r;for(let m=0;m<f;m++){let u=0;for(let h=m;h<f;h++)u+=c[h+m*f]*n[y+h];s[y+m]=u}}}return s}function tt(e,t,n=e.zz){let o=new Float64Array(n.length),r=new Float64Array(64);for(let s=0;s<e.nGroups;s++){let a=e.groupTerm[s],l=e.termNc[a],f=t[a];for(let c=e.blockStart[s];c<e.blockStart[s+1];c++){let i=e.groupTerm[e.blockRow[c]],p=e.termNc[i],y=t[i],m=e.blockValueStart[c];if(p===1&&l===1)o[m]=y[0]*n[m]*f[0];else{let u=r.length>=p*l?r:new Float64Array(p*l);for(let h=0;h<l;h++)for(let d=0;d<p;d++){let b=0;for(let E=h;E<l;E++)b+=n[m+d+E*p]*f[E+h*l];u[d+h*p]=b}for(let h=0;h<l;h++)for(let d=0;d<p;d++){let b=0;for(let E=d;E<p;E++)b+=y[E+d*p]*u[E+h*p];o[m+d+h*p]=b}}if(c===e.blockStart[s])for(let u=0;u<l;u++)o[m+u+u*l]+=1}}return o}function nt(e,t){let{q:n,colStart:o,colRows:r,rowListStart:s,rowListCol:a,rowListPos:l}=e,f=new Float64Array(r.length),c=new Float64Array(n);for(let i=0;i<e.nGroups;i++){let p=e.termNc[e.groupTerm[i]],y=e.groupStart[i];for(let m=0;m<p;m++){let u=y+m;for(let E=e.blockStart[i];E<e.blockStart[i+1];E++){let A=e.blockRow[E],v=e.termNc[e.groupTerm[A]],S=e.blockValueStart[E]+m*v,x=e.groupStart[A];for(let g=A===i?m:0;g<v;g++)c[x+g]=t[S+g]}for(let E=s[u];E<s[u+1];E++){let A=a[E],v=l[E],S=f[v];if(S===0)continue;let x=o[A+1];for(let g=v;g<x;g++)c[r[g]]-=f[g]*S}let h=c[u];if(!(h>0))throw new Error("the penalised least-squares matrix is not positive definite");let d=Math.sqrt(h),b=o[u];f[b]=d,c[u]=0;for(let E=b+1;E<o[u+1];E++){let A=r[E];f[E]=c[A]/d,c[A]=0}}}return f}function ye(e,t,n,o=0){let{q:r,colStart:s,colRows:a}=e;for(let l=0;l<r;l++){let f=s[l],c=n[o+l]/=t[f];if(c!==0)for(let i=f+1;i<s[l+1];i++)n[o+a[i]]-=t[i]*c}}function Ce(e,t,n,o=0){let{q:r,colStart:s,colRows:a}=e;for(let l=r-1;l>=0;l--){let f=s[l],c=n[o+l];for(let i=f+1;i<s[l+1];i++)c-=t[i]*n[o+a[i]];n[o+l]=c/t[f]}}function De(e,t,n){for(let o=0;o<t;o++){let r=o*t,s=n[o];for(let a=0;a<o;a++)s-=e[a+r]*n[a];n[o]=s/e[o+r]}}function Qe(e,t,n){for(let o=t-1;o>=0;o--){let r=n[o]/=e[o+o*t],s=o*t;for(let a=0;a<o;a++)n[a]-=e[a+s]*r}}function sn(e,t){let n=e.slice();for(let o=0;o<t;o++)for(let r=o+1;r<t;r++)n[r+o*t]=0;_t({nrow:t,ncol:t,data:n});for(let o=0;o<t;o++)for(let r=o+1;r<t;r++)n[r+o*t]=n[o+r*t];return n}function an(e,t,n,o={}){let r=At(e,t),{sys:s,t:a,l,rx:f,ws:c,xResiduals:i,res:p,uv:y}=r,{n:m,p:u,q:h}=s,d=Array.from(r.beta),b=Array.from(y),E=Array.from(r.b),A=0;for(let M of p)A+=M*M;let v=0;for(let M of b)v+=M*M;let S=A+v,x=0;for(let M=0;M<h;M++)x+=Math.log(l[s.colStart[M]]);x*=2;let g=0;for(let M=0;M<u;M++)g+=Math.log(f[M+M*u]);g*=2;let w=n?m-u:m,L={deviance:(n?x+g:x)+w*(1+Math.log(2*Math.PI*S/w)),beta:d,u:b,b:E,rss:A,ussq:v,prss:S,logDetL:x,logDetRX:g},D=o.gradient===!0||o.rxInverse===!0?sn(f,u):void 0;return o.rxInverse===!0&&(L.rxInverse=Array.from({length:u},(M,F)=>Array.from({length:u},(U,J)=>D[F+J*u]))),o.gradient===!0&&(L.gradient=Gr(e,s,a,l,t,n,{res:p,uv:y,prss:S,ws:c,xResiduals:i,sInverse:D})),L}function At(e,t){if(t.length!==e.theta.length)throw new RangeError(`theta must have length ${e.theta.length}, not ${t.length}`);if(t.some(x=>!Number.isFinite(x)))throw new RangeError("theta must be finite");let n=bt(e),{n:o,p:r,q:s}=n,a=xt(n,t),l=nt(n,tt(n,a)),f=x=>mt(n,x),c=x=>je(n,x),i=x=>{ye(n,l,x),Ce(n,l,x)},p=we(n,a,n.zx,r);for(let x=0;x<r;x++)ye(n,l,p,x*s);let y=[],m=[];for(let x=0;x<r;x++){let g=p.slice(x*s,(x+1)*s);Ce(n,l,g);let w=Float64Array.from(e.x,D=>D[x]),k=()=>{let D=f(Ie(n,a,g));return w.map((M,F)=>M-D[F])},L=k();for(let D=0;D<er;D++){let M=we(n,a,c(L));for(let F=0;F<s;F++)M[F]-=g[F];i(M);for(let F=0;F<s;F++)g[F]+=M[F];L=k()}y.push(L),m.push(g)}let u=new Float64Array(r*r);for(let x=0;x<r;x++)for(let g=x;g<r;g++){let w=0,k=y[x],L=y[g];for(let F=0;F<o;F++)w+=k[F]*L[F];let D=m[x],M=m[g];for(let F=0;F<s;F++)w+=D[F]*M[F];u[x+g*r]=w,u[g+x*r]=w}let h={nrow:r,ncol:r,data:u};if(r>0&&Te(h)>0)throw new Error("the fixed-effects matrix of the penalised least squares is not positive definite");let d=(x,g)=>{ye(n,l,x);for(let w=0;w<r;w++){let k=g[w];for(let L=0;L<s;L++)k-=p[L+w*s]*x[L];g[w]=k}De(u,r,g),Qe(u,r,g);for(let w=0;w<r;w++){let k=g[w];for(let L=0;L<s;L++)x[L]-=p[L+w*s]*k}Ce(n,l,x)},b=(x,g)=>{let w=f(g),k=new Float64Array(o);for(let L=0;L<o;L++){let D=e.x[L],M=w[L];for(let F=0;F<r;F++)M+=D[F]*x[F];k[L]=e.y[L]-M}return k},E=we(n,a,n.zy),A=Float64Array.from(n.xy);d(E,A);let v=Ie(n,a,E),S=b(A,v);for(let x=0;x<er;x++){let g=we(n,a,c(S));for(let k=0;k<s;k++)g[k]-=E[k];let w=new Float64Array(r);for(let k=0;k<o;k++){let L=e.x[k],D=S[k];for(let M=0;M<r;M++)w[M]+=L[M]*D}d(g,w);for(let k=0;k<s;k++)E[k]+=g[k];for(let k=0;k<r;k++)A[k]+=w[k];v=Ie(n,a,E),S=b(A,v)}return{sys:n,t:a,l,cx:p,rx:u,ws:m,xResiduals:y,res:S,uv:E,beta:A,b:v,solveNormal:d}}function Et(e,t){let{q:n,colStart:o,colRows:r}=e,s=new Float64Array(t.length),a=new Int32Array(n).fill(-1),l=new Float64Array(n),f=new Float64Array(n);for(let c=n-1;c>=0;c--){let i=o[c],p=o[c+1];for(let u=i+1;u<p;u++){let h=r[u];a[h]=c,l[h]=t[u],f[h]=0}for(let u=i+1;u<p;u++){let h=r[u],d=t[u],b=o[h],E=s[b]*d;for(let A=b+1;A<o[h+1];A++){let v=r[A];if(a[v]!==c)continue;let S=s[A];f[v]+=S*d,E+=S*l[v]}f[h]+=E}let y=t[i],m=1/y;for(let u=i+1;u<p;u++){let h=-f[r[u]]/y;s[u]=h,m-=t[u]*h}s[i]=m/y}return s}function rr(e,t,n){let{q:o,colStart:r,colRows:s,rowListStart:a,rowListCol:l,rowListPos:f}=e,c=new Float64Array(t.length),i=new Float64Array(o);for(let p=0;p<e.nGroups;p++){let y=e.termNc[e.groupTerm[p]],m=e.groupStart[p];for(let u=0;u<y;u++){let h=m+u;for(let A=e.blockStart[p];A<e.blockStart[p+1];A++){let v=e.blockRow[A],S=e.termNc[e.groupTerm[v]],x=e.blockValueStart[A]+u*S,g=e.groupStart[v];for(let w=v===p?u:0;w<S;w++)i[g+w]=n[x+w]}for(let A=a[h];A<a[h+1];A++){let v=l[A],S=f[A],x=t[S],g=c[S];if(x===0&&g===0)continue;let w=r[v+1];for(let k=S;k<w;k++)i[s[k]]-=c[k]*x+t[k]*g}let d=r[h],b=t[d],E=i[h]/(2*b);c[d]=E,i[h]=0;for(let A=d+1;A<r[h+1];A++){let v=s[A];c[A]=(i[v]-t[A]*E)/b,i[v]=0}}}return c}function or(e,t,n,o){let{q:r,colStart:s,colRows:a}=e,l=new Float64Array(t.length),f=new Int32Array(r).fill(-1),c=new Float64Array(r),i=new Float64Array(r),p=new Float64Array(r),y=new Float64Array(r);for(let m=r-1;m>=0;m--){let u=s[m],h=s[m+1];for(let v=u+1;v<h;v++){let S=a[v];f[S]=m,c[S]=t[v],i[S]=n[v],p[S]=0,y[S]=0}for(let v=u+1;v<h;v++){let S=a[v],x=t[v],g=n[v],w=s[S],k=o[w]*x,L=l[w]*x+o[w]*g;for(let D=w+1;D<s[S+1];D++){let M=a[D];if(f[M]!==m)continue;let F=o[D],U=l[D];p[M]+=F*x,y[M]+=U*x+F*g,k+=F*c[M],L+=U*c[M]+F*i[M]}p[S]+=k,y[S]+=L}let d=t[u],b=n[u],E=1/d,A=-b/(d*d);for(let v=u+1;v<h;v++){let S=a[v],x=o[v],g=-y[S]/d+p[S]*b/(d*d);l[v]=g,E-=t[v]*x,A-=n[v]*x+t[v]*g}l[u]=A/d-E*b/(d*d)}return l}function Gr(e,t,n,o,r,s,{res:a,uv:l,prss:f,ws:c,xResiduals:i,sInverse:p}){let{n:y,p:m,q:u}=t,h=rt(t,Et(t,o),n),d=new Array(r.length).fill(0);t.termTheta.forEach((x,g)=>{let w=t.termNc[g];for(let k=0;k<w;k++)for(let L=k;L<w;L++)d[x[L+k*w]]+=2*h[g][k+L*w]});let b=new Float64Array(u);for(let x=0;x<y;x++){let g=a[x];for(let w=t.rowStart[x];w<t.rowStart[x+1];w++)b[t.rowCols[w]]+=t.rowValues[w]*g}let E,A;if(s&&m>0){E=new Float64Array(u*m);for(let x=0;x<m;x++)for(let g=0;g<m;g++){let w=p[g+x*m],k=c[g];for(let L=0;L<u;L++)E[L+x*u]+=k[L]*w}A=new Float64Array(m*u);for(let x=0;x<y;x++)for(let g=t.rowStart[x];g<t.rowStart[x+1];g++){let w=t.rowCols[g],k=t.rowValues[g];for(let L=0;L<m;L++)A[L+w*m]+=i[L][x]*k}}let S=2*(s?y-m:y)/f;for(let{row:x,col:g,theta:w}of e.lambda){let k=0;if(E!==void 0&&A!==void 0)for(let L=0;L<m;L++)k+=E[g+L*u]*A[L+x*m];d[w]-=2*k+S*b[x]*l[g]}return d}function rt(e,t,n,o=e.zz){let{colStart:r}=e,s=e.termNc.map(f=>new Float64Array(f*f)),a=new Float64Array(16),l=new Float64Array(16);for(let f=0;f<e.nGroups;f++){let c=e.groupTerm[f],i=e.termNc[c],p=n[c],y=e.groupStart[f];for(let m=e.blockStart[f];m<e.blockStart[f+1];m++){let u=e.blockRow[m],h=e.groupTerm[u],d=e.termNc[h],b=n[h],E=e.blockValueStart[m];if(i===1&&d===1){let x=u===f?r[y]:r[y]+1+e.blockPatternOffset[m],g=t[x]*o[E];s[c][0]+=g*b[0],u!==f&&(s[h][0]+=g*p[0]);continue}let A=d*i;a.length<A&&(a=new Float64Array(A),l=new Float64Array(Math.max(A,d*d,i*i)));for(let x=0;x<i;x++){let g=r[y+x];if(u===f)for(let w=x;w<d;w++){let k=t[g+w-x];a[w+x*d]=k,a[x+w*d]=k}else{let w=g+(i-x)+e.blockPatternOffset[m];for(let k=0;k<d;k++)a[k+x*d]=t[w+k]}}for(let x=0;x<i;x++)for(let g=0;g<d;g++){let w=0;for(let k=g;k<d;k++)w+=b[k+g*d]*o[E+k+x*d];l[g+x*d]=w}let v=s[c];for(let x=0;x<i;x++)for(let g=0;g<i;g++){let w=0;for(let k=0;k<d;k++)w+=a[k+x*d]*l[k+g*d];v[x+g*i]+=w}if(u===f)continue;for(let x=0;x<d;x++)for(let g=0;g<i;g++){let w=0;for(let k=g;k<i;k++)w+=p[k+g*i]*o[E+x+k*d];l[g+x*i]=w}let S=s[h];for(let x=0;x<d;x++)for(let g=0;g<d;g++){let w=0;for(let k=0;k<i;k++)w+=a[x+k*d]*l[k+g*i];S[x+g*d]+=w}}}return s}function Ur(e){let t=[],n=l=>{l.type==="var"?t.includes(l.name)||t.push(l.name):l.type==="binary"?(n(l.left),l.op!=="^"&&n(l.right)):(l.type==="unary"||l.type==="paren"||l.type==="Error")&&n(l.arg)};n(e);let{terms:o}=Bn(e,l=>t.indexOf(l),[]);if(o.length===0)throw new SyntaxError(`invalid formula: no grouping factor in '${Be(e)}'`);let r=o.map((l,f)=>[l,f]).sort((l,f)=>l[0].length-f[0].length||l[1]-f[1]).map(([l])=>l.map(f=>t[f])),s=!1,a=l=>{l.type==="binary"?(l.op==="/"&&(s=!0),a(l.left),a(l.right)):(l.type==="unary"||l.type==="paren"||l.type==="Error")&&a(l.arg)};return a(e),s&&(r=r.map(l=>[...l].reverse()).reverse()),r}function Wr(e){if(e.kind==="factor")return Qn(e);let t=[...new Set(e.values)].sort((r,s)=>r-s),n=[],o=new Map;for(let r of t){let s=nn(r);o.has(s)||(o.set(s,n.length),n.push(s))}return{levels:n,codes:e.values.map(r=>o.get(nn(r)))}}function ln(e,t,n={},o=!1){let r=We(e,!1,!0,o);if(r.response===void 0)throw new TypeError("the formula has no response");let{fixed:s,bars:a}=ft(r.rhs);if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let l={...n.levels===void 0?{}:{levels:n.levels},dropUnusedLevels:!0},f=s??{type:"num",value:1,text:"1"},c={...r,rhs:f},i=yt(r,Object.keys(t),f).variables,p=n.contrasts===void 0?{}:{contrasts:n.contrasts},y=o?{anyResponse:!0}:{},m=Pe(c,t,{...l,...p,...y,extraVariables:i},f),u=[...m.terms.variables,...i.filter(h=>!m.terms.variables.includes(h))];return{parsed:r,bars:a,fixedDesign:m,variables:u,frameOptions:l,responseOptions:y}}function zt(e,t,n){let o=[],r;return n.forEach((s,a)=>{let l=[s.lhs];if(s.double){let c=yt(e,t,s.lhs);l=c.intercept?[{type:"num",value:1,text:"1"}]:[];for(let i of c.terms){let p=i.map(y=>({type:"var",name:c.variables[y]})).reduce((y,m)=>({type:"binary",op:":",left:y,right:m}));l.push({type:"binary",op:"+",left:{type:"num",value:0,text:"0"},right:p})}l.length===0&&(r??=a)}let f=Ur(s.group);for(let c of l)for(let i of f)o.push({lhs:c,groupVariables:i,bar:a})}),{expanded:o,noColumns:r}}function Vt(e,t,n={},o=!1){let{parsed:r,bars:s,fixedDesign:a,variables:l,frameOptions:f,responseOptions:c}=ln(e,t,n,o),{frame:i}=a,p=i.rows.length;if(p===0&&!o)throw new RangeError("0 (non-NA) cases");if(s.length===0)throw new Error("No random effects terms specified in formula");let{expanded:y,noColumns:m}=zt(r,Object.keys(t),s),u=m;if(p===0)throw new RangeError(`Invalid grouping factor specification, ${y[0]?.groupVariables.join(":")??""}`);let h=new Map,d=I=>{let O=I.join(":"),T=h.get(O);if(T!==void 0)return T;let C=I.map(Y=>Wr(i.variables[l.indexOf(Y)])),K;if(C.length===1)K=C[0];else{let Y=new Map;for(let z=0;z<p;z++){let Z=C.map(X=>X.codes[z]);Y.set(Z.join(","),Z)}let j=[...Y.values()].sort((z,Z)=>{for(let X=0;X<z.length;X++)if(z[X]!==Z[X])return z[X]-Z[X];return 0}),V=new Map(j.map((z,Z)=>[z.join(","),Z]));K={levels:j.map(z=>z.map((Z,X)=>C[X].levels[Z]).join(":")),codes:Array.from({length:p},(z,Z)=>V.get(C.map(X=>X.codes[Z]).join(",")))}}return h.set(O,K),K},b=y.map(I=>({...I,name:I.groupVariables.join(":"),factor:d(I.groupVariables)})),E=b;b.some((I,O)=>O>0&&I.factor.levels.length>b[O-1].factor.levels.length)&&(E=b.map((I,O)=>[I,O]).sort((I,O)=>I[0].factor.levels.length-O[0].factor.levels.length||I[1]-O[1]).map(([I])=>I).reverse());let A=[],v=[],S=0,x=0;for(let I of E){let O=A.findIndex(Y=>Y.name===I.name);O<0&&(O=A.length,A.push({name:I.name,levels:I.factor.levels,codes:I.factor.codes}));let T=Pe(r,t,{...f,...c},I.lhs),C=T.colNames.length;C===0&&(u===void 0||I.bar<u)&&(u=I.bar);let K=I.factor.levels.length;v.push({label:`${Be(I.lhs)} | ${I.name}`,group:O,columns:T.colNames,matrix:T.x,nLevels:K,zOffset:S,thetaOffset:x}),S+=K*C,x+=C*(C+1)/2}if(u!==void 0){let I=s[u];throw new Error(`lmer: the random-effects term '${Be(I.lhs)} ${I.double?"||":"|"} ${Be(I.group)}' has no columns`)}if(A.some(I=>I.levels.length<2))throw new RangeError("grouping factors must have > 1 sampled level");let g=o?[]:A.filter(I=>I.levels.length>=p).map(I=>I.name);if(g.length>0)throw new RangeError(`number of levels of each grouping factor must be < number of observations (problems: ${g.join(", ")})`);for(let I of v){let O=I.nLevels*I.columns.length;if(o&&p<O)throw new RangeError(`number of observations (=${p}) < number of random effects (=${O}) for term (${I.label}); the random-effects parameters are probably unidentifiable`);if(!o&&p<=O)throw new RangeError(`number of observations (=${p}) <= number of random effects (=${O}) for term (${I.label}); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable`)}for(let I of v)if(I.matrix.some(O=>O.some(T=>!Number.isFinite(T))))throw new RangeError(`NA/NaN/Inf in the random-effects term (${I.label})`);let w=[],k=[],L=[],D=[],M=[];for(let I of v){let{levels:O,codes:T}=A[I.group],C=I.columns.length,K=Array.from({length:O.length*C},()=>({rows:[],values:[]}));for(let V=0;V<p;V++){let z=I.matrix[V],Z=T[V]*C;for(let X=0;X<C;X++){let R=z[X];if(R===0)continue;let _=K[Z+X];_.rows.push(V),_.values.push(R)}}for(let V=0;V<O.length;V++)for(let z=0;z<C;z++)w.push(K[V*C+z]),k.push(O[V]);let Y=Array.from({length:C},()=>new Array(C).fill(-1)),j=I.thetaOffset;for(let V=0;V<C;V++)for(let z=V;z<C;z++)Y[z][V]=j++,L.push(z===V?1:0),D.push(z===V?0:-1/0);for(let V=0;V<I.nLevels;V++){let z=I.zOffset+V*C;for(let Z=0;Z<C;Z++)for(let X=Z;X<C;X++)M.push({row:z+X,col:z+Z,theta:Y[X][Z]})}}let F=[],U=a.x,J=a.colNames,$=a.assign;if(J.length>0){let I=He(U),O=new Set(I.pivot.slice(I.rank));if(O.size>0){let T=J.map((C,K)=>K).filter(C=>!O.has(C));U=U.map(C=>T.map(K=>C[K])),J=T.map(C=>J[C]),$=T.map(C=>$[C]),F.push(`fixed-effect model matrix is rank deficient so dropping ${O.size} ${O.size===1?"column / coefficient":"columns / coefficients"}`)}}let B=[...a.warnings],P=[];for(let I=0;I<J.length;I++){let O=U.map(K=>K[I]);if(O.every(K=>K===0||K===1))continue;let T=O.reduce((K,Y)=>K+Y,0)/p,C=Math.sqrt(O.reduce((K,Y)=>K+(Y-T)**2,0)/(p-1));P.push(C)}let Q=Math.log(1e3);P.some((I,O)=>Math.abs(Math.log(I))>Q||P.some((T,C)=>C!==O&&Math.abs(Math.log(I/T))>Q))&&B.push(`Some predictor variables are on very different scales: consider rescaling.
3
3
  You may also use (g)lmerControl(autoscale = TRUE) to improve numerical stability.`);let ee={rows:i.rows,y:a.y??new Array(p).fill(0),xColNames:J,x:U,xAssign:[...$],xTermLabels:[...a.terms.termLabels],terms:v,groups:A,q:w.length,z:w,zColLevels:k,theta:L,lower:D,lambda:M,messages:F,warnings:B};if(o){let I=r.responseColumns!==void 0&&r.responseColumns[1]!==r.responseColumns[0];ee.response=I?[i.variables[0],i.variables[1]]:r.responseColumns!==void 0?[i.variables[0],i.variables[0]]:[i.variables[0]]}return ee}var Pt={hessianStep:1e-4,hessianStepFloor:1e-6,stepTolerance:1e-13,stepFloor:1e-10,gradientTolerance:1e-9,releaseTolerance:1e-12},Br=1e-8,sr=.25,ar=1e-6,lr=1e-4,Hr=.001,Qr=500,cr=.75,Xr=1e-8,Kr=50,$t=class extends Error{best;constructor(t,n){super(t),this.best=n}},Yr=3;function cn(e){let t=0;for(let a of e)for(let l of a)t=Math.max(t,Math.abs(l));let n=t===0||!Number.isFinite(t)||t>=2**-256&&t<=2**256?0:Math.floor(Math.log2(t));if(n!==0){let a=cn(e.map(l=>l.map(f=>oe(f,-n))));return{values:a.values.map(l=>oe(l,n)),vectors:a.vectors}}let o=e.length,r=e.map(a=>a.slice()),s=r.map((a,l)=>r.map((f,c)=>l===c?1:0));for(let a=0;a<100;a++){let l=0,f=0;for(let c=0;c<o;c++)for(let i=0;i<o;i++)f+=r[c][i]**2,c!==i&&(l+=r[c][i]**2);if(l<=1e-32*f)break;for(let c=0;c<o-1;c++)for(let i=c+1;i<o;i++){let p=r[c][i];if(p===0)continue;let y=(r[i][i]-r[c][c])/(2*p),m=Math.sign(y||1)/(Math.abs(y)+Math.sqrt(1+y*y)),u=1/Math.sqrt(1+m*m),h=m*u;for(let d=0;d<o;d++){let b=r[d][c],E=r[d][i];r[d][c]=u*b-h*E,r[d][i]=h*b+u*E}for(let d=0;d<o;d++){let b=r[c][d],E=r[i][d];r[c][d]=u*b-h*E,r[i][d]=h*b+u*E}for(let d=0;d<o;d++){let b=s[d][c],E=s[d][i];s[d][c]=u*b-h*E,s[d][i]=h*b+u*E}}}return{values:r.map((a,l)=>a[l]),vectors:r.map((a,l)=>s.map(f=>f[l]))}}var fn=Jt,Xe=(e,t)=>e.reduce((n,o,r)=>n+o*t[r],0);function Zr(e,t,n,o,r){let s=e.length,{values:a,vectors:l}=t,f=l.map(A=>Xe(A,e)),c=A=>{let v=new Array(s).fill(0);return A.forEach((S,x)=>l[x].forEach((g,w)=>v[w]+=S*g)),v},i=tn(a);if(i>=-o&&a.every((A,v)=>A>o||Math.abs(f[v])<=r)){let A=c(a.map((v,S)=>v>o?-f[S]/v:0));if(fn(A)<=n)return{step:A,newton:!0}}let p=Math.max(0,-i),y=A=>Jt(a.map((v,S)=>f[S]/(v+A))),m=Math.max(o,1e-12*Math.max(Je(a.map(Math.abs)),1));if(y(p+m)>=n){let A=p+m,v=p+fn(e)/n+Je(a.map(Math.abs))+m;for(let S=0;S<200&&v-A>1e-15*v;S++){let x=(A+v)/2;y(x)>n?A=x:v=x}return{step:c(a.map((S,x)=>-f[x]/(S+v))),newton:!1}}let u=a.map(A=>A+p<=m),h=c(a.map((A,v)=>u[v]?0:-f[v]/(A+p))),d=a.indexOf(i),b=Math.sqrt(Math.max(0,n*n-Xe(h,h))),E=-Math.sign(f[d])||1;return{step:h.map((A,v)=>A+E*b*l[d][v]),newton:!1}}function ir(e,t,n=new Set){let{diagonal:o,settings:r}=e,s=e.robust===!0,a=t.map((w,k)=>n.has(k)?0:w),l,f=w=>{let k=e.evaluate(w);return s&&Number.isFinite(k.f)&&k.g.every(Number.isFinite)&&(l===void 0||k.f<l.f)&&(l={theta:w.map(L=>L===0?0:L),f:k.f}),k},c=()=>{throw s?new $t(e.failure,l):new Error(e.failure)},{f:i,g:p}=f(a),y=()=>{e.rejectNonFinite&&!Number.isFinite(i)&&c()};y();let m=new Set,u=new Set,h=()=>Math.max(1,...a.map(Math.abs)),d=()=>r.gradientTolerance*Math.max(1,Math.abs(i)),b=sr*h(),E=1/0,A,v=w=>{if(e.hessian!==void 0){let D=e.hessian(a,w,h());return D.some(M=>M.some(F=>!Number.isFinite(F)))&&c(),D}let k=r.hessianStepFloor*h(),L=w.map(D=>{let M=Math.max(r.hessianStep*Math.abs(a[D]),k),F=a.slice();F[D]=a[D]+M;let U=a.slice();U[D]=a[D]-M;let J=f(F).g,$=f(U).g;if(s){let B=w.every(Q=>Number.isFinite(J[Q])),P=w.every(Q=>Number.isFinite($[Q]));if(!B&&!P&&c(),!B||!P)return w.map(Q=>(B?J[Q]-p[Q]:p[Q]-$[Q])/M)}return w.map(B=>(J[B]-$[B])/(2*M))});return w.map((D,M)=>w.map((F,U)=>(L[M][U]+L[U][M])/2))},S=0,x=w=>{let k=r.stepTolerance*h(),L=b<=k;if(s&&L&&w.every(z=>Math.abs(p[z])<=d()))return!0;let D=A===void 0||A.free.length!==w.length||A.free.some((z,Z)=>z!==w[Z]),M=D?v(w):A.h;A=void 0;let F=cn(M);F.values.every(Number.isFinite)||c();let U=ar*Je(F.values.map(Math.abs)),J=w.map(z=>p[z]),{step:$,newton:B}=Zr(J,F,b,U,d());if(s&&!B&&F.values.every(z=>z>U)&&w.every(z=>Math.abs(p[z])<=d())){let z=F.vectors.map(X=>Xe(X,J)),Z=F.vectors[0].map((X,R)=>F.values.reduce((_,G,N)=>_-z[N]/G*F.vectors[N][R],0));if(Je(Z.map(Math.abs))<=Br*h())return!0}let P=a.slice();w.forEach((z,Z)=>P[z]=a[z]+$[Z]);let Q=Je($.map(Math.abs))/h(),ne=f(P),ee=fn($),I=()=>!s||!L||(S++,S<Yr)?!1:w.every(z=>Math.abs(p[z])<=d())?!0:c();if(!Number.isFinite(ne.f)){if(!e.rejectNonFinite)throw new Error(e.failure);return b=Math.max(ee/4,r.stepTolerance*h()),E=1/0,I()}let O=-(Xe(J,$)+Xe($,M.map(z=>Xe(z,$)))/2),T=s?1e-8*Math.max(1,Math.abs(i)):1e-10*Math.abs(i),C=Math.abs(i-ne.f)>T?i-ne.f:-(Xe(J,$)+Xe(w.map(z=>ne.g[z]),$))/2,K=O>0?C/O:C>=0?1:-1;if(K>.1||Q<=r.stepFloor)a=P,i=ne.f,p=ne.g,S=0;else if(I())return!0;if(K<.25?b=Math.max(ee/4,r.stepTolerance*h()):K>.75&&ee>=.99*b&&(b*=2),!B)return E=1/0,!1;let V=w.every(z=>Math.abs(p[z])<=d())&&(Q<=r.stepTolerance||D&&Q<=r.stepFloor&&Q>=.5*E);return K>cr&&K<1/cr&&Q<.5*E&&(A={free:w,h:M}),E=Q,V},g=()=>{if(m.size===0)return!1;let w=a.map((J,$)=>$).filter(J=>!n.has(J)),k=cn(v(w)),L=ar*Je(k.values.map(Math.abs)),D=k.values.indexOf(tn(k.values));if(!(k.values[D]<-L))return!1;let M=k.vectors[D],F=w.filter((J,$)=>m.has(J)&&Math.abs(M[$])>1e-8);if(F.length===0)return!1;for(let J of F)m.delete(J),u.add(J);let U=Hr*h();return w.forEach((J,$)=>a[J]=a[J]+U*M[$]),{f:i,g:p}=f(a),y(),!0};for(let w=0;w<Qr;w++){let k=a.map((F,U)=>U).filter(F=>!m.has(F)&&!n.has(F));if(k.length>0&&!x(k))continue;let L=k.filter(F=>o[F]&&!u.has(F)&&Math.abs(a[F])<lr*h());if(L.length>0){for(let F of L)a[F]=0,m.add(F);({f:i,g:p}=f(a)),y(),E=1/0,A=void 0;continue}let D=r.releaseTolerance*Math.max(1,Math.abs(i)),M=[...m].filter(F=>Math.abs(p[F])>D);if(M.length>0)for(let F of M)m.delete(F),u.add(F);else if(!g()){for(let F of e.columns)if(a[F[0]]<0)for(let U of F)a[U]=-a[U];return{theta:a.map(F=>F===0?0:F),f:i}}b=Math.max(b,sr*lr*h()),E=1/0,A=void 0}return c()}function Nt(e,t,n,o={}){let r,s=(c,i)=>{if(e.robust!==!0)return ir(e,c,i);try{return ir(e,c,i)}catch(p){if(!(p instanceof $t))throw p;r=p.best;return}},a=()=>{if(o.bestOnFailure===!0&&r!==void 0)return{...r,converged:!1};throw new Error(e.failure)},l=s(t);if(l===void 0)return a();let f=l;for(let c=0;c<Kr;c++){let i;for(let y of n){if(y.every(u=>f.theta[u]===0))continue;let m=s(f.theta,new Set(y));m!==void 0&&(i===void 0||m.f<i.f)&&(i=m)}if(i===void 0||!(i.f<f.f-Xr*Math.max(1,Math.abs(f.f))))return{...f,converged:!0};let p=s(i.theta);if(p===void 0)return a();f=p}throw new Error(e.failure)}var Jr=["REML","contrasts","levels"],eo=16*Number.EPSILON,to="lmer: the response is fitted exactly by the fixed effects (a constant response, for example), so the residual variance is 0",no="lmer: the minimisation of the criterion over theta did not converge";function mn(e){let t=[];for(let n of e.terms){let o=n.columns.length,r=n.thetaOffset;for(let s=0;s<o;s++){let a=[];for(let l=s;l<o;l++)a.push(r++);t.push(a)}}return t}var ro=(e,t,n)=>{let o=an(e,t,n,{gradient:!0});return{f:o.deviance,g:o.gradient}};function oo(e,t,n){return{evaluate:o=>n(e,o,t),diagonal:e.lower.map(o=>o===0),columns:mn(e),settings:Pt,rejectNonFinite:!1,failure:no}}function Mt(e){let t=new Array(e.theta.length).fill(0),n=new Array(e.z.length).fill(0);for(let o of e.terms){let r=o.columns.length,s=e.groups[o.group].levels.length,a=Array.from({length:r},(f,c)=>{let i=0;for(let p=0;p<s;p++)for(let y of e.z[o.zOffset+p*r+c].values)i=Math.max(i,Math.abs(y));return i>0&&Number.isFinite(i)?Math.floor(Math.log2(i)):0});for(let f=0;f<s;f++)for(let c=0;c<r;c++)n[o.zOffset+f*r+c]=a[c];let l=o.thetaOffset;for(let f=0;f<r;f++)for(let c=f;c<r;c++)t[l++]=a[c]}return{theta:t,z:n}}function so(e,t,n){let o=Mt(e);if(o.z.every(s=>s===0))return fr(e,t,n);let r={...e,z:e.z.map((s,a)=>({rows:s.rows,values:s.values.map(l=>oe(l,-o.z[a]))})),terms:e.terms.map(s=>({...s,matrix:s.matrix.map(a=>a.map((l,f)=>oe(l,-o.z[s.zOffset+f])))}))};return fr(r,t,n).map((s,a)=>oe(s,-o.theta[a]))}function fr(e,t,n){let o=e.terms.map(r=>{let s=r.columns.length;return Array.from({length:s*(s+1)/2},(a,l)=>r.thetaOffset+l)});return Nt(oo(e,t,n),e.theta,o).theta}function be(e,t,n){if(t===void 0)return;if(typeof t!="object"||t===null||Array.isArray(t))throw new TypeError("options must be an object");let o=Object.keys(t).find(r=>n.includes(r));if(o!==void 0)throw new Error(`${e}: argument '${o}' is not supported`)}function Le(e,t){if(e===void 0)return t;if(typeof e=="boolean")return e;if(typeof e=="number"&&!Number.isNaN(e))return e!==0;throw e===null?new TypeError("argument is of length zero"):new TypeError("argument is not interpretable as logical")}var ao=["weights","offset","subset","naAction","na.action","control","start","verbose","devFunOnly"];function lo(e,t,n){return be("lmer",n,ao),de(n,Jr,"lmer"),ge("lmer",pn(e,t,n,ro))}var mr=new WeakMap;function Ge(e){let t=typeof e=="object"&&e!==null?mr.get(e):void 0;if(t===void 0)throw new TypeError("fit must be a result returned by lmer (not a copy of one)");return t}function un(e,t){return Array.from({length:t},(n,o)=>{let r=0;for(let s of e)r=Math.max(r,Math.abs(s[o]));return r>2**500||r>0&&r<2**-500?Math.floor(Math.log2(r)):0})}function pn(e,t,n,o){let r=n??{},s=Le(r.REML,!0);n={...r,REML:s};let a={};n.contrasts!==void 0&&(a.contrasts=n.contrasts),n.levels!==void 0&&(a.levels=n.levels);let l=Vt(e,t,a);if(l.y.some(j=>!Number.isFinite(j)))throw new RangeError("NA/NaN/Inf in 'y'");let f=l.y.length,c=l.xColNames.length,i=c===0?[]:Xn(He(l.x),l.y),p=l.y.map((j,V)=>j-l.x[V].reduce((z,Z,X)=>z+Z*i[X],0)),y=j=>j.reduce((V,z)=>Math.max(V,Math.abs(z)),0),m=y(p);if(m<=eo*y(l.y))throw new Error(to);let u={...l,y:p},h=m>2**500||m<2**-500?Math.floor(Math.log2(m)):0,d=h===0?u:{...u,y:p.map(j=>oe(j,-h))},b=un(u.x,c),E=b.some(j=>j!==0),A=E?{...d,x:d.x.map(j=>j.map((V,z)=>oe(V,-b[z])))}:d,v=so(A,s,o),S=an(A,v,s,{rxInverse:!0}),x=j=>oe(j,h),g=S.beta.map((j,V)=>(E?oe(x(j),-b[V]):x(j))+i[V]),w=2*Math.PI,k=2*h*Math.LN2,L=E?S.logDetRX+2*b.reduce((j,V)=>j+V,0)*Math.LN2:S.logDetRX,D=S.logDetL+f*(1+Math.log(w*S.prss/f)+k),M=S.logDetL+L+(f-c)*(1+Math.log(w*S.prss/(f-c))+k),F=S.prss/(s?f-c:f),U=x(Math.sqrt(F)),J=S.rxInverse.map((j,V)=>j.map((z,Z)=>oe(F*z,2*h-(E?b[V]+b[Z]:0)))),$=S.rxInverse.map((j,V)=>E?oe(x(Math.sqrt(F*j[V])),-b[V]):x(Math.sqrt(F*j[V]))),B=g.map((j,V)=>j/$[V]),P=u.terms.map(j=>{let V=j.columns.length,z=Array.from({length:V},()=>new Array(V).fill(0)),Z=j.thetaOffset;for(let N=0;N<V;N++)for(let q=N;q<V;q++)z[q][N]=v[Z++];let X=z.map(N=>z.map(q=>F*N.reduce((W,re,le)=>W+re*q[le],0))),R=X.map((N,q)=>Math.sqrt(N[q])),_=R.map(x),G=X.map((N,q)=>N.map((W,re)=>q===re?1:W/(R[q]*R[re])));return{group:u.groups[j.group].name,columns:[...j.columns],sd:_,corr:G}}),Q=u.groups.map((j,V)=>{let z=u.terms.filter(Z=>Z.group===V);return{group:j.name,levels:[...j.levels],columns:z.flatMap(Z=>Z.columns),values:j.levels.map((Z,X)=>z.flatMap(R=>{let _=R.columns.length;return R.columns.map((G,N)=>x(S.b[R.zOffset+X*_+N]))}))}}),ne=u.x.map(j=>j.reduce((V,z,Z)=>V+z*g[Z],0)),ee=A.x.map((j,V)=>A.y[V]-j.reduce((z,Z,X)=>z+Z*S.beta[X],0));u.z.forEach((j,V)=>{let z=S.b[V];z!==0&&j.rows.forEach((Z,X)=>{ne[Z]+=j.values[X]*x(z),ee[Z]-=j.values[X]*z})}),h!==0&&ee.forEach((j,V)=>ee[V]=x(j));let I=-(s?M:D)/2,O=c+v.length+1,T=v.some((j,V)=>u.lower[V]===0&&j<1e-4),C=[...u.messages];T&&C.push("boundary (singular) fit: see help('isSingular')");let K={formula:e,REML:s,nObs:f,rows:[...u.rows],theta:v,fixefNames:[...u.xColNames],fixef:g,coefTable:{estimate:[...g],stdError:$,tValue:B},vcov:J,sigma:U,varCorr:P,ranef:Q,fitted:ne,residuals:ee,logLik:I,df:O,AIC:-2*I+2*O,BIC:-2*I+Math.log(f)*O,deviance:D,REMLcrit:M,singular:T,messages:C,warnings:[...u.warnings]},Y=Object.fromEntries(Object.entries(t).map(([j,V])=>[j,V.slice()]));return mr.set(K,{structure:u,beta0:i,data:t,snapshot:Y,options:n,reml:s,criterion:o,theta:[...v],fixef:[...g],b:S.b.map(x),sigma:U}),K}function ur(e,t,n){let o=new Float64Array(e.zz.length),r=new Float64Array(64);for(let s=0;s<e.nGroups;s++){let a=e.groupTerm[s],l=e.termNc[a],f=n[a];for(let c=e.blockStart[s];c<e.blockStart[s+1];c++){let i=e.groupTerm[e.blockRow[c]],p=e.termNc[i],y=t[i],m=e.blockValueStart[c];r.length<p*l&&(r=new Float64Array(p*l));for(let u=0;u<l;u++)for(let h=0;h<p;h++){let d=0;for(let b=0;b<l;b++)d+=e.zz[m+h+b*p]*f[b+u*l];r[h+u*p]=d}for(let u=0;u<l;u++)for(let h=0;h<p;h++){let d=0;for(let b=0;b<p;b++)d+=y[b+h*p]*r[b+u*p];o[m+h+u*p]=d}}}return o}function kt(e,t,n){let o=new Float64Array(n*n);for(let r=0;r<n;r++)for(let s=0;s<n;s++){let a=t[s+r*n];if(a!==0)for(let l=0;l<n;l++)o[l+r*n]+=e[l+s*n]*a}return o}var hn=(e,t)=>{let n=0;for(let o=0;o<t;o++)n+=e[o+o*t];return n},dn=(e,t)=>Array.from({length:t},(n,o)=>Array.from({length:t},(r,s)=>e[o+s*t]));function pr(e,t,n){let o=At(e,t),{sys:r,t:s,l:a,rx:l,ws:f,xResiduals:c,res:i,uv:p}=o,{n:y,p:m,q:u}=r,h=t.length,d=h+1,b=N=>{let q=new Float64Array(y);for(let W=0;W<y;W++){let re=0;for(let le=r.rowStart[W];le<r.rowStart[W+1];le++)re+=r.rowValues[le]*N[r.rowCols[le]];q[W]=re}return q},E=N=>{let q=new Float64Array(u);for(let W=0;W<y;W++){let re=N[W];if(re!==0)for(let le=r.rowStart[W];le<r.rowStart[W+1];le++)q[r.rowCols[le]]+=r.rowValues[le]*re}return q},A=N=>{let q=new Float64Array(m);for(let W=0;W<y;W++){let re=e.x[W],le=N[W];for(let ae=0;ae<m;ae++)q[ae]+=re[ae]*le}return q},v=N=>{let q=new Float64Array(y);for(let W=0;W<y;W++){let re=e.x[W],le=0;for(let ae=0;ae<m;ae++)le+=re[ae]*N[ae];q[W]=le}return q},S=N=>{ye(r,a,N),Ce(r,a,N)},x=Array.from({length:h},()=>[]),g=Array.from({length:h},()=>[]);for(let{row:N,col:q,theta:W}of e.lambda)x[W].push(N),g[W].push(q);let w=(N,q)=>{let W=new Float64Array(u),re=x[N],le=g[N];for(let ae=0;ae<re.length;ae++)W[re[ae]]+=q[le[ae]];return W},k=(N,q)=>{let W=new Float64Array(u),re=x[N],le=g[N];for(let ae=0;ae<re.length;ae++)W[le[ae]]+=q[re[ae]];return W},L=(N,q,W)=>{let re=x[N],le=g[N],ae=0;for(let ie=0;ie<re.length;ie++)ae+=q[re[ie]]*W[le[ie]];return ae},D=(N,q,W,re,le)=>{for(let ae=0;ae<m;ae++)for(let ie=0;ie<m;ie++){let Me=le*L(N,q[ae],W[ie]);re[ae+ie*m]+=Me,re[ie+ae*m]+=Me}},M=0;for(let N of i)M+=N*N;let F=0;for(let N of p)F+=N*N;let U=M+F,J=n?y-m:y,$=U/J,B=Math.sqrt($),P=0;for(let N=0;N<u;N++)P+=Math.log(a[r.colStart[N]]);P*=2;let Q=0;for(let N=0;N<m;N++)Q+=Math.log(l[N+N*m]);Q*=2;let ee=(n?P+Q:P)+U/$+J*Math.log(2*Math.PI*$),I=sn(l,m),O=Et(r,a),T=E(i),C=c.map(E),K=rt(r,O,s),Y=new Array(h).fill(0),j=(N,q,W)=>{r.termTheta.forEach((re,le)=>{let ae=r.termNc[le];for(let ie=0;ie<ae;ie++)for(let Me=ie;Me<ae;Me++)q[re[Me+ie*ae]]+=W*N[le][ie+Me*ae]})};j(K,Y,2);let V=Array.from({length:h},(N,q)=>-2*L(q,T,p)),z=Array.from({length:h},(N,q)=>{let W=new Float64Array(m*m);return D(q,C,f,W,-1),W});if(n)for(let N=0;N<h;N++)Y[N]+=hn(kt(I,z[N],m),m);let Z=z.map(N=>kt(I,N,m)),X=Array.from({length:d},()=>new Array(d).fill(0));for(let N=0;N<h;N++){let q=r.termTheta.map(se=>Float64Array.from(se,qe=>qe===N?1:0)),W=ur(r,q,s),re=ur(r,s,q);for(let se=0;se<W.length;se++)W[se]+=re[se];let le=rr(r,a,W),ae=or(r,a,le,O),ie=new Array(h).fill(0),Me=rt(r,ae,s),pt=rt(r,O,q);if(j(Me,ie,2),j(pt,ie,2),n&&m>0){let se=[],qe=[];for(let he=0;he<m;he++){let xe=k(N,C[he]),ce=we(r,s,E(b(w(N,f[he]))));for(let ve=0;ve<u;ve++)xe[ve]-=ce[ve];S(xe),se.push(xe);let at=w(N,f[he]),wt=Ie(r,s,xe);for(let ve=0;ve<u;ve++)at[ve]+=wt[ve];let lt=b(at);for(let ve=0;ve<y;ve++)lt[ve]=-lt[ve];qe.push(E(lt))}for(let he=0;he<h;he++){let xe=new Float64Array(m*m);D(he,qe,f,xe,-1),D(he,C,se,xe,-1),ie[he]+=hn(kt(I,xe,m),m)-hn(kt(Z[he],Z[N],m),m)}}let ht=b(w(N,p)),dt=k(N,T),Rt=we(r,s,E(ht));for(let se=0;se<u;se++)dt[se]-=Rt[se];let ze=A(ht);for(let se=0;se<m;se++)ze[se]=-ze[se];let Oe=dt.slice(),Ye=ze.slice();o.solveNormal(Oe,Ye);{let se=b(Ie(r,s,Oe)),qe=v(Ye);for(let ce=0;ce<y;ce++)se[ce]+=qe[ce];let he=we(r,s,E(se)),xe=A(se);for(let ce=0;ce<u;ce++)he[ce]=dt[ce]-he[ce]-Oe[ce];for(let ce=0;ce<m;ce++)xe[ce]=ze[ce]-xe[ce];o.solveNormal(he,xe);for(let ce=0;ce<u;ce++)Oe[ce]+=he[ce];for(let ce=0;ce<m;ce++)Ye[ce]+=xe[ce]}let gt=b(Ie(r,s,Oe)),Tt=v(Ye);for(let se=0;se<y;se++)gt[se]=-(ht[se]+gt[se]+Tt[se]);let Xt=E(gt);for(let se=0;se<h;se++){let qe=-2*(L(se,Xt,p)+L(se,T,Oe));X[se][N]=ie[se]+qe/$}}for(let N=0;N<h;N++){for(let W=N+1;W<h;W++){let re=(X[N][W]+X[W][N])/2;X[N][W]=re,X[W][N]=re}let q=-2*V[N]/($*B);X[N][h]=q,X[h][N]=q}X[h][h]=6*U/($*$)-2*J/$;let R=[...Y.map((N,q)=>N+V[q]/$),-2*U/($*B)+2*J/B],_=dn(I.map(N=>$*N),m),G=[...z.map((N,q)=>dn(kt(Z[q],I,m).map(W=>-$*W),m)),dn(I.map(N=>2*B*N),m)];return{varpar:[...t,B],deviance:ee,gradient:R,phiGradient:Y,prssGradient:V,prss:U,hessian:X,beta:Array.from(o.beta),vcov:_,jacobian:G}}var co=["rhs","joint","confint","level"],io=["ddf"],gn=1e-8,fo=Math.sqrt(Number.EPSILON),mo=1e-8;function gr(e,t){return e.xScale.every(n=>n===0)?t:t.map((n,o)=>oe(n,-e.xScale[o]))}var hr=new WeakMap;function dr(e){if(!Number.isFinite(e))return Number.isNaN(e)?"nan":e>0?"inf":"-inf";let[t,n]=e.toExponential(1).split("e"),o=Number(n);return`${t}e${o<0?"-":"+"}${String(Math.abs(o)).padStart(2,"0")}`}function uo(e){let t=[],n=e.filter(s=>s<-gn),o=e.filter(s=>Math.abs(s)<gn),r=s=>s===1?"eigenvalue":"eigenvalues";return n.length>0&&t.push(`Model failed to converge with ${n.length} negative ${r(n.length)}: ${n.map(dr).join(" ")}`),o.length>0&&t.push(`Model may not have converged with ${o.length} ${r(o.length)} close to zero: ${o.map(dr).join(" ")}`),t}function Ft(e){let t=hr.get(e);if(t!==void 0)return t;let n=Ge(e),o=po(n.structure,n.theta,n.reml,n.beta0);return hr.set(e,o),o}function po(e,t,n,o=[]){let r=0;for(let A of e.y)r=Math.max(r,Math.abs(A));let s=r>2**500||r<2**-500?Math.floor(Math.log2(r)):0,a=s===0?e:{...e,y:e.y.map(A=>oe(A,-s))},l=un(e.x,e.xColNames.length),f=l.every(A=>A===0)?a:{...a,x:a.x.map(A=>A.map((v,S)=>oe(v,-l[S])))},c=pr(f,t,n),i=c.varpar.length,p=c.varpar[i-1],y=p>0&&Number.isFinite(p)?Math.round(Math.log2(p)):0,m=[...Mt(e).theta.map(A=>-A),y],u=c.hessian.map((A,v)=>A.map((S,x)=>oe(S,m[v]+m[x]))),h=on(u,{symmetric:!0}),d=h.values,b=h.vectors,E=Array.from({length:i},()=>new Array(i).fill(0));return d.forEach((A,v)=>{if(A>gn)for(let S=0;S<i;S++){let x=2*b[S][v]/A;for(let g=0;g<i;g++)E[S][g]+=x*b[g][v]}}),E.forEach((A,v)=>A.forEach((S,x)=>A[x]=oe(S,m[v]+m[x]))),{varpar:c.varpar,hessian:c.hessian,hessianEigenvalues:d,vcovVarpar:E,beta:c.beta.map((A,v)=>A+oe(o[v]??0,l[v]-s)),sigma:c.varpar[i-1],vcov:c.vcov,jacobian:c.jacobian,warnings:uo(d),scale:s,xScale:l}}var St=(e,t)=>{let n=0;for(let o=0;o<e.length;o++){let r=e[o];if(r===0)continue;let s=t[o];for(let a=0;a<e.length;a++)n+=r*s[a]*e[a]}return n},Gt=(e,t)=>e.reduce((n,o,r)=>n+o*t[r],0);function wr(e,t,n){return wn(e,gr(e,t),n,e.beta)}function wn(e,t,n,o){if(t.some(Number.isNaN)||Number.isNaN(n))return{estimate:NaN,stdError:NaN,df:NaN,tValue:NaN,pValue:NaN};let r=rn(t),s=r===0?t:t.map(u=>oe(u,-r)),a=oe(n,-r),l=Gt(s,o),f=St(s,e.vcov),c=Math.sqrt(f),i=e.jacobian.map(u=>St(s,u)),p=2*f*f/St(i,e.vcovVarpar),y=(l-oe(a,-e.scale))/c,m=2*Pn(Math.abs(y),p,!1,!1);return{estimate:oe(l,e.scale+r),stdError:oe(c,e.scale+r),df:p,tValue:y,pValue:m}}function Dt(e,t,n,o){let r=e.sigma*e.sigma;return{sumSq:oe(t*r*n,2*e.scale),meanSq:oe(t*r,2*e.scale),numDF:n,denDF:o,F:t,pValue:zn(t,n,o,!1,!1)}}function ho(e){if(e.length===1)return e[0];if(e.every((n,o)=>o===0||n===e[o-1]||Math.abs(n-e[o-1])<mo))return e.reduce((n,o)=>n+o,0)/e.length;if(e.some(n=>n<=2))return 2;let t=e.reduce((n,o)=>n+(o===1/0?1:o/(o-2)),0);return 2*t/(t-e.length)}function go(e,t,n,o){return vn(Ft(e),t,n,o)}function vn(e,t,n,o){let r=t.length;if(r===0)return;let s=t.map(v=>gr(e,v)),a=rn(s.flat()),l=a===0?s:s.map(v=>v.map(S=>oe(S,-a))),f=(n??new Array(r).fill(0)).map(v=>oe(v,-a)),c=f.map(v=>oe(v,-e.scale));if(r===1){let v=l[0];if(!v.some(Number.isNaN)&&!(v.reduce((x,g,w)=>x+g*v.reduce((k,L,D)=>k+e.vcov[w][D]*L,0),0)>0))return;let S=wn(e,v,f[0],e.beta);return Dt(e,S.tValue*S.tValue,1,S.df)}if(l.some(v=>v.some(Number.isNaN))||c.some(Number.isNaN))return Dt(e,NaN,NaN,NaN);let i=l.map(v=>e.vcov.map((S,x)=>v.reduce((g,w,k)=>g+w*e.vcov[k][x],0))),p=on(i.map(v=>l.map(S=>Gt(v,S))),{symmetric:!0}),y=p.values,m=Math.max(fo*y[0],0),u=0;for(;u<r&&y[u]>m;)u++;if(u===0)return;u<r&&c.some(v=>v!==0)&&o.add("Contrast is rank deficient and test may be affected");let h=e.beta;if(c.some(v=>v!==0)){let v=qt(l.map(S=>[...S]));h=e.beta.map((S,x)=>S-Gt(v[x],c))}let d=e.beta.length,b=Array.from({length:u},(v,S)=>Array.from({length:d},(x,g)=>l.reduce((w,k,L)=>w+p.vectors[L][S]*k[g],0)));if(u===1){let v=wn(e,b[0],0,h);return Dt(e,v.tValue*v.tValue,1,v.df)}let E=0,A=[];return b.forEach((v,S)=>{E+=Gt(v,h)**2/y[S];let x=e.jacobian.map(g=>St(v,g));A.push(2*y[S]*y[S]/St(x,e.vcovVarpar))}),Dt(e,E/u,u,ho(A))}function wo(e,t){be("lmerSummary",t,[]),de(t,io,"lmerSummary");let n=t?.ddf??"Satterthwaite";if(n!=="Satterthwaite"&&n!=="lme4")throw new RangeError(`ddf must be "Satterthwaite" or "lme4", not ${JSON.stringify(n)}`);let r=[...Ge(e).structure.xColNames];if(n==="lme4"){let{estimate:f,stdError:c,tValue:i}=e.coefTable;return ge("lmerSummary",{ddf:n,names:r,estimate:[...f],stdError:[...c],tValue:[...i],messages:[],warnings:[]},e)}let s=Ft(e),a=r.length,l=r.map((f,c)=>wr(s,Array.from({length:a},(i,p)=>p===c?1:0),0));return ge("lmerSummary",{ddf:n,names:r,estimate:l.map(f=>f.estimate),stdError:l.map(f=>f.stdError),df:l.map(f=>f.df),tValue:l.map(f=>f.tValue),pValue:l.map(f=>f.pValue),messages:[],warnings:[...s.warnings]},e)}var vo=["collect","checkEstimability","check_estimability","ddf"];function yo(e,t){if(!Array.isArray(e))throw new TypeError("L must be an array of numbers or an array of rows");let n=e.length>0&&Array.isArray(e[0])?e.map(o=>[...o]):[[...e]];for(let o of n){if(!Array.isArray(o)||o.some(r=>typeof r!="number"))throw new TypeError("L must be an array of numbers or an array of rows");if(o.length!==t)throw new TypeError(`each contrast must have ${t} values (one per fixed effect), not ${o.length}`);if(!o.every(Number.isFinite))throw new RangeError("L must not contain NaN or infinite values")}return n}function bo(e,t,n){be("lmerContest",n,vo),de(n,co,"lmerContest"),n??={};let o=Le(n.joint,!0),r=Le(n.confint,!0),a=Ge(e).structure.xColNames.length,l=yo(t,a),f=n.level??.95;if(typeof f!="number"||!(f>0&&f<1))throw new RangeError("level must be a number between 0 and 1");let c=n.rhs??0,i=typeof c=="number"?new Array(l.length).fill(c):[...c];if(i.length!==l.length||i.some(h=>typeof h!="number"))throw new TypeError(`rhs must be a number or have one value per contrast (${l.length})`);if(!i.every(Number.isFinite))throw new RangeError("rhs must not contain NaN or infinite values");let p=new Fe,y=Ft(e);if(y.warnings.forEach(h=>p.add(h)),o){let h=a===0?void 0:go(e,l,i,p),d=b=>h===void 0?[]:[h[b]];return ge("lmerContest",{joint:!0,sumSq:d("sumSq"),meanSq:d("meanSq"),numDF:d("numDF"),denDF:d("denDF"),F:d("F"),pValue:d("pValue"),messages:[],warnings:p.list},e)}let m=a===0?[]:l.map((h,d)=>wr(y,h,i[d])),u={joint:!1,estimate:m.map(h=>h.estimate),stdError:m.map(h=>h.stdError),df:m.map(h=>h.df),tValue:m.map(h=>h.tValue),pValue:m.map(h=>h.pValue),messages:[],warnings:p.list};if(r){let h=m.map(d=>Dn((1-f)/2,d.df,!1,!1));u.lower=m.map((d,b)=>d.estimate-d.stdError*h[b]),u.upper=m.map((d,b)=>d.estimate+d.stdError*h[b])}return ge("lmerContest",u,e)}var xo=Math.log10(2);function Ao(e){let t=new DataView(new ArrayBuffer(8));t.setFloat64(0,e);let n=t.getUint16(0)>>4&2047;if(n>0)return n-1023;let o=t.getUint32(0)&1048575,r=t.getUint32(4);return(o!==0?32+(31-Math.clz32(o)):31-Math.clz32(r))-1074}function Ut(e){let t=1,n=10;for(let o=e;;){if(o&1&&(t*=n),o>>=1,o===0)return t;n*=n}}function Eo(e){let t=Math.floor(e),n=e-t,o=n<.5?t:n>.5?t+1:t%2===0?t:t+1;return o===0?e<0?-0:0:o}function No(e,t=0){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(!Number.isFinite(e)||e===0||t>323)return e;if(t<-308)return 0;if(t===0)return Eo(e);let n=Math.floor(t+.5),o=Math.abs(e);if(xo*(.5+Ao(o))+n>15)return e;let s,a,l;if(n<=308){let p=n>=0?Ut(n):1/Ut(-n),y=o*p;l=Math.floor(y),s=l/p,a=Math.ceil(y)/p}else{let p=Ut(308),y=Ut(n-308),m=o*p*y;l=Math.floor(m),s=l/p/y,a=Math.ceil(m)/p/y}let f=a-o,c=o-s,i=f<c||f===c&&l%2===1?a:s;return e<0?-i:i}function Wt(e,t=7){let n=0;for(let r of e)for(let s of r)Math.abs(s)>n&&(n=Math.abs(s));let o=n>0?Math.max(0,t-Math.log10(n)):t;return e.map(r=>r.map(s=>No(s,o)))}var bn={type:"num",value:1,text:"1"};function yr(e,t,n={}){let o=We(e,!1,!0);if(o.response===void 0)throw new TypeError("the formula has no response");if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let{fixed:r}=ft(o.rhs),s=r??bn,a=yt(o,Object.keys(t),s).variables,l=n.contrasts===void 0?{}:{contrasts:n.contrasts},f=Pe({response:o.response,rhs:s},t,{...n.levels===void 0?{}:{levels:n.levels},dropUnusedLevels:!0,...l,extraVariables:a},s),{terms:c,frame:i}=f,p=f.colNames.map((h,d)=>d);if(p.length>0){let h=He(f.x),d=new Set(h.pivot.slice(h.rank));p=p.filter(b=>!d.has(b))}let y=new Set(c.terms.flat()),m=c.variables.map((h,d)=>({name:h,v:d})).filter(({v:h})=>y.has(h)),u={};for(let{name:h,v:d}of m)i.variables[d].kind==="factor"&&(u[h]=n.contrasts?.[h]??"contr.treatment");return{x:f.x.map(h=>p.map(d=>h[d])),colNames:p.map(h=>f.colNames[h]),assign:p.map(h=>f.assign[h]),intercept:c.intercept,terms:c.terms.map((h,d)=>({label:c.termLabels[d],variables:h.map(b=>c.variables[b])})),variables:m.map(({name:h,v:d})=>({name:h,frame:i.variables[d]})),contrasts:u}}var xn=(e,t=e[0]?.length??0)=>Array.from({length:t},(n,o)=>e.map(r=>r[o]));function yn(e,t,n=t[0]?.length??0){return e.map(o=>{let r=new Array(n).fill(0);return o.forEach((s,a)=>{let l=t[a];for(let f=0;f<n;f++)r[f]+=s*l[f]}),r})}function br(e,t){let n=Array.from({length:t},()=>new Array(t).fill(0));for(let o=0;o<t;o++)for(let r=o;r<t;r++){let s=0;for(let a of e)s+=a[o]*a[r];n[o][r]=s,n[r][o]=s}return n}var Bt=(e,t)=>e.map(n=>t.map(o=>n[o])),vr=1e-6;function xr(e){let t=e.map((r,s)=>r[s]>0?Math.sqrt(r[s]):1),{L:n,U:o}=Mo(e.map((r,s)=>r.map((a,l)=>a/t[s]/t[l])));return{L:n.map((r,s)=>r.map((a,l)=>a*t[s]/t[l])),U:o.map((r,s)=>r.map((a,l)=>a*t[s]*t[l]))}}function Mo(e){let t=e.length,n=Array.from({length:t},(r,s)=>Array.from({length:t},(a,l)=>s===l?1:0)),o=Array.from({length:t},()=>new Array(t).fill(0));for(let r=0;r<t;r++){for(let a=0;a<t;a++){let l=e[r][a];for(let f=0;f<r;f++)l-=n[r][f]*o[f][a];o[r][a]=l}let s=o[r][r];for(let a=r+1;a<t;a++){if(Math.abs(s)<vr){n[a][r]=0;continue}let l=e[a][r];for(let f=0;f<r;f++)l-=n[a][f]*o[f][r];n[a][r]=l/s}}for(let r of[n,o])for(let s of r)s.forEach((a,l)=>{Math.abs(a)<vr&&(s[l]=0)});return{L:n,U:o}}function Ar(e){let t=new Set(e.variables.filter(o=>o.frame.kind==="numeric").map(o=>o.name)),n=o=>o.variables.filter(r=>t.has(r)).sort().join("\0");return e.terms.map(o=>e.terms.map((r,s)=>({b:r,k:s})).filter(({b:r})=>o.variables.every(s=>r.variables.includes(s))&&r.variables.length>o.variables.length&&n(o)===n(r)).map(({k:r})=>r))}var ut=(e,t)=>e.flatMap((n,o)=>n===t+1?[o]:[]);function ko(e,t,n,o,r){return{term:e,rows:n.map(s=>o[s]),columns:[...r],values:n.map(s=>[...t[s]])}}function So(e){let t=e.colNames.length;if(t===0||t===1&&e.intercept)return[];let n=t===1?[[1]]:xn(xr(br(e.x,t)).L,t);return e.terms.map((o,r)=>ko(o.label,n,ut(e.assign,r),e.colNames,e.colNames))}function Fo(e){let t=e.colNames.length,n=Ar(e);return e.terms.map((o,r)=>{let s=ut(e.assign,r);if(n[r].length===0){let m=s.map(u=>Array.from({length:t},(h,d)=>u===d?1:0));return{term:o.label,rows:s.map(u=>e.colNames[u]),columns:[...e.colNames],values:m}}let a=[r,...n[r]].flatMap(m=>ut(e.assign,m)),l=new Set(a),f=[...e.colNames.map((m,u)=>u).filter(m=>!l.has(m)),...a],c=xn(xr(br(Bt(e.x,f),t)).L,t),i=new Set(s),p=f.flatMap((m,u)=>i.has(m)?[u]:[]),y=p.map(m=>{let u=new Array(t).fill(0);return f.forEach((h,d)=>u[h]=c[m][d]),u});return{term:o.label,rows:p.map(m=>e.colNames[f[m]]),columns:[...e.colNames],values:y}})}function Oo(e){let t=e.x.length,n=new Map(e.variables.map(f=>[f.name,f.frame])),o=[],r=[];e.intercept&&(o.push(new Array(t).fill(1)),r.push("(Intercept)"));let s=f=>{let c=n.get(f);return c.kind==="numeric"?[{name:f,values:c.values}]:c.levels.map((i,p)=>({name:f+i,values:c.codes.map(y=>y===p?1:0)}))};for(let f of e.terms){let c=s(f.variables[0]);for(let i of f.variables.slice(1)){let p=[];for(let y of s(i))for(let m of c)p.push({name:`${m.name}:${y.name}`,values:m.values.map((u,h)=>u*y.values[h])});c=p}for(let i of c)o.push(i.values),r.push(i.name)}let a=r.filter((f,c)=>o[c].every(i=>i===0)),l=a.length>0?[`Missing cells for: ${a.join(", ")}.
4
4
  Interpret type III hypotheses with care.`]:[];return{x:Array.from({length:t},(f,c)=>o.map(i=>i[c])),colNames:r,messages:l}}function Ro(e){let t={},n={};for(let{name:c,frame:i}of e.variables)i.kind==="numeric"?t[c]=i.values:(t[c]=i.codes.map(p=>i.levels[p]),n[c]=i.levels);let o=c=>c.reduce((i,p)=>i===void 0?p:{type:"binary",op:"+",left:i,right:p},void 0),r=c=>({type:"var",name:c}),s={response:void 0,rhs:o(e.variables.map(c=>r(c.name)))??bn},a=e.terms.map(c=>c.variables.map(r).reduce((i,p)=>({type:"binary",op:":",left:i,right:p}))),l=Pe(s,t,{levels:n,intercept:e.intercept},o(a)??bn),f=l.colNames.map((c,i)=>i);if(f.length>0){let c=He(l.x);c.rank<f.length&&(f=c.pivot.slice(0,c.rank))}return{x:Bt(l.x,f),colNames:f.map(c=>l.colNames[c]),assign:f.map(c=>l.assign[c])}}var To=1e-8;function Io(e){let t=Object.values(e.contrasts).every(m=>m==="contr.treatment"),n=t?{x:e.x,colNames:e.colNames,assign:e.assign}:Ro(e),o=n.colNames.length,r=Oo(e),s=qt(n.x),a=Wt(xn(yn(s,r.x,r.colNames.length),r.colNames.length)),l=Ar(e),f=e.terms.map((m,u)=>({k:u,order:m.variables.length})).sort((m,u)=>u.order-m.order||m.k-u.k);for(let{k:m}of f){if(l[m].length===0)continue;let u=ut(n.assign,m);if(u.length===0)continue;let h=l[m].flatMap(E=>ut(n.assign,E)),d=Bt(a,u);h.length>0&&(d=Kn(He(Bt(a,h)),d));let b=Wt(d);a.forEach((E,A)=>u.forEach((v,S)=>E[v]=b[A][S]))}let c=new Map(r.colNames.map((m,u)=>[m,u])),i=n.colNames.map(m=>{let u=c.get(m);if(u===void 0)throw new Error(`lmerHypotheses: the column '${m}' of the model matrix is not a column of the full-dummy matrix`);return a[u]}),p=t?void 0:Wt(yn(s,e.x,e.colNames.length));return{hypotheses:e.terms.map((m,u)=>{let h=ut(n.assign,u).filter(b=>i.reduce((E,A)=>E+Math.abs(A[b]),0)>To),d=h.map(b=>i.map(E=>E[b]));return{term:m.label,rows:h.map(b=>n.colNames[b]),columns:[...e.colNames],values:p===void 0?d:yn(d,p,e.colNames.length)}}),messages:r.messages}}function Er(e,t="III"){return e.colNames.length<=1||e.terms.length<=1||t==="I"?{hypotheses:So(e),messages:[]}:t==="II"?{hypotheses:Fo(e),messages:[]}:Io(e)}var Lo={I:"I",II:"II",III:"III",1:"I",2:"II",3:"III"};function jo(e,t){let{structure:n,snapshot:o,options:r}=Ge(e),s={};r.contrasts!==void 0&&(s.contrasts=r.contrasts),r.levels!==void 0&&(s.levels=r.levels);let a=yr(e.formula,o,s);if(a.colNames.length!==n.xColNames.length||a.colNames.some((l,f)=>l!==n.xColNames[f]))throw new Error("lmerAnova: the columns of the hypotheses differ from the fit's fixed effects");return a.terms.length===0?{hypotheses:{hypotheses:[],messages:[]},termLabels:[]}:{hypotheses:Er(a,t),termLabels:a.terms.map(l=>l.label)}}function _o(e,{hypotheses:t},n,o){let r=[],s=[];for(let a of t){let l=a.values.length===0?void 0:vn(e,a.values,void 0,o);l!==void 0&&(r.push(a.term),s.push(l))}for(let a of n)r.includes(a)||(r.push(a),s.push({sumSq:NaN,meanSq:NaN,numDF:NaN,denDF:NaN,F:NaN,pValue:NaN}));return{terms:r,rows:s}}function Co(e,t){let n=Lo[String(t.type??"III")];if(n===void 0)throw new RangeError(`type must be "I", "II" or "III", not ${JSON.stringify(t.type)}`);let{hypotheses:o,termLabels:r}=jo(e,n),s=Ft(e),a=new Fe;s.warnings.forEach(i=>a.add(i));let{terms:l,rows:f}=_o(s,o,r,a),c=i=>f.map(p=>p[i]);return{type:n,terms:l,sumSq:c("sumSq"),meanSq:c("meanSq"),numDF:c("numDF"),denDF:c("denDF"),F:c("F"),pValue:c("pValue"),messages:[...o.messages],warnings:a.list}}function qo(e,t){let n=Object.keys(e);return n.length!==Object.keys(t).length?!1:n.every(o=>{let r=e[o],s=t[o];return s!==void 0&&r.length===s.length&&Array.prototype.every.call(r,(a,l)=>Object.is(a,s[l]))})}function En(e){let t=Ge(e),{snapshot:n,data:o}=t,r=e.formula.slice(0,e.formula.indexOf("~")),s=()=>new Array(e.nObs).fill(1);return{nObs:e.nObs,df:e.df,logLik:e.logLik,reml:e.REML,refitLogLik:()=>pn(e.formula,n,{...t.options,REML:!1},t.criterion).logLik,mixed:{data:o,snapshot:n,rows:e.rows,response:r.replace(/\s+/g,"")},response:()=>e.rows.map(a=>Number(n[r.trim()]?.[a]??NaN)),weights:s,family:"gaussian",link:"identity",offset:()=>new Array(e.nObs).fill(0),x:()=>t.structure.x,randomTerms:e.varCorr}}function An(e,t){return e.length===t.length&&e.every((n,o)=>n===t[o]||Number.isNaN(n)&&Number.isNaN(t[o]))}function Nn(e,t,n){if(t.length<2)throw new TypeError(`${e} needs one fit, or an array of at least two fits to compare`);if(t.some(g=>g.nObs!==t[0].nObs))throw new Error("models were not all fitted to the same size of dataset");let o=t.flatMap(g=>g.mixed===void 0?[]:[g.mixed]),r=o[0];if(r!==void 0){if(o.some(w=>w.data!==r.data))throw new Error("all models must be fit to the same data object");if(o.some(w=>!qo(w.snapshot,r.snapshot)))throw new Error("all models must be fit to the same data object");let g=w=>w.length===r.rows.length&&w.every((k,L)=>k===r.rows[L]);if(o.some(w=>!g(w.rows)))throw new Error("models were not all fitted to the same rows of the data")}let s=t.find(g=>g.mixed!==void 0)??t[0],a=g=>g.mixed!==void 0&&s.mixed!==void 0?g.mixed.response===s.mixed.response:An(g.response(),s.response());if(t.some(g=>g!==s&&!a(g)))throw new Error("models were not all fitted to the same response");if(t.some(g=>g!==s&&!An(g.weights(),s.weights())))throw new Error("models were not all fitted with the same prior weights");let l=new Fe,f=[],c=t.map((g,w)=>`MODEL${w+1}`);if(n.names!==void 0){if(n.names.length!==t.length||n.names.some(g=>typeof g!="string"))throw new TypeError(`names must be ${t.length} strings, one per fit`);new Set(n.names).size===n.names.length?c=[...n.names]:l.add("failed to find model names, assigning generic names")}let i=Le(n.refit,!0),p;i?(t.some(g=>g.reml)&&f.push("refitting model(s) with ML (instead of REML)"),p=t.map(g=>g.reml&&g.refitLogLik!==void 0?g.refitLogLik():g.logLik)):(t.some(g=>g.reml)&&t.some(g=>!g.reml)&&l.add("some models fit with REML = TRUE, some not"),p=t.map(g=>g.logLik));let y=t.map((g,w)=>w).sort((g,w)=>t[g].df-t[w].df||g-w),m=t[0].nObs,u=y.map(g=>t[g].df),h=y.map(g=>p[g]),d=s.weights().map(g=>g!==0),b=g=>g.filter((w,k)=>d[k]),E=(g,w)=>g.family===w.family&&g.link===w.link&&An(b(g.offset()),b(w.offset()))&&Yn(b(g.x()),b(w.x()))&&g.randomTerms.every(k=>w.randomTerms.some(L=>L.group===k.group&&k.columns.every(D=>L.columns.includes(D)))),A=y.map((g,w)=>w>0&&E(t[y[w-1]],t[g])),v=h.map((g,w)=>w===0||!A[w]?NaN:2*Math.max(0,g-h[w-1])),S=u.map((g,w)=>w===0?NaN:g-u[w-1]),x=v.map((g,w)=>w===0||S[w]===0?NaN:_n(g,S[w]/2,2,!1,!1));return{names:y.map(g=>c[g]),npar:u,AIC:h.map((g,w)=>-2*g+2*u[w]),BIC:h.map((g,w)=>-2*g+Math.log(m)*u[w]),logLik:h,deviance:h.map(g=>-2*g),Chisq:v,Df:S,pValue:x,messages:f,warnings:l.list}}function zo(e,t){if(e.length<2)throw new TypeError("lmerAnova needs one fit, or an array of at least two fits to compare");return Nn("lmerAnova",e.map(En),t)}function Vo(e,t){if(Array.isArray(e)){be("lmerAnova",t,["modelNames","model.names"]),de(t,["refit","names"],"lmerAnova");let n=t??{};return ge("lmerAnova",zo(e,{...n,refit:Le(n.refit,!0)}),...e)}return be("lmerAnova",t,["ddf"]),de(t,["type"],"lmerAnova"),ge("lmerAnova",Co(e,t??{}),e)}var Ne=2**-52;function $o(e){switch(e){case"logit":return{linkinv:t=>Math.max(Ne,Math.min(1-Ne,Gn(t,0,1,!0,!1))),muEta:t=>Math.max(Ne,en(t,0,1,!1))};case"probit":return{linkinv:t=>Math.min(1-Ne,Lt(t)),muEta:t=>Yt(t)};case"cauchit":return{linkinv:t=>Math.min(1-Ne,Wn(t,0,1,!0,!1)),muEta:t=>Un(t,0,1,!1)};case"cloglog":return{linkinv:t=>Math.max(Ne,Math.min(1-Ne,-Math.expm1(-Math.exp(t)))),muEta:t=>Math.max(Ne,Math.exp(t-Math.exp(t)))};case"log":return{linkinv:t=>Math.max(Ne,Math.exp(t)),muEta:t=>Math.max(Ne,Math.exp(t))};case"identity":return{linkinv:t=>t,muEta:()=>1};case"inverse":return{linkinv:t=>1/t,muEta:t=>-1/(t*t)};default:return}}function Po(e){switch(e){case"logit":return t=>{let n=en(t,0,1,!1);return n<Ne?0:-n*Math.tanh(t/2)};case"probit":return t=>-t*Yt(t);case"cauchit":return t=>-2*t/(Math.PI*(1+t*t)*(1+t*t));case"cloglog":return t=>{let n=Math.exp(t-Math.exp(t));return n<Ne?0:n*-Math.expm1(t)};case"log":return t=>Math.exp(t)<Ne?0:Math.exp(t);case"identity":return()=>0;case"inverse":return t=>2/(t*t*t);case"sqrt":return()=>2;case"1/mu^2":return t=>.75/(t*t*Math.sqrt(t))}}function Do(e){switch(e){case"binomial":return t=>t*(1-t)<Ne?0:1-2*t;case"poisson":return()=>1;case"Gamma":return t=>2*t;case"inverse.gaussian":return t=>3*t*t;case"gaussian":return()=>0}}function Go(e){switch(e){case"binomial":return t=>Math.max(Ne,t*(1-t));case"poisson":return t=>t;case"Gamma":return t=>t*t;case"inverse.gaussian":return t=>t*t*t;case"gaussian":return()=>1}}var Uo=["binomial","poisson","Gamma","inverse.gaussian","gaussian"];function Nr(e,t){let n=typeof e=="object"&&e!==null?e:{family:e??"gaussian"};if(typeof n.family=="string"&&n.family.startsWith("quasi"))throw new RangeError('"quasi" families cannot be used in glmer');let o=Zn(n,t);if(!Uo.includes(o.family))throw new RangeError(`unknown family "${String(o.family)}"`);let r=o.family,s=o.name;if(r==="gaussian"&&s==="identity")throw new RangeError("family gaussian with the identity link is a linear mixed model: use lmer (R's glmer calls lmer with a deprecation warning)");let a=$o(s);return{family:r,link:s,linkfun:o.link,linkinv:a?.linkinv??o.linkinv,muEta:a?.muEta??o.muEta,variance:Go(r),muEta2:Po(s),varianceDerivative:Do(r),devResid:o.devContribution,glm:o}}var Wo=2**-1022,Ht=e=>Math.abs(e)>=Wo&&Math.abs(e)<1/0;function Ot(e,t){let n=e.glm.muPower,o=e.glm.varPower;if(!(n===void 0||o===void 0||e.family==="binomial"||!(t>0)))return{c:n.c,a:n.a,p:o}}function Mn(e,t){return Ht(e*e)&&Ht(t)}function ot(e,t,n,o){let r=e.variance(o),s=t*n*n/r;if(Mn(n,r)&&Number.isFinite(s))return s;let a=Ot(e,o);return a===void 0?s:t*a.c*a.c*o**(2*a.a-a.p)}function kn(e,t,n,o,r){let s=e.variance(r),a=t*(n-r)*o/s;if(Mn(o,s)&&Number.isFinite(a))return a;let l=Ot(e,r);return l===void 0?a:t*((n-r)*l.c*r**(l.a-l.p))}function Mr(e,t,n,o,r,s){let a=e.muEta(o),l=e.muEta2(o),f=e.variance(r),c=e.varianceDerivative(r),i=n-r,p=t*a*a/f,y=t*(2*a*l/f-a/f*(a/f)*a*c),m=t*i*a/f,u=s?p:p-t*i*(l/f-a/f*(a/f)*c);if(Mn(a,f)&&Number.isFinite(p)&&Number.isFinite(y)&&Number.isFinite(m)&&Number.isFinite(u))return{ww:p,dww:y,score:m,observed:u};let h=Ot(e,r);if(h===void 0)return{ww:p,dww:y,score:m,observed:u};let{c:d,a:b,p:E}=h,A=t*d*d*r**(2*b-E);return{ww:A,dww:t*d*d*d*(2*b-E)*r**(3*b-E-1),score:t*(i*d*r**(b-E)),observed:s?A:A-t*(i*d*d*(b-E)*r**(2*b-E-1))}}function Sn(e,t,n,o){let r=e.variance(o),s=Math.sqrt(t)*(n-o)/Math.sqrt(r),a=Ot(e,o);if(Ht(r)&&Number.isFinite(s)||a===void 0)return s;let l=o**(a.p/4);return(n-o)/l*Math.sqrt(t)/l}function kr(e,t,n,o){let r=e.muEta(n),s=(t-o)/r,a=Ot(e,o);if(Ht(r)||a===void 0)return s;let l=o**(a.a/2);return(t-o)/a.c/l/l}function Sr(e){return e.family==="binomial"&&e.link==="logit"||e.family==="poisson"&&e.link==="log"||e.family==="Gamma"&&e.link==="inverse"||e.family==="inverse.gaussian"&&e.link==="1/mu^2"}function Bo(e){if(e<10)return Cn(e)-Math.log(e)+1/(2*e);let t=1/(e*e);return-t*(1/12-t*(1/120-t*(1/252-t*(1/240-t/132))))}function Ho(e,t,n,o,r,s){let a=t.length,l=new Float64Array(a);switch(e.family){case"binomial":{let f=r!==void 0&&r.some(c=>c>1)?r:o;for(let c=0;c<a;c++){let i=f[c];if(!(i>0))continue;let p=Zt(i*t[c]),y=Zt(i),m=n[c];l[c]=-2*(o[c]/i)*((p===0?0:p/m)-(y-p===0?0:(y-p)/(1-m)))}return l}case"poisson":for(let f=0;f<a;f++)o[f]>0&&(l[f]=-2*o[f]*(t[f]/n[f]-1));return l;case"Gamma":{let f=0;for(let p of o)f+=p;let c=f/s,i=c-2*c*c*Bo(c);for(let p=0;p<a;p++){if(!(o[p]>0))continue;let y=-2*o[p]*(t[p]-n[p])*i,m=n[p]*n[p];l[p]=m<1/0?y/m:y/n[p]/n[p]}return l}case"inverse.gaussian":{let f=0;for(let c of o)c>0&&(f+=c);for(let c=0;c<a;c++){if(!(o[c]>0))continue;let i=-2*o[c]*(t[c]-n[c]),p=n[c]*n[c]*n[c];l[c]=f/s*(Math.abs(p)>=2**-1022&&p<1/0?i/p:i/n[c]/n[c]/n[c])}return l}case"gaussian":{let f=0;for(let c of o)c>0&&f++;for(let c=0;c<a;c++)o[c]>0&&(l[c]=f/s*-2*o[c]*(t[c]-n[c]));return l}}}function Fn(e,t,n){let o=e.y.length,r=e.family,s=Sr(r),a=new Float64Array(o),l=new Float64Array(o),f=new Float64Array(o),c=new Float64Array(o);for(let i=0;i<o;i++){let p=e.weights[i];if(p===0)continue;let y=Mr(r,p,e.y[i],t[i],n[i],s);a[i]=y.ww,l[i]=y.dww,c[i]=y.score,f[i]=y.observed}return{ww:a,dww:l,observed:f,score:c}}function Qo(e,t,n,o,r){let s=e.y.length,a=e.terms.length,l=a*(a+1)/2,{colStart:f,blockPatternOffset:c,rowBlocks:i}=t,p=new Float64Array(s),y=e.terms.map(u=>new Float64Array(u.columns.length)),m=new Int32Array(a);for(let u=0;u<s;u++){if(r[u]===0)continue;for(let d=0;d<a;d++){let b=e.terms[d],E=b.columns.length,A=b.matrix[u],v=n[d],S=y[d];for(let x=0;x<E;x++){let g=0;for(let w=x;w<E;w++)g+=v[w+x*E]*A[w];S[x]=g}m[d]=b.zOffset+e.groups[b.group].codes[u]*E}let h=0;for(let d=0;d<a;d++){let b=y[d].length,E=y[d];for(let A=0;A<b;A++){let v=f[m[d]+A];h+=E[A]*E[A]*o[v];for(let S=A+1;S<b;S++)h+=2*E[S]*E[A]*o[v+S-A]}for(let A=0;A<d;A++){let v=y[A],S=v.length,x=i[u*l+d*(d+1)/2+A];for(let g=0;g<S;g++){let w=f[m[A]+g]+(S-g)+c[x];for(let k=0;k<b;k++)h+=2*E[k]*v[g]*o[w+k]}}}p[u]=h}return p}function Fr(e){let{structure:t,sys:n,t:o,l:r,u:s,eta:a,mu:l,devResid:f,joint:c}=e,{n:i,p,q:y}=n,m=t.family,u=t.x,{ww:h,dww:d,observed:b,score:E}=Fn(t,a,l),A=Ho(m,t.y,l,t.weights,t.size,f),v=Et(n,r),S=Qo(t,n,o,v,d),x=new Float64Array(i);for(let $=0;$<i;$++)t.weights[$]!==0&&(x[$]=d[$]*S[$]+A[$]*m.muEta(a[$]));let g=t.theta.length,w=new Array(g+(c?0:p)).fill(0),k=rt(n,v,o,et(n,t,h));n.termTheta.forEach(($,B)=>{let P=n.termNc[B];for(let Q=0;Q<P;Q++)for(let ne=Q;ne<P;ne++)w[$[ne+Q*P]]+=2*k[B][Q+ne*P]});let L=Sr(m)?r:nt(n,tt(n,o,et(n,t,b))),D=we(n,o,je(n,x));for(let $=0;$<y;$++)D[$]+=2*s[$];let M;if(c){let $=new Float64Array(p);for(let B=0;B<i;B++)for(let P=0;P<p;P++)$[P]+=u[B][P]*x[B];Xo(t,n,o,L,b,D,$),M=mt(n,Ie(n,o,D));for(let B=0;B<i;B++)for(let P=0;P<p;P++)M[B]+=u[B][P]*$[P]}else ye(n,L,D),Ce(n,L,D),M=mt(n,Ie(n,o,D));let F=new Float64Array(i);for(let $=0;$<i;$++)F[$]=x[$]-b[$]*M[$];let U=je(n,F),J=je(n,E);for(let{row:$,col:B,theta:P}of t.lambda)w[P]+=U[$]*s[B]+J[$]*D[B];if(!c)for(let $=0;$<i;$++){let B=F[$];if(B===0)continue;let P=u[$];for(let Q=0;Q<p;Q++)w[g+Q]+=P[Q]*B}return w}function Xo(e,t,n,o,r,s,a){let{n:l,p:f,q:c}=t,i=e.x,p=new Float64Array(c*f);for(let m=0;m<f;m++){let u=we(t,n,je(t,Float64Array.from(i,(h,d)=>r[d]*h[m])));ye(t,o,u),p.set(u,m*c)}let y=new Float64Array(f*f);for(let m=0;m<f;m++)for(let u=m;u<f;u++){let h=0;for(let d=0;d<l;d++)h+=r[d]*i[d][m]*i[d][u];for(let d=0;d<c;d++)h-=p[d+m*c]*p[d+u*c];y[m+u*f]=h,y[u+m*f]=h}if(f>0&&Te({nrow:f,ncol:f,data:y})>0)throw new Error("the fixed-effects matrix of the penalised least squares is not positive definite");ye(t,o,s);for(let m=0;m<f;m++){let u=a[m];for(let h=0;h<c;h++)u-=p[h+m*c]*s[h];a[m]=u}De(y,f,a),Qe(y,f,a);for(let m=0;m<f;m++){let u=a[m];for(let h=0;h<c;h++)s[h]-=p[h+m*c]*u}Ce(t,o,s)}var Ko=20,Yo=500,Zo=1e-13,Jo=1e-10,es=1e-13,ts=1e-7;function Ke(e,t,n,o=1,r={}){if(o!==0&&o!==1)throw new RangeError("nAGQ must be 0 or 1 here");if(t.length!==e.theta.length)throw new RangeError(`theta must have length ${e.theta.length}, not ${t.length}`);if(t.some(b=>!Number.isFinite(b)))throw new RangeError("theta must be finite");let s=bt(e),{n:a,p:l,q:f}=s;if(o===1){if(n===void 0||n.length!==l)throw new RangeError(`beta must have length ${l}`);if(n.some(b=>!Number.isFinite(b)))throw new RangeError("beta must be finite")}let c=o===0,i=e.family,{x:p,y,weights:m,offset:u}=e,h=xt(s,t),d=new Fe;return jt(d,()=>{let b=(B,P)=>{let Q=mt(s,Ie(s,h,B)),ne=new Float64Array(a),ee=new Float64Array(a),I=0;for(let T=0;T<a;T++){let C=p[T],K=u[T]+Q[T];for(let j=0;j<l;j++)K+=C[j]*P[j];ne[T]=K;let Y=i.linkinv(K);ee[T]=Y,I+=i.devResid(y[T],Y,m[T])}let O=0;for(let T of B)O+=T*T;return{u:B,beta:P,eta:ne,mu:ee,devResid:I,pdev:I+O}},E=(B,P)=>{let Q=new Float64Array(a),ne=new Float64Array(a);for(let ee=0;ee<a;ee++){let I=m[ee];if(I===0)continue;let O=i.muEta(B[ee]);Q[ee]=ot(i,I,O,P[ee]),ne[ee]=kn(i,I,y[ee],O,P[ee])}return{ww:Q,score:ne}},A=B=>{let P=nt(s,tt(s,h,et(s,e,B)));if(!c)return{l:P,solve:ee=>{ye(s,P,ee),Ce(s,P,ee)}};let Q=new Float64Array(f*l);for(let ee=0;ee<l;ee++){let I=we(s,h,je(s,Float64Array.from(p,(O,T)=>B[T]*O[ee])));ye(s,P,I),Q.set(I,ee*f)}let ne=new Float64Array(l*l);for(let ee=0;ee<l;ee++)for(let I=ee;I<l;I++){let O=0;for(let T=0;T<a;T++)O+=B[T]*p[T][ee]*p[T][I];for(let T=0;T<f;T++)O-=Q[T+ee*f]*Q[T+I*f];ne[ee+I*l]=O,ne[I+ee*l]=O}if(l>0&&Te({nrow:l,ncol:l,data:ne})>0)throw new Error("the fixed-effects matrix of the penalised least squares is not positive definite");return{l:P,solve:(ee,I)=>{ye(s,P,ee);for(let O=0;O<l;O++){let T=I[O];for(let C=0;C<f;C++)T-=Q[C+O*f]*ee[C];I[O]=T}De(ne,l,I),Qe(ne,l,I);for(let O=0;O<l;O++){let T=I[O];for(let C=0;C<f;C++)ee[C]-=Q[C+O*f]*T}Ce(s,P,ee)}}},v=B=>{let P=0;for(let Q of B)P=Math.max(P,Math.abs(Q));return P},S=Float64Array.from(n??new Array(l).fill(0)),x=B=>{let P,Q=c;if(c){let O=Float64Array.from(e.mustart,(C,K)=>(B?u[K]:0)+i.linkfun(C)),T=O.map(C=>i.linkinv(C));P={u:new Float64Array(f),beta:new Float64Array(l),eta:O,mu:T,devResid:NaN,pdev:1/0}}else P=b(new Float64Array(f),S);let ne=1/0,ee,I=0;for(;;){if(I>=Yo)throw new Error("pwrssUpdate did not converge in (maxit) iterations");let{ww:O,score:T}=E(P.eta,P.mu),C=A(O);ee=C.l;let K=T;Q&&(K=T.map((N,q)=>N+O[q]*(P.eta[q]-u[q])));let Y=we(s,h,je(s,K));if(!Q)for(let N=0;N<f;N++)Y[N]-=P.u[N];let j=new Float64Array(l);if(c)for(let N=0;N<a;N++){let q=p[N],W=K[N];for(let re=0;re<l;re++)j[re]+=q[re]*W}let V=Y.slice(),z=j.slice();C.solve(Y,j),I++;let Z=Q?{u:new Float64Array(f),beta:new Float64Array(l)}:P;if(!Q){let N=Math.max(v(Y)/Math.max(1,v(P.u)),c?v(j)/Math.max(1,v(P.beta)):0);if(N<=Zo||N<Jo&&N>=ne)break;if(N>=ne){let q=0;for(let W=0;W<f;W++)q+=V[W]*Y[W];for(let W=0;W<l;W++)q+=z[W]*j[W];if(q<=es*Math.abs(P.pdev))break}ne=N}let X=()=>{let N=Z.u.map((W,re)=>W+Y[re]),q=c?Z.beta.map((W,re)=>W+j[re]):S;return b(N,q)},R=X(),_=P.pdev,G=Number.isFinite(_)?1e-13*_:0;for(let N=0;N<Ko&&(Number.isNaN(R.pdev)||R.pdev>_+G);N++){for(let q=0;q<f;q++)Y[q]/=2;for(let q=0;q<l;q++)j[q]/=2;R=X()}if(Number.isNaN(R.pdev))throw new Error("PIRLS loop resulted in NaN value");if(R.pdev-_>ts*Math.abs(_))throw new Error("(maxstephalfit) PIRLS step-halvings failed to reduce deviance in pwrssUpdate");P=R,Q=!1}return{cur:P,l:ee,iterations:I}},g;try{g=x(!0)}catch(B){if(!c||!(B instanceof Error)||B.constructor!==Error||u.every(P=>P===0))throw B;try{g=x(!1)}catch{throw B}}let{cur:w,l:k,iterations:L}=g,D=0;for(let B=0;B<f;B++)D+=Math.log(k[s.colStart[B]]);D*=2;let M=Array.from(w.mu),F=Ct(i.glm,y,M,m,e.size,w.devResid,d),U=0;for(let B of w.u)U+=B*B;let J;if(r.rx===!0){let{ww:B}=E(w.eta,w.mu),P=Array.from({length:l},(Q,ne)=>{let ee=we(s,h,je(s,Float64Array.from(p,(I,O)=>B[O]*I[ne])));return ye(s,k,ee),ee});J=Array.from({length:l},(Q,ne)=>Array.from({length:l},(ee,I)=>{let O=0;for(let T=0;T<a;T++)O+=B[T]*p[T][ne]*p[T][I];for(let T=0;T<f;T++)O-=P[ne][T]*P[I][T];return O}))}let $=r.gradient===!0?Fr({structure:e,sys:s,t:h,l:k,u:w.u,eta:w.eta,mu:w.mu,devResid:w.devResid,joint:c}):void 0;return{deviance:D+U+F,beta:Array.from(w.beta),u:Array.from(w.u),b:Array.from(Ie(s,h,w.u)),mu:M,eta:Array.from(w.eta),devResid:w.devResid,ussq:U,pdev:w.devResid+U,ldL2:D,aic:F,iterations:L,warnings:d.list,...J===void 0?{}:{rxtrx:J},...$===void 0?{}:{gradient:$}}})}var Or=new Map;function On(e,t){let n=0,o=1,r=0;for(let s=0;s<t;s++){r+=o*o;let a=(e*o-Math.sqrt(s)*n)/Math.sqrt(s+1);n=o,o=a}return{hk:o,hk1:n,sum:r}}function ns(e){let t=Or.get(e);if(t!==void 0)return t;if(!Number.isInteger(e)||e<1||e>100)throw new RangeError("the number of quadrature points must be an integer from 1 to 100");let n=[],o=Math.sqrt(4*e+2)+.5,r=.01,s=y=>On(y,e).hk,a=e%2===1?r/2:0,l=s(a);for(;a<o&&n.length<Math.floor(e/2);){let y=a+r,m=s(y);if(m===0||Math.sign(l)!==Math.sign(m)){let u=a,h=y,d=l;for(;h-u>1e-6;){let E=(u+h)/2,A=s(E);Math.sign(A)===Math.sign(d)?(u=E,d=A):h=E}let b=(u+h)/2;for(let E=0;E<20;E++){let{hk:A,hk1:v}=On(b,e),S=A/(Math.sqrt(e)*v);if(b-=S,Math.abs(S)<=1e-17*Math.max(1,Math.abs(b)))break}n.push(b)}a=y,l=m}if(n.length!==Math.floor(e/2))throw new Error(`gaussHermiteRule: found ${n.length} positive zeros for K = ${e}`);let f=y=>1/On(y,e).sum,c=[...n.map(y=>-y).reverse(),...e%2===1?[0]:[],...n],i=c.map(y=>f(Math.abs(y))),p={nodes:c,weights:i};return Or.set(e,p),p}function Rn(e,t,n,o,r={}){if(e.terms.length!==1||e.terms[0].columns.length!==1)throw new RangeError("AGQ only defined for a single scalar random-effects term");let s=ns(o),a=Ke(e,t,n,1,{...r.rx===!0?{rx:!0}:{}}),l=e.family,{y:f,weights:c}=e,i=t[0],p=s.nodes.map((b,E)=>Math.log(s.weights[E])+b*b/2),y=0,m=0,u=r.gradient===!0?{...Fn(e,a.eta,a.mu),ge:new Float64Array(f.length),gc:new Float64Array(f.length)}:void 0;if(u!==void 0)for(let b=0;b<f.length;b++)u.ge[b]=-2*u.score[b];let h=e.z.map((b,E)=>{let A=a.u[E],v=b.values.map(k=>k*i),S=1;b.rows.forEach((k,L)=>{c[k]!==0&&(S+=ot(l,c[k],l.muEta(a.eta[k]),a.mu[k])*v[L]*v[L])}),y+=Math.log(S);let x=1/Math.sqrt(S),g=b.rows.map(k=>l.devResid(f[k],a.mu[k],c[k])),w=0;return s.nodes.forEach((k,L)=>{let D=0;if(k!==0){let M=k*x;D=M*(2*A+M),b.rows.forEach((F,U)=>{v[U]!==0&&(D+=l.devResid(f[F],l.linkinv(a.eta[F]+v[U]*M),c[F])-g[U])})}w+=Math.exp(p[L]-D/2)}),m-=2*Math.log(w),u!==void 0&&rs(e,s,p,b.rows,v,A,S,a.eta,u),x}),d;if(u!==void 0){let b=n.length;d=new Array(1+b).fill(0);for(let E=0;E<f.length;E++)for(let A=0;A<b;A++)d[1+A]+=u.ge[E]*e.x[E][A];for(let E of e.z)E.rows.forEach((A,v)=>d[0]+=u.gc[A]*E.values[v])}return{...a,deviance:a.devResid+a.ussq+y+m,ldL2:y,quad:m,condSd:h,...d===void 0?{}:{gradient:d}}}function rs(e,t,n,o,r,s,a,l,f){let c=e.family,{y:i,weights:p}=e,y=Math.sqrt(a),m=o.length,u=o.map(M=>c.devResid(i[M],c.linkinv(l[M]),p[M])),h=t.nodes.length,d=new Float64Array(h),b=new Float64Array(h),E=[];t.nodes.forEach((M,F)=>{let U=M/y,J=U*(2*s+U),$=0,B=new Float64Array(m);o.forEach((P,Q)=>{if(p[P]===0)return;let ne=l[P]+r[Q]*U,ee=c.linkinv(ne);r[Q]!==0&&M!==0&&(J+=c.devResid(i[P],ee,p[P])-u[Q]);let I=kn(c,p[P],i[P],c.muEta(ne),ee);B[Q]=I,$+=I*r[Q]}),d[F]=n[F]-J/2,b[F]=2*(s+U)-2*$,E.push(B)});let A=-1/0;for(let M of d)A=Math.max(A,M);let v=0,S=d.map(M=>Math.exp(M-A));for(let M of S)v+=M;let x=0,g=0;t.nodes.forEach((M,F)=>{S[F]/=v,x+=S[F]*b[F]*M/(2*a*y),g+=S[F]*b[F]});let w=1/a-x,k=1,L=0;o.forEach((M,F)=>{k+=f.observed[M]*r[F]*r[F],L+=f.dww[M]*r[F]*r[F]*r[F]});let D=w*L+g;o.forEach((M,F)=>{let U=0,J=0;t.nodes.forEach((P,Q)=>{let ne=E[Q][F];U+=S[Q]*ne,J+=S[Q]*ne*(s+P/y)});let $=r[F],B=f.dww[M];f.ge[M]=w*B*$*$-2*U+D*-f.observed[M]*$/k,f.gc[M]=w*(B*$*$*s+2*f.ww[M]*$)-2*J+D*(f.score[M]-f.observed[M]*$*s)/k})}function Rr(e,t,n,o){let r;if(typeof e=="string"){if(r=Object.hasOwn(t,e)?t[e]:void 0,r===void 0)throw new RangeError(`object '${e}' not found`)}else r=e;if(!Array.isArray(r)&&!(r instanceof Float64Array))throw new TypeError(`${o} must be an array of numbers or the name of a column`);let s=Array.from(r,a=>{if(a===null)return NaN;if(typeof a!="number")throw new TypeError(`the ${o} must be numeric`);return a});if(s.length!==n)throw new RangeError(`variable lengths differ (found for '(${o})')`);return s}var Tr=(e,t)=>{let n=Object.hasOwn(e,t)?e[t]:void 0;return Array.isArray(n)&&n.some(o=>typeof o=="boolean")};function Ir(e,t,n={}){let o=Nr(n.family,n.link),r=n.nAGQ??1;if(typeof r!="number"||Number.isNaN(r))throw new TypeError("nAGQ must be a number");let s=Math.trunc(r)+0;if(s<0)throw new RangeError("nAGQ must be non-negative");if(s>100)throw new RangeError("nAGQ <= 100L is not TRUE");if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let a=We(e,!1,!0,!0),f=(a.response!==void 0&&Object.hasOwn(t,a.response)?t[a.response]:Object.values(t)[0])?.length??0,c=n.weights===void 0?void 0:Rr(n.weights,t,f,"weights"),i=n.offset===void 0?void 0:Rr(n.offset,t,f,"offset"),p,y=t;if(c!==void 0||i!==void 0){p=[];for(let M=0;M<f;M++)!Number.isNaN(c?.[M]??0)&&!Number.isNaN(i?.[M]??0)&&p.push(M);if(p.length<f){let M=p;y=Object.fromEntries(Object.entries(t).map(([F,U])=>[F,U.length===f?U instanceof Float64Array?Float64Array.from(M,J=>U[J]):M.map(J=>U[J]):U]))}else p=void 0}let{levels:m,contrasts:u}=n,h=Vt(e,y,{...m===void 0?{}:{levels:m},...u===void 0?{}:{contrasts:u}},!0),d=p===void 0?h.rows:h.rows.map(M=>p[M]),b=d.length;if(s>1){if(h.groups.length>1||h.terms[0].columns.length>1)throw new RangeError("nAGQ > 1 is only available for models with a single, scalar random-effects term");if(h.terms.length>1)throw new RangeError("AGQ only defined for a single scalar random-effects term")}let E=h.response,A=a.responseColumns!==void 0;if(A&&o.family!=="binomial")throw new TypeError("can't handle matrix-valued responses: consider using refit()");let v=(M,F,U)=>{if(M.kind==="numeric")return M.values;if(Tr(y,F)&&n.levels?.[F]===void 0)return M.codes.map(J=>M.levels[J]==="TRUE"?1:0);throw new TypeError(U)},S;if(A){let[M,F]=a.responseColumns,U=v(E[0],M,"response must be numeric or factor"),J=v(E[1],F,"response must be numeric or factor");S=U.map(($,B)=>[$,J[B]])}else{let M=E[0],F=a.response;o.family==="binomial"&&M.kind==="factor"&&!(Tr(y,F)&&n.levels?.[F]===void 0)?S=M.codes.map(U=>U===0?0:1):S=v(M,F,o.family==="binomial"?"response must be numeric or factor":"response must be numeric")}if((A?S.flat():S).some(M=>!Number.isFinite(M)))throw new RangeError("NA/NaN/Inf in 'y'");let g=i===void 0?new Array(b).fill(0):d.map(M=>i[M]);if(g.some(M=>!Number.isFinite(M)))throw new RangeError("the offset must be finite");let w=c===void 0?new Array(b).fill(1):d.map(M=>c[M]);if(w.some(M=>M<0))throw new RangeError("all(weights >= 0) is not TRUE");if(w.some(M=>!Number.isFinite(M)))throw new RangeError("non-finite weights not allowed");let k=new Fe;for(let M of h.warnings)k.add(M);let L=jt(k,()=>Jn(o.glm,S,w,!1,k)),{mustart:D}=L;if(o.family==="Gamma"){let M=D.reduce((F,U)=>F+U,0)/D.length;D=D.map(()=>M)}if(new Set(L.y).size<2)throw new RangeError("Response is constant");return{...h,rows:d,y:L.y,family:o,weights:L.weights,offset:g,size:o.family==="binomial"?L.size:void 0,mustart:D,nAGQ:s,warnings:k.list}}var os=["family","link","weights","offset","nAGQ","start","contrasts","levels"],ss=["theta","fixef"],as=["subset","naAction","na.action","control","verbose","devFunOnly","mustart","etastart"],ls="glmer: the minimisation of the deviance did not converge",cs="boundary (singular) fit: see help('isSingular')",Qt=`variance-covariance matrix computed from finite-difference Hessian is
5
5
  not positive definite or contains NA values: falling back to var-cov estimated from RX`,Tn=new WeakMap;function qr(e){return typeof e=="object"&&e!==null&&Tn.has(e)}function st(e){let t=typeof e=="object"&&e!==null?Tn.get(e):void 0;if(t===void 0)throw new TypeError("fit must be a result returned by glmer (not a copy of one)");return t}var is=.001,fs={...Pt,releaseTolerance:1e-10},Lr=1e4,jr=20;function _r(e){let t=e.length,n=new Float64Array(t*t);for(let o=0;o<t;o++)for(let r=0;r<t;r++)n[r+o*t]=e[r][o];if(!n.some(o=>!Number.isFinite(o))&&!(Te({nrow:t,ncol:t,data:n})>0))return _t({nrow:t,ncol:t,data:n}),Array.from({length:t},(o,r)=>Array.from({length:t},(s,a)=>r<=a?n[r+a*t]:n[a+r*t]))}function Cr(e,t){if(e===void 0)return;if(!Array.isArray(e)&&!(e instanceof Float64Array))throw new TypeError(`start.${t} must be an array of numbers`);return Array.from(e,o=>{if(typeof o!="number"||!Number.isFinite(o))throw new TypeError(`start.${t} must be an array of finite numbers`);return o})}function ms(e,t){return Array.from({length:t},(n,o)=>{let r=0;for(let s of e)r=Math.max(r,Math.abs(s[o]));return r>0&&Number.isFinite(r)?Math.floor(Math.log2(r)):0})}function us(e,t,n){return t.every(o=>o===0)&&n.every(o=>o===0)?e:{...e,x:e.x.map(o=>o.map((r,s)=>oe(r,-n[s]))),z:e.z.map((o,r)=>({rows:o.rows,values:o.values.map(s=>oe(s,-t[r]))})),terms:e.terms.map(o=>({...o,matrix:o.matrix.map(r=>r.map((s,a)=>oe(s,-t[o.zOffset+a])))}))}}function ps(e,t){let n=e.length,o=new Float64Array(n*n);for(let r=0;r<n;r++)for(let s=0;s<n;s++)o[s+r*n]=e[s][r]/t;if(o.every(Number.isFinite)&&(n===0||Te({nrow:n,ncol:n,data:o})===0)){for(let r=0;r<n;r++)for(let s=r+1;s<n;s++)o[s+r*n]=0;return o}o.fill(0);for(let r=0;r<n;r++){let s=Math.sqrt(e[r][r]/t);o[r+r*n]=Number.isFinite(s)&&s>0?s:1}return o}function hs(e,t,n){return be("glmer",n,as),de(n,os,"glmer"),de(jn(n,"start"),ss,"glmer","start"),ge("glmer",ds(e,t,n??{}))}function ds(e,t,n){let{family:o,link:r,weights:s,offset:a,nAGQ:l,contrasts:f,levels:c}=n,i=Ir(e,t,{...o===void 0?{}:{family:o},...r===void 0?{}:{link:r},...s===void 0?{}:{weights:s},...a===void 0?{}:{offset:a},...l===void 0?{}:{nAGQ:l},...f===void 0?{}:{contrasts:f},...c===void 0?{}:{levels:c}}),p=i.nAGQ;if(p>1&&i.family.family!=="binomial"&&i.family.family!=="poisson")throw new RangeError(`glmer: nAGQ > 1 is not available for the ${i.family.family} family (lme4 holds the dispersion at 1 there, so its estimates would not be maximum likelihood)`);let y=i.y.length,m=i.xColNames.length,u=i.theta.length,h=i.family.family==="poisson"?i.y.find((M,F)=>i.weights[F]>0&&qn(M)):void 0;if(h!==void 0&&p<=1)throw new RangeError(`glmer: the poisson response has a non-integer count (${h}), where the log-likelihood is -Inf for every parameter: no estimate (R warns "non-integer x = ${h.toFixed(6)}" and returns its starting values)`);let d=Mt(i),b=ms(i.x,m),E=xs(i),A=E===0?i:{...i,y:i.y.map(M=>oe(M,-E)),mustart:i.mustart.map(M=>oe(M,-E)),offset:i.offset.map(M=>M-E*Math.LN2)},v=us(A,d.z,b),S=M=>M.map((F,U)=>oe(F,d.theta[U])),x=M=>M.map((F,U)=>oe(F,-d.theta[U])),g=gs(v),w=()=>new RangeError(`glmer: the scale of the response (or of the prior weights) is outside what the Laplace criterion can resolve: the information about the random effects at the start is ${g} (about 2^-40 to 2^40 is supported; lme4's Laplace criterion leaves out the dispersion, so for poisson, inverse.gaussian and gaussian it is not invariant to the scale of the response)`);if(g>vs)throw w();let k=()=>{let M=S(i.theta);try{Ke(v,M,void 0,0)}catch(H){if(d.theta.every(te=>te===0))throw H;try{Ke(v,i.theta,void 0,0)}catch{throw H}M=[...i.theta]}let F=n.start;if(F!==void 0&&(typeof F!="object"||F===null||Array.isArray(F)))throw new TypeError("start must be an object with theta and fixef");let U=Cr(F?.theta,"theta"),J=Cr(F?.fixef,"fixef");if(p===0&&J!==void 0)throw new RangeError(`'start' has invalid names "fixef"`);if(U!==void 0&&U.length!==u)throw new RangeError(`starting value of 'par' has length not equal to ${u}`);if(U?.some((H,te)=>i.lower[te]===0&&H<0))throw new RangeError("Starting values violate bounds");if(J!==void 0&&J.length!==m)throw new RangeError(`starting value of 'fixef' has length not equal to ${m}`);let $=i.lower.map(H=>H===0),B=mn(i),P=i.terms.map(H=>{let te=H.columns.length;return Array.from({length:te*(te+1)/2},(me,ue)=>H.thetaOffset+ue)}),Q=H=>{try{let te=H();return Number.isFinite(te.deviance)&&te.gradient.every(Number.isFinite)?te:void 0}catch(te){if(te instanceof RangeError||te instanceof Error&&te.constructor===Error)return;throw te}},ne=(H,te)=>({evaluate:me=>{let ue=Q(()=>H(me));return ue===void 0?{f:1/0,g:new Array(te).fill(NaN)}:{f:ue.deviance,g:ue.gradient}},diagonal:[...$,...new Array(te-u).fill(!1)],columns:B,settings:fs,rejectNonFinite:!0,failure:ls,robust:!0}),ee=H=>{let te=Ke(v,H,void 0,0,{gradient:!0});return{deviance:te.deviance,gradient:te.gradient}},I=U===void 0?M:S(U),O=h!==void 0?I:Nt(ne(ee,u),I,P,{bestOnFailure:p>=1}).theta,T=O,C,K;if(p>=1){let H=Ke(v,O,void 0,0,{rx:!0}),te=J?.map((Ae,Ee)=>oe(Ae,b[Ee]))??H.beta,me=i.family.family==="binomial"||i.family.family==="poisson"?1:H.devResid/i.weights.reduce((Ae,Ee)=>Ae+Ee,0),ue=ps(H.rxtrx,me),Ve=Ae=>{let Ee=Float64Array.from(Ae.slice(u));return Qe(ue,m,Ee),te.map((pe,fe)=>pe+Ee[fe])},$e=Ae=>{let Ee=Ae.slice(0,u),pe=p===1?Ke(v,Ee,Ve(Ae),1,{gradient:!0}):Rn(v,Ee,Ve(Ae),p,{gradient:!0}),fe=pe.gradient,_e=Float64Array.from(fe.slice(u));return De(ue,m,_e),{deviance:pe.deviance,gradient:[...fe.slice(0,u),..._e]}},Re=Nt(ne($e,u+m),[...O,...new Array(m).fill(0)],P).theta;T=Re.slice(0,u),C=Ve(Re),K=()=>{let Ae=Re.map((_e,ke)=>{let Se=is*Math.max(1,Math.abs(Re[ke])),Ue=Ln=>{let Ze=Re.slice();return Ze[ke]=Re[ke]+Ln,Q(()=>$e(Ze))?.gradient??new Array(Re.length).fill(NaN)},vt=Ue(Se),ct=Ue(-Se),It=Ue(2*Se),it=Ue(-2*Se);return vt.map((Ln,Ze)=>(8*(vt[Ze]-ct[Ze])-(It[Ze]-it[Ze]))/(12*Se))}),Ee=Ae.map((_e,ke)=>_e.map((Se,Ue)=>(Se+Ae[Ue][ke])/2)),pe=_r(Ee);if(pe===void 0)return;let fe=Array.from({length:m},(_e,ke)=>{let Se=new Float64Array(m);return Se[ke]=1,Qe(ue,m,Se),Se});return Array.from({length:m},(_e,ke)=>Array.from({length:m},(Se,Ue)=>{let vt=0;for(let ct=0;ct<m;ct++){let It=fe[ct][ke];if(It!==0)for(let it=0;it<m;it++)vt+=It*pe[u+ct][u+it]*fe[it][Ue]}return 2*vt}))}}let Y=x(T).map(H=>H===0?0:H),j=p>1?Rn(v,T,C,p,{rx:!0}):Ke(v,T,C,p===0?0:1,{rx:!0}),V=j.beta.map((H,te)=>oe(H,-b[te])),z=j.b.map((H,te)=>oe(H,-d.z[te])),Z=i.family,{y:X,weights:R}=i,_=E===0?j.mu:j.mu.map(H=>oe(H,E)),G=E===0?j.eta:j.eta.map(H=>H+E*Math.LN2),N=X.map((H,te)=>Z.devResid(H,_[te],R[te])),q=N.map((H,te)=>{let me=Math.sqrt(Math.max(0,H));return me===0?0:X[te]>_[te]?me:-me}),W=X.map((H,te)=>Sn(Z,R[te],H,_[te])),re=X.map((H,te)=>kr(Z,H,G[te],_[te])),le=X.map((H,te)=>H-_[te]),ae=N.reduce((H,te)=>H+Math.max(0,te),0),ie=Z.family!=="binomial"&&Z.family!=="poisson",Me=ie?Math.sqrt(As(A,j)):1,pt=Y.some((H,te)=>i.lower[te]===0&&H<1e-4),ht=u+(p>=1?m:0),dt=y<Lr&&ht<jr,Rt=[...i.messages];pt&&dt&&Rt.push(cs);let ze=new Fe;for(let H of i.warnings)ze.add(H);for(let H of j.warnings)ze.add(H);let Oe,Ye="RX";if(K!==void 0&&!pt&&y<Lr&&u+m<jr&&(Oe=K(),Oe===void 0?ze.add(Qt):Ye="hessian"),Oe===void 0){let H=_r(j.rxtrx);Oe=Array.from({length:m},(te,me)=>Array.from({length:m},(ue,Ve)=>Me*Me*(H?.[me][Ve]??NaN)))}let gt=Oe.map((H,te)=>H.map((me,ue)=>oe(me,-b[te]-b[ue]))),Tt=Oe.map((H,te)=>oe(Math.sqrt(H[te]),-b[te])),Xt=V.map((H,te)=>H/Tt[te]),se=j.rxtrx.map((H,te)=>H.map((me,ue)=>oe(me,b[te]+b[ue]))),qe=T.map(H=>H===0?0:H),he=i.terms.map(H=>{let te=H.columns.length,me=Array.from({length:te},()=>new Array(te).fill(0)),ue=H.thetaOffset;for(let pe=0;pe<te;pe++)for(let fe=pe;fe<te;fe++)me[fe][pe]=qe[ue++];let Ve=Array.from({length:te},(pe,fe)=>d.z[H.zOffset+fe]),$e=me.map(pe=>me.map(fe=>pe.reduce((_e,ke,Se)=>_e+ke*fe[Se],0))),Re=$e.map((pe,fe)=>Math.sqrt(pe[fe])),Ae=Re.map((pe,fe)=>oe(pe,-Ve[fe])),Ee=$e.map((pe,fe)=>pe.map((_e,ke)=>fe===ke?1:_e/(Re[fe]*Re[ke])));return{group:i.groups[H.group].name,columns:[...H.columns],sd:Ae,corr:Ee}}),xe=i.groups.map((H,te)=>{let me=i.terms.filter(ue=>ue.group===te);return{group:H.name,levels:[...H.levels],columns:me.flatMap(ue=>ue.columns),values:H.levels.map((ue,Ve)=>me.flatMap($e=>{let Re=$e.columns.length;return $e.columns.map((Ae,Ee)=>z[$e.zOffset+Ve*Re+Ee])}))}}),at=-((E===0?j.deviance:j.ldL2+j.ussq+Ct(Z.glm,X,_,R,i.size,j.devResid,new Fe))+(p>1?j.aic-j.devResid:0))/2+0,wt=m+u+(ie?1:0),lt={formula:e,family:Z.family,link:Z.link,nAGQ:p,nObs:y,rows:[...i.rows],theta:Y,fixefNames:[...i.xColNames],fixef:V,coefTable:{estimate:[...V],stdError:Tt,zValue:Xt},vcov:gt,vcovFrom:Ye,sigma:Me,varCorr:he,ranef:xe,fitted:_,linearPredictor:G,residuals:q,pearsonResiduals:W,workingResiduals:re,responseResiduals:le,deviance:ae,logLik:at,df:wt,AIC:-2*at+2*wt,BIC:-2*at+Math.log(y)*wt,singular:pt,messages:Rt,warnings:ze.list},ve=H=>Array.isArray(H)||H instanceof Float64Array?H.slice():H,Kt={...n};n.weights!==void 0&&(Kt.weights=ve(n.weights)),n.offset!==void 0&&(Kt.offset=ve(n.offset));let Pr=Object.fromEntries(Object.entries(t).map(([H,te])=>[H,te.slice()]));return Tn.set(lt,{structure:i,data:t,snapshot:Pr,options:Kt,theta:[...Y],beta:[...V],rxtrx:se,b:[...z],eta:[...G],mu:[..._],sigma:Me}),lt},L;try{L=k()}catch(M){throw M instanceof Error&&M.constructor===Error&&!(g>=ws)?w():M}let D=-2*L.logLik;if(L.theta.some((M,F)=>M===0&&i.lower[F]===0)&&Number.isFinite(D)&&g<ys*Math.abs(D))throw w();return L}function gs(e){let t=e.family,n=e.mustart.map((r,s)=>e.weights[s]>0?ot(t,e.weights[s],t.muEta(t.linkfun(r)),r):0),o=0;for(let r of e.z){let s=0;r.rows.forEach((a,l)=>s+=n[a]*r.values[l]*r.values[l]),o=Math.max(o,s)}return o}var ws=2**-40,vs=2**40,ys=1e-10,bs=2**-40;function xs(e){if(e.family.family!=="Gamma"||e.family.link!=="log")return 0;let t=1/0,n=0;return e.y.forEach((o,r)=>{!(e.weights[r]>0)||!(o>0)||(t=Math.min(t,o),n=Math.max(n,o))}),t<bs?Math.floor((Math.floor(Math.log2(t))+Math.floor(Math.log2(n)))/2):0}function As(e,t){let n=t.ussq;return e.y.forEach((o,r)=>{let s=t.mu[r],a=e.family.variance(s),l=e.weights[r]*(o-s)**2/a;n+=Number.isFinite(l)&&Math.abs(a)>=2**-1022&&a<1/0?l:Sn(e.family,e.weights[r],o,s)**2}),n/e.y.length}function Es(e,t){let n=e.length;if(n===0)return NaN;let o=(n-1)*t,r=Math.floor(o),s=e[r];return r+1<n?s+(o-r)*(e[r+1]-s):s}function Ns(e,t){be("glmerSummary",t,["correlation","use.hessian","useHessian"]),de(t,[],"glmerSummary"),st(e);let{estimate:n,stdError:o,zValue:r}=e.coefTable,s=e.vcov,a=s.map((c,i)=>c.map((p,y)=>i===y?1:s[i][i]===0||s[y][y]===0?NaN:p/Math.sqrt(s[i][i])/Math.sqrt(s[y][y]))),l=e.pearsonResiduals.map(c=>c/e.sigma).sort((c,i)=>c-i),f=e.family!=="binomial"&&e.family!=="poisson";return ge("glmerSummary",{family:e.family,link:e.link,nAGQ:e.nAGQ,names:[...e.fixefNames],estimate:[...n],stdError:[...o],statistic:[...r],statisticName:f?"t value":"z value",pValue:r.map(c=>Number.isNaN(c)?NaN:2*Lt(-Math.abs(c))),correlation:a,varCorr:e.varCorr.map(c=>({group:c.group,columns:[...c.columns],sd:[...c.sd],corr:c.corr.map(i=>[...i])})),nGroups:e.ranef.map(c=>({group:c.group,levels:c.levels.length})),nObs:e.nObs,logLik:e.logLik,AIC:e.AIC,BIC:e.BIC,df:e.df,minus2LogLik:-2*e.logLik,dfResidual:e.nObs-e.df,residualQuantiles:[0,.25,.5,.75,1].map(c=>Es(l,c)),sigma:e.sigma,messages:[...e.messages],warnings:e.warnings.includes(Qt)?[Qt]:[]},e)}function Ms(e){let{structure:t,rxtrx:n,beta:o}=st(e),r=o.length,s=new Float64Array(r*r);for(let m=0;m<r;m++)for(let u=0;u<r;u++)s[u+m*r]=n[u][m];let a=r>0&&(s.some(m=>!Number.isFinite(m))||Te({nrow:r,ncol:r,data:s})>0),l=Array.from({length:r},(m,u)=>{if(a)return NaN;let h=0;for(let d=u;d<r;d++)h+=s[u+d*r]*o[d];return h}),f=[],c=[],i=[];t.xAssign.forEach((m,u)=>{if(m===0)return;let h=t.xTermLabels[m-1];f[f.length-1]!==h&&(f.push(h),c.push(0),i.push(0)),c[c.length-1]++,i[i.length-1]+=l[u]**2});let p=i.map((m,u)=>m/c[u]),y=e.sigma*e.sigma;return{terms:f,npar:c,sumSq:i,meanSq:p,F:p.map(m=>m/y),messages:[],warnings:[]}}function zr(e){if(typeof e!="object"||e===null)return!1;let t=e;return typeof t.aic=="number"&&typeof t.rank=="number"&&typeof t.family=="string"&&typeof t.link=="string"&&Array.isArray(t.y)&&Array.isArray(t.priorWeights)&&typeof t.model=="object"&&t.model!==null&&Array.isArray(t.model.x)&&Array.isArray(t.model.offset)}function ks(e){let{structure:t,data:n,snapshot:o}=st(e);return{nObs:e.nObs,df:e.df,logLik:e.logLik,reml:!1,mixed:{data:n,snapshot:o,rows:e.rows,response:e.formula.slice(0,e.formula.indexOf("~")).replace(/\s+/g,"")},response:()=>t.y,weights:()=>t.weights,family:e.family,link:e.link,offset:()=>t.offset,x:()=>t.x,randomTerms:e.varCorr}}function Ss(e){let t=e.family==="gaussian"||e.family==="Gamma"||e.family==="inverse.gaussian",n=e.rank+(t?1:0);return{nObs:e.y.length,df:n,logLik:n-e.aic/2,reml:!1,response:()=>e.y,weights:()=>e.priorWeights,family:e.family,link:e.link,offset:()=>e.model.offset,x:()=>e.model.x,randomTerms:[]}}function Fs(e,t){if(Array.isArray(e)){be("glmerAnova",t,["modelNames","model.names"]),de(t,["refit","names"],"glmerAnova");let n=t??{},o=Le(n.refit,!0),r=e;if(r.length<2)throw new TypeError("glmerAnova needs one fit, or an array of at least two fits to compare");let s=r.map(a=>{if(qr(a))return ks(a);if(zr(a))return Ss(a);try{return En(a)}catch(l){throw l instanceof TypeError?new TypeError("each fit must be a result returned by glmer, lmer or glm (not a copy of one)"):l}});if(r.every(zr))throw new TypeError("glmerAnova needs at least one fit of glmer or lmer (glmAnova compares glm fits)");return ge("glmerAnova",Nn("glmerAnova",s,{...n,refit:o}),...r)}return be("glmerAnova",t,[]),de(t,[],"glmerAnova"),ge("glmerAnova",Ms(e),e)}var Os=["newoffset","newdata","reForm","allowNewLevels","randomOnly","type","seFit"],Rs=["newdata","reForm","allowNewLevels","randomOnly","type","seFit"],Vr=["re.form","allow.new.levels","random.only","se.fit","newparams","terms","na.action","naAction"],Ts="se.fit computation uses an approximation to estimate the sampling distribution of the parameters";function Is(e,t){return Object.hasOwn(e,t)?e[t]:void 0}function In(e){return e===null||typeof e=="number"&&Number.isNaN(e)?null:typeof e=="boolean"?e?"TRUE":"FALSE":Hn(e)}function Ls(e){return typeof e=="string"?"character":typeof e=="boolean"?"logical":typeof e}function $r(e,t){let n=e.structure,{newdata:o}=t;if(o!==void 0&&(typeof o!="object"||o===null||Array.isArray(o)))throw new TypeError("newdata must be a data frame (an object of columns)");let r=t.reForm;if(r!=null&&r!==!1&&typeof r!="string")throw new TypeError('reForm must be null, false or a formula string such as "~ (1 | g)"');let s=Le(t.allowNewLevels,!1),a=Le(t.randomOnly,!1),l=t.type??"link";if(l!=="link"&&l!=="response")throw new RangeError(`invalid 'type': ${String(l)}`);let f=Le(t.seFit,!1),c;if(t.newoffset!==void 0){let O=t.newoffset;if(!Array.isArray(O)&&!(O instanceof Float64Array))throw new TypeError("newoffset must be an array of numbers");if(c=Array.from(O,T=>{if(typeof T!="number")throw new TypeError("newoffset must be an array of numbers");return T}),o===void 0)throw new RangeError("newoffset needs newdata (without it the fit's offset is used)");if(e.offsetColumn!==void 0)throw new RangeError(`newoffset cannot be given: the fit's offset is the column '${e.offsetColumn}', read from newdata`);if(c.length!==(Object.values(o)[0]?.length??0))throw new RangeError("newoffset must have one value per row of newdata")}let i=n.rows,p=Object.fromEntries(Object.entries(e.snapshot).map(([O,T])=>[O,T instanceof Float64Array?Float64Array.from(i,C=>T[C]):i.map(C=>T[C])])),y=ln(e.formula,p,e.structureOptions,e.generalised),m=_s(e,y.parsed,y.bars,p,r),u=o??p,h=o===void 0?i.length:Object.values(o)[0]?.length??0,d=O=>{let T=Is(u,O);if(T===void 0)throw new RangeError(`object '${O}' not found`);if(!Array.isArray(T)&&!(T instanceof Float64Array))throw new TypeError(`column '${O}' must be an array`);if(T.length!==h)throw new RangeError(`variable lengths differ (found for '${O}')`);return T},b=(O,T)=>{let C=d(O);if(T.kind==="numeric")return{kind:"numeric",values:Array.from(C,V=>{if(V===null)return NaN;if(typeof V!="number")throw new TypeError(`variable '${O}' was fitted with type "numeric" but type "${Ls(V)}" was supplied`);return V})};let K=new Map(T.levels.map((j,V)=>[j,V])),Y=Array.from(C,j=>{let V=In(j);if(V===null)return-1;let z=K.get(V);if(z===void 0)throw new RangeError(`factor ${O} has new level ${V}`);return z});return{kind:"factor",levels:T.levels,codes:Y}},E=O=>{let T=new Set;O.terms.terms.forEach((Y,j)=>{if(j!==O.terms.responseTerm)for(let V of Y)T.add(V)});let C=[...T].sort((Y,j)=>Y-j);for(let Y of C)d(O.terms.variables[Y]);let K=O.frame.variables.map(()=>{});for(let Y of C)K[Y]=b(O.terms.variables[Y],O.frame.variables[Y]);return K},A=y.fixedDesign,v=A.rowsOf(E(A),h),S=n.xColNames.map(O=>A.colNames.indexOf(O)),x=v.map(O=>S.map(T=>O[T])),g=new Array(h).fill(0);if(o===void 0)g=e.fitOffset??g;else if(e.offsetColumn!==void 0){let O=e.offsetColumn;g=Array.from(d(O),T=>{if(T===null)return NaN;if(typeof T!="number")throw new TypeError(`the offset column '${O}' must be numeric`);return T})}else c!==void 0&&(g=c);for(let O of m)for(let T of O.groupVariables)d(T);let k=m.map(O=>Pe({response:void 0,rhs:O.lhs},p,y.frameOptions,O.lhs)).map(O=>O.rowsOf(E(O),h)),L=m.map(O=>{let T=O.groupVariables.map(C=>d(C));return Array.from({length:h},(C,K)=>T.some(Y=>In(Y[K])===null))});if(!s&&L.some(O=>O.some(Boolean)))throw new RangeError("NAs are not allowed in prediction data for grouping variables unless allow.new.levels is TRUE");let D=m.map((O,T)=>{let C=n.groups[O.group],K=new Map(C.levels.map((z,Z)=>[z,Z])),Y=O.groupVariables.map(z=>d(z)),j=[];return{codes:Array.from({length:h},(z,Z)=>{if(L[T][Z])return-1;let X=Y.map(_=>In(_[Z])).join(":"),R=K.get(X);return R!==void 0?R:(j.includes(X)||j.push(X),-2)}),newLevels:j}});if(!s){let O=D.find(T=>T.newLevels.length>0);if(O!==void 0)throw new RangeError(`new levels detected in newdata: ${js(O.newLevels.join(", "))}`)}let M=n.terms,F=new Array(h).fill(0),U=Array.from({length:h},()=>[]);m.forEach((O,T)=>{let C=D[T].codes,K=k[T];for(let Y=0;Y<h;Y++){let j=C[Y];j<0||O.columns.forEach(({term:V,column:z},Z)=>{let X=M[V],R=X.zOffset+j*X.columns.length+z,_=K[Y][Z];F[Y]+=_*e.b[R],U[Y].push({col:R,value:_})})}});let J=n.xColNames.length,B=x.map((O,T)=>a?0:g[T]+O.reduce((C,K,Y)=>C+K*e.beta[Y],0)).map((O,T)=>{let C=O+F[T];if(!Vn(C,0))return C;let K=a?[]:[1,...x[T]],Y=a?[]:[g[T],...e.beta];for(let{col:j,value:V}of U[T])K.push(V),Y.push(e.b[j]);return $n(K,Y,C)}),P=o===void 0&&r==null&&!a?e.fitted:void 0;P!==void 0&&(B=[...P.eta]);let Q=new Fe,ne;if(f){e.generalised&&Q.add(Ts);let{sys:O,t:T,l:C,cx:K,rx:Y}=e.seFactors(),j=O.q;ne=x.map((V,z)=>{let Z=new Float64Array(j);for(let{col:G,value:N}of U[z])Z[G]+=N;let X=we(O,T,Z);ye(O,C,X);let R=0;for(let G=0;G<j;G++)R+=X[G]*X[G];let _=new Float64Array(J);for(let G=0;G<J;G++){let N=a?0:V[G];for(let q=0;q<j;q++)N-=K[q+G*j]*X[q];_[G]=N}De(Y,J,_);for(let G=0;G<J;G++)R+=_[G]*_[G];if(!(R>=2**-1022&&R<1/0)){let G=0;for(let N of X)G=Math.max(G,Math.abs(N));for(let N of _)G=Math.max(G,Math.abs(N));if(G>0&&Number.isFinite(G)){let N=0;for(let q of X)N+=(q/G)**2;for(let q of _)N+=(q/G)**2;return e.sigma*Math.sqrt(N)*G}}return e.sigma*Math.sqrt(R)})}let ee=B;if(e.generalised&&l==="response"){let O=e.family.glm;ee=P!==void 0?[...P.mu]:B.map(T=>O.linkinv(T)),ne!==void 0&&(ne=ne.map((T,C)=>T*Math.abs(O.muEta(B[C]))))}let I={fit:ee};return ne!==void 0&&(I.seFit=ne),Q.list.length>0&&(I.warnings=Q.list),ge(e.fn,I)}function js(e){if(e.length<=60)return e;let t=e.slice(0,60),n=t.lastIndexOf(",");return n<0?t:`${t.slice(0,n)}, ...`}function _s(e,t,n,o,r){let s=e.structure,a=Object.keys(e.snapshot);if(r===!1)return[];if(r==null){let{expanded:m}=zt(t,a,n);return s.terms.map((u,h)=>{let d=m.find(b=>`${Be(b.lhs)} | ${b.groupVariables.join(":")}`===u.label);return{lhs:d.lhs,groupVariables:d.groupVariables,group:u.group,columns:u.columns.map((b,E)=>({term:h,column:E}))}})}let l=We(r,!1,!0),f=ft(l.rhs);if(f.bars.length===0){if(Be(l.rhs)==="0")return[];throw new Error("No random effects terms specified in formula")}let{expanded:c}=zt(l,a,f.bars),i=m=>s.groups.findIndex(u=>u.name===m.groupVariables.join(":"));if(c.some(m=>i(m)<0))throw new RangeError("grouping factors specified in re.form that were not present in original model");let p=[],y=c.map(m=>{let u=i(m),h={response:void 0,rhs:m.lhs},d=Cs(h,a).filter(v=>!Object.hasOwn(o,v)),b=d.length===0?o:{...o,...Object.fromEntries(d.map(v=>[v,new Array(s.rows.length).fill(0)]))},A=Pe(h,b,{...e.structureOptions.levels===void 0?{}:{levels:e.structureOptions.levels},dropUnusedLevels:!0},m.lhs).colNames.map(v=>{let S=s.terms.findIndex(x=>x.group===u&&x.columns.includes(v));return S<0?(p.includes(v)||p.push(v),{term:-1,column:-1}):{term:S,column:s.terms[S].columns.indexOf(v)}});return{lhs:m.lhs,groupVariables:m.groupVariables,group:u,columns:A}});if(p.length>0)throw new RangeError(`random effects specified in re.form that were not present in original model ${p.join(", ")}`);return y}function Cs(e,t){let n=[],o=r=>{if(r.type==="var")n.includes(r.name)||n.push(r.name);else if(r.type==="binary")o(r.left),o(r.right);else if(r.type==="unary"||r.type==="paren"||r.type==="Error")o(r.arg);else if(r.type==="dot")for(let s of t)n.includes(s)||n.push(s)};return o(e.rhs),n}function qs(e,t){be("lmerPredict",t,Vr),de(t,Rs,"lmerPredict");let n=Ge(e),o=n.structure,r={};return n.options.contrasts!==void 0&&(r.contrasts=n.options.contrasts),n.options.levels!==void 0&&(r.levels=n.options.levels),$r({fn:"lmerPredict",formula:e.formula,generalised:!1,snapshot:n.snapshot,structureOptions:r,structure:o,beta:n.fixef,b:n.b,sigma:n.sigma,seFactors:()=>{let{sys:s,t:a,l,cx:f,rx:c}=At(o,n.theta);return{sys:s,t:a,l,cx:f,rx:c}}},t??{})}function zs(e,t){be("glmerPredict",t,Vr),de(t,Os,"glmerPredict");let n=st(e),o=n.structure,r={};n.options.contrasts!==void 0&&(r.contrasts=n.options.contrasts),n.options.levels!==void 0&&(r.levels=n.options.levels);let s=n.options.offset;return $r({fn:"glmerPredict",formula:e.formula,generalised:!0,snapshot:n.snapshot,structureOptions:r,structure:o,beta:n.beta,b:n.b,sigma:n.sigma,family:o.family,...typeof s=="string"?{offsetColumn:s}:{},fitOffset:o.offset,fitted:{eta:n.eta,mu:n.mu},seFactors:()=>{let a=bt(o),{n:l,p:f,q:c}=a,i=xt(a,n.theta),p=o.family,y=new Float64Array(l);for(let d=0;d<l;d++){let b=o.weights[d];b!==0&&(y[d]=ot(p,b,p.muEta(n.eta[d]),n.mu[d]))}let m=nt(a,tt(a,i,et(a,o,y))),u=new Float64Array(c*f);for(let d=0;d<f;d++){let b=we(a,i,je(a,Float64Array.from(o.x,(E,A)=>y[A]*E[d])));ye(a,m,b),u.set(b,d*c)}let h=new Float64Array(f*f);for(let d=0;d<f;d++)for(let b=0;b<f;b++)h[b+d*f]=n.rxtrx[b][d];return f>0&&Te({nrow:f,ncol:f,data:h})>0&&h.fill(NaN),{sys:a,t:i,l:m,cx:u,rx:h}}},t??{})}export{hs as glmer,Fs as glmerAnova,zs as glmerPredict,Ns as glmerSummary,lo as lmer,Vo as lmerAnova,bo as lmerContest,qs as lmerPredict,wo as lmerSummary,Dr as setWarningHandler};
@@ -0,0 +1,139 @@
1
+ import { type RobustFit, type VcovResult } from "./sandwich.js";
2
+ /**
3
+ * The covariance matrix given to {@link coeftest} and {@link coefci}: a matrix (array of rows), the result of
4
+ * {@link vcovHC} or {@link vcovCL}, or a function of the fit returning either (to pass options, close over them:
5
+ * `(f) => vcovCL(f, { cluster })`).
6
+ *
7
+ * A result (with `names`) is matched to the coefficients by name: the coefficients whose names it has are kept, in the
8
+ * fit's order. A matrix is matched by position: it must be p × p (p the number of coefficients) or k × k (k the number
9
+ * of coefficients that are not aliased, which it then stands for).
10
+ *
11
+ * @category Linear models and ANOVA
12
+ */
13
+ export type VcovArgument = readonly (readonly number[])[] | VcovResult | ((fit: RobustFit) => readonly (readonly number[])[] | VcovResult);
14
+ /**
15
+ * Options for {@link coeftest}.
16
+ *
17
+ * @category Linear models and ANOVA
18
+ */
19
+ export interface CoeftestOptions {
20
+ /** The covariance of the coefficients. Default: the fit's own (R's `vcov(fit)`), with NaN for aliased coefficients. */
21
+ vcov?: VcovArgument;
22
+ /**
23
+ * Degrees of freedom of the t tests. Default: the residual degrees of freedom for an lm fit and `Infinity` (z tests)
24
+ * for a glm fit, of every family (lmtest's default; `glmSummary` uses t tests for the families with an estimated
25
+ * dispersion). A value that is not positive and finite gives z tests.
26
+ */
27
+ df?: number;
28
+ }
29
+ /**
30
+ * Result of {@link coeftest}: R's coefficient table with its attributes.
31
+ *
32
+ * @category Linear models and ANOVA
33
+ */
34
+ export interface CoeftestResult {
35
+ /** The coefficients kept (R's `Estimate`). */
36
+ estimate: number[];
37
+ /** Their standard errors (R's `Std. Error`). */
38
+ stdError: number[];
39
+ /** The t or z statistics. */
40
+ statistic: number[];
41
+ /** Two-sided p-values. */
42
+ pValue: number[];
43
+ /** The names of the coefficients kept (the table's row names). */
44
+ names: string[];
45
+ /** `"t value"` or `"z value"`. */
46
+ statisticName: "t value" | "z value";
47
+ /** The degrees of freedom: as given (or the default), also where z tests are used. */
48
+ df: number;
49
+ /** `"t test of coefficients"` or `"z test of coefficients"`. */
50
+ method: string;
51
+ /** The number of observations of the fit (rows of non-zero weight). */
52
+ nobs: number;
53
+ /** Warnings, including those of a covariance function called. */
54
+ warnings: string[];
55
+ }
56
+ /**
57
+ * Options for {@link coefci}.
58
+ *
59
+ * @category Linear models and ANOVA
60
+ */
61
+ export interface CoefciOptions {
62
+ /**
63
+ * The coefficients: 0-based indices into the coefficients kept, in the order given, or names (the coefficients kept
64
+ * whose names are among them, in their order; unknown names give no row). Default: all.
65
+ */
66
+ parm?: number | string | readonly number[] | readonly string[];
67
+ /** Confidence level, in [0, 1]. Default `0.95`. */
68
+ level?: number;
69
+ /** See {@link CoeftestOptions}. */
70
+ vcov?: VcovArgument;
71
+ /** See {@link CoeftestOptions}. */
72
+ df?: number;
73
+ }
74
+ /**
75
+ * Result of {@link coefci}.
76
+ *
77
+ * @category Linear models and ANOVA
78
+ */
79
+ export interface CoefciResult {
80
+ /** Lower limits. */
81
+ lower: number[];
82
+ /** Upper limits. */
83
+ upper: number[];
84
+ /** The names of the coefficients (the row names). */
85
+ names: string[];
86
+ /** R's column names, such as `["2.5 %", "97.5 %"]`. */
87
+ colNames: [string, string];
88
+ /** Warnings, including those of a covariance function called. */
89
+ warnings: string[];
90
+ }
91
+ /**
92
+ * Tests of the coefficients of an `lm` or `glm` fit with a given covariance matrix. Equivalent to R's
93
+ * `lmtest::coeftest()`.
94
+ *
95
+ * Each coefficient's estimate divided by its standard error (the square root of the covariance's diagonal), with a t
96
+ * test on `df` degrees of freedom or a z test. The default `df` is the residual degrees of freedom of an lm and
97
+ * `Infinity` (z tests) for a glm of every family, as lmtest (unlike `glmSummary`, which uses t tests where the
98
+ * dispersion is estimated). A covariance whose diagonal has a negative, NaN or infinite variance is an error (R gives
99
+ * NaN); only the diagonal is used (the matrix is not checked for symmetry, as R's). For a result of {@link vcovHC} or
100
+ * {@link vcovCL} (or the fit's own covariance) the standard errors come from the covariance in units of powers of 2,
101
+ * so they are right where the matrix holds Infinity or 0 (a response around 1e±300). That record belongs to the matrix
102
+ * object returned: a copy of it (`vcov.map((r) => r.slice())`, `structuredClone`, JSON) loses it and gives the values
103
+ * the copy holds (se 0, z Infinity and p 0 where it holds 0); pass the result or its original matrix instead.
104
+ *
105
+ * @param fit The result of {@link lm} or {@link glm}.
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * const fit = lm([1, 2, 3, 4, 5, 6], [1.1, 2.3, 2.8, 4.5, 4.9, 6.4]);
110
+ * const t = coeftest(fit, { vcov: (f) => vcovHC(f, { type: "HC1" }) });
111
+ * t.stdError; // [0.17608, 0.049777]
112
+ * t.pValue; // [0.72421, 0.0000324]
113
+ * ```
114
+ *
115
+ * @category Linear models and ANOVA
116
+ */
117
+ export declare function coeftest(fit: RobustFit, options?: CoeftestOptions): CoeftestResult;
118
+ /**
119
+ * Confidence intervals of the coefficients of an `lm` or `glm` fit with a given covariance matrix. Equivalent to R's
120
+ * `lmtest::coefci()`.
121
+ *
122
+ * The limits estimate ± q se with the t quantile on `df` degrees of freedom, or the normal quantile (by default for a
123
+ * glm of every family, as lmtest). `parm` takes 0-based indices (R: 1-based) or names. The standard errors as in
124
+ * {@link coeftest}: from the covariance in units of powers of 2 for a result of {@link vcovHC} or {@link vcovCL} or
125
+ * its original matrix, not for a copy of the matrix (which gives intervals of width 0 where it holds 0).
126
+ *
127
+ * @param fit The result of {@link lm} or {@link glm}.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * const fit = lm([1, 2, 3, 4, 5, 6], [1.1, 2.3, 2.8, 4.5, 4.9, 6.4]);
132
+ * const ci = coefci(fit, { vcov: (f) => vcovHC(f) });
133
+ * ci.colNames; // ["2.5 %", "97.5 %"]
134
+ * ci.lower; // [-0.51418, 0.84206]
135
+ * ```
136
+ *
137
+ * @category Linear models and ANOVA
138
+ */
139
+ export declare function coefci(fit: RobustFit, options?: CoefciOptions): CoefciResult;