2024-10-31

What is Linear Regression

  • Linear Regression is a method to establish if there is a relationship between 2 variables.
  • Specifically, it is to find if there is a statistically significant relationship between the two variables

The 2 Variables

  • There is a dependent variable and an independent variable
  • The dependent variable is one that changes from the independent variable
  • The independent variable is the one that the tester manually changes
  • This formula below represents that dependent variable y and the independent variable x
  • This is a linear equation

\[ y = mx + b \]

Explaining the Linear Equation

Linear Equation Explained

  • As you can see 5x + 3 is equal to y where y is the dependent variable and x is the independent variable

Simple Linear Regression Model

\(\text{y} = \beta_0 + \beta_1x + \varepsilon; \hspace{1cm}\)
 

  • As you can see here, there are 5 parts to this equation
  • y is the dependent variable
  • beta0 is the constant or intercept
  • beta1 is the slope
  • x is the independent variable
  • epislon is the error

Example Linear Regression Model

  • Here is an example of a data set that we will be using
trees
##    Girth Height Volume
## 1    8.3     70   10.3
## 2    8.6     65   10.3
## 3    8.8     63   10.2
## 4   10.5     72   16.4
## 5   10.7     81   18.8
## 6   10.8     83   19.7
## 7   11.0     66   15.6
## 8   11.0     75   18.2
## 9   11.1     80   22.6
## 10  11.2     75   19.9
## 11  11.3     79   24.2
## 12  11.4     76   21.0
## 13  11.4     76   21.4
## 14  11.7     69   21.3
## 15  12.0     75   19.1
## 16  12.9     74   22.2
## 17  12.9     85   33.8
## 18  13.3     86   27.4
## 19  13.7     71   25.7
## 20  13.8     64   24.9
## 21  14.0     78   34.5
## 22  14.2     80   31.7
## 23  14.5     74   36.3
## 24  16.0     72   38.3
## 25  16.3     77   42.6
## 26  17.3     81   55.4
## 27  17.5     82   55.7
## 28  17.9     80   58.3
## 29  18.0     80   51.5
## 30  18.0     80   51.0
## 31  20.6     87   77.0

Create the new plot

## `geom_smooth()` using formula = 'y ~ x'

Explnation of the Linear Regression Line

  • The points of the data seemed to be scattered
  • However there is a trend line that is going upwards
  • This means that these two variables are related slightly where is one variable increases, than the other variable also will increase
  • This is the basis of linear regression

Lets do this again

  • You can see the example code here to get the graph
ggplot(data = trees, aes(x = Girth, y = Volume)) +
  geom_point(color = "blue", size = 2) +  
  geom_smooth(method = "lm", color = "red", se = TRUE) +  
  labs(title = "Girth vs. Volume",
       x = "Girth",
       y = "Volume") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

Graph Continued

## `geom_smooth()` using formula = 'y ~ x'

Graph explained

  • Here the slope clearly seems steeper so there is a stronger relationship between these two variables than the graph before
  • As you can see in this presentation how linear regression can help show the relationship between variables and you can even see stronger relationships compared to others both positively and negatively