barnardsTest()

Computes Barnard’s exact test for two nominal, dichotomous (i.e. no more than two manifestations) variables firstColumn and secondColumn. Its result gives the exact probability that the particular observations in the given data can be made if the null hypothesis that both variables are independent from another holds true. It is a more powerful alternative to Fisher’s exact test, however it is computationally more resource demanding. For this reason, this method will return undefined and log an error message if the total number observations exceeds the value for the setting maxBarnardsN (default: 200) which can be changed upon initialisation of this library.

This method returns an object storing the Wald statistic wald, the one- and two-tailed significance and the nuisance parameter nuisance for which this significance is largest.
Parameters:
(string) firstColumn (required)
(string) secondColumn (required)
Usage:
var testData = [
	{ gender: 'female', smoker: 'yes' },
	{ gender: 'female', smoker: 'yes' },
	{ gender: 'female', smoker: 'no' },
	{ gender: 'female', smoker: 'no' },
	{ gender: 'female', smoker: 'no' },
	{ gender: 'female', smoker: 'no' },
	{ gender: 'female', smoker: 'no' },
	{ gender: 'female', smoker: 'no' },
	{ gender: 'male', smoker: 'yes' },
	{ gender: 'male', smoker: 'yes' },
	{ gender: 'male', smoker: 'yes' },
	{ gender: 'male', smoker: 'yes' },
	{ gender: 'male', smoker: 'yes' },
	{ gender: 'male', smoker: 'yes' },
	{ gender: 'male', smoker: 'yes' },
	{ gender: 'male', smoker: 'no' },
	{ gender: 'male', smoker: 'no' }
];

var testVars = {
	gender: {
		scale: 'nominal',
		valueMap: ['male', 'female']
	},
	smoker: {
		scale: 'nominal',
		valueMap: ['yes', 'no']
	}
};

var stats = new Statistics(testData, testVars);
var barnard = stats.barnardsTest('gender', 'smoker');
Returns:
{
	wald: -2.17608…,
	nuisance: 0.50001…,
	pOneTailed: 0.02452…,
	pTwoTailed: 0.04904…
}

binomialTest()

Computes the binomial test for the values of a nominal, dichotomous (i.e. no more than two manifestations) variable data that can be either the name of a column (string) or an array of values. Its result is the probability that the observation of the values of data could be made if the event valueToTest is assumed to happen with a chance of alpha. This method returns the test result for exactly, fewer (or exactly), and more (or exactly) as many events valueToTest can be observed.
Parameters:
(string or array) data (required)
(string) valueToTest (required)
(integer or float) alpha (optional, default: 0.5)
Scales of measure:
nominal, ordinal
Usage:
// results of a medical procedure - probability to survive: 80%
// how likely is it that only 5/12 patients survived?
var testData = [
	{ survival: 'no' },
	{ survival: 'no' },
	{ survival: 'no' },
	{ survival: 'no' },
	{ survival: 'no' },
	{ survival: 'no' },
	{ survival: 'no' },
	{ survival: 'yes' },
	{ survival: 'yes' },
	{ survival: 'yes' },
	{ survival: 'yes' },
	{ survival: 'yes' }
];

var testVars = { survival: { scale: 'nominal', valueMap: ['no', 'yes'] }};

var stats = new Statistics(testData, testVars);
var binomial = stats.binomialTest('survival', 'yes', 0.8);
Returns:
{
	pExactly: 0.00332…,
	pFewer: 0.00058…,
	pAtMost: 0.00390…,
	pMore: 0.99609…,
	pAtLeast: 0.99941…
}

chiSquaredTest()

Computes the Chi-squared test for two ordinal or nominal variables firstColumn and secondColumn. Its result gives the exact probability that the particular observations in the given data can be made if the null hypothesis that both variables are independent from another holds true.
Parameters:
(string) firstColumn (required)
(string) secondColumn (required)
Scales of measure:
nominal, ordinal
Usage:
var testData = [
	{ gender: 'male', voting: 'republican' }, …, // 140 times
	{ gender: 'male', voting: 'democrat' }, …, // 150 times
	{ gender: 'male', voting: 'independent' }, …, // 50 times
	{ gender: 'female', voting: 'republican' }, …, // 90 times
	{ gender: 'female', voting: 'democrat' }, …, // 150 times
	{ gender: 'female', voting: 'independent' }, … // 50 times
];

