@numero-uno-jpn/rstats 0.13.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/README.ja.md +18 -9
- package/README.md +18 -9
- package/dist/chunks/{chunk-MEMYFTYS.js → chunk-4EOKNOXP.js} +1 -1
- package/dist/chunks/chunk-4XGKAHD3.js +2 -0
- package/dist/chunks/chunk-7GEYDIQC.js +2 -0
- package/dist/chunks/{chunk-F44XUHWI.js → chunk-7XHAE2LG.js} +1 -1
- package/dist/chunks/{chunk-BSOUJPDF.js → chunk-A3XZUJCP.js} +2 -2
- package/dist/chunks/{chunk-SVWU5T7C.js → chunk-AEFWSUZ2.js} +2 -2
- package/dist/chunks/chunk-CJ5ETQCK.js +2 -0
- package/dist/chunks/{chunk-4GNJEYXR.js → chunk-CMPDE3SZ.js} +2 -2
- package/dist/chunks/chunk-GZTLODPK.js +3 -0
- package/dist/chunks/{chunk-3AKA4KEL.js → chunk-HNLKOICT.js} +1 -1
- package/dist/chunks/{chunk-SLSDFIW2.js → chunk-HS7KIFMR.js} +2 -2
- package/dist/chunks/{chunk-CQNVEA22.js → chunk-LU5FHU2O.js} +2 -2
- package/dist/chunks/chunk-LVXAWWUX.js +2 -0
- package/dist/chunks/{chunk-BJHBYEEU.js → chunk-LWUTCYTZ.js} +1 -1
- package/dist/chunks/chunk-NYGWVZOC.js +2 -0
- package/dist/chunks/{chunk-4AQNVF3Z.js → chunk-PRDF6IU6.js} +2 -2
- package/dist/chunks/{chunk-VG67L3JA.js → chunk-QQIWOGHR.js} +2 -2
- package/dist/chunks/chunk-VAPGDPHM.js +2 -0
- package/dist/chunks/chunk-VSKWQMSG.js +2 -0
- package/dist/chunks/{chunk-JGFUZKLJ.js → chunk-WEWXYHPR.js} +2 -2
- package/dist/chunks/{chunk-2IAT6MT2.js → chunk-XMTYTJT6.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/mixed.d.ts +3 -1
- package/dist/entries/mixed.js +4 -1
- package/dist/internal/fround.d.ts +12 -0
- package/dist/internal/fround.js +89 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -3
- package/dist/models/glm.js +4 -2
- package/dist/models/glmFamily.js +16 -4
- package/dist/models/lmer.d.ts +64 -8
- package/dist/models/lmer.js +146 -20
- package/dist/models/lmerAnova.d.ts +143 -0
- package/dist/models/lmerAnova.js +140 -0
- package/dist/models/lmerHypotheses.d.ts +106 -0
- package/dist/models/lmerHypotheses.js +305 -0
- package/dist/models/lmerTest.d.ts +239 -0
- package/dist/models/lmerTest.js +311 -0
- package/dist/models/mixedDerivatives.d.ts +27 -0
- package/dist/models/mixedDerivatives.js +342 -0
- package/dist/models/mixedDeviance.d.ts +112 -0
- package/dist/models/mixedDeviance.js +216 -86
- package/dist/models/mixedStructure.d.ts +5 -3
- package/dist/models/mixedStructure.js +30 -14
- package/dist/models/modelMatrix.d.ts +10 -3
- package/dist/models/modelMatrix.js +23 -2
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +13 -13
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -12
- package/llms.txt +6 -1
- package/package.json +1 -1
- package/dist/chunks/chunk-22SXZNR6.js +0 -2
- package/dist/chunks/chunk-AGRKZXLG.js +0 -2
- package/dist/chunks/chunk-N3FV6H3C.js +0 -2
- package/dist/chunks/chunk-NRV3BH7I.js +0 -2
- package/dist/chunks/chunk-RTWLZPPG.js +0 -2
- package/dist/chunks/chunk-VWGJYRE6.js +0 -2
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// R's round(x, digits) and zapsmall(x).
|
|
2
|
+
//
|
|
3
|
+
// Written for rStats.js (no ported code) from the description in oracle/lmertest-behaviour.md ("Rounding") and R's
|
|
4
|
+
// documentation of round (?Round: the candidates on either side of x at the given number of decimal digits, the one
|
|
5
|
+
// closer to x chosen, a tie to the even last digit; digits = 0 is rounding to the nearest integer, ties to even).
|
|
6
|
+
const LOG10_2 = Math.log10(2);
|
|
7
|
+
/** The binary exponent of |x| (finite, nonzero): the exponent of its leading bit, as C's logb */
|
|
8
|
+
function binaryExponent(x) {
|
|
9
|
+
const view = new DataView(new ArrayBuffer(8));
|
|
10
|
+
view.setFloat64(0, x);
|
|
11
|
+
const biased = (view.getUint16(0) >> 4) & 0x7ff;
|
|
12
|
+
if (biased > 0)
|
|
13
|
+
return biased - 1023;
|
|
14
|
+
// subnormal: the position of the leading bit of the significand
|
|
15
|
+
const hi = view.getUint32(0) & 0xfffff;
|
|
16
|
+
const lo = view.getUint32(4);
|
|
17
|
+
const bits = hi !== 0 ? 32 + (31 - Math.clz32(hi)) : 31 - Math.clz32(lo);
|
|
18
|
+
return bits - 1074;
|
|
19
|
+
}
|
|
20
|
+
/** 10^k for an integer k >= 0, by repeated multiplication in double arithmetic */
|
|
21
|
+
function powerOf10(k) {
|
|
22
|
+
let p = 1;
|
|
23
|
+
for (let i = 0; i < k; i++)
|
|
24
|
+
p *= 10;
|
|
25
|
+
return p;
|
|
26
|
+
}
|
|
27
|
+
/** The nearest integer, ties to even (C's nearbyint in the default rounding mode); the sign of x is kept */
|
|
28
|
+
function roundHalfEven(x) {
|
|
29
|
+
const f = Math.floor(x);
|
|
30
|
+
const d = x - f;
|
|
31
|
+
const r = d < 0.5 ? f : d > 0.5 ? f + 1 : f % 2 === 0 ? f : f + 1;
|
|
32
|
+
return r === 0 ? (x < 0 ? -0 : 0) : r;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* R's `round(x, digits)`: x rounded to `digits` decimal places (negative: to a power of ten), choosing between the
|
|
36
|
+
* two neighbouring candidates the one closer to x in double arithmetic, a tie to the even one. Values that already
|
|
37
|
+
* have at most about 15 significant digits at that position are returned unchanged.
|
|
38
|
+
*/
|
|
39
|
+
export function roundR(x, digits = 0) {
|
|
40
|
+
if (Number.isNaN(x) || Number.isNaN(digits))
|
|
41
|
+
return NaN;
|
|
42
|
+
if (!Number.isFinite(x) || x === 0 || digits > 323)
|
|
43
|
+
return x;
|
|
44
|
+
if (digits < -308)
|
|
45
|
+
return 0;
|
|
46
|
+
if (digits === 0)
|
|
47
|
+
return roundHalfEven(x);
|
|
48
|
+
const dig = Math.floor(digits + 0.5);
|
|
49
|
+
const ax = Math.abs(x);
|
|
50
|
+
const l = LOG10_2 * (0.5 + binaryExponent(ax));
|
|
51
|
+
if (l + dig > 15)
|
|
52
|
+
return x;
|
|
53
|
+
let xd;
|
|
54
|
+
let xu;
|
|
55
|
+
let i10;
|
|
56
|
+
if (dig <= 308) {
|
|
57
|
+
const p = dig >= 0 ? powerOf10(dig) : 1 / powerOf10(-dig);
|
|
58
|
+
const x10 = ax * p;
|
|
59
|
+
i10 = Math.floor(x10);
|
|
60
|
+
xd = i10 / p;
|
|
61
|
+
xu = Math.ceil(x10) / p;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const p308 = powerOf10(308);
|
|
65
|
+
const pRest = powerOf10(dig - 308);
|
|
66
|
+
const x10 = ax * p308 * pRest;
|
|
67
|
+
i10 = Math.floor(x10);
|
|
68
|
+
xd = i10 / p308 / pRest;
|
|
69
|
+
xu = Math.ceil(x10) / p308 / pRest;
|
|
70
|
+
}
|
|
71
|
+
const du = xu - ax;
|
|
72
|
+
const dd = ax - xd;
|
|
73
|
+
const r = du < dd || (du === dd && i10 % 2 === 1) ? xu : xd;
|
|
74
|
+
return x < 0 ? -r : r;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* R's `zapsmall(x, digits = 7)` of the entries of a matrix (array of rows) taken together: every entry rounded by
|
|
78
|
+
* {@link roundR} to `max(0, digits - log10(mx))` decimal places, mx the largest absolute entry (NaN left out), or
|
|
79
|
+
* to `digits` places when mx is 0.
|
|
80
|
+
*/
|
|
81
|
+
export function zapsmall(m, digits = 7) {
|
|
82
|
+
let mx = 0;
|
|
83
|
+
for (const row of m)
|
|
84
|
+
for (const v of row)
|
|
85
|
+
if (Math.abs(v) > mx)
|
|
86
|
+
mx = Math.abs(v);
|
|
87
|
+
const d = mx > 0 ? Math.max(0, digits - Math.log10(mx)) : digits;
|
|
88
|
+
return m.map((row) => row.map((v) => roundR(v, d)));
|
|
89
|
+
}
|
package/dist/linalg.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 0.
|
|
2
|
-
import{a as S,b as U,c as Q,d as rt,e as j,f as xt,g as Rt,h as X,i as z,j as Ot,k as et,l as ot}from"./chunks/chunk-22SXZNR6.js";import{F as ct,h as at,i as st}from"./chunks/chunk-SVWU5T7C.js";import"./chunks/chunk-SLSDFIW2.js";import"./chunks/chunk-MEMYFTYS.js";import{a as E,b as nt,c as A,d as O,e as v,f as L,g as T,h as V,k as bt,l as yt,m as vt,n as kt,o as Et,p as qt,q as D,r as Nt,s as At,t as Ft,v as It}from"./chunks/chunk-VWGJYRE6.js";import"./chunks/chunk-JGFUZKLJ.js";import"./chunks/chunk-3AKA4KEL.js";function St(r,n){let t=O(r),o=n===void 0?t:O(n);if(t.nrow!==o.nrow)throw new TypeError(`non-conformable arguments (${t.nrow} and ${o.nrow} rows)`);let u=t.nrow,s=E(t.ncol,o.ncol);for(let c=0;c<o.ncol;c++)for(let i=0;i<t.ncol;i++){if(n===void 0&&i>c)continue;let l=0;for(let a=0;a<u;a++)l+=t.data[a+i*u]*o.data[a+c*u];s.data[i+c*t.ncol]=l}return n===void 0&&it(s),v(s)}function jt(r,n){let t=O(r),o=n===void 0?t:O(n);if(t.ncol!==o.ncol)throw new TypeError(`non-conformable arguments (${t.ncol} and ${o.ncol} columns)`);let u=E(t.nrow,o.nrow);for(let s=0;s<o.nrow;s++)for(let c=0;c<t.nrow;c++){if(n===void 0&&c>s)continue;let i=0;for(let l=0;l<t.ncol;l++)i+=t.data[c+l*t.nrow]*o.data[s+l*o.nrow];u.data[c+s*t.nrow]=i}return n===void 0&&it(u),v(u)}function it(r){let n=r.nrow;for(let t=0;t<n;t++)for(let o=t+1;o<n;o++)r.data[o+t*n]=r.data[t+o*n]}function _t(r,n,t={}){return lt(r,n,t.k,t.upperTri??!0,t.transpose??!1)}function Pt(r,n,t={}){return lt(r,n,t.k,t.upperTri??!1,t.transpose??!1)}function lt(r,n,t,o,u){let s=A(r),c=O(n),i=t??s.ncol;if(!Number.isInteger(i)||i<1||i>s.nrow||i>s.ncol||i>c.nrow)throw new RangeError(`invalid k (got ${i})`);for(let a=0;a<i;a++)if(s.data[a+a*s.nrow]===0)throw new Error(`singular matrix: diagonal element ${a+1} is zero`);let l=E(i,c.ncol);for(let a=0;a<c.ncol;a++)for(let f=0;f<i;f++)l.data[f+a*i]=c.data[f+a*c.nrow];return Tt(s,i,l,o,u),nt(n)?v(l):Array.from(l.data)}function Tt(r,n,t,o,u){let s=r.nrow,c=r.data,i=t.nrow,l=t.data;for(let a=0;a<t.ncol;a++){let f=a*i;if(!u&&o){for(let h=n-1;h>=0;h--)if(l[f+h]!==0){l[f+h]/=c[h+h*s];let m=l[f+h];for(let e=0;e<h;e++)l[f+e]-=m*c[e+h*s]}}else if(u)if(o)for(let h=0;h<n;h++){let m=l[f+h];for(let e=0;e<h;e++)m-=c[e+h*s]*l[f+e];l[f+h]=m/c[h+h*s]}else for(let h=n-1;h>=0;h--){let m=l[f+h];for(let e=h+1;e<n;e++)m-=c[e+h*s]*l[f+e];l[f+h]=m/c[h+h*s]}else for(let h=0;h<n;h++)if(l[f+h]!==0){l[f+h]/=c[h+h*s];let m=l[f+h];for(let e=h+1;e<n;e++)l[f+e]-=m*c[e+h*s]}}}function Ut(r,n={}){if(n.exact){let s=n.norm===void 0?"2":$t(n.norm);if(s==="2"){let c=S(r,{nu:0,nv:0}).d,i=Math.min(...c);return i===0?1/0:Math.max(...c)/i}return U(r,{type:s})*U(j(r),{type:s})}let t=n.norm==="I"?"I":"O";if((n.method??"qr")==="direct"){if(n.triangular)return 1/ft(A(r),(n.uplo??"U")==="U",t);let s=n.norm??"O",c=s.toUpperCase();if(c!=="1"&&c!=="O"&&c!=="I")throw new RangeError(`argument type[1]='${s}' must be one of '1','O', or 'I'`);return 1/X(r,{norm:c==="I"?"I":"O"})}let o=A(r),u=o.nrow<o.ncol?v(L(o)):r;return 1/ft(A(D(V(u))),!0,t)}function ft(r,n,t){return Ht(t==="I"?L(r):r,n)}function $t(r){let n=r.toUpperCase();if(n==="1"||n==="O")return"O";if(n==="E")return"F";if(n==="I"||n==="F"||n==="M"||n==="2")return n;throw new RangeError(`invalid norm "${r}"`)}function Ht(r,n){let t=Math.min(r.nrow,r.ncol),o=r.nrow,u=(m,e)=>r.data[m+e*o],s=!n,c=0;for(let m=0;m<t;m++){let[e,d]=s?[m,t-m]:[0,m+1],p=0;for(let g=e;g<e+d;g++)p+=Math.abs(u(g,m));c=Math.max(c,p)}let i=1,l=new Float64Array(t),a=m=>{for(let e=0;e<t;e++)l[e]*=m};for(let m=0;m<t;m++){let e=s?t-1-m:m,d=u(e,e);if(l[e]!==0&&(i=l[e]>0?-Math.abs(i):Math.abs(i)),Math.abs(i-l[e])>Math.abs(d)){let b=Math.abs(d)/Math.abs(i-l[e]);a(b),i*=b}let p=i-l[e],g=-i-l[e],w=Math.abs(p),M=Math.abs(g);if(d!==0?(p/=d,g/=d):(p=1,g=1),m!==t-1){let[b,R]=s?[0,e-1]:[e+1,t-1];for(let y=b;y<=R;y++)M+=Math.abs(l[y]+g*u(e,y)),l[y]+=p*u(e,y),w+=Math.abs(l[y]);if(w<M){let y=g-p;p=g;for(let x=b;x<=R;x++)l[x]+=y*u(e,x)}}l[e]=p}a(1/ut(l));let f=1;for(let m=0;m<t;m++){let e=s?m:t-1-m,d=u(e,e);if(Math.abs(l[e])>Math.abs(d)){let p=Math.abs(d)/Math.abs(l[e]);a(p),f*=p}if(d!==0?l[e]/=d:l[e]=1,m<t-1){let p=-l[e];if(s)for(let g=e+1;g<t;g++)l[g]+=p*u(g,e);else for(let g=0;g<e;g++)l[g]+=p*u(g,e)}}let h=1/ut(l);return a(h),f*=h,c!==0?f/c:0}function ut(r){let n=0;for(let t of r)n+=Math.abs(t);return n}function Lt(r,n={}){let t=n.tol??Math.sqrt(Number.EPSILON),o=A(r),{d:u,u:s,v:c}=S(r),i=o.nrow,l=o.ncol,a=E(l,i),f=Math.max(t*(u[0]??0),0);for(let h=0;h<u.length;h++){if(!(u[h]>f))continue;let m=1/u[h];for(let e=0;e<i;e++){let d=s[e][h]*m;for(let p=0;p<l;p++)a.data[p+e*l]+=c[p][h]*d}}return v(a)}function Vt(r){let n=A(r);T(n);let t=n.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let e of n.data)if(!Number.isFinite(e))throw new RangeError("the matrix contains NaN or infinite values");let o={nrow:t,ncol:t,data:n.data.slice()},u=E(t,t),s=new Float64Array(t),c=new Float64Array(t);et(o,u),ot(o,u,s,c,!0);let i=o.data,l=u.data,a=(e,d)=>e+d*t,f=new Uint8Array(t);for(let e=0;e+1<t;e++)c[e]>0&&c[e+1]<0&&(f[e++]=1);for(let e=0;e<t;e++)for(let d=e+1;d<t;d++)d===e+1&&f[e]||(i[a(d,e)]=0);let h=new Array(t),m=new Array(t);for(let e=0;e<t;e++){if(!f[e]){h[e]=i[a(e,e)],m[e]=0;continue}let d=Dt(i[a(e,e)],i[a(e,e+1)],i[a(e+1,e)],i[a(e+1,e+1)]);i[a(e,e)]=d.a,i[a(e,e+1)]=d.b,i[a(e+1,e)]=d.c,i[a(e+1,e+1)]=d.d;let{cs:p,sn:g}=d;for(let w=e+2;w<t;w++){let M=i[a(e,w)],b=i[a(e+1,w)];i[a(e,w)]=p*M+g*b,i[a(e+1,w)]=p*b-g*M}for(let w=0;w<e;w++){let M=i[a(w,e)],b=i[a(w,e+1)];i[a(w,e)]=p*M+g*b,i[a(w,e+1)]=p*b-g*M}for(let w=0;w<t;w++){let M=l[a(w,e)],b=l[a(w,e+1)];l[a(w,e)]=p*M+g*b,l[a(w,e+1)]=p*b-g*M}h[e]=d.rt1r,m[e]=d.rt1i,h[e+1]=d.rt2r,m[e+1]=d.rt2i,e++}return{T:v(o),Q:v(u),valuesRe:h,valuesIm:m}}var I=(r,n)=>n>=0&&!Object.is(n,-0)?Math.abs(r):-Math.abs(r);function Dt(r,n,t,o){let u=22250738585072014e-324,s=Number.EPSILON,c=4,i=2**Math.trunc(Math.log(u/s)/Math.log(2)/2),l=1/i,a,f;if(t===0)a=1,f=0;else if(n===0){a=0,f=1;let m=o;o=r,r=m,n=-t,t=0}else if(r-o===0&&I(1,n)!==I(1,t))a=1,f=0;else{let m=r-o,e=.5*m,d=Math.max(Math.abs(n),Math.abs(t)),p=Math.min(Math.abs(n),Math.abs(t))*I(1,n)*I(1,t),g=Math.max(Math.abs(e),d),w=e/g*e+d/g*p;if(w>=c*s){w=e+I(Math.sqrt(g)*Math.sqrt(w),e),r=o+w,o=o-d/w*p;let M=Math.hypot(t,w);a=w/M,f=t/M,n=n-t,t=0}else{let M=0,b=n+t;for(;M++,g=Math.max(Math.abs(m),Math.abs(b)),!(!(g>=l&&(b*=i,m*=i,M<=20))&&!(g<=i&&(b*=l,m*=l,M<=20))););e=.5*m;let R=Math.hypot(b,m);a=Math.sqrt(.5*(1+Math.abs(b)/R)),f=-(e/(R*a))*I(1,b);let y=r*a+n*f,x=-r*f+n*a,J=t*a+o*f,K=-t*f+o*a;if(r=y*a+J*f,n=x*a+K*f,t=-y*f+J*a,o=-x*f+K*a,m=.5*(r+o),r=m,o=m,t!==0)if(n!==0){if(I(1,n)===I(1,t)){let W=Math.sqrt(Math.abs(n)),Y=Math.sqrt(Math.abs(t));e=I(W*Y,t),R=1/Math.sqrt(Math.abs(n+t)),r=m+e,o=m-e,n=n-t,t=0;let Z=W*R,tt=Y*R;m=a*Z-f*tt,f=a*tt+f*Z,a=m}}else n=-t,t=0,m=a,a=-f,f=m}}let h=t===0?0:Math.sqrt(Math.abs(n))*Math.sqrt(Math.abs(t));return{a:r,b:n,c:t,d:o,cs:a,sn:f,rt1r:r,rt1i:h,rt2r:o,rt2i:-h}}var Qt=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function Xt(r,n={}){let t=n.method??"Higham08.b";if(!Qt.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let o=n.order??8;if(!Number.isInteger(o)||o<1)throw new RangeError("order must be a positive integer");let u=A(r);T(u);let s=u.nrow;if(s===0)return[];if(s===1)return[[Math.exp(u.data[0])]];if(t==="Taylor"||t==="TaylorO")return v(Gt(u,o));if(t==="Pade"||t==="PadeO")return v(Jt(u,o));let c,i;t==="Higham08.b"&&(c=z("P",u),i=z("S",u));let l=Bt(u),a=l.data,f=(h,m)=>h+m*s;if(i){let h=i.scale;for(let m=0;m<s;m++)for(let e=0;e<s;e++)a[f(e,m)]=a[f(e,m)]*h[e]/h[m]}if(c){let h=c.scale,m=e=>{let d=h[e];if(d!==e){for(let p=0;p<s;p++)[a[f(p,e)],a[f(p,d)]]=[a[f(p,d)],a[f(p,e)]];for(let p=0;p<s;p++)[a[f(e,p)],a[f(d,p)]]=[a[f(d,p)],a[f(e,p)]]}};for(let e=c.ilo-1;e>=0;e--)m(e);for(let e=c.ihi+1;e<s;e++)m(e)}return v(l)}var zt={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},Ct=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function Bt(r){let n=r.nrow,t=Q(r),o=C(n),u,s,c=0;if(t<=2.1){let f=t<=.015?3:t<=.25?5:t<=.95?7:9,h=zt[f],m=N(r,r),e=$([[h[1],o]]),d=$([[h[0],o]]),p=m;for(let g=1;g<=(f-1)/2;g++)g>1&&(p=N(p,m)),_(e,h[2*g+1],p),_(d,h[2*g],p);u=N(r,e),s=d}else{c=Math.max(0,Math.ceil(Math.log2(t/5.4)));let f=P(r,1/2**c),h=N(f,f),m=N(h,h),e=N(h,m),d=Ct,p=$([[d[13],e],[d[11],m],[d[9],h]]),g=N(e,p);for(let[M,b]of[[7,e],[5,m],[3,h],[1,o]])_(g,d[M],b);u=N(f,g);let w=$([[d[12],e],[d[10],m],[d[8],h]]);s=N(e,w);for(let[M,b]of[[6,e],[4,m],[2,h],[0,o]])_(s,d[M],b)}let i=E(n,n),l=E(n,n);for(let f=0;f<n*n;f++)i.data[f]=s.data[f]-u.data[f],l.data[f]=s.data[f]+u.data[f];let a=A(j(v(i),v(l)));for(let f=0;f<c;f++)a=N(a,a);return a}function ht(r){let n=Math.sqrt(Q(r)*rt(r)),t=1;for(;2**t<n;)t++;return t+4}function dt(r,n){for(let t=0;t<n;t++)r=N(r,r);return r}function pt(r){let n=r.nrow;for(let t=0;t<n;t++)r.data[t+t*n]=r.data[t+t*n]+1}function Gt(r,n){let t=ht(r),o=2**t,u=C(r.nrow);for(let s=n;s>=1;s--)u=P(N(u,r),1/(s*o)),pt(u);return dt(u,t)}function mt(r,n,t,o){let u=o===1?r:P(r,-1),s=C(r.nrow);for(let c=n;c>=1;c--){let i=(n-c+1)/(c*(2*n-c+1)*t);s=P(N(s,u),i),pt(s)}return s}function Jt(r,n){let t=r.nrow,o=ht(r),u=2**o,s=mt(r,n,u,1),c=mt(r,n,u,-1),i=E(t,t);for(let l=0;l<t;l++)i.data.set(Kt(c,s.data.subarray(l*t,(l+1)*t)),l*t);return dt(i,o)}function Kt(r,n){let t=r.nrow,o=1e-100,u=d=>{let p=new Float64Array(t);for(let g=0;g<t;g++){let w=0;for(let M=0;M<t;M++)w+=r.data[g+M*t]*d[M];p[g]=w}return p},s=(d,p)=>{let g=0;for(let w=0;w<t;w++)g+=d[w]*p[w];return g},c=(d,p,g)=>{let w=new Float64Array(t);for(let M=0;M<t;M++)w[M]=d[M]+p*g[M];return w},i=new Float64Array(t),l=Float64Array.from(n),a=Float64Array.from(n),f=Float64Array.from(n),h=Float64Array.from(n),m=Math.sqrt(s(l,l)),e=s(h,n);if(Math.abs(e)<=o)return i;for(let d=0;d<t;d++){let p=u(a),g=s(h,p);if(Math.abs(g)<=o)return i;let w=e/g;if(Math.abs(w)<=o)return i;let M=c(f,-w,p);p=c(f,1,M);let b=u(p);l=c(l,-w,b);let R=Math.sqrt(s(l,l));if(i=c(i,w,p),Math.abs(R/m)<=1e-30)return i;let y=s(h,l);if(Math.abs(y)<=o)return i;let x=y/e;e=y,f=c(l,x,M),b=c(M,x,a),a=c(f,x,b)}return i}function C(r){let n=E(r,r);for(let t=0;t<r;t++)n.data[t+t*r]=1;return n}function N(r,n){let t=r.nrow,o=r.ncol,u=n.ncol,s=E(t,u);for(let c=0;c<u;c++)for(let i=0;i<t;i++){let l=0;for(let a=0;a<o;a++)l+=r.data[i+a*t]*n.data[a+c*o];s.data[i+c*t]=l}return s}function P(r,n){return{nrow:r.nrow,ncol:r.ncol,data:r.data.map(t=>t*n)}}function _(r,n,t){for(let o=0;o<r.data.length;o++)r.data[o]=r.data[o]+n*t.data[o]}function $(r){let[n,t]=r[0],o=P(t,n);for(let u=1;u<r.length;u++)_(o,r[u][0],r[u][1]);return o}function q(r){return typeof r!="number"&&r.length>0&&Array.isArray(r[0])}function k(r){return typeof r=="number"?[r]:Array.from(r)}function F(r){let n=r[0].length;if(r.some(t=>t.length!==n))throw new TypeError("all rows of the matrix must have the same length");return n}function B(r,n){let t,o;if(q(r)&&q(n))t=r.map(c=>[...c]),o=n.map(c=>[...c]);else if(q(r)){let c=k(n),i=F(r);t=r.map(l=>[...l]),o=i===c.length?c.map(l=>[l]):i===1?[c]:[]}else if(q(n)){let c=k(r);o=n.map(i=>[...i]),t=n.length===c.length?[c]:n.length===1?c.map(i=>[i]):[]}else{let c=k(r),i=k(n);if(c.length===i.length)t=[c],o=i.map(l=>[l]);else if(c.length===1)t=[c],o=[i];else if(i.length===1)t=c.map(l=>[l]),o=[i];else throw new RangeError("non-conformable arguments")}let u=t[0]?.length??0;if(t.length===0||o.length!==u)throw new RangeError("non-conformable arguments");F(t);let s=F(o);return t.map(c=>{let i=new Array(s).fill(0);for(let l=0;l<s;l++){let a=0;for(let f=0;f<u;f++)a+=c[f]*o[f][l];i[l]=a}return i})}function H(r,n,t){let o=q(r),u=q(n);if(o&&u){let l=F(r);if(r.length!==n.length||l!==F(n))throw new RangeError("non-conformable arrays");return r.map((a,f)=>a.map((h,m)=>t(h,n[f][m])))}if(o||u){let l=o?r:n,a=k(o?n:r),f=l.length,h=F(l);if(a.length===0)return l.map(m=>m.map(()=>NaN));if(a.length>f*h)throw new RangeError("dims [product "+f*h+"] do not match the length of object ["+a.length+"]");return l.map((m,e)=>m.map((d,p)=>o?t(d,a[(e+p*f)%a.length]):t(a[(e+p*f)%a.length],d)))}let s=k(r),c=k(n);if(s.length===0||c.length===0)return[];let i=Math.max(s.length,c.length);return Array.from({length:i},(l,a)=>t(s[a%s.length],c[a%c.length]))}function Wt(r,n){return H(r,n,(t,o)=>t+o)}function Yt(r,n){return H(r,n,(t,o)=>t-o)}function Zt(r,n){return H(r,n,(t,o)=>t*o)}function tn(r,n){return H(r,n,(t,o)=>t/o)}var nn={"*":(r,n)=>r*n,"+":(r,n)=>r+n,"-":(r,n)=>r-n,"/":(r,n)=>r/n,"^":(r,n)=>r**n};function rn(r,n,t="*"){let o=typeof t=="function"?t:nn[t];if(o===void 0)throw new RangeError(`invalid fun "${String(t)}"`);let u=Array.from(r),s=Array.from(n);return u.map(c=>s.map(i=>o(c,i)))}function G(r){if(!q(r))return[k(r)];let n=F(r);return Array.from({length:n},(t,o)=>r.map(u=>u[o]))}function en(r,n={}){if(q(r)){let i=F(r);return Array.from({length:Math.min(r.length,i)},(l,a)=>r[a][a])}let t=k(r),o,u;if(t.length===1&&n.nrow===void 0&&n.ncol===void 0){if(u=Math.trunc(t[0]),!(u>=0))throw new RangeError("invalid 'nrow' value (< 0)");o=[1]}else o=t,u=n.nrow??t.length;let s=n.ncol??u,c=Array.from({length:u},()=>new Array(s).fill(0));for(let i=0;i<Math.min(u,s);i++)c[i][i]=o[i%o.length];return u===0?[]:c}function wt(r,n){let t=r.map(s=>q(s)?n?G(s):s.map(c=>[...c]):[k(s)]),o=r.filter(q),u=o.length>0?n?o[0].length:F(o[0]):Math.max(...t.map(s=>s[0].length));return o.forEach((s,c)=>{if((n?s.length:F(s))!==u)throw new RangeError(`number of ${n?"rows":"columns"} of matrices must match (see arg ${c+2})`)}),t.flatMap(s=>s.map(c=>Array.from({length:u},(i,l)=>c[l%c.length])))}function on(r){return typeof r=="object"&&!Array.isArray(r)&&!ArrayBuffer.isView(r)}function Mt(r){let n=r.filter(on);if(n.length!==0){if(n.length<r.length)throw new TypeError("cannot combine data frames with vectors or matrices");return n}}function an(...r){let n=Mt(r);return n===void 0?G(wt(r,!0)):st(n)}function sn(...r){let n=Mt(r);return n===void 0?wt(r,!1):at(n)}function cn(r,n={}){let t=k(r),o=t.length,{nrow:u,ncol:s}=n;u===void 0&&s===void 0?(u=o,s=1):u===void 0?u=Math.ceil(o/s):s===void 0&&(s=Math.ceil(o/u));let c=n.byrow??!1;return Array.from({length:u},(i,l)=>Array.from({length:s},(a,f)=>o===0?NaN:t[(c?l*s+f:l+f*u)%o]))}function ln(r,n){if(!q(r)&&!q(n)){let s=k(r),c=k(n);return s.flatMap(i=>c.map(l=>i*l))}let t=q(r)?r:k(r).map(s=>[s]),o=q(n)?n:k(n).map(s=>[s]),u=[];for(let s of t)for(let c of o)u.push(s.flatMap(i=>c.map(l=>i*l)));return u}function fn(r,n,t={}){let{by:o,lengthOut:u}=t;if(n===void 0&&o===void 0&&u===void 0&&r!==void 0){if(!Number.isFinite(r))throw new RangeError("'from' must be a finite number");return gt(1,r)}if(r!==void 0&&!Number.isFinite(r))throw new RangeError("'from' must be a finite number");if(n!==void 0&&!Number.isFinite(n))throw new RangeError("'to' must be a finite number");if(u===void 0){if(o===void 0)return gt(r??1,n??1);let c=r??1,i=n??1,l=i-c;if(l===0&&i===0)return[i];let a=l/o;if(!Number.isFinite(a)){if(o===0&&l===0)return[c];throw new RangeError("invalid '(to - from)/by'")}if(a<0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(l)/Math.max(Math.abs(i),Math.abs(c))<100*Number.EPSILON)return[c];let f=Math.trunc(a+1e-10);return Array.from({length:f+1},(h,m)=>o>0?Math.min(c+m*o,i):Math.max(c+m*o,i))}if(!Number.isFinite(u)||u<0)throw new RangeError("'length.out' must be a non-negative number");let s=Math.ceil(u);if(s===0)return[];if(o===void 0){let c=n??(r??1)+(s-1),i=r??c-(s-1);if(s<=2)return[i,c].slice(0,s);if(i===c)return new Array(s).fill(i);let l=(c-i)/(s-1);return Array.from({length:s},(a,f)=>f===0?i:f===s-1?c:i+f*l)}if(n===void 0){let c=r??1;return Array.from({length:s},(i,l)=>c+l*o)}if(r===void 0)return Array.from({length:s},(c,i)=>n-(s-1-i)*o);throw new RangeError("too many arguments")}function gt(r,n){let t=Math.trunc(Math.abs(n-r)+1+11920928955078125e-23),o=r<=n?1:-1;return Array.from({length:t},(u,s)=>r+o*s)}var un=22250738585072014e-324;function mn(r){let n=r.length,t=n===0?0:r[0].length,o=r.map(a=>{if(a.length!==t)throw new TypeError("all rows of the matrix must have the same length");return Array.from(a)}),u=Array.from({length:n},(a,f)=>f),s=Math.min(n,t);for(let a=0;a<s;a++){let f=a,h=Math.abs(o[a][a]);for(let d=a+1;d<n;d++){let p=Math.abs(o[d][a]);(p>h||Number.isNaN(h))&&(h=p,f=d)}f!==a&&([o[f],o[a]]=[o[a],o[f]],[u[f],u[a]]=[u[a],u[f]]);let m=o[a][a];if(m===0)continue;let e=Math.abs(m)>=un?1/m:NaN;for(let d=a+1;d<n;d++){let p=Number.isNaN(e)?o[d][a]/m:o[d][a]*e;o[d][a]=p;for(let g=a+1;g<t;g++)o[d][g]=o[d][g]-p*o[a][g]}}let c=Array.from({length:n},(a,f)=>Array.from({length:s},(h,m)=>f===m?1:f>m?o[f][m]:0)),i=Array.from({length:s},(a,f)=>Array.from({length:t},(h,m)=>m>=f?o[f][m]:0)),l=Array.from({length:n},()=>new Array(n).fill(0));return u.forEach((a,f)=>l[a][f]=1),{P:l,L:c,U:i}}function hn(r,n={}){let{rank:t,tol:o}=n;if(t!==void 0){if(!Number.isFinite(t))throw new RangeError("is.finite(rank.) is not TRUE");if(!(Math.trunc(t)>0&&Math.abs(t)<2**31))throw new RangeError("as.integer(rank.) > 0 is not TRUE")}let u=r.map(m=>Array.from(m)),s=ct(u,{center:n.center??!0,scale:n.scale??!1});if(s.scale?.some(m=>m===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let c=S(s.scaled,{nu:0}),i=u.length,l=c.d.map(m=>m/Math.sqrt(Math.max(1,i-1))),a=t===void 0?l.length:Math.min(Math.trunc(t),l.length);o!==void 0&&(a=Math.min(a,l.filter(m=>m>l[0]*o).length));let f=c.v.map(m=>m.slice(0,a)),h={sdev:l,rotation:f,x:B(s.scaled,c.v).map(m=>m.slice(0,a))};return s.center!==void 0&&(h.center=s.center),s.scale!==void 0&&(h.scale=s.scale),h}export{Wt as add,_t as backsolve,an as cbind,Ft as chol,It as chol2inv,St as crossprod,Rt as det,xt as determinant,en as diag,tn as divide,Ot as eigen,Xt as expm,Pt as forwardsolve,Lt as ginv,Ut as kappa,ln as kronecker,mn as lu,B as matMul,cn as matrix,Zt as multiply,U as norm,rn as outer,hn as prcomp,V as qr,vt as qrCoef,Et as qrFitted,qt as qrQ,bt as qrQty,yt as qrQy,D as qrR,kt as qrResid,At as qrSolve,Nt as qrX,sn as rbind,X as rcond,Vt as schur,fn as seq,j as solve,Yt as subtract,S as svd,G as t,jt as tcrossprod};
|
|
1
|
+
/*! rStats.js 0.15.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{F as rt,h as tt,i as nt}from"./chunks/chunk-AEFWSUZ2.js";import"./chunks/chunk-HS7KIFMR.js";import{a as Ft,b as St}from"./chunks/chunk-7GEYDIQC.js";import{B as xt,D as L,E as Rt,F as Y,G as Z,a as q,b as K,c as R,d as T,e as k,g as j,h as mt,i as ht,j as dt,k as pt,l as gt,m as wt,n as Mt,o as bt,p as yt,q as vt,r as $,s as kt,t as U,u as W,v as H,w as Et,x as qt,y as At,z as Nt}from"./chunks/chunk-VAPGDPHM.js";import"./chunks/chunk-WEWXYHPR.js";import"./chunks/chunk-HNLKOICT.js";import"./chunks/chunk-4EOKNOXP.js";function Tt(r,n){let t=T(r),e=n===void 0?t:T(n);if(t.nrow!==e.nrow)throw new TypeError(`non-conformable arguments (${t.nrow} and ${e.nrow} rows)`);let u=t.nrow,a=q(t.ncol,e.ncol);for(let c=0;c<e.ncol;c++)for(let i=0;i<t.ncol;i++){if(n===void 0&&i>c)continue;let f=0;for(let o=0;o<u;o++)f+=t.data[o+i*u]*e.data[o+c*u];a.data[i+c*t.ncol]=f}return n===void 0&&et(a),k(a)}function _t(r,n){let t=T(r),e=n===void 0?t:T(n);if(t.ncol!==e.ncol)throw new TypeError(`non-conformable arguments (${t.ncol} and ${e.ncol} columns)`);let u=q(t.nrow,e.nrow);for(let a=0;a<e.nrow;a++)for(let c=0;c<t.nrow;c++){if(n===void 0&&c>a)continue;let i=0;for(let f=0;f<t.ncol;f++)i+=t.data[c+f*t.nrow]*e.data[a+f*e.nrow];u.data[c+a*t.nrow]=i}return n===void 0&&et(u),k(u)}function et(r){let n=r.nrow;for(let t=0;t<n;t++)for(let e=t+1;e<n;e++)r.data[e+t*n]=r.data[t+e*n]}function Pt(r,n,t={}){return ot(r,n,t.k,t.upperTri??!0,t.transpose??!1)}function jt(r,n,t={}){return ot(r,n,t.k,t.upperTri??!1,t.transpose??!1)}function ot(r,n,t,e,u){let a=R(r),c=T(n),i=t??a.ncol;if(!Number.isInteger(i)||i<1||i>a.nrow||i>a.ncol||i>c.nrow)throw new RangeError(`invalid k (got ${i})`);for(let o=0;o<i;o++)if(a.data[o+o*a.nrow]===0)throw new Error(`singular matrix: diagonal element ${o+1} is zero`);let f=q(i,c.ncol);for(let o=0;o<c.ncol;o++)for(let l=0;l<i;l++)f.data[l+o*i]=c.data[l+o*c.nrow];return It(a,i,f,e,u),K(n)?k(f):Array.from(f.data)}function It(r,n,t,e,u){let a=r.nrow,c=r.data,i=t.nrow,f=t.data;for(let o=0;o<t.ncol;o++){let l=o*i;if(!u&&e){for(let h=n-1;h>=0;h--)if(f[l+h]!==0){f[l+h]/=c[h+h*a];let m=f[l+h];for(let s=0;s<h;s++)f[l+s]-=m*c[s+h*a]}}else if(u)if(e)for(let h=0;h<n;h++){let m=f[l+h];for(let s=0;s<h;s++)m-=c[s+h*a]*f[l+s];f[l+h]=m/c[h+h*a]}else for(let h=n-1;h>=0;h--){let m=f[l+h];for(let s=h+1;s<n;s++)m-=c[s+h*a]*f[l+s];f[l+h]=m/c[h+h*a]}else for(let h=0;h<n;h++)if(f[l+h]!==0){f[l+h]/=c[h+h*a];let m=f[l+h];for(let s=h+1;s<n;s++)f[l+s]-=m*c[s+h*a]}}}function Ot(r){let n=R(r);j(n);let t=n.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let s of n.data)if(!Number.isFinite(s))throw new RangeError("the matrix contains NaN or infinite values");let e={nrow:t,ncol:t,data:n.data.slice()},u=q(t,t),a=new Float64Array(t),c=new Float64Array(t);Y(e,u),Z(e,u,a,c,!0);let i=e.data,f=u.data,o=(s,d)=>s+d*t,l=new Uint8Array(t);for(let s=0;s+1<t;s++)c[s]>0&&c[s+1]<0&&(l[s++]=1);for(let s=0;s<t;s++)for(let d=s+1;d<t;d++)d===s+1&&l[s]||(i[o(d,s)]=0);let h=new Array(t),m=new Array(t);for(let s=0;s<t;s++){if(!l[s]){h[s]=i[o(s,s)],m[s]=0;continue}let d=$t(i[o(s,s)],i[o(s,s+1)],i[o(s+1,s)],i[o(s+1,s+1)]);i[o(s,s)]=d.a,i[o(s,s+1)]=d.b,i[o(s+1,s)]=d.c,i[o(s+1,s+1)]=d.d;let{cs:p,sn:w}=d;for(let g=s+2;g<t;g++){let M=i[o(s,g)],b=i[o(s+1,g)];i[o(s,g)]=p*M+w*b,i[o(s+1,g)]=p*b-w*M}for(let g=0;g<s;g++){let M=i[o(g,s)],b=i[o(g,s+1)];i[o(g,s)]=p*M+w*b,i[o(g,s+1)]=p*b-w*M}for(let g=0;g<t;g++){let M=f[o(g,s)],b=f[o(g,s+1)];f[o(g,s)]=p*M+w*b,f[o(g,s+1)]=p*b-w*M}h[s]=d.rt1r,m[s]=d.rt1i,h[s+1]=d.rt2r,m[s+1]=d.rt2i,s++}return{T:k(e),Q:k(u),valuesRe:h,valuesIm:m}}var N=(r,n)=>n>=0&&!Object.is(n,-0)?Math.abs(r):-Math.abs(r);function $t(r,n,t,e){let u=22250738585072014e-324,a=Number.EPSILON,c=4,i=2**Math.trunc(Math.log(u/a)/Math.log(2)/2),f=1/i,o,l;if(t===0)o=1,l=0;else if(n===0){o=0,l=1;let m=e;e=r,r=m,n=-t,t=0}else if(r-e===0&&N(1,n)!==N(1,t))o=1,l=0;else{let m=r-e,s=.5*m,d=Math.max(Math.abs(n),Math.abs(t)),p=Math.min(Math.abs(n),Math.abs(t))*N(1,n)*N(1,t),w=Math.max(Math.abs(s),d),g=s/w*s+d/w*p;if(g>=c*a){g=s+N(Math.sqrt(w)*Math.sqrt(g),s),r=e+g,e=e-d/g*p;let M=Math.hypot(t,g);o=g/M,l=t/M,n=n-t,t=0}else{let M=0,b=n+t;for(;M++,w=Math.max(Math.abs(m),Math.abs(b)),!(!(w>=f&&(b*=i,m*=i,M<=20))&&!(w<=i&&(b*=f,m*=f,M<=20))););s=.5*m;let x=Math.hypot(b,m);o=Math.sqrt(.5*(1+Math.abs(b)/x)),l=-(s/(x*o))*N(1,b);let F=r*o+n*l,S=-r*l+n*o,Q=t*o+e*l,X=-t*l+e*o;if(r=F*o+Q*l,n=S*o+X*l,t=-F*l+Q*o,e=-S*l+X*o,m=.5*(r+e),r=m,e=m,t!==0)if(n!==0){if(N(1,n)===N(1,t)){let B=Math.sqrt(Math.abs(n)),C=Math.sqrt(Math.abs(t));s=N(B*C,t),x=1/Math.sqrt(Math.abs(n+t)),r=m+s,e=m-s,n=n-t,t=0;let G=B*x,J=C*x;m=o*G-l*J,l=o*J+l*G,o=m}}else n=-t,t=0,m=o,o=-l,l=m}}let h=t===0?0:Math.sqrt(Math.abs(n))*Math.sqrt(Math.abs(t));return{a:r,b:n,c:t,d:e,cs:o,sn:l,rt1r:r,rt1i:h,rt2r:e,rt2i:-h}}var Ut=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function Ht(r,n={}){let t=n.method??"Higham08.b";if(!Ut.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let e=n.order??8;if(!Number.isInteger(e)||e<1)throw new RangeError("order must be a positive integer");let u=R(r);j(u);let a=u.nrow;if(a===0)return[];if(a===1)return[[Math.exp(u.data[0])]];if(t==="Taylor"||t==="TaylorO")return k(Dt(u,e));if(t==="Pade"||t==="PadeO")return k(Qt(u,e));let c,i;t==="Higham08.b"&&(c=L("P",u),i=L("S",u));let f=zt(u),o=f.data,l=(h,m)=>h+m*a;if(i){let h=i.scale;for(let m=0;m<a;m++)for(let s=0;s<a;s++)o[l(s,m)]=o[l(s,m)]*h[s]/h[m]}if(c){let h=c.scale,m=s=>{let d=h[s];if(d!==s){for(let p=0;p<a;p++)[o[l(p,s)],o[l(p,d)]]=[o[l(p,d)],o[l(p,s)]];for(let p=0;p<a;p++)[o[l(s,p)],o[l(d,p)]]=[o[l(d,p)],o[l(s,p)]]}};for(let s=c.ilo-1;s>=0;s--)m(s);for(let s=c.ihi+1;s<a;s++)m(s)}return k(f)}var Lt={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},Vt=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function zt(r){let n=r.nrow,t=U(r),e=V(n),u,a,c=0;if(t<=2.1){let l=t<=.015?3:t<=.25?5:t<=.95?7:9,h=Lt[l],m=E(r,r),s=I([[h[1],e]]),d=I([[h[0],e]]),p=m;for(let w=1;w<=(l-1)/2;w++)w>1&&(p=E(p,m)),_(s,h[2*w+1],p),_(d,h[2*w],p);u=E(r,s),a=d}else{c=Math.max(0,Math.ceil(Math.log2(t/5.4)));let l=P(r,1/2**c),h=E(l,l),m=E(h,h),s=E(h,m),d=Vt,p=I([[d[13],s],[d[11],m],[d[9],h]]),w=E(s,p);for(let[M,b]of[[7,s],[5,m],[3,h],[1,e]])_(w,d[M],b);u=E(l,w);let g=I([[d[12],s],[d[10],m],[d[8],h]]);a=E(s,g);for(let[M,b]of[[6,s],[4,m],[2,h],[0,e]])_(a,d[M],b)}let i=q(n,n),f=q(n,n);for(let l=0;l<n*n;l++)i.data[l]=a.data[l]-u.data[l],f.data[l]=a.data[l]+u.data[l];let o=R(H(k(i),k(f)));for(let l=0;l<c;l++)o=E(o,o);return o}function st(r){let n=Math.sqrt(U(r)*W(r)),t=1;for(;2**t<n;)t++;return t+4}function ct(r,n){for(let t=0;t<n;t++)r=E(r,r);return r}function it(r){let n=r.nrow;for(let t=0;t<n;t++)r.data[t+t*n]=r.data[t+t*n]+1}function Dt(r,n){let t=st(r),e=2**t,u=V(r.nrow);for(let a=n;a>=1;a--)u=P(E(u,r),1/(a*e)),it(u);return ct(u,t)}function at(r,n,t,e){let u=e===1?r:P(r,-1),a=V(r.nrow);for(let c=n;c>=1;c--){let i=(n-c+1)/(c*(2*n-c+1)*t);a=P(E(a,u),i),it(a)}return a}function Qt(r,n){let t=r.nrow,e=st(r),u=2**e,a=at(r,n,u,1),c=at(r,n,u,-1),i=q(t,t);for(let f=0;f<t;f++)i.data.set(Xt(c,a.data.subarray(f*t,(f+1)*t)),f*t);return ct(i,e)}function Xt(r,n){let t=r.nrow,e=1e-100,u=d=>{let p=new Float64Array(t);for(let w=0;w<t;w++){let g=0;for(let M=0;M<t;M++)g+=r.data[w+M*t]*d[M];p[w]=g}return p},a=(d,p)=>{let w=0;for(let g=0;g<t;g++)w+=d[g]*p[g];return w},c=(d,p,w)=>{let g=new Float64Array(t);for(let M=0;M<t;M++)g[M]=d[M]+p*w[M];return g},i=new Float64Array(t),f=Float64Array.from(n),o=Float64Array.from(n),l=Float64Array.from(n),h=Float64Array.from(n),m=Math.sqrt(a(f,f)),s=a(h,n);if(Math.abs(s)<=e)return i;for(let d=0;d<t;d++){let p=u(o),w=a(h,p);if(Math.abs(w)<=e)return i;let g=s/w;if(Math.abs(g)<=e)return i;let M=c(l,-g,p);p=c(l,1,M);let b=u(p);f=c(f,-g,b);let x=Math.sqrt(a(f,f));if(i=c(i,g,p),Math.abs(x/m)<=1e-30)return i;let F=a(h,f);if(Math.abs(F)<=e)return i;let S=F/s;s=F,l=c(f,S,M),b=c(M,S,o),o=c(l,S,b)}return i}function V(r){let n=q(r,r);for(let t=0;t<r;t++)n.data[t+t*r]=1;return n}function E(r,n){let t=r.nrow,e=r.ncol,u=n.ncol,a=q(t,u);for(let c=0;c<u;c++)for(let i=0;i<t;i++){let f=0;for(let o=0;o<e;o++)f+=r.data[i+o*t]*n.data[o+c*e];a.data[i+c*t]=f}return a}function P(r,n){return{nrow:r.nrow,ncol:r.ncol,data:r.data.map(t=>t*n)}}function _(r,n,t){for(let e=0;e<r.data.length;e++)r.data[e]=r.data[e]+n*t.data[e]}function I(r){let[n,t]=r[0],e=P(t,n);for(let u=1;u<r.length;u++)_(e,r[u][0],r[u][1]);return e}function v(r){return typeof r!="number"&&r.length>0&&Array.isArray(r[0])}function y(r){return typeof r=="number"?[r]:Array.from(r)}function A(r){let n=r[0].length;if(r.some(t=>t.length!==n))throw new TypeError("all rows of the matrix must have the same length");return n}function z(r,n){let t,e;if(v(r)&&v(n))t=r.map(c=>[...c]),e=n.map(c=>[...c]);else if(v(r)){let c=y(n),i=A(r);t=r.map(f=>[...f]),e=i===c.length?c.map(f=>[f]):i===1?[c]:[]}else if(v(n)){let c=y(r);e=n.map(i=>[...i]),t=n.length===c.length?[c]:n.length===1?c.map(i=>[i]):[]}else{let c=y(r),i=y(n);if(c.length===i.length)t=[c],e=i.map(f=>[f]);else if(c.length===1)t=[c],e=[i];else if(i.length===1)t=c.map(f=>[f]),e=[i];else throw new RangeError("non-conformable arguments")}let u=t[0]?.length??0;if(t.length===0||e.length!==u)throw new RangeError("non-conformable arguments");A(t);let a=A(e);return t.map(c=>{let i=new Array(a).fill(0);for(let f=0;f<a;f++){let o=0;for(let l=0;l<u;l++)o+=c[l]*e[l][f];i[f]=o}return i})}function O(r,n,t){let e=v(r),u=v(n);if(e&&u){let f=A(r);if(r.length!==n.length||f!==A(n))throw new RangeError("non-conformable arrays");return r.map((o,l)=>o.map((h,m)=>t(h,n[l][m])))}if(e||u){let f=e?r:n,o=y(e?n:r),l=f.length,h=A(f);if(o.length===0)return f.map(m=>m.map(()=>NaN));if(o.length>l*h)throw new RangeError("dims [product "+l*h+"] do not match the length of object ["+o.length+"]");return f.map((m,s)=>m.map((d,p)=>e?t(d,o[(s+p*l)%o.length]):t(o[(s+p*l)%o.length],d)))}let a=y(r),c=y(n);if(a.length===0||c.length===0)return[];let i=Math.max(a.length,c.length);return Array.from({length:i},(f,o)=>t(a[o%a.length],c[o%c.length]))}function Bt(r,n){return O(r,n,(t,e)=>t+e)}function Ct(r,n){return O(r,n,(t,e)=>t-e)}function Gt(r,n){return O(r,n,(t,e)=>t*e)}function Jt(r,n){return O(r,n,(t,e)=>t/e)}var Kt={"*":(r,n)=>r*n,"+":(r,n)=>r+n,"-":(r,n)=>r-n,"/":(r,n)=>r/n,"^":(r,n)=>r**n};function Wt(r,n,t="*"){let e=typeof t=="function"?t:Kt[t];if(e===void 0)throw new RangeError(`invalid fun "${String(t)}"`);let u=Array.from(r),a=Array.from(n);return u.map(c=>a.map(i=>e(c,i)))}function D(r){if(!v(r))return[y(r)];let n=A(r);return Array.from({length:n},(t,e)=>r.map(u=>u[e]))}function Yt(r,n={}){if(v(r)){let i=A(r);return Array.from({length:Math.min(r.length,i)},(f,o)=>r[o][o])}let t=y(r),e,u;if(t.length===1&&n.nrow===void 0&&n.ncol===void 0){if(u=Math.trunc(t[0]),!(u>=0))throw new RangeError("invalid 'nrow' value (< 0)");e=[1]}else e=t,u=n.nrow??t.length;let a=n.ncol??u,c=Array.from({length:u},()=>new Array(a).fill(0));for(let i=0;i<Math.min(u,a);i++)c[i][i]=e[i%e.length];return u===0?[]:c}function ft(r,n){let t=r.map(a=>v(a)?n?D(a):a.map(c=>[...c]):[y(a)]),e=r.filter(v),u=e.length>0?n?e[0].length:A(e[0]):Math.max(...t.map(a=>a[0].length));return e.forEach((a,c)=>{if((n?a.length:A(a))!==u)throw new RangeError(`number of ${n?"rows":"columns"} of matrices must match (see arg ${c+2})`)}),t.flatMap(a=>a.map(c=>Array.from({length:u},(i,f)=>c[f%c.length])))}function Zt(r){return typeof r=="object"&&!Array.isArray(r)&&!ArrayBuffer.isView(r)}function ut(r){let n=r.filter(Zt);if(n.length!==0){if(n.length<r.length)throw new TypeError("cannot combine data frames with vectors or matrices");return n}}function tn(...r){let n=ut(r);return n===void 0?D(ft(r,!0)):nt(n)}function nn(...r){let n=ut(r);return n===void 0?ft(r,!1):tt(n)}function rn(r,n={}){let t=y(r),e=t.length,{nrow:u,ncol:a}=n;u===void 0&&a===void 0?(u=e,a=1):u===void 0?u=Math.ceil(e/a):a===void 0&&(a=Math.ceil(e/u));let c=n.byrow??!1;return Array.from({length:u},(i,f)=>Array.from({length:a},(o,l)=>e===0?NaN:t[(c?f*a+l:f+l*u)%e]))}function en(r,n){if(!v(r)&&!v(n)){let a=y(r),c=y(n);return a.flatMap(i=>c.map(f=>i*f))}let t=v(r)?r:y(r).map(a=>[a]),e=v(n)?n:y(n).map(a=>[a]),u=[];for(let a of t)for(let c of e)u.push(a.flatMap(i=>c.map(f=>i*f)));return u}function on(r,n,t={}){let{by:e,lengthOut:u}=t;if(n===void 0&&e===void 0&&u===void 0&&r!==void 0){if(!Number.isFinite(r))throw new RangeError("'from' must be a finite number");return lt(1,r)}if(r!==void 0&&!Number.isFinite(r))throw new RangeError("'from' must be a finite number");if(n!==void 0&&!Number.isFinite(n))throw new RangeError("'to' must be a finite number");if(u===void 0){if(e===void 0)return lt(r??1,n??1);let c=r??1,i=n??1,f=i-c;if(f===0&&i===0)return[i];let o=f/e;if(!Number.isFinite(o)){if(e===0&&f===0)return[c];throw new RangeError("invalid '(to - from)/by'")}if(o<0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(f)/Math.max(Math.abs(i),Math.abs(c))<100*Number.EPSILON)return[c];let l=Math.trunc(o+1e-10);return Array.from({length:l+1},(h,m)=>e>0?Math.min(c+m*e,i):Math.max(c+m*e,i))}if(!Number.isFinite(u)||u<0)throw new RangeError("'length.out' must be a non-negative number");let a=Math.ceil(u);if(a===0)return[];if(e===void 0){let c=n??(r??1)+(a-1),i=r??c-(a-1);if(a<=2)return[i,c].slice(0,a);if(i===c)return new Array(a).fill(i);let f=(c-i)/(a-1);return Array.from({length:a},(o,l)=>l===0?i:l===a-1?c:i+l*f)}if(n===void 0){let c=r??1;return Array.from({length:a},(i,f)=>c+f*e)}if(r===void 0)return Array.from({length:a},(c,i)=>n-(a-1-i)*e);throw new RangeError("too many arguments")}function lt(r,n){let t=Math.trunc(Math.abs(n-r)+1+11920928955078125e-23),e=r<=n?1:-1;return Array.from({length:t},(u,a)=>r+e*a)}var an=22250738585072014e-324;function sn(r){let n=r.length,t=n===0?0:r[0].length,e=r.map(o=>{if(o.length!==t)throw new TypeError("all rows of the matrix must have the same length");return Array.from(o)}),u=Array.from({length:n},(o,l)=>l),a=Math.min(n,t);for(let o=0;o<a;o++){let l=o,h=Math.abs(e[o][o]);for(let d=o+1;d<n;d++){let p=Math.abs(e[d][o]);(p>h||Number.isNaN(h))&&(h=p,l=d)}l!==o&&([e[l],e[o]]=[e[o],e[l]],[u[l],u[o]]=[u[o],u[l]]);let m=e[o][o];if(m===0)continue;let s=Math.abs(m)>=an?1/m:NaN;for(let d=o+1;d<n;d++){let p=Number.isNaN(s)?e[d][o]/m:e[d][o]*s;e[d][o]=p;for(let w=o+1;w<t;w++)e[d][w]=e[d][w]-p*e[o][w]}}let c=Array.from({length:n},(o,l)=>Array.from({length:a},(h,m)=>l===m?1:l>m?e[l][m]:0)),i=Array.from({length:a},(o,l)=>Array.from({length:t},(h,m)=>m>=l?e[l][m]:0)),f=Array.from({length:n},()=>new Array(n).fill(0));return u.forEach((o,l)=>f[o][l]=1),{P:f,L:c,U:i}}function cn(r,n={}){let{rank:t,tol:e}=n;if(t!==void 0){if(!Number.isFinite(t))throw new RangeError("is.finite(rank.) is not TRUE");if(!(Math.trunc(t)>0&&Math.abs(t)<2**31))throw new RangeError("as.integer(rank.) > 0 is not TRUE")}let u=r.map(m=>Array.from(m)),a=rt(u,{center:n.center??!0,scale:n.scale??!1});if(a.scale?.some(m=>m===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let c=$(a.scaled,{nu:0}),i=u.length,f=c.d.map(m=>m/Math.sqrt(Math.max(1,i-1))),o=t===void 0?f.length:Math.min(Math.trunc(t),f.length);e!==void 0&&(o=Math.min(o,f.filter(m=>m>f[0]*e).length));let l=c.v.map(m=>m.slice(0,o)),h={sdev:f,rotation:l,x:z(a.scaled,c.v).map(m=>m.slice(0,o))};return a.center!==void 0&&(h.center=a.center),a.scale!==void 0&&(h.scale=a.scale),h}export{Bt as add,Pt as backsolve,tn as cbind,Nt as chol,xt as chol2inv,Tt as crossprod,qt as det,Et as determinant,Yt as diag,Jt as divide,Rt as eigen,Ht as expm,jt as forwardsolve,St as ginv,Ft as kappa,en as kronecker,sn as lu,z as matMul,rn as matrix,Gt as multiply,kt as norm,Wt as outer,cn as prcomp,mt as qr,pt as qrCoef,wt as qrFitted,Mt as qrQ,ht as qrQty,dt as qrQy,bt as qrR,gt as qrResid,vt as qrSolve,yt as qrX,nn as rbind,At as rcond,Ot as schur,on as seq,H as solve,Ct as subtract,$ as svd,D as t,_t as tcrossprod};
|
package/dist/mixed.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
/*! rStats.js 0.
|
|
2
|
-
import{a as lt}from"./chunks/chunk-RTWLZPPG.js";import{h as bt,u as At,w as xt}from"./chunks/chunk-VWGJYRE6.js";import{a as gt,c as dt,d as ct,f as wt,g as at}from"./chunks/chunk-BSOUJPDF.js";import{b as vt}from"./chunks/chunk-JGFUZKLJ.js";import{b as st}from"./chunks/chunk-3AKA4KEL.js";var Mt=1,kt=new WeakMap;function Ct(r){let n=kt.get(r);if(n!==void 0)return n;let s=r.y.length,l=r.xColNames.length,o=r.q,i=r.terms.length,t=r.terms.reduce((f,M)=>f+M.nLevels,0),e=new Int32Array(t+1),m=new Int32Array(t),b=[],p=[],j=0;r.terms.forEach((f,M)=>{let P=f.columns.length;b.push(j),p.push(P);for(let N=0;N<f.nLevels;N++,j++)e[j]=f.zOffset+N*P,m[j]=M}),e[t]=o;let _=r.terms.map((f,M)=>{let P=p[M],N=new Int32Array(P*P).fill(-1);for(let{row:Y,col:Z,theta:J}of r.lambda)Z>=f.zOffset&&Z<f.zOffset+P&&(N[Y-f.zOffset+(Z-f.zOffset)*P]=J);return N}),k=r.terms.map(f=>r.groups[f.group].codes),u=new Int32Array(s+1);for(let f=0;f<s;f++){let M=0;for(let P of r.terms)for(let N of P.matrix[f])N!==0&&M++;u[f+1]=u[f]+M}let I=u,C=I[s],$=new Int32Array(C),y=new Float64Array(C);{let f=0;for(let M=0;M<s;M++)r.terms.forEach((P,N)=>{let Y=P.zOffset+k[N][M]*p[N];P.matrix[M].forEach((Z,J)=>{Z!==0&&($[f]=Y+J,y[f++]=Z)})})}let A=new Map,x=[],z=[],D=(f,M)=>{let P=M*t+f,N=A.get(P);return N===void 0&&(N=x.length,A.set(P,N),x.push(f),z.push(M)),N};for(let f=0;f<t;f++)D(f,f);let S=new Int32Array(i),G=i*(i+1)/2,H=new Int32Array(s*G);for(let f=0;f<s;f++){for(let M=0;M<i;M++)S[M]=b[M]+k[M][f];for(let M=0;M<i;M++)for(let P=0;P<=M;P++)H[f*G+M*(M+1)/2+P]=D(S[M],S[P])}let U=x.length,g=Array.from({length:U},(f,M)=>M).sort((f,M)=>z[f]-z[M]||x[f]-x[M]),T=new Int32Array(U);g.forEach((f,M)=>T[f]=M);let E=new Int32Array(t+1),V=new Int32Array(U),L=new Int32Array(U+1);g.forEach((f,M)=>{V[M]=x[f],E[z[f]+1]++;let P=p[m[x[f]]]*p[m[z[f]]];L[M+1]=L[M]+P});for(let f=0;f<t;f++)E[f+1]+=E[f];let B=new Float64Array(L[U]),K=new Float64Array(o*l),X=new Float64Array(o),c=new Float64Array(l);for(let f=0;f<s;f++){let M=r.x[f],P=r.y[f];for(let N=0;N<i;N++){let Y=r.terms[N].matrix[f],Z=p[N];for(let J=0;J<=N;J++){let Nt=r.terms[J].matrix[f],jt=p[J],Lt=T[H[f*G+N*(N+1)/2+J]],qt=L[Lt];for(let ot=0;ot<jt;ot++){let pt=Nt[ot];if(pt!==0)for(let rt=0;rt<Z;rt++)B[qt+rt+ot*Z]+=Y[rt]*pt}}}for(let N=I[f];N<I[f+1];N++){let Y=$[N],Z=y[N];for(let J=0;J<l;J++)K[Y+J*o]+=Z*M[J];X[Y]+=Z*P}for(let N=0;N<l;N++)c[N]+=M[N]*P}let a=new Array(t),O=Array.from({length:t},()=>[]),h=new Int32Array(t).fill(-1);for(let f=0;f<t;f++){let M=[],P=Y=>{Y!==f&&h[Y]!==f&&(h[Y]=f,M.push(Y))};for(let Y=E[f]+1;Y<E[f+1];Y++)P(V[Y]);for(let Y of O[f])for(let Z of a[Y])P(Z);let N=Int32Array.from(M).sort();a[f]=N,N.length>0&&O[N[0]].push(f)}let v=new Int32Array(U);{let f=new Int32Array(t);for(let M=0;M<t;M++){let P=0;for(let N of a[M])f[N]=P,P+=e[N+1]-e[N];for(let N=E[M]+1;N<E[M+1];N++)v[N]=f[V[N]]}}let F=new Int32Array(o+1);for(let f=0;f<t;f++){let M=0;for(let P of a[f])M+=e[P+1]-e[P];for(let P=e[f];P<e[f+1];P++)F[P+1]=F[P]+(e[f+1]-P)+M}let R=new Int32Array(F[o]),w=new Int32Array(o+1);for(let f=0;f<t;f++)for(let M=e[f];M<e[f+1];M++){let P=F[M];for(let N=M;N<e[f+1];N++)R[P++]=N;for(let N of a[f])for(let Y=e[N];Y<e[N+1];Y++)R[P++]=Y}for(let f=0;f<o;f++)for(let M=F[f]+1;M<F[f+1];M++)w[R[M]+1]++;for(let f=0;f<o;f++)w[f+1]+=w[f];let d=w.slice(),q=w.slice(0,o),W=new Int32Array(d[o]),tt=new Int32Array(d[o]);for(let f=0;f<o;f++)for(let M=F[f]+1;M<F[f+1];M++){let P=q[R[M]]++;W[P]=f,tt[P]=M}let et={n:s,p:l,q:o,nGroups:t,groupStart:e,groupTerm:m,termNc:p,termTheta:_,blockStart:E,blockRow:V,blockValueStart:L,zz:B,zx:K,zy:X,xy:c,rowStart:I,rowCols:$,rowValues:y,colStart:F,colRows:R,rowListStart:d,rowListCol:W,rowListPos:tt,blockPatternOffset:v};return kt.set(r,et),et}function _t(r,n){return r.termTheta.map(s=>Float64Array.from(s,l=>l<0?0:n[l]))}function ft(r,n,s){let l=new Float64Array(r.q);for(let o=0;o<r.nGroups;o++){let i=r.groupTerm[o],t=r.termNc[i],e=n[i],m=r.groupStart[o];for(let b=0;b<t;b++){let p=s[m+b];if(p!==0)for(let j=b;j<t;j++)l[m+j]+=e[j+b*t]*p}}return l}function nt(r,n,s,l=1){let o=r.q,i=new Float64Array(o*l);for(let t=0;t<r.nGroups;t++){let e=r.groupTerm[t],m=r.termNc[e],b=n[e],p=r.groupStart[t];for(let j=0;j<l;j++){let _=p+j*o;for(let k=0;k<m;k++){let u=0;for(let I=k;I<m;I++)u+=b[I+k*m]*s[_+I];i[_+k]=u}}}return i}function Vt(r,n){let s=new Float64Array(r.zz.length),l=new Float64Array(64);for(let o=0;o<r.nGroups;o++){let i=r.groupTerm[o],t=r.termNc[i],e=n[i];for(let m=r.blockStart[o];m<r.blockStart[o+1];m++){let b=r.groupTerm[r.blockRow[m]],p=r.termNc[b],j=n[b],_=r.blockValueStart[m];if(p===1&&t===1)s[_]=j[0]*r.zz[_]*e[0];else{let k=l.length>=p*t?l:new Float64Array(p*t);for(let u=0;u<t;u++)for(let I=0;I<p;I++){let C=0;for(let $=u;$<t;$++)C+=r.zz[_+I+$*p]*e[$+u*t];k[I+u*p]=C}for(let u=0;u<t;u++)for(let I=0;I<p;I++){let C=0;for(let $=I;$<p;$++)C+=j[$+I*p]*k[$+u*p];s[_+I+u*p]=C}}if(m===r.blockStart[o])for(let k=0;k<t;k++)s[_+k+k*t]+=1}}return s}function Pt(r,n){let{q:s,colStart:l,colRows:o,rowListStart:i,rowListCol:t,rowListPos:e}=r,m=new Float64Array(o.length),b=new Float64Array(s);for(let p=0;p<r.nGroups;p++){let j=r.termNc[r.groupTerm[p]],_=r.groupStart[p];for(let k=0;k<j;k++){let u=_+k;for(let y=r.blockStart[p];y<r.blockStart[p+1];y++){let A=r.blockRow[y],x=r.termNc[r.groupTerm[A]],z=r.blockValueStart[y]+k*x,D=r.groupStart[A];for(let S=A===p?k:0;S<x;S++)b[D+S]=n[z+S]}for(let y=i[u];y<i[u+1];y++){let A=t[y],x=e[y],z=m[x];if(z===0)continue;let D=l[A+1];for(let S=x;S<D;S++)b[o[S]]-=m[S]*z}let I=b[u];if(!(I>0))throw new Error("the penalised least-squares matrix is not positive definite");let C=Math.sqrt(I),$=l[u];m[$]=C,b[u]=0;for(let y=$+1;y<l[u+1];y++){let A=o[y];m[y]=b[A]/C,b[A]=0}}}return m}function it(r,n,s,l=0){let{q:o,colStart:i,colRows:t}=r;for(let e=0;e<o;e++){let m=i[e],b=s[l+e]/=n[m];if(b!==0)for(let p=m+1;p<i[e+1];p++)s[l+t[p]]-=n[p]*b}}function mt(r,n,s,l=0){let{q:o,colStart:i,colRows:t}=r;for(let e=o-1;e>=0;e--){let m=i[e],b=s[l+e];for(let p=m+1;p<i[e+1];p++)b-=n[p]*s[l+t[p]];s[l+e]=b/n[m]}}function Dt(r,n,s){for(let l=0;l<n;l++){let o=l*n,i=s[l];for(let t=0;t<l;t++)i-=r[t+o]*s[t];s[l]=i/r[l+o]}}function Gt(r,n,s){for(let l=n-1;l>=0;l--){let o=s[l]/=r[l+l*n],i=l*n;for(let t=0;t<l;t++)s[t]-=r[t+i]*o}}function $t(r,n){let s=r.slice();for(let l=0;l<n;l++)for(let o=l+1;o<n;o++)s[o+l*n]=0;xt({nrow:n,ncol:n,data:s});for(let l=0;l<n;l++)for(let o=l+1;o<n;o++)s[o+l*n]=s[l+o*n];return s}function ut(r,n,s,l={}){if(n.length!==r.theta.length)throw new RangeError(`theta must have length ${r.theta.length}, not ${n.length}`);if(n.some(a=>!Number.isFinite(a)))throw new RangeError("theta must be finite");let o=Ct(r),{n:i,p:t,q:e}=o,m=_t(o,n),b=Pt(o,Vt(o,m)),p=a=>{let O=new Float64Array(i);for(let h=0;h<i;h++){let v=0;for(let F=o.rowStart[h];F<o.rowStart[h+1];F++)v+=o.rowValues[F]*a[o.rowCols[F]];O[h]=v}return O},j=a=>{let O=new Float64Array(e);for(let h=0;h<i;h++){let v=a[h];for(let F=o.rowStart[h];F<o.rowStart[h+1];F++)O[o.rowCols[F]]+=o.rowValues[F]*v}return O},_=a=>{it(o,b,a),mt(o,b,a)},k=nt(o,m,o.zx,t);for(let a=0;a<t;a++)it(o,b,k,a*e);let u=[],I=[];for(let a=0;a<t;a++){let O=k.slice(a*e,(a+1)*e);mt(o,b,O);let h=Float64Array.from(r.x,R=>R[a]),v=()=>{let R=p(ft(o,m,O));return h.map((w,d)=>w-R[d])},F=v();for(let R=0;R<Mt;R++){let w=nt(o,m,j(F));for(let d=0;d<e;d++)w[d]-=O[d];_(w);for(let d=0;d<e;d++)O[d]+=w[d];F=v()}u.push(F),I.push(O)}let C=new Float64Array(t*t);for(let a=0;a<t;a++)for(let O=a;O<t;O++){let h=0,v=u[a],F=u[O];for(let d=0;d<i;d++)h+=v[d]*F[d];let R=I[a],w=I[O];for(let d=0;d<e;d++)h+=R[d]*w[d];C[a+O*t]=h,C[O+a*t]=h}let $={nrow:t,ncol:t,data:C};if(t>0&&At($)>0)throw new Error("the fixed-effects matrix of the penalised least squares is not positive definite");let y=(a,O)=>{it(o,b,a);for(let h=0;h<t;h++){let v=O[h];for(let F=0;F<e;F++)v-=k[F+h*e]*a[F];O[h]=v}Dt(C,t,O),Gt(C,t,O);for(let h=0;h<t;h++){let v=O[h];for(let F=0;F<e;F++)a[F]-=k[F+h*e]*v}mt(o,b,a)},A=(a,O)=>{let h=p(O),v=new Float64Array(i);for(let F=0;F<i;F++){let R=r.x[F],w=h[F];for(let d=0;d<t;d++)w+=R[d]*a[d];v[F]=r.y[F]-w}return v},x=nt(o,m,o.zy),z=Float64Array.from(o.xy);y(x,z);let D=ft(o,m,x),S=A(z,D);for(let a=0;a<Mt;a++){let O=nt(o,m,j(S));for(let v=0;v<e;v++)O[v]-=x[v];let h=new Float64Array(t);for(let v=0;v<i;v++){let F=r.x[v],R=S[v];for(let w=0;w<t;w++)h[w]+=F[w]*R}y(O,h);for(let v=0;v<e;v++)x[v]+=O[v];for(let v=0;v<t;v++)z[v]+=h[v];D=ft(o,m,x),S=A(z,D)}let G=Array.from(z),H=Array.from(x),U=Array.from(D),g=0;for(let a of S)g+=a*a;let T=0;for(let a of H)T+=a*a;let E=g+T,V=0;for(let a=0;a<e;a++)V+=Math.log(b[o.colStart[a]]);V*=2;let L=0;for(let a=0;a<t;a++)L+=Math.log(C[a+a*t]);L*=2;let B=s?i-t:i,X={deviance:(s?V+L:V)+B*(1+Math.log(2*Math.PI*E/B)),beta:G,u:H,b:U,rss:g,ussq:T,prss:E,logDetL:V,logDetRX:L},c=l.gradient===!0||l.rxInverse===!0?$t(C,t):void 0;return l.rxInverse===!0&&(X.rxInverse=Array.from({length:t},(a,O)=>Array.from({length:t},(h,v)=>c[O+v*t]))),l.gradient===!0&&(X.gradient=Bt(r,o,m,b,n,s,{res:S,uv:x,prss:E,ws:I,xResiduals:u,sInverse:c})),X}function Ut(r,n){let{q:s,colStart:l,colRows:o}=r,i=new Float64Array(n.length),t=new Int32Array(s).fill(-1),e=new Float64Array(s),m=new Float64Array(s);for(let b=s-1;b>=0;b--){let p=l[b],j=l[b+1];for(let u=p+1;u<j;u++){let I=o[u];t[I]=b,e[I]=n[u],m[I]=0}for(let u=p+1;u<j;u++){let I=o[u],C=n[u],$=l[I],y=i[$]*C;for(let A=$+1;A<l[I+1];A++){let x=o[A];if(t[x]!==b)continue;let z=i[A];m[x]+=z*C,y+=z*e[x]}m[I]+=y}let _=n[p],k=1/_;for(let u=p+1;u<j;u++){let I=-m[o[u]]/_;i[u]=I,k-=n[u]*I}i[p]=k/_}return i}function Bt(r,n,s,l,o,i,{res:t,uv:e,prss:m,ws:b,xResiduals:p,sInverse:j}){let{n:_,p:k,q:u,colStart:I,zz:C}=n,$=Ut(n,l),y=n.termNc.map(g=>new Float64Array(g*g)),A=new Float64Array(16),x=new Float64Array(16);for(let g=0;g<n.nGroups;g++){let T=n.groupTerm[g],E=n.termNc[T],V=s[T],L=n.groupStart[g];for(let B=n.blockStart[g];B<n.blockStart[g+1];B++){let K=n.blockRow[B],X=n.groupTerm[K],c=n.termNc[X],a=s[X],O=n.blockValueStart[B];if(E===1&&c===1){let R=K===g?I[L]:I[L]+1+n.blockPatternOffset[B],w=$[R]*C[O];y[T][0]+=w*a[0],K!==g&&(y[X][0]+=w*V[0]);continue}let h=c*E;A.length<h&&(A=new Float64Array(h),x=new Float64Array(Math.max(h,c*c,E*E)));for(let R=0;R<E;R++){let w=I[L+R];if(K===g)for(let d=R;d<c;d++){let q=$[w+d-R];A[d+R*c]=q,A[R+d*c]=q}else{let d=w+(E-R)+n.blockPatternOffset[B];for(let q=0;q<c;q++)A[q+R*c]=$[d+q]}}for(let R=0;R<E;R++)for(let w=0;w<c;w++){let d=0;for(let q=w;q<c;q++)d+=a[q+w*c]*C[O+q+R*c];x[w+R*c]=d}let v=y[T];for(let R=0;R<E;R++)for(let w=0;w<E;w++){let d=0;for(let q=0;q<c;q++)d+=A[q+R*c]*x[q+w*c];v[R+w*E]+=d}if(K===g)continue;for(let R=0;R<c;R++)for(let w=0;w<E;w++){let d=0;for(let q=w;q<E;q++)d+=V[q+w*E]*C[O+R+q*c];x[w+R*E]=d}let F=y[X];for(let R=0;R<c;R++)for(let w=0;w<c;w++){let d=0;for(let q=0;q<E;q++)d+=A[R+q*c]*x[q+w*E];F[R+w*c]+=d}}}let z=new Array(o.length).fill(0);n.termTheta.forEach((g,T)=>{let E=n.termNc[T];for(let V=0;V<E;V++)for(let L=V;L<E;L++)z[g[L+V*E]]+=2*y[T][V+L*E]});let D=new Float64Array(u);for(let g=0;g<_;g++){let T=t[g];for(let E=n.rowStart[g];E<n.rowStart[g+1];E++)D[n.rowCols[E]]+=n.rowValues[E]*T}let S,G;if(i&&k>0){S=new Float64Array(u*k);for(let g=0;g<k;g++)for(let T=0;T<k;T++){let E=j[T+g*k],V=b[T];for(let L=0;L<u;L++)S[L+g*u]+=V[L]*E}G=new Float64Array(k*u);for(let g=0;g<_;g++)for(let T=n.rowStart[g];T<n.rowStart[g+1];T++){let E=n.rowCols[T],V=n.rowValues[T];for(let L=0;L<k;L++)G[L+E*k]+=p[L][g]*V}}let U=2*(i?_-k:_)/m;for(let{row:g,col:T,theta:E}of r.lambda){let V=0;if(S!==void 0&&G!==void 0)for(let L=0;L<k;L++)V+=S[T+L*u]*G[L+g*k];z[E]-=2*V+U*D[g]*e[T]}return z}function Xt(r){let n=[],s=e=>{e.type==="var"?n.includes(e.name)||n.push(e.name):e.type==="binary"?(s(e.left),e.op!=="^"&&s(e.right)):(e.type==="unary"||e.type==="paren"||e.type==="Error")&&s(e.arg)};s(r);let{terms:l}=wt(r,e=>n.indexOf(e),[]);if(l.length===0)throw new SyntaxError(`invalid formula: no grouping factor in '${ct(r)}'`);let o=l.map((e,m)=>[e,m]).sort((e,m)=>e[0].length-m[0].length||e[1]-m[1]).map(([e])=>e.map(m=>n[m])),i=!1,t=e=>{e.type==="binary"?(e.op==="/"&&(i=!0),t(e.left),t(e.right)):(e.type==="unary"||e.type==="paren"||e.type==="Error")&&t(e.arg)};return t(r),i&&(o=o.map(e=>[...e].reverse()).reverse()),o}function Ht(r){if(r.kind==="factor")return vt(r);let n=[...new Set(r.values)].sort((o,i)=>o-i),s=[],l=new Map;for(let o of n){let i=st(o);l.has(i)||(l.set(i,s.length),s.push(i))}return{levels:s,codes:r.values.map(o=>l.get(st(o)))}}function yt(r,n,s={}){let l=gt(r,!1,!0);if(l.response===void 0)throw new TypeError("the formula has no response");let{fixed:o,bars:i}=dt(l.rhs);if(typeof n!="object"||n===null)throw new TypeError("data must be an object of columns");let t={...s.levels===void 0?{}:{levels:s.levels},dropUnusedLevels:!0},e=o??{type:"num",value:1,text:"1"},m=s.contrasts===void 0?{}:{contrasts:s.contrasts,contrastVariables:at({response:l.response,rhs:e},Object.keys(n)).variables},b=lt(l,n,{...t,...m},e),{frame:p}=b,j=p.rows.length;if(j===0)throw new RangeError("0 (non-NA) cases");if(i.length===0)throw new Error("No random effects terms specified in formula");let _=b.terms.variables,k=[];for(let c of i){let a=[c.lhs];if(c.double){let h=at(l,Object.keys(n),c.lhs);a=h.intercept?[{type:"num",value:1,text:"1"}]:[];for(let v of h.terms){let F=v.map(R=>({type:"var",name:_[R]})).reduce((R,w)=>({type:"binary",op:":",left:R,right:w}));a.push({type:"binary",op:"+",left:{type:"num",value:0,text:"0"},right:F})}}let O=Xt(c.group);for(let h of a)for(let v of O)k.push({lhs:h,groupVariables:v})}let u=new Map,I=c=>{let a=c.join(":"),O=u.get(a);if(O!==void 0)return O;let h=c.map(F=>Ht(p.variables[_.indexOf(F)])),v;if(h.length===1)v=h[0];else{let F=new Map;for(let d=0;d<j;d++){let q=h.map(W=>W.codes[d]);F.set(q.join(","),q)}let R=[...F.values()].sort((d,q)=>{for(let W=0;W<d.length;W++)if(d[W]!==q[W])return d[W]-q[W];return 0}),w=new Map(R.map((d,q)=>[d.join(","),q]));v={levels:R.map(d=>d.map((q,W)=>h[W].levels[q]).join(":")),codes:Array.from({length:j},(d,q)=>w.get(h.map(W=>W.codes[q]).join(",")))}}return u.set(a,v),v},C=k.map(c=>({...c,name:c.groupVariables.join(":"),factor:I(c.groupVariables)})),$=C;C.some((c,a)=>a>0&&c.factor.levels.length>C[a-1].factor.levels.length)&&($=C.map((c,a)=>[c,a]).sort((c,a)=>c[0].factor.levels.length-a[0].factor.levels.length||c[1]-a[1]).map(([c])=>c).reverse());let y=[],A=[],x=0,z=0;for(let c of $){let a=y.findIndex(F=>F.name===c.name);a<0&&(a=y.length,y.push({name:c.name,levels:c.factor.levels,codes:c.factor.codes}));let O=lt(l,n,t,c.lhs),h=O.colNames.length,v=c.factor.levels.length;A.push({label:`${ct(c.lhs)} | ${c.name}`,group:a,columns:O.colNames,matrix:O.x,nLevels:v,zOffset:x,thetaOffset:z}),x+=v*h,z+=h*(h+1)/2}if(y.some(c=>c.levels.length<2))throw new RangeError("grouping factors must have > 1 sampled level");let D=y.filter(c=>c.levels.length>=j).map(c=>c.name);if(D.length>0)throw new RangeError(`number of levels of each grouping factor must be < number of observations (problems: ${D.join(", ")})`);for(let c of A){let a=c.nLevels*c.columns.length;if(j<=a)throw new RangeError(`number of observations (=${j}) <= number of random effects (=${a}) for term (${c.label}); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable`)}let S=[],G=[],H=[],U=[],g=[];for(let c of A){let{levels:a,codes:O}=y[c.group],h=c.columns.length,v=Array.from({length:a.length*h},()=>({rows:[],values:[]}));for(let w=0;w<j;w++){let d=c.matrix[w],q=O[w]*h;for(let W=0;W<h;W++){let tt=d[W];if(tt===0)continue;let et=v[q+W];et.rows.push(w),et.values.push(tt)}}for(let w=0;w<a.length;w++)for(let d=0;d<h;d++)S.push(v[w*h+d]),G.push(a[w]);let F=Array.from({length:h},()=>new Array(h).fill(-1)),R=c.thetaOffset;for(let w=0;w<h;w++)for(let d=w;d<h;d++)F[d][w]=R++,H.push(d===w?1:0),U.push(d===w?0:-1/0);for(let w=0;w<c.nLevels;w++){let d=c.zOffset+w*h;for(let q=0;q<h;q++)for(let W=q;W<h;W++)g.push({row:d+W,col:d+q,theta:F[W][q]})}}let T=[],E=b.x,V=b.colNames;if(V.length>0){let c=bt(E),a=new Set(c.pivot.slice(c.rank));if(a.size>0){let O=V.map((h,v)=>v).filter(h=>!a.has(h));E=E.map(h=>O.map(v=>h[v])),V=O.map(h=>V[h]),T.push(`fixed-effect model matrix is rank deficient so dropping ${a.size} ${a.size===1?"column / coefficient":"columns / coefficients"}`)}}let L=[...b.warnings],B=[];for(let c=0;c<V.length;c++){let a=E.map(v=>v[c]);if(a.every(v=>v===0||v===1))continue;let O=a.reduce((v,F)=>v+F,0)/j,h=Math.sqrt(a.reduce((v,F)=>v+(F-O)**2,0)/(j-1));B.push(h)}let K=Math.log(1e3);return B.some((c,a)=>Math.abs(Math.log(c))>K||B.some((O,h)=>h!==a&&Math.abs(Math.log(c/O))>K))&&L.push(`Some predictor variables are on very different scales: consider rescaling.
|
|
3
|
-
You may also use (g)lmerControl(autoscale = TRUE) to improve numerical stability.`),{rows:p.rows,y:b.y,xColNames:V,x:E,terms:A,groups:y,q:S.length,z:S,zColLevels:G,theta:H,lower:U,lambda:g,messages:T,warnings:L}}var St=.25,Wt=1e-4,Yt=1e-6,Et=1e-6,It=1e-4,Kt=.001,Ot=1e-13,Ft=1e-10,Zt=1e-9,Jt=500,Tt=.75,Rt="lmer: the minimisation of the criterion over theta did not converge";function zt(r){let n=r.length,s=r.map(o=>o.slice()),l=s.map((o,i)=>s.map((t,e)=>i===e?1:0));for(let o=0;o<100;o++){let i=0,t=0;for(let e=0;e<n;e++)for(let m=0;m<n;m++)t+=s[e][m]**2,e!==m&&(i+=s[e][m]**2);if(i<=1e-32*t)break;for(let e=0;e<n-1;e++)for(let m=e+1;m<n;m++){let b=s[e][m];if(b===0)continue;let p=(s[m][m]-s[e][e])/(2*b),j=Math.sign(p||1)/(Math.abs(p)+Math.sqrt(1+p*p)),_=1/Math.sqrt(1+j*j),k=j*_;for(let u=0;u<n;u++){let I=s[u][e],C=s[u][m];s[u][e]=_*I-k*C,s[u][m]=k*I+_*C}for(let u=0;u<n;u++){let I=s[e][u],C=s[m][u];s[e][u]=_*I-k*C,s[m][u]=k*I+_*C}for(let u=0;u<n;u++){let I=l[u][e],C=l[u][m];l[u][e]=_*I-k*C,l[u][m]=k*I+_*C}}}return{values:s.map((o,i)=>o[i]),vectors:s.map((o,i)=>l.map(t=>t[i]))}}var ht=r=>Math.sqrt(r.reduce((n,s)=>n+s*s,0)),Q=(r,n)=>r.reduce((s,l,o)=>s+l*n[o],0);function Qt(r,n,s,l,o){let i=r.length,{values:t,vectors:e}=n,m=e.map(A=>Q(A,r)),b=A=>{let x=new Array(i).fill(0);return A.forEach((z,D)=>e[D].forEach((S,G)=>x[G]+=z*S)),x},p=Math.min(...t);if(p>-l&&t.every((A,x)=>A>l||Math.abs(m[x])<=o)){let A=b(t.map((x,z)=>x>l?-m[z]/x:0));if(ht(A)<=s)return{step:A,newton:!0}}let j=Math.max(0,-p),_=A=>Math.sqrt(t.reduce((x,z,D)=>x+(m[D]/(z+A))**2,0)),k=Math.max(l,1e-12*Math.max(...t.map(Math.abs),1));if(_(j+k)>=s){let A=j+k,x=j+ht(r)/s+Math.max(...t.map(Math.abs))+k;for(let z=0;z<200&&x-A>1e-15*x;z++){let D=(A+x)/2;_(D)>s?A=D:x=D}return{step:b(t.map((z,D)=>-m[D]/(z+x))),newton:!1}}let u=t.map(A=>A+j<=k),I=b(t.map((A,x)=>u[x]?0:-m[x]/(A+j))),C=t.indexOf(p),$=Math.sqrt(Math.max(0,s*s-Q(I,I))),y=-Math.sign(m[C])||1;return{step:I.map((A,x)=>A+y*$*e[C][x]),newton:!1}}function te(r){let n=[];for(let s of r.terms){let l=s.columns.length,o=s.thetaOffset;for(let i=0;i<l;i++){let t=[];for(let e=i;e<l;e++)t.push(o++);n.push(t)}}return n}var ee=(r,n,s)=>{let l=ut(r,n,s,{gradient:!0});return{f:l.deviance,g:l.gradient}};function oe(r,n,s){let l=r.lower.map(y=>y===0),o=y=>s(r,y,n),i=r.theta.slice(),{f:t,g:e}=o(i),m=new Set,b=new Set,p=()=>Math.max(1,...i.map(Math.abs)),j=()=>Zt*Math.max(1,Math.abs(t)),_=St*p(),k=1/0,u,I=y=>{let A=Yt*p(),x=y.map(z=>{let D=Math.max(Wt*Math.abs(i[z]),A),S=i.slice();S[z]=i[z]+D;let G=i.slice();G[z]=i[z]-D;let H=o(S).g,U=o(G).g;return y.map(g=>(H[g]-U[g])/(2*D))});return y.map((z,D)=>y.map((S,G)=>(x[D][G]+x[G][D])/2))},C=y=>{let A=u===void 0||u.free.length!==y.length||u.free.some((c,a)=>c!==y[a]),x=A?I(y):u.h;u=void 0;let z=zt(x),D=Et*Math.max(...z.values.map(Math.abs)),S=y.map(c=>e[c]),{step:G,newton:H}=Qt(S,z,_,D,j()),U=i.slice();y.forEach((c,a)=>U[c]=i[c]+G[a]);let g=Math.max(...G.map(Math.abs))/p(),T=o(U);if(!Number.isFinite(T.f))throw new Error(Rt);let E=-(Q(S,G)+Q(G,x.map(c=>Q(c,G)))/2),V=Math.abs(t-T.f)>1e-10*Math.abs(t)?t-T.f:-(Q(S,G)+Q(y.map(c=>T.g[c]),G))/2,L=E>0?V/E:V>=0?1:-1;(L>.1||g<=Ft)&&(i=U,t=T.f,e=T.g);let B=ht(G);if(L<.25?_=Math.max(B/4,Ot*p()):L>.75&&B>=.99*_&&(_*=2),!H)return k=1/0,!1;let X=y.every(c=>Math.abs(e[c])<=j())&&(g<=Ot||A&&g<=Ft&&g>=.5*k);return L>Tt&&L<1/Tt&&g<.5*k&&(u={free:y,h:x}),k=g,X},$=()=>{if(m.size===0)return!1;let y=i.map((H,U)=>U),A=zt(I(y)),x=Et*Math.max(...A.values.map(Math.abs)),z=A.values.indexOf(Math.min(...A.values));if(!(A.values[z]<-x))return!1;let D=A.vectors[z],S=y.filter((H,U)=>m.has(H)&&Math.abs(D[U])>1e-8);if(S.length===0)return!1;for(let H of S)m.delete(H),b.add(H);let G=Kt*p();return y.forEach((H,U)=>i[H]=i[H]+G*D[U]),{f:t,g:e}=o(i),!0};for(let y=0;y<Jt;y++){let A=i.map((S,G)=>G).filter(S=>!m.has(S));if(A.length>0&&!C(A))continue;let x=A.filter(S=>l[S]&&!b.has(S)&&Math.abs(i[S])<It*p());if(x.length>0){for(let S of x)i[S]=0,m.add(S);({f:t,g:e}=o(i)),k=1/0,u=void 0;continue}let z=1e-12*Math.max(1,Math.abs(t)),D=[...m].filter(S=>Math.abs(e[S])>z);if(D.length>0)for(let S of D)m.delete(S),b.add(S);else if(!$()){for(let S of te(r))if(i[S[0]]<0)for(let G of S)i[G]=-i[G];return i.map(S=>S===0?0:S)}_=Math.max(_,St*It*p()),k=1/0,u=void 0}throw new Error(Rt)}function re(r,n,s={}){return ne(r,n,s,ee)}function ne(r,n,s,l){let o=s.REML??!0,i={};s.contrasts!==void 0&&(i.contrasts=s.contrasts),s.levels!==void 0&&(i.levels=s.levels);let t=yt(r,n,i),e=oe(t,o,l),m=ut(t,e,o,{rxInverse:!0}),b=t.y.length,p=t.xColNames.length,j=2*Math.PI,_=m.logDetL+b*(1+Math.log(j*m.prss/b)),k=m.logDetL+m.logDetRX+(b-p)*(1+Math.log(j*m.prss/(b-p))),u=m.prss/(o?b-p:b),I=Math.sqrt(u),C=m.rxInverse.map(g=>g.map(T=>u*T)),$=C.map((g,T)=>Math.sqrt(g[T])),y=m.beta.map((g,T)=>g/$[T]),A=t.terms.map(g=>{let T=g.columns.length,E=Array.from({length:T},()=>new Array(T).fill(0)),V=g.thetaOffset;for(let X=0;X<T;X++)for(let c=X;c<T;c++)E[c][X]=e[V++];let L=E.map(X=>E.map(c=>u*X.reduce((a,O,h)=>a+O*c[h],0))),B=L.map((X,c)=>Math.sqrt(X[c])),K=L.map((X,c)=>X.map((a,O)=>c===O?1:a/(B[c]*B[O])));return{group:t.groups[g.group].name,columns:[...g.columns],sd:B,corr:K}}),x=t.groups.map((g,T)=>{let E=t.terms.filter(V=>V.group===T);return{group:g.name,levels:[...g.levels],columns:E.flatMap(V=>V.columns),values:g.levels.map((V,L)=>E.flatMap(B=>{let K=B.columns.length;return B.columns.map((X,c)=>m.b[B.zOffset+L*K+c])}))}}),z=t.x.map(g=>g.reduce((T,E,V)=>T+E*m.beta[V],0));t.z.forEach((g,T)=>{let E=m.b[T];E!==0&&g.rows.forEach((V,L)=>z[V]+=g.values[L]*E)});let D=t.y.map((g,T)=>g-z[T]),S=-(o?k:_)/2,G=p+e.length+1,H=e.some((g,T)=>t.lower[T]===0&&g<1e-4),U=[...t.messages];return H&&U.push("boundary (singular) fit: see help('isSingular')"),{formula:r,REML:o,nObs:b,rows:[...t.rows],theta:e,fixefNames:[...t.xColNames],fixef:m.beta,coefTable:{estimate:[...m.beta],stdError:$,tValue:y},vcov:C,sigma:I,varCorr:A,ranef:x,fitted:z,residuals:D,logLik:S,df:G,AIC:-2*S+2*G,BIC:-2*S+Math.log(b)*G,deviance:_,REMLcrit:k,singular:H,messages:U,warnings:[...t.warnings]}}export{re as lmer};
|
|
1
|
+
/*! rStats.js 0.15.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{a as xt,c as St}from"./chunks/chunk-GZTLODPK.js";import{c as me,s as ue}from"./chunks/chunk-LVXAWWUX.js";import{b as qt}from"./chunks/chunk-7GEYDIQC.js";import{A as be,C as ye,E as Xt,h as pt,k as we,l as ve}from"./chunks/chunk-VAPGDPHM.js";import{a as Lt,c as zt,d as Nt,f as de,g as Et}from"./chunks/chunk-A3XZUJCP.js";import{k as pe,l as he}from"./chunks/chunk-VSKWQMSG.js";import{b as ge}from"./chunks/chunk-WEWXYHPR.js";import{b as Bt}from"./chunks/chunk-HNLKOICT.js";import"./chunks/chunk-LU5FHU2O.js";import"./chunks/chunk-CMPDE3SZ.js";import"./chunks/chunk-4EOKNOXP.js";var xe=1,Ae=new WeakMap;function Xe(t){let n=Ae.get(t);if(n!==void 0)return n;let e=t.y.length,r=t.xColNames.length,o=t.q,a=t.terms.length,c=t.terms.reduce((w,O)=>w+O.nLevels,0),s=new Int32Array(c+1),f=new Int32Array(c),h=[],p=[],g=0;t.terms.forEach((w,O)=>{let q=w.columns.length;h.push(g),p.push(q);for(let L=0;L<w.nLevels;L++,g++)s[g]=w.zOffset+L*q,f[g]=O}),s[c]=o;let A=t.terms.map((w,O)=>{let q=p[O],L=new Int32Array(q*q).fill(-1);for(let{row:N,col:z,theta:F}of t.lambda)z>=w.zOffset&&z<w.zOffset+q&&(L[N-w.zOffset+(z-w.zOffset)*q]=F);return L}),i=t.terms.map(w=>t.groups[w.group].codes),u=new Int32Array(e+1);for(let w=0;w<e;w++){let O=0;for(let q of t.terms)for(let L of q.matrix[w])L!==0&&O++;u[w+1]=u[w]+O}let l=u,m=l[e],M=new Int32Array(m),T=new Float64Array(m);{let w=0;for(let O=0;O<e;O++)t.terms.forEach((q,L)=>{let N=q.zOffset+i[L][O]*p[L];q.matrix[O].forEach((z,F)=>{z!==0&&(M[w]=N+F,T[w++]=z)})})}let S=new Map,y=[],k=[],d=(w,O)=>{let q=O*c+w,L=S.get(q);return L===void 0&&(L=y.length,S.set(q,L),y.push(w),k.push(O)),L};for(let w=0;w<c;w++)d(w,w);let v=new Int32Array(a),x=a*(a+1)/2,b=new Int32Array(e*x);for(let w=0;w<e;w++){for(let O=0;O<a;O++)v[O]=h[O]+i[O][w];for(let O=0;O<a;O++)for(let q=0;q<=O;q++)b[w*x+O*(O+1)/2+q]=d(v[O],v[q])}let E=y.length,D=Array.from({length:E},(w,O)=>O).sort((w,O)=>k[w]-k[O]||y[w]-y[O]),R=new Int32Array(E);D.forEach((w,O)=>R[w]=O);let j=new Int32Array(c+1),tt=new Int32Array(E),st=new Int32Array(E+1);D.forEach((w,O)=>{tt[O]=y[w],j[k[w]+1]++;let q=p[f[y[w]]]*p[f[k[w]]];st[O+1]=st[O]+q});for(let w=0;w<c;w++)j[w+1]+=j[w];let et=new Float64Array(st[E]),nt=new Float64Array(o*r),$=new Float64Array(o),P=new Float64Array(r);for(let w=0;w<e;w++){let O=t.x[w],q=t.y[w];for(let L=0;L<a;L++){let N=t.terms[L].matrix[w],z=p[L];for(let F=0;F<=L;F++){let Z=t.terms[F].matrix[w],J=p[F],U=R[b[w*x+L*(L+1)/2+F]],rt=st[U];for(let lt=0;lt<J;lt++){let Rt=Z[lt];if(Rt!==0)for(let mt=0;mt<z;mt++)et[rt+mt+lt*z]+=N[mt]*Rt}}}for(let L=l[w];L<l[w+1];L++){let N=M[L],z=T[L];for(let F=0;F<r;F++)nt[N+F*o]+=z*O[F];$[N]+=z*q}for(let L=0;L<r;L++)P[L]+=O[L]*q}let Y=new Array(c),X=Array.from({length:c},()=>[]),I=new Int32Array(c).fill(-1);for(let w=0;w<c;w++){let O=[],q=N=>{N!==w&&I[N]!==w&&(I[N]=w,O.push(N))};for(let N=j[w]+1;N<j[w+1];N++)q(tt[N]);for(let N of X[w])for(let z of Y[N])q(z);let L=Int32Array.from(O).sort();Y[w]=L,L.length>0&&X[L[0]].push(w)}let C=new Int32Array(E);{let w=new Int32Array(c);for(let O=0;O<c;O++){let q=0;for(let L of Y[O])w[L]=q,q+=s[L+1]-s[L];for(let L=j[O]+1;L<j[O+1];L++)C[L]=w[tt[L]]}}let G=new Int32Array(o+1);for(let w=0;w<c;w++){let O=0;for(let q of Y[w])O+=s[q+1]-s[q];for(let q=s[w];q<s[w+1];q++)G[q+1]=G[q]+(s[w+1]-q)+O}let _=new Int32Array(G[o]),V=new Int32Array(o+1);for(let w=0;w<c;w++)for(let O=s[w];O<s[w+1];O++){let q=G[O];for(let L=O;L<s[w+1];L++)_[q++]=L;for(let L of Y[w])for(let N=s[L];N<s[L+1];N++)_[q++]=N}for(let w=0;w<o;w++)for(let O=G[w]+1;O<G[w+1];O++)V[_[O]+1]++;for(let w=0;w<o;w++)V[w+1]+=V[w];let K=V.slice(),at=V.slice(0,o),W=new Int32Array(K[o]),B=new Int32Array(K[o]);for(let w=0;w<o;w++)for(let O=G[w]+1;O<G[w+1];O++){let q=at[_[O]]++;W[q]=w,B[q]=O}let ot={n:e,p:r,q:o,nGroups:c,groupStart:s,groupTerm:f,termNc:p,termTheta:A,blockStart:j,blockRow:tt,blockValueStart:st,zz:et,zx:nt,zy:$,xy:P,rowStart:l,rowCols:M,rowValues:T,colStart:G,colRows:_,rowListStart:K,rowListCol:W,rowListPos:B,blockPatternOffset:C};return Ae.set(t,ot),ot}function Ye(t,n){return t.termTheta.map(e=>Float64Array.from(e,r=>r<0?0:n[r]))}function dt(t,n,e){let r=new Float64Array(t.q);for(let o=0;o<t.nGroups;o++){let a=t.groupTerm[o],c=t.termNc[a],s=n[a],f=t.groupStart[o];for(let h=0;h<c;h++){let p=e[f+h];if(p!==0)for(let g=h;g<c;g++)r[f+g]+=s[g+h*c]*p}}return r}function ht(t,n,e,r=1){let o=t.q,a=new Float64Array(o*r);for(let c=0;c<t.nGroups;c++){let s=t.groupTerm[c],f=t.termNc[s],h=n[s],p=t.groupStart[c];for(let g=0;g<r;g++){let A=p+g*o;for(let i=0;i<f;i++){let u=0;for(let l=i;l<f;l++)u+=h[l+i*f]*e[A+l];a[A+i]=u}}}return a}function Ke(t,n){let e=new Float64Array(t.zz.length),r=new Float64Array(64);for(let o=0;o<t.nGroups;o++){let a=t.groupTerm[o],c=t.termNc[a],s=n[a];for(let f=t.blockStart[o];f<t.blockStart[o+1];f++){let h=t.groupTerm[t.blockRow[f]],p=t.termNc[h],g=n[h],A=t.blockValueStart[f];if(p===1&&c===1)e[A]=g[0]*t.zz[A]*s[0];else{let i=r.length>=p*c?r:new Float64Array(p*c);for(let u=0;u<c;u++)for(let l=0;l<p;l++){let m=0;for(let M=u;M<c;M++)m+=t.zz[A+l+M*p]*s[M+u*c];i[l+u*p]=m}for(let u=0;u<c;u++)for(let l=0;l<p;l++){let m=0;for(let M=l;M<p;M++)m+=g[M+l*p]*i[M+u*p];e[A+l+u*p]=m}}if(f===t.blockStart[o])for(let i=0;i<c;i++)e[A+i+i*c]+=1}}return e}function Ze(t,n){let{q:e,colStart:r,colRows:o,rowListStart:a,rowListCol:c,rowListPos:s}=t,f=new Float64Array(o.length),h=new Float64Array(e);for(let p=0;p<t.nGroups;p++){let g=t.termNc[t.groupTerm[p]],A=t.groupStart[p];for(let i=0;i<g;i++){let u=A+i;for(let T=t.blockStart[p];T<t.blockStart[p+1];T++){let S=t.blockRow[T],y=t.termNc[t.groupTerm[S]],k=t.blockValueStart[T]+i*y,d=t.groupStart[S];for(let v=S===p?i:0;v<y;v++)h[d+v]=n[k+v]}for(let T=a[u];T<a[u+1];T++){let S=c[T],y=s[T],k=f[y];if(k===0)continue;let d=r[S+1];for(let v=y;v<d;v++)h[o[v]]-=f[v]*k}let l=h[u];if(!(l>0))throw new Error("the penalised least-squares matrix is not positive definite");let m=Math.sqrt(l),M=r[u];f[M]=m,h[u]=0;for(let T=M+1;T<r[u+1];T++){let S=o[T];f[T]=h[S]/m,h[S]=0}}}return f}function Mt(t,n,e,r=0){let{q:o,colStart:a,colRows:c}=t;for(let s=0;s<o;s++){let f=a[s],h=e[r+s]/=n[f];if(h!==0)for(let p=f+1;p<a[s+1];p++)e[r+c[p]]-=n[p]*h}}function kt(t,n,e,r=0){let{q:o,colStart:a,colRows:c}=t;for(let s=o-1;s>=0;s--){let f=a[s],h=e[r+s];for(let p=f+1;p<a[s+1];p++)h-=n[p]*e[r+c[p]];e[r+s]=h/n[f]}}function Je(t,n,e){for(let r=0;r<n;r++){let o=r*n,a=e[r];for(let c=0;c<r;c++)a-=t[c+o]*e[c];e[r]=a/t[r+o]}}function Qe(t,n,e){for(let r=n-1;r>=0;r--){let o=e[r]/=t[r+r*n],a=r*n;for(let c=0;c<r;c++)e[c]-=t[c+a]*o}}function Yt(t,n){let e=t.slice();for(let r=0;r<n;r++)for(let o=r+1;o<n;o++)e[o+r*n]=0;ye({nrow:n,ncol:n,data:e});for(let r=0;r<n;r++)for(let o=r+1;o<n;o++)e[o+r*n]=e[r+o*n];return e}function Kt(t,n,e,r={}){let o=Zt(t,n),{sys:a,t:c,l:s,rx:f,ws:h,xResiduals:p,res:g,uv:A}=o,{n:i,p:u,q:l}=a,m=Array.from(o.beta),M=Array.from(A),T=Array.from(o.b),S=0;for(let R of g)S+=R*R;let y=0;for(let R of M)y+=R*R;let k=S+y,d=0;for(let R=0;R<l;R++)d+=Math.log(s[a.colStart[R]]);d*=2;let v=0;for(let R=0;R<u;R++)v+=Math.log(f[R+R*u]);v*=2;let x=e?i-u:i,E={deviance:(e?d+v:d)+x*(1+Math.log(2*Math.PI*k/x)),beta:m,u:M,b:T,rss:S,ussq:y,prss:k,logDetL:d,logDetRX:v},D=r.gradient===!0||r.rxInverse===!0?Yt(f,u):void 0;return r.rxInverse===!0&&(E.rxInverse=Array.from({length:u},(R,j)=>Array.from({length:u},(tt,st)=>D[j+st*u]))),r.gradient===!0&&(E.gradient=to(t,a,c,s,n,e,{res:g,uv:A,prss:k,ws:h,xResiduals:p,sInverse:D})),E}function Zt(t,n){if(n.length!==t.theta.length)throw new RangeError(`theta must have length ${t.theta.length}, not ${n.length}`);if(n.some(d=>!Number.isFinite(d)))throw new RangeError("theta must be finite");let e=Xe(t),{n:r,p:o,q:a}=e,c=Ye(e,n),s=Ze(e,Ke(e,c)),f=d=>{let v=new Float64Array(r);for(let x=0;x<r;x++){let b=0;for(let E=e.rowStart[x];E<e.rowStart[x+1];E++)b+=e.rowValues[E]*d[e.rowCols[E]];v[x]=b}return v},h=d=>{let v=new Float64Array(a);for(let x=0;x<r;x++){let b=d[x];for(let E=e.rowStart[x];E<e.rowStart[x+1];E++)v[e.rowCols[E]]+=e.rowValues[E]*b}return v},p=d=>{Mt(e,s,d),kt(e,s,d)},g=ht(e,c,e.zx,o);for(let d=0;d<o;d++)Mt(e,s,g,d*a);let A=[],i=[];for(let d=0;d<o;d++){let v=g.slice(d*a,(d+1)*a);kt(e,s,v);let x=Float64Array.from(t.x,D=>D[d]),b=()=>{let D=f(dt(e,c,v));return x.map((R,j)=>R-D[j])},E=b();for(let D=0;D<xe;D++){let R=ht(e,c,h(E));for(let j=0;j<a;j++)R[j]-=v[j];p(R);for(let j=0;j<a;j++)v[j]+=R[j];E=b()}A.push(E),i.push(v)}let u=new Float64Array(o*o);for(let d=0;d<o;d++)for(let v=d;v<o;v++){let x=0,b=A[d],E=A[v];for(let j=0;j<r;j++)x+=b[j]*E[j];let D=i[d],R=i[v];for(let j=0;j<a;j++)x+=D[j]*R[j];u[d+v*o]=x,u[v+d*o]=x}let l={nrow:o,ncol:o,data:u};if(o>0&&be(l)>0)throw new Error("the fixed-effects matrix of the penalised least squares is not positive definite");let m=(d,v)=>{Mt(e,s,d);for(let x=0;x<o;x++){let b=v[x];for(let E=0;E<a;E++)b-=g[E+x*a]*d[E];v[x]=b}Je(u,o,v),Qe(u,o,v);for(let x=0;x<o;x++){let b=v[x];for(let E=0;E<a;E++)d[E]-=g[E+x*a]*b}kt(e,s,d)},M=(d,v)=>{let x=f(v),b=new Float64Array(r);for(let E=0;E<r;E++){let D=t.x[E],R=x[E];for(let j=0;j<o;j++)R+=D[j]*d[j];b[E]=t.y[E]-R}return b},T=ht(e,c,e.zy),S=Float64Array.from(e.xy);m(T,S);let y=dt(e,c,T),k=M(S,y);for(let d=0;d<xe;d++){let v=ht(e,c,h(k));for(let b=0;b<a;b++)v[b]-=T[b];let x=new Float64Array(o);for(let b=0;b<r;b++){let E=t.x[b],D=k[b];for(let R=0;R<o;R++)x[R]+=E[R]*D}m(v,x);for(let b=0;b<a;b++)T[b]+=v[b];for(let b=0;b<o;b++)S[b]+=x[b];y=dt(e,c,T),k=M(S,y)}return{sys:e,t:c,l:s,cx:g,rx:u,ws:i,xResiduals:A,res:k,uv:T,beta:S,b:y,solveNormal:m}}function Jt(t,n){let{q:e,colStart:r,colRows:o}=t,a=new Float64Array(n.length),c=new Int32Array(e).fill(-1),s=new Float64Array(e),f=new Float64Array(e);for(let h=e-1;h>=0;h--){let p=r[h],g=r[h+1];for(let u=p+1;u<g;u++){let l=o[u];c[l]=h,s[l]=n[u],f[l]=0}for(let u=p+1;u<g;u++){let l=o[u],m=n[u],M=r[l],T=a[M]*m;for(let S=M+1;S<r[l+1];S++){let y=o[S];if(c[y]!==h)continue;let k=a[S];f[y]+=k*m,T+=k*s[y]}f[l]+=T}let A=n[p],i=1/A;for(let u=p+1;u<g;u++){let l=-f[o[u]]/A;a[u]=l,i-=n[u]*l}a[p]=i/A}return a}function Ne(t,n,e){let{q:r,colStart:o,colRows:a,rowListStart:c,rowListCol:s,rowListPos:f}=t,h=new Float64Array(n.length),p=new Float64Array(r);for(let g=0;g<t.nGroups;g++){let A=t.termNc[t.groupTerm[g]],i=t.groupStart[g];for(let u=0;u<A;u++){let l=i+u;for(let S=t.blockStart[g];S<t.blockStart[g+1];S++){let y=t.blockRow[S],k=t.termNc[t.groupTerm[y]],d=t.blockValueStart[S]+u*k,v=t.groupStart[y];for(let x=y===g?u:0;x<k;x++)p[v+x]=e[d+x]}for(let S=c[l];S<c[l+1];S++){let y=s[S],k=f[S],d=n[k],v=h[k];if(d===0&&v===0)continue;let x=o[y+1];for(let b=k;b<x;b++)p[a[b]]-=h[b]*d+n[b]*v}let m=o[l],M=n[m],T=p[l]/(2*M);h[m]=T,p[l]=0;for(let S=m+1;S<o[l+1];S++){let y=a[S];h[S]=(p[y]-n[S]*T)/M,p[y]=0}}}return h}function Ee(t,n,e,r){let{q:o,colStart:a,colRows:c}=t,s=new Float64Array(n.length),f=new Int32Array(o).fill(-1),h=new Float64Array(o),p=new Float64Array(o),g=new Float64Array(o),A=new Float64Array(o);for(let i=o-1;i>=0;i--){let u=a[i],l=a[i+1];for(let y=u+1;y<l;y++){let k=c[y];f[k]=i,h[k]=n[y],p[k]=e[y],g[k]=0,A[k]=0}for(let y=u+1;y<l;y++){let k=c[y],d=n[y],v=e[y],x=a[k],b=r[x]*d,E=s[x]*d+r[x]*v;for(let D=x+1;D<a[k+1];D++){let R=c[D];if(f[R]!==i)continue;let j=r[D],tt=s[D];g[R]+=j*d,A[R]+=tt*d+j*v,b+=j*h[R],E+=tt*h[R]+j*p[R]}g[k]+=b,A[k]+=E}let m=n[u],M=e[u],T=1/m,S=-M/(m*m);for(let y=u+1;y<l;y++){let k=c[y],d=r[y],v=-A[k]/m+g[k]*M/(m*m);s[y]=v,T-=n[y]*d,S-=e[y]*d+n[y]*v}s[u]=S/m-T*M/(m*m)}return s}function to(t,n,e,r,o,a,{res:c,uv:s,prss:f,ws:h,xResiduals:p,sInverse:g}){let{n:A,p:i,q:u}=n,l=It(n,Jt(n,r),e),m=new Array(o.length).fill(0);n.termTheta.forEach((d,v)=>{let x=n.termNc[v];for(let b=0;b<x;b++)for(let E=b;E<x;E++)m[d[E+b*x]]+=2*l[v][b+E*x]});let M=new Float64Array(u);for(let d=0;d<A;d++){let v=c[d];for(let x=n.rowStart[d];x<n.rowStart[d+1];x++)M[n.rowCols[x]]+=n.rowValues[x]*v}let T,S;if(a&&i>0){T=new Float64Array(u*i);for(let d=0;d<i;d++)for(let v=0;v<i;v++){let x=g[v+d*i],b=h[v];for(let E=0;E<u;E++)T[E+d*u]+=b[E]*x}S=new Float64Array(i*u);for(let d=0;d<A;d++)for(let v=n.rowStart[d];v<n.rowStart[d+1];v++){let x=n.rowCols[v],b=n.rowValues[v];for(let E=0;E<i;E++)S[E+x*i]+=p[E][d]*b}}let k=2*(a?A-i:A)/f;for(let{row:d,col:v,theta:x}of t.lambda){let b=0;if(T!==void 0&&S!==void 0)for(let E=0;E<i;E++)b+=T[v+E*u]*S[E+d*i];m[x]-=2*b+k*M[d]*s[v]}return m}function It(t,n,e){let{colStart:r,zz:o}=t,a=t.termNc.map(f=>new Float64Array(f*f)),c=new Float64Array(16),s=new Float64Array(16);for(let f=0;f<t.nGroups;f++){let h=t.groupTerm[f],p=t.termNc[h],g=e[h],A=t.groupStart[f];for(let i=t.blockStart[f];i<t.blockStart[f+1];i++){let u=t.blockRow[i],l=t.groupTerm[u],m=t.termNc[l],M=e[l],T=t.blockValueStart[i];if(p===1&&m===1){let d=u===f?r[A]:r[A]+1+t.blockPatternOffset[i],v=n[d]*o[T];a[h][0]+=v*M[0],u!==f&&(a[l][0]+=v*g[0]);continue}let S=m*p;c.length<S&&(c=new Float64Array(S),s=new Float64Array(Math.max(S,m*m,p*p)));for(let d=0;d<p;d++){let v=r[A+d];if(u===f)for(let x=d;x<m;x++){let b=n[v+x-d];c[x+d*m]=b,c[d+x*m]=b}else{let x=v+(p-d)+t.blockPatternOffset[i];for(let b=0;b<m;b++)c[b+d*m]=n[x+b]}}for(let d=0;d<p;d++)for(let v=0;v<m;v++){let x=0;for(let b=v;b<m;b++)x+=M[b+v*m]*o[T+b+d*m];s[v+d*m]=x}let y=a[h];for(let d=0;d<p;d++)for(let v=0;v<p;v++){let x=0;for(let b=0;b<m;b++)x+=c[b+d*m]*s[b+v*m];y[d+v*p]+=x}if(u===f)continue;for(let d=0;d<m;d++)for(let v=0;v<p;v++){let x=0;for(let b=v;b<p;b++)x+=g[b+v*p]*o[T+d+b*m];s[v+d*p]=x}let k=a[l];for(let d=0;d<m;d++)for(let v=0;v<m;v++){let x=0;for(let b=0;b<p;b++)x+=c[d+b*m]*s[b+v*p];k[d+v*m]+=x}}}return a}function eo(t){let n=[],e=s=>{s.type==="var"?n.includes(s.name)||n.push(s.name):s.type==="binary"?(e(s.left),s.op!=="^"&&e(s.right)):(s.type==="unary"||s.type==="paren"||s.type==="Error")&&e(s.arg)};e(t);let{terms:r}=de(t,s=>n.indexOf(s),[]);if(r.length===0)throw new SyntaxError(`invalid formula: no grouping factor in '${Nt(t)}'`);let o=r.map((s,f)=>[s,f]).sort((s,f)=>s[0].length-f[0].length||s[1]-f[1]).map(([s])=>s.map(f=>n[f])),a=!1,c=s=>{s.type==="binary"?(s.op==="/"&&(a=!0),c(s.left),c(s.right)):(s.type==="unary"||s.type==="paren"||s.type==="Error")&&c(s.arg)};return c(t),a&&(o=o.map(s=>[...s].reverse()).reverse()),o}function oo(t){if(t.kind==="factor")return ge(t);let n=[...new Set(t.values)].sort((o,a)=>o-a),e=[],r=new Map;for(let o of n){let a=Bt(o);r.has(a)||(r.set(a,e.length),e.push(a))}return{levels:e,codes:t.values.map(o=>r.get(Bt(o)))}}function Se(t,n,e={}){let r=Lt(t,!1,!0);if(r.response===void 0)throw new TypeError("the formula has no response");let{fixed:o,bars:a}=zt(r.rhs);if(typeof n!="object"||n===null)throw new TypeError("data must be an object of columns");let c={...e.levels===void 0?{}:{levels:e.levels},dropUnusedLevels:!0},s=o??{type:"num",value:1,text:"1"},f={response:r.response,rhs:s},h=Et(r,Object.keys(n),s).variables,p=e.contrasts===void 0?{}:{contrasts:e.contrasts},g=xt(f,n,{...c,...p,extraVariables:h},s),{frame:A}=g,i=A.rows.length;if(i===0)throw new RangeError("0 (non-NA) cases");if(a.length===0)throw new Error("No random effects terms specified in formula");let u=[...g.terms.variables,...h.filter(I=>!g.terms.variables.includes(I))],l=[],m;a.forEach((I,C)=>{let G=[I.lhs];if(I.double){let V=Et(r,Object.keys(n),I.lhs);G=V.intercept?[{type:"num",value:1,text:"1"}]:[];for(let K of V.terms){let at=K.map(W=>({type:"var",name:V.variables[W]})).reduce((W,B)=>({type:"binary",op:":",left:W,right:B}));G.push({type:"binary",op:"+",left:{type:"num",value:0,text:"0"},right:at})}G.length===0&&(m??=C)}let _=eo(I.group);for(let V of G)for(let K of _)l.push({lhs:V,groupVariables:K,bar:C})});let M=new Map,T=I=>{let C=I.join(":"),G=M.get(C);if(G!==void 0)return G;let _=I.map(K=>oo(A.variables[u.indexOf(K)])),V;if(_.length===1)V=_[0];else{let K=new Map;for(let B=0;B<i;B++){let ot=_.map(w=>w.codes[B]);K.set(ot.join(","),ot)}let at=[...K.values()].sort((B,ot)=>{for(let w=0;w<B.length;w++)if(B[w]!==ot[w])return B[w]-ot[w];return 0}),W=new Map(at.map((B,ot)=>[B.join(","),ot]));V={levels:at.map(B=>B.map((ot,w)=>_[w].levels[ot]).join(":")),codes:Array.from({length:i},(B,ot)=>W.get(_.map(w=>w.codes[ot]).join(",")))}}return M.set(C,V),V},S=l.map(I=>({...I,name:I.groupVariables.join(":"),factor:T(I.groupVariables)})),y=S;S.some((I,C)=>C>0&&I.factor.levels.length>S[C-1].factor.levels.length)&&(y=S.map((I,C)=>[I,C]).sort((I,C)=>I[0].factor.levels.length-C[0].factor.levels.length||I[1]-C[1]).map(([I])=>I).reverse());let k=[],d=[],v=0,x=0;for(let I of y){let C=k.findIndex(K=>K.name===I.name);C<0&&(C=k.length,k.push({name:I.name,levels:I.factor.levels,codes:I.factor.codes}));let G=xt(r,n,c,I.lhs),_=G.colNames.length;_===0&&(m===void 0||I.bar<m)&&(m=I.bar);let V=I.factor.levels.length;d.push({label:`${Nt(I.lhs)} | ${I.name}`,group:C,columns:G.colNames,matrix:G.x,nLevels:V,zOffset:v,thetaOffset:x}),v+=V*_,x+=_*(_+1)/2}if(m!==void 0){let I=a[m];throw new Error(`lmer: the random-effects term '${Nt(I.lhs)} ${I.double?"||":"|"} ${Nt(I.group)}' has no columns`)}if(k.some(I=>I.levels.length<2))throw new RangeError("grouping factors must have > 1 sampled level");let b=k.filter(I=>I.levels.length>=i).map(I=>I.name);if(b.length>0)throw new RangeError(`number of levels of each grouping factor must be < number of observations (problems: ${b.join(", ")})`);for(let I of d){let C=I.nLevels*I.columns.length;if(i<=C)throw new RangeError(`number of observations (=${i}) <= number of random effects (=${C}) for term (${I.label}); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable`)}let E=[],D=[],R=[],j=[],tt=[];for(let I of d){let{levels:C,codes:G}=k[I.group],_=I.columns.length,V=Array.from({length:C.length*_},()=>({rows:[],values:[]}));for(let W=0;W<i;W++){let B=I.matrix[W],ot=G[W]*_;for(let w=0;w<_;w++){let O=B[w];if(O===0)continue;let q=V[ot+w];q.rows.push(W),q.values.push(O)}}for(let W=0;W<C.length;W++)for(let B=0;B<_;B++)E.push(V[W*_+B]),D.push(C[W]);let K=Array.from({length:_},()=>new Array(_).fill(-1)),at=I.thetaOffset;for(let W=0;W<_;W++)for(let B=W;B<_;B++)K[B][W]=at++,R.push(B===W?1:0),j.push(B===W?0:-1/0);for(let W=0;W<I.nLevels;W++){let B=I.zOffset+W*_;for(let ot=0;ot<_;ot++)for(let w=ot;w<_;w++)tt.push({row:B+w,col:B+ot,theta:K[w][ot]})}}let st=[],et=g.x,nt=g.colNames;if(nt.length>0){let I=pt(et),C=new Set(I.pivot.slice(I.rank));if(C.size>0){let G=nt.map((_,V)=>V).filter(_=>!C.has(_));et=et.map(_=>G.map(V=>_[V])),nt=G.map(_=>nt[_]),st.push(`fixed-effect model matrix is rank deficient so dropping ${C.size} ${C.size===1?"column / coefficient":"columns / coefficients"}`)}}let $=[...g.warnings],P=[];for(let I=0;I<nt.length;I++){let C=et.map(V=>V[I]);if(C.every(V=>V===0||V===1))continue;let G=C.reduce((V,K)=>V+K,0)/i,_=Math.sqrt(C.reduce((V,K)=>V+(K-G)**2,0)/(i-1));P.push(_)}let Y=Math.log(1e3);return P.some((I,C)=>Math.abs(Math.log(I))>Y||P.some((G,_)=>_!==C&&Math.abs(Math.log(I/G))>Y))&&$.push(`Some predictor variables are on very different scales: consider rescaling.
|
|
3
|
+
You may also use (g)lmerControl(autoscale = TRUE) to improve numerical stability.`),{rows:A.rows,y:g.y,xColNames:nt,x:et,terms:d,groups:k,q:E.length,z:E,zColLevels:D,theta:R,lower:j,lambda:tt,messages:st,warnings:$}}var Me=.25,no=1e-4,ro=1e-6,ke=1e-6,Ie=1e-4,so=.001,Te=1e-13,Oe=1e-10,ao=1e-9,co=500,Fe=.75,lo=1e-8,fo=50,io=1e-12,mo="lmer: the response is fitted exactly by the fixed effects (a constant response, for example), so the residual variance is 0",te="lmer: the minimisation of the criterion over theta did not converge";function Re(t){let n=t.length,e=t.map(o=>o.slice()),r=e.map((o,a)=>e.map((c,s)=>a===s?1:0));for(let o=0;o<100;o++){let a=0,c=0;for(let s=0;s<n;s++)for(let f=0;f<n;f++)c+=e[s][f]**2,s!==f&&(a+=e[s][f]**2);if(a<=1e-32*c)break;for(let s=0;s<n-1;s++)for(let f=s+1;f<n;f++){let h=e[s][f];if(h===0)continue;let p=(e[f][f]-e[s][s])/(2*h),g=Math.sign(p||1)/(Math.abs(p)+Math.sqrt(1+p*p)),A=1/Math.sqrt(1+g*g),i=g*A;for(let u=0;u<n;u++){let l=e[u][s],m=e[u][f];e[u][s]=A*l-i*m,e[u][f]=i*l+A*m}for(let u=0;u<n;u++){let l=e[s][u],m=e[f][u];e[s][u]=A*l-i*m,e[f][u]=i*l+A*m}for(let u=0;u<n;u++){let l=r[u][s],m=r[u][f];r[u][s]=A*l-i*m,r[u][f]=i*l+A*m}}}return{values:e.map((o,a)=>o[a]),vectors:e.map((o,a)=>r.map(c=>c[a]))}}var ee=t=>Math.sqrt(t.reduce((n,e)=>n+e*e,0)),gt=(t,n)=>t.reduce((e,r,o)=>e+r*n[o],0);function uo(t,n,e,r,o){let a=t.length,{values:c,vectors:s}=n,f=s.map(S=>gt(S,t)),h=S=>{let y=new Array(a).fill(0);return S.forEach((k,d)=>s[d].forEach((v,x)=>y[x]+=k*v)),y},p=Math.min(...c);if(p>-r&&c.every((S,y)=>S>r||Math.abs(f[y])<=o)){let S=h(c.map((y,k)=>y>r?-f[k]/y:0));if(ee(S)<=e)return{step:S,newton:!0}}let g=Math.max(0,-p),A=S=>Math.sqrt(c.reduce((y,k,d)=>y+(f[d]/(k+S))**2,0)),i=Math.max(r,1e-12*Math.max(...c.map(Math.abs),1));if(A(g+i)>=e){let S=g+i,y=g+ee(t)/e+Math.max(...c.map(Math.abs))+i;for(let k=0;k<200&&y-S>1e-15*y;k++){let d=(S+y)/2;A(d)>e?S=d:y=d}return{step:h(c.map((k,d)=>-f[d]/(k+y))),newton:!1}}let u=c.map(S=>S+g<=i),l=h(c.map((S,y)=>u[y]?0:-f[y]/(S+g))),m=c.indexOf(p),M=Math.sqrt(Math.max(0,e*e-gt(l,l))),T=-Math.sign(f[m])||1;return{step:l.map((S,y)=>S+T*M*s[m][y]),newton:!1}}function po(t){let n=[];for(let e of t.terms){let r=e.columns.length,o=e.thetaOffset;for(let a=0;a<r;a++){let c=[];for(let s=a;s<r;s++)c.push(o++);n.push(c)}}return n}var ho=(t,n,e)=>{let r=Kt(t,n,e,{gradient:!0});return{f:r.deviance,g:r.gradient}};function Qt(t,n,e,r,o=new Set){let a=t.lower.map(y=>y===0),c=y=>e(t,y,n),s=r.map((y,k)=>o.has(k)?0:y),{f,g:h}=c(s),p=new Set,g=new Set,A=()=>Math.max(1,...s.map(Math.abs)),i=()=>ao*Math.max(1,Math.abs(f)),u=Me*A(),l=1/0,m,M=y=>{let k=ro*A(),d=y.map(v=>{let x=Math.max(no*Math.abs(s[v]),k),b=s.slice();b[v]=s[v]+x;let E=s.slice();E[v]=s[v]-x;let D=c(b).g,R=c(E).g;return y.map(j=>(D[j]-R[j])/(2*x))});return y.map((v,x)=>y.map((b,E)=>(d[x][E]+d[E][x])/2))},T=y=>{let k=m===void 0||m.free.length!==y.length||m.free.some((X,I)=>X!==y[I]),d=k?M(y):m.h;m=void 0;let v=Re(d),x=ke*Math.max(...v.values.map(Math.abs)),b=y.map(X=>h[X]),{step:E,newton:D}=uo(b,v,u,x,i()),R=s.slice();y.forEach((X,I)=>R[X]=s[X]+E[I]);let j=Math.max(...E.map(Math.abs))/A(),tt=c(R);if(!Number.isFinite(tt.f))throw new Error(te);let st=-(gt(b,E)+gt(E,d.map(X=>gt(X,E)))/2),et=Math.abs(f-tt.f)>1e-10*Math.abs(f)?f-tt.f:-(gt(b,E)+gt(y.map(X=>tt.g[X]),E))/2,nt=st>0?et/st:et>=0?1:-1;(nt>.1||j<=Oe)&&(s=R,f=tt.f,h=tt.g);let $=ee(E);if(nt<.25?u=Math.max($/4,Te*A()):nt>.75&&$>=.99*u&&(u*=2),!D)return l=1/0,!1;let Y=y.every(X=>Math.abs(h[X])<=i())&&(j<=Te||k&&j<=Oe&&j>=.5*l);return nt>Fe&&nt<1/Fe&&j<.5*l&&(m={free:y,h:d}),l=j,Y},S=()=>{if(p.size===0)return!1;let y=s.map((D,R)=>R).filter(D=>!o.has(D)),k=Re(M(y)),d=ke*Math.max(...k.values.map(Math.abs)),v=k.values.indexOf(Math.min(...k.values));if(!(k.values[v]<-d))return!1;let x=k.vectors[v],b=y.filter((D,R)=>p.has(D)&&Math.abs(x[R])>1e-8);if(b.length===0)return!1;for(let D of b)p.delete(D),g.add(D);let E=so*A();return y.forEach((D,R)=>s[D]=s[D]+E*x[R]),{f,g:h}=c(s),!0};for(let y=0;y<co;y++){let k=s.map((b,E)=>E).filter(b=>!p.has(b)&&!o.has(b));if(k.length>0&&!T(k))continue;let d=k.filter(b=>a[b]&&!g.has(b)&&Math.abs(s[b])<Ie*A());if(d.length>0){for(let b of d)s[b]=0,p.add(b);({f,g:h}=c(s)),l=1/0,m=void 0;continue}let v=1e-12*Math.max(1,Math.abs(f)),x=[...p].filter(b=>Math.abs(h[b])>v);if(x.length>0)for(let b of x)p.delete(b),g.add(b);else if(!S()){for(let b of po(t))if(s[b[0]]<0)for(let E of b)s[E]=-s[E];return{theta:s.map(b=>b===0?0:b),f}}u=Math.max(u,Me*Ie*A()),l=1/0,m=void 0}throw new Error(te)}function go(t,n,e){let r=t.terms.map(a=>{let c=a.columns.length;return Array.from({length:c*(c+1)/2},(s,f)=>a.thetaOffset+f)}),o=Qt(t,n,e,t.theta);for(let a=0;a<fo;a++){let c;for(let s of r){if(s.every(h=>o.theta[h]===0))continue;let f=Qt(t,n,e,o.theta,new Set(s));(c===void 0||f.f<c.f)&&(c=f)}if(c===void 0||!(c.f<o.f-lo*Math.max(1,Math.abs(o.f))))return o.theta;o=Qt(t,n,e,c.theta)}throw new Error(te)}function wt(t,n,e,r){if(n===void 0)return{};if(typeof n!="object"||n===null||Array.isArray(n))throw new TypeError("options must be an object");let o=Object.keys(n),a=o.find(s=>r.includes(s));if(a!==void 0)throw new Error(`${t}: argument '${a}' is not supported`);let c=o.filter(s=>!e.includes(s));if(c.length>0)throw new TypeError(`unused argument${c.length===1?"":"s"} (${c.join(", ")})`);return n}function vt(t,n){if(t===void 0)return n;if(typeof t=="boolean")return t;if(typeof t=="number"&&!Number.isNaN(t))return t!==0;throw t===null?new TypeError("argument is of length zero"):new TypeError("argument is not interpretable as logical")}var wo=["REML","contrasts","levels"],vo=["weights","offset","subset","naAction","na.action","control","start","verbose","devFunOnly"];function bo(t,n,e){return oe(t,n,e,ho)}var je=new WeakMap;function bt(t){let n=typeof t=="object"&&t!==null?je.get(t):void 0;if(n===void 0)throw new TypeError("fit must be a result returned by lmer (not a copy of one)");return n}function oe(t,n,e,r){let o=wt("lmer",e,wo,vo),a=vt(o.REML,!0);e={...o,REML:a};let c={};e.contrasts!==void 0&&(c.contrasts=e.contrasts),e.levels!==void 0&&(c.levels=e.levels);let s=Se(t,n,c),f=s.y.length,h=s.xColNames.length,p=h===0?[]:we(pt(s.x),s.y),g=s.y.map(($,P)=>$-s.x[P].reduce((Y,X,I)=>Y+X*p[I],0)),A=$=>$.reduce((P,Y)=>Math.max(P,Math.abs(Y)),0);if(A(g)<=io*A(s.y))throw new Error(mo);let i={...s,y:g},u=go(i,a,r),l=Kt(i,u,a,{rxInverse:!0}),m=l.beta.map(($,P)=>$+p[P]),M=2*Math.PI,T=l.logDetL+f*(1+Math.log(M*l.prss/f)),S=l.logDetL+l.logDetRX+(f-h)*(1+Math.log(M*l.prss/(f-h))),y=l.prss/(a?f-h:f),k=Math.sqrt(y),d=l.rxInverse.map($=>$.map(P=>y*P)),v=d.map(($,P)=>Math.sqrt($[P])),x=m.map(($,P)=>$/v[P]),b=i.terms.map($=>{let P=$.columns.length,Y=Array.from({length:P},()=>new Array(P).fill(0)),X=$.thetaOffset;for(let _=0;_<P;_++)for(let V=_;V<P;V++)Y[V][_]=u[X++];let I=Y.map(_=>Y.map(V=>y*_.reduce((K,at,W)=>K+at*V[W],0))),C=I.map((_,V)=>Math.sqrt(_[V])),G=I.map((_,V)=>_.map((K,at)=>V===at?1:K/(C[V]*C[at])));return{group:i.groups[$.group].name,columns:[...$.columns],sd:C,corr:G}}),E=i.groups.map(($,P)=>{let Y=i.terms.filter(X=>X.group===P);return{group:$.name,levels:[...$.levels],columns:Y.flatMap(X=>X.columns),values:$.levels.map((X,I)=>Y.flatMap(C=>{let G=C.columns.length;return C.columns.map((_,V)=>l.b[C.zOffset+I*G+V])}))}}),D=i.x.map($=>$.reduce((P,Y,X)=>P+Y*m[X],0)),R=i.x.map(($,P)=>g[P]-$.reduce((Y,X,I)=>Y+X*l.beta[I],0));i.z.forEach(($,P)=>{let Y=l.b[P];Y!==0&&$.rows.forEach((X,I)=>{D[X]+=$.values[I]*Y,R[X]-=$.values[I]*Y})});let j=-(a?S:T)/2,tt=h+u.length+1,st=u.some(($,P)=>i.lower[P]===0&&$<1e-4),et=[...i.messages];st&&et.push("boundary (singular) fit: see help('isSingular')");let nt={formula:t,REML:a,nObs:f,rows:[...i.rows],theta:u,fixefNames:[...i.xColNames],fixef:m,coefTable:{estimate:[...m],stdError:v,tValue:x},vcov:d,sigma:k,varCorr:b,ranef:E,fitted:D,residuals:R,logLik:j,df:tt,AIC:-2*j+2*tt,BIC:-2*j+Math.log(f)*tt,deviance:T,REMLcrit:S,singular:st,messages:et,warnings:[...i.warnings]};return je.set(nt,{structure:i,beta0:p,data:n,options:e,reml:a,criterion:r,theta:[...u]}),nt}function _e(t,n,e){let r=new Float64Array(t.zz.length),o=new Float64Array(64);for(let a=0;a<t.nGroups;a++){let c=t.groupTerm[a],s=t.termNc[c],f=e[c];for(let h=t.blockStart[a];h<t.blockStart[a+1];h++){let p=t.groupTerm[t.blockRow[h]],g=t.termNc[p],A=n[p],i=t.blockValueStart[h];o.length<g*s&&(o=new Float64Array(g*s));for(let u=0;u<s;u++)for(let l=0;l<g;l++){let m=0;for(let M=0;M<s;M++)m+=t.zz[i+l+M*g]*f[M+u*s];o[l+u*g]=m}for(let u=0;u<s;u++)for(let l=0;l<g;l++){let m=0;for(let M=0;M<g;M++)m+=A[M+l*g]*o[M+u*g];r[i+l+u*g]=m}}}return r}function Tt(t,n,e){let r=new Float64Array(e*e);for(let o=0;o<e;o++)for(let a=0;a<e;a++){let c=n[a+o*e];if(c!==0)for(let s=0;s<e;s++)r[s+o*e]+=t[s+a*e]*c}return r}var ne=(t,n)=>{let e=0;for(let r=0;r<n;r++)e+=t[r+r*n];return e},re=(t,n)=>Array.from({length:n},(e,r)=>Array.from({length:n},(o,a)=>t[r+a*n]));function Le(t,n,e){let r=Zt(t,n),{sys:o,t:a,l:c,rx:s,ws:f,xResiduals:h,res:p,uv:g}=r,{n:A,p:i,q:u}=o,l=n.length,m=l+1,M=N=>{let z=new Float64Array(A);for(let F=0;F<A;F++){let Z=0;for(let J=o.rowStart[F];J<o.rowStart[F+1];J++)Z+=o.rowValues[J]*N[o.rowCols[J]];z[F]=Z}return z},T=N=>{let z=new Float64Array(u);for(let F=0;F<A;F++){let Z=N[F];if(Z!==0)for(let J=o.rowStart[F];J<o.rowStart[F+1];J++)z[o.rowCols[J]]+=o.rowValues[J]*Z}return z},S=N=>{let z=new Float64Array(i);for(let F=0;F<A;F++){let Z=t.x[F],J=N[F];for(let U=0;U<i;U++)z[U]+=Z[U]*J}return z},y=N=>{let z=new Float64Array(A);for(let F=0;F<A;F++){let Z=t.x[F],J=0;for(let U=0;U<i;U++)J+=Z[U]*N[U];z[F]=J}return z},k=N=>{Mt(o,c,N),kt(o,c,N)},d=Array.from({length:l},()=>[]),v=Array.from({length:l},()=>[]);for(let{row:N,col:z,theta:F}of t.lambda)d[F].push(N),v[F].push(z);let x=(N,z)=>{let F=new Float64Array(u),Z=d[N],J=v[N];for(let U=0;U<Z.length;U++)F[Z[U]]+=z[J[U]];return F},b=(N,z)=>{let F=new Float64Array(u),Z=d[N],J=v[N];for(let U=0;U<Z.length;U++)F[J[U]]+=z[Z[U]];return F},E=(N,z,F)=>{let Z=d[N],J=v[N],U=0;for(let rt=0;rt<Z.length;rt++)U+=z[Z[rt]]*F[J[rt]];return U},D=(N,z,F,Z,J)=>{for(let U=0;U<i;U++)for(let rt=0;rt<i;rt++){let lt=J*E(N,z[U],F[rt]);Z[U+rt*i]+=lt,Z[rt+U*i]+=lt}},R=0;for(let N of p)R+=N*N;let j=0;for(let N of g)j+=N*N;let tt=R+j,st=e?A-i:A,et=tt/st,nt=Math.sqrt(et),$=0;for(let N=0;N<u;N++)$+=Math.log(c[o.colStart[N]]);$*=2;let P=0;for(let N=0;N<i;N++)P+=Math.log(s[N+N*i]);P*=2;let X=(e?$+P:$)+tt/et+st*Math.log(2*Math.PI*et),I=Yt(s,i),C=Jt(o,c),G=T(p),_=h.map(T),V=It(o,C,a),K=new Array(l).fill(0),at=(N,z,F)=>{o.termTheta.forEach((Z,J)=>{let U=o.termNc[J];for(let rt=0;rt<U;rt++)for(let lt=rt;lt<U;lt++)z[Z[lt+rt*U]]+=F*N[J][rt+lt*U]})};at(V,K,2);let W=Array.from({length:l},(N,z)=>-2*E(z,G,g)),B=Array.from({length:l},(N,z)=>{let F=new Float64Array(i*i);return D(z,_,f,F,-1),F});if(e)for(let N=0;N<l;N++)K[N]+=ne(Tt(I,B[N],i),i);let ot=B.map(N=>Tt(I,N,i)),w=Array.from({length:m},()=>new Array(m).fill(0));for(let N=0;N<l;N++){let z=o.termTheta.map(H=>Float64Array.from(H,ut=>ut===N?1:0)),F=_e(o,z,a),Z=_e(o,a,z);for(let H=0;H<F.length;H++)F[H]+=Z[H];let J=Ne(o,c,F),U=Ee(o,c,J,C),rt=new Array(l).fill(0),lt=It(o,U,a),Rt=It(o,C,z);if(at(lt,rt,2),at(Rt,rt,2),e&&i>0){let H=[],ut=[];for(let ct=0;ct<i;ct++){let it=b(N,_[ct]),Q=ht(o,a,T(M(x(N,f[ct]))));for(let ft=0;ft<u;ft++)it[ft]-=Q[ft];k(it),H.push(it);let ie=x(N,f[ct]),Be=dt(o,a,it);for(let ft=0;ft<u;ft++)ie[ft]+=Be[ft];let Wt=M(ie);for(let ft=0;ft<A;ft++)Wt[ft]=-Wt[ft];ut.push(T(Wt))}for(let ct=0;ct<l;ct++){let it=new Float64Array(i*i);D(ct,ut,f,it,-1),D(ct,_,H,it,-1),rt[ct]+=ne(Tt(I,it,i),i)-ne(Tt(ot[ct],ot[N],i),i)}}let mt=M(x(N,g)),Ut=b(N,G),Ue=ht(o,a,T(mt));for(let H=0;H<u;H++)Ut[H]-=Ue[H];let jt=S(mt);for(let H=0;H<i;H++)jt[H]=-jt[H];let yt=Ut.slice(),_t=jt.slice();r.solveNormal(yt,_t);{let H=M(dt(o,a,yt)),ut=y(_t);for(let Q=0;Q<A;Q++)H[Q]+=ut[Q];let ct=ht(o,a,T(H)),it=S(H);for(let Q=0;Q<u;Q++)ct[Q]=Ut[Q]-ct[Q]-yt[Q];for(let Q=0;Q<i;Q++)it[Q]=jt[Q]-it[Q];r.solveNormal(ct,it);for(let Q=0;Q<u;Q++)yt[Q]+=ct[Q];for(let Q=0;Q<i;Q++)_t[Q]+=it[Q]}let Ht=M(dt(o,a,yt)),He=y(_t);for(let H=0;H<A;H++)Ht[H]=-(mt[H]+Ht[H]+He[H]);let We=T(Ht);for(let H=0;H<l;H++){let ut=-2*(E(H,We,g)+E(H,G,yt));w[H][N]=rt[H]+ut/et}}for(let N=0;N<l;N++){for(let F=N+1;F<l;F++){let Z=(w[N][F]+w[F][N])/2;w[N][F]=Z,w[F][N]=Z}let z=-2*W[N]/(et*nt);w[N][l]=z,w[l][N]=z}w[l][l]=6*tt/(et*et)-2*st/et;let O=[...K.map((N,z)=>N+W[z]/et),-2*tt/(et*nt)+2*st/nt],q=re(I.map(N=>et*N),i),L=[...B.map((N,z)=>re(Tt(ot[z],I,i).map(F=>-et*F),i)),re(I.map(N=>2*nt*N),i)];return{varpar:[...n,nt],deviance:X,gradient:O,phiGradient:K,prssGradient:W,prss:tt,hessian:w,beta:Array.from(r.beta),vcov:q,jacobian:L}}var se=1e-8,yo=Math.sqrt(Number.EPSILON),xo=1e-8,ze=new WeakMap;function qe(t){if(!Number.isFinite(t))return Number.isNaN(t)?"nan":t>0?"inf":"-inf";let[n,e]=t.toExponential(1).split("e"),r=Number(e);return`${n}e${r<0?"-":"+"}${String(Math.abs(r)).padStart(2,"0")}`}function Ao(t){let n=[],e=t.filter(a=>a<-se),r=t.filter(a=>Math.abs(a)<se),o=a=>a===1?"eigenvalue":"eigenvalues";return e.length>0&&n.push(`Model failed to converge with ${e.length} negative ${o(e.length)}: ${e.map(qe).join(" ")}`),r.length>0&&n.push(`Model may not have converged with ${r.length} ${o(r.length)} close to zero: ${r.map(qe).join(" ")}`),n}function Ft(t){let n=ze.get(t);if(n!==void 0)return n;let e=bt(t),r=No(e.structure,e.theta,e.reml,e.beta0);return ze.set(t,r),r}function No(t,n,e,r=[]){let o=Le(t,n,e),a=o.varpar.length,c=Xt(o.hessian,{symmetric:!0}),s=c.values,f=c.vectors,h=Array.from({length:a},()=>new Array(a).fill(0));return s.forEach((p,g)=>{if(p>se)for(let A=0;A<a;A++){let i=2*f[A][g]/p;for(let u=0;u<a;u++)h[A][u]+=i*f[u][g]}}),{varpar:o.varpar,hessian:o.hessian,hessianEigenvalues:s,vcovVarpar:h,beta:o.beta.map((p,g)=>p+(r[g]??0)),sigma:o.varpar[a-1],vcov:o.vcov,jacobian:o.jacobian,warnings:Ao(s)}}var Ot=(t,n)=>{let e=0;for(let r=0;r<t.length;r++){let o=t[r];if(o===0)continue;let a=n[r];for(let c=0;c<t.length;c++)e+=o*a[c]*t[c]}return e},Vt=(t,n)=>t.reduce((e,r,o)=>e+r*n[o],0);function Dt(t,n,e,r=t.beta){if(n.some(Number.isNaN)||Number.isNaN(e))return{estimate:NaN,stdError:NaN,df:NaN,tValue:NaN,pValue:NaN};let o=Vt(n,r),a=Ot(n,t.vcov),c=Math.sqrt(a),s=t.jacobian.map(g=>Ot(n,g)),f=2*a*a/Ot(s,t.vcovVarpar),h=(o-e)/c,p=2*pe(Math.abs(h),f,!1,!1);return{estimate:o,stdError:c,df:f,tValue:h,pValue:p}}function Ct(t,n,e,r){let o=t.sigma*t.sigma;return{sumSq:n*o*e,meanSq:n*o,numDF:e,denDF:r,F:n,pValue:ue(n,e,r,!1,!1)}}function Eo(t){if(t.length===1)return t[0];if(t.every((e,r)=>r===0||Math.abs(e-t[r-1])<xo))return t.reduce((e,r)=>e+r,0)/t.length;if(t.some(e=>e<=2))return 2;let n=t.reduce((e,r)=>e+r/(r-2),0);return 2*n/(n-t.length)}function So(t,n,e,r){return ae(Ft(t),n,e,r)}function ae(t,n,e,r){let o=n.length;if(o===0)return;let a=e??new Array(o).fill(0);if(o===1){let m=Dt(t,n[0],a[0]);return Ct(t,m.tValue*m.tValue,1,m.df)}if(n.some(m=>m.some(Number.isNaN))||a.some(Number.isNaN))return Ct(t,NaN,NaN,NaN);let c=n.map(m=>t.vcov.map((M,T)=>m.reduce((S,y,k)=>S+y*t.vcov[k][T],0))),s=Xt(c.map(m=>n.map(M=>Vt(m,M))),{symmetric:!0}),f=s.values,h=Math.max(yo*f[0],0),p=0;for(;p<o&&f[p]>h;)p++;if(p===0)return;p<o&&a.some(m=>m!==0)&&r.add("Contrast is rank deficient and test may be affected");let g=t.beta;if(a.some(m=>m!==0)){let m=qt(n.map(M=>[...M]));g=t.beta.map((M,T)=>M-Vt(m[T],a))}let A=t.beta.length,i=Array.from({length:p},(m,M)=>Array.from({length:A},(T,S)=>n.reduce((y,k,d)=>y+s.vectors[d][M]*k[S],0)));if(p===1){let m=Dt(t,i[0],0,g);return Ct(t,m.tValue*m.tValue,1,m.df)}let u=0,l=[];return i.forEach((m,M)=>{u+=Vt(m,g)**2/f[M];let T=t.jacobian.map(S=>Ot(m,S));l.push(2*f[M]*f[M]/Ot(T,t.vcovVarpar))}),Ct(t,u/p,p,Eo(l))}function Mo(t,n){let e=wt("lmerSummary",n,["ddf"],[]).ddf??"Satterthwaite";if(e!=="Satterthwaite"&&e!=="lme4")throw new RangeError(`ddf must be "Satterthwaite" or "lme4", not ${JSON.stringify(e)}`);let o=[...bt(t).structure.xColNames];if(e==="lme4"){let{estimate:f,stdError:h,tValue:p}=t.coefTable;return{ddf:e,names:o,estimate:[...f],stdError:[...h],tValue:[...p],messages:[],warnings:[]}}let a=Ft(t),c=o.length,s=o.map((f,h)=>Dt(a,Array.from({length:c},(p,g)=>g===h?1:0),0));return{ddf:e,names:o,estimate:s.map(f=>f.estimate),stdError:s.map(f=>f.stdError),df:s.map(f=>f.df),tValue:s.map(f=>f.tValue),pValue:s.map(f=>f.pValue),messages:[],warnings:[...a.warnings]}}var ko=["rhs","joint","confint","level"],Io=["collect","checkEstimability","check_estimability","ddf"];function To(t,n){if(!Array.isArray(t))throw new TypeError("L must be an array of numbers or an array of rows");let e=t.length>0&&Array.isArray(t[0])?t.map(r=>[...r]):[[...t]];for(let r of e){if(!Array.isArray(r)||r.some(o=>typeof o!="number"))throw new TypeError("L must be an array of numbers or an array of rows");if(r.length!==n)throw new TypeError(`each contrast must have ${n} values (one per fixed effect), not ${r.length}`)}return e}function Oo(t,n,e){e=wt("lmerContest",e,ko,Io);let r=vt(e.joint,!0),o=vt(e.confint,!0),c=bt(t).structure.xColNames.length,s=To(n,c),f=e.level??.95;if(typeof f!="number"||!(f>=0&&f<=1))throw new RangeError("level must be a number between 0 and 1");let h=e.rhs??0,p=typeof h=="number"?new Array(s.length).fill(h):[...h];if(p.length!==s.length||p.some(l=>typeof l!="number"))throw new TypeError(`rhs must be a number or have one value per contrast (${s.length})`);let g=new St,A=Ft(t);if(A.warnings.forEach(l=>g.add(l)),r){let l=c===0?void 0:So(t,s,p,g),m=M=>l===void 0?[]:[l[M]];return{joint:!0,sumSq:m("sumSq"),meanSq:m("meanSq"),numDF:m("numDF"),denDF:m("denDF"),F:m("F"),pValue:m("pValue"),messages:[],warnings:g.list}}let i=c===0?[]:s.map((l,m)=>Dt(A,l,p[m])),u={joint:!1,estimate:i.map(l=>l.estimate),stdError:i.map(l=>l.stdError),df:i.map(l=>l.df),tValue:i.map(l=>l.tValue),pValue:i.map(l=>l.pValue),messages:[],warnings:g.list};if(o){let l=i.map(m=>he((1-f)/2,m.df,!1,!1));u.lower=i.map((m,M)=>m.estimate-m.stdError*l[M]),u.upper=i.map((m,M)=>m.estimate+m.stdError*l[M])}return u}var Fo=Math.log10(2);function Ro(t){let n=new DataView(new ArrayBuffer(8));n.setFloat64(0,t);let e=n.getUint16(0)>>4&2047;if(e>0)return e-1023;let r=n.getUint32(0)&1048575,o=n.getUint32(4);return(r!==0?32+(31-Math.clz32(r)):31-Math.clz32(o))-1074}function $t(t){let n=1;for(let e=0;e<t;e++)n*=10;return n}function jo(t){let n=Math.floor(t),e=t-n,r=e<.5?n:e>.5?n+1:n%2===0?n:n+1;return r===0?t<0?-0:0:r}function _o(t,n=0){if(Number.isNaN(t)||Number.isNaN(n))return NaN;if(!Number.isFinite(t)||t===0||n>323)return t;if(n<-308)return 0;if(n===0)return jo(t);let e=Math.floor(n+.5),r=Math.abs(t);if(Fo*(.5+Ro(r))+e>15)return t;let a,c,s;if(e<=308){let g=e>=0?$t(e):1/$t(-e),A=r*g;s=Math.floor(A),a=s/g,c=Math.ceil(A)/g}else{let g=$t(308),A=$t(e-308),i=r*g*A;s=Math.floor(i),a=s/g/A,c=Math.ceil(i)/g/A}let f=c-r,h=r-a,p=f<h||f===h&&s%2===1?c:a;return t<0?-p:p}function Pt(t,n=7){let e=0;for(let o of t)for(let a of o)Math.abs(a)>e&&(e=Math.abs(a));let r=e>0?Math.max(0,n-Math.log10(e)):n;return t.map(o=>o.map(a=>_o(a,r)))}var le={type:"num",value:1,text:"1"};function Ve(t,n,e={}){let r=Lt(t,!1,!0);if(r.response===void 0)throw new TypeError("the formula has no response");if(typeof n!="object"||n===null)throw new TypeError("data must be an object of columns");let{fixed:o}=zt(r.rhs),a=o??le,c=Et(r,Object.keys(n),a).variables,s=e.contrasts===void 0?{}:{contrasts:e.contrasts},f=xt({response:r.response,rhs:a},n,{...e.levels===void 0?{}:{levels:e.levels},dropUnusedLevels:!0,...s,extraVariables:c},a),{terms:h,frame:p}=f,g=f.colNames.map((l,m)=>m);if(g.length>0){let l=pt(f.x),m=new Set(l.pivot.slice(l.rank));g=g.filter(M=>!m.has(M))}let A=new Set(h.terms.flat()),i=h.variables.map((l,m)=>({name:l,v:m})).filter(({v:l})=>A.has(l)),u={};for(let{name:l,v:m}of i)p.variables[m].kind==="factor"&&(u[l]=e.contrasts?.[l]??"contr.treatment");return{x:f.x.map(l=>g.map(m=>l[m])),colNames:g.map(l=>f.colNames[l]),assign:g.map(l=>f.assign[l]),intercept:h.intercept,terms:h.terms.map((l,m)=>({label:h.termLabels[m],variables:l.map(M=>h.variables[M])})),variables:i.map(({name:l,v:m})=>({name:l,frame:p.variables[m]})),contrasts:u}}var fe=(t,n=t[0]?.length??0)=>Array.from({length:n},(e,r)=>t.map(o=>o[r]));function ce(t,n,e=n[0]?.length??0){return t.map(r=>{let o=new Array(e).fill(0);return r.forEach((a,c)=>{let s=n[c];for(let f=0;f<e;f++)o[f]+=a*s[f]}),o})}function De(t,n){let e=Array.from({length:n},()=>new Array(n).fill(0));for(let r=0;r<n;r++)for(let o=r;o<n;o++){let a=0;for(let c of t)a+=c[r]*c[o];e[r][o]=a,e[o][r]=a}return e}var Gt=(t,n)=>t.map(e=>n.map(r=>e[r])),Ce=1e-6;function $e(t){let n=t.length,e=Array.from({length:n},(o,a)=>Array.from({length:n},(c,s)=>a===s?1:0)),r=Array.from({length:n},()=>new Array(n).fill(0));for(let o=0;o<n;o++){for(let c=0;c<n;c++){let s=t[o][c];for(let f=0;f<o;f++)s-=e[o][f]*r[f][c];r[o][c]=s}let a=r[o][o];for(let c=o+1;c<n;c++){if(Math.abs(a)<Ce){e[c][o]=0;continue}let s=t[c][o];for(let f=0;f<o;f++)s-=e[c][f]*r[f][o];e[c][o]=s/a}}for(let o of[e,r])for(let a of o)a.forEach((c,s)=>{Math.abs(c)<Ce&&(a[s]=0)});return{L:e,U:r}}function Pe(t){let n=new Set(t.variables.filter(r=>r.frame.kind==="numeric").map(r=>r.name)),e=r=>r.variables.filter(o=>n.has(o)).sort().join("\0");return t.terms.map(r=>t.terms.map((o,a)=>({b:o,k:a})).filter(({b:o})=>r.variables.every(a=>o.variables.includes(a))&&o.variables.length>r.variables.length&&e(r)===e(o)).map(({k:o})=>o))}var At=(t,n)=>t.flatMap((e,r)=>e===n+1?[r]:[]);function Lo(t,n,e,r,o){return{term:t,rows:e.map(a=>r[a]),columns:[...o],values:e.map(a=>[...n[a]])}}function zo(t){let n=t.colNames.length;if(n===0||n===1&&t.intercept)return[];let e=n===1?[[1]]:fe($e(De(t.x,n)).L,n);return t.terms.map((r,o)=>Lo(r.label,e,At(t.assign,o),t.colNames,t.colNames))}function qo(t){let n=t.colNames.length,e=Pe(t);return t.terms.map((r,o)=>{let a=At(t.assign,o);if(e[o].length===0){let i=a.map(u=>Array.from({length:n},(l,m)=>u===m?1:0));return{term:r.label,rows:a.map(u=>t.colNames[u]),columns:[...t.colNames],values:i}}let c=[o,...e[o]].flatMap(i=>At(t.assign,i)),s=new Set(c),f=[...t.colNames.map((i,u)=>u).filter(i=>!s.has(i)),...c],h=fe($e(De(Gt(t.x,f),n)).L,n),p=new Set(a),g=f.flatMap((i,u)=>p.has(i)?[u]:[]),A=g.map(i=>{let u=new Array(n).fill(0);return f.forEach((l,m)=>u[l]=h[i][m]),u});return{term:r.label,rows:g.map(i=>t.colNames[f[i]]),columns:[...t.colNames],values:A}})}function Co(t){let n=t.x.length,e=new Map(t.variables.map(f=>[f.name,f.frame])),r=[],o=[];t.intercept&&(r.push(new Array(n).fill(1)),o.push("(Intercept)"));let a=f=>{let h=e.get(f);return h.kind==="numeric"?[{name:f,values:h.values}]:h.levels.map((p,g)=>({name:f+p,values:h.codes.map(A=>A===g?1:0)}))};for(let f of t.terms){let h=a(f.variables[0]);for(let p of f.variables.slice(1)){let g=[];for(let A of a(p))for(let i of h)g.push({name:`${i.name}:${A.name}`,values:i.values.map((u,l)=>u*A.values[l])});h=g}for(let p of h)r.push(p.values),o.push(p.name)}let c=o.filter((f,h)=>r[h].every(p=>p===0)),s=c.length>0?[`Missing cells for: ${c.join(", ")}.
|
|
4
|
+
Interpret type III hypotheses with care.`]:[];return{x:Array.from({length:n},(f,h)=>r.map(p=>p[h])),colNames:o,messages:s}}function Vo(t){let n={},e={};for(let{name:h,frame:p}of t.variables)p.kind==="numeric"?n[h]=p.values:(n[h]=p.codes.map(g=>p.levels[g]),e[h]=p.levels);let r=h=>h.reduce((p,g)=>p===void 0?g:{type:"binary",op:"+",left:p,right:g},void 0),o=h=>({type:"var",name:h}),a={response:void 0,rhs:r(t.variables.map(h=>o(h.name)))??le},c=t.terms.map(h=>h.variables.map(o).reduce((p,g)=>({type:"binary",op:":",left:p,right:g}))),s=xt(a,n,{levels:e,intercept:t.intercept},r(c)??le),f=s.colNames.map((h,p)=>p);if(f.length>0){let h=pt(s.x);h.rank<f.length&&(f=h.pivot.slice(0,h.rank))}return{x:Gt(s.x,f),colNames:f.map(h=>s.colNames[h]),assign:f.map(h=>s.assign[h])}}var Do=1e-8;function $o(t){let n=Object.values(t.contrasts).every(i=>i==="contr.treatment"),e=n?{x:t.x,colNames:t.colNames,assign:t.assign}:Vo(t),r=e.colNames.length,o=Co(t),a=qt(e.x),c=Pt(fe(ce(a,o.x,o.colNames.length),o.colNames.length)),s=Pe(t),f=t.terms.map((i,u)=>({k:u,order:i.variables.length})).sort((i,u)=>u.order-i.order||i.k-u.k);for(let{k:i}of f){if(s[i].length===0)continue;let u=At(e.assign,i);if(u.length===0)continue;let l=s[i].flatMap(T=>At(e.assign,T)),m=Gt(c,u);l.length>0&&(m=ve(pt(Gt(c,l)),m));let M=Pt(m);c.forEach((T,S)=>u.forEach((y,k)=>T[y]=M[S][k]))}let h=new Map(o.colNames.map((i,u)=>[i,u])),p=e.colNames.map(i=>{let u=h.get(i);if(u===void 0)throw new Error(`lmerHypotheses: the column '${i}' of the model matrix is not a column of the full-dummy matrix`);return c[u]}),g=n?void 0:Pt(ce(a,t.x,t.colNames.length));return{hypotheses:t.terms.map((i,u)=>{let l=At(e.assign,u).filter(M=>p.reduce((T,S)=>T+Math.abs(S[M]),0)>Do),m=l.map(M=>p.map(T=>T[M]));return{term:i.label,rows:l.map(M=>e.colNames[M]),columns:[...t.colNames],values:g===void 0?m:ce(m,g,t.colNames.length)}}),messages:o.messages}}function Ge(t,n="III"){return t.colNames.length<=1||t.terms.length<=1||n==="I"?{hypotheses:zo(t),messages:[]}:n==="II"?{hypotheses:qo(t),messages:[]}:$o(t)}var Po={I:"I",II:"II",III:"III",1:"I",2:"II",3:"III"};function Go(t,n){let{structure:e,data:r,options:o}=bt(t),a={};o.contrasts!==void 0&&(a.contrasts=o.contrasts),o.levels!==void 0&&(a.levels=o.levels);let c=Ve(t.formula,r,a);if(c.colNames.length!==e.xColNames.length||c.colNames.some((s,f)=>s!==e.xColNames[f]))throw new Error("lmerAnova: the columns of the hypotheses differ from the fit's fixed effects");return c.terms.length===0?{hypotheses:{hypotheses:[],messages:[]},termLabels:[]}:{hypotheses:Ge(c,n),termLabels:c.terms.map(s=>s.label)}}function Uo(t,{hypotheses:n},e,r){let o=[],a=[];for(let c of n){let s=c.values.length===0?void 0:ae(t,c.values,void 0,r);s!==void 0&&(o.push(c.term),a.push(s))}for(let c of e)o.includes(c)||(o.push(c),a.push({sumSq:NaN,meanSq:NaN,numDF:NaN,denDF:NaN,F:NaN,pValue:NaN}));return{terms:o,rows:a}}function Ho(t,n){let e=Po[String(n.type??"III")];if(e===void 0)throw new RangeError(`type must be "I", "II" or "III", not ${JSON.stringify(n.type)}`);let{hypotheses:r,termLabels:o}=Go(t,e),a=Ft(t),c=new St;a.warnings.forEach(p=>c.add(p));let{terms:s,rows:f}=Uo(a,r,o,c),h=p=>f.map(g=>g[p]);return{type:e,terms:s,sumSq:h("sumSq"),meanSq:h("meanSq"),numDF:h("numDF"),denDF:h("denDF"),F:h("F"),pValue:h("pValue"),messages:[...r.messages],warnings:c.list}}function Wo(t,n){if(t.length<2)throw new TypeError("lmerAnova needs one fit, or an array of at least two fits to compare");let e=t.map(bt);if(t.some(l=>l.nObs!==t[0].nObs))throw new Error("models were not all fitted to the same size of dataset");if(e.some(l=>l.data!==e[0].data))throw new Error("all models must be fit to the same data object");let r=new St,o=[],a=t.map((l,m)=>`MODEL${m+1}`);if(n.names!==void 0){if(n.names.length!==t.length||n.names.some(l=>typeof l!="string"))throw new TypeError(`names must be ${t.length} strings, one per fit`);new Set(n.names).size===n.names.length?a=[...n.names]:r.add("failed to find model names, assigning generic names")}let c=vt(n.refit,!0),s;c?(t.some(l=>l.REML)&&o.push("refitting model(s) with ML (instead of REML)"),s=t.map((l,m)=>{if(!l.REML)return l.logLik;let{data:M,options:T,criterion:S}=e[m];return oe(l.formula,M,{...T,REML:!1},S).logLik})):(t.some(l=>l.REML)&&t.some(l=>!l.REML)&&r.add("some models fit with REML = TRUE, some not"),s=t.map(l=>l.logLik));let f=t.map((l,m)=>m).sort((l,m)=>t[l].df-t[m].df||l-m),h=t[0].nObs,p=f.map(l=>t[l].df),g=f.map(l=>s[l]),A=g.map((l,m)=>m===0?NaN:2*Math.max(0,l-g[m-1])),i=p.map((l,m)=>m===0?NaN:l-p[m-1]),u=A.map((l,m)=>m===0||i[m]===0?NaN:me(l,i[m]/2,2,!1,!1));return{names:f.map(l=>a[l]),npar:p,AIC:g.map((l,m)=>-2*l+2*p[m]),BIC:g.map((l,m)=>-2*l+Math.log(h)*p[m]),logLik:g,deviance:g.map(l=>-2*l),Chisq:A,Df:i,pValue:u,messages:o,warnings:r.list}}function Bo(t,n){if(Array.isArray(t)){let e=wt("lmerAnova",n,["refit","names"],["modelNames","model.names"]);return Wo(t,{...e,refit:vt(e.refit,!0)})}return Ho(t,wt("lmerAnova",n,["type"],["ddf"]))}export{bo as lmer,Bo as lmerAnova,Oo as lmerContest,Mo as lmerSummary};
|
package/dist/models/glm.js
CHANGED
|
@@ -53,6 +53,8 @@ export function irls(inp, warnings) {
|
|
|
53
53
|
deviance: totalDeviance(fam, y, mu, w),
|
|
54
54
|
iter: 0,
|
|
55
55
|
converged: true,
|
|
56
|
+
// (true, not a mistake: R's glm.fit marks the empty model both converged and at the boundary; the warning
|
|
57
|
+
// "algorithm stopped at boundary value" is still not given, see finalWarnings)
|
|
56
58
|
boundary: true,
|
|
57
59
|
rank: 0,
|
|
58
60
|
qr: undefined,
|
|
@@ -340,10 +342,10 @@ export function glm(x, y, options = {}) {
|
|
|
340
342
|
swy += w[i] * yy[i];
|
|
341
343
|
}
|
|
342
344
|
const m = swy / sw;
|
|
343
|
-
nullDeviance = totalDeviance(fam, yy, yy.map(() => m), w);
|
|
345
|
+
nullDeviance = withWarnings(warnings, () => totalDeviance(fam, yy, yy.map(() => m), w));
|
|
344
346
|
}
|
|
345
347
|
else
|
|
346
|
-
nullDeviance =
|
|
348
|
+
nullDeviance = withWarnings(warnings, () => totalDeviance(fam, yy, offset.map((o) => fam.linkinv(o)), w));
|
|
347
349
|
const nOk = n - w.filter((v) => v === 0).length;
|
|
348
350
|
const aic = familyAic(fam, yy, fit.mu, w, size, fit.deviance, warnings) + 2 * fit.rank;
|
|
349
351
|
return {
|
package/dist/models/glmFamily.js
CHANGED
|
@@ -49,6 +49,15 @@ function sqrtWarn(x) {
|
|
|
49
49
|
currentWarnings?.add("NaNs produced");
|
|
50
50
|
return Math.sqrt(x);
|
|
51
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Logarithm that warns as R's log does when it produces NaN (a negative argument, -Inf included; NaN does not warn).
|
|
54
|
+
* Used by the deviance contributions that R computes in R code (poisson, quasipoisson, Gamma, quasi "mu" and "mu^2")
|
|
55
|
+
*/
|
|
56
|
+
function logWarn(x) {
|
|
57
|
+
if (x < 0)
|
|
58
|
+
currentWarnings?.add("NaNs produced");
|
|
59
|
+
return Math.log(x);
|
|
60
|
+
}
|
|
52
61
|
/** A number as C's printf("%g") writes it: 6 significant digits, fixed or scientific, trailing zeros dropped */
|
|
53
62
|
function formatG(v) {
|
|
54
63
|
if (!Number.isFinite(v))
|
|
@@ -175,13 +184,14 @@ function quasiVariance(v) {
|
|
|
175
184
|
return {
|
|
176
185
|
variance: (m) => m,
|
|
177
186
|
muOk: (m) => m > 0,
|
|
178
|
-
dev: (y, mu, w) => 2 * w * ((y === 0 ? 0 : y *
|
|
187
|
+
dev: (y, mu, w) => 2 * w * ((y === 0 ? 0 : y * logWarn(y / mu)) - (y - mu)),
|
|
179
188
|
};
|
|
180
189
|
case "mu^2":
|
|
181
190
|
return {
|
|
182
191
|
variance: (m) => m * m,
|
|
183
192
|
muOk: (m) => m > 0,
|
|
184
|
-
|
|
193
|
+
// log(y/mu) for every y != 0 (NaN for a negative y, as in R), log(1/mu) for y = 0
|
|
194
|
+
dev: (y, mu, w) => Math.max(-2 * w * (logWarn(y !== 0 ? y / mu : 1 / mu) - (y - mu) / mu), 0),
|
|
185
195
|
};
|
|
186
196
|
case "mu^3":
|
|
187
197
|
return { variance: (m) => m * m * m, muOk: (m) => m > 0, dev: inverseGaussianDev };
|
|
@@ -189,6 +199,7 @@ function quasiVariance(v) {
|
|
|
189
199
|
throw new RangeError(`'variance' "${String(v)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`);
|
|
190
200
|
}
|
|
191
201
|
}
|
|
202
|
+
/** (R computes this deviance in compiled code, so a mean outside (0, 1) gives NaN without "NaNs produced") */
|
|
192
203
|
function binomialDev(y, mu, w) {
|
|
193
204
|
const a = y === 0 ? 0 : y * Math.log(y / mu);
|
|
194
205
|
const b = y === 1 ? 0 : (1 - y) * Math.log((1 - y) / (1 - mu));
|
|
@@ -243,14 +254,15 @@ export function makeFamily(family, link) {
|
|
|
243
254
|
fixedDispersion: name === "poisson",
|
|
244
255
|
variance: (m) => m,
|
|
245
256
|
muOk: (m) => Number.isFinite(m) && m > 0,
|
|
246
|
-
|
|
257
|
+
// (a row with y = 0 takes no logarithm, so a negative mean there does not warn, as in R)
|
|
258
|
+
devContribution: (y, mu, w) => (y > 0 ? 2 * w * (y * logWarn(y / mu) - (y - mu)) : 2 * w * mu),
|
|
247
259
|
};
|
|
248
260
|
case "Gamma":
|
|
249
261
|
return {
|
|
250
262
|
...base,
|
|
251
263
|
variance: (m) => m * m,
|
|
252
264
|
muOk: (m) => Number.isFinite(m) && m > 0,
|
|
253
|
-
devContribution: (y, mu, w) => -2 * w * ((y === 0 ? 0 :
|
|
265
|
+
devContribution: (y, mu, w) => -2 * w * ((y === 0 ? 0 : logWarn(y / mu)) - (y - mu) / mu),
|
|
254
266
|
};
|
|
255
267
|
case "inverse.gaussian":
|
|
256
268
|
return { ...base, variance: (m) => m * m * m, muOk: () => true, devContribution: inverseGaussianDev };
|
package/dist/models/lmer.d.ts
CHANGED
|
@@ -2,19 +2,33 @@ import type { DataFrame } from "./modelFrame.js";
|
|
|
2
2
|
import type { ContrastName } from "./modelMatrix.js";
|
|
3
3
|
import { type MixedModelStructure } from "./mixedStructure.js";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* A contrast of a factor of lmer's fixed effects: R's `contr.treatment`, `contr.sum`, `contr.helmert` or `contr.poly`
|
|
6
|
+
* (orthogonal polynomials in the level scores 1, 2, ..., columns `.L`, `.Q`, `.C`, `^4`, ...).
|
|
7
|
+
*
|
|
8
|
+
* @category Mixed models
|
|
9
|
+
*/
|
|
10
|
+
export type LmerContrastName = ContrastName | "contr.poly";
|
|
11
|
+
/**
|
|
12
|
+
* Options of {@link lmer}. Only the keys below are accepted: R's `weights`, `offset`, `subset`, `naAction`,
|
|
13
|
+
* `na.action`, `control`, `start`, `verbose` and `devFunOnly` give the Error `lmer: argument '<key>' is not
|
|
14
|
+
* supported`, any other key the TypeError `unused argument (<key>)` (a key whose value is `undefined` included).
|
|
6
15
|
*
|
|
7
16
|
* @category Mixed models
|
|
8
17
|
*/
|
|
9
18
|
export interface LmerOptions {
|
|
10
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Whether to minimise the REML criterion (default `true`) or the ML deviance (`false`). As in R's conditions, a
|
|
21
|
+
* number is `false` for 0 and `true` otherwise; `null` and other values (the strings `"TRUE"`, `"FALSE"` included)
|
|
22
|
+
* are TypeErrors. The result's `REML` is the boolean.
|
|
23
|
+
*/
|
|
11
24
|
REML?: boolean;
|
|
12
25
|
/**
|
|
13
|
-
* A contrast per factor of the fixed-effects part
|
|
14
|
-
*
|
|
15
|
-
*
|
|
26
|
+
* A contrast per factor of the fixed-effects part: `"contr.treatment"` (default), `"contr.sum"`, `"contr.helmert"` or
|
|
27
|
+
* `"contr.poly"` (see {@link LmerContrastName}); the random-effects terms always use treatment contrasts. A name that
|
|
28
|
+
* is not a variable of the fixed-effects part (a grouping variable alone included) gives the warning
|
|
29
|
+
* `variable 'B' is absent, its contrast will be ignored`.
|
|
16
30
|
*/
|
|
17
|
-
contrasts?: Record<string,
|
|
31
|
+
contrasts?: Record<string, LmerContrastName>;
|
|
18
32
|
/** The order of the levels of a factor (`{ A: ["c", "a", "b"] }`), as `factor(x, levels = ...)` in R. */
|
|
19
33
|
levels?: Record<string, readonly string[]>;
|
|
20
34
|
}
|
|
@@ -118,6 +132,18 @@ export type MixedCriterion = (s: MixedModelStructure, theta: readonly number[],
|
|
|
118
132
|
f: number;
|
|
119
133
|
g: number[];
|
|
120
134
|
};
|
|
135
|
+
/**
|
|
136
|
+
* The option checks of lmer and of lmerTest's functions (internal; oracle/report-0.14.0-behaviour.md, section 3):
|
|
137
|
+
* `options` must be an object when given (not null, not an array); its own keys, in order, may be neither among those
|
|
138
|
+
* of R that are not supported (the first gives an Error) nor unknown (a TypeError listing all of them). A key whose
|
|
139
|
+
* value is `undefined` counts as given. Returns the options as an object (`{}` when not given).
|
|
140
|
+
*/
|
|
141
|
+
export declare function checkOptions<T extends object>(fn: string, options: T | undefined, accepted: readonly string[], unsupported: readonly string[]): T;
|
|
142
|
+
/**
|
|
143
|
+
* A logical option as R's conditions take it (internal): `undefined` gives the default, a boolean itself, a number
|
|
144
|
+
* other than NaN `false` for 0 and `true` otherwise; `null` and anything else are TypeErrors.
|
|
145
|
+
*/
|
|
146
|
+
export declare function logicalOption(value: unknown, defaultValue: boolean): boolean;
|
|
121
147
|
/**
|
|
122
148
|
* Fit a linear mixed model by REML (default) or maximum likelihood. Equivalent to `lme4::lmer()` in R. With the
|
|
123
149
|
* quantities of its methods: `fixef()`, `coef(summary())` (without p-values), `vcov()`, `sigma()`, `VarCorr()`,
|
|
@@ -135,7 +161,7 @@ export type MixedCriterion = (s: MixedModelStructure, theta: readonly number[],
|
|
|
135
161
|
*
|
|
136
162
|
* @param formula A formula such as `"Reaction ~ Days + (Days | Subject)"`.
|
|
137
163
|
* @param data The data frame: an object of columns.
|
|
138
|
-
* @param options `REML`, and `contrasts` and `levels` as in {@link lm}.
|
|
164
|
+
* @param options `REML`, and `contrasts` and `levels` as in {@link lm} (see {@link LmerOptions} for the keys accepted).
|
|
139
165
|
*
|
|
140
166
|
* @example
|
|
141
167
|
* ```ts
|
|
@@ -155,8 +181,38 @@ export type MixedCriterion = (s: MixedModelStructure, theta: readonly number[],
|
|
|
155
181
|
* @category Mixed models
|
|
156
182
|
*/
|
|
157
183
|
export declare function lmer(formula: string, data: DataFrame, options?: LmerOptions): LmerResult;
|
|
184
|
+
/**
|
|
185
|
+
* What a fit by {@link lmer} was made from and with (internal: not exported from the package), for the functions that
|
|
186
|
+
* work on a fit beyond its result fields (lmerTest's tests in src/models/lmerTest.ts, the ANOVA tables, the comparison
|
|
187
|
+
* of fits): they need the model's structure, the data object (model comparison requires the same object), the options
|
|
188
|
+
* (to refit, for example by maximum likelihood) and the criterion (so that a refit uses the same one).
|
|
189
|
+
*/
|
|
190
|
+
export interface LmerInternals {
|
|
191
|
+
/**
|
|
192
|
+
* The structure of the model (X, Z, the terms, the layout of θ, the model frame's rows), with the response replaced
|
|
193
|
+
* by y − X β₀ (see `beta0`): the criterion and every derivative are those of the model, the fixed effects β̂ − β₀
|
|
194
|
+
*/
|
|
195
|
+
structure: MixedModelStructure;
|
|
196
|
+
/** β₀, the least-squares coefficients of y on X, to be added to the fixed effects computed from `structure` */
|
|
197
|
+
beta0: number[];
|
|
198
|
+
/** The data frame given to lmer (the same object, not a copy) */
|
|
199
|
+
data: DataFrame;
|
|
200
|
+
/** The options given to lmer (a copy) */
|
|
201
|
+
options: LmerOptions;
|
|
202
|
+
/** Whether the REML criterion was minimised */
|
|
203
|
+
reml: boolean;
|
|
204
|
+
/** The criterion with its gradient that was minimised (the sparse one, or the injected one of the regression test) */
|
|
205
|
+
criterion: MixedCriterion;
|
|
206
|
+
/** The estimate θ̂ (a copy, unaffected by changes to the result's `theta`) */
|
|
207
|
+
theta: number[];
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* The internals of a fit (see {@link LmerInternals}). Internal. Throws a TypeError for an object that is not a result
|
|
211
|
+
* returned by {@link lmer}.
|
|
212
|
+
*/
|
|
213
|
+
export declare function lmerInternals(fit: LmerResult): LmerInternals;
|
|
158
214
|
/**
|
|
159
215
|
* {@link lmer} with the criterion and its gradient given (internal: not exported from the package). The regression
|
|
160
216
|
* test of the sparse computation fits with the dense reference implementation through it.
|
|
161
217
|
*/
|
|
162
|
-
export declare function lmerWithCriterion(formula: string, data: DataFrame, options: LmerOptions, criterion: MixedCriterion): LmerResult;
|
|
218
|
+
export declare function lmerWithCriterion(formula: string, data: DataFrame, options: LmerOptions | undefined, criterion: MixedCriterion): LmerResult;
|