Assignment
2026-04-19
## Objective
Multiple Liner regression Assignment - To analyze the impact of horsepower (hp) and weight (wt) on milea
---
## Initial Model
``` r
data("mtcars")
RM <- lm(mpg ~ hp + wt, data = mtcars)
summary(RM)
##
## 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
Interpretation:
R-squared = 82.268% indicates that the model explains most of the variation in mpg. Both hp and wt have
negative coefficients, meaning higher horsepower and weight reduce mileage. All variables are statistically
significant (p < 0.05).
Linearity Check
1
plot(RM, which = 1)
Residuals vs Fitted
6
Toyota
Fiat 128Corolla
Chrysler Imperial
4
Residuals
2
0
−2
−4
10 15 20 25 30
Fitted values
lm(mpg ~ hp + wt)
Interpretation: Residual plot shows curvature, thus model is not linear.
Improved Model (Log Transformation)
RM2 <- lm(log(mpg) ~ hp + wt, data = mtcars)
plot(RM2, which = 1)
2
Residuals vs Fitted
0.3
Chrysler Imperial
0.2
Fiat 128
Residuals
0.1
0.0
−0.2
AMC Javelin
2.4 2.6 2.8 3.0 3.2 3.4
Fitted values
lm(log(mpg) ~ hp + wt)
Interpretation: Model becomes comparatively more linear.
Residual Analysis
plot(RM2$residuals)
abline(h = 0, col = "red")
3
0.3
0.2
RM2$residuals
0.1
0.0
−0.1
−0.2
0 5 10 15 20 25 30
Index
Normality Test
Residual <- residuals(RM2)
[Link](Residual)
##
## Shapiro-Wilk normality test
##
## data: Residual
## W = 0.95973, p-value = 0.2701
Interpretation : Dataset is normal as p value > 0.05
qqnorm(Residual, col = "red")
qqline(Residual, col = "green")
4
Normal Q−Q Plot
0.3
0.2
Sample Quantiles
0.1
0.0
−0.1
−0.2
−2 −1 0 1 2
Theoretical Quantiles
plot(RM2, which = 2)
5
Q−Q Residuals
3
Chrysler Imperial
Standardized residuals
Fiat 128
1
0
−1
AMC Javelin
−2 −1 0 1 2
Theoretical Quantiles
lm(log(mpg) ~ hp + wt)
Interpretation :Residuals are approximately normal with slight deviations at the tails, which is acceptable
Homoscedasticity
library(zoo)
##
## Attaching package: ’zoo’
## The following objects are masked from ’package:base’:
##
## [Link], [Link]
library(lmtest)
library(nortest)
bptest(RM2)
##
## studentized Breusch-Pagan test
##
## data: RM2
## BP = 3.621, df = 2, p-value = 0.1636
plot(RM2, which = 3)
6
Scale−Location
Chrysler Imperial
1.5
Standardized residuals
AMC Javelin Fiat 128
1.0
0.5
0.0
2.4 2.6 2.8 3.0 3.2 3.4
Fitted values
lm(log(mpg) ~ hp + wt)
Interpretation : p value is > 0.05, thus data has homoscedasticity Model is homoscedastic but not perfectly,
there is mild heteroscedasticity as seen as slightly downward sloping line
Multicollinearity
library(carData)
library(car)
vif(RM2)
## hp wt
## 1.766625 1.766625
Interpretation : value < 5, No serious multicollinearity between variables.
Autocorrelation & Influence
dwtest(RM2)
##
## Durbin-Watson test
##
## data: RM2
## DW = 1.6099, p-value = 0.09806
## alternative hypothesis: true autocorrelation is greater than 0
7
plot(RM2, which = 5)
Residuals vs Leverage
3
Chrysler Imperial
Standardized residuals
1
2
0.5
Maserati Bora
1
0
−1
Cadillac Fleetwood
0.5
−2
Cook's distance 1
0.0 0.1 0.2 0.3 0.4
Leverage
lm(log(mpg) ~ hp + wt)
Dw = 1.7 which is close to 2 and p > 0.05 , thus autocorrelation is Negligible Maserati Bora shows high
leverage but no extreme influence