{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "So far we've been looking into the problem where we have single categorical/numerical, relationship between categorical-numerical or categorical-categorical. In this blog, we're going to discuss about two numerical variables. We have a correlation test, want to test the strengthness relationship of both numerical variables, inference, and introduction to linear regression.For those that familliar with machine learning, this blog will make additional flavour from statistics.Why is it call linear regression? Because it's all about regress towards the line.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Correlation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w1.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/139) 01:00*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The picture above is about the rate between poverty and high school graduate in the US. First we have poverty rate, as response, and we want to see whether it's affected by HS graduate, the explanatory. When looking into two numerical variable, we often observe the linearity, the direction, and whether both have strong correlation.Correlation here means linear association between two numerical variables. Non-linear will be just called association." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Correlation is all about linearity, and often called **linear association**, and it's denoted by **R**.The explanatory acts as a independent variable(predictor), and the response variable is the dependent variable.We can see the linearity, by looking at the foremention 3 categories:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w2.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/139) 02:00*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The higher the correlation(absolute value), represent the stronger relationship, linear between both variables." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w3.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/139) 02:28*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The direction of the linear decide sign value of R." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w4.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/139) 03:09*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The linear will always between -1 and 1. It will fall perfectly in a line. Whereas 0, as you can see x increases, but nothing's to do with y." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w5.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/139) 04:30*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "R is unitless, meaning no matter how you change the unit, or scale, it will always retain its value. Here we have R close to zero, and you can see based on the previous example, it almost show no correlation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w6.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/139) 05:11*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Even if you flip both axes, R will still be the same." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w7.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/139) 06:15*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you move even 1 point to corner outliers, R will vary significantly. correlation coefficient is sensitive to outlier." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w8.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/139) 08:20*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take a look at the example. correlation coefficient is always between -1 and 1. So you can eliminate e. Its direction is negative, so it only betwen b and c. All that's left is strong/weak correlation. Try to imagine the negative space (shaded by orange color). This will help us better intuition. R with 0.1 will almost not give us negative space." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Residuals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w9.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/141) 00:44*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Residual is the distance between the actual output of y-axis and your hypothesis. This will serve as a basis of predicting the output, in this case poverty. So given grad rate, we want to predict rate in poverty. What we want to do is minimizing the residual overall." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w10.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/141) 01:44*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It will overestimate if the predicted is beyond the actual output, and underestimate if the predicted below the actual output." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Least Squares Line" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this section we want to talk about how we minimize the line in linear regression, by taking least squares (cost function).There's another option where we talk about the distance error as the absolute value, but as we talk before, we want to give higher magnitude to those with longer distance. So we squares all distance, this also give advantage as it's easier to calculate and more common." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w11.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/143) 01:24*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So this would be familliar to those with machine learning experience. We have $\\hat{y}$, as our hypothesis, the predicted output. We have intercept, as a bias unit. We have the weight parameter, a slope that can then multiplied by explanatory variable. We have seen this formula, when we're calculating gradient in high school. Recall that y = mx. With m the slope, and we have additional bias unit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w12.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/143) 02:44*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we have b1, as our slope point estimate (b0 will be represented as intercept point estimate). And the calculation of b1 is just swapping from previous formula.So the slope is standard deviation of response variable, times correlation coeffecient for response-explanatory, divided by standard deviaton of the explanatory variable.\n", "\n", "$$ b1 = \\frac{s_y}{s_x}R$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w13.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/143) 06:08*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we simply have all the parameter, and can calculate the slope. Standard deviation is always positive for both response and explanatory (as you recall, it derrive from squared difference). So the sign of the slope is always depends on the sign of the correlation. If you see the image, you can see that it has negative direction.\n", "\n", "\n", "Since we're talking about the percentage rate, 0.62 is the percentage, so we can say that percentage living in porverty is lower(negative sign!) on average by 0.62%. Mind that since this is observational study, pay attention that we're trying to make a correlation and not causal statements. So 'would expect' is the chosen phrase instead of 'will'. And explanatory variable is the one unit meassurement to the predicted response variable. So we have one unit (percentage), so we say as one percent of HS graduate increase, we would expect poverty to be lower on average by 0.62 %. Pay attention to the units of response and explanatory, as both can be different." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w14.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/143) 07:11*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can replace the formula with $\\bar{y}$, which denotes the average of response variables, and $\\bar{x}$, which denotes the average of explanatory variable.The intercept will then simply swapping the parameters.You see that in linear regression, it expected that the line is go through the center of the data. Which means, the line is all of the average of data points." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w15.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/143) 09:28*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "linear regression is always about the intercept and the slope. Explaining intercept alone is less meaningful. Recall the formula as:\n", "\n", "$$\\hat{y} = \\beta0 + \\beta1\\hat{x}$$\n", "\n", "So when the explanatory variable is zero then,\n", "\n", "$$64.68 = \\beta0 \\\\\n", "\\beta0 = -64.68\n", "$$\n", "\n", "Based on the formula, simplifying to just the intercept, as the picture stated, **\"States with no HS graduates(assumed zero explanatory for the sake of statement), are expected to have 64.68% of their residents living below the poverty line(response). **" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w16.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/143) 10:38*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By doing computation software, the table shows the intercept and slope in the estimate column, while the rest of the table will be explained later.\n", "\n", "So in **intercept**, explaining only that will provide no useful information. you only setting the x = zero, which will intercept at the y-axis. And intercept only benefit as provide base y-axis(height of the line). While **slope**, explaning the correlation of x/y axis. As x increase(each unit), what happen to the y-axis(higher/lower?) on average.\n", "\n", "So least squares line always passes through point average of explanatory and response variable. Using this idea, we can calculate the intercept value as:\n", "\n", "$$ b_0 = \\bar{y} - b_1\\bar{x}$$\n", "\n", "there are different interpretation depending on whether you use numerical/categorical explanatory variable.To interpret intercept when x is numerical \" When x = 0, we would expect y to be equal on average to {intercept}\". When x is categorical, \"We expect the value average of response variable for {reference level} of the {explanatory variable} is {intercept value}\".\n", "\n", "\n", "and the slope like before calculated as:\n", "\n", "$$ b_1 = \\frac{s_y}{s_x}R $$\n", "\n", "To interpret slope, If x is numerical \" For each unit increase x, it's expected an increase/decrease by {slope units} for y in average\". When x is categorical however, we say, \"The value of response variable is predicted to have {slope units} higher/lower between reference level and the other value of explanatory variable. Increase/decrease is depend by sign of the slope.\n", "\n", "\n", "So indicator is the binary explanatory variables. And based on this fact, we can interpret intercept(with level 0) and slope(with level 1). \n", "\n", "Suppose we're given this example,\"The model below predicts GPA based on an indicator variable (0: not premed, 1: premed). Interpret the intercept and slope estimates in context of the data.\"\n", "\n", "gpaˆ=3.57−0.01×premed\n", "\n", "For the intercept, \"When premed equals zero, we would expect gpa in average to be 3.57\". For the slope, \"For each increase of 1 premed, we would expect the gpa on average to be lower on average by 0.01 unit\"\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] -385.0 7.5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xbar = 70\n", "xs = 2\n", "\n", "ybar = 140\n", "ys = 25\n", "\n", "R = 0.6\n", "\n", "slope = (ys/xs)*R\n", "intercept = ybar - slope*xbar\n", "\n", "c(intercept,slope)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] -40" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xp = 72\n", "yp = 115\n", "\n", "predicted =(intercept + slope*xp)\n", "yp - predicted" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Prediction & Extrapolation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall that in the prediction, we're going to map x into the line, and infer y based on the point projected on the linear regression. In other words, which y-value that correspond to given x-value that construct point in the linear regression." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w17.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/143) 10:38*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using intercept and slope from previous example, we can simply plug the explanatory, 82 to the resulting formula. What we get is 13.84. So based we can say on states, given that the state have 82% HS graduation rate, we predict that 13.84% on average in the poverty line. But beware of some **extrapolation**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/7w18.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/143) 02:49*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extrapolation would means that we want to infer something outside realm of the data. The data that we have are the explanatory in range from 70-95(more or less). But far more outside the realm, we don't know if the intercept consistent with linearity, or maybe in exponential(logistic) manner. Take a look at the red line, intercept with the red line would be wrong. And any exponential line would interpret wrong result outside the realm.\n", "\n", "So if the problem will need you to predict the poverty rate at 20% HS graduate, you can't do that. The resulting estimate will yield unreliable output.\n", "\n", "So we can predict for any given value in the explanatory variable, x*, the response variable will be:\n", "\n", "$$ \\hat{y} = b0 + b1x^*$$\n", "\n", "Always remember to not extrapolate response variable beyond the realm of data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> **REFERENCES**:\n", "\n", "> Dr. Mine Çetinkaya-Rundel, [Cousera](https://class.coursera.org/statistics-003/lecture)" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }