2024-03-17

Introduction to Simple Linear Regression

  • Simple linear regression is a statistical method used to model the relationship and correlation between a single independent variable \(x\) and a continuous dependent variable \(y\).
  • It can be used in predicting outcomes and estimating values.

Important For Simple Linear Regression

  • Before applying simple linear regression, it’s important that:
  • There is a linear relationship between \(x\) and \(y\).
  • Independent variables are not correlated to one another.
  • The residuals (difference value between observed and predicted value) are normally distributed.
  • A problem can occur if predictor variables are highly correlated

Simple Linear Regression Equation

  • The simple linear regression equation is defined as:

\[ \hat{y} = \beta_0 + \beta_1 x + \varepsilon \]

  • \(\hat{y}\) is the dependent variable.
  • \(\beta_0\) is the u intercept.
  • \(\beta_1\) is the slope coefficient.
  • \(x\) is the independent variable.
  • \(\varepsilon\) represents the random error term.

Other Useful Information

  • Residuals are calculated as: \[ y - \hat{y} \]

  • The coefficient of determination (\(R^2\)) determines how well the linear regression model predicts the outcome and is calculated as:

\[ R^2 = \frac{\text{Explained variance}}{\text{Total variance}} = 1 - \frac{\text{Residual variance}}{\text{Total variance}} \]

  • The correlation coefficient (\(r\)) is a value between -1 and 1, and measures the strength of the relationship between the two variables.

Plot 1 Using ggplot

Plot 2 using ggplot

R Code for Plot

# Uses a built-in dataset of RStudio

library(ggplot2)

# Scatter plot with linear regression line
ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +  # Data points
  geom_smooth(method = "lm", se = FALSE, color = "darkgreen") +  # Linear regression line
  labs(x = "Horsepower", y = "Miles per Gallon",
       title = "Linear Regression: Miles per Gallon vs. Horsepower")
  theme_minimal()

Plot using plot_ly