Simple Linear Regression

  • What is simple linear regression?
  • Simple linear regression is a linear regression model in statistics that can be used to study the relationship between a single independent variable and a single dependent variable.
  • The independent variable is plotted along the x-axis, and the dependent variable is plotted along the y-axis.

Simple Linear Regression Model

  • This is the equation for the simple linear regression model:

\[ Y = \beta_0 + \beta_1X \] - \(\beta_0\) and \(\beta_1\) are estimated by the least squares method (n = sample size):

\[ \hat{\beta_0} = \bar{y} - \hat{\beta_1}\bar{x} \\ \hat{\beta_1} = \frac{\sum_{i = 1}^{n}y_ix_i - \frac{(\sum_{i = 1}^{n}y_i)(\sum_{i = 1}^{n}x_i)}{n}}{\sum_{i = 1}^{n}x_i^2 - \frac{(\sum_{i = 1}^{n}x_i)^2}{n}} \]

Fitted Regression Equation

  • This is the equation for the fitted/estimated regression line:

\[ y_i = \hat{\beta_0} + \hat{\beta_1}x_i + e_i \] - \((x_i, y_i)\) are the values of the observation point, and \(e_i\) is the residual (error) for observation i.

\[ e_i = y_i - \hat{y_i} \]

Plotting a Linear Regression Model Using Plotly (Code)

  • Using the mtcars dataset, let’s plot a linear regression model to study the relationship between the miles per gallon (mpg) and weight (wt) of a car using R:
plot = plot_ly(data = mtcars, x = ~mpg, y = ~wt, type = "scatter", 
               mode = "markers") %>% layout(title = "MPG vs. Weight of Cars")
plot = add_trace(plot, x = mtcars$mpg, y = fitted(lm(wt ~ mpg, mtcars)), 
                 mode = "lines", showlegend = FALSE)

Plotting a Linear Regression Model Using Plotly (Plot)

  • Here is the resulting plot from the code on the previous slide:

Using ggplot

  • We can also use ggplot to illustrate a linear regression model.
  • Below is an example with the mtcars dataset built into R:
ggplot_plot = ggplot(mtcars, aes(x = mpg, y = wt)) + 
  geom_point() + 
  labs(x = "mpg", y = "wt", title = "mpg vs wt. of Vehicles") +
  geom_smooth(method = "lm", se = FALSE)

Using ggplot (cont.)

  • Here is the plot from the code on the previous slide:
## `geom_smooth()` using formula = 'y ~ x'


- From the plot, we can see that there is a clear relationship between mpg and wt of a car. As the mpg increases, the wt generally decreases. As the mpg decreases, the wt generally increases.

Using ggplot (cont.)

  • Below is another example using the airquality dataset built into R:
airquality_plot = ggplot(airquality, aes(x = Wind, y = Temp)) + 
  geom_point() + 
  labs(x = "Wind", y = "Temp", title = "Wind vs. Temperature") +
  geom_smooth(method = "lm", se = FALSE)

Using ggplot (cont.)

  • Here is the plot from the R code on the previous slide:
## `geom_smooth()` using formula = 'y ~ x'