2025-10-15

Title

Welcome to this short presentation on Linear Regression using R.
We’ll explore how to predict car fuel efficiency (MPG) using engine horsepower and weight.

What is Linear Regression?

Simple linear regression models a response \(y\) as a linear function of predictor \(x\):

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

Where:
- \(y\) = dependent variable
- \(x\) = independent variable
- \(\varepsilon\) = random error

We’ll later extend this to multiple linear regression using two predictors.

Data Preview

We’ll use the built-in mtcars dataset, which includes data on 32 car models.

Let’s preview the first few rows:

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Scatterplot: hp vs mpg (ggplot)

Regression Line on hp vs mpg (ggplot)

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

3D Interactive Plot (plotly)

Fit a Multiple Linear Regression Model (R code)

data(mtcars)
model <- lm(mpg ~ hp + wt, data = mtcars)
summary(model)
## 
## Call:
## lm(formula = mpg ~ hp + wt, data = mtcars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.941 -1.600 -0.182  1.050  5.854 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 37.22727    1.59879  23.285  < 2e-16 ***
## hp          -0.03177    0.00903  -3.519  0.00145 ** 
## wt          -3.87783    0.63273  -6.129 1.12e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared:  0.8268, Adjusted R-squared:  0.8148 
## F-statistic: 69.21 on 2 and 29 DF,  p-value: 9.109e-12

Model Equation (LaTeX)

The fitted multiple linear regression equation is:

\[ \widehat{MPG} = 37.23 + -0.0318 \times HP + -3.88 \times Weight \]

Where:
- Intercept: 37.23
- HP coefficient: -0.0318
- Weight coefficient: -3.88

Diagnostic Plots (ggplot)

Example Code Summary

library(ggplot2)
library(plotly)

data(mtcars)

model <- lm(mpg ~ hp + wt, data = mtcars)

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point(color = "#8C1D40") +
  geom_smooth(method = "lm", se = TRUE, color = "black") +
  labs(title = "MPG vs Horsepower",
       x = "Horsepower (hp)", y = "MPG")

plot_ly(data = mtcars, x = ~hp, y = ~wt, z = ~mpg,
        color = ~mpg, type = "scatter3d",
        mode = "markers", marker = list(size = 4)) %>%
  layout(scene = list(xaxis = list(title = "HP"),
                      yaxis = list(title = "Weight"),
                      zaxis = list(title = "MPG")))

Interpretation & Limitations

Insights:

Weight and horsepower both negatively impact fuel efficiency.

Weight has a stronger effect on MPG than horsepower.

Limitations:

Linear assumption may not capture non-linear trends.

Dataset is small (32 cars).

Model ignores categorical variables like transmission type.

Summary

We used R to:

Fit a multiple linear regression model

Visualize relationships with ggplot and plotly

Express results mathematically with LaTeX

This workflow combines statistics, data visualization, and reproducible reporting, all valuable skills for data science and engineering applications.