2024-04-06

What is Simple Linear Regression

  • Simple linear regression is a statistical method used to model the relationship between two variables: one independent variable and one dependent variable

  • Simple linear regression assumes that the relationship between these variables can be described by a straight line

  • We commonly understand the formula of a straight line to be: \[ y = mx + b \] or \[ y = ax + b \]

  • The formula for simple linear regression is fairly similar, but we alter it to better model the relationship between a dependent variable and an independent variable in statistical analysis

Understanding the Simple Linear Regression Formula

\[ y = \beta_0 + \beta_1 x + \epsilon \]

  • y: Dependent variable (the response)

  • β₀: y- Intercept

  • β₁: Slope coefficient

  • x: Independent variable (considered the predictor)

  • ε: Error term

R Code for Plotly Graph 1

#comparing age vs serum_creatine
x <- df$age
y <- df$serum_creatinine

lm_model <- lm(y ~ x)

y_pred <- predict(lm_model)

plot_ly(x = x, y = y, mode = "markers", type = "scatter",
        name = "Data") %>%
  add_trace(x = x, y = y_pred, mode = "lines",
        name = "Regression Line") %>%
  layout(title = "Age vs. Serum Creatinine Level",
         xaxis = list(title = "Age (Years)"),
         yaxis = list(title = "Serum Creatinine Level(mg/dL)"))
summary(lm_model)

Plotly Graph 1: Age vs. Serum Creatinine Level

Summary of Ploty Graph 1

## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.9900 -0.4516 -0.2716  0.0981  7.7408 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 0.551649   0.308810   1.786   0.0751 . 
## x           0.013845   0.004982   2.779   0.0058 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.023 on 297 degrees of freedom
## Multiple R-squared:  0.02534,    Adjusted R-squared:  0.02206 
## F-statistic: 7.722 on 1 and 297 DF,  p-value: 0.005803

R Code for Plotly Graph 2

color_legend <- scales::col_numeric(palette = c("blue", "red"),
                                    domain = c(0, 1))

plot_ly(df, x = ~serum_sodium, y = ~ejection_fraction, z = ~age, 
        color = ~DEATH_EVENT, colors = color_legend,
        type = "scatter3d", mode = "markers", 
        marker = list(size = 5)) %>%
  layout(scene = list(xaxis = list(title = "Serum Sodium (mEq/L)"),
                      yaxis = list(title = "Ejection Fraction (%)"),
                      zaxis = list(title = "Age (Years)"),
                      aspectratio = list(x = 1, y = 1, z = 1)),
         title = "3D Scatterplot: Age vs. (Ejection Fraction, Serum Sodium Level)")

Plotly Graph 2: 3D Scatterplot

  • where 1 (Red) signifies that a Death Event Occurred
  • where 0 (Blue) signifies that No Death Event Occurred

R code for ggplot Graph 1

#comparing age vs serum_creatine
x <- df$age
y <- df$serum_creatinine

lm_model <- lm(y ~ x)

y_pred <- predict(lm_model)

ggplot(df, aes(x = age, y = serum_creatinine)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +  
  labs(title = "Age vs. Serum Creatinine Level",
       x = "Age (Years)",
       y = "Serum Creatinine (mg/dL)")

ggplot Graph 1: Age vs. Serum Creatinine Level

R code for ggplot Graph 2

x <- df$age
y <- df$serum_sodium

lm_model <- lm(y ~ x)

y_pred <- predict(lm_model)

ggplot(df, aes(x = age, y = serum_sodium)) +
  geom_point() +  # Scatter plot
  geom_smooth(method = "lm", se = FALSE, color = "green") +  
  labs(title = "Age vs. Serum Sodium Level",
       x = "Age (Years)",
       y = "Serum Sodium (mEq/L)")

ggplot Graph 2: Age vs Serum Sodium Level