2024-03-21

Introduction

  • Simple linear regression is a data analysis technique
  • Allows us to model and predict the relationship between two variables
  • Equation: \(Y = \beta_{0} + \beta_{1}X + \epsilon\)
    • \(Y\) is the dependent variable
    • \(X\) is the independent variable
    • \(\beta_{0}\) is the y-intercept of the regression line
    • \(\beta_{1}\) is the slope of the regression line
    • \(\epsilon\) is the error term, which is the difference between the observed (actual) value and the predicted value of the dependent variable

Setup

Install necessary packages and import data

library(dplyr)
library(plotly)
library(ggplot2)
df <- read.csv("concrete.csv")

Check to see if there are any NAs in the data:

sum(is.na(df))
## [1] 0

Rename columns:

names(df) <- c("Cement", "Slag", "Fly_ash","Water",
               "Superplasticizer","Coarse_aggregate", 
               "Fine_aggregate", "Age","Compressive_strength")

Cement vs. Compressive Strength

  • We can observe a relatively week relationship between the amount of cement and compressive strength
  • To dive deeper into possible correlations between other variables, we can represent more variables in a plot…

Water and Slag

  • Here we represent two more variables, the amount of water and slag in the concrete
  • There seems to be a slight negative correlation between the amount of slag versus cement

Age and Superplasticizer

  • In this graph, we use a line that is a polynomial function of x
  • There is no significant correlation between superplasticizer and compressive strength
  • There might be slightly more correlation for age as most large age values fall above the regression line

Error in Linear Regression

  • Error is the difference between the observed values and the values predicted by the regression model
  • Mean Squared Error (MSE) is a measure of the average squared difference between the observed values and the values predicted by the model.
  • Equation: \(MSE = {1 \over n} \sum_{i=1}^n (y_i - \hat{y}_i)^2\)
    • \(n\) is the number of observations
    • \(y_i\) is the value of the dependent variable for the ith observation
    • \(\hat{y}_i\) is the predicted value of the dependent variable for the ith observation
  • Calculate the MSE for our model:
mod = lm(Compressive_strength ~ Cement, data=df) # create linear model
predicted <- predict(mod) # predict values using model
residuals <- df$Compressive_strength - predicted # calculate residuals
mse <- mean(residuals^2) # find mse for our model
mse
## [1] 209.7113
  • Our MSE is very high which indicates that our model does not fit the data well

Conclusion

  • Simple linear regression is a method to model and predict the relationship between two variables
  • Sometimes, linear regression might not be as effective due to the nature of the data
  • For cases like this, other statistical or machine learning methods might provide better insight into the data
  • However, the strategic use of data visualization can give us better insight and show the complexity of the relationships between variables.