Linear Regression Using a Built-in R Dataset

 

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

1. Experiment Title

Implementation of Simple Linear Regression Using the Built-in cars Dataset in R


2. Aim

To perform Simple Linear Regression using the built-in cars dataset in R and study the relationship between the speed of a car and its stopping distance.


3. Objectives

After completing this experiment, students should be able to:

  1. Explore a built-in dataset in R.
  2. Understand the relationship between an independent and dependent variable.
  3. Build a Simple Linear Regression model using lm().
  4. Obtain and interpret the regression equation.
  5. Predict values using the regression model.
  6. Calculate predicted values and residuals.
  7. Evaluate the model using R2R^2.
  8. Visualize the regression line.

4. Theory

4.1 Simple Linear Regression

Simple Linear Regression is a statistical technique used to study the relationship between two numerical variables.

It uses:

  • One independent variable XX
  • One dependent variable YY

The objective is to predict the value of YY based on XX.

The general regression model is:

R² = 0.72 · b₀ = intercept · b₁ = slope · least squares minimizes squared vertical residual gaps.

where:
  • Y^\hat{Y} = Predicted value of the dependent variable
  • XX = Independent variable
  • b0b_0 = Intercept
  • b1b_1 = Regression coefficient or slope

The regression line is obtained using the least squares method, which minimizes the sum of squared differences between the actual and predicted values.


4.2 The cars Dataset

R provides a built-in dataset called:

cars

The dataset contains information about cars, including:

VariableDescription
speed    Speed of the car
dist    Stopping distance of the car

The objective of this experiment is to study:

How does the speed of a car affect its stopping distance?

Here:

Independent Variable

X=speedX = \text{speed}

Dependent Variable

Y=distY = \text{dist}

5. Problem Statement

The cars dataset contains the speed and stopping distance of 50 cars.

Using the built-in dataset, perform the following tasks:

  1. Display and explore the dataset.
  2. Build a Simple Linear Regression model to predict stopping distance based on car speed.
  3. Obtain the regression equation.
  4. Display the regression model summary.
  5. Find the predicted stopping distances.
  6. Calculate residuals.
  7. Determine the R2R^2 value.
  8. Predict the stopping distance of a car travelling at a speed of 20.
  9. Plot the actual observations and the fitted regression line.

6. R Program

# -------------------------------------------------
# Simple Linear Regression Using cars Dataset
# -------------------------------------------------

# Load the built-in cars dataset

data(cars)


# -------------------------------------------------
# Step 1: Display the Dataset
# -------------------------------------------------

cat("First Few Records of the Dataset:\n")

head(cars)


# Display the structure of the dataset

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

str(cars)


# Display summary statistics

cat("\nSummary Statistics:\n")

summary(cars)


# -------------------------------------------------
# Step 2: Create a Scatter Plot
# -------------------------------------------------

plot(
  cars$speed,
  cars$dist,
  main = "Car Speed vs Stopping Distance",
  xlab = "Speed",
  ylab = "Stopping Distance",
  pch = 19
)


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

model <- lm(
  dist ~ speed,
  data = cars
)


# -------------------------------------------------
# Step 4: Display Model Details
# -------------------------------------------------

cat("\nLinear Regression Model:\n")

print(model)


# Display detailed summary

cat("\nModel Summary:\n")

summary(model)


# -------------------------------------------------
# Step 5: Display Regression Coefficients
# -------------------------------------------------

coefficients <- coef(model)

cat("\nRegression Coefficients:\n")

print(coefficients)


# -------------------------------------------------
# Step 6: Calculate Predicted Values
# -------------------------------------------------

predicted_values <- predict(model)

cat("\nPredicted Stopping Distances:\n")

print(predicted_values)


# -------------------------------------------------
# Step 7: Calculate Residuals
# -------------------------------------------------

residual_values <- residuals(model)

cat("\nResidual Values:\n")

print(residual_values)


# -------------------------------------------------
# Step 8: Add Predictions and Residuals
# -------------------------------------------------

cars$Predicted_Distance <- predicted_values

cars$Residual <- residual_values


cat("\nDataset with Predictions and Residuals:\n")

head(cars)


# -------------------------------------------------
# Step 9: Find R-Squared Value
# -------------------------------------------------

r_squared <- summary(model)$r.squared

cat(
  "\nR-Squared Value =",
  r_squared,
  "\n"
)


# -------------------------------------------------
# Step 10: Predict for a New Car
# -------------------------------------------------

new_car <- data.frame(
  speed = 20
)


predicted_distance <- predict(
  model,
  newdata = new_car
)


cat(
  "\nPredicted Stopping Distance for Speed 20 =",
  predicted_distance,
  "\n"
)


# -------------------------------------------------
# Step 11: Plot Regression Line
# -------------------------------------------------

plot(
  cars$speed,
  cars$dist,
  main = "Simple Linear Regression",
  xlab = "Speed",
  ylab = "Stopping Distance",
  pch = 19
)


# Add the fitted regression line

abline(
  model,
  col = "blue",
  lwd = 2
)

Output

First Few Records of the Dataset:

Structure of Dataset:
'data.frame':	50 obs. of  2 variables:
 $ speed: num  4 4 7 7 8 9 10 10 10 11 ...
 $ dist : num  2 10 4 22 16 10 18 26 34 17 ...

