Multiple Linear Regression Using a Built-in R Dataset

 

Experiment: Multiple Linear Regression Using a Built-in R Dataset

1. Experiment Title

Implementation of Multiple Linear Regression Using the Built-in mtcars Dataset in R


2. Aim

To perform Multiple Linear Regression using the built-in mtcars dataset in R and study how multiple vehicle characteristics influence fuel efficiency.


3. Objectives

After completing this experiment, students should be able to:

  1. Explore a built-in dataset in R.
  2. Understand the concept of Multiple Linear Regression.
  3. Identify dependent and independent variables.
  4. Build a Multiple Linear Regression model using lm().
  5. Interpret regression coefficients.
  6. Obtain predicted values and residuals.
  7. Evaluate the model using R2R^2 and Adjusted R2R^2.
  8. Predict the fuel efficiency of a new car.

4. Theory

4.1 Multiple Linear Regression

Multiple Linear Regression is used to study the relationship between:

  • One dependent variable, and
  • Two or more independent variables.

It extends Simple Linear Regression by using multiple predictor variables.

The general model is:

Y^=b0+b1X1+b2X2+⋯+bnXn\hat{Y}=b_0+b_1X_1+b_2X_2+\cdots+b_nX_n

where:

  • Y^\hat{Y} = Predicted value of the dependent variable
  • b0b_0 = Intercept
  • b1,b2,…,bnb_1,b_2,\ldots,b_n = Regression coefficients
  • X1,X2,…,XnX_1,X_2,\ldots,X_n = Independent variables

4.2 The mtcars Dataset

R provides a built-in dataset called:

mtcars

The dataset contains information about 32 automobiles and their characteristics.

Some important variables are:

VariableDescription
mpg    Miles per gallon (fuel efficiency)
cyl    Number of cylinders
disp    Engine displacement
hp    Gross horsepower
wt    Vehicle weight
am    Transmission type

5. Problem Statement

A vehicle analyst wants to study how the horsepower and weight of a car influence its fuel efficiency.

Using the built-in mtcars dataset:

  • Dependent variable: Miles per gallon (mpg)
  • Independent variables:
    • Horsepower (hp)
    • Weight (wt)

Perform Multiple Linear Regression and analyze the relationship between these variables.

Students should:

  1. Explore the mtcars dataset.
  2. Build a Multiple Linear Regression model.
  3. Obtain the regression coefficients.
  4. Display the detailed model summary.
  5. Find R2R^2 and Adjusted R2R^2.
  6. Calculate predicted values and residuals.
  7. Compare actual and predicted fuel efficiency.
  8. Predict the fuel efficiency of a new car.
  9. Visualize the actual versus predicted values.

6. R Program

# -------------------------------------------------
# Multiple Linear Regression Using mtcars Dataset
# -------------------------------------------------

# Load the built-in mtcars dataset

data(mtcars)


# -------------------------------------------------
# Step 1: Explore the Dataset
# -------------------------------------------------

cat("First Few Records:\n")

head(mtcars)


# Display structure

cat("\nStructure of Dataset:\n")

str(mtcars)


# Display summary statistics

cat("\nSummary Statistics:\n")

summary(mtcars)


# -------------------------------------------------
# Step 2: Select Required Variables
# -------------------------------------------------

car_data <- mtcars[, c("mpg", "hp", "wt")]

cat("\nSelected Data:\n")

head(car_data)


# -------------------------------------------------
# Step 3: Build Multiple Linear Regression Model
# -------------------------------------------------

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


# -------------------------------------------------
# Step 4: Display the Model
# -------------------------------------------------

cat("\nMultiple Linear Regression Model:\n")

print(model)


# -------------------------------------------------
# Step 5: Display Model Summary
# -------------------------------------------------

cat("\nModel Summary:\n")

summary(model)


# -------------------------------------------------
# Step 6: Display Regression Coefficients
# -------------------------------------------------

cat("\nRegression Coefficients:\n")

coefficients <- coef(model)

print(coefficients)


# -------------------------------------------------
# Step 7: Calculate Predicted Values
# -------------------------------------------------

predicted_mpg <- predict(model)

cat("\nPredicted MPG Values:\n")

print(predicted_mpg)


# -------------------------------------------------
# Step 8: Calculate Residuals
# -------------------------------------------------

residual_values <- residuals(model)

cat("\nResidual Values:\n")

print(residual_values)


# -------------------------------------------------
# Step 9: Add Predictions to the Dataset
# -------------------------------------------------

car_data$Predicted_MPG <- predicted_mpg

car_data$Residual <- residual_values


cat("\nActual and Predicted Values:\n")

print(car_data)


# -------------------------------------------------
# Step 10: Find R-Squared Values
# -------------------------------------------------

model_summary <- summary(model)


cat(
  "\nR-Squared =",
  model_summary$r.squared,
  "\n"
)


cat(
  "Adjusted R-Squared =",
  model_summary$adj.r.squared,
  "\n"
)


