2024-03-21

Linear Regression

  • A regression line is a straight line that describes how a response variable y (dependent variable) changes as an explanatory variable x (independent variable) changes.
  • The linear regression equation is typically represented as \[\hat{y} = b_{0} + b_{1}x\].
  • The least-squares regression line of y on x is the line that minimizes the sum of the squares of the vertical distances of the data points from the line.
  • A change of one standard deviation in x corresponds to a change of r standard deviations in y.

More Regression Information

  • Even though correlation r ignores the distinction between x and y, there is a close connection between correlation and regression.
  • A regression line describes the overall pattern of a linear relationship between an explanatory variable and a response variable. -Linear regression allows you to predict a value of one variable based on the data of another variable.
  • The vertical distance between points and the least squares regression line are called residuals.
  • A residual is the difference between an observed value of the response variable and the values predicted by the regression line. residual = observed - predicted \[y = y - \hat{y}\].

Our Data

  • We will be using the mt cars data set to explore linear regression further.
  • Below is a sneak peak inside the dataset.
##                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

Plotly

  • The thing that most people think about when purchasing a car is gas mileage. -The graph below shows the relationship between miles per gallon (mpg) and the number of cylinders the car has (cyl).

R Code for Plotly Graph

  • The code for the previous graph is below:
library(plotly)

fig <- plot_ly(data = mtcars, x = ~mpg, y = ~cyl,
        marker = list(size = 10,
                      color = 'rgba(255, 182, 193, .9)',
                      width = 2))

fig <- fig %>%layout(title = 'MPG vs Cylinders')

fig

Linear Regression Plotly

Plotly Linear Regression R Code

  • Linear regression can be usedto predict new data points and can help us understand the impact one variable can have on another.
  • The following code shows how the function lm can calculate the linear regression line.
  • Summary gives all the information that lm calculates.
fit <- lm(mpg ~disp, data = mtcars)

plot_ly(data = mtcars, x = ~disp, y = ~mpg, type = "scatter", 
        mode = "markers") %>% add_lines(x = ~disp, y = fitted(fit))

summary(fit)

Linear Regression GGplot

  • The same graph completed in plotly can also be done in ggplot

R Code for GGplot

library(ggplot2movies)

ggplot(data = mtcars, aes(x = disp, y = mpg)) + geom_point()

An Example of Linear Regressionin GGplot

  • The shaded grey area is the confidence interval around the regression line.
  • By default the confidence interval is set to 95%.
  • The confidence interval can be turned off with se = FALSE and the 95% can be changed if you change the level argument.
ggplot(data = mtcars, aes(x = disp, y = mpg)) + geom_point() + 
  geom_smooth(method = "lm")

An Example of Linear Regressionin GGplot Cont.