This project uses the Life Expectancy WHO data set, with a focus on how schooling and body mass index (BMI) affect life expectancy.
2025-04-12
This project uses the Life Expectancy WHO data set, with a focus on how schooling and body mass index (BMI) affect life expectancy.
Linear regression is a method to describe the relationship between two variables. More specifically, it illustrates how well one variable can be used to predict changes in the response variable. Part of this method includes the regression line, which can be used to predict missing values. One way to find to find this line is through the method of least squares where the distance between the data points and the line of best fit are minimized.
The equation of the regression line is found through the following:
\(\widehat{y}=a+bx\) where \(\widehat{y}\) is the dependent variable, \(a\) is the y-intercept, \(b\) is the slope of the line, and \(x\) is the independent variable
## A marker object has been specified, but markers is not in the mode ## Adding markers to the mode...
fig = plot_ly(data = life_data, x = ~Schooling, y = ~Life.expectancy,
type = "scatter",
mode = "markers",
marker = list(size = 5))
fig = fig %>% add_trace(x = ~Schooling,
y = ~fitted,
mode = "lines",
name = "Linear Regression",
line = list(color = "red", width = 2))
fig = fig %>% layout(title = "Schooling vs Life Expectancy",
xaxis = list(title = "Schooling (in years)"),
yaxis = list(title = "Life Expectancy (in years)"))
In a linear regression model, residuals are the difference between the expected and actual value of y.
Residual = actual \(y\) value - predicted \(y\) value
\(r_{i}=y_{i}-\widehat{y}_{i}\)
model = lm(Life.expectancy ~ Schooling, data = life_data)
life_data$residuals = residuals(model)
r = ggplot(life_data, aes(x = Schooling, y = residuals)) +
geom_point(alpha = 0.5) +
geom_hline(yintercept = 0,
linetype = "dashed", color = "red") +
labs(title = "Residual Plot",
x = "Schooling (in years)",
y = "Residuals")