@numero-uno-jpn/rstats 0.14.0 → 0.15.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 (45) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.ja.md +5 -5
  3. package/README.md +5 -5
  4. package/dist/chunks/{chunk-XCAMWVNO.js → chunk-4EOKNOXP.js} +1 -1
  5. package/dist/chunks/{chunk-7PSGKKOP.js → chunk-4XGKAHD3.js} +2 -2
  6. package/dist/chunks/{chunk-7CPM3M6Q.js → chunk-7GEYDIQC.js} +2 -2
  7. package/dist/chunks/{chunk-A76XBA3D.js → chunk-7XHAE2LG.js} +1 -1
  8. package/dist/chunks/{chunk-ZUESNU7W.js → chunk-A3XZUJCP.js} +2 -2
  9. package/dist/chunks/{chunk-FBDZ6B2G.js → chunk-AEFWSUZ2.js} +2 -2
  10. package/dist/chunks/{chunk-IBJ7RI44.js → chunk-CJ5ETQCK.js} +2 -2
  11. package/dist/chunks/{chunk-IGSR4GIF.js → chunk-CMPDE3SZ.js} +2 -2
  12. package/dist/chunks/chunk-GZTLODPK.js +3 -0
  13. package/dist/chunks/{chunk-SZ357HRC.js → chunk-HNLKOICT.js} +1 -1
  14. package/dist/chunks/{chunk-WM33ADZV.js → chunk-HS7KIFMR.js} +2 -2
  15. package/dist/chunks/{chunk-6JRESWVF.js → chunk-LU5FHU2O.js} +2 -2
  16. package/dist/chunks/{chunk-SD7ML5PE.js → chunk-LVXAWWUX.js} +2 -2
  17. package/dist/chunks/{chunk-NTETWLP3.js → chunk-LWUTCYTZ.js} +1 -1
  18. package/dist/chunks/{chunk-DCGBYO44.js → chunk-NYGWVZOC.js} +2 -2
  19. package/dist/chunks/{chunk-7T74KLE5.js → chunk-PRDF6IU6.js} +2 -2
  20. package/dist/chunks/{chunk-ACE64PMM.js → chunk-QQIWOGHR.js} +2 -2
  21. package/dist/chunks/{chunk-YTV3RFCA.js → chunk-VAPGDPHM.js} +1 -1
  22. package/dist/chunks/{chunk-DWNGNTS6.js → chunk-VSKWQMSG.js} +2 -2
  23. package/dist/chunks/{chunk-RZIWVCHO.js → chunk-WEWXYHPR.js} +2 -2
  24. package/dist/chunks/{chunk-Q4JF4QHS.js → chunk-XMTYTJT6.js} +2 -2
  25. package/dist/descriptive.js +2 -2
  26. package/dist/distributions.js +2 -2
  27. package/dist/linalg.js +2 -2
  28. package/dist/mixed.js +4 -4
  29. package/dist/models/glm.js +4 -2
  30. package/dist/models/glmFamily.js +16 -4
  31. package/dist/models/lmer.d.ts +28 -5
  32. package/dist/models/lmer.js +129 -20
  33. package/dist/models/lmerAnova.d.ts +8 -3
  34. package/dist/models/lmerAnova.js +10 -4
  35. package/dist/models/lmerHypotheses.d.ts +4 -3
  36. package/dist/models/lmerHypotheses.js +9 -7
  37. package/dist/models/lmerTest.d.ts +13 -7
  38. package/dist/models/lmerTest.js +16 -11
  39. package/dist/models/mixedStructure.js +30 -14
  40. package/dist/numerics.js +2 -2
  41. package/dist/rstats.js +13 -13
  42. package/dist/special.js +2 -2
  43. package/dist/tests.js +7 -7
  44. package/package.json +1 -1
  45. package/dist/chunks/chunk-YQLEXH2Y.js +0 -3
