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 `id` as a subject's id, `x` as a subject's height, and `y` as a subject's score on a self-confidence questionnaire, so we would be computing a correlation between height and self-confidence.There are many correlation formulas. Most commonly used is the Pearson product-moment correlation coefficient, which is valid only for normally distributed data (data which roughly fits a bell curve). A good Wikipedia page offers several formulas for the Pearson coefficient. Note that when the data skews significantly from a normal distribution, you very likely need a different formula. That's one problem with correlation: it isn't always a valid number to calculate. Another problem is that computing just the correlation coefficient is never enough. You also need to find the probability that the coefficient you calculated is significantly different from 0. The Wikipedia page on correlation describes several ways of testing the significance of correlations. You will probably 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.For this example we restrict ourselves to calculating correlation assuming a normal distribution and no missing values. Calculation of the correlation coefficient needs two passes: a first to calculate basic statistical quantities, then a second to calculate the slope, intercept and correlation coefficient from those basic quantities. Calculating the coefficient of regression requires a third pass: 1. Calculate the required basic statistics. 2. Use the results of #1 to calculate slope, intercept and r.3. Use the slope and intercept from #2 to calculate the coefficient of regression. 4. Collect and display the results. 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 `FROM` clause of a View are not supported, and neither are parameters.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 ... 1. The innermost query does step 1, 2. The query that references the inner query does step 2, 3. The next outer query calculates the regression coefficient by cross-joining the step 2 query result, which is one logical row, with the original table. 4. The outermost query displays the result: Read the query inside out: ```
``` |