2024-02-05

Introduction

Simple linear regression is a foundational statistical method that allows us to understand and predict the relationship between two continuous variables. It serves as a stepping stone into the world of complex statistical analysis, making it a crucial topic for students and professionals alike.

What is Simple Linear Regression?

Simple linear regression models the relationship between a dependent variable \(Y\) and an independent variable \(X\), assuming a linear relationship.

  • Why is simple linear regression important?

    • Identifying the linear relationship between two variables offers insights into various trends, patterns, and predictive capabilities that are foundational in various fields, including economics, biology, engineering, and social sciences.

Mathematical Representation

The equation of a simple linear regression line is given by:

\[ Y = \beta_0 + \beta_1X + \epsilon \]

  • \(Y\) is the dependent variable.
  • \(X\) is the independent variable.
  • \(\beta_0\) is the y-intercept.
  • \(\beta_1\) is the slope of the line.
  • \(\epsilon\) is the error term.

Assumptions

Simple linear regression relies on four main assumptions:

  • Linearity: The linearity assumption ensures that the change in the response variable is directly proportional to the change in the predictor variable.
  • Independence: The independence assumption posits that the observations must not influence each other, which is critical for the reliability of the model’s predictions.
  • Homoscedasticity: Homoscedasticity implies that the residuals should have constant variance across all levels of the predictor variables to avoid biases in variance estimates.
  • Normality of residuals: Assuming normality of residuals allows for the use of various statistical tests to make inferences about the population parameters from the sample estimates.

Interactive Plot for Simple Linear Regression Using plotly

Example - Introduction to the Dataset

The pressure dataset within R provides a set of observations on the melting point of oxygen at different pressures. It is commonly used to demonstrate the principles of thermodynamics.

Contents of the pressure Dataset

  • The pressure dataset contains measurements of the vapor pressure of mercury as a function of temperature in degrees Celcius.
First 6 observations of the pressure dataset
temperature pressure
0 0.0002
20 0.0012
40 0.0060
60 0.0300
80 0.0900
100 0.2700

Mathematical Model

The relationship between temperature and pressure can be modeled using simple linear regression:

\[ Pressure = \beta_0 + \beta_1 \times Temperature + \epsilon \] Where:

  • \(Pressure\) is the dependent variable
  • \(Temperature\) is the independent variable
  • \(\beta_0\) is the intercept of the regression line
  • \(\beta_1\) is the slope of the regression line
  • \(\epsilon\) is the random error term

Plotting the Regression Using ggplot - Code

library(ggplot2)

# Use the pressure dataset
data(pressure)

# Create a ggplot with a regression line
p <- ggplot(pressure, aes(x = temperature, y = pressure)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, color = "blue")

Print the Plot Object to Display the Plot

Plot 2 for Dataset Cars Using ggplot

  • Similar to the previous example, cars is a default library in R. This is the simple linear regression plot for the dataset.