Finding r in R. The correlation coefficient (r), the... | by Emmett Boudreau | Towards Data Science

The correlation coefficient (r), the correlation coefficient of regression (r2), and the programming language R are all three iconic Rs of Data-Science. The correlation coefficient (r) is an extremely known statistical test that is extremely used in Machine-Learning models. The correlation coefficient of regression can identify how in-line your slope is to the value is to the correlation, and is a great metric for regressive target prediction. R is a functional language that holds its roots in the language S and is a very popular choice among Data-Scientists.
And yes, we’re finding r in R because I think it’s funny.
notebook is available here.
R is a functional language that has roots as old as computing. While this can sometimes be a benefit, R has also always been a smaller language. It’s important to remember that although R is certainly a big deal for Data-Scientists, and is a great language; R will not have the universal support that Python or C carries with it. Of course, Python and C have their limitations as well, as does Julia and Scala. Overall I would say although I find myself using R substantially less than Python, Julia, and even Scala, I still think R is a great tool that i’d like to use even more than I do.
I enjoy low-level Data-Science as well as high-level. It’s surprisingly frequent to come across those who are perfectly familiar with correlation coefficients, models, etc. on a much larger scale. But knowing the inner workings of a model, or at least how a model works with statistics to demonstrate a more accurate result is valuable information.
Designing your own algorithms without the use of libraries can definitely put you on the chopping block for an interview and set you apart from other people in your field. This is because in order to develop complex algorithms, it can require a lot of computer-science skills. I’ll refrain from elaborating, but CS skills can often end up being a very large part of the job.
And besides that justification, its fun!
At first, this calculation seems quite comprehensive and difficult. The formula can be made a lot easier when each part at play is broken into pieces.
n — The sample size.
Σxy — The summation of the dot product of x and y.
Σx — The summation of x.
Σy — The summation of y.
Σx2 — The summation of the dot square of x.
(Σx)2 — The summation of x to the second power.
Σy2 — The summation of the dot square of y.
(Σy)2 — The summation of y to the second power.
After breaking down the equation, obviously we need to calculate each individual parts. Fortunately, R was built with linear algebra in mind. Nearly all of the operands in R can be used with Linear Algebra.
n — We can calculate n using R’s base function “ length.”
xy — We need the product of x and y first, luckily we can just use the * operand to multiply our arrays.
Σ() Base Sigma can be calculated using the sum() function.
    n = length(x)    xy = x * y    Σx = sum(x)    Σy = sum(y)    Σxy = sum(xy)
Pretty simple, right?
Now all we’re missing summation of the dot exponential of both x and y, as well as the summatation of x squared.
The summation of x2 vs the summation of x.....2
    x2 = x ^ 2    y2 = y ^ 2    sx2 = sum(x2)    sy2 = sum(y2)
Note the difference between Σx2 and (Σx)2, they might be read aloud the same, but they are not the same.
Combining all of the mathematics above, we can get our values and then plug them into the formula inside of an R function like so:
correlationcoeff <- function(x,y){    n = length(x)    yl = length(y)    xy = x * y    sx = sum(x)    sy = sum(y)    sxy = sum(xy)    x2 = x ^ 2    y2 = y ^ 2    sx2 = sum(x2)    sy2 = sum(y2)    r = ((n*sxy) - (sx * sy)) / (sqrt((((n*sx2)-(sx^2)) * ((n*sy2)-(sy^2)))))    return(r)}
Needless to say, pretty cool! But let’s take it a step further, and do a little more just to say that we did.
The Correlation Coefficient of Regression (the correlation coefficient squared) is also a really cool concept you should certainly be familiar with. r2 scores are well known for their use as a continuous model regression metric. r2 are generally calculated in a percentage form, representing a regressive similarity or difference (for machine-learning, similarity to a training set is a good thing.)
The good news is we can recycle our previous function in order to obtain our correlation coefficient to square (r)
r2 <- function(x,y){    r = correlationcoeff(x,y)    r2 = r ^ 2    return(r2)}
Easy, peasy, lemon-squeezy
Using our correlation coefficient (r) for feature selection, and our correlation coefficient of determination (r2), we can now go about writing some serious weight-pulling inferential models. Let’s use a linear-least-square regressor! If this were a little more official, we would make a constructor that we can put into a prediction function, this is generally a good practice when working with a model, as they are seen as object types rather than a single mathematical function (likely because of fit transforming.) Luckily, this function isn’t my portfolio piece or proprietary in any way whatsoever, so we’re just going to put x and y into the parameters!
pred_linearleastsquare <- function(x,y,xt){# Summatation of x*yxy = x * ysxy = sum(xy)# Nn = length(x)# Summatation of x^2x2 = x ^ 2sx2 = sum(x2)# Summatation of x and ysx = sum(x)sy = sum(y)# Calculate the slope:slope = ((n*sxy) - (sx * sy)) / ((n * sx2) - (sx)^2)# Calculate the y interceptb = (sy - (slope*sx)) / n# Empty prediction list:y_pred = c()for (i in xt){pred = (slope*i)+by_pred = append(y_pred,pred)}return(y_pred)}
Let’s make some data
Obviously random data isn’t representative of a real-world situation, but to not have to tamper with data-cleaning to test functions is certainly a viable excuse for refraining from using real-world data.
First, let’s start by predicting with our linear least square function:
y_pred = pred_linearleastsquare(trainX,trainy,testX)
And then we can use the r2 for a validation on how well our model is doing.
The coefficient of the correlation is very effective at providing information on the correlation of two different features, or better-yet — the feature and your target. I’ve certainly seen a lot of use of r in conditional models, especially ones that pertain to continuous targets.
So, needless to say, R, r, and r2 are all very cool and important concepts in statistics and data-science. I look forward to seeing where R is in the next few years, as well as the position of other languages.