2024-10-29

What is Simple Linear Regression

  • Estimated relationship between two variables: dependent and independent
  • Demonstrates strength of correlation between the two variables
  • \(y = \beta_0 + \beta_1x + \varepsilon\)
    • Shows the true y values with epsilon (error)
  • \(\hat{y} = \beta_0 + \beta_1x\)
    • Shows the estimated y-hat values given by the line of best fit
  • Let us visualize this with data between Systolic BP and Diastolic BP

  df = read.csv("C:/Users/hilla/OneDrive/Desktop/DAT301/dataset/heart/heart_attackCSV.csv", 
                sep=",", header = T)
  model1 = lm(pressurelow~pressurehight, data = df)
  intercept1 = coef(model1)[1]
  slope1 = coef(model1)[2]

  equation1 = cat("y =", round(intercept1, 4), "+", round(slope1,4), "*x\n")
## y = 32.2223 + 0.3149 *x
  plot1 = df %>% select(pressurehight, pressurelow) %>% 
        ggplot(aes(x = pressurehight, y = pressurelow)) + 
        geom_point() + 
        geom_smooth(method="lm", level = 0.95) + 
        coord_cartesian(ylim=c(0,175)) + theme_bw() + 
        labs(title = "Simple LR: Systolic BP vs Diastolic BP", 
             x = "Diastolic BP", y = "Systolic BP") + 
        annotate("text", x = 200, y = 160, 
          label ="y = 32.2223 + 0.3149 *x" , color = "red", size = 4)

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

Understanding the Equation

  • \(\varepsilon = y-\hat{y}\)
  • This measures the error between our predicted y value and the actual y value
  • For instance, our linear regression is \(\hat{y} = 32.2223 + 0.3149x\)
  • Now, let us take a point on our plot, and compare the estimated value
  • Random point = (105, 80)
  • In our model \(\hat{y} = 32.2223 + 0.3149(105)\), it becomes \(\hat{y} = 65.2896\)
  • which in our case becomes \(80- 65.2896\), so at \(x=105\), we have a residual of \(14.7132\)
  • To see an overall graph of the residuals…

  residModel = ggplot(model1, aes(x = .fitted, y = .resid)) + 
              geom_point() + 
              geom_hline(color = "blue", yintercept = 0) + 
              labs(title = "Residual Graph", x= "Fitted", y = "Residual")
  print(residModel)

Understanding the residual plot

  • In the previous slide, we see the distance of a point from the line.
  • Each point is the (predicted value of the model, difference between \(y-\hat{y}\))
  • Using this graph, we can look for any randomness.
  • Since, the variables are randomly around the line, it reasonably suggests a linear relationship.
  • Furthermore, the closeness of the residuals to the line also indicates the points are close within perspective of each other and the fitted line.
  • We can also discover some outliers in the graph, as in the top left there is a point that deviates from the rest of the points.

Code for Plotly

  df$gender[(df$gender == 0 ) ] = "Male"
  df$gender[df$gender == 1] = "Female"

  plot1 = df %>% select(pressurehight, pressurelow, gender) %>% 
          ggplot(aes(x = pressurehight, y = pressurelow, color = gender)) + 
          geom_point() + geom_smooth(method = "lm", se = FALSE) +
          labs(title = "Diastolic BP vs Systolic BP based on Gender", 
              x = "Diastolic BP", y = "Systolic BP")
  plotly1 = ggplotly(plot1)
## `geom_smooth()` using formula = 'y ~ x'

  plotly1

Different factors in Linear Regression

  • Here, we can see different linear regression lines in the data based on the gender.
  • However, since the lines appear to be very close together, we can probably surmise that the gender does not affect the relationship between Diastolic BP and Systolic BP
  • Let us try based off of impulse/heart-rate

  print(summary(df))
##       age            gender             impluse        pressurehight  
##  Min.   : 14.00   Length:1319        Min.   :  20.00   Min.   : 42.0  
##  1st Qu.: 47.00   Class :character   1st Qu.:  64.00   1st Qu.:110.0  
##  Median : 58.00   Mode  :character   Median :  74.00   Median :124.0  
##  Mean   : 56.19                      Mean   :  78.34   Mean   :127.2  
##  3rd Qu.: 65.00                      3rd Qu.:  85.00   3rd Qu.:143.0  
##  Max.   :103.00                      Max.   :1111.00   Max.   :223.0  
##   pressurelow        glucose           kcm             troponin      
##  Min.   : 38.00   Min.   : 35.0   Min.   :  0.321   Min.   : 0.0010  
##  1st Qu.: 62.00   1st Qu.: 98.0   1st Qu.:  1.655   1st Qu.: 0.0060  
##  Median : 72.00   Median :116.0   Median :  2.850   Median : 0.0140  
##  Mean   : 72.27   Mean   :146.6   Mean   : 15.274   Mean   : 0.3609  
##  3rd Qu.: 81.00   3rd Qu.:169.5   3rd Qu.:  5.805   3rd Qu.: 0.0855  
##  Max.   :154.00   Max.   :541.0   Max.   :300.000   Max.   :10.3000  
##     class          
##  Length:1319       
##  Class :character  
##  Mode  :character  
##                    
##                    
## 

  df$impluse <- ifelse(df$impluse < 74, "Heart Rate < 74", "Heart Rate >= 74")
  df$impluse <- as.factor(df$impluse)

  plot2 = df %>% select(pressurehight, pressurelow, impluse) %>% 
          ggplot(aes(x = pressurehight, y = pressurelow, color = impluse)) + 
          geom_point() + geom_smooth(method = "lm", se = FALSE) +
          labs(title = "Diastolic BP vs Systolic BP based on Heart Rate", 
              x = "Diastolic BP", y = "Systolic BP")
  plotly2 = ggplotly(plot2)
## `geom_smooth()` using formula = 'y ~ x'

Different factors in Linear Regression

  • Since the median of Impulse was 74, we set 74 to be the split for categorizing Impulse as a discrete variable.
  • Here, unlike gender, the heart rate has a noticeable effect.
  • For heart rates greater than or equal to 74, we can see a steeper line compared to heart rates less than 74.