Significance Testing - Independent Samples Column Means and Proportions Tests

From Q
Jump to: navigation, search

This rule over-rides Q's automatic statistical testing and performs independent samples column means and proportion tests. It also includes options designed to mimic those in introductory statistics courses and in SPSS Custom Tables.

Technical details

  • Show significance needs to be set to Compare columns when applying this rule.
  • The default formulas used in this rule are not the same as those used by Q to automatically test independent samples. If you wish to use Q's in-built testing for independent samples on data that has a dependent structure, the best way to achieve this is by first Stacking the data file. However, in most situations the default settings in this rule will correspond to the general defaults in Q. Reasons for differences are described in some of the following points.
  • As with all Rules, this Rule works by over-riding/ignoring settings and safe-guards built into Q. Many of the settings that are over-ridden are designed to protect users from problems in their data, and if applying this Rule a higher degree of diligence is required in checking results, particularly if performing other modifications that make your version of Q work differently to the defaults.
  • This rule over-rides most, but not all, settings in Statistical Assumptions. In particular:
    • Bessel corrections are still used as inputs to computations of Standard Errors and Standard Deviations (i.e., those computed on the tables use this setting, and these then feed into the some of the tests).
    • The Weights and significance determines the Effective Base n, which is an input to the Effective sample size used in this rule.
  • In various situations, the formulas used for computing significance in this rule may give incorrect/misleading results (e.g., when the assumption of independence is inappropriate and when the various tests selected are wrong).
  • Effective sample size is computed as Effective Base n * Column Population / Base Population.
  • Weighted sample size is Population.
  • When the Question in the Blue drop-down is a Pick One - Multi, Pick Any - Grid, or a Number - Grid, the sample size used in the test is the Base n (or Base Population, or Effective Base n, otherwise the Column n (or the Column Population, or Effective Column n) is used.
  • Simplified Independent Complex Samples T-Test - Comparing Two Means is used when Numeric data variance assumptions (t-test) is set to Complex samples.
  • Independent Samples T-Test - Unequal Variance is used when Numeric data variance assumptions (t-test) is set to Unequal variance. When this option is selected in conjunction with setting Base used in test of means to Weighted sample size and Multiple comparison correction is set to None, the resulting tests correspond to an an Independent Samples T-Test in SPSS with Equal variances not assumed.
  • Independent Samples T-Test - Equal Variance is used when Numeric data variance assumptions (t-test) is set to Equal variance (categories compared). When this option is selected in conjunction with setting Base used in test of means to Weighted sample size and Multiple comparison correction to None, the resulting tests correspond to an an Independent Samples T-Test in SPSS with Equal variances assumed and also to Compare column means (t-tests) with Estimate variance only from the categories compared selected in SPSS Custom Tables, except where the columns are from multiple response questions.
  • Independent Samples T-Test - Pooled Variance is used when Numeric data variance assumptions (t-test) is set to Equal variance (all non-ignored categories). When this option is selected in conjunction with setting Base used in test of means to Weighted sample size and Multiple comparison correction to None, the resulting tests correspond to Compare column means (t-tests) with Estimate variance only from the categories compared not selected in SPSS Custom Tables, except where the columns are from multiple response questions.
  • Simplified Independent Complex Samples T-Test - Comparing Two Proportions is used when Proportions test is set to Complex samples t-test. Note that this test is not available via modifying settings in Statistical Assumptions, although it should give similar results to the default test, which uses the Second Order Rao-Scott Test of Independence of a Contingency Table (that test is not used in this Rule as the statistics used in its computation are not available on the table, and are instead computed from the raw data).
  • Independent Samples Z-Test - Comparing Two Proportions (Pooled) is used when Proportions test is set to Pooled z-test. When this option is selected in conjunction with setting Base used in test of means to Weighted sample size and Multiple comparison correction is set to None, the resulting tests correspond to Compare column proportions (z-tests) in SPSS Custom Tables.
  • Independent Samples Z-Test - Comparing Two Proportions (Un-Pooled) is used when Proportions test is set to Un-pooled z-test

Specifying columns to compare

The setting called Set column comparisons can be used to specify which columns of the table should be compared. When this option is selected, you should enter the names of the columns that you would like to compare according to the notation described on the page Specifying comparisons.

Note that this option:

  1. Automatically renames the columns in the standard alphabetic order (A, B, C, etc). This is equivalent to un-checking the option Recycle column letters in Edit > Table Options > Statistical Assumptions.
  2. Ignores the spans in the table, so that you can perform comparisons between columns in different spans.

Spans

As with Q's default Column Comparisons testing, spans in the table are used to specify which columns compare. Comparisons are only conducted within the lowest-level spans. As with the default testing, results will only be useful when the spans have been set up to accurately reflect the comparisons that you want to make.

When spans have not been set up to guide statistical testing, neither Q's default column comparison results nor the results provided by this rule will be useful. In both cases, the issue will manifest in slightly different ways, as shown in the following example.

BadColumnComparisons2.PNG

The first table shows Q's default column comparisons where columns are compared in the lowest span, and the second shows the comparisons evaluated by this rule (where comparisons are only made within each span). In both cases the sensible set of columns to compare is the set of age categories, that is with both spans removed.

Footers

The footer information relating to statistical testing will relate to Q's default test, and these should be turned off when you use this rule. This is done by editing the Table Options for the relevant tables and un-ticking the options relating to statistical testing in the Footers tab. Information about the settings used in the rule can be added to the table footer by ticking the option in the rule called Include settings information in footer.

How to apply this rule

For the first time in a project

  • Select the table(s)/chart(s) that you wish to apply the rule to.
  • Start typing the name of the Rule into the Search features and data box in the top right of the Q window.
  • Click on the Rule when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Choose this rule from the list.

Additional applications of the rule

  • Select a table or chart that has the rule and any table(s)/chart(s) that you wish to apply the rule to.
  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to apply.
  • Click on the Apply drop-down and choose your desired option.
  • Check New items to have it automatically applied to new items that you create. Use Edit > Project Options > Save as Template to create a new project template that automatically uses this rule.

Removing the rule

  • Select the table(s)/chart(s) that you wish to remove the rule from.
  • Press the Rules tab (bottom-right corner).
  • Press Apply next to the rule you wish to remove and choose the appropriate option.

How to modify the rule

  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to modify.
  • Click Edit Rule and make the desired changes. Alternatively, you can use the JavaScript below to make your own rule (see Customizing Rules).

JavaScript

// Version 2.1.2
//loading jstat library
this.j$=this.jStat=function(a,b){function f(b,c){var d=b>c?b:c;return a.pow(10,17-~~(a.log(d>0?d:-d)*a.LOG10E))}function h(a){return e.call(a)==="[object Function]"}function i(a){return typeof a=="number"&&a===a}function j(a){return c.apply([],a)}function k(){return new k._init(arguments)}function l(){return 0}function m(){return 1}function n(a,b){return a===b?1:0}var c=Array.prototype.concat,d=Array.prototype.slice,e=Object.prototype.toString,g=Array.isArray||function(b){return e.call(b)==="[object Array]"};k.fn=k.prototype,k._init=function(b){var c;if(g(b[0]))if(g(b[0][0])){h(b[1])&&(b[0]=k.map(b[0],b[1]));for(c=0;c<b[0].length;c++)this[c]=b[0][c];this.length=b[0].length}else this[0]=h(b[1])?k.map(b[0],b[1]):b[0],this.length=1;else if(i(b[0]))this[0]=k.seq.apply(null,b),this.length=1;else{if(b[0]instanceof k)return k(b[0].toArray());this[0]=[],this.length=1}return this},k._init.prototype=k.prototype,k._init.constructor=k,k.utils={calcRdx:f,isArray:g,isFunction:h,isNumber:i,toVector:j},k.extend=function(b){var c,d;if(arguments.length===1){for(d in b)k[d]=b[d];return this}for(c=1;c<arguments.length;c++)for(d in arguments[c])b[d]=arguments[c][d];return b},k.rows=function(b){return b.length||1},k.cols=function(b){return b[0].length||1},k.dimensions=function(b){return{rows:k.rows(b),cols:k.cols(b)}},k.row=function(b,c){return b[c]},k.col=function(b,c){var d=new Array(b.length);for(var e=0;e<b.length;e++)d[e]=[b[e][c]];return d},k.diag=function(b){var c=k.rows(b),d=new Array(c);for(var e=0;e<c;e++)d[e]=[b[e][e]];return d},k.antidiag=function(b){var c=k.rows(b)-1,d=new Array(c);for(var e=0;c>=0;c--,e++)d[e]=[b[e][c]];return d},k.transpose=function(b){var c=[],d,e,f,h,i;g(b[0])||(b=[b]),e=b.length,f=b[0].length;for(i=0;i<f;i++){d=new Array(e);for(h=0;h<e;h++)d[h]=b[h][i];c.push(d)}return c.length===1?c[0]:c},k.map=function(b,c,d){var e,f,h,i,j;g(b[0])||(b=[b]),f=b.length,h=b[0].length,i=d?b:new Array(f);for(e=0;e<f;e++){i[e]||(i[e]=new Array(h));for(j=0;j<h;j++)i[e][j]=c(b[e][j],e,j)}return i.length===1?i[0]:i},k.alter=function(b,c){return k.map(b,c,!0)},k.create=function(b,c,d){var e=new Array(b),f,g;h(c)&&(d=c,c=b);for(f=0;f<b;f++){e[f]=new Array(c);for(g=0;g<c;g++)e[f][g]=d(f,g)}return e},k.zeros=function(b,c){return i(c)||(c=b),k.create(b,c,l)},k.ones=function(b,c){return i(c)||(c=b),k.create(b,c,m)},k.rand=function(c,d){return i(d)||(d=c),k.create(c,d,a.random)},k.identity=function(b,c){return i(c)||(c=b),k.create(b,c,n)},k.symmetric=function(b){var c=!0,d=b.length,e,f;if(b.length!==b[0].length)return!1;for(e=0;e<d;e++)for(f=0;f<d;f++)if(b[f][e]!==b[e][f])return!1;return!0},k.clear=function(b){return k.alter(b,l)},k.seq=function(b,c,d,e){h(e)||(e=!1);var g=[],i=f(b,c),j=(c*i-b*i)/((d-1)*i),k=b,l;for(l=0;k<=c;l++,k=(b*i+j*i*l)/i)g.push(e?e(k,l):k);return g};var o=k.prototype;return o.length=0,o.push=Array.prototype.push,o.sort=Array.prototype.sort,o.splice=Array.prototype.splice,o.slice=Array.prototype.slice,o.toArray=function(){return this.length>1?d.call(this):d.call(this)[0]},o.map=function(b,c){return k(k.map(this,b,c))},o.alter=function(b){return k.alter(this,b),this},function(a){for(var b=0;b<a.length;b++)(function(a){o[a]=function(b){var c=this,d;return b?(setTimeout(function(){b.call(c,o[a].call(c))}),this):(d=k[a](this),g(d)?k(d):d)}})(a[b])}("transpose clear symmetric rows cols dimensions diag antidiag".split(" ")),function(a){for(var b=0;b<a.length;b++)(function(a){o[a]=function(b,c){var d=this;return c?(setTimeout(function(){c.call(d,o[a].call(d,b))}),this):k(k[a](this,b))}})(a[b])}("row col".split(" ")),function(a){for(var b=0;b<a.length;b++)(function(a){o[a]=new Function("return jStat(jStat."+a+".apply(null, arguments));")})(a[b])}("create zeros ones rand identity".split(" ")),k}(Math),function(a,b){function d(a,b){return a-b}function e(a,c,d){return b.max(c,b.min(a,d))}var c=a.utils.isFunction;a.sum=function g(a){var g=0,b=a.length,c;while(--b>=0)g+=a[b];return g},a.sumsqrd=function(b){var c=0,d=b.length;while(--d>=0)c+=b[d]*b[d];return c},a.sumsqerr=function(c){var d=a.mean(c),e=0,f=c.length,g;while(--f>=0)g=c[f]-d,e+=g*g;return e},a.product=function(b){var c=1,d=b.length;while(--d>=0)c*=b[d];return c},a.min=function(b){var c=b[0],d=0;while(++d<b.length)b[d]<c&&(c=b[d]);return c},a.max=function(b){var c=b[0],d=0;while(++d<b.length)b[d]>c&&(c=b[d]);return c},a.mean=function(c){return a.sum(c)/c.length},a.meansqerr=function(c){return a.sumsqerr(c)/c.length},a.geomean=function(d){return b.pow(a.product(d),1/d.length)},a.median=function(b){var c=b.length,e=b.slice().sort(d);return c&1?e[c/2|0]:(e[c/2-1]+e[c/2])/2},a.cumsum=function(b){var c=b.length,d=new Array(c),e;d[0]=b[0];for(e=1;e<c;e++)d[e]=d[e-1]+b[e];return d},a.diff=function(b){var c=[],d=b.length,e;for(e=1;e<d;e++)c.push(b[e]-b[e-1]);return c},a.mode=function(b){var c=b.length,e=b.slice().sort(d),f=1,g=0,h=0,i=[],j;for(j=0;j<c;j++)e[j]===e[j+1]?f++:(f>g?(i=[e[j]],g=f,h=0):f===g&&(i.push(e[j]),h++),f=1);return h===0?i[0]:i},a.range=function(c){return a.max(c)-a.min(c)},a.variance=function(c,d){return a.sumsqerr(c)/(c.length-(d?1:0))},a.stdev=function(d,e){return b.sqrt(a.variance(d,e))},a.meandev=function(d){var e=0,f=a.mean(d),g;for(g=d.length-1;g>=0;g--)e+=b.abs(d[g]-f);return e/d.length},a.meddev=function(d){var e=0,f=a.median(d),g;for(g=d.length-1;g>=0;g--)e+=b.abs(d[g]-f);return e/d.length},a.coeffvar=function(c){return a.stdev(c)/a.mean(c)},a.quartiles=function(c){var e=c.length,f=c.slice().sort(d);return[f[b.round(e/4)-1],f[b.round(e/2)-1],f[b.round(e*3/4)-1]]},a.quantiles=function(c,f,g,h){var i=c.slice().sort(d),j=[f.length],k=c.length,l,m,n,o,p,q;typeof g=="undefined"&&(g=3/8),typeof h=="undefined"&&(h=3/8);for(l=0;l<f.length;l++)m=f[l],n=g+m*(1-g-h),o=k*m+n,p=b.floor(e(o,1,k-1)),q=e(o-p,0,1),j[l]=(1-q)*i[p-1]+q*i[p];return j},a.percentileOfScore=function(b,c,d){var e=0,f=b.length,g=!1,h,i;d==="strict"&&(g=!0);for(i=0;i<f;i++)h=b[i],(g&&h<c||!g&&h<=c)&&e++;return e/f},a.covariance=function(c,d){var e=a.mean(c),f=a.mean(d),g=c.length,h=new Array(g),i;for(i=0;i<g;i++)h[i]=(c[i]-e)*(d[i]-f);return a.sum(h)/(g-1)},a.corrcoeff=function(c,d){return a.covariance(c,d)/a.stdev(c,1)/a.stdev(d,1)};var f=a.prototype;f.cumsum=function(b,d){var e=[],g=0,h=this;c(b)&&(d=b,b=!1);if(d)return setTimeout(function(){d.call(h,f.cumsum.call(h,b))}),this;if(this.length>1){h=b===!0?this:this.transpose();for(;g<h.length;g++)e[g]=a.cumsum(h[g]);return e}return a.cumsum(this[0],b)},function(b){for(var d=0;d<b.length;d++)(function(b){f[b]=function(d,e){var g=[],h=0,i=this;c(d)&&(e=d,d=!1);if(e)return setTimeout(function(){e.call(i,f[b].call(i,d))}),this;if(this.length>1){i=d===!0?this:this.transpose();for(;h<i.length;h++)g[h]=a[b](i[h]);return d===!0?a[b](a.utils.toVector(g)):g}return a[b](this[0],d)}})(b[d])}("sum sumsqrd sumsqerr product min max mean meansqerr geomean median diff mode range variance stdev meandev meddev coeffvar quartiles".split(" ")),function(b){for(var d=0;d<b.length;d++)(function(b){f[b]=function(){var d=[],e=0,g=this,h=Array.prototype.slice.call(arguments);if(c(h[h.length-1])){var i=h[h.length-1],j=h.slice(0,h.length-1);return setTimeout(function(){i.call(g,f[b].apply(g,j))}),this}var i=undefined,k=function(d){return a[b].apply(g,[d].concat(h))};if(this.length>1){g=g.transpose();for(;e<g.length;e++)d[e]=k(g[e]);return d}return k(this[0])}})(b[d])}("quantiles percentileOfScore".split(" "))}(this.jStat,Math),function(a,b){a.gammaln=function(c){var d=0,e=[76.18009172947146,-86.50532032941678,24.01409824083091,-1.231739572450155,.001208650973866179,-0.000005395239384953],f=1.000000000190015,g,h,i;i=(h=g=c)+5.5,i-=(g+.5)*b.log(i);for(;d<6;d++)f+=e[d]/++h;return b.log(2.5066282746310007*f/g)-i},a.gammafn=function(c){var d=[-1.716185138865495,24.76565080557592,-379.80425647094563,629.3311553128184,866.9662027904133,-31451.272968848367,-36144.413418691176,66456.14382024054],e=[-30.8402300119739,315.35062697960416,-1015.1563674902192,-3107.771671572311,22538.11842098015,4755.846277527881,-134659.9598649693,-115132.2596755535],f=!1,g=0,h=0,i=0,j=c,k,l,m,n,o,p;if(j<=0){n=j%1+3.6e-16;if(n)f=(j&1?-1:1)*b.PI/b.sin(b.PI*n),j=1-j;else return Infinity}m=j,j<1?l=j++:l=(j-=g=(j|0)-1)-1;for(k=0;k<8;++k)i=(i+d[k])*l,h=h*l+e[k];n=i/h+1;if(m<j)n/=m;else if(m>j)for(k=0;k<g;++k)n*=j,j++;return f&&(n=f/n),n},a.gammap=function(d,e){var f=a.gammaln(d),g=d,h=1/d,i=h,j=e+1-d,k=1/1e-30,l=1/j,m=l,n=1,o=-~(b.log(d>=1?d:1/d)*8.5+d*.4+17),p,q;if(e<0||d<=0)return NaN;if(e<d+1){for(;n<=o;n++)h+=i*=e/++g;return h*b.exp(-e+d*b.log(e)-f)}for(;n<=o;n++)p=-n*(n-d),j+=2,l=p*l+j,k=j+p/k,l=1/l,m*=l*k;return 1-m*b.exp(-e+d*b.log(e)-f)},a.factorialln=function(c){return c<0?NaN:a.gammaln(c+1)},a.factorial=function(c){return c<0?NaN:a.gammafn(c+1)},a.combination=function(d,e){return d>170||e>170?b.exp(a.combinationln(d,e)):a.factorial(d)/a.factorial(e)/a.factorial(d-e)},a.combinationln=function(c,d){return a.factorialln(c)-a.factorialln(d)-a.factorialln(c-d)},a.permutation=function(c,d){return a.factorial(c)/a.factorial(c-d)},a.betafn=function(d,e){return d<=0||e<=0?undefined:d+e>170?b.exp(a.betaln(d,e)):a.gammafn(d)*a.gammafn(e)/a.gammafn(d+e)},a.betaln=function(c,d){return a.gammaln(c)+a.gammaln(d)-a.gammaln(c+d)},a.betacf=function(c,d,e){var f=1e-30,g=1,h=d+e,i=d+1,j=d-1,k=1,l=1-h*c/i,m,n,o,p;b.abs(l)<f&&(l=f),l=1/l,p=l;for(;g<=100;g++){m=2*g,n=g*(e-g)*c/((j+m)*(d+m)),l=1+n*l,b.abs(l)<f&&(l=f),k=1+n/k,b.abs(k)<f&&(k=f),l=1/l,p*=l*k,n=-(d+g)*(h+g)*c/((d+m)*(i+m)),l=1+n*l,b.abs(l)<f&&(l=f),k=1+n/k,b.abs(k)<f&&(k=f),l=1/l,o=l*k,p*=o;if(b.abs(o-1)<3e-7)break}return p},a.gammapinv=function(d,e){var f=0,g=e-1,h=1e-8,i=a.gammaln(e),j,k,l,m,n,o,p;if(d>=1)return b.max(100,e+100*b.sqrt(e));if(d<=0)return 0;e>1?(o=b.log(g),p=b.exp(g*(o-1)-i),n=d<.5?d:1-d,l=b.sqrt(-2*b.log(n)),j=(2.30753+l*.27061)/(1+l*(.99229+l*.04481))-l,d<.5&&(j=-j),j=b.max(.001,e*b.pow(1-1/(9*e)-j/(3*b.sqrt(e)),3))):(l=1-e*(.253+e*.12),d<l?j=b.pow(d/l,1/e):j=1-b.log(1-(d-l)/(1-l)));for(;f<12;f++){if(j<=0)return 0;k=a.gammap(e,j)-d,e>1?l=p*b.exp(-(j-g)+g*(b.log(j)-o)):l=b.exp(-j+g*b.log(j)-i),m=k/l,j-=l=m/(1-.5*b.min(1,m*((e-1)/j-1))),j<=0&&(j=.5*(j+l));if(b.abs(l)<h*j)break}return j},a.erf=function(c){var d=[-1.3026537197817094,.6419697923564902,.019476473204185836,-0.00956151478680863,-0.000946595344482036,.000366839497852761,42523324806907e-18,-0.000020278578112534,-0.000001624290004647,130365583558e-17,1.5626441722e-8,-8.5238095915e-8,6.529054439e-9,5.059343495e-9,-9.91364156e-10,-2.27365122e-10,9.6467911e-11,2.394038e-12,-6.886027e-12,8.94487e-13,3.13092e-13,-1.12708e-13,3.81e-16,7.106e-15,-1.523e-15,-9.4e-17,1.21e-16,-2.8e-17],e=d.length-1,f=!1,g=0,h=0,i,j,k,l;c<0&&(c=-c,f=!0),i=2/(2+c),j=4*i-2;for(;e>0;e--)k=g,g=j*g-h+d[e],h=k;return l=i*b.exp(-c*c+.5*(d[0]+j*g)-h),f?l-1:1-l},a.erfc=function(c){return 1-a.erf(c)},a.erfcinv=function(d){var e=0,f,g,h,i;if(d>=2)return-100;if(d<=0)return 100;i=d<1?d:2-d,h=b.sqrt(-2*b.log(i/2)),f=-0.70711*((2.30753+h*.27061)/(1+h*(.99229+h*.04481))-h);for(;e<2;e++)g=a.erfc(f)-i,f+=g/(1.1283791670955126*b.exp(-f*f)-f*g);return d<1?f:-f},a.ibetainv=function(d,e,f){var g=1e-8,h=e-1,i=f-1,j=0,k,l,m,n,o,p,q,r,s,t,u;if(d<=0)return 0;if(d>=1)return 1;e>=1&&f>=1?(m=d<.5?d:1-d,n=b.sqrt(-2*b.log(m)),q=(2.30753+n*.27061)/(1+n*(.99229+n*.04481))-n,d<.5&&(q=-q),r=(q*q-3)/6,s=2/(1/(2*e-1)+1/(2*f-1)),t=q*b.sqrt(r+s)/s-(1/(2*f-1)-1/(2*e-1))*(r+5/6-2/(3*s)),q=e/(e+f*b.exp(2*t))):(k=b.log(e/(e+f)),l=b.log(f/(e+f)),n=b.exp(e*k)/e,o=b.exp(f*l)/f,t=n+o,d<n/t?q=b.pow(e*t*d,1/e):q=1-b.pow(f*t*(1-d),1/f)),u=-a.gammaln(e)-a.gammaln(f)+a.gammaln(e+f);for(;j<10;j++){if(q===0||q===1)return q;p=a.ibeta(q,e,f)-d,n=b.exp(h*b.log(q)+i*b.log(1-q)+u),o=p/n,q-=n=o/(1-.5*b.min(1,o*(h/q-i/(1-q)))),q<=0&&(q=.5*(q+n)),q>=1&&(q=.5*(q+n+1));if(b.abs(n)<g*q&&j>0)break}return q},a.ibeta=function(d,e,f){var g=d===0||d===1?0:b.exp(a.gammaln(e+f)-a.gammaln(e)-a.gammaln(f)+e*b.log(d)+f*b.log(1-d));return d<0||d>1?!1:d<(e+1)/(e+f+2)?g*a.betacf(d,e,f)/e:1-g*a.betacf(1-d,f,e)/f},a.randn=function(d,e){var f,g,h,i,j,k;e||(e=d);if(d)return a.create(d,e,function(){return a.randn()});do f=b.random(),g=1.7156*(b.random()-.5),h=f-.449871,i=b.abs(g)+.386595,j=h*h+i*(.196*i-.25472*h);while(j>.27597&&(j>.27846||g*g>-4*b.log(f)*f*f));return g/f},a.randg=function(d,e,f){var g=d,h,i,j,k,l,m;f||(f=e),d||(d=1);if(e)return m=a.zeros(e,f),m.alter(function(){return a.randg(d)}),m;d<1&&(d+=1),h=d-1/3,i=1/b.sqrt(9*h);do{do l=a.randn(),k=1+i*l;while(k<=0);k=k*k*k,j=b.random()}while(j>1-.331*b.pow(l,4)&&b.log(j)>.5*l*l+h*(1-k+b.log(k)));if(d==g)return h*k;do j=b.random();while(j===0);return b.pow(j,1/g)*h*k},function(b){for(var c=0;c<b.length;c++)(function(b){a.fn[b]=function(){return a(a.map(this,function(c){return a[b](c)}))}})(b[c])}("gammaln gammafn factorial factorialln".split(" ")),function(b){for(var c=0;c<b.length;c++)(function(b){a.fn[b]=function(){return a(a[b].apply(null,arguments))}})(b[c])}("randn".split(" "))}(this.jStat,Math),function(a,b){(function(b){for(var c=0;c<b.length;c++)(function(b){a[b]=function(a,b,c){return this instanceof arguments.callee?(this._a=a,this._b=b,this._c=c,this):new arguments.callee(a,b,c)},a.fn[b]=function(c,d,e){var f=a[b](c,d,e);return f.data=this,f},a[b].prototype.sample=function(c){var d=this._a,e=this._b,f=this._c;return c?a.alter(c,function(){return a[b].sample(d,e,f)}):a[b].sample(d,e,f)},function(c){for(var d=0;d<c.length;d++)(function(c){a[b].prototype[c]=function(d){var e=this._a,f=this._b,g=this._c;return d||(d=this.data),typeof d!="number"?a.fn.map.call(d,function(d){return a[b][c](d,e,f,g)}):a[b][c](d,e,f,g)}})(c[d])}("pdf cdf inv".split(" ")),function(c){for(var d=0;d<c.length;d++)(function(c){a[b].prototype[c]=function(){return a[b][c](this._a,this._b,this._c)}})(c[d])}("mean median mode variance".split(" "))})(b[c])})("beta centralF cauchy chisquare exponential gamma invgamma kumaraswamy lognormal normal pareto studentt weibull uniform  binomial negbin hypgeom poisson triangular".split(" ")),a.extend(a.beta,{pdf:function(c,d,e){return c>1||c<0?0:d==1&&e==1?1:b.exp((d-1)*b.log(c)+(e-1)*b.log(1-c)-a.betaln(d,e))},cdf:function(b,c,d){return b>1||b<0?(b>1)*1:a.ibeta(b,c,d)},inv:function(b,c,d){return a.ibetainv(b,c,d)},mean:function(a,b){return a/(a+b)},median:function(a,b){},mode:function(a,c){return a*c/(b.pow(a+c,2)*(a+c+1))},sample:function(b,c){var d=a.randg(b);return d/(d+a.randg(c))},variance:function(a,c){return a*c/(b.pow(a+c,2)*(a+c+1))}}),a.extend(a.centralF,{pdf:function(c,d,e){return c>=0?b.sqrt(b.pow(d*c,d)*b.pow(e,e)/b.pow(d*c+e,d+e))/(c*a.betafn(d/2,e/2)):undefined},cdf:function(b,c,d){return a.ibeta(c*b/(c*b+d),c/2,d/2)},inv:function(b,c,d){return d/(c*(1/a.ibetainv(b,c/2,d/2)-1))},mean:function(a,b){return b>2?b/(b-2):undefined},mode:function(a,b){return a>2?b*(a-2)/(a*(b+2)):undefined},sample:function(b,c){var d=a.randg(b/2)*2,e=a.randg(c/2)*2;return d/b/(e/c)},variance:function(a,b){return b>4?2*b*b*(a+b-2)/(a*(b-2)*(b-2)*(b-4)):undefined}}),a.extend(a.cauchy,{pdf:function(a,c,d){return d/(b.pow(a-c,2)+b.pow(d,2))/b.PI},cdf:function(a,c,d){return b.atan((a-c)/d)/b.PI+.5},inv:function(a,c,d){return c+d*b.tan(b.PI*(a-.5))},median:function(a,b){return a},mode:function(a,b){return a},sample:function(c,d){return a.randn()*b.sqrt(1/(2*a.randg(.5)))*d+c}}),a.extend(a.chisquare,{pdf:function(c,d){return b.exp((d/2-1)*b.log(c)-c/2-d/2*b.log(2)-a.gammaln(d/2))},cdf:function(b,c){return a.gammap(c/2,b/2)},inv:function(b,c){return 2*a.gammapinv(b,.5*c)},mean:function(a){return a},median:function(a){return a*b.pow(1-2/(9*a),3)},mode:function(a){return a-2>0?a-2:0},sample:function(b){return a.randg(b/2)*2},variance:function(a){return 2*a}}),a.extend(a.exponential,{pdf:function(a,c){return a<0?0:c*b.exp(-c*a)},cdf:function(a,c){return a<0?0:1-b.exp(-c*a)},inv:function(a,c){return-b.log(1-a)/c},mean:function(a){return 1/a},median:function(a){return 1/a*b.log(2)},mode:function(a){return 0},sample:function(a){return-1/a*b.log(b.random())},variance:function(a){return b.pow(a,-2)}}),a.extend(a.gamma,{pdf:function(c,d,e){return b.exp((d-1)*b.log(c)-c/e-a.gammaln(d)-d*b.log(e))},cdf:function(b,c,d){return a.gammap(c,b/d)},inv:function(b,c,d){return a.gammapinv(b,c)*d},mean:function(a,b){return a*b},mode:function(a,b){return a>1?(a-1)*b:undefined},sample:function(b,c){return a.randg(b)*c},variance:function(a,b){return a*b*b}}),a.extend(a.invgamma,{pdf:function(c,d,e){return b.exp(-(d+1)*b.log(c)-e/c-a.gammaln(d)+d*b.log(e))},cdf:function(b,c,d){return 1-a.gammap(c,d/b)},inv:function(b,c,d){return d/a.gammapinv(1-b,c)},mean:function(a,b){return a>1?b/(a-1):undefined},mode:function(a,b){return b/(a+1)},sample:function(b,c){return c/a.randg(b)},variance:function(a,b){return a>2?b*b/((a-1)*(a-1)*(a-2)):undefined}}),a.extend(a.kumaraswamy,{pdf:function(a,c,d){return b.exp(b.log(c)+b.log(d)+(c-1)*b.log(a)+(d-1)*b.log(1-b.pow(a,c)))},cdf:function(a,c,d){return 1-b.pow(1-b.pow(a,c),d)},mean:function(b,c){return c*a.gammafn(1+1/b)*a.gammafn(c)/a.gammafn(1+1/b+c)},median:function(a,c){return b.pow(1-b.pow(2,-1/c),1/a)},mode:function(a,c){return a>=1&&c>=1&&a!==1&&c!==1?b.pow((a-1)/(a*c-1),1/a):undefined},variance:function(a,b){}}),a.extend(a.lognormal,{pdf:function(a,c,d){return b.exp(-b.log(a)-.5*b.log(2*b.PI)-b.log(d)-b.pow(b.log(a)-c,2)/(2*d*d))},cdf:function(c,d,e){return.5+.5*a.erf((b.log(c)-d)/b.sqrt(2*e*e))},inv:function(c,d,e){return b.exp(-1.4142135623730951*e*a.erfcinv(2*c)+d)},mean:function(a,c){return b.exp(a+c*c/2)},median:function(a,c){return b.exp(a)},mode:function(a,c){return b.exp(a-c*c)},sample:function(c,d){return b.exp(a.randn()*d+c)},variance:function(a,c){return(b.exp(c*c)-1)*b.exp(2*a+c*c)}}),a.extend(a.normal,{pdf:function(a,c,d){return b.exp(-0.5*b.log(2*b.PI)-b.log(d)-b.pow(a-c,2)/(2*d*d))},cdf:function(c,d,e){return.5*(1+a.erf((c-d)/b.sqrt(2*e*e)))},inv:function(b,c,d){return-1.4142135623730951*d*a.erfcinv(2*b)+c},mean:function(a,b){return a},median:function(a,b){return a},mode:function(a,b){return a},sample:function(b,c){return a.randn()*c+b},variance:function(a,b){return b*b}}),a.extend(a.pareto,{pdf:function(a,c,d){return a>c?d*b.pow(c,d)/b.pow(a,d+1):undefined},cdf:function(a,c,d){return 1-b.pow(c/a,d)},mean:function(a,c){return c>1?c*b.pow(a,c)/(c-1):undefined},median:function(a,c){return a*c*b.SQRT2},mode:function(a,b){return a},variance:function(a,c){return c>2?a*a*c/(b.pow(c-1,2)*(c-2)):undefined}}),a.extend(a.studentt,{pdf:function(c,d){return a.gammafn((d+1)/2)/(b.sqrt(d*b.PI)*a.gammafn(d/2))*b.pow(1+c*c/d,-((d+1)/2))},cdf:function(c,d){var e=d/2;return a.ibeta((c+b.sqrt(c*c+d))/(2*b.sqrt(c*c+d)),e,e)},inv:function(c,d){var e=a.ibetainv(2*b.min(c,1-c),.5*d,.5);return e=b.sqrt(d*(1-e)/e),c>0?e:-e},mean:function(a){return a>1?0:undefined},median:function(a){return 0},mode:function(a){return 0},sample:function(c){return a.randn()*b.sqrt(c/(2*a.randg(c/2)))},variance:function(a){return a>2?a/(a-2):a>1?Infinity:undefined}}),a.extend(a.weibull,{pdf:function(a,c,d){return a<0?0:d/c*b.pow(a/c,d-1)*b.exp(-b.pow(a/c,d))},cdf:function(a,c,d){return a<0?0:1-b.exp(-b.pow(a/c,d))},inv:function(a,c,d){return c*b.pow(-b.log(1-a),1/d)},mean:function(b,c){return b*a.gammafn(1+1/c)},median:function(a,c){return a*b.pow(b.log(2),1/c)},mode:function(a,c){return c>1?a*b.pow((c-1)/c,1/c):undefined},sample:function(a,c){return a*b.pow(-b.log(b.random()),1/c)},variance:function(c,d){return c*c*a.gammafn(1+2/d)-b.pow(this.mean(c,d),2)}}),a.extend(a.uniform,{pdf:function(a,b,c){return a<b||a>c?0:1/(c-b)},cdf:function(a,b,c){return a<b?0:a<c?(a-b)/(c-b):1},mean:function(a,b){return.5*(a+b)},median:function(b,c){return a.mean(b,c)},mode:function(a,b){},sample:function(a,c){return a/2+c/2+(c/2-a/2)*(2*b.random()-1)},variance:function(a,c){return b.pow(c-a,2)/12}}),a.extend(a.binomial,{pdf:function(c,d,e){return e===0||e===1?d*e===c?1:0:a.combination(d,c)*b.pow(e,c)*b.pow(1-e,d-c)},cdf:function(b,c,d){var e=[],f=0;if(b<0)return 0;if(b<c){for(;f<=b;f++)e[f]=a.binomial.pdf(f,c,d);return a.sum(e)}return 1}}),a.extend(a.negbin,{pdf:function(c,d,e){return c!==c|0?!1:c<0?0:a.combination(c+d-1,c)*b.pow(1-e,d)*b.pow(e,c)},cdf:function(b,c,d){var e=0,f=0;if(b<0)return 0;for(;f<=b;f++)e+=a.negbin.pdf(f,c,d);return e}}),a.extend(a.hypgeom,{pdf:function(b,c,d,e){return b!==b|0?!1:b<0?0:a.combination(d,b)*a.combination(c-d,e-b)/a.combination(c,e)},cdf:function(b,c,d,e){var f=0,g=0;if(b<0)return 0;for(;g<=b;g++)f+=a.hypgeom.pdf(g,c,d,e);return f}}),a.extend(a.poisson,{pdf:function(c,d){return b.pow(d,c)*b.exp(-d)/a.factorial(c)},cdf:function(b,c){var d=[],e=0;if(b<0)return 0;for(;e<=b;e++)d.push(a.poisson.pdf(e,c));return a.sum(d)},mean:function(a){return a},variance:function(a){return a},sample:function(a){var c=1,d=0,e=b.exp(-a);do d++,c*=b.random();while(c>e);return d-1}}),a.extend(a.triangular,{pdf:function(a,b,c,d){return c<=b||d<b||d>c?undefined:a<b||a>c?0:a<=d?2*(a-b)/((c-b)*(d-b)):2*(c-a)/((c-b)*(c-d))},cdf:function(a,c,d,e){return d<=c||e<c||e>d?undefined:a<c?0:a<=e?b.pow(a-c,2)/((d-c)*(e-c)):1-b.pow(d-a,2)/((d-c)*(d-e))},mean:function(a,b,c){return(a+b+c)/3},median:function(a,c,d){if(d<=(a+c)/2)return c-b.sqrt((c-a)*(c-d))/b.sqrt(2);if(d>(a+c)/2)return a+b.sqrt((c-a)*(d-a))/b.sqrt(2)},mode:function(a,b,c){return c},sample:function(a,c,d){var e=b.random();return e<(d-a)/(c-a)?a+b.sqrt(e*(c-a)*(d-a)):c-b.sqrt((1-e)*(c-a)*(c-d))},variance:function(a,b,c){return(a*a+b*b+c*c-a*b-a*c-b*c)/18}})}(this.jStat,Math),function(a,b){var d=Array.prototype.push,e=a.utils.isArray;a.extend({add:function(b,c){return e(c)?(e(c[0])||(c=[c]),a.map(b,function(a,b,d){return a+c[b][d]})):a.map(b,function(a){return a+c})},subtract:function(b,c){return e(c)?(e(c[0])||(c=[c]),a.map(b,function(a,b,d){return a-c[b][d]||0})):a.map(b,function(a){return a-c})},divide:function(b,c){return e(c)?(e(c[0])||(c=[c]),a.multiply(b,a.inv(c))):a.map(b,function(a){return a/c})},multiply:function(b,c){var d,f,g,h,i=b.length,j=b[0].length,k=a.zeros(i,g=e(c)?c[0].length:j),l=0;if(e(c)){for(;l<g;l++)for(d=0;d<i;d++){h=0;for(f=0;f<j;f++)h+=b[d][f]*c[f][l];k[d][l]=h}return i===1&&l===1?k[0][0]:k}return a.map(b,function(a){return a*c})},dot:function(b,c){e(b[0])||(b=[b]),e(c[0])||(c=[c]);var d=b[0].length===1&&b.length!==1?a.transpose(b):b,f=c[0].length===1&&c.length!==1?a.transpose(c):c,g=[],h=0,i=d.length,j=d[0].length,k,l;for(;h<i;h++){g[h]=[],k=0;for(l=0;l<j;l++)k+=d[h][l]*f[h][l];g[h]=k}return g.length===1?g[0]:g},pow:function(c,d){return a.map(c,function(a){return b.pow(a,d)})},abs:function(c){return a.map(c,function(a){return b.abs(a)})},norm:function(a,c){var d=0,f=0;isNaN(c)&&(c=2),e(a[0])&&(a=a[0]);for(;f<a.length;f++)d+=b.pow(b.abs(a[f]),c);return b.pow(d,1/c)},angle:function(c,d){return b.acos(a.dot(c,d)/(a.norm(c)*a.norm(d)))},aug:function(a,b){var c=a.slice(),e=0;for(;e<c.length;e++)d.apply(c[e],b[e]);return c},inv:function(b){var c=b.length,d=b[0].length,e=a.identity(c,d),f=a.gauss_jordan(b,e),g=[],h=0,i;for(;h<c;h++){g[h]=[];for(i=d-1;i<f[0].length;i++)g[h][i-d]=f[h][i]}return g},det:function(a){var b=a.length,c=b*2,d=new Array(c),e=b-1,f=c-1,g=e-b+1,h=f,i=0,j=0,k;if(b===2)return a[0][0]*a[1][1]-a[0][1]*a[1][0];for(;i<c;i++)d[i]=1;for(i=0;i<b;i++){for(k=0;k<b;k++)d[g<0?g+b:g]*=a[i][k],d[h<b?h+b:h]*=a[i][k],g++,h--;g=--e-b+1,h=--f}for(i=0;i<b;i++)j+=d[i];for(;i<c;i++)j-=d[i];return j},gauss_elimination:function(c,d){var e=0,f=0,g=c.length,h=c[0].length,i=1,j=0,k=[],l,m,n,o;c=a.aug(c,d),l=c[0].length;for(;e<g;e++){m=c[e][e],f=e;for(o=e+1;o<h;o++)m<b.abs(c[o][e])&&(m=c[o][e],f=o);if(f!=e)for(o=0;o<l;o++)n=c[e][o],c[e][o]=c[f][o],c[f][o]=n;for(f=e+1;f<g;f++){i=c[f][e]/c[e][e];for(o=e;o<l;o++)c[f][o]=c[f][o]-i*c[e][o]}}for(e=g-1;e>=0;e--){j=0;for(f=e+1;f<=g-1;f++)j=k[f]*c[e][f];k[e]=(c[e][l-1]-j)/c[e][e]}return k},gauss_jordan:function(d,e){var f=a.aug(d,e),g=f.length,h=f[0].length;for(var i=0;i<g;i++){var j=i;for(var k=i+1;k<g;k++)b.abs(f[k][i])>b.abs(f[j][i])&&(j=k);var l=f[i];f[i]=f[j],f[j]=l;for(var k=i+1;k<g;k++){c=f[k][i]/f[i][i];for(var m=i;m<h;m++)f[k][m]-=f[i][m]*c}}for(var i=g-1;i>=0;i--){c=f[i][i];for(var k=0;k<i;k++)for(var m=h-1;m>i-1;m--)f[k][m]-=f[i][m]*f[k][i]/c;f[i][i]/=c;for(var m=g;m<h;m++)f[i][m]/=c}return f},lu:function(a,b){},cholesky:function(a,b){},gauss_jacobi:function(c,d,e,f){var g=0,h=0,i=c.length,j=[],k=[],l=[],m,n,o,p;for(;g<i;g++){j[g]=[],k[g]=[],l[g]=[];for(h=0;h<i;h++)g>h?(j[g][h]=c[g][h],k[g][h]=l[g][h]=0):g<h?(k[g][h]=c[g][h],j[g][h]=l[g][h]=0):(l[g][h]=c[g][h],j[g][h]=k[g][h]=0)}o=a.multiply(a.multiply(a.inv(l),a.add(j,k)),-1),n=a.multiply(a.inv(l),d),m=e,p=a.add(a.multiply(o,e),n),g=2;while(b.abs(a.norm(a.subtract(p,m)))>f)m=p,p=a.add(a.multiply(o,m),n),g++;return p},gauss_seidel:function(c,d,e,f){var g=0,h=c.length,i=[],j=[],k=[],l,m,n,o,p;for(;g<h;g++){i[g]=[],j[g]=[],k[g]=[];for(l=0;l<h;l++)g>l?(i[g][l]=c[g][l],j[g][l]=k[g][l]=0):g<l?(j[g][l]=c[g][l],i[g][l]=k[g][l]=0):(k[g][l]=c[g][l],i[g][l]=j[g][l]=0)}o=a.multiply(a.multiply(a.inv(a.add(k,i)),j),-1),n=a.multiply(a.inv(a.add(k,i)),d),m=e,p=a.add(a.multiply(o,e),n),g=2;while(b.abs(a.norm(a.subtract(p,m)))>f)m=p,p=a.add(a.multiply(o,m),n),g+=1;return p},SOR:function(c,d,e,f,g){var h=0,i=c.length,j=[],k=[],l=[],m,n,o,p,q;for(;h<i;h++){j[h]=[],k[h]=[],l[h]=[];for(m=0;m<i;m++)h>m?(j[h][m]=c[h][m],k[h][m]=l[h][m]=0):h<m?(k[h][m]=c[h][m],j[h][m]=l[h][m]=0):(l[h][m]=c[h][m],j[h][m]=k[h][m]=0)}p=a.multiply(a.inv(a.add(l,a.multiply(j,g))),a.subtract(a.multiply(l,1-g),a.multiply(k,g))),o=a.multiply(a.multiply(a.inv(a.add(l,a.multiply(j,g))),d),g),n=e,q=a.add(a.multiply(p,e),o),h=2;while(b.abs(a.norm(a.subtract(q,n)))>f)n=q,q=a.add(a.multiply(p,n),o),h++;return q},householder:function(c){var d=c.length,e=c[0].length,f=0,g=[],h=[],i,j,k,l,m;for(;f<d-1;f++){i=0;for(l=f+1;l<e;l++)i+=c[l][f]*c[l][f];m=c[f+1][f]>0?-1:1,i=m*b.sqrt(i),j=b.sqrt((i*i-c[f+1][f]*i)/2),g=a.zeros(d,1),g[f+1][0]=(c[f+1][f]-i)/(2*j);for(k=f+2;k<d;k++)g[k][0]=c[k][f]/(2*j);h=a.subtract(a.identity(d,e),a.multiply(a.multiply(g,a.transpose(g)),2)),c=a.multiply(h,a.multiply(c,h))}return c},QR:function(c,d){var e=c.length,f=c[0].length,g=0,h=[],i=[],j=[],k,l,m,n,o,p;for(;g<e-1;g++){l=0;for(k=g+1;k<f;k++)l+=c[k][g]*c[k][g];o=c[g+1][g]>0?-1:1,l=o*b.sqrt(l),m=b.sqrt((l*l-c[g+1][g]*l)/2),h=a.zeros(e,1),h[g+1][0]=(c[g+1][g]-l)/(2*m);for(n=g+2;n<e;n++)h[n][0]=c[n][g]/(2*m);i=a.subtract(a.identity(e,f),a.multiply(a.multiply(h,a.transpose(h)),2)),c=a.multiply(i,c),d=a.multiply(i,d)}for(g=e-1;g>=0;g--){p=0;for(k=g+1;k<=f-1;k++)p=j[k]*c[g][k];j[g]=d[g][0]/c[g][g]}return j},jacobi:function(c){var d=1,e=0,f=c.length,g=a.identity(f,f),h=[],i,j,k,l,m,n,o,p;while(d===1){e++,n=c[0][1],l=0,m=1;for(j=0;j<f;j++)for(k=0;k<f;k++)j!=k&&n<b.abs(c[j][k])&&(n=b.abs(c[j][k]),l=j,m=k);c[l][l]===c[m][m]?o=c[l][m]>0?b.PI/4:-b.PI/4:o=b.atan(2*c[l][m]/(c[l][l]-c[m][m]))/2,p=a.identity(f,f),p[l][l]=b.cos(o),p[l][m]=-b.sin(o),p[m][l]=b.sin(o),p[m][m]=b.cos(o),g=a.multiply(g,p),i=a.multiply(a.multiply(a.inv(p),c),p),c=i,d=0;for(j=1;j<f;j++)for(k=1;k<f;k++)j!=k&&b.abs(c[j][k])>.001&&(d=1)}for(j=0;j<f;j++)h.push(c[j][j]);return[g,h]},rungekutta:function(a,b,c,d,e,f){var g,h,i,j,k;if(f===2)while(d<=c)g=b*a(d,e),h=b*a(d+b,e+g),i=e+(g+h)/2,e=i,d+=b;if(f===4)while(d<=c)g=b*a(d,e),h=b*a(d+b/2,e+g/2),j=b*a(d+b/2,e+h/2),k=b*a(d+b,e+j),i=e+(g+2*h+2*j+k)/6,e=i,d+=b;return e},romberg:function(a,c,d,e){var f=0,g=(d-c)/2,h=[],i=[],j=[],k,l,m,n,o,p;while(f<e/2){o=a(c);for(m=c,n=0;m<=d;m+=g,n++)h[n]=m;k=h.length;for(m=1;m<k-1;m++)o+=(m%2!==0?4:2)*a(h[m]);o=g/3*(o+a(d)),j[f]=o,g/=2,f++}l=j.length,k=1;while(l!==1){for(m=0;m<l-1;m++)i[m]=(b.pow(4,k)*j[m+1]-j[m])/(b.pow(4,k)-1);l=i.length,j=i,i=[],k++}return j},richardson:function(a,c,d,e){function f(a,b){var c=0,d=a.length,e;for(;c<d;c++)a[c]===b&&(e=c);return e}var g=a.length,h=b.abs(d-a[f(a,d)+1]),i=0,j=[],k=[],l,m,n,o,p;while(e>=h)l=f(a,d+e),m=f(a,d),j[i]=(c[l]-2*c[m]+c[2*m-l])/(e*e),e/=2,i++;o=j.length,n=1;while(o!=1){for(p=0;p<o-1;p++)k[p]=(b.pow(4,n)*j[p+1]-j[p])/(b.pow(4,n)-1);o=k.length,j=k,k=[],n++}return j},simpson:function(a,b,c,d){var e=(c-b)/d,f=a(b),g=[],h=b,i=0,j=1,k;for(;h<=c;h+=e,i++)g[i]=h;k=g.length;for(;j<k-1;j++)f+=(j%2!==0?4:2)*a(g[j]);return e/3*(f+a(c))},hermite:function(a,b,c,d){var e=a.length,f=0,g=0,h=[],i=[],j=[],k=[],l;for(;g<e;g++){h[g]=1;for(l=0;l<e;l++)g!=l&&(h[g]*=(d-a[l])/(a[g]-a[l]));i[g]=0;for(l=0;l<e;l++)g!=l&&(i[g]+=1/(a[g]-a[l]));j[g]=(1-2*(d-a[g])*i[g])*h[g]*h[g],k[g]=(d-a[g])*h[g]*h[g],f+=j[g]*b[g]+k[g]*c[g]}return f},lagrange:function(a,b,c){var d=0,e=0,f,g,h=a.length;for(;e<h;e++){g=b[e];for(f=0;f<h;f++)e!=f&&(g*=(c-a[f])/(a[e]-a[f]));d+=g}return d},cubic_spline:function(b,c,d){var e=b.length,f=0,g,h=[],i=[],j=[],k=[],l=[],m=[],n=[];for(;f<e-1;f++)l[f]=b[f+1]-b[f];j[0]=0;for(f=1;f<e-1;f++)j[f]=3/l[f]*(c[f+1]-c[f])-3/l[f-1]*(c[f]-c[f-1]);for(f=1;f<e-1;f++)h[f]=[],i[f]=[],h[f][f-1]=l[f-1],h[f][f]=2*(l[f-1]+l[f]),h[f][f+1]=l[f],i[f][0]=j[f];k=a.multiply(a.inv(h),i);for(g=0;g<e-1;g++)m[g]=(c[g+1]-c[g])/l[g]-l[g]*(k[g+1][0]+2*k[g][0])/3,n[g]=(k[g+1][0]-k[g][0])/(3*l[g]);for(g=0;g<e;g++)if(b[g]>d)break;return g-=1,c[g]+(d-b[g])*m[g]+a.sq(d-b[g])*k[g]+(d-b[g])*a.sq(d-b[g])*n[g]},gauss_quadrature:function(){},PCA:function(b){var c=b.length,d=b[0].length,e=!1,f=0,g,h,i=[],j=[],k=[],l=[],m=[],n=[],o=[],p=[],q=[],r=[];for(f=0;f<c;f++)i[f]=a.sum(b[f])/d;for(f=0;f<d;f++){o[f]=[];for(g=0;g<c;g++)o[f][g]=b[g][f]-i[g]}o=a.transpose(o);for(f=0;f<c;f++){p[f]=[];for(g=0;g<c;g++)p[f][g]=a.dot([o[f]],[o[g]])/(d-1)}k=a.jacobi(p),q=k[0],j=k[1],r=a.transpose(q);for(f=0;f<j.length;f++)for(g=f;g<j.length;g++)j[f]<j[g]&&(h=j[f],j[f]=j[g],j[g]=h,l=r[f],r[f]=r[g],r[g]=l);n=a.transpose(o);for(f=0;f<c;f++){m[f]=[];for(g=0;g<n.length;g++)m[f][g]=a.dot([r[f]],[n[g]])}return[b,j,r,m]}}),function(b){for(var c=0;c<b.length;c++)(function(b){a.fn[b]=function(c,d){var e=this;return d?(setTimeout(function(){d.call(e,a.fn[b].call(e,c))},15),this):a(a[b](this,c))}})(b[c])}("add divide multiply subtract dot pow abs norm angle".split(" "))}(this.jStat,Math),function(a,b){var c=[].slice,d=a.utils.isNumber;a.extend({zscore:function(){var b=c.call(arguments);return d(b[1])?(b[0]-b[1])/b[2]:(b[0]-a.mean(b[1]))/a.stdev(b[1],b[2])},ztest:function(){var e=c.call(arguments);if(e.length===4){if(d(e[1])){var f=a.zscore(e[0],e[1],e[2]);return e[3]===1?a.normal.cdf(-b.abs(f),0,1):a.normal.cdf(-b.abs(f),0,1)*2}var f=e[0];return e[2]===1?a.normal.cdf(-b.abs(f),0,1):a.normal.cdf(-b.abs(f),0,1)*2}var f=a.zscore(e[0],e[1],e[3]);return e[1]===1?a.normal.cdf(-b.abs(f),0,1):a.normal.cdf(-b.abs(f),0,1)*2}}),a.extend(a.fn,{zscore:function(a,b){return(a-this.mean())/this.stdev(b)},ztest:function(c,d,e){var f=b.abs(this.zscore(c,e));return d===1?a.normal.cdf(-f,0,1):a.normal.cdf(-f,0,1)*2}}),a.extend({tscore:function(){var d=c.call(arguments);return d.length===4?(d[0]-d[1])/(d[2]/b.sqrt(d[3])):(d[0]-a.mean(d[1]))/(a.stdev(d[1],!0)/b.sqrt(d[1].length))},ttest:function(){var e=c.call(arguments),f;return e.length===5?(f=b.abs(a.tscore(e[0],e[1],e[2],e[3])),e[4]===1?a.studentt.cdf(-f,e[3]-1):a.studentt.cdf(-f,e[3]-1)*2):d(e[1])?(f=b.abs(e[0]),e[2]==1?a.studentt.cdf(-f,e[1]-1):a.studentt.cdf(-f,e[1]-1)*2):(f=b.abs(a.tscore(e[0],e[1])),e[2]==1?a.studentt.cdf(-f,e[1].length-1):a.studentt.cdf(-f,e[1].length-1)*2)}}),a.extend(a.fn,{tscore:function(a){return(a-this.mean())/(this.stdev(!0)/b.sqrt(this.cols()))},ttest:function(c,d){return d===1?1-a.studentt.cdf(b.abs(this.tscore(c)),this.cols()-1):a.studentt.cdf(-b.abs(this.tscore(c)),this.cols()-1)*2}}),a.extend({anovafscore:function(){var d=c.call(arguments),e,f,g,h,i,j,k,l;if(d.length===1){i=new Array(d[0].length);for(k=0;k<d[0].length;k++)i[k]=d[0][k];d=i}if(d.length===2)return a.variance(d[0])/a.variance(d[1]);f=new Array;for(k=0;k<d.length;k++)f=f.concat(d[k]);g=a.mean(f),e=0;for(k=0;k<d.length;k++)e+=d[k].length*b.pow(a.mean(d[k])-g,2);e/=d.length-1,j=0;for(k=0;k<d.length;k++){h=a.mean(d[k]);for(l=0;l<d[k].length;l++)j+=b.pow(d[k][l]-h,2)}return j/=f.length-d.length,e/j},anovaftest:function(){var b=c.call(arguments),e,f,g,h;if(d(b[0]))return 1-a.centralF.cdf(b[0],b[1],b[2]);anovafscore=a.anovafscore(b),e=b.length-1,g=0;for(h=0;h<b.length;h++)g+=b[h].length;return f=g-e-1,1-a.centralF.cdf(anovafscore,e,f)},ftest:function(b,c,d){return 1-a.centralF.cdf(b,c,d)}}),a.extend(a.fn,{anovafscore:function(){return a.anovafscore(this.toArray())},anovaftest:function(){var b=0,c;for(c=0;c<this.length;c++)b+=this[c].length;return a.ftest(this.anovafscore(),this.length-1,b-this.length)}}),a.extend({normalci:function(){var d=c.call(arguments),e=new Array(2),f;return d.length===4?f=b.abs(a.normal.inv(d[1]/2,0,1)*d[2]/b.sqrt(d[3])):f=b.abs(a.normal.inv(d[1]/2,0,1)*a.stdev(d[2])/b.sqrt(d[2].length)),e[0]=d[0]-f,e[1]=d[0]+f,e},tci:function(){var d=c.call(arguments),e=new Array(2),f;return d.length===4?f=b.abs(a.studentt.inv(d[1]/2,d[3]-1
    )*d[2]/b.sqrt(d[3])):f=b.abs(a.studentt.inv(d[1]/2,d[2].length)*a.stdev(d[2],!0)/b.sqrt(d[2].length)),e[0]=d[0]-f,e[1]=d[0]+f,e},significant:function(a,b){return a<b}}),a.extend(a.fn,{normalci:function(b,c){return a.normalci(b,c,this.toArray())},tci:function(b,c){return a.tci(b,c,this.toArray())}})}(this.jStat,Math);
 
// Work out which spans are lowest in the table.
// These are the spans that are tested within.
function getLowestLevelSpans(spans) {
    var lowest_spans = [];
    var found_indices = [];
    spans.forEach(function (span) {
        if (span.indices.every(function (x) { return found_indices.indexOf(x) == -1; })) {
            found_indices = found_indices.concat(span.indices);
            lowest_spans.push(span);
        }
    });
    return lowest_spans;
}
 
 
 
// checking suitability of data
var has_cc = table.statistics.indexOf('Column Comparisons') != -1;//checking if table has column letter showing
var has_average = table.statistics.indexOf('Average') != -1;//checking if table has averages
var has_percent = table.statistics.indexOf('%') != -1;
var has_column_percent = table.statistics.indexOf('Column %') != -1;
var has_row_percent = table.statistics.indexOf('Row %') != -1;
var has_column_n = table.availableStatistics.indexOf('Column n') != -1;
var has_column_se = table.availableStatistics.indexOf('Column Standard Error') != -1;
var blue_type = table.blueQuestion.questionType;
var has_below = false; 
try {has_below = below_table.availableStatistics.indexOf('Column Names') != -1;}  catch (e) { }
var has_cc_below = has_below && below_table.availableStatistics.indexOf('Column Comparisons') != -1;
var is_summary =  table.brownQuestion == "SUMMARY";
var two_d_types = ["Pick One - Multi", "Number - Grid", "Pick Any - Grid"];
var two_d = two_d_types.indexOf(blue_type) != -1; 
 var has_row_n = table.availableStatistics.indexOf('Row n') != -1;
var is_proportion = has_percent || has_column_percent || has_row_percent;
 
 
if (!has_average && !is_proportion)
  form.ruleNotApplicable("this rule requires the table to have percentages or an average");
 
// If column comparisons are not available for this table
// then stop.
if (table.availableStatistics.indexOf('Column Comparisons') == -1)
    form.ruleNotApplicable("this rule requires the table to have Column Comparisons available") ;
 
var n_columns = table.numberColumns;
var n_rows = table.numberRows;
 
//// Creating form.
 
var grid_checkbox = form.newCheckBox("gridCheck", "Apply to Number - Grid and Pick Any - Grid questions SUMMARYs");
grid_checkbox.lineBreakAfter = true;
grid_checkbox.setDefault(true);
var pick_one_multi_checkbox = form.newCheckBox("pickOneMultiCheck", "Apply to Pick One - Multi SUMMARYs");
pick_one_multi_checkbox.lineBreakAfter = true;
pick_one_multi_checkbox.setDefault(true);
var crosstab_checkbox = form.newCheckBox("crosstabCheck", "Apply to crosstabs");
crosstab_checkbox.lineBreakAfter = true;
crosstab_checkbox.setDefault(false);
var apply_without_cc_checkbox = form.newCheckBox("ccOverrideCheck" ,"Apply when Column Comparisons are not shown on the table");
apply_without_cc_checkbox.setDefault(false);
apply_without_cc_checkbox.lineBreakAfter = true;
var set_comparisons_checkbox = form.newCheckBox("comparisonsBox", "Set column comparisons (e.g: A/B, C/D)");
set_comparisons_checkbox.lineBreakAfter = true;
set_comparisons_checkbox.setDefault = false;
var manual_comparisons = set_comparisons_checkbox.getValue();
var comparisons_text = null;
if (manual_comparisons) {
    comparisons_text = form.newTextBox("comparisonsString");
    comparisons_text.lineBreakAfter = true;
}
var footer_checkbox = form.newCheckBox("footercheck", "Include settings information in footer");
footer_checkbox.setDefault = false;
footer_checkbox.lineBreakAfter = true;
 
 
var alpha_lower_label = form.newLabel('Significance Level (alpha) for lowercase letters:');
var alpha_lower_up_down = form.newNumericUpDown('alpha'); //creating a control where a user can enter a number.
alpha_lower_up_down.lineBreakAfter = true;
alpha_lower_up_down.setDefault(0.05);
alpha_lower_up_down.setIncrement(0.0000001); 
alpha_lower_up_down.setMinimum(0); 
alpha_lower_up_down.setMaximum(1); 
alpha_lower_up_down.lineBreakAfter = true;
var alpha_upper_label = form.newLabel('Significance Level (alpha) for UPPERCASE letters:');
var alpha_upper_up_down = form.newNumericUpDown('alphaUpper'); //creating a control where a user can enter a number.
alpha_upper_up_down.lineBreakAfter = true;
alpha_upper_up_down.setDefault(0.05);
alpha_upper_up_down.setIncrement(0.000000001); 
alpha_upper_up_down.setMinimum(0); 
alpha_upper_up_down.setMaximum(1); 
alpha_upper_up_down.lineBreakAfter = true;
 
var multiple_comparison_correction =  ["None", "False Discovery Rate", "Bonferroni"];
var multiple_comparison_dropdown = form.newComboBox('multiple',multiple_comparison_correction);
var multiple_comparison_wording = form.newLabel("Multiple comparison correction:");
multiple_comparison_dropdown.setDefault(multiple_comparison_correction[1]);
multiple_comparison_dropdown.lineBreakAfter = true;
 
var variance_assumptions =  ["Complex samples", "Unequal variance", "Equal variance (categories compared)", "Equal variance (all non-ignored categories)"]
var variance_dropdown = form.newComboBox('variance',variance_assumptions);
var variance_wording = form.newLabel("Numeric data variance assumptions (t-test):");
variance_dropdown.lineBreakAfter = true;
variance_dropdown.setDefault(variance_assumptions[0]);
 
var base_assumptions =  ["Sample size", "Weighted sample size", "Effective sample size"];
var base_dropdown = form.newComboBox('base', base_assumptions);
var base_wording = form.newLabel("Base used in tests of means:");
base_dropdown.lineBreakAfter = true;
base_dropdown.setDefault(base_assumptions[0]);
 
var proportion_tests =  ["Complex samples t-test", "Pooled z-test", "Un-pooled z-test"]
var proportion_dropdown = form.newComboBox('proportion',proportion_tests);
var proportion_wording = form.newLabel("Proportions test:");
proportion_dropdown.lineBreakAfter = true;
proportion_dropdown.setDefault(proportion_tests[0]);
 
var base_dropdown_proportions = form.newComboBox('baseProportions', base_assumptions);
var base_wording_proportions = form.newLabel("Base used in tests of proportions:");
base_dropdown_proportions.lineBreakAfter = true;
base_dropdown_proportions.setDefault(base_assumptions[0]);
 
var min_label = form.newLabel('Minimum sample size in a column to include in testing:');
var min_box = form.newTextBox('textMinimum');
min_box.setDefault("2");
min_box.lineBreakAfter = true;
 
// Columns to ignore from testing.
var label_ignore = form.newLabel('Columns to ignore from testing:');
var ignore_box = form.newTextBox('textIgnore');
ignore_box.setDefault("NET");
var ignore_box1 = form.newTextBox('textIgnore1');
ignore_box1.setDefault("SUM");
 
// Creating the form.
form.setHeading('Independent Samples Column Means and Proportions');
 
var control_array = [grid_checkbox, pick_one_multi_checkbox, crosstab_checkbox, apply_without_cc_checkbox, set_comparisons_checkbox];
if (manual_comparisons)
    control_array.push(comparisons_text);
control_array = control_array.concat([footer_checkbox, alpha_lower_label, alpha_lower_up_down, alpha_upper_label, alpha_upper_up_down, multiple_comparison_wording, 
                     multiple_comparison_dropdown,variance_wording, variance_dropdown,  
                     base_wording,base_dropdown,
                     proportion_wording, proportion_dropdown, 
                     base_wording_proportions, base_dropdown_proportions,
                     min_label, min_box, 
                     label_ignore, ignore_box, ignore_box1]);
form.setInputControls(control_array);
 
// Adding textboxes if required.
var labels_to_ignore = [ignore_box.getValue()];
var last_selection = ignore_box1.getValue();
while (last_selection != "") {
    labels_to_ignore.push(last_selection);
    var new_name = "textIgnore" + (control_array.length - 1);
    var new_menu = form.newTextBox(new_name);
    last_selection = new_menu.getValue();
    control_array.push(new_menu);
    form.setInputControls(control_array);
}
 
// Getting alpha and creating summary.
var lower_alpha = alpha_lower_up_down.getValue();
var upper_alpha = alpha_upper_up_down.getValue();
var alpha = Math.max(lower_alpha, upper_alpha);
var mean_assumption = variance_dropdown.requireValue();
var proportion_assumption = proportion_dropdown.requireValue()
form.setSummary("Independent Samples Column Means and Proportions Tests");
 
// Make sure column comparisons are shown on the table, unless user overrides
if (!has_cc && !apply_without_cc_checkbox.getValue())
   form.ruleNotApplicable("this rule requires the table has 'Show significance' set to 'Compare columns' and that 'Column Comparisons' is selected in 'Statistics - Cells'");
 
 
 
var min_sample_size = min_box.getValue();
// Checking to see if rule needs to be applied.
// performing the testing    
var grid_summary = (table.blueQuestion.questionType == "Pick Any - Grid" ||  table.blueQuestion.questionType == "Number - Grid") && table.brownQuestion == "SUMMARY";
var pick_multi_summary = table.blueQuestion.questionType == "Pick One - Multi" && table.brownQuestion == "SUMMARY";
if (pick_multi_summary) {
    if(!pick_one_multi_checkbox.getValue())
        form.ruleNotApplicable("the option 'Apply to Pick One - Multi SUMMARY tables' is not selected");
} else if (grid_summary) {
    if (!grid_checkbox.getValue())
        form.ruleNotApplicable("the option 'Apply to Number - Grid and Pick Any - Grid questions SUMMARYs' is not selected");
} else if (!crosstab_checkbox.getValue())
    form.ruleNotApplicable("the option 'Apply to crosstabs' is not selected");
 
table.hypothesisTestingEnabled = false;
 
var comparisons_string = manual_comparisons ? comparisons_text.getValue() : "";
 
// Getting data.
var sd = has_average ? table.get('Standard Deviation') : null;
var sd_below = has_cc_below ? below_table.get('Standard Deviation') : null;
var standard_error = has_column_se ? table.get('Column Standard Error') : table.get('Standard Error');
var standard_error_below = has_cc_below ? below_table.get('Standard Error') : null;
var cc = table.get('Column Comparisons');    
var cc_below = has_cc_below ? below_table.get('Column Comparisons') : null; 
 
var base_type = (has_average ? base_dropdown.getValue() : base_dropdown_proportions.getValue());
var weighted_n = base_type == base_assumptions[1];
var effective_n = base_type == base_assumptions[2];
 
var base;
if (two_d) {
    if (weighted_n) 
        base = table.get("Base Population");
    else if (effective_n)
        base = table.get("Effective Base n");
    else
        base = table.get("Base n");
} else {
    if (weighted_n)
        base = table.get("Column Population");
    else if (effective_n) {
        var col_pop = table.get("Column Population");
        var base_pop = table.get("Base Population");
        base = table.get("Effective Base n");
        for (var r = 0; r < n_rows; r++)
            for (var c = 0; c < n_columns; c++)
                base[r][c] *= col_pop[r][c] / base_pop[r][c];

    }
    else
        base = table.get("Column n");
}
var base_below;
 
if (has_cc_below) {
 
    if (base_dropdown.getValue() == base_assumptions[1]) // Weighted n.
        base_below = below_table.get("Column Population");
    else if (base_dropdown.getValue() == base_assumptions[2]) { // Effective sample size.
        var missing_statistics_below = below_table.availableStatistics.indexOf('Effective Base n') == -1;
        if (missing_statistics_below && table.netRows.length != 1)
            form.ruleNotApplicable("requires the NET row to be shown on the table for when 'Effective sample size' is selected above");
        var has_no_column_pop = below_table.availableStatistics.indexOf("Column Population") == -1;
        var col_pop = below_table.get("Column Population");
        var base_pop = below_table.get("Base Population");
        if (missing_statistics_below) { 
            var effective_base_n = table.get("Effective Base n");
            var base_below = [new Array(n_columns)];
            var net_row = table.netRows[0]; // Effective sample size must be obtained from the NET row
            for (var c = 0; c < n_columns; c++)
                base_below[0][c] = effective_base_n[net_row][c] * (has_no_column_pop ? 1 : col_pop[0][c] / base_pop[0][c]); //When Column Population is not shown on tables (eg for Pick One - Multi) the cokumn population is the same as the base population and so no adjustement needs to be made
        } else {
            var effective_base_n = below_table.get('Effective Base n');
            var base_below = [new Array(n_columns)];
            for (var c = 0; c < n_columns; c++)
                base_below[0][c] = effective_base_n[0][c] * (has_no_column_pop ? 1 : col_pop[0][c] / base_pop[0][c]); 
        }
    }
    else
        base_below = below_table.get("Column n");
}
 
 
 
var values;
if (has_average)
    values = table.get('Average');
else {
    if (has_column_percent)
        values = table.get('Column %');
    else if (has_row_percent)
        values = table.get('Row %');
    else 
        values = table.get("%");
}
var values_below = has_cc_below ? below_table.get('Average') : null;
 
var cnames = table.get('Column Names');    
var columns_compared = table.get('Columns Compared');
var cnames_below = has_below ? below_table.get('Column Names') : null;    
var columns_compared_below = has_below ? below_table.get('Columns Compared') : null; 
 
// Dealing with inconsistent structuring of arrays in statistics below
var cc_transposed_below = has_below && cc_below != null && cc_below[0].length != table.numberColumns;
var cnames_transposed_below = has_below && cnames_below != null && cnames_below[0].length != table.numberColumns;
var columns_compared_transposed_below = has_below && columns_compared_below != null && columns_compared_below[0].length != table.numberColumns;
 
cc_below_column = function(c) {return cc_transposed_below ? 0 : c};
cc_below_row = function(c) {return cc_transposed_below ? c : 0};
 
cnames_below_column = function(c) {return cnames_transposed_below ? 0 : c};
cnames_below_row = function(c) {return cnames_transposed_below ? c : 0};
 
columns_compared_below_column = function(c) {return columns_compared_transposed_below ? 0 : c};
columns_compared_below_row = function(c) {return columns_compared_transposed_below ? c : 0};
 
// write results        
if (n_columns > 260)
  form.ruleNotApplicable("this rule only works with 260 or fewer columns");
 
 
 
 
// creating column letters
var LETTERS = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
var letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
if (n_columns > 26) {
    var new_lower = new Array(260);
    var new_upper = new Array(260);
    for (var l = 0; l < 26; l++)
        for (var i = 0; i < 10; i++) {
            var counter = i * 26 + l;
            new_upper[counter] = LETTERS[l] + i;
            new_lower[counter] = letters[l] + i;
        }
    LETTERS = new_upper;
    letters = new_lower;
}
 
 
// Work out the spans in the table.
// Where the comparisons have been specified manually, ignore the existing
// spans and use the manual comparisons to define psuedo-spans.
var spans;
var indices_not_in_spans = [];
if (manual_comparisons) {
    // Reset column names in table and below as this option ignores the spans
    for (var col = 0; col < table.numberColumns; col++) {
        for (var row = 0; row < table.numberRows; row++) {
            cnames[row][col] = LETTERS[col];
        }
        if (has_below)
            cnames_below[cnames_below_row(col)][cnames_below_column(col)] = LETTERS[col];
    }
    // Interepret the comparisons from the user's input
    var comparison_strings = comparisons_string.split(",");
    var comparisons = comparison_strings.map(function (string) { return string.trim().split("/"); });
    spans = comparisons.map(function (a) {
        return { indices: a.map(function (chr) { return letters.indexOf(chr.toLowerCase()) } ) };
    });
    var valid_spans = spans.map(function (span) { return span.indices.indexOf(-1) == -1 && span.indices.every(function (x) { return x < n_columns;} ); });
    valid_spans.forEach(function (x, ind) {
        if (!x)
            form.ruleNotApplicable("unable to interpret comparison '" + comparison_strings[ind] + "'");
    });
    var indices_included = [];
} else {
    spans = table.columnSpans;
    // If no spans then inlude all columns
    if (spans.length == 0) {
        var indices = new Array(n_columns);
        for (var i = 0; i < n_columns; i++)
            indices[i] = i;
        spans = [{"indices": indices}];
    } else {
        spans = getLowestLevelSpans(spans);
    }
}
// Figure out which indices aren't in spans so that we
// replace the default column comparisons results with
// the no-test symbol.
var indices_included = [];
spans.forEach(function (span) {
    indices_included = indices_included.concat(span.indices);
});
for (var j = 0; j < n_columns; j++) {
    if (indices_included.indexOf(j) == -1)
        indices_not_in_spans.push(j);
}
var n_spans = spans.length;
 
// Clearing out Statistics - Below.
if (has_below)
    for (var c = 0; c < n_columns; c++) {
        columns_compared_below[columns_compared_below_row(c)][columns_compared_below_column(c)] = "";
        if (has_cc_below) {
            cc_below[cc_below_row(c)][cc_below_column(c)] = "";
        }
}
 
// Identifying NET columns
var column_labels = table.columnLabels;
var not_net_columns = new Array(n_columns);
for (var c = 0; c < n_columns; c++) {
    var column_label = column_labels[c];
    column_label = column_label.split('\r\n')[0]; // Removing sample size or column name letters
    not_net_columns[c] = labels_to_ignore.indexOf(column_label) == -1;
}
 
// Preliminary calculations and clearing out some arrays
var net_columns = new Array(n_spans);
var group_var = new Array(n_rows);
var dfs = new Array(n_rows);
var gs = new Array(n_rows);
var variances = new Array(n_rows);
var valid = new Array(n_rows);
 
var group_var_below = new Array(1);
var dfs_below = new Array(1);
var gs_below = new Array(1);
var variances_below = new Array(1);
var valid_below = new Array(1);
 
var no_test_symbol = "-";
var start_r = has_cc_below ? -1 : 0; //using -1 for statistics - below
for (var r = start_r; r < n_rows; r++) {
    var marginal = r == -1;
    var group_var_row = new Array(n_spans);
    var n_row = new Array(n_spans);
    var df_row = new Array(n_spans);
    var g_row = new Array(n_spans);
    var variances_row = new Array(n_columns);
    var valid_row = Array(n_spans);
    for (var span = 0; span < n_spans; span++) {
        var current_indices = spans[span].indices;
        var n_columns_in_span = current_indices.length;
        var g = 0;
        var net = false;
        var ss = 0.0;
        var n = 0;
        for (var c = 0; c < n_columns_in_span; c++) {
            var column = current_indices[c];
            var this_base = marginal ? base_below[cnames_below_row(column)][cnames_below_column(column)] : base[r][column];
            var vald = not_net_columns[column] && this_base >= min_sample_size;
            valid_row[column] = vald;
            if (vald) {
                g++;
                var standard_dev, variance ;
                if (has_average || marginal) {
                    standard_dev =  marginal ? sd_below[cc_below_row(column)][cc_below_column(column)] : sd[r][column];
                    variance = standard_dev * standard_dev; 
                    var n_group = marginal ? base_below[cc_below_row(column)][cc_below_column(column)] : base[r][column];
                    if (!isNaN(variance))
                        ss += (n_group - 1) * variance;
                    n += n_group;
                } 
                variances_row[column] = variance;
                if (marginal) 
                    cc_below[cc_below_row(column)][cc_below_column(column)] = "";
                else 
                    cc[r][column] = "";                    
            } else {
                if (marginal)
                    cc_below[cc_below_row(column)][cc_below_column(column)] = no_test_symbol;
                else
                    cc[r][column] = no_test_symbol;
            }
            if (marginal) {
                if (manual_comparisons)
                    cnames_below[cnames_below_row(column)][cnames_below_column(column)] = LETTERS[column];
                else
                    cnames_below[cnames_below_row(column)][cnames_below_column(column)] = LETTERS[c];
                columns_compared_below[columns_compared_below_row(column)][columns_compared_below_column(column)] = "";
            } else {
                if (manual_comparisons)
                    cnames[r][column] = LETTERS[column];
                else
                    cnames[r][column] = LETTERS[c];
                columns_compared[r][column] = "";
            }
        }
        var df = n - g;
        g_row[span] = g;
        df_row[span] = df;
        group_var_row[span] = ss / df;
    }
 
    if (marginal) {        
        group_var_below[0] = group_var_row;
        gs_below[0] = g_row;
        dfs_below[0] = df_row;
        variances_below[0] = variances_row;
        valid_below[0] = valid_row;
    } else {
        group_var[r] = group_var_row;
        gs[r] = g_row;
        dfs[r] = df_row;
        variances[r] =  variances_row;
        valid[r] = valid_row;
    }
    // Columns which are not included in testing should be given symbols for no testing
    // and blanks for columns compared
    indices_not_in_spans.forEach(function (column) {
        if (marginal)
            cc_below[cc_below_row(column)][cc_below_column(column)] = no_test_symbol;
        else
            cc[r][column] = no_test_symbol;
        if (marginal)
            columns_compared_below[columns_compared_below_row(column)][columns_compared_below_column(column)] = "";
        else
            columns_compared[r][column] = "";
    });
}
var bonferroni = multiple_comparison_dropdown.getValue() == "Bonferroni";
var fdr = multiple_comparison_dropdown.getValue() == "False Discovery Rate";
var no_correction = !bonferroni && !fdr;
 
 
for (var r = start_r; r < table.numberRows; r++) {
    for (var span = 0; span < n_spans; span++) {
        var marginal = r == -1;
        var current_indices = spans[span].indices;
        var n_columns_in_span = current_indices.length;
        // performing comparisons
        var df = dfs[r];
        var ps = new Array(n_columns_in_span);
        var p_list = [];
        for (var right_c = 1; right_c < n_columns_in_span; right_c++) {
            ps[right_c] = new Array(n_columns_in_span);
            for (var left_c = 0; left_c < right_c; left_c ++) {
                var left_column = current_indices[left_c];
                var right_column = current_indices[right_c];
                var left_row_below = cnames_below_row(left_column);
                var left_column_below = cnames_below_column(left_column);
                var right_row_below = cnames_below_row(right_column);
                var right_column_below = cnames_below_column(right_column);
                var left_valid = marginal ? valid_below[0][left_column] : valid[r][left_column] ;
                var right_valid = marginal ? valid_below[0][right_column] : valid[r][right_column];
                if (left_valid && right_valid) {
                    var left_n =  marginal ? base_below[left_row_below][left_column_below] : base[r][left_column];
                    var right_n = marginal ? base_below[right_row_below][right_column_below] : base[r][right_column];
                    var left_value = marginal ? values_below[left_row_below][left_column_below] : values[r][left_column];
                    var right_value = marginal ? values_below[right_row_below][right_column_below] : values[r][right_column];
                    if (!has_average && !marginal) {
                        left_value /= 100;
                        right_value /= 100;
                    }
                    var net_column = net_columns[span];
                    var degrees_of_freedom, se, p;
                    var diff = right_value - left_value;
                    if(has_average || marginal) {
                        if (mean_assumption == "Complex samples") {
                            var left_se = marginal ? standard_error_below[left_row_below][left_column_below] : standard_error[r][left_column];
                            var right_se = marginal ? standard_error_below[right_row_below][right_column_below] : standard_error[r][right_column];
                            var standard_error_1_squared = Math.pow(left_se, 2);
                            var standard_error_2_squared = Math.pow(right_se, 2);
                            var D1 = Math.pow(standard_error_1_squared, 2) / (left_n - 1);
                            var D2 = Math.pow(standard_error_2_squared, 2) / (right_n - 1);
                            se = Math.pow(standard_error_1_squared + standard_error_2_squared, 0.5);
                            degrees_of_freedom = Math.pow(standard_error_1_squared + standard_error_2_squared, 2) / (D1 + D2); 
                        } else if (mean_assumption == "Unequal variance") {
                            var var_left = marginal ? variances_below[0][left_column] : variances[r][left_column];
                            var var_right = marginal ? variances_below[0][right_column] : variances[r][right_column];
                            var standard_error_1_squared = var_left / left_n; 
                            var standard_error_2_squared = var_right / right_n;
                            var D1 = Math.pow(standard_error_1_squared, 2) / (left_n - 1); 
                            var D2 = Math.pow(standard_error_2_squared, 2) / (right_n - 1);
                            degrees_of_freedom = Math.pow(standard_error_1_squared + standard_error_2_squared, 2) / (D1 + D2);
                            se = Math.sqrt(standard_error_1_squared + standard_error_2_squared);
                        } else if (mean_assumption ==  "Equal variance (categories compared)") {
                            var var_left = marginal ? variances_below[0][left_column] : variances[r][left_column];
                            var var_right = marginal ? variances_below[0][right_column] : variances[r][right_column];
                            degrees_of_freedom = left_n + right_n - 2;
                            var s_2 = ((left_n - 1)* var_left + (right_n - 1) * var_right) / degrees_of_freedom;
                            se = Math.sqrt(s_2 *  (1 / left_n +  1 / right_n));
                        } else { // "Equal variance (all non-ignored categories)"
                            var total_var = marginal ? group_var_below[0][span] : group_var[r][span];
                            se = Math.sqrt(total_var * (1 / left_n +  1 / right_n));
                            degrees_of_freedom = marginal ? dfs_below[0][span] : dfs[r][span];
                        }
                        var t = se <= 0.0 ? 0 : diff / se ;
                        p = 2 * (1 - jStat.studentt.cdf(Math.abs(t), degrees_of_freedom));
                    } else { // Proportions test
                        if (marginal) {
                            alert("Error");
                        } else {
                        // Marginal statistics cannot be proportions for testing purposes.
                            if (proportion_assumption == proportion_tests[0]) { // Complex samples t-test.
                                var left_se = standard_error[r][left_column];
                                var right_se = standard_error[r][right_column];
                                var standard_error_1_squared = Math.pow(left_se, 2);
                                var standard_error_2_squared = Math.pow(right_se, 2);
                                se = Math.pow(standard_error_1_squared + standard_error_2_squared, 0.5);
                                degrees_of_freedom = left_n + right_n - 1;
                            } else if (proportion_assumption == proportion_tests[1]) { // Pooled z-test.
                                var pooled_proportion = (left_value * left_n + right_value * right_n) / (left_n + right_n);
                                se = Math.sqrt(pooled_proportion * (1 - pooled_proportion) * (1 / left_n +  1 / right_n));
                            } else { // Un-pooled z-test
                                se = Math.sqrt(left_value * (1 - left_value)/ left_n + right_value * (1 - right_value)/ right_n);
                            }
                            var z = se <= 0.0 ? 0 : diff / se;
                            if (proportion_assumption == proportion_tests[0])
                                p = 2 * (1 - jStat.studentt.cdf(Math.abs(z), degrees_of_freedom));
                            else
                                p = 2 * (1 - jStat.normal.cdf(Math.abs(z), 0, 1));
 
                        }
                    }
                    if (!no_correction && diff != 0)
                        p_list.push(p);
                    ps[right_c][left_c] = p;
                }
            }
        }
        // multiple comparison corrections
        var n_pvalues = p_list.length;
        if (n_pvalues > 0 || no_correction) {
            var m = 1;
            if (!no_correction) {
 
                if (bonferroni || n_pvalues == 1) 
                    m = n_pvalues;
                else if (fdr) {
                    var sorted_pvalues = p_list.sort(function(x, y) { return x - y });
                    var new_alpha = alpha / n_pvalues;
                    for (var i=0; i<n_pvalues; i++) {
                        var temp_alpha = alpha * (1 + i) / n_pvalues;
                        var cur_p = sorted_pvalues[i];
                        if (cur_p >= alpha)
                            break;
                        if (cur_p <= temp_alpha )
                            new_alpha = temp_alpha;
                    }
                    m = alpha / new_alpha;
                }
            }
 
            // writing results
            // Work out the set of letters in this span.
            var SPAN_LETTERS = manual_comparisons ? current_indices.map(function (x) { return LETTERS[x]; }) : LETTERS;
            var span_letters = manual_comparisons ? current_indices.map(function (x) { return letters[x]; }) : letters;
            for (var right_c = 1; right_c < n_columns_in_span; right_c++) {
                for (var left_c = 0; left_c < right_c; left_c ++) {
                    var left_column = current_indices[left_c];
                    var right_column = current_indices[right_c];
                    if (not_net_columns[left_column]  && not_net_columns[right_column]) {
                        var marginal = r == -1;
                        var left_value = marginal ? values_below[cnames_below_row(left_column)][cnames_below_column(left_column)] : values[r][left_column];
                        var right_value = marginal ? values_below[cnames_below_row(right_column)][cnames_below_column(right_column)] : values[r][right_column];
                        var p = ps[right_c][left_c];
                        p *= m; //applying bonferroni correction (if required)
                        if (p <= alpha){
                            if (left_value === undefined || right_value === undefined)
                                throw new Error('undefined values');
                            var left_lower = left_value < right_value;
                            var letter_column = left_lower ? left_c : right_c;
                            var insertion_column = left_lower ? right_column : left_column;
                            var letter = p <= upper_alpha ? SPAN_LETTERS[letter_column] : span_letters[letter_column];
                            if (marginal)
                                cc_below[cc_below_row(insertion_column)][cc_below_column(insertion_column)] += letter + " ";
                            else
                                cc[r][insertion_column] += letter + " ";
                        }
                        if (marginal) {
                            columns_compared_below[columns_compared_below_row(left_column)][columns_compared_below_column(left_column)] += SPAN_LETTERS[right_c];
                            columns_compared_below[columns_compared_below_row(right_column)][columns_compared_below_column(right_column)] += SPAN_LETTERS[left_c];
                        } else {
                            columns_compared[r][left_column] += SPAN_LETTERS[right_c];
                            columns_compared[r][right_column] += SPAN_LETTERS[left_c];
                        }
                    }
                }
            }
        }
    }
}
 
// If there are spans in the table, then any columns not in spans should
// get the no-test symbol.
for (var r = start_r; r < table.numberRows; r++) {
    var marginal = r == -1;
    indices_not_in_spans.forEach(function (column) {
        if (marginal)
            cc_below[cc_below_row(column)][cc_below_column(column)] = no_test_symbol;
        else
            cc[r][column] = no_test_symbol;
    });
}
 
 
// Writing results back to the table.
table.set('Column Comparisons', cc);
table.set('Column Names', cnames);
table.set('Columns Compared', columns_compared);

// No column comparisons below the table, but Column Names
// still need to be updated to reflect those used in the 
// cells of the table
if (has_below && !has_cc_below) {
    for (var c = 0; c < table.numberColumns; c++)
        cnames_below[cnames_below_row(c)][cnames_below_column(c)] = cnames[0][c]
}
 
if (has_below) {
    below_table.set('Column Names', cnames_below);
    if (has_cc_below) {
        if (below_table.numberColumns != cc_below[0].length)
            cc_below = Q.transpose(cc_below);
        below_table.set('Column Comparisons', cc_below);
    }
    below_table.set('Columns Compared', columns_compared_below);
}
 
// Construct footer to indicate testing options
var footers = table.extraFooters;
 
if (footer_checkbox.getValue()) {   
    if (manual_comparisons)
        footers.push("Comparisons: " + comparisons_string.toUpperCase());
 
    if (upper_alpha < lower_alpha)
        footers.push("Lower case letters: p < " + lower_alpha);
 
    footers.push("Uppercase letters: p < " + upper_alpha);
 
    if (!no_correction)
        footers.push("Multiple comparison correction: " + multiple_comparison_dropdown.getValue());
 
    footers.push("Numeric data variance assumption (t-test): " + variance_dropdown.getValue());
    footers.push("Base used in tests of means: " + base_dropdown.getValue());
    footers.push("Proportions test: " + proportion_dropdown.getValue());
    footers.push("Base used in tests of proportions: " + base_dropdown_proportions.getValue());
    footers.push("Minimum column sample size used in testing: " + min_box.getValue());
    footers.push("Columns ignored: " + labels_to_ignore.join(", "));
}
 
footers.push("Columns are assumed to be independent in statistical testing");
 
table.extraFooters = footers;

See also