@numero-uno-jpn/rstats 0.15.2 → 0.15.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +114 -0
- package/README.ja.md +8 -8
- package/README.md +8 -8
- package/dist/chunks/{chunk-PEYGUK32.js → chunk-3THFTB36.js} +2 -2
- package/dist/chunks/chunk-3TSWYE5L.js +2 -0
- package/dist/chunks/{chunk-IBPWRQIF.js → chunk-3WPU6LGK.js} +2 -2
- package/dist/chunks/{chunk-VWJ74UHC.js → chunk-5ITR67MB.js} +2 -2
- package/dist/chunks/chunk-5NRFU6F6.js +2 -0
- package/dist/chunks/{chunk-XNPU54NH.js → chunk-E2LCSVQV.js} +1 -1
- package/dist/chunks/chunk-FCNVTIPJ.js +2 -0
- package/dist/chunks/chunk-FKAKALOZ.js +2 -0
- package/dist/chunks/{chunk-HBMHR5SS.js → chunk-HD73UKPT.js} +2 -2
- package/dist/chunks/{chunk-4Z2XDWKV.js → chunk-IUULP3LE.js} +1 -1
- package/dist/chunks/{chunk-Q4PGYESP.js → chunk-JQG3YQOC.js} +2 -2
- package/dist/chunks/{chunk-LG74QY5U.js → chunk-KT66SB7R.js} +1 -1
- package/dist/chunks/{chunk-SREPSVTT.js → chunk-MAUAOQG3.js} +2 -2
- package/dist/chunks/{chunk-YVREYVI2.js → chunk-MDYE7QP2.js} +2 -2
- package/dist/chunks/{chunk-XPLDEK6D.js → chunk-NM3Z2UBQ.js} +2 -2
- package/dist/chunks/{chunk-AJBLCNZ7.js → chunk-PMJI5QO6.js} +2 -2
- package/dist/chunks/chunk-UF7KOTHF.js +3 -0
- package/dist/chunks/chunk-UOE57JGT.js +2 -0
- package/dist/chunks/{chunk-3WQCKGKP.js → chunk-USPEBXSR.js} +2 -2
- package/dist/chunks/{chunk-PWVQVJTP.js → chunk-VLUL2HFW.js} +1 -1
- package/dist/chunks/{chunk-YGT4P7EU.js → chunk-ZIX7NYQF.js} +1 -1
- package/dist/descriptive.js +2 -2
- package/dist/distributions/beta.js +11 -1
- package/dist/distributions/cauchy.js +76 -5
- package/dist/distributions/exponential.js +6 -1
- package/dist/distributions/f.js +41 -5
- package/dist/distributions/gamma.js +34 -3
- package/dist/distributions/geometric.js +4 -1
- package/dist/distributions/invgamma.js +4 -0
- package/dist/distributions/kumaraswamy.js +22 -4
- package/dist/distributions/laplace.js +5 -2
- package/dist/distributions/logistic.js +8 -2
- package/dist/distributions/lognormal.js +12 -1
- package/dist/distributions/nbeta.js +66 -24
- package/dist/distributions/nchisq.js +58 -11
- package/dist/distributions/negbinomial.js +6 -1
- package/dist/distributions/normal.js +26 -1
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +145 -19
- package/dist/distributions/pareto.js +5 -0
- package/dist/distributions/t.js +55 -3
- package/dist/distributions/triangular.js +35 -8
- package/dist/distributions/tukey.js +16 -0
- package/dist/distributions/uniform.js +2 -0
- package/dist/distributions/weibull.js +23 -3
- package/dist/distributions.js +2 -2
- package/dist/internal/ddexp.d.ts +8 -0
- package/dist/internal/ddexp.js +27 -0
- package/dist/internal/invert.d.ts +5 -0
- package/dist/internal/invert.js +10 -5
- package/dist/internal/noncentral.d.ts +7 -0
- package/dist/internal/noncentral.js +9 -0
- package/dist/internal/poissonMixture.js +29 -6
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +14 -14
- package/dist/special/beta.js +7 -0
- package/dist/special/gamma.js +4 -0
- package/dist/special/igam.js +33 -6
- package/dist/special/incbet.js +70 -6
- package/dist/special/incbetLargeA.js +7 -2
- package/dist/special/incbetPrefactor.d.ts +5 -0
- package/dist/special/incbetPrefactor.js +14 -3
- package/dist/special/loader.js +70 -21
- package/dist/special.js +2 -2
- package/dist/tests.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-2CUKVVRO.js +0 -2
- package/dist/chunks/chunk-D4F7GQAY.js +0 -2
- package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
- package/dist/chunks/chunk-JLAH3X77.js +0 -2
- package/dist/chunks/chunk-RCAL4VHM.js +0 -3
- package/dist/chunks/chunk-UFT662QZ.js +0 -2
package/dist/mixed.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! rStats.js 0.15.
|
|
2
|
-
import{a as xt,c as St}from"./chunks/chunk-RCAL4VHM.js";import{c as me,t as ue}from"./chunks/chunk-EBQMGCQZ.js";import{b as qt}from"./chunks/chunk-YVREYVI2.js";import{A as be,C as ye,E as Xt,h as pt,k as we,l as ve}from"./chunks/chunk-XNPU54NH.js";import{a as Lt,c as zt,d as Nt,f as de,g as Et}from"./chunks/chunk-PEYGUK32.js";import{o as pe,p as he}from"./chunks/chunk-UFT662QZ.js";import{b as ge}from"./chunks/chunk-HBMHR5SS.js";import{b as Bt}from"./chunks/chunk-PWVQVJTP.js";import"./chunks/chunk-D4F7GQAY.js";import"./chunks/chunk-2CUKVVRO.js";import"./chunks/chunk-LG74QY5U.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.
|
|
1
|
+
/*! rStats.js 0.15.3 | 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-UF7KOTHF.js";import{l as ue}from"./chunks/chunk-5NRFU6F6.js";import{b as qt}from"./chunks/chunk-MDYE7QP2.js";import{A as be,C as ye,E as Xt,h as pt,k as we,l as ve}from"./chunks/chunk-E2LCSVQV.js";import{a as Lt,c as zt,d as Nt,f as de,g as Et}from"./chunks/chunk-3THFTB36.js";import{i as me,x as pe,y as he}from"./chunks/chunk-FCNVTIPJ.js";import{b as ge}from"./chunks/chunk-HD73UKPT.js";import{b as Bt}from"./chunks/chunk-VLUL2HFW.js";import"./chunks/chunk-FKAKALOZ.js";import"./chunks/chunk-3TSWYE5L.js";import"./chunks/chunk-KT66SB7R.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
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
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};
|
package/dist/numerics.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 0.15.
|
|
2
|
-
import{a as $,b as T,c as D,d as F,e as O}from"./chunks/chunk-AJBLCNZ7.js";import"./chunks/chunk-YGT4P7EU.js";import{a as k,f as j}from"./chunks/chunk-SREPSVTT.js";import"./chunks/chunk-4Z2XDWKV.js";import{f as S}from"./chunks/chunk-IBPWRQIF.js";import"./chunks/chunk-PWVQVJTP.js";import"./chunks/chunk-LG74QY5U.js";function M(o,l,t="mean"){if(o.length!==l.length)throw new RangeError("'x' and 'y' lengths differ");let n=[];for(let r=0;r<o.length;r++)!Number.isNaN(o[r])&&!Number.isNaN(l[r])&&n.push(r);n.sort((r,s)=>o[r]-o[s]);let e=typeof t=="function"?t:t==="min"?r=>Math.min(...r):t==="max"?r=>Math.max(...r):S,a=[],f=[];for(let r=0;r<n.length;){let s=o[n[r]],c=r+1;for(;c<n.length&&o[n[c]]===s;)c++;a.push(s),f.push(c-r===1?l[n[r]]:e(n.slice(r,c).map(i=>l[i]))),r=c}return{x:a,y:f}}function X(o,l,t,n){let e=(n-o[t])/(o[t+1]-o[t]);return l[t]+(l[t+1]-l[t])*e}function J(o,l,t){let n=(e,a)=>e===0?0:e*a;return n(1-t,o[l])+n(t,o[l+1])}function P(o,l,t={}){let{method:n="linear",n:e=50,f:a=0,rule:f=1,ties:r="mean"}=t,s=M(o,l,r),c=s.x.length;if(c<=1&&n==="linear")throw new RangeError("need at least two non-NA values to interpolate");if(c===0)throw new RangeError("zero non-NA points");if(n==="constant"&&!(a>=0&&a<=1))throw new RangeError("approx(): invalid f value");let[i,p]=typeof f=="number"?[f,f]:f,d=t.yleft??(i===1?NaN:s.y[0]),h=t.yright??(p===1?NaN:s.y[c-1]),m;if(t.xout!==void 0)m=Array.from(t.xout);else{if(e<=0)throw new RangeError("'approx' requires n >= 1");m=k(s.x[0],s.x[c-1],e)}let g=s.x,u=s.y,w=j(m,g),y=m.map((b,A)=>{let R=w[A];if(Number.isNaN(R))return NaN;if(R===0)return d;let E=R-1;return b===g[E]?u[E]:R===c?h:n==="linear"?X(g,u,E,b):J(u,E,a)});return{x:m,y}}function v(o,l){let t=o.length,n=new Array(t).fill(0),e=new Array(t).fill(0),a=new Array(t).fill(0);if(t<2)return{b:n,c:e,d:a};if(t<3)return n[0]=(l[1]-l[0])/(o[1]-o[0]),n[1]=n[0],{b:n,c:e,d:a};let f=t-1;a[0]=o[1]-o[0],e[1]=(l[1]-l[0])/a[0];for(let r=1;r<f;r++)a[r]=o[r+1]-o[r],n[r]=2*(a[r-1]+a[r]),e[r+1]=(l[r+1]-l[r])/a[r],e[r]=e[r+1]-e[r];n[0]=-a[0],n[f]=-a[t-2],e[0]=0,e[f]=0,t>3&&(e[0]=e[2]/(o[3]-o[1])-e[1]/(o[2]-o[0]),e[f]=e[t-2]/(o[f]-o[t-3])-e[t-3]/(o[t-2]-o[t-4]),e[0]=e[0]*a[0]*a[0]/(o[3]-o[0]),e[f]=-e[f]*a[t-2]*a[t-2]/(o[f]-o[t-4]));for(let r=1;r<t;r++){let s=a[r-1]/n[r-1];n[r]=n[r]-s*a[r-1],e[r]=e[r]-s*e[r-1]}e[f]=e[f]/n[f];for(let r=t-2;r>=0;r--)e[r]=(e[r]-a[r]*e[r+1])/n[r];n[f]=(l[f]-l[t-2])/a[t-2]+a[t-2]*(e[t-2]+2*e[f]);for(let r=0;r<f;r++)n[r]=(l[r+1]-l[r])/a[r]-a[r]*(e[r+1]+2*e[r]),a[r]=(e[r+1]-e[r])/a[r],e[r]=3*e[r];return e[f]=3*e[f],a[f]=a[t-2],{b:n,c:e,d:a}}function z(o,l,t){let n=o.length,e=new Array(n).fill(0),a=new Array(n).fill(0),f=new Array(n).fill(0);for(let r=0;r<n-1;r++){let s=o[r+1]-o[r];e[r]=(l[r+1]-l[r])/s-s*(t[r+1]+2*t[r]),a[r]=3*t[r],f[r]=(t[r+1]-t[r])/s}return{b:e,c:a,d:f}}function Y(o,l){let t=o.length;if(t<3)return v(o,l);let n=Array.from({length:t-1},(c,i)=>o[i+1]-o[i]),e=n.map((c,i)=>(l[i+1]-l[i])/c),a=new Array(t).fill(0),f=new Array(t).fill(0);for(let c=1;c<t-1;c++)a[c]=2*(n[c-1]+n[c]),f[c]=e[c]-e[c-1];for(let c=2;c<t-1;c++){let i=n[c-1]/a[c-1];a[c]=a[c]-i*n[c-1],f[c]=f[c]-i*f[c-1]}let r=new Array(t).fill(0);r[t-2]=f[t-2]/a[t-2];for(let c=t-3;c>=1;c--)r[c]=(f[c]-n[c]*r[c+1])/a[c];let s=z(o,l,r);return s.b[t-1]=e[t-2]+n[t-2]*r[t-2],s}function B(o,l){let t=o.length;if(l[0]!==l[t-1])throw new RangeError("periodic spline needs y[1] == y[n]");if(t<3)return{b:new Array(t).fill(0),c:new Array(t).fill(0),d:new Array(t).fill(0)};let n=t-1,e=Array.from({length:n},(u,w)=>o[w+1]-o[w]),a=e.map((u,w)=>(l[w+1]-l[w])/u),f=u=>u===0?n-1:u-1,r=Array.from({length:n},(u,w)=>2*(e[f(w)]+e[w])),s=Array.from({length:n},(u,w)=>a[w]-a[f(w)]),c=e.slice(),i=new Array(n).fill(0);i[0]=e[n-1],i[n-2]=i[n-2]+e[n-2];let p=i[0],d=r[n-1],h=s[n-1];for(let u=0;u<n-1;u++){if(u+1<n-1){let y=e[u]/r[u];r[u+1]=r[u+1]-y*c[u],i[u+1]=i[u+1]-y*i[u],s[u+1]=s[u+1]-y*s[u]}let w=p/r[u];d-=w*i[u],h-=w*s[u],u+1<n-1&&(p=(u+1===n-2?e[n-2]:0)-w*c[u])}let m=new Array(t).fill(0);m[n-1]=h/d;for(let u=n-2;u>=0;u--){let w=s[u]-i[u]*m[n-1]-(u<n-2?c[u]*m[u+1]:0);m[u]=w/r[u]}m[t-1]=m[0];let g=z(o,l,m);return g.b[t-1]=g.b[0],g.c[t-1]=g.c[0],g.d[t-1]=g.d[0],g}function G(o,l){let t=o.length,{b:n,c:e,d:a}=v(o,l);if(t<2)return{b:n,c:e,d:a};let f=Array.from({length:t-1},(s,c)=>(l[c+1]-l[c])/(o[c+1]-o[c]));for(let s=0;s<t;s++){let c=f[s===0?0:s-1],i=f[s===t-1?t-2:s],p=Math.min(Math.abs(c),Math.abs(i)),d=c*i>0?i>0:n[s]>=0;n[s]=d?Math.min(Math.max(0,n[s]),3*p):Math.max(Math.min(0,n[s]),-3*p)}for(let s=0;s<t-1;s++){let c=o[s+1]-o[s],i=(l[s+1]-l[s])/c;e[s]=(3*i-2*n[s]-n[s+1])/c,a[s]=(n[s]+n[s+1]-2*i)/(c*c)}let r=o[t-1]-o[t-2];return e[t-1]=(n[t-2]+2*n[t-1]-3*(l[t-1]-l[t-2])/r)/r,a[t-1]=a[t-2],{b:n,c:e,d:a}}function q(o,l,t,n){let e=M(o,l,n),a=e.x.length;if(a===0)throw new RangeError("zero non-NA points");t==="periodic"&&e.y[a-1]!==e.y[0]&&(e.y[a-1]=e.y[0]),t==="hyman"&&K(e.y);let f=t==="fmm"?v(e.x,e.y):t==="natural"?Y(e.x,e.y):t==="periodic"?B(e.x,e.y):t==="hyman"?G(e.x,e.y):(()=>{throw new RangeError(`invalid spline method '${String(t)}'`)})();return{method:t,...e,...f}}function K(o){let l=!0,t=!0;for(let n=1;n<o.length;n++){let e=o[n]-o[n-1];e>=0||(l=!1),e<=0||(t=!1)}if(!l&&!t)throw new RangeError("'y' must be increasing or decreasing")}function C(o){let l=Math.trunc(o);if(!(l>=0&&l<=3))throw new RangeError("'deriv' must be between 0 and 3");return l}function H(o,l,t){let n=C(t),{x:e,y:a,b:f,c:r,d:s,method:c}=o,i=e.length,p=c==="natural",d=e[i-1]-e[0],h=0;return Array.from(l,m=>{let g=m;if(c==="periodic"&&i>1){let y=(g-e[0])%d;y<0&&(y+=d),g=e[0]+y}if(p&&n>0&&g<=e[0])return n===1?f[0]:0;if(!(e[h]<=g&&(h===i-1||g<=e[h+1]))){let y=0,b=i;for(;b-y>1;){let A=y+b>>1;e[A]<=g?y=A:b=A}h=y}let u=g-e[h],w=p&&g<e[0]?0:s[h];switch(n){case 0:return a[h]+u*(f[h]+u*(r[h]+u*w));case 1:return f[h]+u*(2*r[h]+u*(3*w));case 2:return 2*r[h]+u*(6*w);default:return 6*w}})}function Q(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t,a=q(o,l,n,e),f;if(t.xout!==void 0)f=Array.from(t.xout);else{let r=a.x.length,s=t.n??3*r;if(s<=0)throw new RangeError("'spline' requires n >= 1");f=k(t.xmin??a.x[0],t.xmax??a.x[r-1],s)}return{x:f,y:H(a,f,0)}}function U(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t;if(n==="monoH.FC"){let f=M(o,l,e);if(f.x.length===0)throw new RangeError("zero non-NA points");let r=V(f.x,f.y);return(s,c=0)=>I(f.x,f.y,r,s,c)}let a=q(o,l,n,e);return(f,r=0)=>H(a,f,r)}function V(o,l){let t=o.length;if(t<2)throw new RangeError("length(m) must be at least two");let n=Array.from({length:t-1},(a,f)=>(l[f+1]-l[f])/(o[f+1]-o[f])),e=Array.from({length:t},(a,f)=>f===0?n[0]:f===t-1?n[t-2]:(n[f-1]+n[f])/2);for(let a=0;a<t-1;a++){let f=n[a];if(f===0){e[a]=0,e[a+1]=0;continue}let r=e[a]/f,s=e[a+1]/f,c=2*r+s-3,i=r+2*s-3;if(c>0&&i>0&&r*(c+i)<c*c){let p=3*f/Math.sqrt(r*r+s*s);e[a]=p*r,e[a+1]=p*s}}return e}function I(o,l,t,n,e){let a=C(e),f=o.length;return Array.from(n,r=>{if(Number.isNaN(r))return NaN;let s=0,c=f;for(;s<c;){let A=s+c>>1;o[A]<=r?s=A+1:c=A}let i=s;if(i===0||i===f){let A=i===0?0:f-1;return a===0?l[A]+t[A]*(r-o[A]):a===1?t[A]:0}let p=o[i-1],d=o[i]-p,h=(r-p)/d,m=h-1,g=l[i-1],u=l[i],w=t[i-1],y=t[i],b=(u-g)/d;switch(a){case 0:{let A=h*h*(3-2*h);return g*(1-A)+d*w*(h*m*m)+u*A+d*y*(h*h*m)}case 1:return b*(-6*h*m)+w*(m*(3*h-1))+y*(h*(3*h-2));case 2:return(b*(6-12*h)+w*(6*h-4)+y*(6*h-2))/d;default:return(-12*b+6*w+6*y)/(d*d)}})}function W(o,l,t){let n=o.length;if(l.length!==n||t.length!==n)throw new RangeError("'x', 'y' and 'm' must have the same length");let e=Array.from({length:n},(s,c)=>c).sort((s,c)=>o[s]-o[c]),a=e.map(s=>o[s]),f=e.map(s=>l[s]),r=e.map(s=>t[s]);for(let s=1;s<n;s++)if(!(a[s]-a[s-1]>0))throw new RangeError("'x' must be *strictly* increasing (non - NA)");return(s,c=0)=>I(a,f,r,s,c)}function Z(o,l,t){let n=Array.from(o),e=Array.from(l),a=n.length;if(e.length!==a)throw new RangeError("'xi' and 'yi' must have the same length");let f=Array.from(t),r=Math.min(...n),s=Math.max(...n);if(f.some(i=>i<r||i>s))throw new RangeError("Some interpolation points outside the nodes.");let c=n.map((i,p)=>{let d=1;for(let h=0;h<a;h++)h!==p&&(d*=i-n[h]);return 1/d});return f.map(i=>{let p=0,d=0,h=n.lastIndexOf(i);if(h>=0)return e[h];for(let m=0;m<a;m++){let g=i-n[m],u=c[m]/g;p+=u*e[m],d+=u}return p/d})}function tt(o,l,t,n){let e=Array.from(o),a=Array.from(l),f=Array.from(t),r=e.length;if(a.length!==r||f.length!==r)throw new RangeError("'xi', 'yi' and 'di' must have the same length");let s=e.flatMap(h=>[h,h]),c=2*r,i=a.flatMap(h=>[h,h]),p=[i[0]],d=i.slice();for(let h=1;h<c;h++){let m=[];for(let g=0;g<c-h;g++)h===1&&s[g+1]===s[g]?m.push(f[g>>1]):m.push((d[g+1]-d[g])/(s[g+h]-s[g]));p.push(m[0]),d=m}return Array.from(n,h=>{let m=p[c-1];for(let g=c-2;g>=0;g--)m=m*(h-s[g])+p[g];return m})}function nt(o,l,t,n=[],e={}){let a=e.method??"rk4";if(a!=="euler"&&a!=="rk2"&&a!=="rk4")throw new RangeError(`unknown method '${String(a)}'`);let f=[...n],r=o.length,s=(d,h)=>{let m=Array.from(t(d,h,f));if(m.length!==r)throw new RangeError(`The number of derivatives returned by func() (${m.length}) must equal the length of the initial conditions vector (${r})`);return m},c=(d,h,m)=>d.map((g,u)=>g+h*m[u]),i=[...o],p=[];if(l.length===0)return p;p.push([l[0],...i]);for(let d=1;d<l.length;d++){let h=l[d-1],m=l[d]-h,g=s(h,i);if(a==="euler")i=c(i,m,g);else if(a==="rk2"){let u=s(h+m,c(i,m,g));i=i.map((w,y)=>w+m*(.5*g[y]+.5*u[y]))}else{let u=s(h+m/2,c(i,m/2,g)),w=s(h+m/2,c(i,m/2,u)),y=s(h+m,c(i,m,w));i=i.map((b,A)=>b+m/6*(g[A]+2*u[A]+2*w[A]+y[A]))}p.push([l[d],...i])}return p}function _(o,l,t){let n=l.eps??1e-4,e=l.d??t,a=l.zeroTol??Math.sqrt(Number.EPSILON/7e-7),f=l.r??4;if(!(Number.isInteger(f)&&f>=1))throw new RangeError("'r' must be a positive integer");return{eps:n,r:f,h0:o.map(r=>Math.abs(e*r)+(Math.abs(r)<a?n:0))}}function x(o){let l=[...o];for(let t=1;t<l.length;t++){let n=4**t;for(let e=0;e<l.length-t;e++)l[e]=(l[e+1]*n-l[e])/(n-1)}return l[0]}function N(o,l,t){let n=[...o];return n[l]=n[l]+t,n}function L(o){let l=o.method??"Richardson";if(l!=="Richardson"&&l!=="simple")throw new RangeError(`unknown method "${String(l)}" (expected "Richardson" or "simple")`);return l}function rt(o,l,t={}){let n=[...l],e=n.length;if(L(t)==="simple"){let s=t.eps??1e-4,c=o([...n]);return n.map((i,p)=>(o(N(n,p,s))-c)/s)}let{r:a,h0:f}=_(n,t,1e-4),r=[];for(let s=0;s<e;s++){let c=[],i=f[s];for(let p=0;p<a;p++){if(p>0&&Math.abs(c[p-1])<1e-20?c.push(0):c.push((o(N(n,s,i))-o(N(n,s,-i)))/(2*i)),Number.isNaN(c[p]))throw new RangeError(`func returned NaN at step ${i} from x`);i/=2}r.push(x(c))}return r}function et(o,l,t={}){let n=[...l],e=n.length,a=Array.from(o([...n])),f=a.map(()=>new Array(e).fill(NaN));if(L(t)==="simple"){let c=t.eps??1e-4;for(let i=0;i<e;i++){let p=o(N(n,i,c));a.forEach((d,h)=>f[h][i]=(p[h]-d)/c)}return f}let{r,h0:s}=_(n,t,1e-4);for(let c=0;c<e;c++){let i=a.map(()=>[]),p=s[c];for(let d=0;d<r;d++){let h=o(N(n,c,p)),m=o(N(n,c,-p));a.forEach((g,u)=>i[u].push((h[u]-m[u])/(2*p))),p/=2}a.forEach((d,h)=>f[h][c]=x(i[h]))}return f}function ot(o,l,t={}){if((t.method??"Richardson")!=="Richardson")throw new RangeError('hessian supports only method "Richardson"');let n=[...l],e=n.length,{r:a,h0:f}=_(n,t,.1),r=o([...n]),s=[];for(let i=0;i<e;i++){let p=[],d=f[i];for(let h=0;h<a;h++)p.push((o(N(n,i,d))-2*r+o(N(n,i,-d)))/d**2),d/=2;s.push(x(p))}let c=n.map(()=>new Array(e).fill(NaN));for(let i=0;i<e;i++){c[i][i]=s[i];for(let p=0;p<i;p++){let d=[],h=f[i],m=f[p];for(let g=0;g<a;g++){let u=N(N(n,i,h),p,m),w=N(N(n,i,-h),p,-m);d.push((o(u)-2*r+o(w)-s[i]*h**2-s[p]*m**2)/(2*h*m)),h/=2,m/=2}c[i][p]=c[p][i]=x(d)}}return c}export{P as approx,Z as barylag,rt as grad,tt as hermiteInterp,ot as hessian,$ as integrate,et as jacobian,nt as ode,O as optim,F as optimHess,T as optimize,Q as spline,U as splinefun,W as splinefunH,D as uniroot};
|
|
1
|
+
/*! rStats.js 0.15.3 | 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 $,b as T,c as D,d as F,e as O}from"./chunks/chunk-PMJI5QO6.js";import"./chunks/chunk-ZIX7NYQF.js";import{a as k,f as j}from"./chunks/chunk-MAUAOQG3.js";import"./chunks/chunk-IUULP3LE.js";import{f as S}from"./chunks/chunk-3WPU6LGK.js";import"./chunks/chunk-VLUL2HFW.js";import"./chunks/chunk-KT66SB7R.js";function M(o,l,t="mean"){if(o.length!==l.length)throw new RangeError("'x' and 'y' lengths differ");let n=[];for(let r=0;r<o.length;r++)!Number.isNaN(o[r])&&!Number.isNaN(l[r])&&n.push(r);n.sort((r,s)=>o[r]-o[s]);let e=typeof t=="function"?t:t==="min"?r=>Math.min(...r):t==="max"?r=>Math.max(...r):S,a=[],f=[];for(let r=0;r<n.length;){let s=o[n[r]],c=r+1;for(;c<n.length&&o[n[c]]===s;)c++;a.push(s),f.push(c-r===1?l[n[r]]:e(n.slice(r,c).map(i=>l[i]))),r=c}return{x:a,y:f}}function X(o,l,t,n){let e=(n-o[t])/(o[t+1]-o[t]);return l[t]+(l[t+1]-l[t])*e}function J(o,l,t){let n=(e,a)=>e===0?0:e*a;return n(1-t,o[l])+n(t,o[l+1])}function P(o,l,t={}){let{method:n="linear",n:e=50,f:a=0,rule:f=1,ties:r="mean"}=t,s=M(o,l,r),c=s.x.length;if(c<=1&&n==="linear")throw new RangeError("need at least two non-NA values to interpolate");if(c===0)throw new RangeError("zero non-NA points");if(n==="constant"&&!(a>=0&&a<=1))throw new RangeError("approx(): invalid f value");let[i,p]=typeof f=="number"?[f,f]:f,d=t.yleft??(i===1?NaN:s.y[0]),h=t.yright??(p===1?NaN:s.y[c-1]),m;if(t.xout!==void 0)m=Array.from(t.xout);else{if(e<=0)throw new RangeError("'approx' requires n >= 1");m=k(s.x[0],s.x[c-1],e)}let g=s.x,u=s.y,w=j(m,g),y=m.map((b,A)=>{let R=w[A];if(Number.isNaN(R))return NaN;if(R===0)return d;let E=R-1;return b===g[E]?u[E]:R===c?h:n==="linear"?X(g,u,E,b):J(u,E,a)});return{x:m,y}}function v(o,l){let t=o.length,n=new Array(t).fill(0),e=new Array(t).fill(0),a=new Array(t).fill(0);if(t<2)return{b:n,c:e,d:a};if(t<3)return n[0]=(l[1]-l[0])/(o[1]-o[0]),n[1]=n[0],{b:n,c:e,d:a};let f=t-1;a[0]=o[1]-o[0],e[1]=(l[1]-l[0])/a[0];for(let r=1;r<f;r++)a[r]=o[r+1]-o[r],n[r]=2*(a[r-1]+a[r]),e[r+1]=(l[r+1]-l[r])/a[r],e[r]=e[r+1]-e[r];n[0]=-a[0],n[f]=-a[t-2],e[0]=0,e[f]=0,t>3&&(e[0]=e[2]/(o[3]-o[1])-e[1]/(o[2]-o[0]),e[f]=e[t-2]/(o[f]-o[t-3])-e[t-3]/(o[t-2]-o[t-4]),e[0]=e[0]*a[0]*a[0]/(o[3]-o[0]),e[f]=-e[f]*a[t-2]*a[t-2]/(o[f]-o[t-4]));for(let r=1;r<t;r++){let s=a[r-1]/n[r-1];n[r]=n[r]-s*a[r-1],e[r]=e[r]-s*e[r-1]}e[f]=e[f]/n[f];for(let r=t-2;r>=0;r--)e[r]=(e[r]-a[r]*e[r+1])/n[r];n[f]=(l[f]-l[t-2])/a[t-2]+a[t-2]*(e[t-2]+2*e[f]);for(let r=0;r<f;r++)n[r]=(l[r+1]-l[r])/a[r]-a[r]*(e[r+1]+2*e[r]),a[r]=(e[r+1]-e[r])/a[r],e[r]=3*e[r];return e[f]=3*e[f],a[f]=a[t-2],{b:n,c:e,d:a}}function z(o,l,t){let n=o.length,e=new Array(n).fill(0),a=new Array(n).fill(0),f=new Array(n).fill(0);for(let r=0;r<n-1;r++){let s=o[r+1]-o[r];e[r]=(l[r+1]-l[r])/s-s*(t[r+1]+2*t[r]),a[r]=3*t[r],f[r]=(t[r+1]-t[r])/s}return{b:e,c:a,d:f}}function Y(o,l){let t=o.length;if(t<3)return v(o,l);let n=Array.from({length:t-1},(c,i)=>o[i+1]-o[i]),e=n.map((c,i)=>(l[i+1]-l[i])/c),a=new Array(t).fill(0),f=new Array(t).fill(0);for(let c=1;c<t-1;c++)a[c]=2*(n[c-1]+n[c]),f[c]=e[c]-e[c-1];for(let c=2;c<t-1;c++){let i=n[c-1]/a[c-1];a[c]=a[c]-i*n[c-1],f[c]=f[c]-i*f[c-1]}let r=new Array(t).fill(0);r[t-2]=f[t-2]/a[t-2];for(let c=t-3;c>=1;c--)r[c]=(f[c]-n[c]*r[c+1])/a[c];let s=z(o,l,r);return s.b[t-1]=e[t-2]+n[t-2]*r[t-2],s}function B(o,l){let t=o.length;if(l[0]!==l[t-1])throw new RangeError("periodic spline needs y[1] == y[n]");if(t<3)return{b:new Array(t).fill(0),c:new Array(t).fill(0),d:new Array(t).fill(0)};let n=t-1,e=Array.from({length:n},(u,w)=>o[w+1]-o[w]),a=e.map((u,w)=>(l[w+1]-l[w])/u),f=u=>u===0?n-1:u-1,r=Array.from({length:n},(u,w)=>2*(e[f(w)]+e[w])),s=Array.from({length:n},(u,w)=>a[w]-a[f(w)]),c=e.slice(),i=new Array(n).fill(0);i[0]=e[n-1],i[n-2]=i[n-2]+e[n-2];let p=i[0],d=r[n-1],h=s[n-1];for(let u=0;u<n-1;u++){if(u+1<n-1){let y=e[u]/r[u];r[u+1]=r[u+1]-y*c[u],i[u+1]=i[u+1]-y*i[u],s[u+1]=s[u+1]-y*s[u]}let w=p/r[u];d-=w*i[u],h-=w*s[u],u+1<n-1&&(p=(u+1===n-2?e[n-2]:0)-w*c[u])}let m=new Array(t).fill(0);m[n-1]=h/d;for(let u=n-2;u>=0;u--){let w=s[u]-i[u]*m[n-1]-(u<n-2?c[u]*m[u+1]:0);m[u]=w/r[u]}m[t-1]=m[0];let g=z(o,l,m);return g.b[t-1]=g.b[0],g.c[t-1]=g.c[0],g.d[t-1]=g.d[0],g}function G(o,l){let t=o.length,{b:n,c:e,d:a}=v(o,l);if(t<2)return{b:n,c:e,d:a};let f=Array.from({length:t-1},(s,c)=>(l[c+1]-l[c])/(o[c+1]-o[c]));for(let s=0;s<t;s++){let c=f[s===0?0:s-1],i=f[s===t-1?t-2:s],p=Math.min(Math.abs(c),Math.abs(i)),d=c*i>0?i>0:n[s]>=0;n[s]=d?Math.min(Math.max(0,n[s]),3*p):Math.max(Math.min(0,n[s]),-3*p)}for(let s=0;s<t-1;s++){let c=o[s+1]-o[s],i=(l[s+1]-l[s])/c;e[s]=(3*i-2*n[s]-n[s+1])/c,a[s]=(n[s]+n[s+1]-2*i)/(c*c)}let r=o[t-1]-o[t-2];return e[t-1]=(n[t-2]+2*n[t-1]-3*(l[t-1]-l[t-2])/r)/r,a[t-1]=a[t-2],{b:n,c:e,d:a}}function q(o,l,t,n){let e=M(o,l,n),a=e.x.length;if(a===0)throw new RangeError("zero non-NA points");t==="periodic"&&e.y[a-1]!==e.y[0]&&(e.y[a-1]=e.y[0]),t==="hyman"&&K(e.y);let f=t==="fmm"?v(e.x,e.y):t==="natural"?Y(e.x,e.y):t==="periodic"?B(e.x,e.y):t==="hyman"?G(e.x,e.y):(()=>{throw new RangeError(`invalid spline method '${String(t)}'`)})();return{method:t,...e,...f}}function K(o){let l=!0,t=!0;for(let n=1;n<o.length;n++){let e=o[n]-o[n-1];e>=0||(l=!1),e<=0||(t=!1)}if(!l&&!t)throw new RangeError("'y' must be increasing or decreasing")}function C(o){let l=Math.trunc(o);if(!(l>=0&&l<=3))throw new RangeError("'deriv' must be between 0 and 3");return l}function H(o,l,t){let n=C(t),{x:e,y:a,b:f,c:r,d:s,method:c}=o,i=e.length,p=c==="natural",d=e[i-1]-e[0],h=0;return Array.from(l,m=>{let g=m;if(c==="periodic"&&i>1){let y=(g-e[0])%d;y<0&&(y+=d),g=e[0]+y}if(p&&n>0&&g<=e[0])return n===1?f[0]:0;if(!(e[h]<=g&&(h===i-1||g<=e[h+1]))){let y=0,b=i;for(;b-y>1;){let A=y+b>>1;e[A]<=g?y=A:b=A}h=y}let u=g-e[h],w=p&&g<e[0]?0:s[h];switch(n){case 0:return a[h]+u*(f[h]+u*(r[h]+u*w));case 1:return f[h]+u*(2*r[h]+u*(3*w));case 2:return 2*r[h]+u*(6*w);default:return 6*w}})}function Q(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t,a=q(o,l,n,e),f;if(t.xout!==void 0)f=Array.from(t.xout);else{let r=a.x.length,s=t.n??3*r;if(s<=0)throw new RangeError("'spline' requires n >= 1");f=k(t.xmin??a.x[0],t.xmax??a.x[r-1],s)}return{x:f,y:H(a,f,0)}}function U(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t;if(n==="monoH.FC"){let f=M(o,l,e);if(f.x.length===0)throw new RangeError("zero non-NA points");let r=V(f.x,f.y);return(s,c=0)=>I(f.x,f.y,r,s,c)}let a=q(o,l,n,e);return(f,r=0)=>H(a,f,r)}function V(o,l){let t=o.length;if(t<2)throw new RangeError("length(m) must be at least two");let n=Array.from({length:t-1},(a,f)=>(l[f+1]-l[f])/(o[f+1]-o[f])),e=Array.from({length:t},(a,f)=>f===0?n[0]:f===t-1?n[t-2]:(n[f-1]+n[f])/2);for(let a=0;a<t-1;a++){let f=n[a];if(f===0){e[a]=0,e[a+1]=0;continue}let r=e[a]/f,s=e[a+1]/f,c=2*r+s-3,i=r+2*s-3;if(c>0&&i>0&&r*(c+i)<c*c){let p=3*f/Math.sqrt(r*r+s*s);e[a]=p*r,e[a+1]=p*s}}return e}function I(o,l,t,n,e){let a=C(e),f=o.length;return Array.from(n,r=>{if(Number.isNaN(r))return NaN;let s=0,c=f;for(;s<c;){let A=s+c>>1;o[A]<=r?s=A+1:c=A}let i=s;if(i===0||i===f){let A=i===0?0:f-1;return a===0?l[A]+t[A]*(r-o[A]):a===1?t[A]:0}let p=o[i-1],d=o[i]-p,h=(r-p)/d,m=h-1,g=l[i-1],u=l[i],w=t[i-1],y=t[i],b=(u-g)/d;switch(a){case 0:{let A=h*h*(3-2*h);return g*(1-A)+d*w*(h*m*m)+u*A+d*y*(h*h*m)}case 1:return b*(-6*h*m)+w*(m*(3*h-1))+y*(h*(3*h-2));case 2:return(b*(6-12*h)+w*(6*h-4)+y*(6*h-2))/d;default:return(-12*b+6*w+6*y)/(d*d)}})}function W(o,l,t){let n=o.length;if(l.length!==n||t.length!==n)throw new RangeError("'x', 'y' and 'm' must have the same length");let e=Array.from({length:n},(s,c)=>c).sort((s,c)=>o[s]-o[c]),a=e.map(s=>o[s]),f=e.map(s=>l[s]),r=e.map(s=>t[s]);for(let s=1;s<n;s++)if(!(a[s]-a[s-1]>0))throw new RangeError("'x' must be *strictly* increasing (non - NA)");return(s,c=0)=>I(a,f,r,s,c)}function Z(o,l,t){let n=Array.from(o),e=Array.from(l),a=n.length;if(e.length!==a)throw new RangeError("'xi' and 'yi' must have the same length");let f=Array.from(t),r=Math.min(...n),s=Math.max(...n);if(f.some(i=>i<r||i>s))throw new RangeError("Some interpolation points outside the nodes.");let c=n.map((i,p)=>{let d=1;for(let h=0;h<a;h++)h!==p&&(d*=i-n[h]);return 1/d});return f.map(i=>{let p=0,d=0,h=n.lastIndexOf(i);if(h>=0)return e[h];for(let m=0;m<a;m++){let g=i-n[m],u=c[m]/g;p+=u*e[m],d+=u}return p/d})}function tt(o,l,t,n){let e=Array.from(o),a=Array.from(l),f=Array.from(t),r=e.length;if(a.length!==r||f.length!==r)throw new RangeError("'xi', 'yi' and 'di' must have the same length");let s=e.flatMap(h=>[h,h]),c=2*r,i=a.flatMap(h=>[h,h]),p=[i[0]],d=i.slice();for(let h=1;h<c;h++){let m=[];for(let g=0;g<c-h;g++)h===1&&s[g+1]===s[g]?m.push(f[g>>1]):m.push((d[g+1]-d[g])/(s[g+h]-s[g]));p.push(m[0]),d=m}return Array.from(n,h=>{let m=p[c-1];for(let g=c-2;g>=0;g--)m=m*(h-s[g])+p[g];return m})}function nt(o,l,t,n=[],e={}){let a=e.method??"rk4";if(a!=="euler"&&a!=="rk2"&&a!=="rk4")throw new RangeError(`unknown method '${String(a)}'`);let f=[...n],r=o.length,s=(d,h)=>{let m=Array.from(t(d,h,f));if(m.length!==r)throw new RangeError(`The number of derivatives returned by func() (${m.length}) must equal the length of the initial conditions vector (${r})`);return m},c=(d,h,m)=>d.map((g,u)=>g+h*m[u]),i=[...o],p=[];if(l.length===0)return p;p.push([l[0],...i]);for(let d=1;d<l.length;d++){let h=l[d-1],m=l[d]-h,g=s(h,i);if(a==="euler")i=c(i,m,g);else if(a==="rk2"){let u=s(h+m,c(i,m,g));i=i.map((w,y)=>w+m*(.5*g[y]+.5*u[y]))}else{let u=s(h+m/2,c(i,m/2,g)),w=s(h+m/2,c(i,m/2,u)),y=s(h+m,c(i,m,w));i=i.map((b,A)=>b+m/6*(g[A]+2*u[A]+2*w[A]+y[A]))}p.push([l[d],...i])}return p}function _(o,l,t){let n=l.eps??1e-4,e=l.d??t,a=l.zeroTol??Math.sqrt(Number.EPSILON/7e-7),f=l.r??4;if(!(Number.isInteger(f)&&f>=1))throw new RangeError("'r' must be a positive integer");return{eps:n,r:f,h0:o.map(r=>Math.abs(e*r)+(Math.abs(r)<a?n:0))}}function x(o){let l=[...o];for(let t=1;t<l.length;t++){let n=4**t;for(let e=0;e<l.length-t;e++)l[e]=(l[e+1]*n-l[e])/(n-1)}return l[0]}function N(o,l,t){let n=[...o];return n[l]=n[l]+t,n}function L(o){let l=o.method??"Richardson";if(l!=="Richardson"&&l!=="simple")throw new RangeError(`unknown method "${String(l)}" (expected "Richardson" or "simple")`);return l}function rt(o,l,t={}){let n=[...l],e=n.length;if(L(t)==="simple"){let s=t.eps??1e-4,c=o([...n]);return n.map((i,p)=>(o(N(n,p,s))-c)/s)}let{r:a,h0:f}=_(n,t,1e-4),r=[];for(let s=0;s<e;s++){let c=[],i=f[s];for(let p=0;p<a;p++){if(p>0&&Math.abs(c[p-1])<1e-20?c.push(0):c.push((o(N(n,s,i))-o(N(n,s,-i)))/(2*i)),Number.isNaN(c[p]))throw new RangeError(`func returned NaN at step ${i} from x`);i/=2}r.push(x(c))}return r}function et(o,l,t={}){let n=[...l],e=n.length,a=Array.from(o([...n])),f=a.map(()=>new Array(e).fill(NaN));if(L(t)==="simple"){let c=t.eps??1e-4;for(let i=0;i<e;i++){let p=o(N(n,i,c));a.forEach((d,h)=>f[h][i]=(p[h]-d)/c)}return f}let{r,h0:s}=_(n,t,1e-4);for(let c=0;c<e;c++){let i=a.map(()=>[]),p=s[c];for(let d=0;d<r;d++){let h=o(N(n,c,p)),m=o(N(n,c,-p));a.forEach((g,u)=>i[u].push((h[u]-m[u])/(2*p))),p/=2}a.forEach((d,h)=>f[h][c]=x(i[h]))}return f}function ot(o,l,t={}){if((t.method??"Richardson")!=="Richardson")throw new RangeError('hessian supports only method "Richardson"');let n=[...l],e=n.length,{r:a,h0:f}=_(n,t,.1),r=o([...n]),s=[];for(let i=0;i<e;i++){let p=[],d=f[i];for(let h=0;h<a;h++)p.push((o(N(n,i,d))-2*r+o(N(n,i,-d)))/d**2),d/=2;s.push(x(p))}let c=n.map(()=>new Array(e).fill(NaN));for(let i=0;i<e;i++){c[i][i]=s[i];for(let p=0;p<i;p++){let d=[],h=f[i],m=f[p];for(let g=0;g<a;g++){let u=N(N(n,i,h),p,m),w=N(N(n,i,-h),p,-m);d.push((o(u)-2*r+o(w)-s[i]*h**2-s[p]*m**2)/(2*h*m)),h/=2,m/=2}c[i][p]=c[p][i]=x(d)}}return c}export{P as approx,Z as barylag,rt as grad,tt as hermiteInterp,ot as hessian,$ as integrate,et as jacobian,nt as ode,O as optim,F as optimHess,T as optimize,Q as spline,U as splinefun,W as splinefunH,D as uniroot};
|