What is Simple Linear Regression?

  • Simple Linear Regression is a statistical method used to model and quantify the relationship between two variables.
  • Variable x serves as the independent variable or predictor, representing the input used to predict or understand changes in the dependent variable y.
  • Variable y is the dependent variable or response, representing the output or the variable under investigation in terms of the variations in the independent variable x.

Equation

\[ y = \beta_{0} + \beta_{1}x + \varepsilon \]

Where:

  • \(y\) is the dependent variable,
  • \(x\) is the independent variable,
  • \(\beta_{0}\) is the intercept (constant term),
  • \(\beta_{1}\) is the slope (the change in \(y\) for a unit change in \(x\)),
  • \(\varepsilon\) represents the random error term.

Purpose of Simple Linear Regression

  • Simple linear regression is employed to model and quantify the linear relationship between two variables. It helps in understanding how changes in the independent variable correspond to changes in the dependent variable.
  • One of the primary purposes is to make predictions about the dependent variable based on the values of the independent variable. This predictive capability is valuable in various fields.
  • Simple linear regression provides coefficients (intercept and slope) that quantify the strength and direction of the linear relationship. These coefficients offer insights into the impact of changes in the independent variable on the dependent variable. ## Example Let’s define the relationship between hours studied and exam scores as an example. Here’s the data:
##   Hours_Studied Exam_Scores
## 1      26.85479    131.2850
## 2      17.17651    103.0823
## 3      21.81564    131.2371
## 4      23.16431    126.4718
## 5      22.02134    119.7608
## 6      19.46938    116.1512

Example

We can use the scatter plot to display the data set

Example Continue

Then we can use the function lm() to display the line of best fit:

To find the Slope

In order to find the Slope of the line of best fit, we can use a combination of function in R to calculate:

\[ \beta_1 = \frac{cov(x,y)}{var(x)} \]

Which gives 1.6927735

Multipoint Linear Regression

We can even use Linear Regression to display on a 3D data set! Let’s include a new column of data call Grades. Here’s the new data set:

##   Hours_Studied Exam_Scores     Grade
## 1      26.85479    131.2850 116.28702
## 2      17.17651    103.0823 100.98852
## 3      21.81564    131.2371  97.70742
## 4      23.16431    126.4718 113.98888
## 5      22.02134    119.7608  99.69815
## 6      19.46938    116.1512  99.73164

3D Plot

Code

Here’s the code I used to create both the 2D plot and the 3D plot:

library(ggplot2)
library(plotly)

model <- lm(Exam_Scores ~ Hours_Studied, data = study_data)
ggplot(study_data, aes(x = Hours_Studied, y = Exam_Scores)) +
  geom_point() +  
  labs(title = "Scatterplot with Line of Best Fit",
       x = "Hours Studied",
       y = "Exam Scores")
study_data$Grade <- 70 + 1.5 * study_data$Hours_Studied + rnorm(50, mean = 0, sd = 5)
head(study_data)
plot_ly(study_data, x = ~Hours_Studied, y = ~Exam_Scores, z = ~Grade, type = "scatter3d", mode = "markers",
        marker = list(size = 5, color = 'red')) %>%
  layout(scene = list(xaxis = list(title = 'Hours Studied'),
                      yaxis = list(title = 'Exam Scores'),
                      zaxis = list(title = 'Grade')))