var testVars = {
	gender: {
		scale: 'nominal',
		valueMap: ['male', 'female']
	},
	voting: {
		scale: 'nominal',
		valueMap: ['republican', 'democrat', 'independent']
	}
};

var stats = new Statistics(testData, testVars);
var chiSquared = stats.chiSquaredTest('gender', 'voting');
Returns:
{
	PearsonChiSquared: 6.94505…,
	degreesOfFreedom: 2,
	significance: 0.03103…
}

fishersExactTest()

Computes Fisher’s exact test for two nominal, dichotomous (i.e. no more than two manifestations) variables firstColumn and secondColumn. Its result gives the exact probability that the particular observations in the given data can be made if the null hypothesis that both variables are independent from another holds true. In the example given below, it means that if men and women were equally likely to be smokers there is about 4.9% chance that the supplied observations stated in testData can be made. If the level of significance (expressed as the p value) was agreed to be larger than this value, then the data suggest that this hypothesis of equally likelihood to be a smoker should be rejected.
Parameters:
(string) firstColumn (required)
(string) secondColumn (required)
Scales of measure:
nominal, ordinal
Usage:
var testData = [
	{ gender: 'male', smoker: false },
	{ gender: 'male', smoker: true },
	{ gender: 'female', smoker: false },
	{ gender: 'male', smoker: true },
	{ gender: 'female', smoker: false },
	{ gender: 'female', smoker: false },
	{ gender: 'male', smoker: false },
	{ gender: 'male', smoker: true },
	{ gender: 'female', smoker: false },
	{ gender: 'female', smoker: false },
	{ gender: 'male', smoker: true },
	{ gender: 'male', smoker: true },
	{ gender: 'female', smoker: false },
	{ gender: 'female', smoker: true }
];

var testVars = {
	 gender: {
		scale: 'nominal',
		valueMap: ['male', 'female']
	},
	smoker: {
		scale: 'nominal',
		valueMap: [true, false]
	}
};

var stats = new Statistics(testData, testVars);
var fishersExactTest = stats.fishersExactTest('gender', 'smoker');
Returns:
0.04895…

mannWhitneyU()

Performs the Mann-Whitney U test on the values of dependentColumn assigned to the two groups given by the variable independentColumn. It tests the null hypothesis that it is equally likely that a randomly selected sample from one group is less or greater than another from the other group. This test compares the rank sums of the observed data and computes a statistic that is normally distributed, however does not require the assumption of a normally distributed variable. This method returns the Mann-Whitney U along with its associated z-score and both p-values for one- and two-sided null hypotheses.
Parameters:
(string) independentColumn (required)
(string) dependentColumn (required)
Scales of measure:
ordinal, interval, metric
Usage:
var testData = [
	{ gender: 'male', income: 2150 },
	{ gender: 'female', income: 1800 },
	{ gender: 'male', income: 2300 },
	{ gender: 'female', income: 1600 },
	{ gender: 'female', income: 1700 },
	{ gender: 'male', income: 2000 },
	{ gender: 'female', income: 1850 },
	{ gender: 'female', income: 2200 },
	{ gender: 'female', income: 1750 },
	{ gender: 'male', income: 2050 },
];

var testVars = {
	gender: 'nominal',
	income: 'metric'
}

var stats = new Statistics(testData, testVars);
var u = stats.mannWhitneyU('gender', 'income'));
Returns:
{
	MannWhitneyU: 3,
	zScore: -1.91880…,
	pOneTailed: 0.02749…,
	pTwoTailed: 0.05499…
}

signTest()

