|Probably more bunk has been written about correlation than about any other statistic. We'll keep this short and straight. At its simplest, correlation is a statistical measure of non-random, linear association between pairs of values in a dataset. It's denoted by r, and varies from -1 through +1, where -1 indicates perfect inverse correlation (the regression line goes down left to right), 0 indicates no correlation (there is no regression line; it's just a scatterplot), and +1 indicates perfect direct correlation (the regression line goes up left to right).|
For an example we'll use a bit of imaginary data:
If you like to think about such problems concretely, you can think of
There are many correlation formulas. Most commonly used is the Pearson product-moment correlation coefficient, which is valid only for normally distributed data (data that roughly fits a bell curve). When the data skews significantly from a normal distribution, you very likely need a different correlation method.
You will also want the slope of the relationship or regression line, its intercept with the Y axis, and the coefficient of regression, which is the proportion of observed variation due to correlation.
And, you need to calculate the probability that the coefficient you calculated is significantly different from zero. Here we use a simple standard formula to calculate a t statistic for the correlation result. The Wikipedia page on correlation describes several ways of testing the significance of correlations.
For this example we restrict ourselves to calculating correlation assuming a normal distribution and no missing values.
Calculating the correlation coefficient needs two passes: a first pass to calculate basic statistical quantities, then a second pass to calculate the slope, intercept and correlation coefficient from those basic quantities. Calculating the coefficient of regression and statistical significance needs a third pass. So this will be a 4-step:
Step 1: Calculate the required basic statistics.
Step 2: Use the results of Step 1 to calculate slope, intercept and r.
Step 3: Run the slope and intercept from Step 2 against
Step 4: Collect the result, calculate t and degrees of freedom (df).
If we're computing correlation in a database like MySQL, then, you'd expect Views will be useful. Unfortunately we're immediately bitten by two limitations in the MySQL implementation of Views: subqueries in the
Then do we need to encapsulate correlation in a stored procedure? We could, but we needn't because SQL implementations like MySQL provide the required basic statistical quantities, permitting us to do it all in one four-layer query ...
Step 1 is the innermost query, returning one row of summary statistics,
Step 2 collects and formats the Step 1 result row,
Step 3 calculates the regression coefficient by cross-joining the Step 2 query result with the original table.
Step 4, the outermost query, applies a simple formula for the t statiatic with N-2 degrees of freedom.
Read the query inside out:
You can look up the significance of the t value at a pre-decided level of significance for the number of degrees of freedom in your sample in a standard statistical table of t values, for example this one. As you can see there, the higher the degrees of freedom, the lower the t value needed for a given level of significance.