# -------------------------------------------------
# Step 11: Predict Fuel Efficiency of a New Car
# -------------------------------------------------

new_car <- data.frame(
  hp = 120,
  wt = 3.0
)


predicted_value <- predict(
  model,
  newdata = new_car
)


cat(
  "\nPredicted MPG for New Car =",
  predicted_value,
  "\n"
)


# -------------------------------------------------
# Step 12: Plot Actual vs Predicted Values
# -------------------------------------------------

plot(
  car_data$mpg,
  predicted_mpg,
  main = "Actual vs Predicted Fuel Efficiency",
  xlab = "Actual MPG",
  ylab = "Predicted MPG",
  pch = 19
)


# Add Reference Line

abline(
  a = 0,
  b = 1,
  col = "blue",
  lwd = 2
)

Output

First Few Records:

Structure of Dataset:
'data.frame':	32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

Summary Statistics:

Selected Data:

Multiple Linear Regression Model:

Call:
lm(formula = mpg ~ hp + wt, data = car_data)

Coefficients:
(Intercept)           hp           wt  
   37.22727     -0.03177     -3.87783  


Model Summary:

Regression Coefficients:
(Intercept)          hp          wt 
37.22727012 -0.03177295 -3.87783074 

Predicted MPG Values:
          Mazda RX4       Mazda RX4 Wag          Datsun 710 
          23.572329           22.583483           25.275819 
     Hornet 4 Drive   Hornet Sportabout             Valiant 
          21.265020           18.327267           20.473816 
         Duster 360           Merc 240D            Merc 230 
          15.599042           22.887067           21.993673 
           Merc 280           Merc 280C          Merc 450SE 
          19.979460           19.979460           15.725369 
         Merc 450SL         Merc 450SLC  Cadillac Fleetwood 
          17.043831           16.849939           10.355205 
Lincoln Continental   Chrysler Imperial            Fiat 128 
           9.362733            9.192487           26.599028 
        Honda Civic      Toyota Corolla       Toyota Corona 
          29.312380           28.046209           24.586441 
   Dodge Challenger         AMC Javelin          Camaro Z28 
          18.811364           19.140979           14.552028 
   Pontiac Firebird           Fiat X1-9       Porsche 914-2 
          16.756745           27.626653           26.037374 
       Lotus Europa      Ford Pantera L        Ferrari Dino 
          27.769769           16.546489           20.925413 
      Maserati Bora          Volvo 142E 
          12.739477           22.983649 

Residual Values:
          Mazda RX4       Mazda RX4 Wag          Datsun 710 
        -2.57232940         -1.58348256         -2.47581872 
     Hornet 4 Drive   Hornet Sportabout             Valiant 
         0.13497989          0.37273336         -2.37381631 
         Duster 360           Merc 240D            Merc 230 
        -1.29904236          1.51293266          0.80632669 
           Merc 280           Merc 280C          Merc 450SE 
        -0.77945988         -2.17945988          0.67463146 
         Merc 450SL         Merc 450SLC  Cadillac Fleetwood 
         0.25616901         -1.64993945          0.04479541 
Lincoln Continental   Chrysler Imperial            Fiat 128 
         1.03726743          5.50751301          5.80097202 
        Honda Civic      Toyota Corolla       Toyota Corona 
         1.08761978          5.85379085         -3.08644148 
   Dodge Challenger         AMC Javelin          Camaro Z28 
        -3.31136386         -3.94097947         -1.25202805 
   Pontiac Firebird           Fiat X1-9       Porsche 914-2 
         2.44325481         -0.32665313         -0.03737415 
       Lotus Europa      Ford Pantera L        Ferrari Dino 
         2.63023081         -0.74648866         -1.22541324 
      Maserati Bora          Volvo 142E 
         2.26052287         -1.58364943 

Actual and Predicted Values:
                     mpg  hp    wt Predicted_MPG    Residual
