Download the PHP package richjenks/stats without Composer
On this page you can find all versions of the php package richjenks/stats. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download richjenks/stats
More information about richjenks/stats
Files in richjenks/stats
Package stats
Short Description Statistics library for non-statistical people
License MIT
Homepage https://github.com/richjenks/stats
Informations about the package stats
Stats
Statistics library for non-statistical people
- Introduction
- Installation
- Quickstart
- Usage
- Mean/Average
- Median
- Mode
- Frequencies
- Range
- Variance, Standard Deviation & Standard Error
- Individual Deviations
- Sample or Population
- Quartiles, Interquartile Range & Outliers
- Quartiles
- Interquartile Range
- Outliers
- Percentiles
- All Percentiles
- Single Percentile
- In Percentile
- CLI
- Unit Tests
Introduction
If you're into statistics then PHP will not be your language of choice (try R instead) but if for any reason you, a non-statistician, need to do some stats then this library aims to provide a simple set of methods for common statistical functions.
By design, with the exception of statistical tests, functions generally accept a single series of data at a time. This is to keep the library simple to use
Many of the methods in this library are available from the Statistics Extension, however this is not included in PHP by default. If possible, I'd recommend using this extension rather than my stats library.
Installation
- Install with Composer:
composer require richjenks/stats
- Include autoloader:
require 'vendor/autoload.php';
- All static methods are available from the
RichJenks\Stats\Stats
class
Quickstart
Stats will generally return either a
float
or anarray
, whichever is most appropriate for the function
Usage
Mean/Average
Calculates the mean/average of given data:
The
average
function aliasesmean
, e.g.Stats::average([1, 2, 3]);
also returns2
Median
Calculates the median (middle value) of given data:
Mode
Calculates the mode(s) — most common value(s) — of given data:
This function always return an array because it is able to handle multi-modal data and an empty array would mean there is no mode
Frequencies
Constructs a sorted array of frequencies for each value in a series:
Range
Determines the range (highest minus lowest) of given data:
Variance & Standard Deviation
These functions calculate:
- Variance: square of average variation from the mean
- Standard Deviation: average variation from the mean (square root of Variance)
Individual Deviations
The deviations
function is also available if you require the deviations for each individual value, for example:
Sample or Population
Sample
is the default mode for Variance and Standard Deviation but if you're unsure of the effect this decision has on your data then you probably don't need it and can skip this section.
Definitions
Population Every subject applicable, e.g. people who wear glasses or non-extinct species of frog
Sample The subset of subjects for which data is available, e.g. 100 glass-wearing subjects or a dozen species of frog
You can optionally pass the constants Stats::Sample
or Stats::POPULATION
as second parameters to determine whether your data is for a sample or a whole population:
Standard Error of the Mean
Estimates how well the sample mean approximates the population mean:
Quartiles, Interquartile Range & Outliers
These functions calculate the data required to construct a Box Plot which, when you understand what each data point means, is a concise way of displaying and comparing data sets.
Quartiles
Calculates Quartiles 0—4, where:
- 0 is the lowest data point
- 1 is Q¹
- 2 is Q² (the median)
- 3 is Q³
- 4 is the highest data point
Interquartile Range
Calculates the range between Q¹ and Q³ (the middle 50% of data):
Outliers
Determines which values in a series are outliers (too far from the other values so sometimes omitted from the data set, possibly due to experimental error):
Inliers
Determines which values in a series are not outliers, i.e. removes outliers:
Whiskers
Determines the lower and upper limit for identifying outliers:
Percentiles
All percentile functions accept an optional additional parameter for rounding that works as follows:
- If omitted, percentages are rounded to the nearest whole
- If a positive integer, percentages are rounded to that many decimal places
- If a negative integer (e.g.
-1
), percentages are not rounded
All Percentiles
Determines the percentile of each value:
Single Percentile
Determines the value closest to the given percentile:
Intra-Percentile
Determines the values that fall in the given percentile, i.e. the lowest x% of all values:
CLI
CLI usage is supported via the included scli
(Stats Command Line Interface) file and simply expects the name of the required method followed by its arguments:
In cases where the result is a set (i.e. an array) it is presented as comma-separated