-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart6_multiple_linear_regression.R
More file actions
57 lines (46 loc) · 1.6 KB
/
part6_multiple_linear_regression.R
File metadata and controls
57 lines (46 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
############################################################
# PART 6: Multiple Linear Regression
# Author: Suman Bhowmick
# Description:
# Demonstration of multiple linear regression with several
# predictors using the mtcars dataset.
############################################################
### 0. Install and Load Required Packages
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("car")) install.packages("car") # For VIF check
library(tidyverse)
library(car)
### 1. Data Preparation
df <- mtcars %>%
mutate(
cyl = factor(cyl),
gear = factor(gear),
car = rownames(mtcars)
)
cat("\nData prepared. Using predictors: wt, hp, cyl to model mpg.\n")
print(head(df))
### 2. Model Building
multi_model <- lm(mpg ~ wt + hp + cyl, data = df)
cat("\nMultiple linear regression model built.\n")
### 3. Model Summary
cat("\nMODEL SUMMARY:\n")
print(summary(multi_model))
### 4. Multicollinearity Check
vif_values <- vif(multi_model)
cat("\nVARIANCE INFLATION FACTOR (VIF):\n")
print(vif_values)
### 5. Prediction
new_car <- data.frame(
wt = 2.5, # 2500 lbs
hp = 150, # 150 horsepower
cyl = factor(6)
)
prediction <- predict(multi_model, newdata = new_car)
cat("\nPrediction for new car:\n")
print(paste("Predicted MPG:", round(prediction, 2)))
### 6. Model Comparison
simple_model <- lm(mpg ~ wt, data = df)
cat("\nMODEL COMPARISON (Adjusted R-squared):\n")
cat(paste("Simple model (mpg ~ wt):", round(summary(simple_model)$adj.r.squared, 4), "\n"))
cat(paste("Multiple model (mpg ~ wt + hp + cyl):", round(summary(multi_model)$adj.r.squared, 4), "\n"))
cat("\n-- End of Part 6 --\n")