-
Notifications
You must be signed in to change notification settings - Fork 1
/
chi-square-p-value.js
1 lines (1 loc) · 8.21 KB
/
chi-square-p-value.js
1
!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define("analyse",[],r):"object"==typeof exports?exports.analyse=r():t.analyse=r()}(this,(function(){return function(t){var r={};function a(e){if(r[e])return r[e].exports;var n=r[e]={i:e,l:!1,exports:{}};return t[e].call(n.exports,n,n.exports,a),n.l=!0,n.exports}return a.m=t,a.c=r,a.d=function(t,r,e){a.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:e})},a.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},a.t=function(t,r){if(1&r&&(t=a(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var e=Object.create(null);if(a.r(e),Object.defineProperty(e,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var n in t)a.d(e,n,function(r){return t[r]}.bind(null,n));return e},a.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return a.d(r,"a",r),r},a.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},a.p="",a(a.s=0)}([function(module,exports,__webpack_require__){"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _matrix=__webpack_require__(1),_matrix2=_interopRequireDefault(_matrix);function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}var sum=void 0,madd=function(t){return t/sum},mpwr=function(t){return t*t},div=function(t,r){return t/r},LogGamma=function(t){var r=1+76.18009173/t-86.50532033/(t+1)+24.01409822/(t+2)-1.231739516/(t+3)+.00120858003/(t+4)-536382e-11/(t+5);return(t-.5)*Math.log(t+4.5)-(t+4.5)+Math.log(2.50662827465*r)},Gcf=function(t,r){for(var a=0,e=1,n=1,i=t,o=0,u=0;Math.abs((n-o)/n)>1e-5;)o=n,n=t*(a=n+((u+=1)-r)*a)+u*n,a/=i=t*(e=i+(u-r)*e)+u*i,e/=i,n/=i,i=1;return 1-Math.exp(r*Math.log(t)-t-LogGamma(r))*n},Gser=function(t,r){for(var a=1/r,e=a,n=1;a>1e-5*e;)e+=a=a*t/(r+n),n+=1;return e*=Math.exp(r*Math.log(t)-t-LogGamma(r))},Gammacdf=function(t,r){return t<=0?0:t<r+1?Gser(t,r):Gcf(t,r)},computeP=function computeP(chi,df){var Z=eval(chi),DF=eval(df),Chisqcdf=void 0;return DF<=0?console.error("Degrees of freedom must be positive"):Chisqcdf=Gammacdf(Z/2,DF/2),Chisqcdf=Math.round(1e5*Chisqcdf)/1e5,Chisqcdf<1?(1-Chisqcdf).toFixed(5):0},computeResidual=function(t,r){for(var a=t.m,e=t.n,n=[],i=_matrix2.default.sumMatrix(t),o=0;o<a;o+=1){void 0===n[o]&&(n[o]=[]);for(var u=_matrix2.default.calculateRowTotal(o,t),m=0;m<e;m+=1){var f=t.mat[o][m]-r.mat[o][m],s=_matrix2.default.calculateColumnTotal(m,t),c=Math.sqrt(u*s/i*(1-u/i)*(1-s/i));n[o][m]=f/c}}return n},analyse=function(t){var r=_matrix2.default.create(t),a=_matrix2.default.map((function(){return 1}),r),e=_matrix2.default.create(a.mat.slice(0,1)),n=_matrix2.default.transpose(_matrix2.default.create(_matrix2.default.transpose(a).mat.slice(0,1)));sum=_matrix2.default.mult(_matrix2.default.mult(_matrix2.default.transpose(a),r),_matrix2.default.transpose(e)).mat[1];var i=_matrix2.default.mult(e,_matrix2.default.transpose(r)),o=_matrix2.default.mult(_matrix2.default.transpose(r),n),u=_matrix2.default.transpose(_matrix2.default.mult(o,i));u=_matrix2.default.map(madd,u);var m=_matrix2.default.sub(r,u);m=_matrix2.default.map(mpwr,m),m=_matrix2.default.combine(div,m,u);var f=(m=_matrix2.default.mult(_matrix2.default.mult(a,_matrix2.default.transpose(m)),n)).mat[1],s=(r.m-1)*(r.n-1);return{chi:f,df:s,pValue:computeP(f,s),residual:computeResidual(r,u)}};exports.default=analyse},function(t,r,a){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var e=new function(){this.version="Matrix 1.01";var t=Math.abs,r=Math.pow(2,-40);function a(a){return t(a)<r?0:a}function n(t,r,a){if(!a.isMatrix)throw"***ERROR: Argument "+r+" of Matrix."+t+' is not a Matrix; its value is "'+a+'".'}function i(t){for(var r=t.length,e=0;e<r;e++){if(!(t[e]instanceof Array))throw"***ERROR: in Matrix.create: argument 1 is not a 2D array.";if(t[e].length!=t[0].length)throw"***ERROR: in Matrix.create: argument 1 has different length rows."}var n=t[0].length,i=new Array(r);for(e=0;e<r;e++){i[e]=new Array(n);for(var o=0;o<n;o++)i[e][o]=a(t[e][o])}var u=new Object;return u.m=r,u.n=n,u.mat=i,u.isMatrix=!0,u}function o(t,r){for(var a=new Array(t),e=0;e<t;e++)a[e]=new Array(r);var n=new Object;return n.m=t,n.n=r,n.mat=a,n.isMatrix=!0,n}this.getEPS=function(){return r},this.setEPS=function(t){r=t},this.create=function(t,r){var a=t instanceof Array;if(!a&&"number"!=typeof t)throw"***ERROR: in Matrix.create: argument 1 is not an array or a number.";if(a&&null!=r)throw"***ERROR: in Matrix.create: argument 1 is an array but argument 2 is also present.";return a?i(t):o(t,r)},this.identity=function(t,r){for(var a=o(t,r),e=0;e<a.m;e++)for(var n=0;n<a.n;n++)a.mat[e][n]=0;for(e=0;e<Math.min(t,r);e++)a.mat[e][e]=1;return a},this.unit=function(t,r){for(var a=o(t,r),e=0;e<t;e++)for(var n=0;n<r;n++)a.mat[e][n]=1;return a},this.random=function(t,r){for(var e=o(t,r),n=0;n<t;n++)for(var i=0;i<r;i++)e.mat[n][i]=a(Math.random());return e},this.copy=function(t,r,a,e,i){n("copy",1,t);for(var u=o(e,i),m=0;m<e;m++)for(var f=0;f<i;f++)u.mat[m][f]=t.mat[m+r][f+a];return u},this.transpose=function(t){n("transpose",1,t);for(var r=o(t.n,t.m),a=0;a<r.m;a++)for(var e=0;e<r.n;e++)r.mat[a][e]=t.mat[e][a];return r},this.diagOf=function(t){n("diagOf",1,t);for(var r=o(Math.min(t.m,t.n),1),a=0;a<r.m;a++)r.mat[a][0]=t.mat[a][a];return r},this.diag=function(t){if(n("diag",1,t),1!=t.n)throw"***ERROR: in Matrix.diag: argument 1 is not a column vector.";for(var r=e.identity(t.m,t.m),a=0;a<r.m;a++)r.mat[a][a]=t.mat[a][0];return r},this.max=function(t){n("max",1,t);for(var r=t.mat[0][0],e=0;e<t.m;e++)for(var i=0;i<t.n;i++)t.mat[e][i]>r&&(r=t.mat[e][i]);return a(r)},this.min=function(t){n("min",1,t);for(var r=t.mat[0][0],e=0;e<t.m;e++)for(var i=0;i<t.n;i++)t.mat[e][i]<r&&(r=t.mat[e][i]);return a(r)},this.scale=function(t,r){n("scale",1,t);for(var e=i(t.mat),o=0;o<e.m;o++)for(var u=0;u<e.n;u++)e.mat[o][u]=a(r*e.mat[o][u]);return e},this.add=function(t,r){if(n("add",1,t),n("add",2,r),t.m!=r.m||t.n!=r.n)throw"***ERROR: in Matrix.add: matrix dimensions don't match.";for(var e=o(t.m,t.n),i=0;i<e.m;i++)for(var u=0;u<e.n;u++)e.mat[i][u]=a(t.mat[i][u]+r.mat[i][u]);return e},this.sub=function(t,r){if(n("sub",1,t),n("sub",2,r),t.m!=r.m||t.n!=r.n)throw"***ERROR: in Matrix.sub: matrix dimensions don't match.";for(var e=o(t.m,t.n),i=0;i<e.m;i++)for(var u=0;u<e.n;u++)e.mat[i][u]=a(t.mat[i][u]-r.mat[i][u]);return e},this.mult=function(t,r){if(n("mult",1,t),n("mult",2,r),t.n!=r.m)throw"***ERROR: in Matrix.mult: matrix dimensions don't match.";for(var e,i=o(t.m,r.n),u=0;u<i.m;u++)for(var m=0;m<i.n;m++){e=0;for(var f=0;f<t.n;f++)e+=t.mat[u][f]*r.mat[f][m];i.mat[u][m]=a(e)}return i},this.map=function(t,r){n("map",2,r);for(var e=o(r.m,r.n),i=0;i<e.m;i++)for(var u=0;u<e.n;u++)e.mat[i][u]=a(t(r.mat[i][u]));return e},this.combine=function(t,r,e){if(n("combine",1,r),n("combine",2,e),r.m!=e.m||r.n!=e.n)throw"***ERROR: in Matrix.combine: matrix dimensions don't match.";for(var i=o(r.m,r.n),u=0;u<i.m;u++)for(var m=0;m<i.n;m++)i.mat[u][m]=a(t(r.mat[u][m],e.mat[u][m]));return i},this.trace=function(t){n("trace",1,t);for(var r=0,e=0;e<Math.min(t.m,t.n);e++)r+=t.mat[e][e];return a(r)},this.det=function(t){if(n("det",1,t),t.m!=t.n)throw"***ERROR: in Matrix.det: argument is not square.";return a(LUDecomposition.det(create(t)))},this.inverse=function(t){return n("inverse",1,t),e.solve(t,e.identity(t.m,t.m))},this.solve=function(t,r){if(n("solve",1,t),n("solve",2,r),t.m==t.n)return LUDecomposition.solve(create(t),r);if(t.m>t.n){var a=create(t);return QRDecomposition.solve(a,r)}throw"***ERROR: in Matrix.solve: argument 1 has fewer rows than columns."},this.eigenstructure=function(t){if(n("eigenstructure",1,t),t.m!=t.n)throw"***ERROR: in Matrix.eigenstructure: argument is not a square matrix.";return EVDecomposition.create(t)},this.calculateRowTotal=function(t,r){return r.mat[t].reduce((function(t,r){return t+r}))},this.calculateColumnTotal=function(t,r){for(var a=[],e=0;e<r.m;e+=1)a.push(r.mat[e][t]);return a.reduce((function(t,r){return t+r}))},this.sumMatrix=function(t){for(var r=0,a=0;a<t.m;a+=1)for(var e=0;e<t.n;e+=1)r+=t.mat[a][e];return r},this.display=function(t,r){n("display",1,t),null==r&&(r=3),displayMat(t.mat,null,null,r)}};r.default=e}])}));