Linear regression is a statistical method used to model the relationship between a dependent variable and one independent variable. This relationship is represented by a straight line when plotted on an x-y plane.

Linear Equation \[ y = mx + b \]

  • \(y\): Dependent variable
  • \(x\): Independent variable
  • \(m\): Slope of the line
  • \(b\): y Intercept

This is the R code used to create the interactive dynamic slope plot in the previous slide.

create_plot <- function(slope, intercept) { x <- seq(-10, 10, length.out = 100) y <- slope * x + intercept

plot_ly() %>% add_lines(x = x, y = y, name = “Regression Line”, line = list(color = “lightGreen”)) %>% layout( title = “Slopes Affect On Linear Regression”, xaxis = list(title = “X”, zeroline = TRUE, range = c(-10, 10)), yaxis = list(title = “Y”, zeroline = TRUE, range = c(-10, 10)), showlegend = FALSE ) }

slope <- 1 intercept <- 0

slopePlot <- create_plot(slope, intercept)

slopePlot <- slopePlot %>% layout( sliders = list( list( active = 0, currentvalue = list(prefix = “Slope:”), pad = list(t = 50), steps = lapply(seq(-10, 10, by = 0.5), function(slope) { list( label = as.character(slope), method = “restyle”, args = list(list( y = list(slope * seq(-10, 10, length.out = 100) + intercept) )))})))) slopePlot

Best Fit Line \[R^2 = 1 - \frac{\sum (y_i - \hat{y}_i)^2}{\sum (y_i - \bar{y})^2}\] \(R^2\): is a measure of the variance from the best fit line to the actual values.
\(y_i\): is the actual y values from the plot.
\(\hat{y}_i\): is the y values produced using the best fit line formula.
\(\bar{y}\): is the mean of the actual y values from the plot.

If \(R^2\) is equal to \(1\) then the best fit line fits the given data perfectly. If \(R^2\) is equal to \(0\) then their is no correlation between the best fit line and the given data.

Note how the data points are not represented as accuratly as in the previous plot, this is reflected in \(R^2\) being a lower value.

This is the R code used to create the previous slides plot, Miles per Gallon vs Horsepower.

data(mtcars)

fitMtcars <- lm(mpg ~ hp, data = mtcars)

ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point() + geom_smooth(method = “lm”, se = F, color = “lightGreen”) + labs( title = “Miles per Gallon vs Horsepower”, x = “Horsepower (hp)”, y = “Miles per Gallon (mpg)”, caption = paste(“R-squared:”, round(summary(fitMtcars)$r.squared, 2)) ) + theme_minimal()

Summary

  • Linear Regression: A statistical method to represent the relationship between a dependent and independent variable.

  • Equation: \(y = mx + b\)

  • \(R^2\): Quantifies the ability of a best fit line to accuratly represent a dataset.

  • Values from \(0\) to \(1\), \(1\) being a perfect representation.

  • Equation: \(R^2 = 1 - \frac{\sum (y_i - \hat{y}_i)^2}{\sum (y_i - \bar{y})^2}\)