package/dist/mixed.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! rStats.js 0.14.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 bt,c as At}from"./chunks/chunk-YQLEXH2Y.js";import{c as ce,s as le}from"./chunks/chunk-SD7ML5PE.js";import{b as zt}from"./chunks/chunk-7CPM3M6Q.js";import{A as he,C as ge,E as Bt,h as vt,l as pe}from"./chunks/chunk-YTV3RFCA.js";import{a as Rt,c as jt,d as Wt,f as me,g as xt}from"./chunks/chunk-ZUESNU7W.js";import{k as fe,l as ie}from"./chunks/chunk-DWNGNTS6.js";import{b as ue}from"./chunks/chunk-RZIWVCHO.js";import{b as Ut}from"./chunks/chunk-SZ357HRC.js";import"./chunks/chunk-6JRESWVF.js";import"./chunks/chunk-IGSR4GIF.js";import"./chunks/chunk-XCAMWVNO.js";var de=1,we=new WeakMap;function Ue(t){let r=we.get(t);if(r!==void 0)return r;let e=t.y.length,n=t.xColNames.length,o=t.q,s=t.terms.length,a=t.terms.reduce((b,R)=>b+R.nLevels,0),c=new Int32Array(a+1),l=new Int32Array(a),h=[],m=[],w=0;t.terms.forEach((b,R)=>{let V=b.columns.length;h.push(w),m.push(V);for(let L=0;L<b.nLevels;L++,w++)c[w]=b.zOffset+L*V,l[w]=R}),c[a]=o;let N=t.terms.map((b,R)=>{let V=m[R],L=new Int32Array(V*V).fill(-1);for(let{row:M,col:q,theta:j}of t.lambda)q>=b.zOffset&&q<b.zOffset+V&&(L[M-b.zOffset+(q-b.zOffset)*V]=j);return L}),u=t.terms.map(b=>t.groups[b.group].codes),f=new Int32Array(e+1);for(let b=0;b<e;b++){let R=0;for(let V of t.terms)for(let L of V.matrix[b])L!==0&&R++;f[b+1]=f[b]+R}let i=f,p=i[e],S=new Int32Array(p),A=new Float64Array(p);{let b=0;for(let R=0;R<e;R++)t.terms.forEach((V,L)=>{let M=V.zOffset+u[L][R]*m[L];V.matrix[R].forEach((q,j)=>{q!==0&&(S[b]=M+j,A[b++]=q)})})}let x=new Map,E=[],I=[],g=(b,R)=>{let V=R*a+b,L=x.get(V);return L===void 0&&(L=E.length,x.set(V,L),E.push(b),I.push(R)),L};for(let b=0;b<a;b++)g(b,b);let d=new Int32Array(s),v=s*(s+1)/2,y=new Int32Array(e*v);for(let b=0;b<e;b++){for(let R=0;R<s;R++)d[R]=h[R]+u[R][b];for(let R=0;R<s;R++)for(let V=0;V<=R;V++)y[b*v+R*(R+1)/2+V]=g(d[R],d[V])}let k=E.length,C=Array.from({length:k},(b,R)=>R).sort((b,R)=>I[b]-I[R]||E[b]-E[R]),F=new Int32Array(k);C.forEach((b,R)=>F[b]=R);let O=new Int32Array(a+1),P=new Int32Array(k),B=new Int32Array(k+1);C.forEach((b,R)=>{P[R]=E[b],O[I[b]+1]++;let V=m[l[E[b]]]*m[l[I[b]]];B[R+1]=B[R]+V});for(let b=0;b<a;b++)O[b+1]+=O[b];let X=new Float64Array(B[k]),ot=new Float64Array(o*n),st=new Float64Array(o),T=new Float64Array(n);for(let b=0;b<e;b++){let R=t.x[b],V=t.y[b];for(let L=0;L<s;L++){let M=t.terms[L].matrix[b],q=m[L];for(let j=0;j<=L;j++){let H=t.terms[j].matrix[b],J=m[j],D=F[y[b*v+L*(L+1)/2+j]],et=B[D];for(let at=0;at<J;at++){let Tt=H[at];if(Tt!==0)for(let mt=0;mt<q;mt++)X[et+mt+at*q]+=M[mt]*Tt}}}for(let L=i[b];L<i[b+1];L++){let M=S[L],q=A[L];for(let j=0;j<n;j++)ot[M+j*o]+=q*R[j];st[M]+=q*V}for(let L=0;L<n;L++)T[L]+=R[L]*V}let z=new Array(a),Q=Array.from({length:a},()=>[]),_=new Int32Array(a).fill(-1);for(let b=0;b<a;b++){let R=[],V=M=>{M!==b&&_[M]!==b&&(_[M]=b,R.push(M))};for(let M=O[b]+1;M<O[b+1];M++)V(P[M]);for(let M of Q[b])for(let q of z[M])V(q);let L=Int32Array.from(R).sort();z[b]=L,L.length>0&&Q[L[0]].push(b)}let G=new Int32Array(k);{let b=new Int32Array(a);for(let R=0;R<a;R++){let V=0;for(let L of z[R])b[L]=V,V+=c[L+1]-c[L];for(let L=O[R]+1;L<O[R+1];L++)G[L]=b[P[L]]}}let Y=new Int32Array(o+1);for(let b=0;b<a;b++){let R=0;for(let V of z[b])R+=c[V+1]-c[V];for(let V=c[b];V<c[b+1];V++)Y[V+1]=Y[V]+(c[b+1]-V)+R}let rt=new Int32Array(Y[o]),W=new Int32Array(o+1);for(let b=0;b<a;b++)for(let R=c[b];R<c[b+1];R++){let V=Y[R];for(let L=R;L<c[b+1];L++)rt[V++]=L;for(let L of z[b])for(let M=c[L];M<c[L+1];M++)rt[V++]=M}for(let b=0;b<o;b++)for(let R=Y[b]+1;R<Y[b+1];R++)W[rt[R]+1]++;for(let b=0;b<o;b++)W[b+1]+=W[b];let U=W.slice(),tt=W.slice(0,o),Z=new Int32Array(U[o]),ft=new Int32Array(U[o]);for(let b=0;b<o;b++)for(let R=Y[b]+1;R<Y[b+1];R++){let V=tt[rt[R]]++;Z[V]=b,ft[V]=R}let it={n:e,p:n,q:o,nGroups:a,groupStart:c,groupTerm:l,termNc:m,termTheta:N,blockStart:O,blockRow:P,blockValueStart:B,zz:X,zx:ot,zy:st,xy:T,rowStart:i,rowCols:S,rowValues:A,colStart:Y,colRows:rt,rowListStart:U,rowListCol:Z,rowListPos:ft,blockPatternOffset:G};return we.set(t,it),it}function We(t,r){return t.termTheta.map(e=>Float64Array.from(e,n=>n<0?0:r[n]))}function ht(t,r,e){let n=new Float64Array(t.q);for(let o=0;o<t.nGroups;o++){let s=t.groupTerm[o],a=t.termNc[s],c=r[s],l=t.groupStart[o];for(let h=0;h<a;h++){let m=e[l+h];if(m!==0)for(let w=h;w<a;w++)n[l+w]+=c[w+h*a]*m}}return n}function pt(t,r,e,n=1){let o=t.q,s=new Float64Array(o*n);for(let a=0;a<t.nGroups;a++){let c=t.groupTerm[a],l=t.termNc[c],h=r[c],m=t.groupStart[a];for(let w=0;w<n;w++){let N=m+w*o;for(let u=0;u<l;u++){let f=0;for(let i=u;i<l;i++)f+=h[i+u*l]*e[N+i];s[N+u]=f}}}return s}function Be(t,r){let e=new Float64Array(t.zz.length),n=new Float64Array(64);for(let o=0;o<t.nGroups;o++){let s=t.groupTerm[o],a=t.termNc[s],c=r[s];for(let l=t.blockStart[o];l<t.blockStart[o+1];l++){let h=t.groupTerm[t.blockRow[l]],m=t.termNc[h],w=r[h],N=t.blockValueStart[l];if(m===1&&a===1)e[N]=w[0]*t.zz[N]*c[0];else{let u=n.length>=m*a?n:new Float64Array(m*a);for(let f=0;f<a;f++)for(let i=0;i<m;i++){let p=0;for(let S=f;S<a;S++)p+=t.zz[N+i+S*m]*c[S+f*a];u[i+f*m]=p}for(let f=0;f<a;f++)for(let i=0;i<m;i++){let p=0;for(let S=i;S<m;S++)p+=w[S+i*m]*u[S+f*m];e[N+i+f*m]=p}}if(l===t.blockStart[o])for(let u=0;u<a;u++)e[N+u+u*a]+=1}}return e}function He(t,r){let{q:e,colStart:n,colRows:o,rowListStart:s,rowListCol:a,rowListPos:c}=t,l=new Float64Array(o.length),h=new Float64Array(e);for(let m=0;m<t.nGroups;m++){let w=t.termNc[t.groupTerm[m]],N=t.groupStart[m];for(let u=0;u<w;u++){let f=N+u;for(let A=t.blockStart[m];A<t.blockStart[m+1];A++){let x=t.blockRow[A],E=t.termNc[t.groupTerm[x]],I=t.blockValueStart[A]+u*E,g=t.groupStart[x];for(let d=x===m?u:0;d<E;d++)h[g+d]=r[I+d]}for(let A=s[f];A<s[f+1];A++){let x=a[A],E=c[A],I=l[E];if(I===0)continue;let g=n[x+1];for(let d=E;d<g;d++)h[o[d]]-=l[d]*I}let i=h[f];if(!(i>0))throw new Error("the penalised least-squares matrix is not positive definite");let p=Math.sqrt(i),S=n[f];l[S]=p,h[f]=0;for(let A=S+1;A<n[f+1];A++){let x=o[A];l[A]=h[x]/p,h[x]=0}}}return l}function Nt(t,r,e,n=0){let{q:o,colStart:s,colRows:a}=t;for(let c=0;c<o;c++){let l=s[c],h=e[n+c]/=r[l];if(h!==0)for(let m=l+1;m<s[c+1];m++)e[n+a[m]]-=r[m]*h}}function Et(t,r,e,n=0){let{q:o,colStart:s,colRows:a}=t;for(let c=o-1;c>=0;c--){let l=s[c],h=e[n+c];for(let m=l+1;m<s[c+1];m++)h-=r[m]*e[n+a[m]];e[n+c]=h/r[l]}}function Xe(t,r,e){for(let n=0;n<r;n++){let o=n*r,s=e[n];for(let a=0;a<n;a++)s-=t[a+o]*e[a];e[n]=s/t[n+o]}}function Ye(t,r,e){for(let n=r-1;n>=0;n--){let o=e[n]/=t[n+n*r],s=n*r;for(let a=0;a<n;a++)e[a]-=t[a+s]*o}}function Ht(t,r){let e=t.slice();for(let n=0;n<r;n++)for(let o=n+1;o<r;o++)e[o+n*r]=0;ge({nrow:r,ncol:r,data:e});for(let n=0;n<r;n++)for(let o=n+1;o<r;o++)e[o+n*r]=e[n+o*r];return e}function Xt(t,r,e,n={}){let o=Yt(t,r),{sys:s,t:a,l:c,rx:l,ws:h,xResiduals:m,res:w,uv:N}=o,{n:u,p:f,q:i}=s,p=Array.from(o.beta),S=Array.from(N),A=Array.from(o.b),x=0;for(let F of w)x+=F*F;let E=0;for(let F of S)E+=F*F;let I=x+E,g=0;for(let F=0;F<i;F++)g+=Math.log(c[s.colStart[F]]);g*=2;let d=0;for(let F=0;F<f;F++)d+=Math.log(l[F+F*f]);d*=2;let v=e?u-f:u,k={deviance:(e?g+d:g)+v*(1+Math.log(2*Math.PI*I/v)),beta:p,u:S,b:A,rss:x,ussq:E,prss:I,logDetL:g,logDetRX:d},C=n.gradient===!0||n.rxInverse===!0?Ht(l,f):void 0;return n.rxInverse===!0&&(k.rxInverse=Array.from({length:f},(F,O)=>Array.from({length:f},(P,B)=>C[O+B*f]))),n.gradient===!0&&(k.gradient=Ze(t,s,a,c,r,e,{res:w,uv:N,prss:I,ws:h,xResiduals:m,sInverse:C})),k}function Yt(t,r){if(r.length!==t.theta.length)throw new RangeError(`theta must have length ${t.theta.length}, not ${r.length}`);if(r.some(g=>!Number.isFinite(g)))throw new RangeError("theta must be finite");let e=Ue(t),{n,p:o,q:s}=e,a=We(e,r),c=He(e,Be(e,a)),l=g=>{let d=new Float64Array(n);for(let v=0;v<n;v++){let y=0;for(let k=e.rowStart[v];k<e.rowStart[v+1];k++)y+=e.rowValues[k]*g[e.rowCols[k]];d[v]=y}return d},h=g=>{let d=new Float64Array(s);for(let v=0;v<n;v++){let y=g[v];for(let k=e.rowStart[v];k<e.rowStart[v+1];k++)d[e.rowCols[k]]+=e.rowValues[k]*y}return d},m=g=>{Nt(e,c,g),Et(e,c,g)},w=pt(e,a,e.zx,o);for(let g=0;g<o;g++)Nt(e,c,w,g*s);let N=[],u=[];for(let g=0;g<o;g++){let d=w.slice(g*s,(g+1)*s);Et(e,c,d);let v=Float64Array.from(t.x,C=>C[g]),y=()=>{let C=l(ht(e,a,d));return v.map((F,O)=>F-C[O])},k=y();for(let C=0;C<de;C++){let F=pt(e,a,h(k));for(let O=0;O<s;O++)F[O]-=d[O];m(F);for(let O=0;O<s;O++)d[O]+=F[O];k=y()}N.push(k),u.push(d)}let f=new Float64Array(o*o);for(let g=0;g<o;g++)for(let d=g;d<o;d++){let v=0,y=N[g],k=N[d];for(let O=0;O<n;O++)v+=y[O]*k[O];let C=u[g],F=u[d];for(let O=0;O<s;O++)v+=C[O]*F[O];f[g+d*o]=v,f[d+g*o]=v}let i={nrow:o,ncol:o,data:f};if(o>0&&he(i)>0)throw new Error("the fixed-effects matrix of the penalised least squares is not positive definite");let p=(g,d)=>{Nt(e,c,g);for(let v=0;v<o;v++){let y=d[v];for(let k=0;k<s;k++)y-=w[k+v*s]*g[k];d[v]=y}Xe(f,o,d),Ye(f,o,d);for(let v=0;v<o;v++){let y=d[v];for(let k=0;k<s;k++)g[k]-=w[k+v*s]*y}Et(e,c,g)},S=(g,d)=>{let v=l(d),y=new Float64Array(n);for(let k=0;k<n;k++){let C=t.x[k],F=v[k];for(let O=0;O<o;O++)F+=C[O]*g[O];y[k]=t.y[k]-F}return y},A=pt(e,a,e.zy),x=Float64Array.from(e.xy);p(A,x);let E=ht(e,a,A),I=S(x,E);for(let g=0;g<de;g++){let d=pt(e,a,h(I));for(let y=0;y<s;y++)d[y]-=A[y];let v=new Float64Array(o);for(let y=0;y<n;y++){let k=t.x[y],C=I[y];for(let F=0;F<o;F++)v[F]+=k[F]*C}p(d,v);for(let y=0;y<s;y++)A[y]+=d[y];for(let y=0;y<o;y++)x[y]+=v[y];E=ht(e,a,A),I=S(x,E)}return{sys:e,t:a,l:c,cx:w,rx:f,ws:u,xResiduals:N,res:I,uv:A,beta:x,b:E,solveNormal:p}}function Zt(t,r){let{q:e,colStart:n,colRows:o}=t,s=new Float64Array(r.length),a=new Int32Array(e).fill(-1),c=new Float64Array(e),l=new Float64Array(e);for(let h=e-1;h>=0;h--){let m=n[h],w=n[h+1];for(let f=m+1;f<w;f++){let i=o[f];a[i]=h,c[i]=r[f],l[i]=0}for(let f=m+1;f<w;f++){let i=o[f],p=r[f],S=n[i],A=s[S]*p;for(let x=S+1;x<n[i+1];x++){let E=o[x];if(a[E]!==h)continue;let I=s[x];l[E]+=I*p,A+=I*c[E]}l[i]+=A}let N=r[m],u=1/N;for(let f=m+1;f<w;f++){let i=-l[o[f]]/N;s[f]=i,u-=r[f]*i}s[m]=u/N}return s}function ve(t,r,e){let{q:n,colStart:o,colRows:s,rowListStart:a,rowListCol:c,rowListPos:l}=t,h=new Float64Array(r.length),m=new Float64Array(n);for(let w=0;w<t.nGroups;w++){let N=t.termNc[t.groupTerm[w]],u=t.groupStart[w];for(let f=0;f<N;f++){let i=u+f;for(let x=t.blockStart[w];x<t.blockStart[w+1];x++){let E=t.blockRow[x],I=t.termNc[t.groupTerm[E]],g=t.blockValueStart[x]+f*I,d=t.groupStart[E];for(let v=E===w?f:0;v<I;v++)m[d+v]=e[g+v]}for(let x=a[i];x<a[i+1];x++){let E=c[x],I=l[x],g=r[I],d=h[I];if(g===0&&d===0)continue;let v=o[E+1];for(let y=I;y<v;y++)m[s[y]]-=h[y]*g+r[y]*d}let p=o[i],S=r[p],A=m[i]/(2*S);h[p]=A,m[i]=0;for(let x=p+1;x<o[i+1];x++){let E=s[x];h[x]=(m[E]-r[x]*A)/S,m[E]=0}}}return h}function be(t,r,e,n){let{q:o,colStart:s,colRows:a}=t,c=new Float64Array(r.length),l=new Int32Array(o).fill(-1),h=new Float64Array(o),m=new Float64Array(o),w=new Float64Array(o),N=new Float64Array(o);for(let u=o-1;u>=0;u--){let f=s[u],i=s[u+1];for(let E=f+1;E<i;E++){let I=a[E];l[I]=u,h[I]=r[E],m[I]=e[E],w[I]=0,N[I]=0}for(let E=f+1;E<i;E++){let I=a[E],g=r[E],d=e[E],v=s[I],y=n[v]*g,k=c[v]*g+n[v]*d;for(let C=v+1;C<s[I+1];C++){let F=a[C];if(l[F]!==u)continue;let O=n[C],P=c[C];w[F]+=O*g,N[F]+=P*g+O*d,y+=O*h[F],k+=P*h[F]+O*m[F]}w[I]+=y,N[I]+=k}let p=r[f],S=e[f],A=1/p,x=-S/(p*p);for(let E=f+1;E<i;E++){let I=a[E],g=n[E],d=-N[I]/p+w[I]*S/(p*p);c[E]=d,A-=r[E]*g,x-=e[E]*g+r[E]*d}c[f]=x/p-A*S/(p*p)}return c}function Ze(t,r,e,n,o,s,{res:a,uv:c,prss:l,ws:h,xResiduals:m,sInverse:w}){let{n:N,p:u,q:f}=r,i=Mt(r,Zt(r,n),e),p=new Array(o.length).fill(0);r.termTheta.forEach((g,d)=>{let v=r.termNc[d];for(let y=0;y<v;y++)for(let k=y;k<v;k++)p[g[k+y*v]]+=2*i[d][y+k*v]});let S=new Float64Array(f);for(let g=0;g<N;g++){let d=a[g];for(let v=r.rowStart[g];v<r.rowStart[g+1];v++)S[r.rowCols[v]]+=r.rowValues[v]*d}let A,x;if(s&&u>0){A=new Float64Array(f*u);for(let g=0;g<u;g++)for(let d=0;d<u;d++){let v=w[d+g*u],y=h[d];for(let k=0;k<f;k++)A[k+g*f]+=y[k]*v}x=new Float64Array(u*f);for(let g=0;g<N;g++)for(let d=r.rowStart[g];d<r.rowStart[g+1];d++){let v=r.rowCols[d],y=r.rowValues[d];for(let k=0;k<u;k++)x[k+v*u]+=m[k][g]*y}}let I=2*(s?N-u:N)/l;for(let{row:g,col:d,theta:v}of t.lambda){let y=0;if(A!==void 0&&x!==void 0)for(let k=0;k<u;k++)y+=A[d+k*f]*x[k+g*u];p[v]-=2*y+I*S[g]*c[d]}return p}function Mt(t,r,e){let{colStart:n,zz:o}=t,s=t.termNc.map(l=>new Float64Array(l*l)),a=new Float64Array(16),c=new Float64Array(16);for(let l=0;l<t.nGroups;l++){let h=t.groupTerm[l],m=t.termNc[h],w=e[h],N=t.groupStart[l];for(let u=t.blockStart[l];u<t.blockStart[l+1];u++){let f=t.blockRow[u],i=t.groupTerm[f],p=t.termNc[i],S=e[i],A=t.blockValueStart[u];if(m===1&&p===1){let g=f===l?n[N]:n[N]+1+t.blockPatternOffset[u],d=r[g]*o[A];s[h][0]+=d*S[0],f!==l&&(s[i][0]+=d*w[0]);continue}let x=p*m;a.length<x&&(a=new Float64Array(x),c=new Float64Array(Math.max(x,p*p,m*m)));for(let g=0;g<m;g++){let d=n[N+g];if(f===l)for(let v=g;v<p;v++){let y=r[d+v-g];a[v+g*p]=y,a[g+v*p]=y}else{let v=d+(m-g)+t.blockPatternOffset[u];for(let y=0;y<p;y++)a[y+g*p]=r[v+y]}}for(let g=0;g<m;g++)for(let d=0;d<p;d++){let v=0;for(let y=d;y<p;y++)v+=S[y+d*p]*o[A+y+g*p];c[d+g*p]=v}let E=s[h];for(let g=0;g<m;g++)for(let d=0;d<m;d++){let v=0;for(let y=0;y<p;y++)v+=a[y+g*p]*c[y+d*p];E[g+d*m]+=v}if(f===l)continue;for(let g=0;g<p;g++)for(let d=0;d<m;d++){let v=0;for(let y=d;y<m;y++)v+=w[y+d*m]*o[A+g+y*p];c[d+g*m]=v}let I=s[i];for(let g=0;g<p;g++)for(let d=0;d<p;d++){let v=0;for(let y=0;y<m;y++)v+=a[g+y*p]*c[y+d*m];I[g+d*p]+=v}}}return s}function Je(t){let r=[],e=c=>{c.type==="var"?r.includes(c.name)||r.push(c.name):c.type==="binary"?(e(c.left),c.op!=="^"&&e(c.right)):(c.type==="unary"||c.type==="paren"||c.type==="Error")&&e(c.arg)};e(t);let{terms:n}=me(t,c=>r.indexOf(c),[]);if(n.length===0)throw new SyntaxError(`invalid formula: no grouping factor in '${Wt(t)}'`);let o=n.map((c,l)=>[c,l]).sort((c,l)=>c[0].length-l[0].length||c[1]-l[1]).map(([c])=>c.map(l=>r[l])),s=!1,a=c=>{c.type==="binary"?(c.op==="/"&&(s=!0),a(c.left),a(c.right)):(c.type==="unary"||c.type==="paren"||c.type==="Error")&&a(c.arg)};return a(t),s&&(o=o.map(c=>[...c].reverse()).reverse()),o}function Ke(t){if(t.kind==="factor")return ue(t);let r=[...new Set(t.values)].sort((o,s)=>o-s),e=[],n=new Map;for(let o of r){let s=Ut(o);n.has(s)||(n.set(s,e.length),e.push(s))}return{levels:e,codes:t.values.map(o=>n.get(Ut(o)))}}function ye(t,r,e={}){let n=Rt(t,!1,!0);if(n.response===void 0)throw new TypeError("the formula has no response");let{fixed:o,bars:s}=jt(n.rhs);if(typeof r!="object"||r===null)throw new TypeError("data must be an object of columns");let a={...e.levels===void 0?{}:{levels:e.levels},dropUnusedLevels:!0},c=o??{type:"num",value:1,text:"1"},l=e.contrasts===void 0?{}:{contrasts:e.contrasts,contrastVariables:xt({response:n.response,rhs:c},Object.keys(r)).variables},h=bt(n,r,{...a,...l},c),{frame:m}=h,w=m.rows.length;if(w===0)throw new RangeError("0 (non-NA) cases");if(s.length===0)throw new Error("No random effects terms specified in formula");let N=h.terms.variables,u=[];for(let T of s){let z=[T.lhs];if(T.double){let _=xt(n,Object.keys(r),T.lhs);z=_.intercept?[{type:"num",value:1,text:"1"}]:[];for(let G of _.terms){let Y=G.map(rt=>({type:"var",name:N[rt]})).reduce((rt,W)=>({type:"binary",op:":",left:rt,right:W}));z.push({type:"binary",op:"+",left:{type:"num",value:0,text:"0"},right:Y})}}let Q=Je(T.group);for(let _ of z)for(let G of Q)u.push({lhs:_,groupVariables:G})}let f=new Map,i=T=>{let z=T.join(":"),Q=f.get(z);if(Q!==void 0)return Q;let _=T.map(Y=>Ke(m.variables[N.indexOf(Y)])),G;if(_.length===1)G=_[0];else{let Y=new Map;for(let U=0;U<w;U++){let tt=_.map(Z=>Z.codes[U]);Y.set(tt.join(","),tt)}let rt=[...Y.values()].sort((U,tt)=>{for(let Z=0;Z<U.length;Z++)if(U[Z]!==tt[Z])return U[Z]-tt[Z];return 0}),W=new Map(rt.map((U,tt)=>[U.join(","),tt]));G={levels:rt.map(U=>U.map((tt,Z)=>_[Z].levels[tt]).join(":")),codes:Array.from({length:w},(U,tt)=>W.get(_.map(Z=>Z.codes[tt]).join(",")))}}return f.set(z,G),G},p=u.map(T=>({...T,name:T.groupVariables.join(":"),factor:i(T.groupVariables)})),S=p;p.some((T,z)=>z>0&&T.factor.levels.length>p[z-1].factor.levels.length)&&(S=p.map((T,z)=>[T,z]).sort((T,z)=>T[0].factor.levels.length-z[0].factor.levels.length||T[1]-z[1]).map(([T])=>T).reverse());let A=[],x=[],E=0,I=0;for(let T of S){let z=A.findIndex(Y=>Y.name===T.name);z<0&&(z=A.length,A.push({name:T.name,levels:T.factor.levels,codes:T.factor.codes}));let Q=bt(n,r,a,T.lhs),_=Q.colNames.length,G=T.factor.levels.length;x.push({label:`${Wt(T.lhs)} | ${T.name}`,group:z,columns:Q.colNames,matrix:Q.x,nLevels:G,zOffset:E,thetaOffset:I}),E+=G*_,I+=_*(_+1)/2}if(A.some(T=>T.levels.length<2))throw new RangeError("grouping factors must have > 1 sampled level");let g=A.filter(T=>T.levels.length>=w).map(T=>T.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 T of x){let z=T.nLevels*T.columns.length;if(w<=z)throw new RangeError(`number of observations (=${w}) <= number of random effects (=${z}) for term (${T.label}); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable`)}let d=[],v=[],y=[],k=[],C=[];for(let T of x){let{levels:z,codes:Q}=A[T.group],_=T.columns.length,G=Array.from({length:z.length*_},()=>({rows:[],values:[]}));for(let W=0;W<w;W++){let U=T.matrix[W],tt=Q[W]*_;for(let Z=0;Z<_;Z++){let ft=U[Z];if(ft===0)continue;let it=G[tt+Z];it.rows.push(W),it.values.push(ft)}}for(let W=0;W<z.length;W++)for(let U=0;U<_;U++)d.push(G[W*_+U]),v.push(z[W]);let Y=Array.from({length:_},()=>new Array(_).fill(-1)),rt=T.thetaOffset;for(let W=0;W<_;W++)for(let U=W;U<_;U++)Y[U][W]=rt++,y.push(U===W?1:0),k.push(U===W?0:-1/0);for(let W=0;W<T.nLevels;W++){let U=T.zOffset+W*_;for(let tt=0;tt<_;tt++)for(let Z=tt;Z<_;Z++)C.push({row:U+Z,col:U+tt,theta:Y[Z][tt]})}}let F=[],O=h.x,P=h.colNames;if(P.length>0){let T=vt(O),z=new Set(T.pivot.slice(T.rank));if(z.size>0){let Q=P.map((_,G)=>G).filter(_=>!z.has(_));O=O.map(_=>Q.map(G=>_[G])),P=Q.map(_=>P[_]),F.push(`fixed-effect model matrix is rank deficient so dropping ${z.size} ${z.size===1?"column / coefficient":"columns / coefficients"}`)}}let B=[...h.warnings],X=[];for(let T=0;T<P.length;T++){let z=O.map(G=>G[T]);if(z.every(G=>G===0||G===1))continue;let Q=z.reduce((G,Y)=>G+Y,0)/w,_=Math.sqrt(z.reduce((G,Y)=>G+(Y-Q)**2,0)/(w-1));X.push(_)}let ot=Math.log(1e3);return X.some((T,z)=>Math.abs(Math.log(T))>ot||X.some((Q,_)=>_!==z&&Math.abs(Math.log(T/Q))>ot))&&B.push(`Some predictor variables are on very different scales: consider rescaling.
3
- You may also use (g)lmerControl(autoscale = TRUE) to improve numerical stability.`),{rows:m.rows,y:h.y,xColNames:P,x:O,terms:x,groups:A,q:d.length,z:d,zColLevels:v,theta:y,lower:k,lambda:C,messages:F,warnings:B}}var xe=.25,Qe=1e-4,to=1e-6,Ae=1e-6,Ne=1e-4,eo=.001,Ee=1e-13,Me=1e-10,oo=1e-9,ro=500,Se=.75,ke="lmer: the minimisation of the criterion over theta did not converge";function Ie(t){let r=t.length,e=t.map(o=>o.slice()),n=e.map((o,s)=>e.map((a,c)=>s===c?1:0));for(let o=0;o<100;o++){let s=0,a=0;for(let c=0;c<r;c++)for(let l=0;l<r;l++)a+=e[c][l]**2,c!==l&&(s+=e[c][l]**2);if(s<=1e-32*a)break;for(let c=0;c<r-1;c++)for(let l=c+1;l<r;l++){let h=e[c][l];if(h===0)continue;let m=(e[l][l]-e[c][c])/(2*h),w=Math.sign(m||1)/(Math.abs(m)+Math.sqrt(1+m*m)),N=1/Math.sqrt(1+w*w),u=w*N;for(let f=0;f<r;f++){let i=e[f][c],p=e[f][l];e[f][c]=N*i-u*p,e[f][l]=u*i+N*p}for(let f=0;f<r;f++){let i=e[c][f],p=e[l][f];e[c][f]=N*i-u*p,e[l][f]=u*i+N*p}for(let f=0;f<r;f++){let i=n[f][c],p=n[f][l];n[f][c]=N*i-u*p,n[f][l]=u*i+N*p}}}return{values:e.map((o,s)=>o[s]),vectors:e.map((o,s)=>n.map(a=>a[s]))}}var Jt=t=>Math.sqrt(t.reduce((r,e)=>r+e*e,0)),gt=(t,r)=>t.reduce((e,n,o)=>e+n*r[o],0);function no(t,r,e,n,o){let s=t.length,{values:a,vectors:c}=r,l=c.map(x=>gt(x,t)),h=x=>{let E=new Array(s).fill(0);return x.forEach((I,g)=>c[g].forEach((d,v)=>E[v]+=I*d)),E},m=Math.min(...a);if(m>-n&&a.every((x,E)=>x>n||Math.abs(l[E])<=o)){let x=h(a.map((E,I)=>E>n?-l[I]/E:0));if(Jt(x)<=e)return{step:x,newton:!0}}let w=Math.max(0,-m),N=x=>Math.sqrt(a.reduce((E,I,g)=>E+(l[g]/(I+x))**2,0)),u=Math.max(n,1e-12*Math.max(...a.map(Math.abs),1));if(N(w+u)>=e){let x=w+u,E=w+Jt(t)/e+Math.max(...a.map(Math.abs))+u;for(let I=0;I<200&&E-x>1e-15*E;I++){let g=(x+E)/2;N(g)>e?x=g:E=g}return{step:h(a.map((I,g)=>-l[g]/(I+E))),newton:!1}}let f=a.map(x=>x+w<=u),i=h(a.map((x,E)=>f[E]?0:-l[E]/(x+w))),p=a.indexOf(m),S=Math.sqrt(Math.max(0,e*e-gt(i,i))),A=-Math.sign(l[p])||1;return{step:i.map((x,E)=>x+A*S*c[p][E]),newton:!1}}function so(t){let r=[];for(let e of t.terms){let n=e.columns.length,o=e.thetaOffset;for(let s=0;s<n;s++){let a=[];for(let c=s;c<n;c++)a.push(o++);r.push(a)}}return r}var ao=(t,r,e)=>{let n=Xt(t,r,e,{gradient:!0});return{f:n.deviance,g:n.gradient}};function co(t,r,e){let n=t.lower.map(A=>A===0),o=A=>e(t,A,r),s=t.theta.slice(),{f:a,g:c}=o(s),l=new Set,h=new Set,m=()=>Math.max(1,...s.map(Math.abs)),w=()=>oo*Math.max(1,Math.abs(a)),N=xe*m(),u=1/0,f,i=A=>{let x=to*m(),E=A.map(I=>{let g=Math.max(Qe*Math.abs(s[I]),x),d=s.slice();d[I]=s[I]+g;let v=s.slice();v[I]=s[I]-g;let y=o(d).g,k=o(v).g;return A.map(C=>(y[C]-k[C])/(2*g))});return A.map((I,g)=>A.map((d,v)=>(E[g][v]+E[v][g])/2))},p=A=>{let x=f===void 0||f.free.length!==A.length||f.free.some((T,z)=>T!==A[z]),E=x?i(A):f.h;f=void 0;let I=Ie(E),g=Ae*Math.max(...I.values.map(Math.abs)),d=A.map(T=>c[T]),{step:v,newton:y}=no(d,I,N,g,w()),k=s.slice();A.forEach((T,z)=>k[T]=s[T]+v[z]);let C=Math.max(...v.map(Math.abs))/m(),F=o(k);if(!Number.isFinite(F.f))throw new Error(ke);let O=-(gt(d,v)+gt(v,E.map(T=>gt(T,v)))/2),P=Math.abs(a-F.f)>1e-10*Math.abs(a)?a-F.f:-(gt(d,v)+gt(A.map(T=>F.g[T]),v))/2,B=O>0?P/O:P>=0?1:-1;(B>.1||C<=Me)&&(s=k,a=F.f,c=F.g);let X=Jt(v);if(B<.25?N=Math.max(X/4,Ee*m()):B>.75&&X>=.99*N&&(N*=2),!y)return u=1/0,!1;let st=A.every(T=>Math.abs(c[T])<=w())&&(C<=Ee||x&&C<=Me&&C>=.5*u);return B>Se&&B<1/Se&&C<.5*u&&(f={free:A,h:E}),u=C,st},S=()=>{if(l.size===0)return!1;let A=s.map((y,k)=>k),x=Ie(i(A)),E=Ae*Math.max(...x.values.map(Math.abs)),I=x.values.indexOf(Math.min(...x.values));if(!(x.values[I]<-E))return!1;let g=x.vectors[I],d=A.filter((y,k)=>l.has(y)&&Math.abs(g[k])>1e-8);if(d.length===0)return!1;for(let y of d)l.delete(y),h.add(y);let v=eo*m();return A.forEach((y,k)=>s[y]=s[y]+v*g[k]),{f:a,g:c}=o(s),!0};for(let A=0;A<ro;A++){let x=s.map((d,v)=>v).filter(d=>!l.has(d));if(x.length>0&&!p(x))continue;let E=x.filter(d=>n[d]&&!h.has(d)&&Math.abs(s[d])<Ne*m());if(E.length>0){for(let d of E)s[d]=0,l.add(d);({f:a,g:c}=o(s)),u=1/0,f=void 0;continue}let I=1e-12*Math.max(1,Math.abs(a)),g=[...l].filter(d=>Math.abs(c[d])>I);if(g.length>0)for(let d of g)l.delete(d),h.add(d);else if(!S()){for(let d of so(t))if(s[d[0]]<0)for(let v of d)s[v]=-s[v];return s.map(d=>d===0?0:d)}N=Math.max(N,xe*Ne*m()),u=1/0,f=void 0}throw new Error(ke)}function lo(t,r,e={}){return Kt(t,r,e,ao)}var Te=new WeakMap;function dt(t){let r=typeof t=="object"&&t!==null?Te.get(t):void 0;if(r===void 0)throw new TypeError("fit must be a result returned by lmer (not a copy of one)");return r}function Kt(t,r,e,n){let o=e.REML??!0,s={};e.contrasts!==void 0&&(s.contrasts=e.contrasts),e.levels!==void 0&&(s.levels=e.levels);let a=ye(t,r,s),c=co(a,o,n),l=Xt(a,c,o,{rxInverse:!0}),h=a.y.length,m=a.xColNames.length,w=2*Math.PI,N=l.logDetL+h*(1+Math.log(w*l.prss/h)),u=l.logDetL+l.logDetRX+(h-m)*(1+Math.log(w*l.prss/(h-m))),f=l.prss/(o?h-m:h),i=Math.sqrt(f),p=l.rxInverse.map(F=>F.map(O=>f*O)),S=p.map((F,O)=>Math.sqrt(F[O])),A=l.beta.map((F,O)=>F/S[O]),x=a.terms.map(F=>{let O=F.columns.length,P=Array.from({length:O},()=>new Array(O).fill(0)),B=F.thetaOffset;for(let T=0;T<O;T++)for(let z=T;z<O;z++)P[z][T]=c[B++];let X=P.map(T=>P.map(z=>f*T.reduce((Q,_,G)=>Q+_*z[G],0))),ot=X.map((T,z)=>Math.sqrt(T[z])),st=X.map((T,z)=>T.map((Q,_)=>z===_?1:Q/(ot[z]*ot[_])));return{group:a.groups[F.group].name,columns:[...F.columns],sd:ot,corr:st}}),E=a.groups.map((F,O)=>{let P=a.terms.filter(B=>B.group===O);return{group:F.name,levels:[...F.levels],columns:P.flatMap(B=>B.columns),values:F.levels.map((B,X)=>P.flatMap(ot=>{let st=ot.columns.length;return ot.columns.map((T,z)=>l.b[ot.zOffset+X*st+z])}))}}),I=a.x.map(F=>F.reduce((O,P,B)=>O+P*l.beta[B],0));a.z.forEach((F,O)=>{let P=l.b[O];P!==0&&F.rows.forEach((B,X)=>I[B]+=F.values[X]*P)});let g=a.y.map((F,O)=>F-I[O]),d=-(o?u:N)/2,v=m+c.length+1,y=c.some((F,O)=>a.lower[O]===0&&F<1e-4),k=[...a.messages];y&&k.push("boundary (singular) fit: see help('isSingular')");let C={formula:t,REML:o,nObs:h,rows:[...a.rows],theta:c,fixefNames:[...a.xColNames],fixef:l.beta,coefTable:{estimate:[...l.beta],stdError:S,tValue:A},vcov:p,sigma:i,varCorr:x,ranef:E,fitted:I,residuals:g,logLik:d,df:v,AIC:-2*d+2*v,BIC:-2*d+Math.log(h)*v,deviance:N,REMLcrit:u,singular:y,messages:k,warnings:[...a.warnings]};return Te.set(C,{structure:a,data:r,options:{...e},reml:o,criterion:n,theta:[...c]}),C}function Fe(t,r,e){let n=new Float64Array(t.zz.length),o=new Float64Array(64);for(let s=0;s<t.nGroups;s++){let a=t.groupTerm[s],c=t.termNc[a],l=e[a];for(let h=t.blockStart[s];h<t.blockStart[s+1];h++){let m=t.groupTerm[t.blockRow[h]],w=t.termNc[m],N=r[m],u=t.blockValueStart[h];o.length<w*c&&(o=new Float64Array(w*c));for(let f=0;f<c;f++)for(let i=0;i<w;i++){let p=0;for(let S=0;S<c;S++)p+=t.zz[u+i+S*w]*l[S+f*c];o[i+f*w]=p}for(let f=0;f<c;f++)for(let i=0;i<w;i++){let p=0;for(let S=0;S<w;S++)p+=N[S+i*w]*o[S+f*w];n[u+i+f*w]=p}}}return n}function St(t,r,e){let n=new Float64Array(e*e);for(let o=0;o<e;o++)for(let s=0;s<e;s++){let a=r[s+o*e];if(a!==0)for(let c=0;c<e;c++)n[c+o*e]+=t[c+s*e]*a}return n}var Qt=(t,r)=>{let e=0;for(let n=0;n<r;n++)e+=t[n+n*r];return e},te=(t,r)=>Array.from({length:r},(e,n)=>Array.from({length:r},(o,s)=>t[n+s*r]));function Oe(t,r,e){let n=Yt(t,r),{sys:o,t:s,l:a,rx:c,ws:l,xResiduals:h,res:m,uv:w}=n,{n:N,p:u,q:f}=o,i=r.length,p=i+1,S=M=>{let q=new Float64Array(N);for(let j=0;j<N;j++){let H=0;for(let J=o.rowStart[j];J<o.rowStart[j+1];J++)H+=o.rowValues[J]*M[o.rowCols[J]];q[j]=H}return q},A=M=>{let q=new Float64Array(f);for(let j=0;j<N;j++){let H=M[j];if(H!==0)for(let J=o.rowStart[j];J<o.rowStart[j+1];J++)q[o.rowCols[J]]+=o.rowValues[J]*H}return q},x=M=>{let q=new Float64Array(u);for(let j=0;j<N;j++){let H=t.x[j],J=M[j];for(let D=0;D<u;D++)q[D]+=H[D]*J}return q},E=M=>{let q=new Float64Array(N);for(let j=0;j<N;j++){let H=t.x[j],J=0;for(let D=0;D<u;D++)J+=H[D]*M[D];q[j]=J}return q},I=M=>{Nt(o,a,M),Et(o,a,M)},g=Array.from({length:i},()=>[]),d=Array.from({length:i},()=>[]);for(let{row:M,col:q,theta:j}of t.lambda)g[j].push(M),d[j].push(q);let v=(M,q)=>{let j=new Float64Array(f),H=g[M],J=d[M];for(let D=0;D<H.length;D++)j[H[D]]+=q[J[D]];return j},y=(M,q)=>{let j=new Float64Array(f),H=g[M],J=d[M];for(let D=0;D<H.length;D++)j[J[D]]+=q[H[D]];return j},k=(M,q,j)=>{let H=g[M],J=d[M],D=0;for(let et=0;et<H.length;et++)D+=q[H[et]]*j[J[et]];return D},C=(M,q,j,H,J)=>{for(let D=0;D<u;D++)for(let et=0;et<u;et++){let at=J*k(M,q[D],j[et]);H[D+et*u]+=at,H[et+D*u]+=at}},F=0;for(let M of m)F+=M*M;let O=0;for(let M of w)O+=M*M;let P=F+O,B=e?N-u:N,X=P/B,ot=Math.sqrt(X),st=0;for(let M=0;M<f;M++)st+=Math.log(a[o.colStart[M]]);st*=2;let T=0;for(let M=0;M<u;M++)T+=Math.log(c[M+M*u]);T*=2;let Q=(e?st+T:st)+P/X+B*Math.log(2*Math.PI*X),_=Ht(c,u),G=Zt(o,a),Y=A(m),rt=h.map(A),W=Mt(o,G,s),U=new Array(i).fill(0),tt=(M,q,j)=>{o.termTheta.forEach((H,J)=>{let D=o.termNc[J];for(let et=0;et<D;et++)for(let at=et;at<D;at++)q[H[at+et*D]]+=j*M[J][et+at*D]})};tt(W,U,2);let Z=Array.from({length:i},(M,q)=>-2*k(q,Y,w)),ft=Array.from({length:i},(M,q)=>{let j=new Float64Array(u*u);return C(q,rt,l,j,-1),j});if(e)for(let M=0;M<i;M++)U[M]+=Qt(St(_,ft[M],u),u);let it=ft.map(M=>St(_,M,u)),b=Array.from({length:p},()=>new Array(p).fill(0));for(let M=0;M<i;M++){let q=o.termTheta.map($=>Float64Array.from($,ut=>ut===M?1:0)),j=Fe(o,q,s),H=Fe(o,s,q);for(let $=0;$<j.length;$++)j[$]+=H[$];let J=ve(o,a,j),D=be(o,a,J,G),et=new Array(i).fill(0),at=Mt(o,D,s),Tt=Mt(o,G,q);if(tt(at,et,2),tt(Tt,et,2),e&&u>0){let $=[],ut=[];for(let nt=0;nt<u;nt++){let lt=y(M,rt[nt]),K=pt(o,s,A(S(v(M,l[nt]))));for(let ct=0;ct<f;ct++)lt[ct]-=K[ct];I(lt),$.push(lt);let ae=v(M,l[nt]),Ge=ht(o,s,lt);for(let ct=0;ct<f;ct++)ae[ct]+=Ge[ct];let Gt=S(ae);for(let ct=0;ct<N;ct++)Gt[ct]=-Gt[ct];ut.push(A(Gt))}for(let nt=0;nt<i;nt++){let lt=new Float64Array(u*u);C(nt,ut,l,lt,-1),C(nt,rt,$,lt,-1),et[nt]+=Qt(St(_,lt,u),u)-Qt(St(it[nt],it[M],u),u)}}let mt=S(v(M,w)),$t=y(M,Y),De=pt(o,s,A(mt));for(let $=0;$<f;$++)$t[$]-=De[$];let Ft=x(mt);for(let $=0;$<u;$++)Ft[$]=-Ft[$];let wt=$t.slice(),Ot=Ft.slice();n.solveNormal(wt,Ot);{let $=S(ht(o,s,wt)),ut=E(Ot);for(let K=0;K<N;K++)$[K]+=ut[K];let nt=pt(o,s,A($)),lt=x($);for(let K=0;K<f;K++)nt[K]=$t[K]-nt[K]-wt[K];for(let K=0;K<u;K++)lt[K]=Ft[K]-lt[K];n.solveNormal(nt,lt);for(let K=0;K<f;K++)wt[K]+=nt[K];for(let K=0;K<u;K++)Ot[K]+=lt[K]}let Pt=S(ht(o,s,wt)),$e=E(Ot);for(let $=0;$<N;$++)Pt[$]=-(mt[$]+Pt[$]+$e[$]);let Pe=A(Pt);for(let $=0;$<i;$++){let ut=-2*(k($,Pe,w)+k($,Y,wt));b[$][M]=et[$]+ut/X}}for(let M=0;M<i;M++){for(let j=M+1;j<i;j++){let H=(b[M][j]+b[j][M])/2;b[M][j]=H,b[j][M]=H}let q=-2*Z[M]/(X*ot);b[M][i]=q,b[i][M]=q}b[i][i]=6*P/(X*X)-2*B/X;let R=[...U.map((M,q)=>M+Z[q]/X),-2*P/(X*ot)+2*B/ot],V=te(_.map(M=>X*M),u),L=[...ft.map((M,q)=>te(St(it[q],_,u).map(j=>-X*j),u)),te(_.map(M=>2*ot*M),u)];return{varpar:[...r,ot],deviance:Q,gradient:R,phiGradient:U,prssGradient:Z,prss:P,hessian:b,beta:Array.from(n.beta),vcov:V,jacobian:L}}var ee=1e-8,fo=Math.sqrt(Number.EPSILON),io=1e-8,Re=new WeakMap;function je(t){if(!Number.isFinite(t))return Number.isNaN(t)?"nan":t>0?"inf":"-inf";let[r,e]=t.toExponential(1).split("e"),n=Number(e);return`${r}e${n<0?"-":"+"}${String(Math.abs(n)).padStart(2,"0")}`}function mo(t){let r=[],e=t.filter(s=>s<-ee),n=t.filter(s=>Math.abs(s)<ee),o=s=>s===1?"eigenvalue":"eigenvalues";return e.length>0&&r.push(`Model failed to converge with ${e.length} negative ${o(e.length)}: ${e.map(je).join(" ")}`),n.length>0&&r.push(`Model may not have converged with ${n.length} ${o(n.length)} close to zero: ${n.map(je).join(" ")}`),r}function It(t){let r=Re.get(t);if(r!==void 0)return r;let e=dt(t),n=uo(e.structure,e.theta,e.reml);return Re.set(t,n),n}function uo(t,r,e){let n=Oe(t,r,e),o=n.varpar.length,s=Bt(n.hessian,{symmetric:!0}),a=s.values,c=s.vectors,l=Array.from({length:o},()=>new Array(o).fill(0));return a.forEach((h,m)=>{if(h>ee)for(let w=0;w<o;w++){let N=2*c[w][m]/h;for(let u=0;u<o;u++)l[w][u]+=N*c[u][m]}}),{varpar:n.varpar,hessian:n.hessian,hessianEigenvalues:a,vcovVarpar:l,beta:n.beta,sigma:n.varpar[o-1],vcov:n.vcov,jacobian:n.jacobian,warnings:mo(a)}}var kt=(t,r)=>{let e=0;for(let n=0;n<t.length;n++){let o=t[n];if(o===0)continue;let s=r[n];for(let a=0;a<t.length;a++)e+=o*s[a]*t[a]}return e},_t=(t,r)=>t.reduce((e,n,o)=>e+n*r[o],0);function qt(t,r,e,n=t.beta){if(r.some(Number.isNaN)||Number.isNaN(e))return{estimate:NaN,stdError:NaN,df:NaN,tValue:NaN,pValue:NaN};let o=_t(r,n),s=kt(r,t.vcov),a=Math.sqrt(s),c=t.jacobian.map(w=>kt(r,w)),l=2*s*s/kt(c,t.vcovVarpar),h=(o-e)/a,m=2*fe(Math.abs(h),l,!1,!1);return{estimate:o,stdError:a,df:l,tValue:h,pValue:m}}function Lt(t,r,e,n){let o=t.sigma*t.sigma;return{sumSq:r*o*e,meanSq:r*o,numDF:e,denDF:n,F:r,pValue:le(r,e,n,!1,!1)}}function po(t){if(t.length===1)return t[0];if(t.every((e,n)=>n===0||Math.abs(e-t[n-1])<io))return t.reduce((e,n)=>e+n,0)/t.length;if(t.some(e=>e<=2))return 2;let r=t.reduce((e,n)=>e+n/(n-2),0);return 2*r/(r-t.length)}function ho(t,r,e,n){return oe(It(t),r,e,n)}function oe(t,r,e,n){let o=r.length;if(o===0)return;let s=e??new Array(o).fill(0);if(o===1){let p=qt(t,r[0],s[0]);return Lt(t,p.tValue*p.tValue,1,p.df)}if(r.some(p=>p.some(Number.isNaN))||s.some(Number.isNaN))return Lt(t,NaN,NaN,NaN);let a=r.map(p=>t.vcov.map((S,A)=>p.reduce((x,E,I)=>x+E*t.vcov[I][A],0))),c=Bt(a.map(p=>r.map(S=>_t(p,S))),{symmetric:!0}),l=c.values,h=Math.max(fo*l[0],0),m=0;for(;m<o&&l[m]>h;)m++;if(m===0)return;m<o&&s.some(p=>p!==0)&&n.add("Contrast is rank deficient and test may be affected");let w=t.beta;if(s.some(p=>p!==0)){let p=zt(r.map(S=>[...S]));w=t.beta.map((S,A)=>S-_t(p[A],s))}let N=t.beta.length,u=Array.from({length:m},(p,S)=>Array.from({length:N},(A,x)=>r.reduce((E,I,g)=>E+c.vectors[g][S]*I[x],0)));if(m===1){let p=qt(t,u[0],0,w);return Lt(t,p.tValue*p.tValue,1,p.df)}let f=0,i=[];return u.forEach((p,S)=>{f+=_t(p,w)**2/l[S];let A=t.jacobian.map(x=>kt(p,x));i.push(2*l[S]*l[S]/kt(A,t.vcovVarpar))}),Lt(t,f/m,m,po(i))}function go(t,r={}){let e=r.ddf??"Satterthwaite";if(e!=="Satterthwaite"&&e!=="lme4")throw new RangeError(`ddf must be "Satterthwaite" or "lme4", not ${JSON.stringify(e)}`);let o=[...dt(t).structure.xColNames];if(e==="lme4"){let{estimate:l,stdError:h,tValue:m}=t.coefTable;return{ddf:e,names:o,estimate:[...l],stdError:[...h],tValue:[...m],messages:[],warnings:[]}}let s=It(t),a=o.length,c=o.map((l,h)=>qt(s,Array.from({length:a},(m,w)=>w===h?1:0),0));return{ddf:e,names:o,estimate:c.map(l=>l.estimate),stdError:c.map(l=>l.stdError),df:c.map(l=>l.df),tValue:c.map(l=>l.tValue),pValue:c.map(l=>l.pValue),messages:[],warnings:[...s.warnings]}}function wo(t,r){if(!Array.isArray(t))throw new TypeError("L must be an array of numbers or an array of rows");let e=t.length>0&&Array.isArray(t[0])?t.map(n=>[...n]):[[...t]];for(let n of e){if(!Array.isArray(n)||n.some(o=>typeof o!="number"))throw new TypeError("L must be an array of numbers or an array of rows");if(n.length!==r)throw new TypeError(`each contrast must have ${r} values (one per fixed effect), not ${n.length}`)}return e}function vo(t,r,e={}){let o=dt(t).structure.xColNames.length,s=wo(r,o),a=e.joint??!0,c=e.confint??!0,l=e.level??.95;if(typeof l!="number"||!(l>=0&&l<=1))throw new RangeError("level must be a number between 0 and 1");let h=e.rhs??0,m=typeof h=="number"?new Array(s.length).fill(h):[...h];if(m.length!==s.length||m.some(i=>typeof i!="number"))throw new TypeError(`rhs must be a number or have one value per contrast (${s.length})`);let w=new At,N=It(t);if(N.warnings.forEach(i=>w.add(i)),a){let i=o===0?void 0:ho(t,s,m,w),p=S=>i===void 0?[]:[i[S]];return{joint:!0,sumSq:p("sumSq"),meanSq:p("meanSq"),numDF:p("numDF"),denDF:p("denDF"),F:p("F"),pValue:p("pValue"),messages:[],warnings:w.list}}let u=o===0?[]:s.map((i,p)=>qt(N,i,m[p])),f={joint:!1,estimate:u.map(i=>i.estimate),stdError:u.map(i=>i.stdError),df:u.map(i=>i.df),tValue:u.map(i=>i.tValue),pValue:u.map(i=>i.pValue),messages:[],warnings:w.list};if(c){let i=u.map(p=>ie((1-l)/2,p.df,!1,!1));f.lower=u.map((p,S)=>p.estimate-p.stdError*i[S]),f.upper=u.map((p,S)=>p.estimate+p.stdError*i[S])}return f}var bo=Math.log10(2);function yo(t){let r=new DataView(new ArrayBuffer(8));r.setFloat64(0,t);let e=r.getUint16(0)>>4&2047;if(e>0)return e-1023;let n=r.getUint32(0)&1048575,o=r.getUint32(4);return(n!==0?32+(31-Math.clz32(n)):31-Math.clz32(o))-1074}function Vt(t){let r=1;for(let e=0;e<t;e++)r*=10;return r}function xo(t){let r=Math.floor(t),e=t-r,n=e<.5?r:e>.5?r+1:r%2===0?r:r+1;return n===0?t<0?-0:0:n}function Ao(t,r=0){if(Number.isNaN(t)||Number.isNaN(r))return NaN;if(!Number.isFinite(t)||t===0||r>323)return t;if(r<-308)return 0;if(r===0)return xo(t);let e=Math.floor(r+.5),n=Math.abs(t);if(bo*(.5+yo(n))+e>15)return t;let s,a,c;if(e<=308){let w=e>=0?Vt(e):1/Vt(-e),N=n*w;c=Math.floor(N),s=c/w,a=Math.ceil(N)/w}else{let w=Vt(308),N=Vt(e-308),u=n*w*N;c=Math.floor(u),s=c/w/N,a=Math.ceil(u)/w/N}let l=a-n,h=n-s,m=l<h||l===h&&c%2===1?a:s;return t<0?-m:m}function Ct(t,r=7){let e=0;for(let o of t)for(let s of o)Math.abs(s)>e&&(e=Math.abs(s));let n=e>0?Math.max(0,r-Math.log10(e)):r;return t.map(o=>o.map(s=>Ao(s,n)))}var ne={type:"num",value:1,text:"1"};function Le(t,r,e={}){let n=Rt(t,!1,!0);if(n.response===void 0)throw new TypeError("the formula has no response");if(typeof r!="object"||r===null)throw new TypeError("data must be an object of columns");let{fixed:o}=jt(n.rhs),s=o??ne,a=e.contrasts===void 0?{}:{contrasts:e.contrasts,contrastVariables:xt({response:n.response,rhs:s},Object.keys(r)).variables},c=bt(n,r,{...e.levels===void 0?{}:{levels:e.levels},dropUnusedLevels:!0,...a},s),{terms:l,frame:h}=c,m=c.colNames.map((f,i)=>i);if(m.length>0){let f=vt(c.x),i=new Set(f.pivot.slice(f.rank));m=m.filter(p=>!i.has(p))}let w=new Set(l.terms.flat()),N=l.variables.map((f,i)=>({name:f,v:i})).filter(({v:f})=>w.has(f)),u={};for(let{name:f,v:i}of N)h.variables[i].kind==="factor"&&(u[f]=e.contrasts?.[f]??"contr.treatment");return{x:c.x.map(f=>m.map(i=>f[i])),colNames:m.map(f=>c.colNames[f]),assign:m.map(f=>c.assign[f]),intercept:l.intercept,terms:l.terms.map((f,i)=>({label:l.termLabels[i],variables:f.map(p=>l.variables[p])})),variables:N.map(({name:f,v:i})=>({name:f,frame:h.variables[i]})),contrasts:u}}var se=(t,r=t[0]?.length??0)=>Array.from({length:r},(e,n)=>t.map(o=>o[n]));function re(t,r,e=r[0]?.length??0){return t.map(n=>{let o=new Array(e).fill(0);return n.forEach((s,a)=>{let c=r[a];for(let l=0;l<e;l++)o[l]+=s*c[l]}),o})}function _e(t,r){let e=Array.from({length:r},()=>new Array(r).fill(0));for(let n=0;n<r;n++)for(let o=n;o<r;o++){let s=0;for(let a of t)s+=a[n]*a[o];e[n][o]=s,e[o][n]=s}return e}var Dt=(t,r)=>t.map(e=>r.map(n=>e[n])),ze=1e-6;function qe(t){let r=t.length,e=Array.from({length:r},(o,s)=>Array.from({length:r},(a,c)=>s===c?1:0)),n=Array.from({length:r},()=>new Array(r).fill(0));for(let o=0;o<r;o++){for(let a=0;a<r;a++){let c=t[o][a];for(let l=0;l<o;l++)c-=e[o][l]*n[l][a];n[o][a]=c}let s=n[o][o];for(let a=o+1;a<r;a++){if(Math.abs(s)<ze){e[a][o]=0;continue}let c=t[a][o];for(let l=0;l<o;l++)c-=e[a][l]*n[l][o];e[a][o]=c/s}}for(let o of[e,n])for(let s of o)s.forEach((a,c)=>{Math.abs(a)<ze&&(s[c]=0)});return{L:e,U:n}}function Ve(t){let r=new Set(t.variables.filter(n=>n.frame.kind==="numeric").map(n=>n.name)),e=n=>n.variables.filter(o=>r.has(o)).sort().join("\0");return t.terms.map(n=>t.terms.map((o,s)=>({b:o,k:s})).filter(({b:o})=>n.variables.every(s=>o.variables.includes(s))&&o.variables.length>n.variables.length&&e(n)===e(o)).map(({k:o})=>o))}var yt=(t,r)=>t.flatMap((e,n)=>e===r+1?[n]:[]);function No(t,r,e,n,o){return{term:t,rows:e.map(s=>n[s]),columns:[...o],values:e.map(s=>[...r[s]])}}function Eo(t){let r=t.colNames.length;if(r===0||r===1&&t.intercept)return[];let e=r===1?[[1]]:se(qe(_e(t.x,r)).L,r);return t.terms.map((n,o)=>No(n.label,e,yt(t.assign,o),t.colNames,t.colNames))}function Mo(t){let r=t.colNames.length,e=Ve(t);return t.terms.map((n,o)=>{let s=yt(t.assign,o);if(e[o].length===0){let u=s.map(f=>Array.from({length:r},(i,p)=>f===p?1:0));return{term:n.label,rows:s.map(f=>t.colNames[f]),columns:[...t.colNames],values:u}}let a=[o,...e[o]].flatMap(u=>yt(t.assign,u)),c=new Set(a),l=[...t.colNames.map((u,f)=>f).filter(u=>!c.has(u)),...a],h=se(qe(_e(Dt(t.x,l),r)).L,r),m=new Set(s),w=l.flatMap((u,f)=>m.has(u)?[f]:[]),N=w.map(u=>{let f=new Array(r).fill(0);return l.forEach((i,p)=>f[i]=h[u][p]),f});return{term:n.label,rows:w.map(u=>t.colNames[l[u]]),columns:[...t.colNames],values:N}})}function So(t){let r=t.x.length,e=new Map(t.variables.map(l=>[l.name,l.frame])),n=[],o=[];t.intercept&&(n.push(new Array(r).fill(1)),o.push("(Intercept)"));let s=l=>{let h=e.get(l);return h.kind==="numeric"?[{name:l,values:h.values}]:h.levels.map((m,w)=>({name:l+m,values:h.codes.map(N=>N===w?1:0)}))};for(let l of t.terms){let h=s(l.variables[0]);for(let m of l.variables.slice(1)){let w=[];for(let N of s(m))for(let u of h)w.push({name:`${u.name}:${N.name}`,values:u.values.map((f,i)=>f*N.values[i])});h=w}for(let m of h)n.push(m.values),o.push(m.name)}let a=o.filter((l,h)=>n[h].every(m=>m===0)),c=a.length>0?[`Missing cells for: ${a.join(", ")}.
4
- Interpret type III hypotheses with care.`]:[];return{x:Array.from({length:r},(l,h)=>n.map(m=>m[h])),colNames:o,messages:c}}function ko(t){let r={},e={};for(let{name:h,frame:m}of t.variables)m.kind==="numeric"?r[h]=m.values:(r[h]=m.codes.map(w=>m.levels[w]),e[h]=m.levels);let n=h=>h.reduce((m,w)=>m===void 0?w:{type:"binary",op:"+",left:m,right:w},void 0),o=h=>({type:"var",name:h}),s={response:void 0,rhs:n(t.variables.map(h=>o(h.name)))??ne},a=t.terms.map(h=>h.variables.map(o).reduce((m,w)=>({type:"binary",op:":",left:m,right:w}))),c=bt(s,r,{levels:e,intercept:t.intercept},n(a)??ne),l=c.colNames.map((h,m)=>m);if(l.length>0){let h=vt(c.x);h.rank<l.length&&(l=h.pivot.slice(0,h.rank))}return{x:Dt(c.x,l),colNames:l.map(h=>c.colNames[h]),assign:l.map(h=>c.assign[h])}}var Io=1e-8;function To(t){let r=Object.values(t.contrasts).every(u=>u==="contr.treatment"),e=r?{x:t.x,colNames:t.colNames,assign:t.assign}:ko(t),n=e.colNames.length,o=So(t),s=zt(e.x),a=Ct(se(re(s,o.x,o.colNames.length),o.colNames.length)),c=Ve(t),l=t.terms.map((u,f)=>({k:f,order:u.variables.length})).sort((u,f)=>f.order-u.order||u.k-f.k);for(let{k:u}of l){if(c[u].length===0)continue;let f=yt(e.assign,u);if(f.length===0)continue;let i=c[u].flatMap(A=>yt(e.assign,A)),p=Dt(a,f);i.length>0&&(p=pe(vt(Dt(a,i)),p));let S=Ct(p);a.forEach((A,x)=>f.forEach((E,I)=>A[E]=S[x][I]))}let h=new Map(o.colNames.map((u,f)=>[u,f])),m=e.colNames.map(u=>{let f=h.get(u);if(f===void 0)throw new Error(`lmerHypotheses: the column '${u}' of the model matrix is not a column of the full-dummy matrix`);return a[f]}),w=r?void 0:Ct(re(s,t.x,t.colNames.length));return{hypotheses:t.terms.map((u,f)=>{let i=yt(e.assign,f).filter(S=>m.reduce((A,x)=>A+Math.abs(x[S]),0)>Io),p=i.map(S=>m.map(A=>A[S]));return{term:u.label,rows:i.map(S=>e.colNames[S]),columns:[...t.colNames],values:w===void 0?p:re(p,w,t.colNames.length)}}),messages:o.messages}}function Ce(t,r="III"){return t.colNames.length<=1||t.terms.length<=1||r==="I"?{hypotheses:Eo(t),messages:[]}:r==="II"?{hypotheses:Mo(t),messages:[]}:To(t)}var Fo={I:"I",II:"II",III:"III",1:"I",2:"II",3:"III"};function Oo(t,r){let{structure:e,data:n,options:o}=dt(t),s={};o.contrasts!==void 0&&(s.contrasts=o.contrasts),o.levels!==void 0&&(s.levels=o.levels);let a=Le(t.formula,n,s);if(a.colNames.length!==e.xColNames.length||a.colNames.some((c,l)=>c!==e.xColNames[l]))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:Ce(a,r),termLabels:a.terms.map(c=>c.label)}}function Ro(t,{hypotheses:r},e,n){let o=[],s=[];for(let a of r){let c=a.values.length===0?void 0:oe(t,a.values,void 0,n);c!==void 0&&(o.push(a.term),s.push(c))}for(let a of e)o.includes(a)||(o.push(a),s.push({sumSq:NaN,meanSq:NaN,numDF:NaN,denDF:NaN,F:NaN,pValue:NaN}));return{terms:o,rows:s}}function jo(t,r){let e=Fo[String(r.type??"III")];if(e===void 0)throw new RangeError(`type must be "I", "II" or "III", not ${JSON.stringify(r.type)}`);let{hypotheses:n,termLabels:o}=Oo(t,e),s=It(t),a=new At;s.warnings.forEach(m=>a.add(m));let{terms:c,rows:l}=Ro(s,n,o,a),h=m=>l.map(w=>w[m]);return{type:e,terms:c,sumSq:h("sumSq"),meanSq:h("meanSq"),numDF:h("numDF"),denDF:h("denDF"),F:h("F"),pValue:h("pValue"),messages:[...n.messages],warnings:a.list}}function zo(t,r){if(t.length<2)throw new TypeError("lmerAnova needs one fit, or an array of at least two fits to compare");let e=t.map(dt);if(t.some(i=>i.nObs!==t[0].nObs))throw new Error("models were not all fitted to the same size of dataset");if(e.some(i=>i.data!==e[0].data))throw new Error("all models must be fit to the same data object");let n=new At,o=[],s=t.map((i,p)=>`MODEL${p+1}`);if(r.names!==void 0){if(r.names.length!==t.length||r.names.some(i=>typeof i!="string"))throw new TypeError(`names must be ${t.length} strings, one per fit`);new Set(r.names).size===r.names.length?s=[...r.names]:n.add("failed to find model names, assigning generic names")}let a=r.refit??!0,c;a?(t.some(i=>i.REML)&&o.push("refitting model(s) with ML (instead of REML)"),c=t.map((i,p)=>{if(!i.REML)return i.logLik;let{data:S,options:A,criterion:x}=e[p];return Kt(i.formula,S,{...A,REML:!1},x).logLik})):(t.some(i=>i.REML)&&t.some(i=>!i.REML)&&n.add("some models fit with REML = TRUE, some not"),c=t.map(i=>i.logLik));let l=t.map((i,p)=>p).sort((i,p)=>t[i].df-t[p].df||i-p),h=t[0].nObs,m=l.map(i=>t[i].df),w=l.map(i=>c[i]),N=w.map((i,p)=>p===0?NaN:2*Math.max(0,i-w[p-1])),u=m.map((i,p)=>p===0?NaN:i-m[p-1]),f=N.map((i,p)=>p===0||u[p]===0?NaN:ce(i,u[p]/2,2,!1,!1));return{names:l.map(i=>s[i]),npar:m,AIC:w.map((i,p)=>-2*i+2*m[p]),BIC:w.map((i,p)=>-2*i+Math.log(h)*m[p]),logLik:w,deviance:w.map(i=>-2*i),Chisq:N,Df:u,pValue:f,messages:o,warnings:n.list}}function Lo(t,r={}){return Array.isArray(t)?zo(t,r):jo(t,r)}export{lo as lmer,Lo as lmerAnova,vo as lmerContest,go as lmerSummary};
1
+ /*! rStats.js 0.15.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 xt,c as St}from"./chunks/chunk-GZTLODPK.js";import{c as me,s as ue}from"./chunks/chunk-LVXAWWUX.js";import{b as qt}from"./chunks/chunk-7GEYDIQC.js";import{A as be,C as ye,E as Xt,h as pt,k as we,l as ve}from"./chunks/chunk-VAPGDPHM.js";import{a as Lt,c as zt,d as Nt,f as de,g as Et}from"./chunks/chunk-A3XZUJCP.js";import{k as pe,l as he}from"./chunks/chunk-VSKWQMSG.js";import{b as ge}from"./chunks/chunk-WEWXYHPR.js";import{b as Bt}from"./chunks/chunk-HNLKOICT.js";import"./chunks/chunk-LU5FHU2O.js";import"./chunks/chunk-CMPDE3SZ.js";import"./chunks/chunk-4EOKNOXP.js";var xe=1,Ae=new WeakMap;function Xe(t){let n=Ae.get(t);if(n!==void 0)return n;let e=t.y.length,r=t.xColNames.length,o=t.q,a=t.terms.length,c=t.terms.reduce((w,O)=>w+O.nLevels,0),s=new Int32Array(c+1),f=new Int32Array(c),h=[],p=[],g=0;t.terms.forEach((w,O)=>{let q=w.columns.length;h.push(g),p.push(q);for(let L=0;L<w.nLevels;L++,g++)s[g]=w.zOffset+L*q,f[g]=O}),s[c]=o;let A=t.terms.map((w,O)=>{let q=p[O],L=new Int32Array(q*q).fill(-1);for(let{row:N,col:z,theta:F}of t.lambda)z>=w.zOffset&&z<w.zOffset+q&&(L[N-w.zOffset+(z-w.zOffset)*q]=F);return L}),i=t.terms.map(w=>t.groups[w.group].codes),u=new Int32Array(e+1);for(let w=0;w<e;w++){let O=0;for(let q of t.terms)for(let L of q.matrix[w])L!==0&&O++;u[w+1]=u[w]+O}let l=u,m=l[e],M=new Int32Array(m),T=new Float64Array(m);{let w=0;for(let O=0;O<e;O++)t.terms.forEach((q,L)=>{let N=q.zOffset+i[L][O]*p[L];q.matrix[O].forEach((z,F)=>{z!==0&&(M[w]=N+F,T[w++]=z)})})}let S=new Map,y=[],k=[],d=(w,O)=>{let q=O*c+w,L=S.get(q);return L===void 0&&(L=y.length,S.set(q,L),y.push(w),k.push(O)),L};for(let w=0;w<c;w++)d(w,w);let v=new Int32Array(a),x=a*(a+1)/2,b=new Int32Array(e*x);for(let w=0;w<e;w++){for(let O=0;O<a;O++)v[O]=h[O]+i[O][w];for(let O=0;O<a;O++)for(let q=0;q<=O;q++)b[w*x+O*(O+1)/2+q]=d(v[O],v[q])}let E=y.length,D=Array.from({length:E},(w,O)=>O).sort((w,O)=>k[w]-k[O]||y[w]-y[O]),R=new Int32Array(E);D.forEach((w,O)=>R[w]=O);let j=new Int32Array(c+1),tt=new Int32Array(E),st=new Int32Array(E+1);D.forEach((w,O)=>{tt[O]=y[w],j[k[w]+1]++;let q=p[f[y[w]]]*p[f[k[w]]];st[O+1]=st[O]+q});for(let w=0;w<c;w++)j[w+1]+=j[w];let et=new Float64Array(st[E]),nt=new Float64Array(o*r),$=new Float64Array(o),P=new Float64Array(r);for(let w=0;w<e;w++){let O=t.x[w],q=t.y[w];for(let L=0;L<a;L++){let N=t.terms[L].matrix[w],z=p[L];for(let F=0;F<=L;F++){let Z=t.terms[F].matrix[w],J=p[F],U=R[b[w*x+L*(L+1)/2+F]],rt=st[U];for(let lt=0;lt<J;lt++){let Rt=Z[lt];if(Rt!==0)for(let mt=0;mt<z;mt++)et[rt+mt+lt*z]+=N[mt]*Rt}}}for(let L=l[w];L<l[w+1];L++){let N=M[L],z=T[L];for(let F=0;F<r;F++)nt[N+F*o]+=z*O[F];$[N]+=z*q}for(let L=0;L<r;L++)P[L]+=O[L]*q}let Y=new Array(c),X=Array.from({length:c},()=>[]),I=new Int32Array(c).fill(-1);for(let w=0;w<c;w++){let O=[],q=N=>{N!==w&&I[N]!==w&&(I[N]=w,O.push(N))};for(let N=j[w]+1;N<j[w+1];N++)q(tt[N]);for(let N of X[w])for(let z of Y[N])q(z);let L=Int32Array.from(O).sort();Y[w]=L,L.length>0&&X[L[0]].push(w)}let C=new Int32Array(E);{let w=new Int32Array(c);for(let O=0;O<c;O++){let q=0;for(let L of Y[O])w[L]=q,q+=s[L+1]-s[L];for(let L=j[O]+1;L<j[O+1];L++)C[L]=w[tt[L]]}}let G=new Int32Array(o+1);for(let w=0;w<c;w++){let O=0;for(let q of Y[w])O+=s[q+1]-s[q];for(let q=s[w];q<s[w+1];q++)G[q+1]=G[q]+(s[w+1]-q)+O}let _=new Int32Array(G[o]),V=new Int32Array(o+1);for(let w=0;w<c;w++)for(let O=s[w];O<s[w+1];O++){let q=G[O];for(let L=O;L<s[w+1];L++)_[q++]=L;for(let L of Y[w])for(let N=s[L];N<s[L+1];N++)_[q++]=N}for(let w=0;w<o;w++)for(let O=G[w]+1;O<G[w+1];O++)V[_[O]+1]++;for(let w=0;w<o;w++)V[w+1]+=V[w];let K=V.slice(),at=V.slice(0,o),W=new Int32Array(K[o]),B=new Int32Array(K[o]);for(let w=0;w<o;w++)for(let O=G[w]+1;O<G[w+1];O++){let q=at[_[O]]++;W[q]=w,B[q]=O}let ot={n:e,p:r,q:o,nGroups:c,groupStart:s,groupTerm:f,termNc:p,termTheta:A,blockStart:j,blockRow:tt,blockValueStart:st,zz:et,zx:nt,zy:$,xy:P,rowStart:l,rowCols:M,rowValues:T,colStart:G,colRows:_,rowListStart:K,rowListCol:W,rowListPos:B,blockPatternOffset:C};return Ae.set(t,ot),ot}function Ye(t,n){return t.termTheta.map(e=>Float64Array.from(e,r=>r<0?0:n[r]))}function dt(t,n,e){let r=new Float64Array(t.q);for(let o=0;o<t.nGroups;o++){let a=t.groupTerm[o],c=t.termNc[a],s=n[a],f=t.groupStart[o];for(let h=0;h<c;h++){let p=e[f+h];if(p!==0)for(let g=h;g<c;g++)r[f+g]+=s[g+h*c]*p}}return r}function ht(t,n,e,r=1){let o=t.q,a=new Float64Array(o*r);for(let c=0;c<t.nGroups;c++){let s=t.groupTerm[c],f=t.termNc[s],h=n[s],p=t.groupStart[c];for(let g=0;g<r;g++){let A=p+g*o;for(let i=0;i<f;i++){let u=0;for(let l=i;l<f;l++)u+=h[l+i*f]*e[A+l];a[A+i]=u}}}return a}function Ke(t,n){let e=new Float64Array(t.zz.length),r=new Float64Array(64);for(let o=0;o<t.nGroups;o++){let a=t.groupTerm[o],c=t.termNc[a],s=n[a];for(let f=t.blockStart[o];f<t.blockStart[o+1];f++){let h=t.groupTerm[t.blockRow[f]],p=t.termNc[h],g=n[h],A=t.blockValueStart[f];if(p===1&&c===1)e[A]=g[0]*t.zz[A]*s[0];else{let i=r.length>=p*c?r:new Float64Array(p*c);for(let u=0;u<c;u++)for(let l=0;l<p;l++){let m=0;for(let M=u;M<c;M++)m+=t.zz[A+l+M*p]*s[M+u*c];i[l+u*p]=m}for(let u=0;u<c;u++)for(let l=0;l<p;l++){let m=0;for(let M=l;M<p;M++)m+=g[M+l*p]*i[M+u*p];e[A+l+u*p]=m}}if(f===t.blockStart[o])for(let i=0;i<c;i++)e[A+i+i*c]+=1}}return e}function Ze(t,n){let{q:e,colStart:r,colRows:o,rowListStart:a,rowListCol:c,rowListPos:s}=t,f=new Float64Array(o.length),h=new Float64Array(e);for(let p=0;p<t.nGroups;p++){let g=t.termNc[t.groupTerm[p]],A=t.groupStart[p];for(let i=0;i<g;i++){let u=A+i;for(let T=t.blockStart[p];T<t.blockStart[p+1];T++){let S=t.blockRow[T],y=t.termNc[t.groupTerm[S]],k=t.blockValueStart[T]+i*y,d=t.groupStart[S];for(let v=S===p?i:0;v<y;v++)h[d+v]=n[k+v]}for(let T=a[u];T<a[u+1];T++){let S=c[T],y=s[T],k=f[y];if(k===0)continue;let d=r[S+1];for(let v=y;v<d;v++)h[o[v]]-=f[v]*k}let l=h[u];if(!(l>0))throw new Error("the penalised least-squares matrix is not positive definite");let m=Math.sqrt(l),M=r[u];f[M]=m,h[u]=0;for(let T=M+1;T<r[u+1];T++){let S=o[T];f[T]=h[S]/m,h[S]=0}}}return f}function Mt(t,n,e,r=0){let{q:o,colStart:a,colRows:c}=t;for(let s=0;s<o;s++){let f=a[s],h=e[r+s]/=n[f];if(h!==0)for(let p=f+1;p<a[s+1];p++)e[r+c[p]]-=n[p]*h}}function kt(t,n,e,r=0){let{q:o,colStart:a,colRows:c}=t;for(let s=o-1;s>=0;s--){let f=a[s],h=e[r+s];for(let p=f+1;p<a[s+1];p++)h-=n[p]*e[r+c[p]];e[r+s]=h/n[f]}}function Je(t,n,e){for(let r=0;r<n;r++){let o=r*n,a=e[r];for(let c=0;c<r;c++)a-=t[c+o]*e[c];e[r]=a/t[r+o]}}function Qe(t,n,e){for(let r=n-1;r>=0;r--){let o=e[r]/=t[r+r*n],a=r*n;for(let c=0;c<r;c++)e[c]-=t[c+a]*o}}function Yt(t,n){let e=t.slice();for(let r=0;r<n;r++)for(let o=r+1;o<n;o++)e[o+r*n]=0;ye({nrow:n,ncol:n,data:e});for(let r=0;r<n;r++)for(let o=r+1;o<n;o++)e[o+r*n]=e[r+o*n];return e}function Kt(t,n,e,r={}){let o=Zt(t,n),{sys:a,t:c,l:s,rx:f,ws:h,xResiduals:p,res:g,uv:A}=o,{n:i,p:u,q:l}=a,m=Array.from(o.beta),M=Array.from(A),T=Array.from(o.b),S=0;for(let R of g)S+=R*R;let y=0;for(let R of M)y+=R*R;let k=S+y,d=0;for(let R=0;R<l;R++)d+=Math.log(s[a.colStart[R]]);d*=2;let v=0;for(let R=0;R<u;R++)v+=Math.log(f[R+R*u]);v*=2;let x=e?i-u:i,E={deviance:(e?d+v:d)+x*(1+Math.log(2*Math.PI*k/x)),beta:m,u:M,b:T,rss:S,ussq:y,prss:k,logDetL:d,logDetRX:v},D=r.gradient===!0||r.rxInverse===!0?Yt(f,u):void 0;return r.rxInverse===!0&&(E.rxInverse=Array.from({length:u},(R,j)=>Array.from({length:u},(tt,st)=>D[j+st*u]))),r.gradient===!0&&(E.gradient=to(t,a,c,s,n,e,{res:g,uv:A,prss:k,ws:h,xResiduals:p,sInverse:D})),E}function Zt(t,n){if(n.length!==t.theta.length)throw new RangeError(`theta must have length ${t.theta.length}, not ${n.length}`);if(n.some(d=>!Number.isFinite(d)))throw new RangeError("theta must be finite");let e=Xe(t),{n:r,p:o,q:a}=e,c=Ye(e,n),s=Ze(e,Ke(e,c)),f=d=>{let v=new Float64Array(r);for(let x=0;x<r;x++){let b=0;for(let E=e.rowStart[x];E<e.rowStart[x+1];E++)b+=e.rowValues[E]*d[e.rowCols[E]];v[x]=b}return v},h=d=>{let v=new Float64Array(a);for(let x=0;x<r;x++){let b=d[x];for(let E=e.rowStart[x];E<e.rowStart[x+1];E++)v[e.rowCols[E]]+=e.rowValues[E]*b}return v},p=d=>{Mt(e,s,d),kt(e,s,d)},g=ht(e,c,e.zx,o);for(let d=0;d<o;d++)Mt(e,s,g,d*a);let A=[],i=[];for(let d=0;d<o;d++){let v=g.slice(d*a,(d+1)*a);kt(e,s,v);let x=Float64Array.from(t.x,D=>D[d]),b=()=>{let D=f(dt(e,c,v));return x.map((R,j)=>R-D[j])},E=b();for(let D=0;D<xe;D++){let R=ht(e,c,h(E));for(let j=0;j<a;j++)R[j]-=v[j];p(R);for(let j=0;j<a;j++)v[j]+=R[j];E=b()}A.push(E),i.push(v)}let u=new Float64Array(o*o);for(let d=0;d<o;d++)for(let v=d;v<o;v++){let x=0,b=A[d],E=A[v];for(let j=0;j<r;j++)x+=b[j]*E[j];let D=i[d],R=i[v];for(let j=0;j<a;j++)x+=D[j]*R[j];u[d+v*o]=x,u[v+d*o]=x}let l={nrow:o,ncol:o,data:u};if(o>0&&be(l)>0)throw new Error("the fixed-effects matrix of the penalised least squares is not positive definite");let m=(d,v)=>{Mt(e,s,d);for(let x=0;x<o;x++){let b=v[x];for(let E=0;E<a;E++)b-=g[E+x*a]*d[E];v[x]=b}Je(u,o,v),Qe(u,o,v);for(let x=0;x<o;x++){let b=v[x];for(let E=0;E<a;E++)d[E]-=g[E+x*a]*b}kt(e,s,d)},M=(d,v)=>{let x=f(v),b=new Float64Array(r);for(let E=0;E<r;E++){let D=t.x[E],R=x[E];for(let j=0;j<o;j++)R+=D[j]*d[j];b[E]=t.y[E]-R}return b},T=ht(e,c,e.zy),S=Float64Array.from(e.xy);m(T,S);let y=dt(e,c,T),k=M(S,y);for(let d=0;d<xe;d++){let v=ht(e,c,h(k));for(let b=0;b<a;b++)v[b]-=T[b];let x=new Float64Array(o);for(let b=0;b<r;b++){let E=t.x[b],D=k[b];for(let R=0;R<o;R++)x[R]+=E[R]*D}m(v,x);for(let b=0;b<a;b++)T[b]+=v[b];for(let b=0;b<o;b++)S[b]+=x[b];y=dt(e,c,T),k=M(S,y)}return{sys:e,t:c,l:s,cx:g,rx:u,ws:i,xResiduals:A,res:k,uv:T,beta:S,b:y,solveNormal:m}}function Jt(t,n){let{q:e,colStart:r,colRows:o}=t,a=new Float64Array(n.length),c=new Int32Array(e).fill(-1),s=new Float64Array(e),f=new Float64Array(e);for(let h=e-1;h>=0;h--){let p=r[h],g=r[h+1];for(let u=p+1;u<g;u++){let l=o[u];c[l]=h,s[l]=n[u],f[l]=0}for(let u=p+1;u<g;u++){let l=o[u],m=n[u],M=r[l],T=a[M]*m;for(let S=M+1;S<r[l+1];S++){let y=o[S];if(c[y]!==h)continue;let k=a[S];f[y]+=k*m,T+=k*s[y]}f[l]+=T}let A=n[p],i=1/A;for(let u=p+1;u<g;u++){let l=-f[o[u]]/A;a[u]=l,i-=n[u]*l}a[p]=i/A}return a}function Ne(t,n,e){let{q:r,colStart:o,colRows:a,rowListStart:c,rowListCol:s,rowListPos:f}=t,h=new Float64Array(n.length),p=new Float64Array(r);for(let g=0;g<t.nGroups;g++){let A=t.termNc[t.groupTerm[g]],i=t.groupStart[g];for(let u=0;u<A;u++){let l=i+u;for(let S=t.blockStart[g];S<t.blockStart[g+1];S++){let y=t.blockRow[S],k=t.termNc[t.groupTerm[y]],d=t.blockValueStart[S]+u*k,v=t.groupStart[y];for(let x=y===g?u:0;x<k;x++)p[v+x]=e[d+x]}for(let S=c[l];S<c[l+1];S++){let y=s[S],k=f[S],d=n[k],v=h[k];if(d===0&&v===0)continue;let x=o[y+1];for(let b=k;b<x;b++)p[a[b]]-=h[b]*d+n[b]*v}let m=o[l],M=n[m],T=p[l]/(2*M);h[m]=T,p[l]=0;for(let S=m+1;S<o[l+1];S++){let y=a[S];h[S]=(p[y]-n[S]*T)/M,p[y]=0}}}return h}function Ee(t,n,e,r){let{q:o,colStart:a,colRows:c}=t,s=new Float64Array(n.length),f=new Int32Array(o).fill(-1),h=new Float64Array(o),p=new Float64Array(o),g=new Float64Array(o),A=new Float64Array(o);for(let i=o-1;i>=0;i--){let u=a[i],l=a[i+1];for(let y=u+1;y<l;y++){let k=c[y];f[k]=i,h[k]=n[y],p[k]=e[y],g[k]=0,A[k]=0}for(let y=u+1;y<l;y++){let k=c[y],d=n[y],v=e[y],x=a[k],b=r[x]*d,E=s[x]*d+r[x]*v;for(let D=x+1;D<a[k+1];D++){let R=c[D];if(f[R]!==i)continue;let j=r[D],tt=s[D];g[R]+=j*d,A[R]+=tt*d+j*v,b+=j*h[R],E+=tt*h[R]+j*p[R]}g[k]+=b,A[k]+=E}let m=n[u],M=e[u],T=1/m,S=-M/(m*m);for(let y=u+1;y<l;y++){let k=c[y],d=r[y],v=-A[k]/m+g[k]*M/(m*m);s[y]=v,T-=n[y]*d,S-=e[y]*d+n[y]*v}s[u]=S/m-T*M/(m*m)}return s}function to(t,n,e,r,o,a,{res:c,uv:s,prss:f,ws:h,xResiduals:p,sInverse:g}){let{n:A,p:i,q:u}=n,l=It(n,Jt(n,r),e),m=new Array(o.length).fill(0);n.termTheta.forEach((d,v)=>{let x=n.termNc[v];for(let b=0;b<x;b++)for(let E=b;E<x;E++)m[d[E+b*x]]+=2*l[v][b+E*x]});let M=new Float64Array(u);for(let d=0;d<A;d++){let v=c[d];for(let x=n.rowStart[d];x<n.rowStart[d+1];x++)M[n.rowCols[x]]+=n.rowValues[x]*v}let T,S;if(a&&i>0){T=new Float64Array(u*i);for(let d=0;d<i;d++)for(let v=0;v<i;v++){let x=g[v+d*i],b=h[v];for(let E=0;E<u;E++)T[E+d*u]+=b[E]*x}S=new Float64Array(i*u);for(let d=0;d<A;d++)for(let v=n.rowStart[d];v<n.rowStart[d+1];v++){let x=n.rowCols[v],b=n.rowValues[v];for(let E=0;E<i;E++)S[E+x*i]+=p[E][d]*b}}let k=2*(a?A-i:A)/f;for(let{row:d,col:v,theta:x}of t.lambda){let b=0;if(T!==void 0&&S!==void 0)for(let E=0;E<i;E++)b+=T[v+E*u]*S[E+d*i];m[x]-=2*b+k*M[d]*s[v]}return m}function It(t,n,e){let{colStart:r,zz:o}=t,a=t.termNc.map(f=>new Float64Array(f*f)),c=new Float64Array(16),s=new Float64Array(16);for(let f=0;f<t.nGroups;f++){let h=t.groupTerm[f],p=t.termNc[h],g=e[h],A=t.groupStart[f];for(let i=t.blockStart[f];i<t.blockStart[f+1];i++){let u=t.blockRow[i],l=t.groupTerm[u],m=t.termNc[l],M=e[l],T=t.blockValueStart[i];if(p===1&&m===1){let d=u===f?r[A]:r[A]+1+t.blockPatternOffset[i],v=n[d]*o[T];a[h][0]+=v*M[0],u!==f&&(a[l][0]+=v*g[0]);continue}let S=m*p;c.length<S&&(c=new Float64Array(S),s=new Float64Array(Math.max(S,m*m,p*p)));for(let d=0;d<p;d++){let v=r[A+d];if(u===f)for(let x=d;x<m;x++){let b=n[v+x-d];c[x+d*m]=b,c[d+x*m]=b}else{let x=v+(p-d)+t.blockPatternOffset[i];for(let b=0;b<m;b++)c[b+d*m]=n[x+b]}}for(let d=0;d<p;d++)for(let v=0;v<m;v++){let x=0;for(let b=v;b<m;b++)x+=M[b+v*m]*o[T+b+d*m];s[v+d*m]=x}let y=a[h];for(let d=0;d<p;d++)for(let v=0;v<p;v++){let x=0;for(let b=0;b<m;b++)x+=c[b+d*m]*s[b+v*m];y[d+v*p]+=x}if(u===f)continue;for(let d=0;d<m;d++)for(let v=0;v<p;v++){let x=0;for(let b=v;b<p;b++)x+=g[b+v*p]*o[T+d+b*m];s[v+d*p]=x}let k=a[l];for(let d=0;d<m;d++)for(let v=0;v<m;v++){let x=0;for(let b=0;b<p;b++)x+=c[d+b*m]*s[b+v*p];k[d+v*m]+=x}}}return a}function eo(t){let n=[],e=s=>{s.type==="var"?n.includes(s.name)||n.push(s.name):s.type==="binary"?(e(s.left),s.op!=="^"&&e(s.right)):(s.type==="unary"||s.type==="paren"||s.type==="Error")&&e(s.arg)};e(t);let{terms:r}=de(t,s=>n.indexOf(s),[]);if(r.length===0)throw new SyntaxError(`invalid formula: no grouping factor in '${Nt(t)}'`);let o=r.map((s,f)=>[s,f]).sort((s,f)=>s[0].length-f[0].length||s[1]-f[1]).map(([s])=>s.map(f=>n[f])),a=!1,c=s=>{s.type==="binary"?(s.op==="/"&&(a=!0),c(s.left),c(s.right)):(s.type==="unary"||s.type==="paren"||s.type==="Error")&&c(s.arg)};return c(t),a&&(o=o.map(s=>[...s].reverse()).reverse()),o}function oo(t){if(t.kind==="factor")return ge(t);let n=[...new Set(t.values)].sort((o,a)=>o-a),e=[],r=new Map;for(let o of n){let a=Bt(o);r.has(a)||(r.set(a,e.length),e.push(a))}return{levels:e,codes:t.values.map(o=>r.get(Bt(o)))}}function Se(t,n,e={}){let r=Lt(t,!1,!0);if(r.response===void 0)throw new TypeError("the formula has no response");let{fixed:o,bars:a}=zt(r.rhs);if(typeof n!="object"||n===null)throw new TypeError("data must be an object of columns");let c={...e.levels===void 0?{}:{levels:e.levels},dropUnusedLevels:!0},s=o??{type:"num",value:1,text:"1"},f={response:r.response,rhs:s},h=Et(r,Object.keys(n),s).variables,p=e.contrasts===void 0?{}:{contrasts:e.contrasts},g=xt(f,n,{...c,...p,extraVariables:h},s),{frame:A}=g,i=A.rows.length;if(i===0)throw new RangeError("0 (non-NA) cases");if(a.length===0)throw new Error("No random effects terms specified in formula");let u=[...g.terms.variables,...h.filter(I=>!g.terms.variables.includes(I))],l=[],m;a.forEach((I,C)=>{let G=[I.lhs];if(I.double){let V=Et(r,Object.keys(n),I.lhs);G=V.intercept?[{type:"num",value:1,text:"1"}]:[];for(let K of V.terms){let at=K.map(W=>({type:"var",name:V.variables[W]})).reduce((W,B)=>({type:"binary",op:":",left:W,right:B}));G.push({type:"binary",op:"+",left:{type:"num",value:0,text:"0"},right:at})}G.length===0&&(m??=C)}let _=eo(I.group);for(let V of G)for(let K of _)l.push({lhs:V,groupVariables:K,bar:C})});let M=new Map,T=I=>{let C=I.join(":"),G=M.get(C);if(G!==void 0)return G;let _=I.map(K=>oo(A.variables[u.indexOf(K)])),V;if(_.length===1)V=_[0];else{let K=new Map;for(let B=0;B<i;B++){let ot=_.map(w=>w.codes[B]);K.set(ot.join(","),ot)}let at=[...K.values()].sort((B,ot)=>{for(let w=0;w<B.length;w++)if(B[w]!==ot[w])return B[w]-ot[w];return 0}),W=new Map(at.map((B,ot)=>[B.join(","),ot]));V={levels:at.map(B=>B.map((ot,w)=>_[w].levels[ot]).join(":")),codes:Array.from({length:i},(B,ot)=>W.get(_.map(w=>w.codes[ot]).join(",")))}}return M.set(C,V),V},S=l.map(I=>({...I,name:I.groupVariables.join(":"),factor:T(I.groupVariables)})),y=S;S.some((I,C)=>C>0&&I.factor.levels.length>S[C-1].factor.levels.length)&&(y=S.map((I,C)=>[I,C]).sort((I,C)=>I[0].factor.levels.length-C[0].factor.levels.length||I[1]-C[1]).map(([I])=>I).reverse());let k=[],d=[],v=0,x=0;for(let I of y){let C=k.findIndex(K=>K.name===I.name);C<0&&(C=k.length,k.push({name:I.name,levels:I.factor.levels,codes:I.factor.codes}));let G=xt(r,n,c,I.lhs),_=G.colNames.length;_===0&&(m===void 0||I.bar<m)&&(m=I.bar);let V=I.factor.levels.length;d.push({label:`${Nt(I.lhs)} | ${I.name}`,group:C,columns:G.colNames,matrix:G.x,nLevels:V,zOffset:v,thetaOffset:x}),v+=V*_,x+=_*(_+1)/2}if(m!==void 0){let I=a[m];throw new Error(`lmer: the random-effects term '${Nt(I.lhs)} ${I.double?"||":"|"} ${Nt(I.group)}' has no columns`)}if(k.some(I=>I.levels.length<2))throw new RangeError("grouping factors must have > 1 sampled level");let b=k.filter(I=>I.levels.length>=i).map(I=>I.name);if(b.length>0)throw new RangeError(`number of levels of each grouping factor must be < number of observations (problems: ${b.join(", ")})`);for(let I of d){let C=I.nLevels*I.columns.length;if(i<=C)throw new RangeError(`number of observations (=${i}) <= number of random effects (=${C}) for term (${I.label}); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable`)}let E=[],D=[],R=[],j=[],tt=[];for(let I of d){let{levels:C,codes:G}=k[I.group],_=I.columns.length,V=Array.from({length:C.length*_},()=>({rows:[],values:[]}));for(let W=0;W<i;W++){let B=I.matrix[W],ot=G[W]*_;for(let w=0;w<_;w++){let O=B[w];if(O===0)continue;let q=V[ot+w];q.rows.push(W),q.values.push(O)}}for(let W=0;W<C.length;W++)for(let B=0;B<_;B++)E.push(V[W*_+B]),D.push(C[W]);let K=Array.from({length:_},()=>new Array(_).fill(-1)),at=I.thetaOffset;for(let W=0;W<_;W++)for(let B=W;B<_;B++)K[B][W]=at++,R.push(B===W?1:0),j.push(B===W?0:-1/0);for(let W=0;W<I.nLevels;W++){let B=I.zOffset+W*_;for(let ot=0;ot<_;ot++)for(let w=ot;w<_;w++)tt.push({row:B+w,col:B+ot,theta:K[w][ot]})}}let st=[],et=g.x,nt=g.colNames;if(nt.length>0){let I=pt(et),C=new Set(I.pivot.slice(I.rank));if(C.size>0){let G=nt.map((_,V)=>V).filter(_=>!C.has(_));et=et.map(_=>G.map(V=>_[V])),nt=G.map(_=>nt[_]),st.push(`fixed-effect model matrix is rank deficient so dropping ${C.size} ${C.size===1?"column / coefficient":"columns / coefficients"}`)}}let $=[...g.warnings],P=[];for(let I=0;I<nt.length;I++){let C=et.map(V=>V[I]);if(C.every(V=>V===0||V===1))continue;let G=C.reduce((V,K)=>V+K,0)/i,_=Math.sqrt(C.reduce((V,K)=>V+(K-G)**2,0)/(i-1));P.push(_)}let Y=Math.log(1e3);return P.some((I,C)=>Math.abs(Math.log(I))>Y||P.some((G,_)=>_!==C&&Math.abs(Math.log(I/G))>Y))&&$.push(`Some predictor variables are on very different scales: consider rescaling.
3
+ You may also use (g)lmerControl(autoscale = TRUE) to improve numerical stability.`),{rows:A.rows,y:g.y,xColNames:nt,x:et,terms:d,groups:k,q:E.length,z:E,zColLevels:D,theta:R,lower:j,lambda:tt,messages:st,warnings:$}}var Me=.25,no=1e-4,ro=1e-6,ke=1e-6,Ie=1e-4,so=.001,Te=1e-13,Oe=1e-10,ao=1e-9,co=500,Fe=.75,lo=1e-8,fo=50,io=1e-12,mo="lmer: the response is fitted exactly by the fixed effects (a constant response, for example), so the residual variance is 0",te="lmer: the minimisation of the criterion over theta did not converge";function Re(t){let n=t.length,e=t.map(o=>o.slice()),r=e.map((o,a)=>e.map((c,s)=>a===s?1:0));for(let o=0;o<100;o++){let a=0,c=0;for(let s=0;s<n;s++)for(let f=0;f<n;f++)c+=e[s][f]**2,s!==f&&(a+=e[s][f]**2);if(a<=1e-32*c)break;for(let s=0;s<n-1;s++)for(let f=s+1;f<n;f++){let h=e[s][f];if(h===0)continue;let p=(e[f][f]-e[s][s])/(2*h),g=Math.sign(p||1)/(Math.abs(p)+Math.sqrt(1+p*p)),A=1/Math.sqrt(1+g*g),i=g*A;for(let u=0;u<n;u++){let l=e[u][s],m=e[u][f];e[u][s]=A*l-i*m,e[u][f]=i*l+A*m}for(let u=0;u<n;u++){let l=e[s][u],m=e[f][u];e[s][u]=A*l-i*m,e[f][u]=i*l+A*m}for(let u=0;u<n;u++){let l=r[u][s],m=r[u][f];r[u][s]=A*l-i*m,r[u][f]=i*l+A*m}}}return{values:e.map((o,a)=>o[a]),vectors:e.map((o,a)=>r.map(c=>c[a]))}}var ee=t=>Math.sqrt(t.reduce((n,e)=>n+e*e,0)),gt=(t,n)=>t.reduce((e,r,o)=>e+r*n[o],0);function uo(t,n,e,r,o){let a=t.length,{values:c,vectors:s}=n,f=s.map(S=>gt(S,t)),h=S=>{let y=new Array(a).fill(0);return S.forEach((k,d)=>s[d].forEach((v,x)=>y[x]+=k*v)),y},p=Math.min(...c);if(p>-r&&c.every((S,y)=>S>r||Math.abs(f[y])<=o)){let S=h(c.map((y,k)=>y>r?-f[k]/y:0));if(ee(S)<=e)return{step:S,newton:!0}}let g=Math.max(0,-p),A=S=>Math.sqrt(c.reduce((y,k,d)=>y+(f[d]/(k+S))**2,0)),i=Math.max(r,1e-12*Math.max(...c.map(Math.abs),1));if(A(g+i)>=e){let S=g+i,y=g+ee(t)/e+Math.max(...c.map(Math.abs))+i;for(let k=0;k<200&&y-S>1e-15*y;k++){let d=(S+y)/2;A(d)>e?S=d:y=d}return{step:h(c.map((k,d)=>-f[d]/(k+y))),newton:!1}}let u=c.map(S=>S+g<=i),l=h(c.map((S,y)=>u[y]?0:-f[y]/(S+g))),m=c.indexOf(p),M=Math.sqrt(Math.max(0,e*e-gt(l,l))),T=-Math.sign(f[m])||1;return{step:l.map((S,y)=>S+T*M*s[m][y]),newton:!1}}function po(t){let n=[];for(let e of t.terms){let r=e.columns.length,o=e.thetaOffset;for(let a=0;a<r;a++){let c=[];for(let s=a;s<r;s++)c.push(o++);n.push(c)}}return n}var ho=(t,n,e)=>{let r=Kt(t,n,e,{gradient:!0});return{f:r.deviance,g:r.gradient}};function Qt(t,n,e,r,o=new Set){let a=t.lower.map(y=>y===0),c=y=>e(t,y,n),s=r.map((y,k)=>o.has(k)?0:y),{f,g:h}=c(s),p=new Set,g=new Set,A=()=>Math.max(1,...s.map(Math.abs)),i=()=>ao*Math.max(1,Math.abs(f)),u=Me*A(),l=1/0,m,M=y=>{let k=ro*A(),d=y.map(v=>{let x=Math.max(no*Math.abs(s[v]),k),b=s.slice();b[v]=s[v]+x;let E=s.slice();E[v]=s[v]-x;let D=c(b).g,R=c(E).g;return y.map(j=>(D[j]-R[j])/(2*x))});return y.map((v,x)=>y.map((b,E)=>(d[x][E]+d[E][x])/2))},T=y=>{let k=m===void 0||m.free.length!==y.length||m.free.some((X,I)=>X!==y[I]),d=k?M(y):m.h;m=void 0;let v=Re(d),x=ke*Math.max(...v.values.map(Math.abs)),b=y.map(X=>h[X]),{step:E,newton:D}=uo(b,v,u,x,i()),R=s.slice();y.forEach((X,I)=>R[X]=s[X]+E[I]);let j=Math.max(...E.map(Math.abs))/A(),tt=c(R);if(!Number.isFinite(tt.f))throw new Error(te);let st=-(gt(b,E)+gt(E,d.map(X=>gt(X,E)))/2),et=Math.abs(f-tt.f)>1e-10*Math.abs(f)?f-tt.f:-(gt(b,E)+gt(y.map(X=>tt.g[X]),E))/2,nt=st>0?et/st:et>=0?1:-1;(nt>.1||j<=Oe)&&(s=R,f=tt.f,h=tt.g);let $=ee(E);if(nt<.25?u=Math.max($/4,Te*A()):nt>.75&&$>=.99*u&&(u*=2),!D)return l=1/0,!1;let Y=y.every(X=>Math.abs(h[X])<=i())&&(j<=Te||k&&j<=Oe&&j>=.5*l);return nt>Fe&&nt<1/Fe&&j<.5*l&&(m={free:y,h:d}),l=j,Y},S=()=>{if(p.size===0)return!1;let y=s.map((D,R)=>R).filter(D=>!o.has(D)),k=Re(M(y)),d=ke*Math.max(...k.values.map(Math.abs)),v=k.values.indexOf(Math.min(...k.values));if(!(k.values[v]<-d))return!1;let x=k.vectors[v],b=y.filter((D,R)=>p.has(D)&&Math.abs(x[R])>1e-8);if(b.length===0)return!1;for(let D of b)p.delete(D),g.add(D);let E=so*A();return y.forEach((D,R)=>s[D]=s[D]+E*x[R]),{f,g:h}=c(s),!0};for(let y=0;y<co;y++){let k=s.map((b,E)=>E).filter(b=>!p.has(b)&&!o.has(b));if(k.length>0&&!T(k))continue;let d=k.filter(b=>a[b]&&!g.has(b)&&Math.abs(s[b])<Ie*A());if(d.length>0){for(let b of d)s[b]=0,p.add(b);({f,g:h}=c(s)),l=1/0,m=void 0;continue}let v=1e-12*Math.max(1,Math.abs(f)),x=[...p].filter(b=>Math.abs(h[b])>v);if(x.length>0)for(let b of x)p.delete(b),g.add(b);else if(!S()){for(let b of po(t))if(s[b[0]]<0)for(let E of b)s[E]=-s[E];return{theta:s.map(b=>b===0?0:b),f}}u=Math.max(u,Me*Ie*A()),l=1/0,m=void 0}throw new Error(te)}function go(t,n,e){let r=t.terms.map(a=>{let c=a.columns.length;return Array.from({length:c*(c+1)/2},(s,f)=>a.thetaOffset+f)}),o=Qt(t,n,e,t.theta);for(let a=0;a<fo;a++){let c;for(let s of r){if(s.every(h=>o.theta[h]===0))continue;let f=Qt(t,n,e,o.theta,new Set(s));(c===void 0||f.f<c.f)&&(c=f)}if(c===void 0||!(c.f<o.f-lo*Math.max(1,Math.abs(o.f))))return o.theta;o=Qt(t,n,e,c.theta)}throw new Error(te)}function wt(t,n,e,r){if(n===void 0)return{};if(typeof n!="object"||n===null||Array.isArray(n))throw new TypeError("options must be an object");let o=Object.keys(n),a=o.find(s=>r.includes(s));if(a!==void 0)throw new Error(`${t}: argument '${a}' is not supported`);let c=o.filter(s=>!e.includes(s));if(c.length>0)throw new TypeError(`unused argument${c.length===1?"":"s"} (${c.join(", ")})`);return n}function vt(t,n){if(t===void 0)return n;if(typeof t=="boolean")return t;if(typeof t=="number"&&!Number.isNaN(t))return t!==0;throw t===null?new TypeError("argument is of length zero"):new TypeError("argument is not interpretable as logical")}var wo=["REML","contrasts","levels"],vo=["weights","offset","subset","naAction","na.action","control","start","verbose","devFunOnly"];function bo(t,n,e){return oe(t,n,e,ho)}var je=new WeakMap;function bt(t){let n=typeof t=="object"&&t!==null?je.get(t):void 0;if(n===void 0)throw new TypeError("fit must be a result returned by lmer (not a copy of one)");return n}function oe(t,n,e,r){let o=wt("lmer",e,wo,vo),a=vt(o.REML,!0);e={...o,REML:a};let c={};e.contrasts!==void 0&&(c.contrasts=e.contrasts),e.levels!==void 0&&(c.levels=e.levels);let s=Se(t,n,c),f=s.y.length,h=s.xColNames.length,p=h===0?[]:we(pt(s.x),s.y),g=s.y.map(($,P)=>$-s.x[P].reduce((Y,X,I)=>Y+X*p[I],0)),A=$=>$.reduce((P,Y)=>Math.max(P,Math.abs(Y)),0);if(A(g)<=io*A(s.y))throw new Error(mo);let i={...s,y:g},u=go(i,a,r),l=Kt(i,u,a,{rxInverse:!0}),m=l.beta.map(($,P)=>$+p[P]),M=2*Math.PI,T=l.logDetL+f*(1+Math.log(M*l.prss/f)),S=l.logDetL+l.logDetRX+(f-h)*(1+Math.log(M*l.prss/(f-h))),y=l.prss/(a?f-h:f),k=Math.sqrt(y),d=l.rxInverse.map($=>$.map(P=>y*P)),v=d.map(($,P)=>Math.sqrt($[P])),x=m.map(($,P)=>$/v[P]),b=i.terms.map($=>{let P=$.columns.length,Y=Array.from({length:P},()=>new Array(P).fill(0)),X=$.thetaOffset;for(let _=0;_<P;_++)for(let V=_;V<P;V++)Y[V][_]=u[X++];let I=Y.map(_=>Y.map(V=>y*_.reduce((K,at,W)=>K+at*V[W],0))),C=I.map((_,V)=>Math.sqrt(_[V])),G=I.map((_,V)=>_.map((K,at)=>V===at?1:K/(C[V]*C[at])));return{group:i.groups[$.group].name,columns:[...$.columns],sd:C,corr:G}}),E=i.groups.map(($,P)=>{let Y=i.terms.filter(X=>X.group===P);return{group:$.name,levels:[...$.levels],columns:Y.flatMap(X=>X.columns),values:$.levels.map((X,I)=>Y.flatMap(C=>{let G=C.columns.length;return C.columns.map((_,V)=>l.b[C.zOffset+I*G+V])}))}}),D=i.x.map($=>$.reduce((P,Y,X)=>P+Y*m[X],0)),R=i.x.map(($,P)=>g[P]-$.reduce((Y,X,I)=>Y+X*l.beta[I],0));i.z.forEach(($,P)=>{let Y=l.b[P];Y!==0&&$.rows.forEach((X,I)=>{D[X]+=$.values[I]*Y,R[X]-=$.values[I]*Y})});let j=-(a?S:T)/2,tt=h+u.length+1,st=u.some(($,P)=>i.lower[P]===0&&$<1e-4),et=[...i.messages];st&&et.push("boundary (singular) fit: see help('isSingular')");let nt={formula:t,REML:a,nObs:f,rows:[...i.rows],theta:u,fixefNames:[...i.xColNames],fixef:m,coefTable:{estimate:[...m],stdError:v,tValue:x},vcov:d,sigma:k,varCorr:b,ranef:E,fitted:D,residuals:R,logLik:j,df:tt,AIC:-2*j+2*tt,BIC:-2*j+Math.log(f)*tt,deviance:T,REMLcrit:S,singular:st,messages:et,warnings:[...i.warnings]};return je.set(nt,{structure:i,beta0:p,data:n,options:e,reml:a,criterion:r,theta:[...u]}),nt}function _e(t,n,e){let r=new Float64Array(t.zz.length),o=new Float64Array(64);for(let a=0;a<t.nGroups;a++){let c=t.groupTerm[a],s=t.termNc[c],f=e[c];for(let h=t.blockStart[a];h<t.blockStart[a+1];h++){let p=t.groupTerm[t.blockRow[h]],g=t.termNc[p],A=n[p],i=t.blockValueStart[h];o.length<g*s&&(o=new Float64Array(g*s));for(let u=0;u<s;u++)for(let l=0;l<g;l++){let m=0;for(let M=0;M<s;M++)m+=t.zz[i+l+M*g]*f[M+u*s];o[l+u*g]=m}for(let u=0;u<s;u++)for(let l=0;l<g;l++){let m=0;for(let M=0;M<g;M++)m+=A[M+l*g]*o[M+u*g];r[i+l+u*g]=m}}}return r}function Tt(t,n,e){let r=new Float64Array(e*e);for(let o=0;o<e;o++)for(let a=0;a<e;a++){let c=n[a+o*e];if(c!==0)for(let s=0;s<e;s++)r[s+o*e]+=t[s+a*e]*c}return r}var ne=(t,n)=>{let e=0;for(let r=0;r<n;r++)e+=t[r+r*n];return e},re=(t,n)=>Array.from({length:n},(e,r)=>Array.from({length:n},(o,a)=>t[r+a*n]));function Le(t,n,e){let r=Zt(t,n),{sys:o,t:a,l:c,rx:s,ws:f,xResiduals:h,res:p,uv:g}=r,{n:A,p:i,q:u}=o,l=n.length,m=l+1,M=N=>{let z=new Float64Array(A);for(let F=0;F<A;F++){let Z=0;for(let J=o.rowStart[F];J<o.rowStart[F+1];J++)Z+=o.rowValues[J]*N[o.rowCols[J]];z[F]=Z}return z},T=N=>{let z=new Float64Array(u);for(let F=0;F<A;F++){let Z=N[F];if(Z!==0)for(let J=o.rowStart[F];J<o.rowStart[F+1];J++)z[o.rowCols[J]]+=o.rowValues[J]*Z}return z},S=N=>{let z=new Float64Array(i);for(let F=0;F<A;F++){let Z=t.x[F],J=N[F];for(let U=0;U<i;U++)z[U]+=Z[U]*J}return z},y=N=>{let z=new Float64Array(A);for(let F=0;F<A;F++){let Z=t.x[F],J=0;for(let U=0;U<i;U++)J+=Z[U]*N[U];z[F]=J}return z},k=N=>{Mt(o,c,N),kt(o,c,N)},d=Array.from({length:l},()=>[]),v=Array.from({length:l},()=>[]);for(let{row:N,col:z,theta:F}of t.lambda)d[F].push(N),v[F].push(z);let x=(N,z)=>{let F=new Float64Array(u),Z=d[N],J=v[N];for(let U=0;U<Z.length;U++)F[Z[U]]+=z[J[U]];return F},b=(N,z)=>{let F=new Float64Array(u),Z=d[N],J=v[N];for(let U=0;U<Z.length;U++)F[J[U]]+=z[Z[U]];return F},E=(N,z,F)=>{let Z=d[N],J=v[N],U=0;for(let rt=0;rt<Z.length;rt++)U+=z[Z[rt]]*F[J[rt]];return U},D=(N,z,F,Z,J)=>{for(let U=0;U<i;U++)for(let rt=0;rt<i;rt++){let lt=J*E(N,z[U],F[rt]);Z[U+rt*i]+=lt,Z[rt+U*i]+=lt}},R=0;for(let N of p)R+=N*N;let j=0;for(let N of g)j+=N*N;let tt=R+j,st=e?A-i:A,et=tt/st,nt=Math.sqrt(et),$=0;for(let N=0;N<u;N++)$+=Math.log(c[o.colStart[N]]);$*=2;let P=0;for(let N=0;N<i;N++)P+=Math.log(s[N+N*i]);P*=2;let X=(e?$+P:$)+tt/et+st*Math.log(2*Math.PI*et),I=Yt(s,i),C=Jt(o,c),G=T(p),_=h.map(T),V=It(o,C,a),K=new Array(l).fill(0),at=(N,z,F)=>{o.termTheta.forEach((Z,J)=>{let U=o.termNc[J];for(let rt=0;rt<U;rt++)for(let lt=rt;lt<U;lt++)z[Z[lt+rt*U]]+=F*N[J][rt+lt*U]})};at(V,K,2);let W=Array.from({length:l},(N,z)=>-2*E(z,G,g)),B=Array.from({length:l},(N,z)=>{let F=new Float64Array(i*i);return D(z,_,f,F,-1),F});if(e)for(let N=0;N<l;N++)K[N]+=ne(Tt(I,B[N],i),i);let ot=B.map(N=>Tt(I,N,i)),w=Array.from({length:m},()=>new Array(m).fill(0));for(let N=0;N<l;N++){let z=o.termTheta.map(H=>Float64Array.from(H,ut=>ut===N?1:0)),F=_e(o,z,a),Z=_e(o,a,z);for(let H=0;H<F.length;H++)F[H]+=Z[H];let J=Ne(o,c,F),U=Ee(o,c,J,C),rt=new Array(l).fill(0),lt=It(o,U,a),Rt=It(o,C,z);if(at(lt,rt,2),at(Rt,rt,2),e&&i>0){let H=[],ut=[];for(let ct=0;ct<i;ct++){let it=b(N,_[ct]),Q=ht(o,a,T(M(x(N,f[ct]))));for(let ft=0;ft<u;ft++)it[ft]-=Q[ft];k(it),H.push(it);let ie=x(N,f[ct]),Be=dt(o,a,it);for(let ft=0;ft<u;ft++)ie[ft]+=Be[ft];let Wt=M(ie);for(let ft=0;ft<A;ft++)Wt[ft]=-Wt[ft];ut.push(T(Wt))}for(let ct=0;ct<l;ct++){let it=new Float64Array(i*i);D(ct,ut,f,it,-1),D(ct,_,H,it,-1),rt[ct]+=ne(Tt(I,it,i),i)-ne(Tt(ot[ct],ot[N],i),i)}}let mt=M(x(N,g)),Ut=b(N,G),Ue=ht(o,a,T(mt));for(let H=0;H<u;H++)Ut[H]-=Ue[H];let jt=S(mt);for(let H=0;H<i;H++)jt[H]=-jt[H];let yt=Ut.slice(),_t=jt.slice();r.solveNormal(yt,_t);{let H=M(dt(o,a,yt)),ut=y(_t);for(let Q=0;Q<A;Q++)H[Q]+=ut[Q];let ct=ht(o,a,T(H)),it=S(H);for(let Q=0;Q<u;Q++)ct[Q]=Ut[Q]-ct[Q]-yt[Q];for(let Q=0;Q<i;Q++)it[Q]=jt[Q]-it[Q];r.solveNormal(ct,it);for(let Q=0;Q<u;Q++)yt[Q]+=ct[Q];for(let Q=0;Q<i;Q++)_t[Q]+=it[Q]}let Ht=M(dt(o,a,yt)),He=y(_t);for(let H=0;H<A;H++)Ht[H]=-(mt[H]+Ht[H]+He[H]);let We=T(Ht);for(let H=0;H<l;H++){let ut=-2*(E(H,We,g)+E(H,G,yt));w[H][N]=rt[H]+ut/et}}for(let N=0;N<l;N++){for(let F=N+1;F<l;F++){let Z=(w[N][F]+w[F][N])/2;w[N][F]=Z,w[F][N]=Z}let z=-2*W[N]/(et*nt);w[N][l]=z,w[l][N]=z}w[l][l]=6*tt/(et*et)-2*st/et;let O=[...K.map((N,z)=>N+W[z]/et),-2*tt/(et*nt)+2*st/nt],q=re(I.map(N=>et*N),i),L=[...B.map((N,z)=>re(Tt(ot[z],I,i).map(F=>-et*F),i)),re(I.map(N=>2*nt*N),i)];return{varpar:[...n,nt],deviance:X,gradient:O,phiGradient:K,prssGradient:W,prss:tt,hessian:w,beta:Array.from(r.beta),vcov:q,jacobian:L}}var se=1e-8,yo=Math.sqrt(Number.EPSILON),xo=1e-8,ze=new WeakMap;function qe(t){if(!Number.isFinite(t))return Number.isNaN(t)?"nan":t>0?"inf":"-inf";let[n,e]=t.toExponential(1).split("e"),r=Number(e);return`${n}e${r<0?"-":"+"}${String(Math.abs(r)).padStart(2,"0")}`}function Ao(t){let n=[],e=t.filter(a=>a<-se),r=t.filter(a=>Math.abs(a)<se),o=a=>a===1?"eigenvalue":"eigenvalues";return e.length>0&&n.push(`Model failed to converge with ${e.length} negative ${o(e.length)}: ${e.map(qe).join(" ")}`),r.length>0&&n.push(`Model may not have converged with ${r.length} ${o(r.length)} close to zero: ${r.map(qe).join(" ")}`),n}function Ft(t){let n=ze.get(t);if(n!==void 0)return n;let e=bt(t),r=No(e.structure,e.theta,e.reml,e.beta0);return ze.set(t,r),r}function No(t,n,e,r=[]){let o=Le(t,n,e),a=o.varpar.length,c=Xt(o.hessian,{symmetric:!0}),s=c.values,f=c.vectors,h=Array.from({length:a},()=>new Array(a).fill(0));return s.forEach((p,g)=>{if(p>se)for(let A=0;A<a;A++){let i=2*f[A][g]/p;for(let u=0;u<a;u++)h[A][u]+=i*f[u][g]}}),{varpar:o.varpar,hessian:o.hessian,hessianEigenvalues:s,vcovVarpar:h,beta:o.beta.map((p,g)=>p+(r[g]??0)),sigma:o.varpar[a-1],vcov:o.vcov,jacobian:o.jacobian,warnings:Ao(s)}}var Ot=(t,n)=>{let e=0;for(let r=0;r<t.length;r++){let o=t[r];if(o===0)continue;let a=n[r];for(let c=0;c<t.length;c++)e+=o*a[c]*t[c]}return e},Vt=(t,n)=>t.reduce((e,r,o)=>e+r*n[o],0);function Dt(t,n,e,r=t.beta){if(n.some(Number.isNaN)||Number.isNaN(e))return{estimate:NaN,stdError:NaN,df:NaN,tValue:NaN,pValue:NaN};let o=Vt(n,r),a=Ot(n,t.vcov),c=Math.sqrt(a),s=t.jacobian.map(g=>Ot(n,g)),f=2*a*a/Ot(s,t.vcovVarpar),h=(o-e)/c,p=2*pe(Math.abs(h),f,!1,!1);return{estimate:o,stdError:c,df:f,tValue:h,pValue:p}}function Ct(t,n,e,r){let o=t.sigma*t.sigma;return{sumSq:n*o*e,meanSq:n*o,numDF:e,denDF:r,F:n,pValue:ue(n,e,r,!1,!1)}}function Eo(t){if(t.length===1)return t[0];if(t.every((e,r)=>r===0||Math.abs(e-t[r-1])<xo))return t.reduce((e,r)=>e+r,0)/t.length;if(t.some(e=>e<=2))return 2;let n=t.reduce((e,r)=>e+r/(r-2),0);return 2*n/(n-t.length)}function So(t,n,e,r){return ae(Ft(t),n,e,r)}function ae(t,n,e,r){let o=n.length;if(o===0)return;let a=e??new Array(o).fill(0);if(o===1){let m=Dt(t,n[0],a[0]);return Ct(t,m.tValue*m.tValue,1,m.df)}if(n.some(m=>m.some(Number.isNaN))||a.some(Number.isNaN))return Ct(t,NaN,NaN,NaN);let c=n.map(m=>t.vcov.map((M,T)=>m.reduce((S,y,k)=>S+y*t.vcov[k][T],0))),s=Xt(c.map(m=>n.map(M=>Vt(m,M))),{symmetric:!0}),f=s.values,h=Math.max(yo*f[0],0),p=0;for(;p<o&&f[p]>h;)p++;if(p===0)return;p<o&&a.some(m=>m!==0)&&r.add("Contrast is rank deficient and test may be affected");let g=t.beta;if(a.some(m=>m!==0)){let m=qt(n.map(M=>[...M]));g=t.beta.map((M,T)=>M-Vt(m[T],a))}let A=t.beta.length,i=Array.from({length:p},(m,M)=>Array.from({length:A},(T,S)=>n.reduce((y,k,d)=>y+s.vectors[d][M]*k[S],0)));if(p===1){let m=Dt(t,i[0],0,g);return Ct(t,m.tValue*m.tValue,1,m.df)}let u=0,l=[];return i.forEach((m,M)=>{u+=Vt(m,g)**2/f[M];let T=t.jacobian.map(S=>Ot(m,S));l.push(2*f[M]*f[M]/Ot(T,t.vcovVarpar))}),Ct(t,u/p,p,Eo(l))}function Mo(t,n){let e=wt("lmerSummary",n,["ddf"],[]).ddf??"Satterthwaite";if(e!=="Satterthwaite"&&e!=="lme4")throw new RangeError(`ddf must be "Satterthwaite" or "lme4", not ${JSON.stringify(e)}`);let o=[...bt(t).structure.xColNames];if(e==="lme4"){let{estimate:f,stdError:h,tValue:p}=t.coefTable;return{ddf:e,names:o,estimate:[...f],stdError:[...h],tValue:[...p],messages:[],warnings:[]}}let a=Ft(t),c=o.length,s=o.map((f,h)=>Dt(a,Array.from({length:c},(p,g)=>g===h?1:0),0));return{ddf:e,names:o,estimate:s.map(f=>f.estimate),stdError:s.map(f=>f.stdError),df:s.map(f=>f.df),tValue:s.map(f=>f.tValue),pValue:s.map(f=>f.pValue),messages:[],warnings:[...a.warnings]}}var ko=["rhs","joint","confint","level"],Io=["collect","checkEstimability","check_estimability","ddf"];function To(t,n){if(!Array.isArray(t))throw new TypeError("L must be an array of numbers or an array of rows");let e=t.length>0&&Array.isArray(t[0])?t.map(r=>[...r]):[[...t]];for(let r of e){if(!Array.isArray(r)||r.some(o=>typeof o!="number"))throw new TypeError("L must be an array of numbers or an array of rows");if(r.length!==n)throw new TypeError(`each contrast must have ${n} values (one per fixed effect), not ${r.length}`)}return e}function Oo(t,n,e){e=wt("lmerContest",e,ko,Io);let r=vt(e.joint,!0),o=vt(e.confint,!0),c=bt(t).structure.xColNames.length,s=To(n,c),f=e.level??.95;if(typeof f!="number"||!(f>=0&&f<=1))throw new RangeError("level must be a number between 0 and 1");let h=e.rhs??0,p=typeof h=="number"?new Array(s.length).fill(h):[...h];if(p.length!==s.length||p.some(l=>typeof l!="number"))throw new TypeError(`rhs must be a number or have one value per contrast (${s.length})`);let g=new St,A=Ft(t);if(A.warnings.forEach(l=>g.add(l)),r){let l=c===0?void 0:So(t,s,p,g),m=M=>l===void 0?[]:[l[M]];return{joint:!0,sumSq:m("sumSq"),meanSq:m("meanSq"),numDF:m("numDF"),denDF:m("denDF"),F:m("F"),pValue:m("pValue"),messages:[],warnings:g.list}}let i=c===0?[]:s.map((l,m)=>Dt(A,l,p[m])),u={joint:!1,estimate:i.map(l=>l.estimate),stdError:i.map(l=>l.stdError),df:i.map(l=>l.df),tValue:i.map(l=>l.tValue),pValue:i.map(l=>l.pValue),messages:[],warnings:g.list};if(o){let l=i.map(m=>he((1-f)/2,m.df,!1,!1));u.lower=i.map((m,M)=>m.estimate-m.stdError*l[M]),u.upper=i.map((m,M)=>m.estimate+m.stdError*l[M])}return u}var Fo=Math.log10(2);function Ro(t){let n=new DataView(new ArrayBuffer(8));n.setFloat64(0,t);let e=n.getUint16(0)>>4&2047;if(e>0)return e-1023;let r=n.getUint32(0)&1048575,o=n.getUint32(4);return(r!==0?32+(31-Math.clz32(r)):31-Math.clz32(o))-1074}function $t(t){let n=1;for(let e=0;e<t;e++)n*=10;return n}function jo(t){let n=Math.floor(t),e=t-n,r=e<.5?n:e>.5?n+1:n%2===0?n:n+1;return r===0?t<0?-0:0:r}function _o(t,n=0){if(Number.isNaN(t)||Number.isNaN(n))return NaN;if(!Number.isFinite(t)||t===0||n>323)return t;if(n<-308)return 0;if(n===0)return jo(t);let e=Math.floor(n+.5),r=Math.abs(t);if(Fo*(.5+Ro(r))+e>15)return t;let a,c,s;if(e<=308){let g=e>=0?$t(e):1/$t(-e),A=r*g;s=Math.floor(A),a=s/g,c=Math.ceil(A)/g}else{let g=$t(308),A=$t(e-308),i=r*g*A;s=Math.floor(i),a=s/g/A,c=Math.ceil(i)/g/A}let f=c-r,h=r-a,p=f<h||f===h&&s%2===1?c:a;return t<0?-p:p}function Pt(t,n=7){let e=0;for(let o of t)for(let a of o)Math.abs(a)>e&&(e=Math.abs(a));let r=e>0?Math.max(0,n-Math.log10(e)):n;return t.map(o=>o.map(a=>_o(a,r)))}var le={type:"num",value:1,text:"1"};function Ve(t,n,e={}){let r=Lt(t,!1,!0);if(r.response===void 0)throw new TypeError("the formula has no response");if(typeof n!="object"||n===null)throw new TypeError("data must be an object of columns");let{fixed:o}=zt(r.rhs),a=o??le,c=Et(r,Object.keys(n),a).variables,s=e.contrasts===void 0?{}:{contrasts:e.contrasts},f=xt({response:r.response,rhs:a},n,{...e.levels===void 0?{}:{levels:e.levels},dropUnusedLevels:!0,...s,extraVariables:c},a),{terms:h,frame:p}=f,g=f.colNames.map((l,m)=>m);if(g.length>0){let l=pt(f.x),m=new Set(l.pivot.slice(l.rank));g=g.filter(M=>!m.has(M))}let A=new Set(h.terms.flat()),i=h.variables.map((l,m)=>({name:l,v:m})).filter(({v:l})=>A.has(l)),u={};for(let{name:l,v:m}of i)p.variables[m].kind==="factor"&&(u[l]=e.contrasts?.[l]??"contr.treatment");return{x:f.x.map(l=>g.map(m=>l[m])),colNames:g.map(l=>f.colNames[l]),assign:g.map(l=>f.assign[l]),intercept:h.intercept,terms:h.terms.map((l,m)=>({label:h.termLabels[m],variables:l.map(M=>h.variables[M])})),variables:i.map(({name:l,v:m})=>({name:l,frame:p.variables[m]})),contrasts:u}}var fe=(t,n=t[0]?.length??0)=>Array.from({length:n},(e,r)=>t.map(o=>o[r]));function ce(t,n,e=n[0]?.length??0){return t.map(r=>{let o=new Array(e).fill(0);return r.forEach((a,c)=>{let s=n[c];for(let f=0;f<e;f++)o[f]+=a*s[f]}),o})}function De(t,n){let e=Array.from({length:n},()=>new Array(n).fill(0));for(let r=0;r<n;r++)for(let o=r;o<n;o++){let a=0;for(let c of t)a+=c[r]*c[o];e[r][o]=a,e[o][r]=a}return e}var Gt=(t,n)=>t.map(e=>n.map(r=>e[r])),Ce=1e-6;function $e(t){let n=t.length,e=Array.from({length:n},(o,a)=>Array.from({length:n},(c,s)=>a===s?1:0)),r=Array.from({length:n},()=>new Array(n).fill(0));for(let o=0;o<n;o++){for(let c=0;c<n;c++){let s=t[o][c];for(let f=0;f<o;f++)s-=e[o][f]*r[f][c];r[o][c]=s}let a=r[o][o];for(let c=o+1;c<n;c++){if(Math.abs(a)<Ce){e[c][o]=0;continue}let s=t[c][o];for(let f=0;f<o;f++)s-=e[c][f]*r[f][o];e[c][o]=s/a}}for(let o of[e,r])for(let a of o)a.forEach((c,s)=>{Math.abs(c)<Ce&&(a[s]=0)});return{L:e,U:r}}function Pe(t){let n=new Set(t.variables.filter(r=>r.frame.kind==="numeric").map(r=>r.name)),e=r=>r.variables.filter(o=>n.has(o)).sort().join("\0");return t.terms.map(r=>t.terms.map((o,a)=>({b:o,k:a})).filter(({b:o})=>r.variables.every(a=>o.variables.includes(a))&&o.variables.length>r.variables.length&&e(r)===e(o)).map(({k:o})=>o))}var At=(t,n)=>t.flatMap((e,r)=>e===n+1?[r]:[]);function Lo(t,n,e,r,o){return{term:t,rows:e.map(a=>r[a]),columns:[...o],values:e.map(a=>[...n[a]])}}function zo(t){let n=t.colNames.length;if(n===0||n===1&&t.intercept)return[];let e=n===1?[[1]]:fe($e(De(t.x,n)).L,n);return t.terms.map((r,o)=>Lo(r.label,e,At(t.assign,o),t.colNames,t.colNames))}function qo(t){let n=t.colNames.length,e=Pe(t);return t.terms.map((r,o)=>{let a=At(t.assign,o);if(e[o].length===0){let i=a.map(u=>Array.from({length:n},(l,m)=>u===m?1:0));return{term:r.label,rows:a.map(u=>t.colNames[u]),columns:[...t.colNames],values:i}}let c=[o,...e[o]].flatMap(i=>At(t.assign,i)),s=new Set(c),f=[...t.colNames.map((i,u)=>u).filter(i=>!s.has(i)),...c],h=fe($e(De(Gt(t.x,f),n)).L,n),p=new Set(a),g=f.flatMap((i,u)=>p.has(i)?[u]:[]),A=g.map(i=>{let u=new Array(n).fill(0);return f.forEach((l,m)=>u[l]=h[i][m]),u});return{term:r.label,rows:g.map(i=>t.colNames[f[i]]),columns:[...t.colNames],values:A}})}function Co(t){let n=t.x.length,e=new Map(t.variables.map(f=>[f.name,f.frame])),r=[],o=[];t.intercept&&(r.push(new Array(n).fill(1)),o.push("(Intercept)"));let a=f=>{let h=e.get(f);return h.kind==="numeric"?[{name:f,values:h.values}]:h.levels.map((p,g)=>({name:f+p,values:h.codes.map(A=>A===g?1:0)}))};for(let f of t.terms){let h=a(f.variables[0]);for(let p of f.variables.slice(1)){let g=[];for(let A of a(p))for(let i of h)g.push({name:`${i.name}:${A.name}`,values:i.values.map((u,l)=>u*A.values[l])});h=g}for(let p of h)r.push(p.values),o.push(p.name)}let c=o.filter((f,h)=>r[h].every(p=>p===0)),s=c.length>0?[`Missing cells for: ${c.join(", ")}.
4
+ Interpret type III hypotheses with care.`]:[];return{x:Array.from({length:n},(f,h)=>r.map(p=>p[h])),colNames:o,messages:s}}function Vo(t){let n={},e={};for(let{name:h,frame:p}of t.variables)p.kind==="numeric"?n[h]=p.values:(n[h]=p.codes.map(g=>p.levels[g]),e[h]=p.levels);let r=h=>h.reduce((p,g)=>p===void 0?g:{type:"binary",op:"+",left:p,right:g},void 0),o=h=>({type:"var",name:h}),a={response:void 0,rhs:r(t.variables.map(h=>o(h.name)))??le},c=t.terms.map(h=>h.variables.map(o).reduce((p,g)=>({type:"binary",op:":",left:p,right:g}))),s=xt(a,n,{levels:e,intercept:t.intercept},r(c)??le),f=s.colNames.map((h,p)=>p);if(f.length>0){let h=pt(s.x);h.rank<f.length&&(f=h.pivot.slice(0,h.rank))}return{x:Gt(s.x,f),colNames:f.map(h=>s.colNames[h]),assign:f.map(h=>s.assign[h])}}var Do=1e-8;function $o(t){let n=Object.values(t.contrasts).every(i=>i==="contr.treatment"),e=n?{x:t.x,colNames:t.colNames,assign:t.assign}:Vo(t),r=e.colNames.length,o=Co(t),a=qt(e.x),c=Pt(fe(ce(a,o.x,o.colNames.length),o.colNames.length)),s=Pe(t),f=t.terms.map((i,u)=>({k:u,order:i.variables.length})).sort((i,u)=>u.order-i.order||i.k-u.k);for(let{k:i}of f){if(s[i].length===0)continue;let u=At(e.assign,i);if(u.length===0)continue;let l=s[i].flatMap(T=>At(e.assign,T)),m=Gt(c,u);l.length>0&&(m=ve(pt(Gt(c,l)),m));let M=Pt(m);c.forEach((T,S)=>u.forEach((y,k)=>T[y]=M[S][k]))}let h=new Map(o.colNames.map((i,u)=>[i,u])),p=e.colNames.map(i=>{let u=h.get(i);if(u===void 0)throw new Error(`lmerHypotheses: the column '${i}' of the model matrix is not a column of the full-dummy matrix`);return c[u]}),g=n?void 0:Pt(ce(a,t.x,t.colNames.length));return{hypotheses:t.terms.map((i,u)=>{let l=At(e.assign,u).filter(M=>p.reduce((T,S)=>T+Math.abs(S[M]),0)>Do),m=l.map(M=>p.map(T=>T[M]));return{term:i.label,rows:l.map(M=>e.colNames[M]),columns:[...t.colNames],values:g===void 0?m:ce(m,g,t.colNames.length)}}),messages:o.messages}}function Ge(t,n="III"){return t.colNames.length<=1||t.terms.length<=1||n==="I"?{hypotheses:zo(t),messages:[]}:n==="II"?{hypotheses:qo(t),messages:[]}:$o(t)}var Po={I:"I",II:"II",III:"III",1:"I",2:"II",3:"III"};function Go(t,n){let{structure:e,data:r,options:o}=bt(t),a={};o.contrasts!==void 0&&(a.contrasts=o.contrasts),o.levels!==void 0&&(a.levels=o.levels);let c=Ve(t.formula,r,a);if(c.colNames.length!==e.xColNames.length||c.colNames.some((s,f)=>s!==e.xColNames[f]))throw new Error("lmerAnova: the columns of the hypotheses differ from the fit's fixed effects");return c.terms.length===0?{hypotheses:{hypotheses:[],messages:[]},termLabels:[]}:{hypotheses:Ge(c,n),termLabels:c.terms.map(s=>s.label)}}function Uo(t,{hypotheses:n},e,r){let o=[],a=[];for(let c of n){let s=c.values.length===0?void 0:ae(t,c.values,void 0,r);s!==void 0&&(o.push(c.term),a.push(s))}for(let c of e)o.includes(c)||(o.push(c),a.push({sumSq:NaN,meanSq:NaN,numDF:NaN,denDF:NaN,F:NaN,pValue:NaN}));return{terms:o,rows:a}}function Ho(t,n){let e=Po[String(n.type??"III")];if(e===void 0)throw new RangeError(`type must be "I", "II" or "III", not ${JSON.stringify(n.type)}`);let{hypotheses:r,termLabels:o}=Go(t,e),a=Ft(t),c=new St;a.warnings.forEach(p=>c.add(p));let{terms:s,rows:f}=Uo(a,r,o,c),h=p=>f.map(g=>g[p]);return{type:e,terms:s,sumSq:h("sumSq"),meanSq:h("meanSq"),numDF:h("numDF"),denDF:h("denDF"),F:h("F"),pValue:h("pValue"),messages:[...r.messages],warnings:c.list}}function Wo(t,n){if(t.length<2)throw new TypeError("lmerAnova needs one fit, or an array of at least two fits to compare");let e=t.map(bt);if(t.some(l=>l.nObs!==t[0].nObs))throw new Error("models were not all fitted to the same size of dataset");if(e.some(l=>l.data!==e[0].data))throw new Error("all models must be fit to the same data object");let r=new St,o=[],a=t.map((l,m)=>`MODEL${m+1}`);if(n.names!==void 0){if(n.names.length!==t.length||n.names.some(l=>typeof l!="string"))throw new TypeError(`names must be ${t.length} strings, one per fit`);new Set(n.names).size===n.names.length?a=[...n.names]:r.add("failed to find model names, assigning generic names")}let c=vt(n.refit,!0),s;c?(t.some(l=>l.REML)&&o.push("refitting model(s) with ML (instead of REML)"),s=t.map((l,m)=>{if(!l.REML)return l.logLik;let{data:M,options:T,criterion:S}=e[m];return oe(l.formula,M,{...T,REML:!1},S).logLik})):(t.some(l=>l.REML)&&t.some(l=>!l.REML)&&r.add("some models fit with REML = TRUE, some not"),s=t.map(l=>l.logLik));let f=t.map((l,m)=>m).sort((l,m)=>t[l].df-t[m].df||l-m),h=t[0].nObs,p=f.map(l=>t[l].df),g=f.map(l=>s[l]),A=g.map((l,m)=>m===0?NaN:2*Math.max(0,l-g[m-1])),i=p.map((l,m)=>m===0?NaN:l-p[m-1]),u=A.map((l,m)=>m===0||i[m]===0?NaN:me(l,i[m]/2,2,!1,!1));return{names:f.map(l=>a[l]),npar:p,AIC:g.map((l,m)=>-2*l+2*p[m]),BIC:g.map((l,m)=>-2*l+Math.log(h)*p[m]),logLik:g,deviance:g.map(l=>-2*l),Chisq:A,Df:i,pValue:u,messages:o,warnings:r.list}}function Bo(t,n){if(Array.isArray(t)){let e=wt("lmerAnova",n,["refit","names"],["modelNames","model.names"]);return Wo(t,{...e,refit:vt(e.refit,!0)})}return Ho(t,wt("lmerAnova",n,["type"],["ddf"]))}export{bo as lmer,Bo as lmerAnova,Oo as lmerContest,Mo as lmerSummary};
@@ -53,6 +53,8 @@ export function irls(inp, warnings) {
53
53
  deviance: totalDeviance(fam, y, mu, w),
54
54
  iter: 0,
55
55
  converged: true,
56
+ // (true, not a mistake: R's glm.fit marks the empty model both converged and at the boundary; the warning
57
+ // "algorithm stopped at boundary value" is still not given, see finalWarnings)
56
58
  boundary: true,
57
59
  rank: 0,
58
60
  qr: undefined,
@@ -340,10 +342,10 @@ export function glm(x, y, options = {}) {
340
342
  swy += w[i] * yy[i];
341
343
  }
342
344
  const m = swy / sw;
343
- nullDeviance = totalDeviance(fam, yy, yy.map(() => m), w);
345
+ nullDeviance = withWarnings(warnings, () => totalDeviance(fam, yy, yy.map(() => m), w));
344
346
  }
345
347
  else
346
- nullDeviance = totalDeviance(fam, yy, withWarnings(warnings, () => offset.map((o) => fam.linkinv(o))), w);
348
+ nullDeviance = withWarnings(warnings, () => totalDeviance(fam, yy, offset.map((o) => fam.linkinv(o)), w));
347
349
  const nOk = n - w.filter((v) => v === 0).length;
348
350
  const aic = familyAic(fam, yy, fit.mu, w, size, fit.deviance, warnings) + 2 * fit.rank;
349
351
  return {
@@ -49,6 +49,15 @@ function sqrtWarn(x) {
49
49
  currentWarnings?.add("NaNs produced");
50
50
  return Math.sqrt(x);
51
51
  }
52
+ /**
53
+ * Logarithm that warns as R's log does when it produces NaN (a negative argument, -Inf included; NaN does not warn).
54
+ * Used by the deviance contributions that R computes in R code (poisson, quasipoisson, Gamma, quasi "mu" and "mu^2")
55
+ */
56
+ function logWarn(x) {
57
+ if (x < 0)
58
+ currentWarnings?.add("NaNs produced");
59
+ return Math.log(x);
60
+ }
52
61
  /** A number as C's printf("%g") writes it: 6 significant digits, fixed or scientific, trailing zeros dropped */
