2024-02-01

Studies of correlation and regression of two variables begins with a scatter diagram (a graph of paired data values(x,y)). To find a linear relationship between x and y values:
- The best line would be the one that comes closet to each of the points of the scatter diagram.
\(\bullet\) If all points are on a line => perfect linear correlation.
\(\bullet\) If the points seems close to a line => linear correlation is moderate to high.
\(\bullet\) If the points are located so that no line is a good fit => no linear correlation.

Positive correlation: high value of x are associated with high values of y, and low values of x are associated with low values of y.

Negative correlation: low values of x are associated with high values of y, and high values of x are associated with low values of y.

Example:

How to calculate the linear regression equation?

Linear regression line within the plot:

\[ \hat{y} = a + bx \] In this equation, b is calculate as: \[ \text{Slope b}=\left(\frac{n\Sigma xy - (\Sigma x) (\Sigma y)}{n\Sigma x^2 - (\Sigma x)^2} \right) \]

where:
\({n}\) = sample size. \({\Sigma x}\) = sum of x values. \({\Sigma y}\) = sum of y values. \({\Sigma x^2}\) = sum of power of 2 of x values. \({(\Sigma x)^2}\) = power of 2 of sum of x values.

Based on the equation and the x and y values in the example given, we have:

\[ \text{Slope b}=\left(\frac{(12)(143) - (158)(143)}{(12)(2812) - 24964} \right) \approx 1.0034 \] In the equation, a is calculated as: \[ \text{a}=\left(\frac{(\Sigma y)(\Sigma x^2) - (\Sigma x) (\Sigma xy)}{(n)(\Sigma x^2) - (\Sigma x)^2} \right) \] Substitute x and y values, we have:
\[ \text{a}=\left(\frac{(143)(2812) - (158)(2617)}{12*2812 - 24964} \right) \approx -1.2950 \]

From a and slope b calculated, we have a complete linear regression equation:
\[ \hat{y} = -1.2950 + 1.003417x \] with \(\text{b}=1.003417\) means for each 1 unit increase in \(\text{x}, \text{y}\) is predicted to increased by 1.003417 units.

Then, we will plot the scatter diagram with linear regression line:

example = ggplot(co,aes(x, y)) +
  geom_point(mapping = aes(), color = "orangered3", size = 3) +
  geom_smooth(method='lm', se=FALSE) +
  theme_light()
regline = lm(formula = y~x, data = co)
coef = coefficients(regline)

## (Intercept)           x 
##   -1.294989    1.003417

Sample correlation coefficient:

Sample correlation coefficient r is a unit numerical measurement that assesses the strength of a linear relationship between two variables x and y. The formula is:
\[ \text{r}=\frac{n\Sigma xy - \Sigma x\Sigma y}{\sqrt{(n\Sigma x^2 - (\Sigma x)^2)*(n\Sigma y^2 - (\Sigma y)^2)}} \] \(\bullet\) Positive \(\text{r}\): Positive linear correlation.
\(\bullet\) Negative \(\text{r}\): Negative linear correlation.
\(\bullet\) \(\text{r}=0\): no linear correlation. \(\rightarrow\) the closer \(\text{r}\) to 0, the weaker the linear correlation.

Based on the formula and data given, \(\text{r}\) is:

## [1] 0.9438591

Coefficient of determination:

Coefficient of determination \(r^2\) is the square of sample correlation coefficient \(\text{r}\). In this example, \(r^2 \approx 0.8909\)

\(\rightarrow\) approximately 89.09% of the variation in y can be explained by the linear model.

\(\rightarrow\) 100% - 89.09% = 10.91% of the variation is due to random chance or other unknown sources.

Another example, with data in R:

Let’s take a look at another example. Below data is the weight of a chicken associates with its age. There is total of 578 pairs, the data shown is only part of the full table.

data = ChickWeight %>%
  select(weight, Time)
head(data)
##   weight Time
## 1     42    0
## 2     51    2
## 3     59    4
## 4     64    6
## 5     76    8
## 6     93   10

After calculation, the following will be the plot with linear regression line:

reg = lm(yy~xx, data = data)
plot_ly(x=xx, y=yy, type='scatter', mode='markers') %>%
  add_markers(y=yy) %>%
  add_lines(x=xx, y=fitted(reg)) %>%
  layout(xaxis = list(range=c(0,400)), yaxis = list(range=c(0,23)))
rr = cor(xx,yy)
rr
rr2 = (rr)**2
rr2

\(r\approx 0.8371017\).

\(r^2\approx 0.7007393\)

Correlation and Causation

include_graphics("https://cdn1.vox-cdn.com/assets/4442401/Screenshot_2014-05-12_12.46.30.png")

In this graph, there is clearly a strong negative correlation between marriage rate in Mississippi and per capita consumption of whole milk in US (99%). However, does it mean one of these factors effect the other?

The answer is clear, definitely not!

This is why it is important to choose and analyze the data carefully!