Mazda RX4           21.0 110 2.620     23.572329 -2.57232940
Mazda RX4 Wag       21.0 110 2.875     22.583483 -1.58348256
Datsun 710          22.8  93 2.320     25.275819 -2.47581872
Hornet 4 Drive      21.4 110 3.215     21.265020  0.13497989
Hornet Sportabout   18.7 175 3.440     18.327267  0.37273336
Valiant             18.1 105 3.460     20.473816 -2.37381631
Duster 360          14.3 245 3.570     15.599042 -1.29904236
Merc 240D           24.4  62 3.190     22.887067  1.51293266
Merc 230            22.8  95 3.150     21.993673  0.80632669
Merc 280            19.2 123 3.440     19.979460 -0.77945988
Merc 280C           17.8 123 3.440     19.979460 -2.17945988
Merc 450SE          16.4 180 4.070     15.725369  0.67463146
Merc 450SL          17.3 180 3.730     17.043831  0.25616901
Merc 450SLC         15.2 180 3.780     16.849939 -1.64993945
Cadillac Fleetwood  10.4 205 5.250     10.355205  0.04479541
Lincoln Continental 10.4 215 5.424      9.362733  1.03726743
Chrysler Imperial   14.7 230 5.345      9.192487  5.50751301
Fiat 128            32.4  66 2.200     26.599028  5.80097202
Honda Civic         30.4  52 1.615     29.312380  1.08761978
Toyota Corolla      33.9  65 1.835     28.046209  5.85379085
Toyota Corona       21.5  97 2.465     24.586441 -3.08644148
Dodge Challenger    15.5 150 3.520     18.811364 -3.31136386
AMC Javelin         15.2 150 3.435     19.140979 -3.94097947
Camaro Z28          13.3 245 3.840     14.552028 -1.25202805
Pontiac Firebird    19.2 175 3.845     16.756745  2.44325481
Fiat X1-9           27.3  66 1.935     27.626653 -0.32665313
Porsche 914-2       26.0  91 2.140     26.037374 -0.03737415
Lotus Europa        30.4 113 1.513     27.769769  2.63023081
Ford Pantera L      15.8 264 3.170     16.546489 -0.74648866
Ferrari Dino        19.7 175 2.770     20.925413 -1.22541324
Maserati Bora       15.0 335 3.570     12.739477  2.26052287
Volvo 142E          21.4 109 2.780     22.983649 -1.58364943

R-Squared = 0.8267855 
Adjusted R-Squared = 0.8148396 

Predicted MPG for New Car = 21.78102 



7. Explanation of the Program

Step 1: Loading the Dataset

data(mtcars)

The mtcars dataset is a built-in R dataset containing information about different cars.

To view the first few records:

head(mtcars)

Step 2: Exploring the Dataset

str(mtcars)

This displays:

  • Variable names
  • Data types
  • Number of observations

The command:

summary(mtcars)

provides descriptive statistics for each variable.


8. Building the Multiple Regression Model

The model is created using:

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

This means:

Predict fuel efficiency (mpg) using horsepower (hp) and vehicle weight (wt).

The regression equation has the form:

mpg^=b0+b1(hp)+b2(wt)\widehat{mpg} = b_0+b_1(hp)+b_2(wt)

where:

  • b0b_0 = Intercept
  • b1b_1 = Effect of horsepower
  • b2b_2 = Effect of vehicle weight

9. Interpretation of Regression Coefficients

The coefficients can be obtained using:

coef(model)

Horsepower coefficient

The coefficient of hp represents the expected change in fuel efficiency when horsepower increases by one unit, while keeping vehicle weight constant.

Weight coefficient

The coefficient of wt represents the expected change in fuel efficiency when vehicle weight increases by one unit, while keeping horsepower constant.

This phrase is very important in Multiple Linear Regression:

The effect of one independent variable is interpreted while keeping the other independent variables constant.


10. Predicted Values

predicted_mpg <- predict(model)

This calculates the predicted fuel efficiency for every car in the dataset.

The program then adds these predictions to the data frame.


11. Residuals

A residual is calculated as:

Residual=Actual Value−Predicted Value\text{Residual} = \text{Actual Value} - \text{Predicted Value}

The residuals are obtained using:

residuals(model)

Positive residual:

Actual value is greater than predicted value.

Negative residual:

Actual value is less than predicted value.


12. R-Squared

The R2R^2 value indicates how much variation in mpg is explained by the independent variables.

summary(model)$r.squared

For example:

If R2=0.80R^2=0.80, approximately 80% of the variation in fuel efficiency is explained by horsepower and vehicle weight.


13. Adjusted R-Squared

Adjusted R2R^2 is especially useful in Multiple Linear Regression because it considers the number of predictor variables used.

It is obtained using:

summary(model)$adj.r.squared

Unlike ordinary R2R^2, Adjusted R2R^2 penalizes unnecessary variables.


14. Predicting for a New Car

The program predicts the fuel efficiency of a car with:

  • Horsepower = 120
  • Weight = 3.0
new_car <- data.frame(
  hp = 120,
  wt = 3.0
)

The prediction is calculated using:

predict(model, newdata = new_car)

15. Actual vs Predicted Plot

The graph compares:

  • Actual MPG on the X-axis
  • Predicted MPG on the Y-axis

The reference line:

y=x

represents perfect predictions.

Therefore:

The closer the points are to the reference line, the closer the predicted values are to the actual values.


16. Important Functions Used

FunctionPurpose
data()    Loads a built-in dataset
head()    Displays first few records
str()    Displays dataset structure
summary()    Provides statistical summary
lm()    Builds a linear regression model
coef()    Obtains regression coefficients
predict()    Predicts values
residuals()    Obtains residuals
abline()    Adds a reference line
plot()    Creates a graph

17. Result

Multiple Linear Regression was successfully implemented using the built-in mtcars dataset in R. The model was developed to predict the fuel efficiency of a car based on its horsepower and weight. Regression coefficients, predicted values, residuals, R-squared, and Adjusted R-squared values were obtained, and the model was used to predict the fuel efficiency of a new car.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Programs to try using Functions in R - Assignment 3