concept correlation coefficient in category data

appears as: correlation coefficient, The correlation coefficient
Data Wrangling with JavaScript

This is an excerpt from Manning's book Data Wrangling with JavaScript.

We don’t have to rely on our visual judgment to determine the strength of the correlation between two variables. We can quantify the amount and type of correlation using the correlation coefficient, which is a numerical measure of correlation. The values of the correlation coefficient range from –1 to +1, with –1 indicating a perfect negative correlation and +1 indicating a perfect positive correlation. This forms the spectrum shown in figure 9.18. Negative correlation is on the left, positive on the right, and no correlation is in the middle.

The correlation coefficient for rainfall versus umbrella sales turns out to be around 0.64. Figure 9.18 shows that this value fits in the spectrum under the category of strong positive correlation.

9.8.4 Computing the correlation coefficient

You have various ways to compute the correlation coefficient, and in each case the math is rather complicated. Fortunately, we already have the simple-statistics code module, and it has a convenient sampleCorrelation function ready for us to use. The following listing shows how we can use this function to compute the correlation coefficient for rainfall versus umbrella sales since 2013.

Listing 9.17 Computing the correlation coefficient for rainfall vs umbrella sales since 2013 (listing-9.17.js)
const dataForge = require('data-forge');
const simpleStatistics = require('simple-statistics');

let dataFrame = dataForge.readFileSync("./data/nyc-weather.csv")    #1  
 .parseCSV()    #1  
 .parseInts(["Year", "Month", "Day"])    #1  
 .where(row => row.Year >= 2013)    #2  
 .parseFloats("Precipitation")    #1  
 .generateSeries({    #1  
 Date: row => new Date(row.Year, row.Month-1, row.Day),    #3  
 })    #1  
 .setIndex("Date");    #4  

const umbrellaSalesData = dataForge
 .readFileSync("./data/nyc-umbrella-sales.csv")    #5  
 .parseCSV()    #5  
 .parseDates("Date", "DD/MM/YYYY") //    #5  
 .parseFloats("Sales")    #5  
 .setIndex("Date");    #6  

dataFrame = dataFrame
 .withSeries(    #7  
 "UmbrellaSales",    #7  
 umbrellaSalesData.getSeries("Sales")    #7  
 )    #7  
 .where(row => row.Precipitation !== undefined    #8  
 && row.UmbrellaSales !== undefined);    #8  


const x = dataFrame.getSeries("Precipitation").toArray();    #9  
const y = dataFrame.getSeries("UmbrellaSales").toArray();    #10  
const correlationCoefficient = simpleStatistics
 .sampleCorrelation(x, y);    #11  
console.log(correlationCoefficient);    #12  

#1   Loads and parses weather data
#2   Removes all records prior to 2013
#3   Generates a date from year, month, and day columns
#4   Indexes by date so that we can merge our data
#5   Loads and parses umbrella sales data
#6   Indexes by date so that we can merge our data
#7   Merges umbrella sales into the DataFrame. This ensures that our dates line up.
#8   Drops rows with missing values. Rows in the CSV file may not line up.
#9   Extracts x values for the correlation coefficient
#10   Extracts y values for the correlation coefficient
#11   Computes the correlation coefficient
#12   Prints to console to see the result

You can run listing 9.17, and it will print a correlation coefficient of around 0.64, which should meet our expectations after having visually studied the scatter plot chart in figure 9.16. We expected a strong positive correlation, but not a perfect correlation. We’ve quantified the relationship between rainfall and umbrella sales.