Summary Statistics:

Linear Regression Model:

Call:
lm(formula = dist ~ speed, data = cars)

Coefficients:
(Intercept)        speed  
    -17.579        3.932  


Model Summary:

Regression Coefficients:
(Intercept)       speed 
 -17.579095    3.932409 

Predicted Stopping Distances:
        1         2         3         4         5         6         7 
-1.849460 -1.849460  9.947766  9.947766 13.880175 17.812584 21.744993 
        8         9        10        11        12        13        14 
21.744993 21.744993 25.677401 25.677401 29.609810 29.609810 29.609810 
       15        16        17        18        19        20        21 
29.609810 33.542219 33.542219 33.542219 33.542219 37.474628 37.474628 
       22        23        24        25        26        27        28 
37.474628 37.474628 41.407036 41.407036 41.407036 45.339445 45.339445 
       29        30        31        32        33        34        35 
49.271854 49.271854 49.271854 53.204263 53.204263 53.204263 53.204263 
       36        37        38        39        40        41        42 
57.136672 57.136672 57.136672 61.069080 61.069080 61.069080 61.069080 
       43        44        45        46        47        48        49 
61.069080 68.933898 72.866307 76.798715 76.798715 76.798715 76.798715 
       50 
80.731124 

Residual Values:
         1          2          3          4          5          6 
  3.849460  11.849460  -5.947766  12.052234   2.119825  -7.812584 
         7          8          9         10         11         12 
 -3.744993   4.255007  12.255007  -8.677401   2.322599 -15.609810 
        13         14         15         16         17         18 
 -9.609810  -5.609810  -1.609810  -7.542219   0.457781   0.457781 
        19         20         21         22         23         24 
 12.457781 -11.474628  -1.474628  22.525372  42.525372 -21.407036 
        25         26         27         28         29         30 
-15.407036  12.592964 -13.339445  -5.339445 -17.271854  -9.271854 
        31         32         33         34         35         36 
  0.728146 -11.204263   2.795737  22.795737  30.795737 -21.136672 
        37         38         39         40         41         42 
-11.136672  10.863328 -29.069080 -13.069080  -9.069080  -5.069080 
        43         44         45         46         47         48 
  2.930920  -2.933898 -18.866307  -6.798715  15.201285  16.201285 
        49         50 
 43.201285   4.268876 

Dataset with Predictions and Residuals:

R-Squared Value = 0.6510794 

Predicted Stopping Distance for Speed 20 = 61.06908 




7. Explanation of the Program

Step 1: Loading the Dataset

data(cars)

The cars dataset is already available in R.

You can display it using:

head(cars)

This shows the first few observations.


Step 2: Exploring the Dataset

str(cars)

This displays the structure of the dataset.

summary(cars)

This provides statistical information such as:

  • Minimum
  • Maximum
  • Mean
  • Median
  • Quartiles

Step 3: Creating a Scatter Plot

plot(cars$speed, cars$dist)

The scatter plot helps us visually examine the relationship between:

  • Car speed
  • Stopping distance

Generally, as the speed increases, the stopping distance also tends to increase.


Step 4: Building the Linear Regression Model

model <- lm(dist ~ speed, data = cars)

The lm() function stands for Linear Model.

The expression:

dist ~ speed

means:

Predict stopping distance (dist) using car speed (speed).


8. Regression Equation

The model produces an equation of the form:

dist^=b0+b1(speed)\hat{dist} = b_0 + b_1(speed)

After running the program, students can obtain the coefficients using:

coef(model)

The general interpretation is:

  • b0b_0 → Expected stopping distance when speed is zero.
  • b1b_1 → Expected change in stopping distance for every one-unit increase in speed.

9. Predicted Values

predict(model)

The predict() function calculates the estimated stopping distance for each car.

For every actual observation, the model produces a corresponding predicted value.


10. Residuals

A residual is the difference between the actual value and the predicted value.

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

In R:

residuals(model)

A good regression model should generally have residuals that are randomly distributed around zero.


11. R-Squared Value

The R2R^2 value indicates how much of the variation in the dependent variable is explained by the independent variable.

It can be obtained using:

summary(model)$r.squared

For example:

  • R2=0.70R^2 = 0.70

means approximately:

70% of the variation in the stopping distance is explained by car speed.


12. Predicting a New Value

The following code predicts the stopping distance when:

Speed = 20
new_car <- data.frame(speed = 20)

predict(model, newdata = new_car)

The model uses the regression equation to estimate the stopping distance.


13. Interpretation of the Graph

The graph contains:

  • Points → Actual observations from the dataset.
  • Regression line → Best-fitting predicted line.

The regression line represents the overall relationship between:

Car Speed and Stopping Distance

If the line has a positive slope, it indicates that:

As the speed of the car increases, the stopping distance tends to increase.


14. Important R Functions Used

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

15. Result

Simple Linear Regression was successfully performed using the built-in cars dataset in R. A regression model was developed to study the relationship between car speed and stopping distance. The model was used to obtain regression coefficients, predicted values, residuals, and the R-squared value, and to predict the stopping distance of a car travelling at a specified speed.

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