Simple Linear Regression, a high-level overview.


Intro to Linear Regression

Linear Regression is focused on predicting an outcome given one or more independent variables. It has real world use in nearly every industry and can be made to be


Simple Linear Regression, Overview.

The concept of linear regression is based around a simple elementary-level algebra concept: the “line-of-best-fit”.

\[ y = mx + b \]

This presentation will cover simple linear regression, where we use a single predictor (independent) variable to infer the value(s) of the dependent variable. To respect with the equation above, our variables are: \[ y : dependent\\ m : slope\\ x : independent\\ b : intercept\\ \]

Example 1: Simple Linear Reg. year against selling_price.

The following plot uses sample data pulled from https://www.kaggle.com/datasets/nehalbirla/vehicle-dataset-from-cardekho?resource=download&select=car+data.csv

## `geom_smooth()` using formula = 'y ~ x'


Example 2: Simple Linear Reg. kms_driven against year.

## `geom_smooth()` using formula = 'y ~ x'


Upgrading to a more contextual model.

Linear Regression can be applied to multiple influencing variables. In our case, we may want to see how both kms_driven and year affect selling_price. As a result, applying two variables would help us determine a “best-fit” for this specific case. Do keep in mind that while this still gives us somewhat useful data, there is far more context that can be applied to give us a clearer picture.


Linear Regression with multiple variables, Overview.

The mathematics for the equation of the line is as follows: \[ y = c + \beta_1x_1 + \beta_2x_2 +... \beta_nx_n \] In this case, I’ve taken the liberty of swapping out the familiar \[ y = mx + b \] since this problem is slightly more complex.

With the new variables: \[ y: dependent\\ \beta_n: slope_n\\ x_n: independent_n\\ c: constant \\ \] We can use the Residual Sum of Squares formula to help us find the difference between our prediction and the actual data values we’ve supplied for the model. The residual simply represents the error present in our prediction. I will not be diving deeply into this nor using it (simply because I am not educated enough), but I feel it is important to mention the existence of this formula. \[ \text{RSS} = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 \]


Example 3: Linear Regression in a 3D visualization + code overview.

model <- lm(Selling_Price ~ Year + Kms_Driven, data = car_data)

year_seq <- seq(min(car_data$Year), max(car_data$Year), length.out = 30)
km_seq <- seq(min(car_data$Kms_Driven), max(car_data$Kms_Driven), length.out = 30)

grid <- expand.grid(Year = year_seq, Kms_Driven = km_seq)
grid$Predicted_Price <- predict(model, newdata = grid)

plot_ly() %>%
  add_markers(data = car_data,
              x = ~Year, y = ~Kms_Driven, z = ~Selling_Price,
              marker = list(color = 'blue', size = 4, opacity = 0.6),
              name = 'Actual Data') %>%
  add_surface(x = ~year_seq, y = ~km_seq,
              z = matrix(grid$Predicted_Price,
                         nrow = length(year_seq),
                         ncol = length(km_seq)),
              colorscale = list(c(0,1), c("orange", "red")),
              opacity = 0.7,
              name = 'Regression Plane') %>%
  layout(
    title = "3D Linear Regression - Expected Selling Price given Year and Km driven",
    scene = list(
      xaxis = list(title = "Year"),
      yaxis = list(title = "Km Driven"),
      zaxis = list(title = "Selling Price (in lakhs)")
    )
  )

The code at the top of the page generates a 3d representation of how Year and Km Driven may have an effect on the Selling Price in our data set.


Conclusion

This was a very brief and simple overview of linear regression. The topic has a plethora of resources available online for further learning and my presentation does not do it justice. There are notable real-world weaknesses with using only linear regression; it is heavily reliant on existing data to fuel its predictions. It is pertinent to examine the data set more rigorously before deriving any concrete findings.