53
62
  function formatG(v) {
54
63
  if (!Number.isFinite(v))
@@ -175,13 +184,14 @@ function quasiVariance(v) {
175
184
  return {
176
185
  variance: (m) => m,
177
186
  muOk: (m) => m > 0,
178
- dev: (y, mu, w) => 2 * w * ((y === 0 ? 0 : y * Math.log(y / mu)) - (y - mu)),
187
+ dev: (y, mu, w) => 2 * w * ((y === 0 ? 0 : y * logWarn(y / mu)) - (y - mu)),
179
188
  };
180
189
  case "mu^2":
181
190
  return {
182
191
  variance: (m) => m * m,
183
192
  muOk: (m) => m > 0,
184
- dev: (y, mu, w) => Math.max(-2 * w * (Math.log(y > 0 ? y / mu : 1 / mu) - (y - mu) / mu), 0),
193
+ // log(y/mu) for every y != 0 (NaN for a negative y, as in R), log(1/mu) for y = 0
194
+ dev: (y, mu, w) => Math.max(-2 * w * (logWarn(y !== 0 ? y / mu : 1 / mu) - (y - mu) / mu), 0),
185
195
  };
186
196
  case "mu^3":
187
197
  return { variance: (m) => m * m * m, muOk: (m) => m > 0, dev: inverseGaussianDev };
@@ -189,6 +199,7 @@ function quasiVariance(v) {
189
199
  throw new RangeError(`'variance' "${String(v)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`);
190
200
  }
191
201
  }
202
+ /** (R computes this deviance in compiled code, so a mean outside (0, 1) gives NaN without "NaNs produced") */
192
203
  function binomialDev(y, mu, w) {
193
204
  const a = y === 0 ? 0 : y * Math.log(y / mu);
194
205
  const b = y === 1 ? 0 : (1 - y) * Math.log((1 - y) / (1 - mu));
@@ -243,14 +254,15 @@ export function makeFamily(family, link) {
243
254
  fixedDispersion: name === "poisson",
244
255
  variance: (m) => m,
245
256
  muOk: (m) => Number.isFinite(m) && m > 0,
246
- devContribution: (y, mu, w) => (y > 0 ? 2 * w * (y * Math.log(y / mu) - (y - mu)) : 2 * w * mu),
257
+ // (a row with y = 0 takes no logarithm, so a negative mean there does not warn, as in R)
258
+ devContribution: (y, mu, w) => (y > 0 ? 2 * w * (y * logWarn(y / mu) - (y - mu)) : 2 * w * mu),
247
259
  };
248
260
  case "Gamma":
249
261
  return {
250
262
  ...base,
251
263
  variance: (m) => m * m,
252
264
  muOk: (m) => Number.isFinite(m) && m > 0,
253
- devContribution: (y, mu, w) => -2 * w * ((y === 0 ? 0 : Math.log(y / mu)) - (y - mu) / mu),
265
+ devContribution: (y, mu, w) => -2 * w * ((y === 0 ? 0 : logWarn(y / mu)) - (y - mu) / mu),
254
266
  };
255
267
  case "inverse.gaussian":
256
268
  return { ...base, variance: (m) => m * m * m, muOk: () => true, devContribution: inverseGaussianDev };
@@ -9,12 +9,18 @@ import { type MixedModelStructure } from "./mixedStructure.js";
9
9
  */
10
10
  export type LmerContrastName = ContrastName | "contr.poly";
11
11
  /**
12
- * Options of {@link lmer}.
12
+ * Options of {@link lmer}. Only the keys below are accepted: R's `weights`, `offset`, `subset`, `naAction`,
13
+ * `na.action`, `control`, `start`, `verbose` and `devFunOnly` give the Error `lmer: argument '<key>' is not
14
+ * supported`, any other key the TypeError `unused argument (<key>)` (a key whose value is `undefined` included).
13
15
  *
14
16
  * @category Mixed models
15
17
  */
16
18
  export interface LmerOptions {
17
- /** Whether to minimise the REML criterion (default `true`) or the ML deviance (`false`). */
19
+ /**
20
+ * Whether to minimise the REML criterion (default `true`) or the ML deviance (`false`). As in R's conditions, a
21
+ * number is `false` for 0 and `true` otherwise; `null` and other values (the strings `"TRUE"`, `"FALSE"` included)
22
+ * are TypeErrors. The result's `REML` is the boolean.
23
+ */
18
24
  REML?: boolean;
19
25
  /**
20
26
  * A contrast per factor of the fixed-effects part: `"contr.treatment"` (default), `"contr.sum"`, `"contr.helmert"` or
@@ -126,6 +132,18 @@ export type MixedCriterion = (s: MixedModelStructure, theta: readonly number[],
126
132
  f: number;
127
133
  g: number[];
128
134
  };
135
+ /**
136
+ * The option checks of lmer and of lmerTest's functions (internal; oracle/report-0.14.0-behaviour.md, section 3):
137
+ * `options` must be an object when given (not null, not an array); its own keys, in order, may be neither among those
138
+ * of R that are not supported (the first gives an Error) nor unknown (a TypeError listing all of them). A key whose
139
+ * value is `undefined` counts as given. Returns the options as an object (`{}` when not given).
140
+ */
141
+ export declare function checkOptions<T extends object>(fn: string, options: T | undefined, accepted: readonly string[], unsupported: readonly string[]): T;
142
+ /**
143
+ * A logical option as R's conditions take it (internal): `undefined` gives the default, a boolean itself, a number
144
+ * other than NaN `false` for 0 and `true` otherwise; `null` and anything else are TypeErrors.
145
+ */
146
+ export declare function logicalOption(value: unknown, defaultValue: boolean): boolean;
129
147
  /**
130
148
  * Fit a linear mixed model by REML (default) or maximum likelihood. Equivalent to `lme4::lmer()` in R. With the
131
149
  * quantities of its methods: `fixef()`, `coef(summary())` (without p-values), `vcov()`, `sigma()`, `VarCorr()`,
@@ -143,7 +161,7 @@ export type MixedCriterion = (s: MixedModelStructure, theta: readonly number[],
143
161
  *
144
162
  * @param formula A formula such as `"Reaction ~ Days + (Days | Subject)"`.
145
163
  * @param data The data frame: an object of columns.
146
- * @param options `REML`, and `contrasts` and `levels` as in {@link lm}.
164
+ * @param options `REML`, and `contrasts` and `levels` as in {@link lm} (see {@link LmerOptions} for the keys accepted).
147
165
  *
148
166
  * @example
149
167
  * ```ts
@@ -170,8 +188,13 @@ export declare function lmer(formula: string, data: DataFrame, options?: LmerOpt
170
188
  * (to refit, for example by maximum likelihood) and the criterion (so that a refit uses the same one).
171
189
  */
172
190
  export interface LmerInternals {
173
- /** The structure of the model (X, Z, the terms, the layout of θ, the model frame's rows) */
191
+ /**
192
+ * The structure of the model (X, Z, the terms, the layout of θ, the model frame's rows), with the response replaced
193
+ * by y − X β₀ (see `beta0`): the criterion and every derivative are those of the model, the fixed effects β̂ − β₀
194
+ */
174
195
  structure: MixedModelStructure;
196
+ /** β₀, the least-squares coefficients of y on X, to be added to the fixed effects computed from `structure` */
197
+ beta0: number[];
175
198
  /** The data frame given to lmer (the same object, not a copy) */
176
199
  data: DataFrame;
177
200
  /** The options given to lmer (a copy) */
@@ -192,4 +215,4 @@ export declare function lmerInternals(fit: LmerResult): LmerInternals;
192
215
  * {@link lmer} with the criterion and its gradient given (internal: not exported from the package). The regression
193
216
  * test of the sparse computation fits with the dense reference implementation through it.
194
217
  */
195
- export declare function lmerWithCriterion(formula: string, data: DataFrame, options: LmerOptions, criterion: MixedCriterion): LmerResult;
218
+ export declare function lmerWithCriterion(formula: string, data: DataFrame, options: LmerOptions | undefined, criterion: MixedCriterion): LmerResult;