2024-01-05

What is Linear Regression?

  • One of the most fundamental statistical models

  • Relates the interaction between a “response” variable and one or more “explanatory” variables

  • Easy to interpret and understand since the basis come from the linear equation: \[ Y = mX + b \]

So how do we perform linear regression?

  • We do this by trying to establish a linear expression relating “response” or dependent variable to our “explanatory” or independent variable(s)

  • This expression can best be understood as being of the general form: \[ Y = b + m_1X_1 + m_2X_2 + m_nX_n + \varepsilon \]

  • Where:

\(Y\) is the dependent variable

The \(m\) terms are the slopes associated with their respective \(X\) term

The \(X\) terms are the independent variables

\(b\) is the y-intercept and \(\varepsilon\) is the error term

The right tool for the right problem!

  • Not always the best tool, and not always useful, but powerful nonetheless
  • Requires us to make the assumption that there is a linear relationship between variables
  • How can we tell?

When in doubt, check!

  • We can use ggplot2 to create a scatter plot to see if there seems to be a generally linear relationship. For example:

Now that we know, lets build the model!

  • First we have to do some work under the hood beacuse the information we want to work with is a time series
#turn the time series into a workable data frame
data <- data.frame(Year = as.numeric(time(airmiles)), 
                   Airmiles = as.numeric(airmiles))
#Next we need to do the actual regression 
#luckily R makes that very easy for us
linreg <- lm(Airmiles ~ Year, data)

Lets plot the line as well now! (The Code)

  • using ggplot2
ggplot(data, aes(x = Year, y = Airmiles)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, color = "dodgerblue2") +
  labs(x = "Year", y = "Airmiles") +
  ggtitle("Plot With Added Regression Line")

Lets plot the line as well now! (The Plot)

Lets go a step further!

  • Recall from earlier that we can have multiple independent variables. Lets see what that looks like!
  • First we’ve got to do some prep work again
data2 <- data.frame(Year = as.numeric(time(airmiles)),
                    Airmiles = as.numeric(airmiles))
# add a new IV (Incidents) to the data frame
#with random values from 0-7
data2$Incidents <- sample(0:7, nrow(data2), replace = TRUE)
# our data now looks like this: 
#(note that these are totally made up incident numbers)
summary(data2)
##       Year         Airmiles       Incidents    
##  Min.   :1937   Min.   :  412   Min.   :0.000  
##  1st Qu.:1943   1st Qu.: 1580   1st Qu.:1.000  
##  Median :1948   Median : 6431   Median :3.500  
##  Mean   :1948   Mean   :10528   Mean   :3.375  
##  3rd Qu.:1954   3rd Qu.:17532   3rd Qu.:5.250  
##  Max.   :1960   Max.   :30514   Max.   :7.000

Lets now plot it in 3d with plotly!

  • Instead of a line represeting our linear regression we now have two lines that make a plane!

So what kind of conclusions can we draw?

  • The beauty of linear regression is it’s simplicity and the ease from which we can intuit general trends.

  • A statistician in 1960 could have made a clear prediction that going forwards air travel will continue to increase in popularity.

  • They also would have been able to intuit that this trend would continue regardless of the number of (fictional) incidents!

The End

This presentation covered some of the basics of linear regression and has hopefully demonstrated some of the value of this very old, but still very useful statistical tool!