What is Regression?

  • Regression is the process of taking related or supposedly related variables and applying statistical estimations in order to create a mathematical equations that aims to describe the correlation between the variables.

  • Linear regression restricts the definition of regression to only linear relationships describe how 2 variables interact. There are many different equation families which regression can be used to model.

  • Regression can be use to:

    • Determine correlation between variables
    • Predict values with the same relationship

How Simple Linear Regression Works

The equation below gives the basic linear form of the Regression equation:

\[Y = \beta_{0} + \beta_{s}X + \epsilon\]

The goal of this equation is to determine a \(\beta_{0}\) and \(\beta_{s}\) for which the sum of \(\epsilon^2\) is minimized for all data points that relate X and Y and make the equation true.

Example 1: women

This plot relates the height and weight of women. The red lines, called residuals, show the \(\epsilon\) values that are squared and minimized to best fit the line to the data. Using this model we can predict that the weight for a women who 65 inches tall is 136.73 pounds

## [1] "Predicted weight for height 65 inches: 136.733333333333 pounds"

Example 2: mtcars (ggplot2)

Here is a more complex example. Here we can draw the conclusion that as the car gets heavier, the Miles per Gallon decrees. This makes logical sense and it is clear that the data proves this relationship.

Example 2: mtcars cont.

This in the code for the previous plot

data(mtcars)
model = lm(mpg ~ wt, data = mtcars)
mtcars$predicted = predict(model)

plot = ggplot(mtcars,aes(x=wt,y=mpg))+
  geom_point() +
  labs(title = 'Miles Per Gallon vs Weight') +
  geom_smooth(method = 'lm', se = FALSE,formula = y ~ x) +
  geom_segment(aes(xend = wt, yend = predicted), color = 'red')

Example 3: trees (plotly)

Here is another example graphed with plotly. As expected, the volume of the tree increases as the girth gets larger

Example 3: trees cont.

This in the code for the previous plot

data(trees)
model = lm(Volume ~ Girth, data = trees)
trees$predicted = predict(model)

plot =  plot_ly(
  trees,
  x=~Girth,
  y=~Volume,
  type = 'scatter', 
  mode = 'markers')%>%
  add_trace(
    y = ~predicted, 
    mode = "lines", 
    name = "Linear Fit")%>%
  layout(
    title= 'Tree Volume vs Girth'
    )

Conclusion

As demonstrated in these slides, linear regression is a powerful statistical/mathematical tool that helps us identify and quantify relationships between variables. It allows us to analyze trends in existing data and make predictions about future or unseen data based on that relationship.