Computes the sign test for two variables firstColumn and secondColumn by comparing the values in pairs of observations and counting the amount of pairs where the value for firstColumn is larger than that for secondColumn, returned as positives. The order of these variables is therefore important, i.e. all calculated significance parameters should be interpreted in regard to the number of positives under the assumption that the null hypothesis of equally likelihood that any pair can yield a negative or a positive difference.
Parameters:
(string) firstColumn (required)
(string) secondColumn (required)
Scales of measure:
ordinal, interval, metric
Usage:
var testData = [
	{ beforeDiet: 80, afterDiet: 83 },
	{ beforeDiet: 78, afterDiet: 82 },
	{ beforeDiet: 92, afterDiet: 94 },
	{ beforeDiet: 76, afterDiet: 64 },
	{ beforeDiet: 94, afterDiet: 96 },
	{ beforeDiet: 93, afterDiet: 97 },
	{ beforeDiet: 82, afterDiet: 83 },
	{ beforeDiet: 104, afterDiet: 87 },
	{ beforeDiet: 74, afterDiet: 77 },
	{ beforeDiet: 84, afterDiet: 85 }
];

var testVars = {
	beforeDiet: 'metric',
	afterDiet: 'metric'
};

var stats = new Statistics(testData, testVars);
var sign = stats.signTest('beforeDiet', 'afterDiet');
Returns:
{
	positives: 2,
	pExactly: 0.04394…,
	pFewer: 0.01074…,
	pAtMost: 0.05468…,
	pMore: 0.94531…,
	pAtLeast: 0.98925…
}

studentsTTestOneSample()

Computes Student’s one sample t-test for the values of a variable given by column. It examines if the arithmetic mean of a sample column is equal to the predefined or suspected arithmetic mean nullHypothesisMean. This method returns the associated t-statistic, the degrees of freedom and both one- and two-sided probabilities that the given observation can be made if the null hypothesis of equal arithmetic means is to hold true.
Parameters:
(string) column (required)
(integer or float) nullHypothesisMean (required)
Scales of measure:
interval, metric
Usage:
var batchMeasurements = [
	{ weight: 185 },
	{ weight: 201 },
	{ weight: 193 },
	{ weight: 184 },
	{ weight: 180 },
	{ weight: 176 },
	{ weight: 193 },
	{ weight: 182 },
	{ weight: 197 },
	{ weight: 204 }
];

var stats = new Statistics(batchMeasurements, { weight: 'metric' });
var oneSample = stats.studentsTTestOneSample('weight', 200));
Returns:
{
	tStatistic: -3.51631…,
	degreesOfFreedom: 9,
	pOneSided: 0.00327…,
	pTwoSided: 0.00655…
}

studentsTTestTwoSamples()

Computes Student’s two sample t-test for the values of two variables given by firstColumn and secondColumn. It examines if the arithmetic means of these samples are equal to each other or, in general, that their difference is equal to nullHypothesisDifference (default is 0). This test distinguishes between dependent and independent samples as controlled by dependent. A dependent sample is characterised by repeated measures (e.g. samples on the same subjects before and after a an intervention) or when two samples have been paired (e.g. cases and controls in a study). This method returns the associated t-statistic, the degrees of freedom and both one- and two-sided probabilities that the given observation can be made if the null hypothesis of equal arithmetic means/a given difference nullHypothesisDifference of their arithmetic means is to hold true. If dependent is set to true, missing values will be discarded and their amount will also be returned.
Parameters:
(string) firstColumn (required)
(string) secondColumn (required)
Options:
(integer or float) nullHypothesisDifference (default: 0)
(boolean) dependent (default: false)
Scales of measure:
interval, metric
Usage:
var treatmentResults = [
	{ before: 204, after: 189 },
	{ before: 212, after: 199 },
	{ before: 199, after: 191 },
	{ before: 209, after: 182 },
	{ before: 191, after: 176 },
	{ before: 190, after: 180 },
	{ before: 223, after: 220 },
	{ before: 185, after: 182 },
	{ before: 206, after: 194 },
	{ before: 212, after: 216 }
];

var testVars = { before: 'metric', after: 'metric'  };
var stats = new Statistics(treatmentResults, testVars);
var treatmentSuccess = stats.studentsTTestTwoSamples('before', 'after', { dependent: true }));
Returns:
{
	tStatistic: 3.79663…,
	degreesOfFreedom: 9,
	missings: 0,
	pOneSided: 0.00211…,
	pTwoSided: 0.00423…
}