1 Group Members

Student ID Name Role
17061830 Nadzirah Raihan Discussions & Conclusion
22104060 Zhang Wei Introduction & Project Objective
23059592 Boaz Chung Yi Heng Data Understanding
23069683 Diva Alifta Chandra Data Analysis - Regressor
23083416 Yong Ting Kang Data Analysis - Classifier
23072236 Sim Jin Xiang Data Cleaning

2 Introduction

When exploring strategic approaches in the automotive industry, utilizing machine learning models to understand and predict car pricing can significantly enhance decision-making and optimize business operations.

This project presents a comprehensive analysis of employing both classification and regression models to dissect the intricacies of car pricing based on a rich dataset that includes a variety of car attributes such as make, fuel type, body style, and more importantly, price.

The dataset provides a robust platform for applying these models, containing detailed features that describe each car’s specifications. By analyzing these attributes, we aim to classify cars into distinct price segments—high-end and mid-low-end—using classification algorithms. This segmentation will allow for targeted marketing strategies and precise customer segmentation, crucial for competitive advantage in a saturated market.

Furthermore, we explore the use of regression models to predict the exact price of cars based on their features. This predictive modeling will help in setting accurate prices, essential for maintaining profitability and competitiveness. By predicting prices, we can also gain insights into market trends and the factors influencing car values, which is invaluable for strategic planning and risk management.

This project aims to not only demonstrate the application of these models to real-world data but also to discuss the implications of the findings in terms of business strategies and operations optimization in the automotive sector. By leveraging machine learning, automotive businesses can achieve a greater understanding of their product pricing and market dynamics, thereby enhancing their marketing, sales, and development strategies.

2.1 Objectives & Outputs

2.1.1 Using Classification Models to Segment Car Prices

2.1.1.1 Precision Marketing:

By identifying cars in different price segments, companies can more effectively target their market and design customized marketing strategies for different customer groups. For example, high-end cars might focus more on promoting brand image and unique features, while mid-low-end cars might emphasize cost-effectiveness and practical features.

2.1.1.2 Product Positioning and Pricing Strategy:

By categorizing cars into high-end and mid-low-end segments, companies can adjust their product features and pricing strategies to maximize market coverage and profits.

2.1.1.3 Promotional and Sales Strategies:

Classification models enable sales teams to better understand customer purchasing power and preferences, allowing for effective sales and promotional strategies.

2.1.1.4 Consumer Insights:

Analyzing the behaviors and preferences of car buyers across different price segments can provide important insights into consumer demand. This helps businesses better understand market dynamics and design products that meet consumer expectations.

2.1.2 Using Regression Models to Predict Car Prices

2.1.2.1 Accurate Pricing:

Regression models can predict the specific price of cars, providing businesses with a scientific basis for pricing, helping to set more precise prices and avoiding profit losses or reduced market competitiveness due to overpricing or underpricing.

2.1.2.2 Market Trend Analysis:

By analyzing how various factors affect car prices, regression models can reveal trends and causes of price changes, providing strategic decision support for a company’s long-term development.

2.1.2.3 Risk Management:

In financial services such as car loans and insurance, accurate price predictions help assess asset values and associated risks, thereby designing reasonable loan amounts and insurance rates.

2.1.2.4 Supply Chain Optimization:

Predicting the price and market demand for different types of cars enables companies to manage their production plans and inventory more effectively, reducing costs and improving operational efficiency.

3 Loading Necessary Packages

# Load necessary packages
suppressPackageStartupMessages({
  library(dplyr)
  library(VIM)
  library(ggplot2)
  library(zoo)
  library(gridExtra)
})

4 Data Understanding

4.1 Loading Data

This project uses the dataset titled “Automobile” from 1985 Ward’s Automotive Yearbook donated on 18-May-1987.

# Use the existing dataset
data <- read.csv('imports-85.data', sep=',')

# Rename columns if necessary
colnames(data) <- c('symboling', 'normalized.losses','make', 'fuel.type','aspiration',
                    'num.of.doors','body.style','drive.wheels','engine.location',
                    'wheel.base','length','width','height','curb.weight','engine.type',
                    'num.of.cylinders','engine.size','fuel.system','bore','stroke',
                    'compression.ratio','horsepower','peak.rpm','city.mpg','highway.mpg',
                    'price')

4.2 Initial EDA

This dataset is structured in a tabular format and is made up of 26 columns and 205 rows (or observations). It consists of three types of information: the vehicle specifications, its assigned insurance risk rating, and its normalized losses. The variable names, data types and summary statistics of each variable are described below.

# Dataset dimensions
dim(data)
## [1] 204  26
# Inspecting the data
head(data)
##   symboling normalized.losses        make fuel.type aspiration num.of.doors
## 1         3                 ? alfa-romero       gas        std          two
## 2         1                 ? alfa-romero       gas        std          two
## 3         2               164        audi       gas        std         four
## 4         2               164        audi       gas        std         four
## 5         2                 ?        audi       gas        std          two
## 6         1               158        audi       gas        std         four
##    body.style drive.wheels engine.location wheel.base length width height
## 1 convertible          rwd           front       88.6  168.8  64.1   48.8
## 2   hatchback          rwd           front       94.5  171.2  65.5   52.4
## 3       sedan          fwd           front       99.8  176.6  66.2   54.3
## 4       sedan          4wd           front       99.4  176.6  66.4   54.3
## 5       sedan          fwd           front       99.8  177.3  66.3   53.1
## 6       sedan          fwd           front      105.8  192.7  71.4   55.7
##   curb.weight engine.type num.of.cylinders engine.size fuel.system bore stroke
## 1        2548        dohc             four         130        mpfi 3.47   2.68
## 2        2823        ohcv              six         152        mpfi 2.68   3.47
## 3        2337         ohc             four         109        mpfi 3.19   3.40
## 4        2824         ohc             five         136        mpfi 3.19   3.40
## 5        2507         ohc             five         136        mpfi 3.19   3.40
## 6        2844         ohc             five         136        mpfi 3.19   3.40
##   compression.ratio horsepower peak.rpm city.mpg highway.mpg price
## 1               9.0        111     5000       21          27 16500
## 2               9.0        154     5000       19          26 16500
## 3              10.0        102     5500       24          30 13950
## 4               8.0        115     5500       18          22 17450
## 5               8.5        110     5500       19          25 15250
## 6               8.5        110     5500       19          25 17710
# Variable names and data types
str(data)
## 'data.frame':    204 obs. of  26 variables:
##  $ symboling        : int  3 1 2 2 2 1 1 1 0 2 ...
##  $ normalized.losses: chr  "?" "?" "164" "164" ...
##  $ make             : chr  "alfa-romero" "alfa-romero" "audi" "audi" ...
##  $ fuel.type        : chr  "gas" "gas" "gas" "gas" ...
##  $ aspiration       : chr  "std" "std" "std" "std" ...
##  $ num.of.doors     : chr  "two" "two" "four" "four" ...
##  $ body.style       : chr  "convertible" "hatchback" "sedan" "sedan" ...
##  $ drive.wheels     : chr  "rwd" "rwd" "fwd" "4wd" ...
##  $ engine.location  : chr  "front" "front" "front" "front" ...
##  $ wheel.base       : num  88.6 94.5 99.8 99.4 99.8 ...
##  $ length           : num  169 171 177 177 177 ...
##  $ width            : num  64.1 65.5 66.2 66.4 66.3 71.4 71.4 71.4 67.9 64.8 ...
##  $ height           : num  48.8 52.4 54.3 54.3 53.1 55.7 55.7 55.9 52 54.3 ...
##  $ curb.weight      : int  2548 2823 2337 2824 2507 2844 2954 3086 3053 2395 ...
##  $ engine.type      : chr  "dohc" "ohcv" "ohc" "ohc" ...
##  $ num.of.cylinders : chr  "four" "six" "four" "five" ...
##  $ engine.size      : int  130 152 109 136 136 136 136 131 131 108 ...
##  $ fuel.system      : chr  "mpfi" "mpfi" "mpfi" "mpfi" ...
##  $ bore             : chr  "3.47" "2.68" "3.19" "3.19" ...
##  $ stroke           : chr  "2.68" "3.47" "3.40" "3.40" ...
##  $ compression.ratio: num  9 9 10 8 8.5 8.5 8.5 8.3 7 8.8 ...
##  $ horsepower       : chr  "111" "154" "102" "115" ...
##  $ peak.rpm         : chr  "5000" "5000" "5500" "5500" ...
##  $ city.mpg         : int  21 19 24 18 19 19 19 17 16 23 ...
##  $ highway.mpg      : int  27 26 30 22 25 25 25 20 22 29 ...
##  $ price            : chr  "16500" "16500" "13950" "17450" ...
# Summary statistics
summary(data)
##    symboling       normalized.losses      make            fuel.type        
##  Min.   :-2.0000   Length:204         Length:204         Length:204        
##  1st Qu.: 0.0000   Class :character   Class :character   Class :character  
##  Median : 1.0000   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 0.8235                                                           
##  3rd Qu.: 2.0000                                                           
##  Max.   : 3.0000                                                           
##   aspiration        num.of.doors        body.style        drive.wheels      
##  Length:204         Length:204         Length:204         Length:204        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##  engine.location      wheel.base         length          width      
##  Length:204         Min.   : 86.60   Min.   :141.1   Min.   :60.30  
##  Class :character   1st Qu.: 94.50   1st Qu.:166.3   1st Qu.:64.08  
##  Mode  :character   Median : 97.00   Median :173.2   Median :65.50  
##                     Mean   : 98.81   Mean   :174.1   Mean   :65.92  
##                     3rd Qu.:102.40   3rd Qu.:183.2   3rd Qu.:66.90  
##                     Max.   :120.90   Max.   :208.1   Max.   :72.30  
##      height       curb.weight   engine.type        num.of.cylinders  
##  Min.   :47.80   Min.   :1488   Length:204         Length:204        
##  1st Qu.:52.00   1st Qu.:2145   Class :character   Class :character  
##  Median :54.10   Median :2414   Mode  :character   Mode  :character  
##  Mean   :53.75   Mean   :2556                                        
##  3rd Qu.:55.50   3rd Qu.:2939                                        
##  Max.   :59.80   Max.   :4066                                        
##   engine.size    fuel.system            bore              stroke         
##  Min.   : 61.0   Length:204         Length:204         Length:204        
##  1st Qu.: 97.0   Class :character   Class :character   Class :character  
##  Median :119.5   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :126.9                                                           
##  3rd Qu.:142.0                                                           
##  Max.   :326.0                                                           
##  compression.ratio  horsepower          peak.rpm            city.mpg    
##  Min.   : 7.000    Length:204         Length:204         Min.   :13.00  
##  1st Qu.: 8.575    Class :character   Class :character   1st Qu.:19.00  
##  Median : 9.000    Mode  :character   Mode  :character   Median :24.00  
##  Mean   :10.148                                          Mean   :25.24  
##  3rd Qu.: 9.400                                          3rd Qu.:30.00  
##  Max.   :23.000                                          Max.   :49.00  
##   highway.mpg       price          
##  Min.   :16.00   Length:204        
##  1st Qu.:25.00   Class :character  
##  Median :30.00   Mode  :character  
##  Mean   :30.77                     
##  3rd Qu.:34.50                     
##  Max.   :54.00

5 Data Cleaning

#We can see that many attributes are not of the correct datatype. 
# Replace '?' with NA
data[data == '?'] <- NA

# Identify columns with at least one NA value
columns_with_na <- colnames(data)[colSums(is.na(data)) > 0]
data_filtered <- data[, columns_with_na]

# Visualize missing data
aggr(data_filtered, numbers = TRUE, sortVars = TRUE, labels = colnames(data_filtered), col = c('red', 'blue'), cex.axis = 0.55)

## 
##  Variables sorted by number of missings: 
##           Variable       Count
##  normalized.losses 0.196078431
##               bore 0.019607843
##             stroke 0.019607843
##              price 0.019607843
##       num.of.doors 0.009803922
##         horsepower 0.009803922
##           peak.rpm 0.009803922

5.1 Findings

# Convert 'normalized.losses' to numeric (if it's not already)
data$normalized.losses <- as.numeric(as.character(data$normalized.losses))

# Create a histogram after filtering out NA values
ggplot(data[!is.na(data$normalized.losses), ], aes(x = normalized.losses)) +
  geom_histogram(binwidth = 5,color="blue",fill="lightgreen") +  
  xlab("Normalized Losses") +
  ylab("Count")

# Calculate summary statistics by symboling
summary_stats <- aggregate(normalized.losses ~ symboling, data, function(x) {
  c(count = length(na.omit(x)),
    mean = mean(x, na.rm = TRUE),
    min = min(x, na.rm = TRUE),
    Q1 = quantile(x, probs = 0.25, na.rm = TRUE),
    median = median(x, na.rm = TRUE),
    Q3 = quantile(x, probs = 0.75, na.rm = TRUE),
    max = max(x, na.rm = TRUE))
})

print(summary_stats)
##   symboling normalized.losses.count normalized.losses.mean
## 1        -2                  3.0000               103.0000
## 2        -1                 20.0000                85.6000
## 3         0                 48.0000               113.1667
## 4         1                 47.0000               128.5745
## 5         2                 29.0000               125.6897
## 6         3                 17.0000               168.6471
##   normalized.losses.min normalized.losses.Q1.25% normalized.losses.median
## 1              103.0000                 103.0000                 103.0000
## 2               65.0000                  71.7500                  91.5000
## 3               77.0000                  91.0000                 102.0000
## 4               74.0000                 105.5000                 125.0000
## 5               83.0000                  94.0000                 134.0000
## 6              142.0000                 150.0000                 150.0000
##   normalized.losses.Q3.75% normalized.losses.max
## 1                 103.0000              103.0000
## 2                  95.0000              137.0000
## 3                 120.5000              192.0000
## 4                 148.0000              231.0000
## 5                 137.0000              192.0000
## 6                 194.0000              256.0000

5.2 Drop NA values from specific columns

data2 <- data[complete.cases(data[, c("price", "bore", "stroke", "peak.rpm", "horsepower", "num.of.doors")]), ]
data2
##     symboling normalized.losses          make fuel.type aspiration num.of.doors
## 1           3                NA   alfa-romero       gas        std          two
## 2           1                NA   alfa-romero       gas        std          two
## 3           2               164          audi       gas        std         four
## 4           2               164          audi       gas        std         four
## 5           2                NA          audi       gas        std          two
## 6           1               158          audi       gas        std         four
## 7           1                NA          audi       gas        std         four
## 8           1               158          audi       gas      turbo         four
## 10          2               192           bmw       gas        std          two
## 11          0               192           bmw       gas        std         four
## 12          0               188           bmw       gas        std          two
## 13          0               188           bmw       gas        std         four
## 14          1                NA           bmw       gas        std         four
## 15          0                NA           bmw       gas        std         four
## 16          0                NA           bmw       gas        std          two
## 17          0                NA           bmw       gas        std         four
## 18          2               121     chevrolet       gas        std          two
## 19          1                98     chevrolet       gas        std          two
## 20          0                81     chevrolet       gas        std         four
## 21          1               118         dodge       gas        std          two
## 22          1               118         dodge       gas        std          two
## 23          1               118         dodge       gas      turbo          two
## 24          1               148         dodge       gas        std         four
## 25          1               148         dodge       gas        std         four
## 26          1               148         dodge       gas        std         four
## 28         -1               110         dodge       gas        std         four
## 29          3               145         dodge       gas      turbo          two
## 30          2               137         honda       gas        std          two
## 31          2               137         honda       gas        std          two
## 32          1               101         honda       gas        std          two
## 33          1               101         honda       gas        std          two
## 34          1               101         honda       gas        std          two
## 35          0               110         honda       gas        std         four
## 36          0                78         honda       gas        std         four
## 37          0               106         honda       gas        std          two
## 38          0               106         honda       gas        std          two
## 39          0                85         honda       gas        std         four
## 40          0                85         honda       gas        std         four
## 41          0                85         honda       gas        std         four
## 42          1               107         honda       gas        std          two
## 43          0                NA         isuzu       gas        std         four
## 46          2                NA         isuzu       gas        std          two
## 47          0               145        jaguar       gas        std         four
## 48          0                NA        jaguar       gas        std         four
## 49          0                NA        jaguar       gas        std          two
## 50          1               104         mazda       gas        std          two
## 51          1               104         mazda       gas        std          two
## 52          1               104         mazda       gas        std          two
## 53          1               113         mazda       gas        std         four
## 54          1               113         mazda       gas        std         four
## 59          1               129         mazda       gas        std          two
## 60          0               115         mazda       gas        std         four
## 61          1               129         mazda       gas        std          two
## 62          0               115         mazda       gas        std         four
## 64          0               115         mazda       gas        std         four
## 65          0               118         mazda       gas        std         four
## 66          0                NA         mazda    diesel        std         four
## 67         -1                93 mercedes-benz    diesel      turbo         four
## 68         -1                93 mercedes-benz    diesel      turbo         four
## 69          0                93 mercedes-benz    diesel      turbo          two
## 70         -1                93 mercedes-benz    diesel      turbo         four
## 71         -1                NA mercedes-benz       gas        std         four
## 72          3               142 mercedes-benz       gas        std          two
## 73          0                NA mercedes-benz       gas        std         four
## 74          1                NA mercedes-benz       gas        std          two
## 75          1                NA       mercury       gas      turbo          two
## 76          2               161    mitsubishi       gas        std          two
## 77          2               161    mitsubishi       gas        std          two
## 78          2               161    mitsubishi       gas        std          two
## 79          1               161    mitsubishi       gas      turbo          two
## 80          3               153    mitsubishi       gas      turbo          two
## 81          3               153    mitsubishi       gas        std          two
## 82          3                NA    mitsubishi       gas      turbo          two
## 83          3                NA    mitsubishi       gas      turbo          two
## 84          3                NA    mitsubishi       gas      turbo          two
## 85          1               125    mitsubishi       gas        std         four
## 86          1               125    mitsubishi       gas        std         four
## 87          1               125    mitsubishi       gas      turbo         four
## 88         -1               137    mitsubishi       gas        std         four
## 89          1               128        nissan       gas        std          two
## 90          1               128        nissan    diesel        std          two
## 91          1               128        nissan       gas        std          two
## 92          1               122        nissan       gas        std         four
## 93          1               103        nissan       gas        std         four
## 94          1               128        nissan       gas        std          two
## 95          1               128        nissan       gas        std          two
## 96          1               122        nissan       gas        std         four
## 97          1               103        nissan       gas        std         four
## 98          2               168        nissan       gas        std          two
## 99          0               106        nissan       gas        std         four
## 100         0               106        nissan       gas        std         four
## 101         0               128        nissan       gas        std         four
## 102         0               108        nissan       gas        std         four
## 103         0               108        nissan       gas        std         four
## 104         3               194        nissan       gas        std          two
## 105         3               194        nissan       gas      turbo          two
## 106         1               231        nissan       gas        std          two
## 107         0               161        peugot       gas        std         four
## 108         0               161        peugot    diesel      turbo         four
## 109         0                NA        peugot       gas        std         four
## 110         0                NA        peugot    diesel      turbo         four
## 111         0               161        peugot       gas        std         four
## 112         0               161        peugot    diesel      turbo         four
## 113         0                NA        peugot       gas        std         four
## 114         0                NA        peugot    diesel      turbo         four
## 115         0               161        peugot       gas        std         four
## 116         0               161        peugot    diesel      turbo         four
## 117         0               161        peugot       gas      turbo         four
## 118         1               119      plymouth       gas        std          two
## 119         1               119      plymouth       gas      turbo          two
## 120         1               154      plymouth       gas        std         four
## 121         1               154      plymouth       gas        std         four
## 122         1               154      plymouth       gas        std         four
## 123        -1                74      plymouth       gas        std         four
## 124         3                NA      plymouth       gas      turbo          two
## 125         3               186       porsche       gas        std          two
## 126         3                NA       porsche       gas        std          two
## 127         3                NA       porsche       gas        std          two
## 128         3                NA       porsche       gas        std          two
## 132         3               150          saab       gas        std          two
## 133         2               104          saab       gas        std         four
## 134         3               150          saab       gas        std          two
## 135         2               104          saab       gas        std         four
## 136         3               150          saab       gas      turbo          two
## 137         2               104          saab       gas      turbo         four
## 138         2                83        subaru       gas        std          two
## 139         2                83        subaru       gas        std          two
## 140         2                83        subaru       gas        std          two
## 141         0               102        subaru       gas        std         four
## 142         0               102        subaru       gas        std         four
## 143         0               102        subaru       gas        std         four
## 144         0               102        subaru       gas        std         four
## 145         0               102        subaru       gas      turbo         four
## 146         0                89        subaru       gas        std         four
## 147         0                89        subaru       gas        std         four
## 148         0                85        subaru       gas        std         four
## 149         0                85        subaru       gas      turbo         four
## 150         1                87        toyota       gas        std          two
## 151         1                87        toyota       gas        std          two
## 152         1                74        toyota       gas        std         four
## 153         0                77        toyota       gas        std         four
## 154         0                81        toyota       gas        std         four
## 155         0                91        toyota       gas        std         four
## 156         0                91        toyota       gas        std         four
## 157         0                91        toyota       gas        std         four
## 158         0                91        toyota    diesel        std         four
## 159         0                91        toyota    diesel        std         four
## 160         0                91        toyota       gas        std         four
## 161         0                91        toyota       gas        std         four
## 162         0                91        toyota       gas        std         four
## 163         1               168        toyota       gas        std          two
## 164         1               168        toyota       gas        std          two
## 165         1               168        toyota       gas        std          two
## 166         1               168        toyota       gas        std          two
## 167         2               134        toyota       gas        std          two
## 168         2               134        toyota       gas        std          two
## 169         2               134        toyota       gas        std          two
## 170         2               134        toyota       gas        std          two
## 171         2               134        toyota       gas        std          two
## 172         2               134        toyota       gas        std          two
## 173        -1                65        toyota       gas        std         four
## 174        -1                65        toyota    diesel      turbo         four
## 175        -1                65        toyota       gas        std         four
## 176        -1                65        toyota       gas        std         four
## 177        -1                65        toyota       gas        std         four
## 178         3               197        toyota       gas        std          two
## 179         3               197        toyota       gas        std          two
## 180        -1                90        toyota       gas        std         four
## 181        -1                NA        toyota       gas        std         four
## 182         2               122    volkswagen    diesel        std          two
## 183         2               122    volkswagen       gas        std          two
## 184         2                94    volkswagen    diesel        std         four
## 185         2                94    volkswagen       gas        std         four
## 186         2                94    volkswagen       gas        std         four
## 187         2                94    volkswagen    diesel      turbo         four
## 188         2                94    volkswagen       gas        std         four
## 189         3                NA    volkswagen       gas        std          two
## 190         3               256    volkswagen       gas        std          two
## 191         0                NA    volkswagen       gas        std         four
## 192         0                NA    volkswagen    diesel      turbo         four
## 193         0                NA    volkswagen       gas        std         four
## 194        -2               103         volvo       gas        std         four
## 195        -1                74         volvo       gas        std         four
## 196        -2               103         volvo       gas        std         four
## 197        -1                74         volvo       gas        std         four
## 198        -2               103         volvo       gas      turbo         four
## 199        -1                74         volvo       gas      turbo         four
## 200        -1                95         volvo       gas        std         four
## 201        -1                95         volvo       gas      turbo         four
## 202        -1                95         volvo       gas        std         four
## 203        -1                95         volvo    diesel      turbo         four
## 204        -1                95         volvo       gas      turbo         four
##      body.style drive.wheels engine.location wheel.base length width height
## 1   convertible          rwd           front       88.6  168.8  64.1   48.8
## 2     hatchback          rwd           front       94.5  171.2  65.5   52.4
## 3         sedan          fwd           front       99.8  176.6  66.2   54.3
## 4         sedan          4wd           front       99.4  176.6  66.4   54.3
## 5         sedan          fwd           front       99.8  177.3  66.3   53.1
## 6         sedan          fwd           front      105.8  192.7  71.4   55.7
## 7         wagon          fwd           front      105.8  192.7  71.4   55.7
## 8         sedan          fwd           front      105.8  192.7  71.4   55.9
## 10        sedan          rwd           front      101.2  176.8  64.8   54.3
## 11        sedan          rwd           front      101.2  176.8  64.8   54.3
## 12        sedan          rwd           front      101.2  176.8  64.8   54.3
## 13        sedan          rwd           front      101.2  176.8  64.8   54.3
## 14        sedan          rwd           front      103.5  189.0  66.9   55.7
## 15        sedan          rwd           front      103.5  189.0  66.9   55.7
## 16        sedan          rwd           front      103.5  193.8  67.9   53.7
## 17        sedan          rwd           front      110.0  197.0  70.9   56.3
## 18    hatchback          fwd           front       88.4  141.1  60.3   53.2
## 19    hatchback          fwd           front       94.5  155.9  63.6   52.0
## 20        sedan          fwd           front       94.5  158.8  63.6   52.0
## 21    hatchback          fwd           front       93.7  157.3  63.8   50.8
## 22    hatchback          fwd           front       93.7  157.3  63.8   50.8
## 23    hatchback          fwd           front       93.7  157.3  63.8   50.8
## 24    hatchback          fwd           front       93.7  157.3  63.8   50.6
## 25        sedan          fwd           front       93.7  157.3  63.8   50.6
## 26        sedan          fwd           front       93.7  157.3  63.8   50.6
## 28        wagon          fwd           front      103.3  174.6  64.6   59.8
## 29    hatchback          fwd           front       95.9  173.2  66.3   50.2
## 30    hatchback          fwd           front       86.6  144.6  63.9   50.8
## 31    hatchback          fwd           front       86.6  144.6  63.9   50.8
## 32    hatchback          fwd           front       93.7  150.0  64.0   52.6
## 33    hatchback          fwd           front       93.7  150.0  64.0   52.6
## 34    hatchback          fwd           front       93.7  150.0  64.0   52.6
## 35        sedan          fwd           front       96.5  163.4  64.0   54.5
## 36        wagon          fwd           front       96.5  157.1  63.9   58.3
## 37    hatchback          fwd           front       96.5  167.5  65.2   53.3
## 38    hatchback          fwd           front       96.5  167.5  65.2   53.3
## 39        sedan          fwd           front       96.5  175.4  65.2   54.1
## 40        sedan          fwd           front       96.5  175.4  62.5   54.1
## 41        sedan          fwd           front       96.5  175.4  65.2   54.1
## 42        sedan          fwd           front       96.5  169.1  66.0   51.0
## 43        sedan          rwd           front       94.3  170.7  61.8   53.5
## 46    hatchback          rwd           front       96.0  172.6  65.2   51.4
## 47        sedan          rwd           front      113.0  199.6  69.6   52.8
## 48        sedan          rwd           front      113.0  199.6  69.6   52.8
## 49        sedan          rwd           front      102.0  191.7  70.6   47.8
## 50    hatchback          fwd           front       93.1  159.1  64.2   54.1
## 51    hatchback          fwd           front       93.1  159.1  64.2   54.1
## 52    hatchback          fwd           front       93.1  159.1  64.2   54.1
## 53        sedan          fwd           front       93.1  166.8  64.2   54.1
## 54        sedan          fwd           front       93.1  166.8  64.2   54.1
## 59    hatchback          fwd           front       98.8  177.8  66.5   53.7
## 60        sedan          fwd           front       98.8  177.8  66.5   55.5
## 61    hatchback          fwd           front       98.8  177.8  66.5   53.7
## 62        sedan          fwd           front       98.8  177.8  66.5   55.5
## 64    hatchback          fwd           front       98.8  177.8  66.5   55.5
## 65        sedan          rwd           front      104.9  175.0  66.1   54.4
## 66        sedan          rwd           front      104.9  175.0  66.1   54.4
## 67        sedan          rwd           front      110.0  190.9  70.3   56.5
## 68        wagon          rwd           front      110.0  190.9  70.3   58.7
## 69      hardtop          rwd           front      106.7  187.5  70.3   54.9
## 70        sedan          rwd           front      115.6  202.6  71.7   56.3
## 71        sedan          rwd           front      115.6  202.6  71.7   56.5
## 72  convertible          rwd           front       96.6  180.3  70.5   50.8
## 73        sedan          rwd           front      120.9  208.1  71.7   56.7
## 74      hardtop          rwd           front      112.0  199.2  72.0   55.4
## 75    hatchback          rwd           front      102.7  178.4  68.0   54.8
## 76    hatchback          fwd           front       93.7  157.3  64.4   50.8
## 77    hatchback          fwd           front       93.7  157.3  64.4   50.8
## 78    hatchback          fwd           front       93.7  157.3  64.4   50.8
## 79    hatchback          fwd           front       93.0  157.3  63.8   50.8
## 80    hatchback          fwd           front       96.3  173.0  65.4   49.4
## 81    hatchback          fwd           front       96.3  173.0  65.4   49.4
## 82    hatchback          fwd           front       95.9  173.2  66.3   50.2
## 83    hatchback          fwd           front       95.9  173.2  66.3   50.2
## 84    hatchback          fwd           front       95.9  173.2  66.3   50.2
## 85        sedan          fwd           front       96.3  172.4  65.4   51.6
## 86        sedan          fwd           front       96.3  172.4  65.4   51.6
## 87        sedan          fwd           front       96.3  172.4  65.4   51.6
## 88        sedan          fwd           front       96.3  172.4  65.4   51.6
## 89        sedan          fwd           front       94.5  165.3  63.8   54.5
## 90        sedan          fwd           front       94.5  165.3  63.8   54.5
## 91        sedan          fwd           front       94.5  165.3  63.8   54.5
## 92        sedan          fwd           front       94.5  165.3  63.8   54.5
## 93        wagon          fwd           front       94.5  170.2  63.8   53.5
## 94        sedan          fwd           front       94.5  165.3  63.8   54.5
## 95    hatchback          fwd           front       94.5  165.6  63.8   53.3
## 96        sedan          fwd           front       94.5  165.3  63.8   54.5
## 97        wagon          fwd           front       94.5  170.2  63.8   53.5
## 98      hardtop          fwd           front       95.1  162.4  63.8   53.3
## 99    hatchback          fwd           front       97.2  173.4  65.2   54.7
## 100       sedan          fwd           front       97.2  173.4  65.2   54.7
## 101       sedan          fwd           front      100.4  181.7  66.5   55.1
## 102       wagon          fwd           front      100.4  184.6  66.5   56.1
## 103       sedan          fwd           front      100.4  184.6  66.5   55.1
## 104   hatchback          rwd           front       91.3  170.7  67.9   49.7
## 105   hatchback          rwd           front       91.3  170.7  67.9   49.7
## 106   hatchback          rwd           front       99.2  178.5  67.9   49.7
## 107       sedan          rwd           front      107.9  186.7  68.4   56.7
## 108       sedan          rwd           front      107.9  186.7  68.4   56.7
## 109       wagon          rwd           front      114.2  198.9  68.4   58.7
## 110       wagon          rwd           front      114.2  198.9  68.4   58.7
## 111       sedan          rwd           front      107.9  186.7  68.4   56.7
## 112       sedan          rwd           front      107.9  186.7  68.4   56.7
## 113       wagon          rwd           front      114.2  198.9  68.4   56.7
## 114       wagon          rwd           front      114.2  198.9  68.4   58.7
## 115       sedan          rwd           front      107.9  186.7  68.4   56.7
## 116       sedan          rwd           front      107.9  186.7  68.4   56.7
## 117       sedan          rwd           front      108.0  186.7  68.3   56.0
## 118   hatchback          fwd           front       93.7  157.3  63.8   50.8
## 119   hatchback          fwd           front       93.7  157.3  63.8   50.8
## 120   hatchback          fwd           front       93.7  157.3  63.8   50.6
## 121       sedan          fwd           front       93.7  167.3  63.8   50.8
## 122       sedan          fwd           front       93.7  167.3  63.8   50.8
## 123       wagon          fwd           front      103.3  174.6  64.6   59.8
## 124   hatchback          rwd           front       95.9  173.2  66.3   50.2
## 125   hatchback          rwd           front       94.5  168.9  68.3   50.2
## 126     hardtop          rwd            rear       89.5  168.9  65.0   51.6
## 127     hardtop          rwd            rear       89.5  168.9  65.0   51.6
## 128 convertible          rwd            rear       89.5  168.9  65.0   51.6
## 132   hatchback          fwd           front       99.1  186.6  66.5   56.1
## 133       sedan          fwd           front       99.1  186.6  66.5   56.1
## 134   hatchback          fwd           front       99.1  186.6  66.5   56.1
## 135       sedan          fwd           front       99.1  186.6  66.5   56.1
## 136   hatchback          fwd           front       99.1  186.6  66.5   56.1
## 137       sedan          fwd           front       99.1  186.6  66.5   56.1
## 138   hatchback          fwd           front       93.7  156.9  63.4   53.7
## 139   hatchback          fwd           front       93.7  157.9  63.6   53.7
## 140   hatchback          4wd           front       93.3  157.3  63.8   55.7
## 141       sedan          fwd           front       97.2  172.0  65.4   52.5
## 142       sedan          fwd           front       97.2  172.0  65.4   52.5
## 143       sedan          fwd           front       97.2  172.0  65.4   52.5
## 144       sedan          4wd           front       97.0  172.0  65.4   54.3
## 145       sedan          4wd           front       97.0  172.0  65.4   54.3
## 146       wagon          fwd           front       97.0  173.5  65.4   53.0
## 147       wagon          fwd           front       97.0  173.5  65.4   53.0
## 148       wagon          4wd           front       96.9  173.6  65.4   54.9
## 149       wagon          4wd           front       96.9  173.6  65.4   54.9
## 150   hatchback          fwd           front       95.7  158.7  63.6   54.5
## 151   hatchback          fwd           front       95.7  158.7  63.6   54.5
## 152   hatchback          fwd           front       95.7  158.7  63.6   54.5
## 153       wagon          fwd           front       95.7  169.7  63.6   59.1
## 154       wagon          4wd           front       95.7  169.7  63.6   59.1
## 155       wagon          4wd           front       95.7  169.7  63.6   59.1
## 156       sedan          fwd           front       95.7  166.3  64.4   53.0
## 157   hatchback          fwd           front       95.7  166.3  64.4   52.8
## 158       sedan          fwd           front       95.7  166.3  64.4   53.0
## 159   hatchback          fwd           front       95.7  166.3  64.4   52.8
## 160       sedan          fwd           front       95.7  166.3  64.4   53.0
## 161   hatchback          fwd           front       95.7  166.3  64.4   52.8
## 162       sedan          fwd           front       95.7  166.3  64.4   52.8
## 163       sedan          rwd           front       94.5  168.7  64.0   52.6
## 164   hatchback          rwd           front       94.5  168.7  64.0   52.6
## 165       sedan          rwd           front       94.5  168.7  64.0   52.6
## 166   hatchback          rwd           front       94.5  168.7  64.0   52.6
## 167     hardtop          rwd           front       98.4  176.2  65.6   52.0
## 168     hardtop          rwd           front       98.4  176.2  65.6   52.0
## 169   hatchback          rwd           front       98.4  176.2  65.6   52.0
## 170     hardtop          rwd           front       98.4  176.2  65.6   52.0
## 171   hatchback          rwd           front       98.4  176.2  65.6   52.0
## 172 convertible          rwd           front       98.4  176.2  65.6   53.0
## 173       sedan          fwd           front      102.4  175.6  66.5   54.9
## 174       sedan          fwd           front      102.4  175.6  66.5   54.9
## 175   hatchback          fwd           front      102.4  175.6  66.5   53.9
## 176       sedan          fwd           front      102.4  175.6  66.5   54.9
## 177   hatchback          fwd           front      102.4  175.6  66.5   53.9
## 178   hatchback          rwd           front      102.9  183.5  67.7   52.0
## 179   hatchback          rwd           front      102.9  183.5  67.7   52.0
## 180       sedan          rwd           front      104.5  187.8  66.5   54.1
## 181       wagon          rwd           front      104.5  187.8  66.5   54.1
## 182       sedan          fwd           front       97.3  171.7  65.5   55.7
## 183       sedan          fwd           front       97.3  171.7  65.5   55.7
## 184       sedan          fwd           front       97.3  171.7  65.5   55.7
## 185       sedan          fwd           front       97.3  171.7  65.5   55.7
## 186       sedan          fwd           front       97.3  171.7  65.5   55.7
## 187       sedan          fwd           front       97.3  171.7  65.5   55.7
## 188       sedan          fwd           front       97.3  171.7  65.5   55.7
## 189 convertible          fwd           front       94.5  159.3  64.2   55.6
## 190   hatchback          fwd           front       94.5  165.7  64.0   51.4
## 191       sedan          fwd           front      100.4  180.2  66.9   55.1
## 192       sedan          fwd           front      100.4  180.2  66.9   55.1
## 193       wagon          fwd           front      100.4  183.1  66.9   55.1
## 194       sedan          rwd           front      104.3  188.8  67.2   56.2
## 195       wagon          rwd           front      104.3  188.8  67.2   57.5
## 196       sedan          rwd           front      104.3  188.8  67.2   56.2
## 197       wagon          rwd           front      104.3  188.8  67.2   57.5
## 198       sedan          rwd           front      104.3  188.8  67.2   56.2
## 199       wagon          rwd           front      104.3  188.8  67.2   57.5
## 200       sedan          rwd           front      109.1  188.8  68.9   55.5
## 201       sedan          rwd           front      109.1  188.8  68.8   55.5
## 202       sedan          rwd           front      109.1  188.8  68.9   55.5
## 203       sedan          rwd           front      109.1  188.8  68.9   55.5
## 204       sedan          rwd           front      109.1  188.8  68.9   55.5
##     curb.weight engine.type num.of.cylinders engine.size fuel.system bore
## 1          2548        dohc             four         130        mpfi 3.47
## 2          2823        ohcv              six         152        mpfi 2.68
## 3          2337         ohc             four         109        mpfi 3.19
## 4          2824         ohc             five         136        mpfi 3.19
## 5          2507         ohc             five         136        mpfi 3.19
## 6          2844         ohc             five         136        mpfi 3.19
## 7          2954         ohc             five         136        mpfi 3.19
## 8          3086         ohc             five         131        mpfi 3.13
## 10         2395         ohc             four         108        mpfi 3.50
## 11         2395         ohc             four         108        mpfi 3.50
## 12         2710         ohc              six         164        mpfi 3.31
## 13         2765         ohc              six         164        mpfi 3.31
## 14         3055         ohc              six         164        mpfi 3.31
## 15         3230         ohc              six         209        mpfi 3.62
## 16         3380         ohc              six         209        mpfi 3.62
## 17         3505         ohc              six         209        mpfi 3.62
## 18         1488           l            three          61        2bbl 2.91
## 19         1874         ohc             four          90        2bbl 3.03
## 20         1909         ohc             four          90        2bbl 3.03
## 21         1876         ohc             four          90        2bbl 2.97
## 22         1876         ohc             four          90        2bbl 2.97
## 23         2128         ohc             four          98        mpfi 3.03
## 24         1967         ohc             four          90        2bbl 2.97
## 25         1989         ohc             four          90        2bbl 2.97
## 26         1989         ohc             four          90        2bbl 2.97
## 28         2535         ohc             four         122        2bbl 3.34
## 29         2811         ohc             four         156         mfi 3.60
## 30         1713         ohc             four          92        1bbl 2.91
## 31         1819         ohc             four          92        1bbl 2.91
## 32         1837         ohc             four          79        1bbl 2.91
## 33         1940         ohc             four          92        1bbl 2.91
## 34         1956         ohc             four          92        1bbl 2.91
## 35         2010         ohc             four          92        1bbl 2.91
## 36         2024         ohc             four          92        1bbl 2.92
## 37         2236         ohc             four         110        1bbl 3.15
## 38         2289         ohc             four         110        1bbl 3.15
## 39         2304         ohc             four         110        1bbl 3.15
## 40         2372         ohc             four         110        1bbl 3.15
## 41         2465         ohc             four         110        mpfi 3.15
## 42         2293         ohc             four         110        2bbl 3.15
## 43         2337         ohc             four         111        2bbl 3.31
## 46         2734         ohc             four         119        spfi 3.43
## 47         4066        dohc              six         258        mpfi 3.63
## 48         4066        dohc              six         258        mpfi 3.63
## 49         3950        ohcv           twelve         326        mpfi 3.54
## 50         1890         ohc             four          91        2bbl 3.03
## 51         1900         ohc             four          91        2bbl 3.03
## 52         1905         ohc             four          91        2bbl 3.03
## 53         1945         ohc             four          91        2bbl 3.03
## 54         1950         ohc             four          91        2bbl 3.08
## 59         2385         ohc             four         122        2bbl 3.39
## 60         2410         ohc             four         122        2bbl 3.39
## 61         2385         ohc             four         122        2bbl 3.39
## 62         2410         ohc             four         122        2bbl 3.39
## 64         2425         ohc             four         122        2bbl 3.39
## 65         2670         ohc             four         140        mpfi 3.76
## 66         2700         ohc             four         134         idi 3.43
## 67         3515         ohc             five         183         idi 3.58
## 68         3750         ohc             five         183         idi 3.58
## 69         3495         ohc             five         183         idi 3.58
## 70         3770         ohc             five         183         idi 3.58
## 71         3740        ohcv            eight         234        mpfi 3.46
## 72         3685        ohcv            eight         234        mpfi 3.46
## 73         3900        ohcv            eight         308        mpfi 3.80
## 74         3715        ohcv            eight         304        mpfi 3.80
## 75         2910         ohc             four         140        mpfi 3.78
## 76         1918         ohc             four          92        2bbl 2.97
## 77         1944         ohc             four          92        2bbl 2.97
## 78         2004         ohc             four          92        2bbl 2.97
## 79         2145         ohc             four          98        spdi 3.03
## 80         2370         ohc             four         110        spdi 3.17
## 81         2328         ohc             four         122        2bbl 3.35
## 82         2833         ohc             four         156        spdi 3.58
## 83         2921         ohc             four         156        spdi 3.59
## 84         2926         ohc             four         156        spdi 3.59
## 85         2365         ohc             four         122        2bbl 3.35
## 86         2405         ohc             four         122        2bbl 3.35
## 87         2403         ohc             four         110        spdi 3.17
## 88         2403         ohc             four         110        spdi 3.17
## 89         1889         ohc             four          97        2bbl 3.15
## 90         2017         ohc             four         103         idi 2.99
## 91         1918         ohc             four          97        2bbl 3.15
## 92         1938         ohc             four          97        2bbl 3.15
## 93         2024         ohc             four          97        2bbl 3.15
## 94         1951         ohc             four          97        2bbl 3.15
## 95         2028         ohc             four          97        2bbl 3.15
## 96         1971         ohc             four          97        2bbl 3.15
## 97         2037         ohc             four          97        2bbl 3.15
## 98         2008         ohc             four          97        2bbl 3.15
## 99         2324         ohc             four         120        2bbl 3.33
## 100        2302         ohc             four         120        2bbl 3.33
## 101        3095        ohcv              six         181        mpfi 3.43
## 102        3296        ohcv              six         181        mpfi 3.43
## 103        3060        ohcv              six         181        mpfi 3.43
## 104        3071        ohcv              six         181        mpfi 3.43
## 105        3139        ohcv              six         181        mpfi 3.43
## 106        3139        ohcv              six         181        mpfi 3.43
## 107        3020           l             four         120        mpfi 3.46
## 108        3197           l             four         152         idi 3.70
## 109        3230           l             four         120        mpfi 3.46
## 110        3430           l             four         152         idi 3.70
## 111        3075           l             four         120        mpfi 3.46
## 112        3252           l             four         152         idi 3.70
## 113        3285           l             four         120        mpfi 3.46
## 114        3485           l             four         152         idi 3.70
## 115        3075           l             four         120        mpfi 3.46
## 116        3252           l             four         152         idi 3.70
## 117        3130           l             four         134        mpfi 3.61
## 118        1918         ohc             four          90        2bbl 2.97
## 119        2128         ohc             four          98        spdi 3.03
## 120        1967         ohc             four          90        2bbl 2.97
## 121        1989         ohc             four          90        2bbl 2.97
## 122        2191         ohc             four          98        2bbl 2.97
## 123        2535         ohc             four         122        2bbl 3.35
## 124        2818         ohc             four         156        spdi 3.59
## 125        2778         ohc             four         151        mpfi 3.94
## 126        2756        ohcf              six         194        mpfi 3.74
## 127        2756        ohcf              six         194        mpfi 3.74
## 128        2800        ohcf              six         194        mpfi 3.74
## 132        2658         ohc             four         121        mpfi 3.54
## 133        2695         ohc             four         121        mpfi 3.54
## 134        2707         ohc             four         121        mpfi 2.54
## 135        2758         ohc             four         121        mpfi 3.54
## 136        2808        dohc             four         121        mpfi 3.54
## 137        2847        dohc             four         121        mpfi 3.54
## 138        2050        ohcf             four          97        2bbl 3.62
## 139        2120        ohcf             four         108        2bbl 3.62
## 140        2240        ohcf             four         108        2bbl 3.62
## 141        2145        ohcf             four         108        2bbl 3.62
## 142        2190        ohcf             four         108        2bbl 3.62
## 143        2340        ohcf             four         108        mpfi 3.62
## 144        2385        ohcf             four         108        2bbl 3.62
## 145        2510        ohcf             four         108        mpfi 3.62
## 146        2290        ohcf             four         108        2bbl 3.62
## 147        2455        ohcf             four         108        mpfi 3.62
## 148        2420        ohcf             four         108        2bbl 3.62
## 149        2650        ohcf             four         108        mpfi 3.62
## 150        1985         ohc             four          92        2bbl 3.05
## 151        2040         ohc             four          92        2bbl 3.05
## 152        2015         ohc             four          92        2bbl 3.05
## 153        2280         ohc             four          92        2bbl 3.05
## 154        2290         ohc             four          92        2bbl 3.05
## 155        3110         ohc             four          92        2bbl 3.05
## 156        2081         ohc             four          98        2bbl 3.19
## 157        2109         ohc             four          98        2bbl 3.19
## 158        2275         ohc             four         110         idi 3.27
## 159        2275         ohc             four         110         idi 3.27
## 160        2094         ohc             four          98        2bbl 3.19
## 161        2122         ohc             four          98        2bbl 3.19
## 162        2140         ohc             four          98        2bbl 3.19
## 163        2169         ohc             four          98        2bbl 3.19
## 164        2204         ohc             four          98        2bbl 3.19
## 165        2265        dohc             four          98        mpfi 3.24
## 166        2300        dohc             four          98        mpfi 3.24
## 167        2540         ohc             four         146        mpfi 3.62
## 168        2536         ohc             four         146        mpfi 3.62
## 169        2551         ohc             four         146        mpfi 3.62
## 170        2679         ohc             four         146        mpfi 3.62
## 171        2714         ohc             four         146        mpfi 3.62
## 172        2975         ohc             four         146        mpfi 3.62
## 173        2326         ohc             four         122        mpfi 3.31
## 174        2480         ohc             four         110         idi 3.27
## 175        2414         ohc             four         122        mpfi 3.31
## 176        2414         ohc             four         122        mpfi 3.31
## 177        2458         ohc             four         122        mpfi 3.31
## 178        2976        dohc              six         171        mpfi 3.27
## 179        3016        dohc              six         171        mpfi 3.27
## 180        3131        dohc              six         171        mpfi 3.27
## 181        3151        dohc              six         161        mpfi 3.27
## 182        2261         ohc             four          97         idi 3.01
## 183        2209         ohc             four         109        mpfi 3.19
## 184        2264         ohc             four          97         idi 3.01
## 185        2212         ohc             four         109        mpfi 3.19
## 186        2275         ohc             four         109        mpfi 3.19
## 187        2319         ohc             four          97         idi 3.01
## 188        2300         ohc             four         109        mpfi 3.19
## 189        2254         ohc             four         109        mpfi 3.19
## 190        2221         ohc             four         109        mpfi 3.19
## 191        2661         ohc             five         136        mpfi 3.19
## 192        2579         ohc             four          97         idi 3.01
## 193        2563         ohc             four         109        mpfi 3.19
## 194        2912         ohc             four         141        mpfi 3.78
## 195        3034         ohc             four         141        mpfi 3.78
## 196        2935         ohc             four         141        mpfi 3.78
## 197        3042         ohc             four         141        mpfi 3.78
## 198        3045         ohc             four         130        mpfi 3.62
## 199        3157         ohc             four         130        mpfi 3.62
## 200        2952         ohc             four         141        mpfi 3.78
## 201        3049         ohc             four         141        mpfi 3.78
## 202        3012        ohcv              six         173        mpfi 3.58
## 203        3217         ohc              six         145         idi 3.01
## 204        3062         ohc             four         141        mpfi 3.78
##     stroke compression.ratio horsepower peak.rpm city.mpg highway.mpg price
## 1     2.68              9.00        111     5000       21          27 16500
## 2     3.47              9.00        154     5000       19          26 16500
## 3     3.40             10.00        102     5500       24          30 13950
## 4     3.40              8.00        115     5500       18          22 17450
## 5     3.40              8.50        110     5500       19          25 15250
## 6     3.40              8.50        110     5500       19          25 17710
## 7     3.40              8.50        110     5500       19          25 18920
## 8     3.40              8.30        140     5500       17          20 23875
## 10    2.80              8.80        101     5800       23          29 16430
## 11    2.80              8.80        101     5800       23          29 16925
## 12    3.19              9.00        121     4250       21          28 20970
## 13    3.19              9.00        121     4250       21          28 21105
## 14    3.19              9.00        121     4250       20          25 24565
## 15    3.39              8.00        182     5400       16          22 30760
## 16    3.39              8.00        182     5400       16          22 41315
## 17    3.39              8.00        182     5400       15          20 36880
## 18    3.03              9.50         48     5100       47          53  5151
## 19    3.11              9.60         70     5400       38          43  6295
## 20    3.11              9.60         70     5400       38          43  6575
## 21    3.23              9.41         68     5500       37          41  5572
## 22    3.23              9.40         68     5500       31          38  6377
## 23    3.39              7.60        102     5500       24          30  7957
## 24    3.23              9.40         68     5500       31          38  6229
## 25    3.23              9.40         68     5500       31          38  6692
## 26    3.23              9.40         68     5500       31          38  7609
## 28    3.46              8.50         88     5000       24          30  8921
## 29    3.90              7.00        145     5000       19          24 12964
## 30    3.41              9.60         58     4800       49          54  6479
## 31    3.41              9.20         76     6000       31          38  6855
## 32    3.07             10.10         60     5500       38          42  5399
## 33    3.41              9.20         76     6000       30          34  6529
## 34    3.41              9.20         76     6000       30          34  7129
## 35    3.41              9.20         76     6000       30          34  7295
## 36    3.41              9.20         76     6000       30          34  7295
## 37    3.58              9.00         86     5800       27          33  7895
## 38    3.58              9.00         86     5800       27          33  9095
## 39    3.58              9.00         86     5800       27          33  8845
## 40    3.58              9.00         86     5800       27          33 10295
## 41    3.58              9.00        101     5800       24          28 12945
## 42    3.58              9.10        100     5500       25          31 10345
## 43    3.23              8.50         78     4800       24          29  6785
## 46    3.23              9.20         90     5000       24          29 11048
## 47    4.17              8.10        176     4750       15          19 32250
## 48    4.17              8.10        176     4750       15          19 35550
## 49    2.76             11.50        262     5000       13          17 36000
## 50    3.15              9.00         68     5000       30          31  5195
## 51    3.15              9.00         68     5000       31          38  6095
## 52    3.15              9.00         68     5000       31          38  6795
## 53    3.15              9.00         68     5000       31          38  6695
## 54    3.15              9.00         68     5000       31          38  7395
## 59    3.39              8.60         84     4800       26          32  8845
## 60    3.39              8.60         84     4800       26          32  8495
## 61    3.39              8.60         84     4800       26          32 10595
## 62    3.39              8.60         84     4800       26          32 10245
## 64    3.39              8.60         84     4800       26          32 11245
## 65    3.16              8.00        120     5000       19          27 18280
## 66    3.64             22.00         72     4200       31          39 18344
## 67    3.64             21.50        123     4350       22          25 25552
## 68    3.64             21.50        123     4350       22          25 28248
## 69    3.64             21.50        123     4350       22          25 28176
## 70    3.64             21.50        123     4350       22          25 31600
## 71    3.10              8.30        155     4750       16          18 34184
## 72    3.10              8.30        155     4750       16          18 35056
## 73    3.35              8.00        184     4500       14          16 40960
## 74    3.35              8.00        184     4500       14          16 45400
## 75    3.12              8.00        175     5000       19          24 16503
## 76    3.23              9.40         68     5500       37          41  5389
## 77    3.23              9.40         68     5500       31          38  6189
## 78    3.23              9.40         68     5500       31          38  6669
## 79    3.39              7.60        102     5500       24          30  7689
## 80    3.46              7.50        116     5500       23          30  9959
## 81    3.46              8.50         88     5000       25          32  8499
## 82    3.86              7.00        145     5000       19          24 12629
## 83    3.86              7.00        145     5000       19          24 14869
## 84    3.86              7.00        145     5000       19          24 14489
## 85    3.46              8.50         88     5000       25          32  6989
## 86    3.46              8.50         88     5000       25          32  8189
## 87    3.46              7.50        116     5500       23          30  9279
## 88    3.46              7.50        116     5500       23          30  9279
## 89    3.29              9.40         69     5200       31          37  5499
## 90    3.47             21.90         55     4800       45          50  7099
## 91    3.29              9.40         69     5200       31          37  6649
## 92    3.29              9.40         69     5200       31          37  6849
## 93    3.29              9.40         69     5200       31          37  7349
## 94    3.29              9.40         69     5200       31          37  7299
## 95    3.29              9.40         69     5200       31          37  7799
## 96    3.29              9.40         69     5200       31          37  7499
## 97    3.29              9.40         69     5200       31          37  7999
## 98    3.29              9.40         69     5200       31          37  8249
## 99    3.47              8.50         97     5200       27          34  8949
## 100   3.47              8.50         97     5200       27          34  9549
## 101   3.27              9.00        152     5200       17          22 13499
## 102   3.27              9.00        152     5200       17          22 14399
## 103   3.27              9.00        152     5200       19          25 13499
## 104   3.27              9.00        160     5200       19          25 17199
## 105   3.27              7.80        200     5200       17          23 19699
## 106   3.27              9.00        160     5200       19          25 18399
## 107   3.19              8.40         97     5000       19          24 11900
## 108   3.52             21.00         95     4150       28          33 13200
## 109   3.19              8.40         97     5000       19          24 12440
## 110   3.52             21.00         95     4150       25          25 13860
## 111   2.19              8.40         95     5000       19          24 15580
## 112   3.52             21.00         95     4150       28          33 16900
## 113   2.19              8.40         95     5000       19          24 16695
## 114   3.52             21.00         95     4150       25          25 17075
## 115   3.19              8.40         97     5000       19          24 16630
## 116   3.52             21.00         95     4150       28          33 17950
## 117   3.21              7.00        142     5600       18          24 18150
## 118   3.23              9.40         68     5500       37          41  5572
## 119   3.39              7.60        102     5500       24          30  7957
## 120   3.23              9.40         68     5500       31          38  6229
## 121   3.23              9.40         68     5500       31          38  6692
## 122   3.23              9.40         68     5500       31          38  7609
## 123   3.46              8.50         88     5000       24          30  8921
## 124   3.86              7.00        145     5000       19          24 12764
## 125   3.11              9.50        143     5500       19          27 22018
## 126   2.90              9.50        207     5900       17          25 32528
## 127   2.90              9.50        207     5900       17          25 34028
## 128   2.90              9.50        207     5900       17          25 37028
## 132   3.07              9.31        110     5250       21          28 11850
## 133   3.07              9.30        110     5250       21          28 12170
## 134   2.07              9.30        110     5250       21          28 15040
## 135   3.07              9.30        110     5250       21          28 15510
## 136   3.07              9.00        160     5500       19          26 18150
## 137   3.07              9.00        160     5500       19          26 18620
## 138   2.36              9.00         69     4900       31          36  5118
## 139   2.64              8.70         73     4400       26          31  7053
## 140   2.64              8.70         73     4400       26          31  7603
## 141   2.64              9.50         82     4800       32          37  7126
## 142   2.64              9.50         82     4400       28          33  7775
## 143   2.64              9.00         94     5200       26          32  9960
## 144   2.64              9.00         82     4800       24          25  9233
## 145   2.64              7.70        111     4800       24          29 11259
## 146   2.64              9.00         82     4800       28          32  7463
## 147   2.64              9.00         94     5200       25          31 10198
## 148   2.64              9.00         82     4800       23          29  8013
## 149   2.64              7.70        111     4800       23          23 11694
## 150   3.03              9.00         62     4800       35          39  5348
## 151   3.03              9.00         62     4800       31          38  6338
## 152   3.03              9.00         62     4800       31          38  6488
## 153   3.03              9.00         62     4800       31          37  6918
## 154   3.03              9.00         62     4800       27          32  7898
## 155   3.03              9.00         62     4800       27          32  8778
## 156   3.03              9.00         70     4800       30          37  6938
## 157   3.03              9.00         70     4800       30          37  7198
## 158   3.35             22.50         56     4500       34          36  7898
## 159   3.35             22.50         56     4500       38          47  7788
## 160   3.03              9.00         70     4800       38          47  7738
## 161   3.03              9.00         70     4800       28          34  8358
## 162   3.03              9.00         70     4800       28          34  9258
## 163   3.03              9.00         70     4800       29          34  8058
## 164   3.03              9.00         70     4800       29          34  8238
## 165   3.08              9.40        112     6600       26          29  9298
## 166   3.08              9.40        112     6600       26          29  9538
## 167   3.50              9.30        116     4800       24          30  8449
## 168   3.50              9.30        116     4800       24          30  9639
## 169   3.50              9.30        116     4800       24          30  9989
## 170   3.50              9.30        116     4800       24          30 11199
## 171   3.50              9.30        116     4800       24          30 11549
## 172   3.50              9.30        116     4800       24          30 17669
## 173   3.54              8.70         92     4200       29          34  8948
## 174   3.35             22.50         73     4500       30          33 10698
## 175   3.54              8.70         92     4200       27          32  9988
## 176   3.54              8.70         92     4200       27          32 10898
## 177   3.54              8.70         92     4200       27          32 11248
## 178   3.35              9.30        161     5200       20          24 16558
## 179   3.35              9.30        161     5200       19          24 15998
## 180   3.35              9.20        156     5200       20          24 15690
## 181   3.35              9.20        156     5200       19          24 15750
## 182   3.40             23.00         52     4800       37          46  7775
## 183   3.40              9.00         85     5250       27          34  7975
## 184   3.40             23.00         52     4800       37          46  7995
## 185   3.40              9.00         85     5250       27          34  8195
## 186   3.40              9.00         85     5250       27          34  8495
## 187   3.40             23.00         68     4500       37          42  9495
## 188   3.40             10.00        100     5500       26          32  9995
## 189   3.40              8.50         90     5500       24          29 11595
## 190   3.40              8.50         90     5500       24          29  9980
## 191   3.40              8.50        110     5500       19          24 13295
## 192   3.40             23.00         68     4500       33          38 13845
## 193   3.40              9.00         88     5500       25          31 12290
## 194   3.15              9.50        114     5400       23          28 12940
## 195   3.15              9.50        114     5400       23          28 13415
## 196   3.15              9.50        114     5400       24          28 15985
## 197   3.15              9.50        114     5400       24          28 16515
## 198   3.15              7.50        162     5100       17          22 18420
## 199   3.15              7.50        162     5100       17          22 18950
## 200   3.15              9.50        114     5400       23          28 16845
## 201   3.15              8.70        160     5300       19          25 19045
## 202   2.87              8.80        134     5500       18          23 21485
## 203   3.40             23.00        106     4800       26          27 22470
## 204   3.15              9.50        114     5400       19          25 22625

5.3 Fill NA values in ‘normalized.losses’ with the mean grouped by ‘symboling’

data2$normalized.losses <- ave(data2$normalized.losses, data2$symboling, FUN = function(x) {
  ifelse(is.na(x), mean(x, na.rm = TRUE), x)
})
data2
##     symboling normalized.losses          make fuel.type aspiration num.of.doors
## 1           3          174.3846   alfa-romero       gas        std          two
## 2           1          128.1522   alfa-romero       gas        std          two
## 3           2          164.0000          audi       gas        std         four
## 4           2          164.0000          audi       gas        std         four
## 5           2          125.6897          audi       gas        std          two
## 6           1          158.0000          audi       gas        std         four
## 7           1          128.1522          audi       gas        std         four
## 8           1          158.0000          audi       gas      turbo         four
## 10          2          192.0000           bmw       gas        std          two
## 11          0          192.0000           bmw       gas        std         four
## 12          0          188.0000           bmw       gas        std          two
## 13          0          188.0000           bmw       gas        std         four
## 14          1          128.1522           bmw       gas        std         four
## 15          0          113.1667           bmw       gas        std         four
## 16          0          113.1667           bmw       gas        std          two
## 17          0          113.1667           bmw       gas        std         four
## 18          2          121.0000     chevrolet       gas        std          two
## 19          1           98.0000     chevrolet       gas        std          two
## 20          0           81.0000     chevrolet       gas        std         four
## 21          1          118.0000         dodge       gas        std          two
## 22          1          118.0000         dodge       gas        std          two
## 23          1          118.0000         dodge       gas      turbo          two
## 24          1          148.0000         dodge       gas        std         four
## 25          1          148.0000         dodge       gas        std         four
## 26          1          148.0000         dodge       gas        std         four
## 28         -1          110.0000         dodge       gas        std         four
## 29          3          145.0000         dodge       gas      turbo          two
## 30          2          137.0000         honda       gas        std          two
## 31          2          137.0000         honda       gas        std          two
## 32          1          101.0000         honda       gas        std          two
## 33          1          101.0000         honda       gas        std          two
## 34          1          101.0000         honda       gas        std          two
## 35          0          110.0000         honda       gas        std         four
## 36          0           78.0000         honda       gas        std         four
## 37          0          106.0000         honda       gas        std          two
## 38          0          106.0000         honda       gas        std          two
## 39          0           85.0000         honda       gas        std         four
## 40          0           85.0000         honda       gas        std         four
## 41          0           85.0000         honda       gas        std         four
## 42          1          107.0000         honda       gas        std          two
## 43          0          113.1667         isuzu       gas        std         four
## 46          2          125.6897         isuzu       gas        std          two
## 47          0          145.0000        jaguar       gas        std         four
## 48          0          113.1667        jaguar       gas        std         four
## 49          0          113.1667        jaguar       gas        std          two
## 50          1          104.0000         mazda       gas        std          two
## 51          1          104.0000         mazda       gas        std          two
## 52          1          104.0000         mazda       gas        std          two
## 53          1          113.0000         mazda       gas        std         four
## 54          1          113.0000         mazda       gas        std         four
## 59          1          129.0000         mazda       gas        std          two
## 60          0          115.0000         mazda       gas        std         four
## 61          1          129.0000         mazda       gas        std          two
## 62          0          115.0000         mazda       gas        std         four
## 64          0          115.0000         mazda       gas        std         four
## 65          0          118.0000         mazda       gas        std         four
## 66          0          113.1667         mazda    diesel        std         four
## 67         -1           93.0000 mercedes-benz    diesel      turbo         four
## 68         -1           93.0000 mercedes-benz    diesel      turbo         four
## 69          0           93.0000 mercedes-benz    diesel      turbo          two
## 70         -1           93.0000 mercedes-benz    diesel      turbo         four
## 71         -1           85.6000 mercedes-benz       gas        std         four
## 72          3          142.0000 mercedes-benz       gas        std          two
## 73          0          113.1667 mercedes-benz       gas        std         four
## 74          1          128.1522 mercedes-benz       gas        std          two
## 75          1          128.1522       mercury       gas      turbo          two
## 76          2          161.0000    mitsubishi       gas        std          two
## 77          2          161.0000    mitsubishi       gas        std          two
## 78          2          161.0000    mitsubishi       gas        std          two
## 79          1          161.0000    mitsubishi       gas      turbo          two
## 80          3          153.0000    mitsubishi       gas      turbo          two
## 81          3          153.0000    mitsubishi       gas        std          two
## 82          3          174.3846    mitsubishi       gas      turbo          two
## 83          3          174.3846    mitsubishi       gas      turbo          two
## 84          3          174.3846    mitsubishi       gas      turbo          two
## 85          1          125.0000    mitsubishi       gas        std         four
## 86          1          125.0000    mitsubishi       gas        std         four
## 87          1          125.0000    mitsubishi       gas      turbo         four
## 88         -1          137.0000    mitsubishi       gas        std         four
## 89          1          128.0000        nissan       gas        std          two
## 90          1          128.0000        nissan    diesel        std          two
## 91          1          128.0000        nissan       gas        std          two
## 92          1          122.0000        nissan       gas        std         four
## 93          1          103.0000        nissan       gas        std         four
## 94          1          128.0000        nissan       gas        std          two
## 95          1          128.0000        nissan       gas        std          two
## 96          1          122.0000        nissan       gas        std         four
## 97          1          103.0000        nissan       gas        std         four
## 98          2          168.0000        nissan       gas        std          two
## 99          0          106.0000        nissan       gas        std         four
## 100         0          106.0000        nissan       gas        std         four
## 101         0          128.0000        nissan       gas        std         four
## 102         0          108.0000        nissan       gas        std         four
## 103         0          108.0000        nissan       gas        std         four
## 104         3          194.0000        nissan       gas        std          two
## 105         3          194.0000        nissan       gas      turbo          two
## 106         1          231.0000        nissan       gas        std          two
## 107         0          161.0000        peugot       gas        std         four
## 108         0          161.0000        peugot    diesel      turbo         four
## 109         0          113.1667        peugot       gas        std         four
## 110         0          113.1667        peugot    diesel      turbo         four
## 111         0          161.0000        peugot       gas        std         four
## 112         0          161.0000        peugot    diesel      turbo         four
## 113         0          113.1667        peugot       gas        std         four
## 114         0          113.1667        peugot    diesel      turbo         four
## 115         0          161.0000        peugot       gas        std         four
## 116         0          161.0000        peugot    diesel      turbo         four
## 117         0          161.0000        peugot       gas      turbo         four
## 118         1          119.0000      plymouth       gas        std          two
## 119         1          119.0000      plymouth       gas      turbo          two
## 120         1          154.0000      plymouth       gas        std         four
## 121         1          154.0000      plymouth       gas        std         four
## 122         1          154.0000      plymouth       gas        std         four
## 123        -1           74.0000      plymouth       gas        std         four
## 124         3          174.3846      plymouth       gas      turbo          two
## 125         3          186.0000       porsche       gas        std          two
## 126         3          174.3846       porsche       gas        std          two
## 127         3          174.3846       porsche       gas        std          two
## 128         3          174.3846       porsche       gas        std          two
## 132         3          150.0000          saab       gas        std          two
## 133         2          104.0000          saab       gas        std         four
## 134         3          150.0000          saab       gas        std          two
## 135         2          104.0000          saab       gas        std         four
## 136         3          150.0000          saab       gas      turbo          two
## 137         2          104.0000          saab       gas      turbo         four
## 138         2           83.0000        subaru       gas        std          two
## 139         2           83.0000        subaru       gas        std          two
## 140         2           83.0000        subaru       gas        std          two
## 141         0          102.0000        subaru       gas        std         four
## 142         0          102.0000        subaru       gas        std         four
## 143         0          102.0000        subaru       gas        std         four
## 144         0          102.0000        subaru       gas        std         four
## 145         0          102.0000        subaru       gas      turbo         four
## 146         0           89.0000        subaru       gas        std         four
## 147         0           89.0000        subaru       gas        std         four
## 148         0           85.0000        subaru       gas        std         four
## 149         0           85.0000        subaru       gas      turbo         four
## 150         1           87.0000        toyota       gas        std          two
## 151         1           87.0000        toyota       gas        std          two
## 152         1           74.0000        toyota       gas        std         four
## 153         0           77.0000        toyota       gas        std         four
## 154         0           81.0000        toyota       gas        std         four
## 155         0           91.0000        toyota       gas        std         four
## 156         0           91.0000        toyota       gas        std         four
## 157         0           91.0000        toyota       gas        std         four
## 158         0           91.0000        toyota    diesel        std         four
## 159         0           91.0000        toyota    diesel        std         four
## 160         0           91.0000        toyota       gas        std         four
## 161         0           91.0000        toyota       gas        std         four
## 162         0           91.0000        toyota       gas        std         four
## 163         1          168.0000        toyota       gas        std          two
## 164         1          168.0000        toyota       gas        std          two
## 165         1          168.0000        toyota       gas        std          two
## 166         1          168.0000        toyota       gas        std          two
## 167         2          134.0000        toyota       gas        std          two
## 168         2          134.0000        toyota       gas        std          two
## 169         2          134.0000        toyota       gas        std          two
## 170         2          134.0000        toyota       gas        std          two
## 171         2          134.0000        toyota       gas        std          two
## 172         2          134.0000        toyota       gas        std          two
## 173        -1           65.0000        toyota       gas        std         four
## 174        -1           65.0000        toyota    diesel      turbo         four
## 175        -1           65.0000        toyota       gas        std         four
## 176        -1           65.0000        toyota       gas        std         four
## 177        -1           65.0000        toyota       gas        std         four
## 178         3          197.0000        toyota       gas        std          two
## 179         3          197.0000        toyota       gas        std          two
## 180        -1           90.0000        toyota       gas        std         four
## 181        -1           85.6000        toyota       gas        std         four
## 182         2          122.0000    volkswagen    diesel        std          two
## 183         2          122.0000    volkswagen       gas        std          two
## 184         2           94.0000    volkswagen    diesel        std         four
## 185         2           94.0000    volkswagen       gas        std         four
## 186         2           94.0000    volkswagen       gas        std         four
## 187         2           94.0000    volkswagen    diesel      turbo         four
## 188         2           94.0000    volkswagen       gas        std         four
## 189         3          174.3846    volkswagen       gas        std          two
## 190         3          256.0000    volkswagen       gas        std          two
## 191         0          113.1667    volkswagen       gas        std         four
## 192         0          113.1667    volkswagen    diesel      turbo         four
## 193         0          113.1667    volkswagen       gas        std         four
## 194        -2          103.0000         volvo       gas        std         four
## 195        -1           74.0000         volvo       gas        std         four
## 196        -2          103.0000         volvo       gas        std         four
## 197        -1           74.0000         volvo       gas        std         four
## 198        -2          103.0000         volvo       gas      turbo         four
## 199        -1           74.0000         volvo       gas      turbo         four
## 200        -1           95.0000         volvo       gas        std         four
## 201        -1           95.0000         volvo       gas      turbo         four
## 202        -1           95.0000         volvo       gas        std         four
## 203        -1           95.0000         volvo    diesel      turbo         four
## 204        -1           95.0000         volvo       gas      turbo         four
##      body.style drive.wheels engine.location wheel.base length width height
## 1   convertible          rwd           front       88.6  168.8  64.1   48.8
## 2     hatchback          rwd           front       94.5  171.2  65.5   52.4
## 3         sedan          fwd           front       99.8  176.6  66.2   54.3
## 4         sedan          4wd           front       99.4  176.6  66.4   54.3
## 5         sedan          fwd           front       99.8  177.3  66.3   53.1
## 6         sedan          fwd           front      105.8  192.7  71.4   55.7
## 7         wagon          fwd           front      105.8  192.7  71.4   55.7
## 8         sedan          fwd           front      105.8  192.7  71.4   55.9
## 10        sedan          rwd           front      101.2  176.8  64.8   54.3
## 11        sedan          rwd           front      101.2  176.8  64.8   54.3
## 12        sedan          rwd           front      101.2  176.8  64.8   54.3
## 13        sedan          rwd           front      101.2  176.8  64.8   54.3
## 14        sedan          rwd           front      103.5  189.0  66.9   55.7
## 15        sedan          rwd           front      103.5  189.0  66.9   55.7
## 16        sedan          rwd           front      103.5  193.8  67.9   53.7
## 17        sedan          rwd           front      110.0  197.0  70.9   56.3
## 18    hatchback          fwd           front       88.4  141.1  60.3   53.2
## 19    hatchback          fwd           front       94.5  155.9  63.6   52.0
## 20        sedan          fwd           front       94.5  158.8  63.6   52.0
## 21    hatchback          fwd           front       93.7  157.3  63.8   50.8
## 22    hatchback          fwd           front       93.7  157.3  63.8   50.8
## 23    hatchback          fwd           front       93.7  157.3  63.8   50.8
## 24    hatchback          fwd           front       93.7  157.3  63.8   50.6
## 25        sedan          fwd           front       93.7  157.3  63.8   50.6
## 26        sedan          fwd           front       93.7  157.3  63.8   50.6
## 28        wagon          fwd           front      103.3  174.6  64.6   59.8
## 29    hatchback          fwd           front       95.9  173.2  66.3   50.2
## 30    hatchback          fwd           front       86.6  144.6  63.9   50.8
## 31    hatchback          fwd           front       86.6  144.6  63.9   50.8
## 32    hatchback          fwd           front       93.7  150.0  64.0   52.6
## 33    hatchback          fwd           front       93.7  150.0  64.0   52.6
## 34    hatchback          fwd           front       93.7  150.0  64.0   52.6
## 35        sedan          fwd           front       96.5  163.4  64.0   54.5
## 36        wagon          fwd           front       96.5  157.1  63.9   58.3
## 37    hatchback          fwd           front       96.5  167.5  65.2   53.3
## 38    hatchback          fwd           front       96.5  167.5  65.2   53.3
## 39        sedan          fwd           front       96.5  175.4  65.2   54.1
## 40        sedan          fwd           front       96.5  175.4  62.5   54.1
## 41        sedan          fwd           front       96.5  175.4  65.2   54.1
## 42        sedan          fwd           front       96.5  169.1  66.0   51.0
## 43        sedan          rwd           front       94.3  170.7  61.8   53.5
## 46    hatchback          rwd           front       96.0  172.6  65.2   51.4
## 47        sedan          rwd           front      113.0  199.6  69.6   52.8
## 48        sedan          rwd           front      113.0  199.6  69.6   52.8
## 49        sedan          rwd           front      102.0  191.7  70.6   47.8
## 50    hatchback          fwd           front       93.1  159.1  64.2   54.1
## 51    hatchback          fwd           front       93.1  159.1  64.2   54.1
## 52    hatchback          fwd           front       93.1  159.1  64.2   54.1
## 53        sedan          fwd           front       93.1  166.8  64.2   54.1
## 54        sedan          fwd           front       93.1  166.8  64.2   54.1
## 59    hatchback          fwd           front       98.8  177.8  66.5   53.7
## 60        sedan          fwd           front       98.8  177.8  66.5   55.5
## 61    hatchback          fwd           front       98.8  177.8  66.5   53.7
## 62        sedan          fwd           front       98.8  177.8  66.5   55.5
## 64    hatchback          fwd           front       98.8  177.8  66.5   55.5
## 65        sedan          rwd           front      104.9  175.0  66.1   54.4
## 66        sedan          rwd           front      104.9  175.0  66.1   54.4
## 67        sedan          rwd           front      110.0  190.9  70.3   56.5
## 68        wagon          rwd           front      110.0  190.9  70.3   58.7
## 69      hardtop          rwd           front      106.7  187.5  70.3   54.9
## 70        sedan          rwd           front      115.6  202.6  71.7   56.3
## 71        sedan          rwd           front      115.6  202.6  71.7   56.5
## 72  convertible          rwd           front       96.6  180.3  70.5   50.8
## 73        sedan          rwd           front      120.9  208.1  71.7   56.7
## 74      hardtop          rwd           front      112.0  199.2  72.0   55.4
## 75    hatchback          rwd           front      102.7  178.4  68.0   54.8
## 76    hatchback          fwd           front       93.7  157.3  64.4   50.8
## 77    hatchback          fwd           front       93.7  157.3  64.4   50.8
## 78    hatchback          fwd           front       93.7  157.3  64.4   50.8
## 79    hatchback          fwd           front       93.0  157.3  63.8   50.8
## 80    hatchback          fwd           front       96.3  173.0  65.4   49.4
## 81    hatchback          fwd           front       96.3  173.0  65.4   49.4
## 82    hatchback          fwd           front       95.9  173.2  66.3   50.2
## 83    hatchback          fwd           front       95.9  173.2  66.3   50.2
## 84    hatchback          fwd           front       95.9  173.2  66.3   50.2
## 85        sedan          fwd           front       96.3  172.4  65.4   51.6
## 86        sedan          fwd           front       96.3  172.4  65.4   51.6
## 87        sedan          fwd           front       96.3  172.4  65.4   51.6
## 88        sedan          fwd           front       96.3  172.4  65.4   51.6
## 89        sedan          fwd           front       94.5  165.3  63.8   54.5
## 90        sedan          fwd           front       94.5  165.3  63.8   54.5
## 91        sedan          fwd           front       94.5  165.3  63.8   54.5
## 92        sedan          fwd           front       94.5  165.3  63.8   54.5
## 93        wagon          fwd           front       94.5  170.2  63.8   53.5
## 94        sedan          fwd           front       94.5  165.3  63.8   54.5
## 95    hatchback          fwd           front       94.5  165.6  63.8   53.3
## 96        sedan          fwd           front       94.5  165.3  63.8   54.5
## 97        wagon          fwd           front       94.5  170.2  63.8   53.5
## 98      hardtop          fwd           front       95.1  162.4  63.8   53.3
## 99    hatchback          fwd           front       97.2  173.4  65.2   54.7
## 100       sedan          fwd           front       97.2  173.4  65.2   54.7
## 101       sedan          fwd           front      100.4  181.7  66.5   55.1
## 102       wagon          fwd           front      100.4  184.6  66.5   56.1
## 103       sedan          fwd           front      100.4  184.6  66.5   55.1
## 104   hatchback          rwd           front       91.3  170.7  67.9   49.7
## 105   hatchback          rwd           front       91.3  170.7  67.9   49.7
## 106   hatchback          rwd           front       99.2  178.5  67.9   49.7
## 107       sedan          rwd           front      107.9  186.7  68.4   56.7
## 108       sedan          rwd           front      107.9  186.7  68.4   56.7
## 109       wagon          rwd           front      114.2  198.9  68.4   58.7
## 110       wagon          rwd           front      114.2  198.9  68.4   58.7
## 111       sedan          rwd           front      107.9  186.7  68.4   56.7
## 112       sedan          rwd           front      107.9  186.7  68.4   56.7
## 113       wagon          rwd           front      114.2  198.9  68.4   56.7
## 114       wagon          rwd           front      114.2  198.9  68.4   58.7
## 115       sedan          rwd           front      107.9  186.7  68.4   56.7
## 116       sedan          rwd           front      107.9  186.7  68.4   56.7
## 117       sedan          rwd           front      108.0  186.7  68.3   56.0
## 118   hatchback          fwd           front       93.7  157.3  63.8   50.8
## 119   hatchback          fwd           front       93.7  157.3  63.8   50.8
## 120   hatchback          fwd           front       93.7  157.3  63.8   50.6
## 121       sedan          fwd           front       93.7  167.3  63.8   50.8
## 122       sedan          fwd           front       93.7  167.3  63.8   50.8
## 123       wagon          fwd           front      103.3  174.6  64.6   59.8
## 124   hatchback          rwd           front       95.9  173.2  66.3   50.2
## 125   hatchback          rwd           front       94.5  168.9  68.3   50.2
## 126     hardtop          rwd            rear       89.5  168.9  65.0   51.6
## 127     hardtop          rwd            rear       89.5  168.9  65.0   51.6
## 128 convertible          rwd            rear       89.5  168.9  65.0   51.6
## 132   hatchback          fwd           front       99.1  186.6  66.5   56.1
## 133       sedan          fwd           front       99.1  186.6  66.5   56.1
## 134   hatchback          fwd           front       99.1  186.6  66.5   56.1
## 135       sedan          fwd           front       99.1  186.6  66.5   56.1
## 136   hatchback          fwd           front       99.1  186.6  66.5   56.1
## 137       sedan          fwd           front       99.1  186.6  66.5   56.1
## 138   hatchback          fwd           front       93.7  156.9  63.4   53.7
## 139   hatchback          fwd           front       93.7  157.9  63.6   53.7
## 140   hatchback          4wd           front       93.3  157.3  63.8   55.7
## 141       sedan          fwd           front       97.2  172.0  65.4   52.5
## 142       sedan          fwd           front       97.2  172.0  65.4   52.5
## 143       sedan          fwd           front       97.2  172.0  65.4   52.5
## 144       sedan          4wd           front       97.0  172.0  65.4   54.3
## 145       sedan          4wd           front       97.0  172.0  65.4   54.3
## 146       wagon          fwd           front       97.0  173.5  65.4   53.0
## 147       wagon          fwd           front       97.0  173.5  65.4   53.0
## 148       wagon          4wd           front       96.9  173.6  65.4   54.9
## 149       wagon          4wd           front       96.9  173.6  65.4   54.9
## 150   hatchback          fwd           front       95.7  158.7  63.6   54.5
## 151   hatchback          fwd           front       95.7  158.7  63.6   54.5
## 152   hatchback          fwd           front       95.7  158.7  63.6   54.5
## 153       wagon          fwd           front       95.7  169.7  63.6   59.1
## 154       wagon          4wd           front       95.7  169.7  63.6   59.1
## 155       wagon          4wd           front       95.7  169.7  63.6   59.1
## 156       sedan          fwd           front       95.7  166.3  64.4   53.0
## 157   hatchback          fwd           front       95.7  166.3  64.4   52.8
## 158       sedan          fwd           front       95.7  166.3  64.4   53.0
## 159   hatchback          fwd           front       95.7  166.3  64.4   52.8
## 160       sedan          fwd           front       95.7  166.3  64.4   53.0
## 161   hatchback          fwd           front       95.7  166.3  64.4   52.8
## 162       sedan          fwd           front       95.7  166.3  64.4   52.8
## 163       sedan          rwd           front       94.5  168.7  64.0   52.6
## 164   hatchback          rwd           front       94.5  168.7  64.0   52.6
## 165       sedan          rwd           front       94.5  168.7  64.0   52.6
## 166   hatchback          rwd           front       94.5  168.7  64.0   52.6
## 167     hardtop          rwd           front       98.4  176.2  65.6   52.0
## 168     hardtop          rwd           front       98.4  176.2  65.6   52.0
## 169   hatchback          rwd           front       98.4  176.2  65.6   52.0
## 170     hardtop          rwd           front       98.4  176.2  65.6   52.0
## 171   hatchback          rwd           front       98.4  176.2  65.6   52.0
## 172 convertible          rwd           front       98.4  176.2  65.6   53.0
## 173       sedan          fwd           front      102.4  175.6  66.5   54.9
## 174       sedan          fwd           front      102.4  175.6  66.5   54.9
## 175   hatchback          fwd           front      102.4  175.6  66.5   53.9
## 176       sedan          fwd           front      102.4  175.6  66.5   54.9
## 177   hatchback          fwd           front      102.4  175.6  66.5   53.9
## 178   hatchback          rwd           front      102.9  183.5  67.7   52.0
## 179   hatchback          rwd           front      102.9  183.5  67.7   52.0
## 180       sedan          rwd           front      104.5  187.8  66.5   54.1
## 181       wagon          rwd           front      104.5  187.8  66.5   54.1
## 182       sedan          fwd           front       97.3  171.7  65.5   55.7
## 183       sedan          fwd           front       97.3  171.7  65.5   55.7
## 184       sedan          fwd           front       97.3  171.7  65.5   55.7
## 185       sedan          fwd           front       97.3  171.7  65.5   55.7
## 186       sedan          fwd           front       97.3  171.7  65.5   55.7
## 187       sedan          fwd           front       97.3  171.7  65.5   55.7
## 188       sedan          fwd           front       97.3  171.7  65.5   55.7
## 189 convertible          fwd           front       94.5  159.3  64.2   55.6
## 190   hatchback          fwd           front       94.5  165.7  64.0   51.4
## 191       sedan          fwd           front      100.4  180.2  66.9   55.1
## 192       sedan          fwd           front      100.4  180.2  66.9   55.1
## 193       wagon          fwd           front      100.4  183.1  66.9   55.1
## 194       sedan          rwd           front      104.3  188.8  67.2   56.2
## 195       wagon          rwd           front      104.3  188.8  67.2   57.5
## 196       sedan          rwd           front      104.3  188.8  67.2   56.2
## 197       wagon          rwd           front      104.3  188.8  67.2   57.5
## 198       sedan          rwd           front      104.3  188.8  67.2   56.2
## 199       wagon          rwd           front      104.3  188.8  67.2   57.5
## 200       sedan          rwd           front      109.1  188.8  68.9   55.5
## 201       sedan          rwd           front      109.1  188.8  68.8   55.5
## 202       sedan          rwd           front      109.1  188.8  68.9   55.5
## 203       sedan          rwd           front      109.1  188.8  68.9   55.5
## 204       sedan          rwd           front      109.1  188.8  68.9   55.5
##     curb.weight engine.type num.of.cylinders engine.size fuel.system bore
## 1          2548        dohc             four         130        mpfi 3.47
## 2          2823        ohcv              six         152        mpfi 2.68
## 3          2337         ohc             four         109        mpfi 3.19
## 4          2824         ohc             five         136        mpfi 3.19
## 5          2507         ohc             five         136        mpfi 3.19
## 6          2844         ohc             five         136        mpfi 3.19
## 7          2954         ohc             five         136        mpfi 3.19
## 8          3086         ohc             five         131        mpfi 3.13
## 10         2395         ohc             four         108        mpfi 3.50
## 11         2395         ohc             four         108        mpfi 3.50
## 12         2710         ohc              six         164        mpfi 3.31
## 13         2765         ohc              six         164        mpfi 3.31
## 14         3055         ohc              six         164        mpfi 3.31
## 15         3230         ohc              six         209        mpfi 3.62
## 16         3380         ohc              six         209        mpfi 3.62
## 17         3505         ohc              six         209        mpfi 3.62
## 18         1488           l            three          61        2bbl 2.91
## 19         1874         ohc             four          90        2bbl 3.03
## 20         1909         ohc             four          90        2bbl 3.03
## 21         1876         ohc             four          90        2bbl 2.97
## 22         1876         ohc             four          90        2bbl 2.97
## 23         2128         ohc             four          98        mpfi 3.03
## 24         1967         ohc             four          90        2bbl 2.97
## 25         1989         ohc             four          90        2bbl 2.97
## 26         1989         ohc             four          90        2bbl 2.97
## 28         2535         ohc             four         122        2bbl 3.34
## 29         2811         ohc             four         156         mfi 3.60
## 30         1713         ohc             four          92        1bbl 2.91
## 31         1819         ohc             four          92        1bbl 2.91
## 32         1837         ohc             four          79        1bbl 2.91
## 33         1940         ohc             four          92        1bbl 2.91
## 34         1956         ohc             four          92        1bbl 2.91
## 35         2010         ohc             four          92        1bbl 2.91
## 36         2024         ohc             four          92        1bbl 2.92
## 37         2236         ohc             four         110        1bbl 3.15
## 38         2289         ohc             four         110        1bbl 3.15
## 39         2304         ohc             four         110        1bbl 3.15
## 40         2372         ohc             four         110        1bbl 3.15
## 41         2465         ohc             four         110        mpfi 3.15
## 42         2293         ohc             four         110        2bbl 3.15
## 43         2337         ohc             four         111        2bbl 3.31
## 46         2734         ohc             four         119        spfi 3.43
## 47         4066        dohc              six         258        mpfi 3.63
## 48         4066        dohc              six         258        mpfi 3.63
## 49         3950        ohcv           twelve         326        mpfi 3.54
## 50         1890         ohc             four          91        2bbl 3.03
## 51         1900         ohc             four          91        2bbl 3.03
## 52         1905         ohc             four          91        2bbl 3.03
## 53         1945         ohc             four          91        2bbl 3.03
## 54         1950         ohc             four          91        2bbl 3.08
## 59         2385         ohc             four         122        2bbl 3.39
## 60         2410         ohc             four         122        2bbl 3.39
## 61         2385         ohc             four         122        2bbl 3.39
## 62         2410         ohc             four         122        2bbl 3.39
## 64         2425         ohc             four         122        2bbl 3.39
## 65         2670         ohc             four         140        mpfi 3.76
## 66         2700         ohc             four         134         idi 3.43
## 67         3515         ohc             five         183         idi 3.58
## 68         3750         ohc             five         183         idi 3.58
## 69         3495         ohc             five         183         idi 3.58
## 70         3770         ohc             five         183         idi 3.58
## 71         3740        ohcv            eight         234        mpfi 3.46
## 72         3685        ohcv            eight         234        mpfi 3.46
## 73         3900        ohcv            eight         308        mpfi 3.80
## 74         3715        ohcv            eight         304        mpfi 3.80
## 75         2910         ohc             four         140        mpfi 3.78
## 76         1918         ohc             four          92        2bbl 2.97
## 77         1944         ohc             four          92        2bbl 2.97
## 78         2004         ohc             four          92        2bbl 2.97
## 79         2145         ohc             four          98        spdi 3.03
## 80         2370         ohc             four         110        spdi 3.17
## 81         2328         ohc             four         122        2bbl 3.35
## 82         2833         ohc             four         156        spdi 3.58
## 83         2921         ohc             four         156        spdi 3.59
## 84         2926         ohc             four         156        spdi 3.59
## 85         2365         ohc             four         122        2bbl 3.35
## 86         2405         ohc             four         122        2bbl 3.35
## 87         2403         ohc             four         110        spdi 3.17
## 88         2403         ohc             four         110        spdi 3.17
## 89         1889         ohc             four          97        2bbl 3.15
## 90         2017         ohc             four         103         idi 2.99
## 91         1918         ohc             four          97        2bbl 3.15
## 92         1938         ohc             four          97        2bbl 3.15
## 93         2024         ohc             four          97        2bbl 3.15
## 94         1951         ohc             four          97        2bbl 3.15
## 95         2028         ohc             four          97        2bbl 3.15
## 96         1971         ohc             four          97        2bbl 3.15
## 97         2037         ohc             four          97        2bbl 3.15
## 98         2008         ohc             four          97        2bbl 3.15
## 99         2324         ohc             four         120        2bbl 3.33
## 100        2302         ohc             four         120        2bbl 3.33
## 101        3095        ohcv              six         181        mpfi 3.43
## 102        3296        ohcv              six         181        mpfi 3.43
## 103        3060        ohcv              six         181        mpfi 3.43
## 104        3071        ohcv              six         181        mpfi 3.43
## 105        3139        ohcv              six         181        mpfi 3.43
## 106        3139        ohcv              six         181        mpfi 3.43
## 107        3020           l             four         120        mpfi 3.46
## 108        3197           l             four         152         idi 3.70
## 109        3230           l             four         120        mpfi 3.46
## 110        3430           l             four         152         idi 3.70
## 111        3075           l             four         120        mpfi 3.46
## 112        3252           l             four         152         idi 3.70
## 113        3285           l             four         120        mpfi 3.46
## 114        3485           l             four         152         idi 3.70
## 115        3075           l             four         120        mpfi 3.46
## 116        3252           l             four         152         idi 3.70
## 117        3130           l             four         134        mpfi 3.61
## 118        1918         ohc             four          90        2bbl 2.97
## 119        2128         ohc             four          98        spdi 3.03
## 120        1967         ohc             four          90        2bbl 2.97
## 121        1989         ohc             four          90        2bbl 2.97
## 122        2191         ohc             four          98        2bbl 2.97
## 123        2535         ohc             four         122        2bbl 3.35
## 124        2818         ohc             four         156        spdi 3.59
## 125        2778         ohc             four         151        mpfi 3.94
## 126        2756        ohcf              six         194        mpfi 3.74
## 127        2756        ohcf              six         194        mpfi 3.74
## 128        2800        ohcf              six         194        mpfi 3.74
## 132        2658         ohc             four         121        mpfi 3.54
## 133        2695         ohc             four         121        mpfi 3.54
## 134        2707         ohc             four         121        mpfi 2.54
## 135        2758         ohc             four         121        mpfi 3.54
## 136        2808        dohc             four         121        mpfi 3.54
## 137        2847        dohc             four         121        mpfi 3.54
## 138        2050        ohcf             four          97        2bbl 3.62
## 139        2120        ohcf             four         108        2bbl 3.62
## 140        2240        ohcf             four         108        2bbl 3.62
## 141        2145        ohcf             four         108        2bbl 3.62
## 142        2190        ohcf             four         108        2bbl 3.62
## 143        2340        ohcf             four         108        mpfi 3.62
## 144        2385        ohcf             four         108        2bbl 3.62
## 145        2510        ohcf             four         108        mpfi 3.62
## 146        2290        ohcf             four         108        2bbl 3.62
## 147        2455        ohcf             four         108        mpfi 3.62
## 148        2420        ohcf             four         108        2bbl 3.62
## 149        2650        ohcf             four         108        mpfi 3.62
## 150        1985         ohc             four          92        2bbl 3.05
## 151        2040         ohc             four          92        2bbl 3.05
## 152        2015         ohc             four          92        2bbl 3.05
## 153        2280         ohc             four          92        2bbl 3.05
## 154        2290         ohc             four          92        2bbl 3.05
## 155        3110         ohc             four          92        2bbl 3.05
## 156        2081         ohc             four          98        2bbl 3.19
## 157        2109         ohc             four          98        2bbl 3.19
## 158        2275         ohc             four         110         idi 3.27
## 159        2275         ohc             four         110         idi 3.27
## 160        2094         ohc             four          98        2bbl 3.19
## 161        2122         ohc             four          98        2bbl 3.19
## 162        2140         ohc             four          98        2bbl 3.19
## 163        2169         ohc             four          98        2bbl 3.19
## 164        2204         ohc             four          98        2bbl 3.19
## 165        2265        dohc             four          98        mpfi 3.24
## 166        2300        dohc             four          98        mpfi 3.24
## 167        2540         ohc             four         146        mpfi 3.62
## 168        2536         ohc             four         146        mpfi 3.62
## 169        2551         ohc             four         146        mpfi 3.62
## 170        2679         ohc             four         146        mpfi 3.62
## 171        2714         ohc             four         146        mpfi 3.62
## 172        2975         ohc             four         146        mpfi 3.62
## 173        2326         ohc             four         122        mpfi 3.31
## 174        2480         ohc             four         110         idi 3.27
## 175        2414         ohc             four         122        mpfi 3.31
## 176        2414         ohc             four         122        mpfi 3.31
## 177        2458         ohc             four         122        mpfi 3.31
## 178        2976        dohc              six         171        mpfi 3.27
## 179        3016        dohc              six         171        mpfi 3.27
## 180        3131        dohc              six         171        mpfi 3.27
## 181        3151        dohc              six         161        mpfi 3.27
## 182        2261         ohc             four          97         idi 3.01
## 183        2209         ohc             four         109        mpfi 3.19
## 184        2264         ohc             four          97         idi 3.01
## 185        2212         ohc             four         109        mpfi 3.19
## 186        2275         ohc             four         109        mpfi 3.19
## 187        2319         ohc             four          97         idi 3.01
## 188        2300         ohc             four         109        mpfi 3.19
## 189        2254         ohc             four         109        mpfi 3.19
## 190        2221         ohc             four         109        mpfi 3.19
## 191        2661         ohc             five         136        mpfi 3.19
## 192        2579         ohc             four          97         idi 3.01
## 193        2563         ohc             four         109        mpfi 3.19
## 194        2912         ohc             four         141        mpfi 3.78
## 195        3034         ohc             four         141        mpfi 3.78
## 196        2935         ohc             four         141        mpfi 3.78
## 197        3042         ohc             four         141        mpfi 3.78
## 198        3045         ohc             four         130        mpfi 3.62
## 199        3157         ohc             four         130        mpfi 3.62
## 200        2952         ohc             four         141        mpfi 3.78
## 201        3049         ohc             four         141        mpfi 3.78
## 202        3012        ohcv              six         173        mpfi 3.58
## 203        3217         ohc              six         145         idi 3.01
## 204        3062         ohc             four         141        mpfi 3.78
##     stroke compression.ratio horsepower peak.rpm city.mpg highway.mpg price
## 1     2.68              9.00        111     5000       21          27 16500
## 2     3.47              9.00        154     5000       19          26 16500
## 3     3.40             10.00        102     5500       24          30 13950
## 4     3.40              8.00        115     5500       18          22 17450
## 5     3.40              8.50        110     5500       19          25 15250
## 6     3.40              8.50        110     5500       19          25 17710
## 7     3.40              8.50        110     5500       19          25 18920
## 8     3.40              8.30        140     5500       17          20 23875
## 10    2.80              8.80        101     5800       23          29 16430
## 11    2.80              8.80        101     5800       23          29 16925
## 12    3.19              9.00        121     4250       21          28 20970
## 13    3.19              9.00        121     4250       21          28 21105
## 14    3.19              9.00        121     4250       20          25 24565
## 15    3.39              8.00        182     5400       16          22 30760
## 16    3.39              8.00        182     5400       16          22 41315
## 17    3.39              8.00        182     5400       15          20 36880
## 18    3.03              9.50         48     5100       47          53  5151
## 19    3.11              9.60         70     5400       38          43  6295
## 20    3.11              9.60         70     5400       38          43  6575
## 21    3.23              9.41         68     5500       37          41  5572
## 22    3.23              9.40         68     5500       31          38  6377
## 23    3.39              7.60        102     5500       24          30  7957
## 24    3.23              9.40         68     5500       31          38  6229
## 25    3.23              9.40         68     5500       31          38  6692
## 26    3.23              9.40         68     5500       31          38  7609
## 28    3.46              8.50         88     5000       24          30  8921
## 29    3.90              7.00        145     5000       19          24 12964
## 30    3.41              9.60         58     4800       49          54  6479
## 31    3.41              9.20         76     6000       31          38  6855
## 32    3.07             10.10         60     5500       38          42  5399
## 33    3.41              9.20         76     6000       30          34  6529
## 34    3.41              9.20         76     6000       30          34  7129
## 35    3.41              9.20         76     6000       30          34  7295
## 36    3.41              9.20         76     6000       30          34  7295
## 37    3.58              9.00         86     5800       27          33  7895
## 38    3.58              9.00         86     5800       27          33  9095
## 39    3.58              9.00         86     5800       27          33  8845
## 40    3.58              9.00         86     5800       27          33 10295
## 41    3.58              9.00        101     5800       24          28 12945
## 42    3.58              9.10        100     5500       25          31 10345
## 43    3.23              8.50         78     4800       24          29  6785
## 46    3.23              9.20         90     5000       24          29 11048
## 47    4.17              8.10        176     4750       15          19 32250
## 48    4.17              8.10        176     4750       15          19 35550
## 49    2.76             11.50        262     5000       13          17 36000
## 50    3.15              9.00         68     5000       30          31  5195
## 51    3.15              9.00         68     5000       31          38  6095
## 52    3.15              9.00         68     5000       31          38  6795
## 53    3.15              9.00         68     5000       31          38  6695
## 54    3.15              9.00         68     5000       31          38  7395
## 59    3.39              8.60         84     4800       26          32  8845
## 60    3.39              8.60         84     4800       26          32  8495
## 61    3.39              8.60         84     4800       26          32 10595
## 62    3.39              8.60         84     4800       26          32 10245
## 64    3.39              8.60         84     4800       26          32 11245
## 65    3.16              8.00        120     5000       19          27 18280
## 66    3.64             22.00         72     4200       31          39 18344
## 67    3.64             21.50        123     4350       22          25 25552
## 68    3.64             21.50        123     4350       22          25 28248
## 69    3.64             21.50        123     4350       22          25 28176
## 70    3.64             21.50        123     4350       22          25 31600
## 71    3.10              8.30        155     4750       16          18 34184
## 72    3.10              8.30        155     4750       16          18 35056
## 73    3.35              8.00        184     4500       14          16 40960
## 74    3.35              8.00        184     4500       14          16 45400
## 75    3.12              8.00        175     5000       19          24 16503
## 76    3.23              9.40         68     5500       37          41  5389
## 77    3.23              9.40         68     5500       31          38  6189
## 78    3.23              9.40         68     5500       31          38  6669
## 79    3.39              7.60        102     5500       24          30  7689
## 80    3.46              7.50        116     5500       23          30  9959
## 81    3.46              8.50         88     5000       25          32  8499
## 82    3.86              7.00        145     5000       19          24 12629
## 83    3.86              7.00        145     5000       19          24 14869
## 84    3.86              7.00        145     5000       19          24 14489
## 85    3.46              8.50         88     5000       25          32  6989
## 86    3.46              8.50         88     5000       25          32  8189
## 87    3.46              7.50        116     5500       23          30  9279
## 88    3.46              7.50        116     5500       23          30  9279
## 89    3.29              9.40         69     5200       31          37  5499
## 90    3.47             21.90         55     4800       45          50  7099
## 91    3.29              9.40         69     5200       31          37  6649
## 92    3.29              9.40         69     5200       31          37  6849
## 93    3.29              9.40         69     5200       31          37  7349
## 94    3.29              9.40         69     5200       31          37  7299
## 95    3.29              9.40         69     5200       31          37  7799
## 96    3.29              9.40         69     5200       31          37  7499
## 97    3.29              9.40         69     5200       31          37  7999
## 98    3.29              9.40         69     5200       31          37  8249
## 99    3.47              8.50         97     5200       27          34  8949
## 100   3.47              8.50         97     5200       27          34  9549
## 101   3.27              9.00        152     5200       17          22 13499
## 102   3.27              9.00        152     5200       17          22 14399
## 103   3.27              9.00        152     5200       19          25 13499
## 104   3.27              9.00        160     5200       19          25 17199
## 105   3.27              7.80        200     5200       17          23 19699
## 106   3.27              9.00        160     5200       19          25 18399
## 107   3.19              8.40         97     5000       19          24 11900
## 108   3.52             21.00         95     4150       28          33 13200
## 109   3.19              8.40         97     5000       19          24 12440
## 110   3.52             21.00         95     4150       25          25 13860
## 111   2.19              8.40         95     5000       19          24 15580
## 112   3.52             21.00         95     4150       28          33 16900
## 113   2.19              8.40         95     5000       19          24 16695
## 114   3.52             21.00         95     4150       25          25 17075
## 115   3.19              8.40         97     5000       19          24 16630
## 116   3.52             21.00         95     4150       28          33 17950
## 117   3.21              7.00        142     5600       18          24 18150
## 118   3.23              9.40         68     5500       37          41  5572
## 119   3.39              7.60        102     5500       24          30  7957
## 120   3.23              9.40         68     5500       31          38  6229
## 121   3.23              9.40         68     5500       31          38  6692
## 122   3.23              9.40         68     5500       31          38  7609
## 123   3.46              8.50         88     5000       24          30  8921
## 124   3.86              7.00        145     5000       19          24 12764
## 125   3.11              9.50        143     5500       19          27 22018
## 126   2.90              9.50        207     5900       17          25 32528
## 127   2.90              9.50        207     5900       17          25 34028
## 128   2.90              9.50        207     5900       17          25 37028
## 132   3.07              9.31        110     5250       21          28 11850
## 133   3.07              9.30        110     5250       21          28 12170
## 134   2.07              9.30        110     5250       21          28 15040
## 135   3.07              9.30        110     5250       21          28 15510
## 136   3.07              9.00        160     5500       19          26 18150
## 137   3.07              9.00        160     5500       19          26 18620
## 138   2.36              9.00         69     4900       31          36  5118
## 139   2.64              8.70         73     4400       26          31  7053
## 140   2.64              8.70         73     4400       26          31  7603
## 141   2.64              9.50         82     4800       32          37  7126
## 142   2.64              9.50         82     4400       28          33  7775
## 143   2.64              9.00         94     5200       26          32  9960
## 144   2.64              9.00         82     4800       24          25  9233
## 145   2.64              7.70        111     4800       24          29 11259
## 146   2.64              9.00         82     4800       28          32  7463
## 147   2.64              9.00         94     5200       25          31 10198
## 148   2.64              9.00         82     4800       23          29  8013
## 149   2.64              7.70        111     4800       23          23 11694
## 150   3.03              9.00         62     4800       35          39  5348
## 151   3.03              9.00         62     4800       31          38  6338
## 152   3.03              9.00         62     4800       31          38  6488
## 153   3.03              9.00         62     4800       31          37  6918
## 154   3.03              9.00         62     4800       27          32  7898
## 155   3.03              9.00         62     4800       27          32  8778
## 156   3.03              9.00         70     4800       30          37  6938
## 157   3.03              9.00         70     4800       30          37  7198
## 158   3.35             22.50         56     4500       34          36  7898
## 159   3.35             22.50         56     4500       38          47  7788
## 160   3.03              9.00         70     4800       38          47  7738
## 161   3.03              9.00         70     4800       28          34  8358
## 162   3.03              9.00         70     4800       28          34  9258
## 163   3.03              9.00         70     4800       29          34  8058
## 164   3.03              9.00         70     4800       29          34  8238
## 165   3.08              9.40        112     6600       26          29  9298
## 166   3.08              9.40        112     6600       26          29  9538
## 167   3.50              9.30        116     4800       24          30  8449
## 168   3.50              9.30        116     4800       24          30  9639
## 169   3.50              9.30        116     4800       24          30  9989
## 170   3.50              9.30        116     4800       24          30 11199
## 171   3.50              9.30        116     4800       24          30 11549
## 172   3.50              9.30        116     4800       24          30 17669
## 173   3.54              8.70         92     4200       29          34  8948
## 174   3.35             22.50         73     4500       30          33 10698
## 175   3.54              8.70         92     4200       27          32  9988
## 176   3.54              8.70         92     4200       27          32 10898
## 177   3.54              8.70         92     4200       27          32 11248
## 178   3.35              9.30        161     5200       20          24 16558
## 179   3.35              9.30        161     5200       19          24 15998
## 180   3.35              9.20        156     5200       20          24 15690
## 181   3.35              9.20        156     5200       19          24 15750
## 182   3.40             23.00         52     4800       37          46  7775
## 183   3.40              9.00         85     5250       27          34  7975
## 184   3.40             23.00         52     4800       37          46  7995
## 185   3.40              9.00         85     5250       27          34  8195
## 186   3.40              9.00         85     5250       27          34  8495
## 187   3.40             23.00         68     4500       37          42  9495
## 188   3.40             10.00        100     5500       26          32  9995
## 189   3.40              8.50         90     5500       24          29 11595
## 190   3.40              8.50         90     5500       24          29  9980
## 191   3.40              8.50        110     5500       19          24 13295
## 192   3.40             23.00         68     4500       33          38 13845
## 193   3.40              9.00         88     5500       25          31 12290
## 194   3.15              9.50        114     5400       23          28 12940
## 195   3.15              9.50        114     5400       23          28 13415
## 196   3.15              9.50        114     5400       24          28 15985
## 197   3.15              9.50        114     5400       24          28 16515
## 198   3.15              7.50        162     5100       17          22 18420
## 199   3.15              7.50        162     5100       17          22 18950
## 200   3.15              9.50        114     5400       23          28 16845
## 201   3.15              8.70        160     5300       19          25 19045
## 202   2.87              8.80        134     5500       18          23 21485
## 203   3.40             23.00        106     4800       26          27 22470
## 204   3.15              9.50        114     5400       19          25 22625

5.4 Converting Data Type

# Define the columns to convert to numeric
columns_to_convert <- c("bore", "stroke", "horsepower", "peak.rpm", "price")

# Loop through the columns and convert them to numeric
for (col in columns_to_convert) {
  data2[[col]] <- as.numeric(data2[[col]])
}

6 Exploratory Data Analysis (EDA)

# Filter numerical columns
numerical_columns <- data2 %>%
  select(where(is.numeric)) %>%
  names()

# Filter categorical columns
categorical_columns <- data2 %>%
  select(where(is.character)) %>%
  names()

# Print the filtered columns
print("Numerical columns:")
## [1] "Numerical columns:"
print(numerical_columns)
##  [1] "symboling"         "normalized.losses" "wheel.base"       
##  [4] "length"            "width"             "height"           
##  [7] "curb.weight"       "engine.size"       "bore"             
## [10] "stroke"            "compression.ratio" "horsepower"       
## [13] "peak.rpm"          "city.mpg"          "highway.mpg"      
## [16] "price"
print("Categorical columns:")
## [1] "Categorical columns:"
print(categorical_columns)
##  [1] "make"             "fuel.type"        "aspiration"       "num.of.doors"    
##  [5] "body.style"       "drive.wheels"     "engine.location"  "engine.type"     
##  [9] "num.of.cylinders" "fuel.system"

6.1 Univariate Analysis

6.1.1 Frequency Plots of Categorical Features

The majority classes for each categorical feature provide valuable insights into the popular features in the car market and user preferences. In the dataset, gas fuel type, standard aspiration, four doors, sedans, fwd drive.wheels, front engine location, ohc engine type, 4 cylinders, and mpfi fuel system are predominant. This analysis highlights the popularity of certain features like front engine location (98%) and gas fuel type (90%) features, which are the highest among the majority classes determined.

In terms of car make, Toyota is the most common car make, with the highest frequency count of 32, suggesting strong popularity or market presence. Nissan & Honda also show notable frequency counts of 18 and 13, respectively, indicating they are relatively common makes in the dataset. The remaining car makes have lower frequencies, which could imply less popularity or a smaller market share in this particular sample.

# Create a list to hold the plots
plots <- list()

# Loop through each categorical column and create a plot, then add it to the list
for (col in categorical_columns[-1]) {
  p <- ggplot(data2, aes_string(x = col)) +
    geom_bar() +
    geom_text(stat='count', aes(label=after_stat(count)), vjust=-0.5) +
    ggtitle(col) +
    theme_minimal() +
    xlab(NULL) +
    ylab(NULL) +
    ylim(0, max(table(data2[[col]])) * 1.3) +
    theme(axis.text.x = element_text(angle = 30, hjust = 1)) # Set y-axis limit slightly above maximum count
  plots[[col]] <- p
}
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Arrange all the plots into a grid
grid.arrange(grobs = plots, ncol = 3,
             top = textGrob("Univariate Analysis of Categorical Variables", gp = gpar(fontsize = 14)))

#For car make
ggplot(data2, aes_string(x = categorical_columns[1])) +
    geom_bar() +
    geom_text(stat='count', aes(label=after_stat(count)), vjust=-0.5) +
    ggtitle(categorical_columns[1]) +
    theme_minimal() +
    xlab(NULL) +
    ylab(NULL) +
    ylim(0, max(table(data2[[categorical_columns[1]]])) * 1.2) +  # Set y-axis limit slightly above maximum count
    theme(axis.text.x = element_text(angle = 45, hjust = 1))  # Rotate x-axis labels by 90 degrees

6.1.2 Histograms for Numerical Categories

Distribution peaks can help identify common values or ranges, which is useful for understanding the typical characteristics of cars in this dataset.

Right-skewed distributions were observed for normalized losses, curb weight, engine size, horsepower, and price. This indicates that while most cars fall within a typical range, there are a few outliers with significantly higher values. Features such as engine size, horsepower, curb weight, and price are key indicators of a car’s performance and market segment.

Meanwhile, symmetric distributions that were observed for height, city MPG, and highway MPG suggest a more uniform spread around a central value, indicating consistency within these features.

# Generate summary statistics using summary() function

plots <- list()

for (col in numerical_columns) {
  p <- ggplot(data2, aes(x = .data[[col]])) +
    geom_histogram(bins = 15, fill = 'blue', color = 'black', alpha = 0.7) +
    ggtitle(col) +
    labs(x = col, y = "Frequency") +
    xlab(NULL) +
    ylab(NULL) +
    theme_minimal()
  plots[[col]] <- p
}

# Arrange all the plots into a grid
grid.arrange(grobs = plots, ncol = 4,
             top = textGrob("Univariate Analysis of Numerical Variables", gp = gpar(fontsize = 14)))

6.2 Bivariate Analysis

6.2.1 Price vs Categorical Features

The box plots above reveal significant price differences associated with certain classes of categorical features.

Examples of categories associated with significantly lower prices include:

  1. Drive wheels with fwd and 4wd
  2. Three and four cylinders
  3. Engine type with ohc and ohcf

Fwd, four doors, and ohc engine type are not only associated with lower prices but also popular, indicating consumer preferences for affordability. This observation suggests a potential correlation between feature popularity and price points. However, without conducing formal statistical tests, it is challenging to conclusively determine significant differences in prices for the categories of other features based on graphical analysis alone.

In terms of car make, Mercedes-Benz, Jaguar, BMW and Porsche have higher median prices, suggesting their positions as premium options in the market. Meanwhile, Chevrolet, Dodge and Honda have lower median prices, indicating they are more affordable. The presence of outliers in certain brands indicates that some cars are priced significantly higher or lower than the typical range for those makes.

This analysis can help identify market positioning and pricing strategies of different car makes. It also highlights the variability in car prices within each make.

plots <- list()

for (col in categorical_columns[-1]) {
    p <- ggplot(data2, aes_string(x = col, y = 'price')) +
        geom_boxplot() +
        ggtitle(paste(col)) +
        theme_minimal() +
        theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
        labs(x = NULL, y = "Price")  # Update y-axis label
    plots[[col]] <- p
}

# Arrange all the plots into a grid
grid.arrange(grobs = plots, ncol = 3,
             top = textGrob("Bivariate Analysis of Price vs Categorical Variables", gp = gpar(fontsize = 14)))

ggplot(data2, aes_string(x = categorical_columns[1], y = 'price')) +
        geom_boxplot() +
        ggtitle(paste(categorical_columns[1])) +
        theme_minimal() +
        theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
        labs(x = NULL, y = "Price")  # Update y-axis label

6.2.2 Price vs Numerical Variables

Scatter plots provided offer a visual representation of the relationship between the price and various numerical features. Upon analysis of the plots, features such as wheel base, length, width, curb weight, engine size, and horsepower show a positive correlation with price. This indicates that larger, heavier, and more powerful cars are generally more expensive. Both city MPG and highway MPG show a negative correlation with price, suggesting that more fuel-efficient cars tend to be less expensive, potentially because high-performance cars are typically less fuel-efficient.

plots <- list()

# Bivariate analysis for numeric variables
for (col in numerical_columns) {
    p <- ggplot(data2, aes_string(x = col, y = 'price')) +
        geom_point() +
        ggtitle(paste(col)) +
        theme_minimal() +
        theme(axis.text.x = element_text(angle = 0, hjust = 1)) +
        theme(axis.text.y = element_text(angle = 0, hjust = 1)) +  #Rotate y-axis labels horizontally
        labs(x = NULL, y = NULL)
    plots[[col]] <- p}

# Arrange all the plots into a grid
grid.arrange(grobs = plots, ncol = 4,
             top = textGrob("Bivariate Analysis of Price vs Numerical Variables", gp = gpar(fontsize = 14)))

6.3 Correlation Analysis

Pearson’s Linear Correlation Test was conducted to test the linear relationship between all numerical features.

Engine Size, Curb Weight, Horsepower, Width, Length have strong positive correlations with price, indicating that they are key factors in determining the price of a car. Meanwhile, city MPG, and Highway MPG have strong negative correlations with price

Wheel Base and Bore have moderate positive correlations with price, indicating they are significant but not as strong as the primary predictors.

Normalized Losses, Stroke, Compression Ratio, Height, Symboling, Peak RPM have weak to very weak correlations with price, indicating they have minimal influence on the pricing of cars.

library(corrplot)
## corrplot 0.92 loaded
cor(data2[numerical_columns], method="pearson")
##                     symboling normalized.losses wheel.base      length
## symboling          1.00000000        0.58814292 -0.5291596 -0.36227088
## normalized.losses  0.58814292        1.00000000 -0.1563805 -0.03566146
## wheel.base        -0.52915959       -0.15638053  1.0000000  0.88239411
## length            -0.36227088       -0.03566146  0.8823941  1.00000000
## width             -0.24237878        0.04096101  0.8186494  0.85744361
## height            -0.50824126       -0.43721274  0.5837984  0.49220052
## curb.weight       -0.23279091        0.08644904  0.7877767  0.88308964
## engine.size       -0.06932669        0.15306926  0.5730134  0.68745892
## bore              -0.15092219        0.02900189  0.5045288  0.60828824
## stroke             0.00604524        0.07278378  0.1609373  0.11878302
## compression.ratio -0.17396693       -0.14827838  0.2516232  0.15550752
## horsepower         0.06821986        0.27745856  0.3816420  0.59047323
## peak.rpm           0.23184768        0.24858595 -0.3553639 -0.27681283
## city.mpg           0.02418975       -0.24175405 -0.5148726 -0.70492158
## highway.mpg        0.09179984       -0.18683917 -0.5814205 -0.73352164
## price             -0.08579408        0.16132156  0.5895412  0.69634695
##                         width      height curb.weight engine.size         bore
## symboling         -0.24237878 -0.50824126 -0.23279091 -0.06932669 -0.150922188
## normalized.losses  0.04096101 -0.43721274  0.08644904  0.15306926  0.029001887
## wheel.base         0.81864939  0.58379843  0.78777668  0.57301338  0.504528795
## length             0.85744361  0.49220052  0.88308964  0.68745892  0.608288238
## width              1.00000000  0.30550276  0.86913180  0.74147318  0.545260313
## height             0.30550276  1.00000000  0.30920311  0.02773305  0.190493617
## curb.weight        0.86913180  0.30920311  1.00000000  0.85720031  0.645582794
## engine.size        0.74147318  0.02773305  0.85720031  1.00000000  0.582135321
## bore               0.54526031  0.19049362  0.64558279  0.58213532  1.000000000
## stroke             0.18270681 -0.07586437  0.17661700  0.21679871 -0.060767868
## compression.ratio  0.18775525  0.25379732  0.16102691  0.02533041 -0.003404321
## horsepower         0.62361635 -0.08049284  0.76225979  0.84536946  0.572893037
## peak.rpm          -0.24903584 -0.26282579 -0.27858962 -0.21774587 -0.273415676
## city.mpg          -0.66214432 -0.12023538 -0.77879121 -0.71708350 -0.600687839
## highway.mpg       -0.70632311 -0.16812712 -0.81884222 -0.73800061 -0.608223920
## price              0.75615917  0.13815231  0.83537415  0.88877867  0.546601472
##                        stroke compression.ratio  horsepower     peak.rpm
## symboling          0.00604524      -0.173966933  0.06821986  0.231847683
## normalized.losses  0.07278378      -0.148278380  0.27745856  0.248585947
## wheel.base         0.16093732       0.251623187  0.38164204 -0.355363893
## length             0.11878302       0.155507520  0.59047323 -0.276812834
## width              0.18270681       0.187755250  0.62361635 -0.249035837
## height            -0.07586437       0.253797316 -0.08049284 -0.262825794
## curb.weight        0.17661700       0.161026911  0.76225979 -0.278589619
## engine.size        0.21679871       0.025330415  0.84536946 -0.217745867
## bore              -0.06076787      -0.003404321  0.57289304 -0.273415676
## stroke             1.00000000       0.198625944  0.10569755 -0.071044789
## compression.ratio  0.19862594       1.000000000 -0.20358465 -0.440209040
## horsepower         0.10569755      -0.203584653  1.00000000  0.101625949
## peak.rpm          -0.07104479      -0.440209040  0.10162595  1.000000000
## city.mpg          -0.03801297       0.314074011 -0.83400078 -0.061868727
## highway.mpg       -0.04595019       0.249087955 -0.81224170 -0.009039878
## price              0.09708210       0.074537955  0.81251116 -0.103818883
##                      city.mpg  highway.mpg       price
## symboling          0.02418975  0.091799842 -0.08579408
## normalized.losses -0.24175405 -0.186839173  0.16132156
## wheel.base        -0.51487263 -0.581420476  0.58954124
## length            -0.70492158 -0.733521635  0.69634695
## width             -0.66214432 -0.706323107  0.75615917
## height            -0.12023538 -0.168127120  0.13815231
## curb.weight       -0.77879121 -0.818842219  0.83537415
## engine.size       -0.71708350 -0.738000612  0.88877867
## bore              -0.60068784 -0.608223920  0.54660147
## stroke            -0.03801297 -0.045950191  0.09708210
## compression.ratio  0.31407401  0.249087955  0.07453795
## horsepower        -0.83400078 -0.812241702  0.81251116
## peak.rpm          -0.06186873 -0.009039878 -0.10381888
## city.mpg           1.00000000  0.971957937 -0.70737714
## highway.mpg        0.97195794  1.000000000 -0.71968527
## price             -0.70737714 -0.719685268  1.00000000
relation<-cor(data2[numerical_columns])

corrplot(relation, method = "color", 
         addCoef.col = "black", # Color of coefficient text
         number.cex = 0.5,      # Size of coefficient text
         tl.cex = 0.7)          # Size of variable names

7 Feature Engineering

7.1 Data Transformation - Form new ‘is-high-end’ column

# Calculate the value count of price
table(data2$price)
## 
##  5118  5151  5195  5348  5389  5399  5499  5572  6095  6189  6229  6295  6338 
##     1     1     1     1     1     1     1     2     1     1     2     1     1 
##  6377  6479  6488  6529  6575  6649  6669  6692  6695  6785  6795  6849  6855 
##     1     1     1     1     1     1     1     2     1     1     1     1     1 
##  6918  6938  6989  7053  7099  7126  7129  7198  7295  7299  7349  7395  7463 
##     1     1     1     1     1     1     1     1     2     1     1     1     1 
##  7499  7603  7609  7689  7738  7775  7788  7799  7895  7898  7957  7975  7995 
##     1     1     2     1     1     2     1     1     1     2     2     1     1 
##  7999  8013  8058  8189  8195  8238  8249  8358  8449  8495  8499  8778  8845 
##     1     1     1     1     1     1     1     1     1     2     1     1     2 
##  8921  8948  8949  9095  9233  9258  9279  9298  9495  9538  9549  9639  9959 
##     2     1     1     1     1     1     2     1     1     1     1     1     1 
##  9960  9980  9988  9989  9995 10198 10245 10295 10345 10595 10698 10898 11048 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
## 11199 11245 11248 11259 11549 11595 11694 11850 11900 12170 12290 12440 12629 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
## 12764 12940 12945 12964 13200 13295 13415 13499 13845 13860 13950 14399 14489 
##     1     1     1     1     1     1     1     2     1     1     1     1     1 
## 14869 15040 15250 15510 15580 15690 15750 15985 15998 16430 16500 16503 16515 
##     1     1     1     1     1     1     1     1     1     1     2     1     1 
## 16558 16630 16695 16845 16900 16925 17075 17199 17450 17669 17710 17950 18150 
##     1     1     1     1     1     1     1     1     1     1     1     1     2 
## 18280 18344 18399 18420 18620 18920 18950 19045 19699 20970 21105 21485 22018 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
## 22470 22625 23875 24565 25552 28176 28248 30760 31600 32250 32528 34028 34184 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
## 35056 35550 36000 36880 37028 40960 41315 45400 
##     1     1     1     1     1     1     1     1
# Check whether there is any null value in 'price' column.
NA_price = sum(is.na(data2$price))
cat('Number of null value :',NA_price, '\n')
## Number of null value : 0
# Display the percentiles of price
quantile(data2$price, probs = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1))
##     10%     20%     30%     40%     50%     60%     70%     80%     90%    100% 
##  6582.4  7295.8  7962.4  8931.8 10221.5 12553.4 15559.0 17625.2 22609.5 45400.0

From, the percentiles of the price the maximum price is $45400. To classify the high end group of cars, 75th percentile of the total price is used as an indicator for the classification.

price_75th_percentile <- quantile(data2$price, 0.75)

# Classify cars based on the 75th percentile
data2$is_high_end <- ifelse(data2$price > price_75th_percentile, 1, 0)

data2[c('price', 'is_high_end')]
##     price is_high_end
## 1   16500           0
## 2   16500           0
## 3   13950           0
## 4   17450           1
## 5   15250           0
## 6   17710           1
## 7   18920           1
## 8   23875           1
## 10  16430           0
## 11  16925           1
## 12  20970           1
## 13  21105           1
## 14  24565           1
## 15  30760           1
## 16  41315           1
## 17  36880           1
## 18   5151           0
## 19   6295           0
## 20   6575           0
## 21   5572           0
## 22   6377           0
## 23   7957           0
## 24   6229           0
## 25   6692           0
## 26   7609           0
## 28   8921           0
## 29  12964           0
## 30   6479           0
## 31   6855           0
## 32   5399           0
## 33   6529           0
## 34   7129           0
## 35   7295           0
## 36   7295           0
## 37   7895           0
## 38   9095           0
## 39   8845           0
## 40  10295           0
## 41  12945           0
## 42  10345           0
## 43   6785           0
## 46  11048           0
## 47  32250           1
## 48  35550           1
## 49  36000           1
## 50   5195           0
## 51   6095           0
## 52   6795           0
## 53   6695           0
## 54   7395           0
## 59   8845           0
## 60   8495           0
## 61  10595           0
## 62  10245           0
## 64  11245           0
## 65  18280           1
## 66  18344           1
## 67  25552           1
## 68  28248           1
## 69  28176           1
## 70  31600           1
## 71  34184           1
## 72  35056           1
## 73  40960           1
## 74  45400           1
## 75  16503           0
## 76   5389           0
## 77   6189           0
## 78   6669           0
## 79   7689           0
## 80   9959           0
## 81   8499           0
## 82  12629           0
## 83  14869           0
## 84  14489           0
## 85   6989           0
## 86   8189           0
## 87   9279           0
## 88   9279           0
## 89   5499           0
## 90   7099           0
## 91   6649           0
## 92   6849           0
## 93   7349           0
## 94   7299           0
## 95   7799           0
## 96   7499           0
## 97   7999           0
## 98   8249           0
## 99   8949           0
## 100  9549           0
## 101 13499           0
## 102 14399           0
## 103 13499           0
## 104 17199           1
## 105 19699           1
## 106 18399           1
## 107 11900           0
## 108 13200           0
## 109 12440           0
## 110 13860           0
## 111 15580           0
## 112 16900           1
## 113 16695           1
## 114 17075           1
## 115 16630           1
## 116 17950           1
## 117 18150           1
## 118  5572           0
## 119  7957           0
## 120  6229           0
## 121  6692           0
## 122  7609           0
## 123  8921           0
## 124 12764           0
## 125 22018           1
## 126 32528           1
## 127 34028           1
## 128 37028           1
## 132 11850           0
## 133 12170           0
## 134 15040           0
## 135 15510           0
## 136 18150           1
## 137 18620           1
## 138  5118           0
## 139  7053           0
## 140  7603           0
## 141  7126           0
## 142  7775           0
## 143  9960           0
## 144  9233           0
## 145 11259           0
## 146  7463           0
## 147 10198           0
## 148  8013           0
## 149 11694           0
## 150  5348           0
## 151  6338           0
## 152  6488           0
## 153  6918           0
## 154  7898           0
## 155  8778           0
## 156  6938           0
## 157  7198           0
## 158  7898           0
## 159  7788           0
## 160  7738           0
## 161  8358           0
## 162  9258           0
## 163  8058           0
## 164  8238           0
## 165  9298           0
## 166  9538           0
## 167  8449           0
## 168  9639           0
## 169  9989           0
## 170 11199           0
## 171 11549           0
## 172 17669           1
## 173  8948           0
## 174 10698           0
## 175  9988           0
## 176 10898           0
## 177 11248           0
## 178 16558           1
## 179 15998           0
## 180 15690           0
## 181 15750           0
## 182  7775           0
## 183  7975           0
## 184  7995           0
## 185  8195           0
## 186  8495           0
## 187  9495           0
## 188  9995           0
## 189 11595           0
## 190  9980           0
## 191 13295           0
## 192 13845           0
## 193 12290           0
## 194 12940           0
## 195 13415           0
## 196 15985           0
## 197 16515           0
## 198 18420           1
## 199 18950           1
## 200 16845           1
## 201 19045           1
## 202 21485           1
## 203 22470           1
## 204 22625           1

7.2 Data Preparation

library(caret)
## Loading required package: lattice
# Select features (exclude 'price' and 'is_high_end')
target <- data2$is_high_end

regressors <- names(data2)[!names(data2) %in% c('price','is_high_end')]
features <- data2[, regressors]

# Train test split
set.seed(123)
trainIndex <- createDataPartition(target, p = .8, list = FALSE)
data_train <- data2[trainIndex,]
data_test <- data2[-trainIndex,]

# Numerical feature normalization
num <- c('symboling', 'normalized.losses', 'length','width','height', 'horsepower', 'wheel.base',
         'bore', 'stroke', 'compression.ratio', 'peak.rpm','curb.weight','engine.size','city.mpg','highway.mpg')

preProcValues <- preProcess(data_train[, num], method = c("center", "scale"))
data_train[, num] <- predict(preProcValues, data_train[, num])
data_test[, num] <- predict(preProcValues, data_test[, num])

# Dummy variables for categorical data types
classes <- c('make', 'fuel.type', 'aspiration', 'num.of.doors', 
               'body.style', 'drive.wheels', 'engine.location',
               'engine.type', 'num.of.cylinders', 'fuel.system')

dummies_train <- model.matrix(~.-1, data = data_train[, classes])
dummies_test <- model.matrix(~.-1, data = data_test[, classes])

# Combine both features
data_train <- cbind(data_train[, num], dummies_train)
data_test <- cbind(data_test[, num], dummies_test)


# Make sure the training and test sets have the same columns
common_cols <- intersect(names(data_train), names(data_test))

data_train <- data_train[, common_cols]
data_test <- data_test[, common_cols]

# Renmae column name
colnames(data_train)[colnames(data_train) == "makemercedes-benz"] <- "makemercedes.benz"
colnames(data_test)[colnames(data_test) == "makemercedes-benz"] <- "makemercedes.benz"

# Add target variable
data_train$is_high_end <- target[trainIndex]
data_test$is_high_end <- target[-trainIndex]

data_train$is_high_end<- factor(data_train$is_high_end)
data_test$is_high_end<- factor(data_test$is_high_end)

x_test <- data_test[,-ncol(data_test)]
y_test <- data_test$is_high_end

8 Classifier Model

  1. Perform feature engineering by scaling numerical data and creating dummy variables from categorical data.
  2. Split the dataset into training and testing sets with an 80-20 ratio and fit the models.
  3. Use repeated K-fold cross-validation to evaluate the models due to the small dataset size. Compare and assess the performance of the models.
# Load required library
library(e1071)  # SVM model
library(randomForest) # RF model
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:gridExtra':
## 
##     combine
## The following object is masked from 'package:ggplot2':
## 
##     margin
## The following object is masked from 'package:dplyr':
## 
##     combine
# Create Support Vector Machine model
svm_model <- svm(is_high_end ~ ., data = data_train)
svm_pred <- predict(svm_model, newdata = x_test)


svm_accuracy <- mean(svm_pred == y_test)
svm_report <- confusionMatrix(svm_pred, y_test)
# Create and train the Random Forest model
rf_model <- randomForest(is_high_end ~ ., data = data_train, ntree = 20)
rf_pred <- predict(rf_model, newdata = x_test)

rf_accuracy <- mean(rf_pred == y_test)
rf_report <- confusionMatrix(rf_pred, y_test)
# Create and train the KNN model
knn_model <- train(is_high_end ~ ., data = data_train, method = "knn")
knn_pred <- predict(knn_model, newdata = x_test)

knn_accuracy <- mean(knn_pred == y_test)
knn_report <- confusionMatrix(knn_pred, y_test)
# Print results
print(svm_accuracy)
## [1] 0.8648649
print(svm_report)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 26  3
##          1  2  6
##                                           
##                Accuracy : 0.8649          
##                  95% CI : (0.7123, 0.9546)
##     No Information Rate : 0.7568          
##     P-Value [Acc > NIR] : 0.08439         
##                                           
##                   Kappa : 0.6186          
##                                           
##  Mcnemar's Test P-Value : 1.00000         
##                                           
##             Sensitivity : 0.9286          
##             Specificity : 0.6667          
##          Pos Pred Value : 0.8966          
##          Neg Pred Value : 0.7500          
##              Prevalence : 0.7568          
##          Detection Rate : 0.7027          
##    Detection Prevalence : 0.7838          
##       Balanced Accuracy : 0.7976          
##                                           
##        'Positive' Class : 0               
## 
print(rf_accuracy)
## [1] 0.9189189
print(rf_report)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 26  1
##          1  2  8
##                                          
##                Accuracy : 0.9189         
##                  95% CI : (0.7809, 0.983)
##     No Information Rate : 0.7568         
##     P-Value [Acc > NIR] : 0.01129        
##                                          
##                   Kappa : 0.7878         
##                                          
##  Mcnemar's Test P-Value : 1.00000        
##                                          
##             Sensitivity : 0.9286         
##             Specificity : 0.8889         
##          Pos Pred Value : 0.9630         
##          Neg Pred Value : 0.8000         
##              Prevalence : 0.7568         
##          Detection Rate : 0.7027         
##    Detection Prevalence : 0.7297         
##       Balanced Accuracy : 0.9087         
##                                          
##        'Positive' Class : 0              
## 
print(knn_accuracy)
## [1] 0.8918919
print(knn_report)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 26  2
##          1  2  7
##                                           
##                Accuracy : 0.8919          
##                  95% CI : (0.7458, 0.9697)
##     No Information Rate : 0.7568          
##     P-Value [Acc > NIR] : 0.0347          
##                                           
##                   Kappa : 0.7063          
##                                           
##  Mcnemar's Test P-Value : 1.0000          
##                                           
##             Sensitivity : 0.9286          
##             Specificity : 0.7778          
##          Pos Pred Value : 0.9286          
##          Neg Pred Value : 0.7778          
##              Prevalence : 0.7568          
##          Detection Rate : 0.7027          
##    Detection Prevalence : 0.7568          
##       Balanced Accuracy : 0.8532          
##                                           
##        'Positive' Class : 0               
## 

8.1 Cross Validation

The accuracy and kappa can be increase more by using cross validation. We are using repeated K fold

# CV for SVM
set.seed(50)

trctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3)

# Define the grid of hyperparameters to search
svm_radial_grid <- expand.grid(
  sigma = c(0.01, 0.1, 1),
  C = c(0.01, 0.1, 1, 10, 100)  #  cost parameter
)

svm_fit <- train(is_high_end ~ ., data = data_train, method = "svmRadial", trControl=trctrl,tuneGrid = svm_radial_grid)
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
plot(svm_fit) 

# SVM fit graph shows sigma = 0.01 and cost = 10 are the best parameter for tunning the SVM model.

8.2 CV for SVM model

library(kernlab)
## 
## Attaching package: 'kernlab'
## The following object is masked from 'package:ggplot2':
## 
##     alpha
#fit in the best param
svm_best_model <- ksvm(is_high_end ~ ., data = data_train, type = "C-svc", kernel = "rbfdot",
                       kpar = list(sigma = 0.01), C = 10)

cv_svm_pred <- predict(svm_best_model, newdata = x_test)

# Evaluate the model
cv_svm_accuracy <- mean(cv_svm_pred == y_test)
cv_svm_report <- confusionMatrix(cv_svm_pred, y_test)

8.3 CV for Random Forest

# Cv for random forest

rf_grid <- expand.grid(
  mtry = c(2,5,8,10,14,30)
)

rf_fit <- train(is_high_end ~ ., data = data_train, method = "rf", trControl=trctrl,tuneGrid = rf_grid)
plot(rf_fit) 

# Set up cross-validation
trctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3) 

# Fit the Random Forest model using the best mtry value
rf_best_fit <- train(is_high_end ~ ., data = data_train, method = "rf",
                     trControl = trctrl, tuneGrid = expand.grid(mtry = 10))

# Make predictions 
rf_best_pred <- predict(rf_best_fit, newdata = x_test)

# Evaluate the model
cv_rf_accuracy <- mean(rf_best_pred == y_test)
cv_rf_report <- confusionMatrix(rf_best_pred, y_test)

8.4 CV for KNN model

# Set up cross-validation
trctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3)  

# Define the grid of hyperparameters to search
knn_grid <- expand.grid(k = c(3, 5, 7, 9, 11, 13, 15)) 

# Train the k-NN model with grid search and cross-validation
knn_fit <- train(is_high_end ~ ., data = data_train, method = "knn",
                 trControl = trctrl, tuneGrid = knn_grid )
plot(knn_fit)

cv_knn_pred <- predict(knn_fit, newdata = x_test)

# Evaluate the model
cv_knn_accuracy <- mean(cv_knn_pred == y_test)
cv_knn_report <- confusionMatrix(cv_knn_pred, y_test)
# Print results

# Compare accuracy
cat("SVM before CV:",svm_accuracy,'\n')
## SVM before CV: 0.8648649
cat("SVM after CV:",cv_svm_accuracy,'\n')
## SVM after CV: 0.8918919
cat('\n')
cat("Random Forest before CV:",rf_accuracy,'\n')
## Random Forest before CV: 0.9189189
cat("Random Forest after CV:",cv_rf_accuracy,'\n')
## Random Forest after CV: 0.9189189
cat('\n')
cat("KNN before CV:",knn_accuracy,'\n')
## KNN before CV: 0.8918919
cat("KNN after CV:",cv_knn_accuracy,'\n')
## KNN after CV: 1
# Show CV confusion matrix result
print(cv_svm_report)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 26  2
##          1  2  7
##                                           
##                Accuracy : 0.8919          
##                  95% CI : (0.7458, 0.9697)
##     No Information Rate : 0.7568          
##     P-Value [Acc > NIR] : 0.0347          
##                                           
##                   Kappa : 0.7063          
##                                           
##  Mcnemar's Test P-Value : 1.0000          
##                                           
##             Sensitivity : 0.9286          
##             Specificity : 0.7778          
##          Pos Pred Value : 0.9286          
##          Neg Pred Value : 0.7778          
##              Prevalence : 0.7568          
##          Detection Rate : 0.7027          
##    Detection Prevalence : 0.7568          
##       Balanced Accuracy : 0.8532          
##                                           
##        'Positive' Class : 0               
## 
print(cv_rf_report)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 26  1
##          1  2  8
##                                          
##                Accuracy : 0.9189         
##                  95% CI : (0.7809, 0.983)
##     No Information Rate : 0.7568         
##     P-Value [Acc > NIR] : 0.01129        
##                                          
##                   Kappa : 0.7878         
##                                          
##  Mcnemar's Test P-Value : 1.00000        
##                                          
##             Sensitivity : 0.9286         
##             Specificity : 0.8889         
##          Pos Pred Value : 0.9630         
##          Neg Pred Value : 0.8000         
##              Prevalence : 0.7568         
##          Detection Rate : 0.7027         
##    Detection Prevalence : 0.7297         
##       Balanced Accuracy : 0.9087         
##                                          
##        'Positive' Class : 0              
## 
print(cv_knn_report)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 28  0
##          1  0  9
##                                      
##                Accuracy : 1          
##                  95% CI : (0.9051, 1)
##     No Information Rate : 0.7568     
##     P-Value [Acc > NIR] : 3.322e-05  
##                                      
##                   Kappa : 1          
##                                      
##  Mcnemar's Test P-Value : NA         
##                                      
##             Sensitivity : 1.0000     
##             Specificity : 1.0000     
##          Pos Pred Value : 1.0000     
##          Neg Pred Value : 1.0000     
##              Prevalence : 0.7568     
##          Detection Rate : 0.7568     
##    Detection Prevalence : 0.7568     
##       Balanced Accuracy : 1.0000     
##                                      
##        'Positive' Class : 0          
## 

9 Regression Model

Regression predicts a target attribute which is a number (as opposed to a category). Since the required libraries have been loaded and the missing values have been dealt with, we can proceed to do Linear Regression.

9.1 Linear Regression

  1. Linear Regression models the relationship between a dependent variable(‘price’) and one or more independent variables (‘bore’, ‘stroke’, ‘horsepower’, ‘peak.rpm’) by fitting a linear equation to observed data. In the code ‘lm’ function is used to perform linear regression on the ‘data2’ dataset.
  2. The ‘summary’ function provides a summary of the linear regression model, including coefficients, standards errors, t-statistics, and p-values.
  3. The ‘plot’ function visualizes the linear regression model by plotting the actual prices against the predicted prices and adding a regression linear

9.1.1 Load necessary libraries

library(ggplot2)
library(Metrics)
## 
## Attaching package: 'Metrics'
## The following objects are masked from 'package:caret':
## 
##     precision, recall

9.1.2 Linear regression

linear_model <- lm(price ~ bore + stroke + horsepower + peak.rpm + curb.weight + engine.size + compression.ratio, data = data2)

9.1.3 Summary of the linear regression model

summary(linear_model)
## 
## Call:
## lm(formula = price ~ bore + stroke + horsepower + peak.rpm + 
##     curb.weight + engine.size + compression.ratio, data = data2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12659.5  -1423.3   -159.7   1539.5  13322.4 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -1.686e+04  6.591e+03  -2.558 0.011327 *  
## bore              -7.944e+02  1.228e+03  -0.647 0.518325    
## stroke            -2.969e+03  8.105e+02  -3.663 0.000326 ***
## horsepower         2.655e+01  1.574e+01   1.686 0.093463 .  
## peak.rpm           2.251e+00  6.902e-01   3.261 0.001321 ** 
## curb.weight        3.849e+00  1.007e+00   3.820 0.000182 ***
## engine.size        1.234e+02  1.471e+01   8.387 1.28e-14 ***
## compression.ratio  2.514e+02  7.291e+01   3.448 0.000700 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3261 on 184 degrees of freedom
## Multiple R-squared:  0.8443, Adjusted R-squared:  0.8384 
## F-statistic: 142.5 on 7 and 184 DF,  p-value: < 2.2e-16

9.1.4 Predicted prices

predicted_prices <- predict(linear_model)

9.1.5 Create a data frame with actual and predicted prices

results <- data.frame(actual_price = data2$price, predicted_price = predicted_prices)

9.1.6 Determine the range of prices

price_range <- range(data2$price, predicted_prices)

9.1.7 Visualize the linear regression model

ggplot(results, aes(x = actual_price, y = predicted_price)) +
  geom_point(color = "blue") +  # Scatterplot of actual vs predicted price
  geom_abline(intercept = coef(linear_model)[1], slope = coef(linear_model)[2], color = "red") +  # Add regression line
  labs(title = "Linear Regression", x = "Actual Price", y = "Predicted Price") +  # Labels and title
  xlim(price_range) +  # Limit x-axis to the range of prices
  ylim(price_range)  # Limit y-axis to the range of prices

9.1.8 Assess price volatility and risk

ggplot(data2, aes(x = predicted_prices, fill = is_high_end)) +
  geom_density(alpha = 0.5) +
  ggtitle("Distribution of Predicted Prices by Risk Level") +
  xlab("Predicted Price") +
  ylab("Density")
## Warning: The following aesthetics were dropped during statistical transformation: fill.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

9.1.9 Results Interpretation

It’s already a good result: R squared of 0.83 and the regression line in the scatter plot is straight and aligned with the diagonal. This indicates that the model’s predictions are very close to the actual prices. The statistical metrics show low residual standard error, high R-squared values, and a significant F-statistic, it suggests that the model is performing well in predicting the prices. Furthermore, there is a strong linear relationship between the predicted and actual prices.

9.1.10 Evaluate the model

MAE and MSE is a metric to evaluate performance of regression models by measuring the average absolute difference between the actual and predicted values. In the code, the ‘MAE’ function from the ‘Metrics’ library is used to compute the MAE for the linear regression model.

9.1.11 First Trial: Making predictions on the test set

y_test <- data2$price
y_pred <- predict(linear_model, newdata = data2)

9.1.12 Mean Squared Error (MSE)

mse <- mean(linear_model$residuals^2)
cat("Mean Squared Error (MSE):", mse, "\n")
## Mean Squared Error (MSE): 10188573

9.1.13 Calculate R-squared (R2)

r2 <- cor(y_test, y_pred)^ 2
cat("R-squared (R2):", r2, "\n")
## R-squared (R2): 0.8442901

9.1.14 Mean Absolute Error (MAE)

mae <- MAE(data2$price, predict(linear_model))
cat("Mean Absolute Error (MAE):", mae, "\n")
## Mean Absolute Error (MAE): 2240.889

###Second Trial

MSE <- function(y_true, y_pred) {
  mse <- mean((y_true - y_pred) ^ 2)
  cat('MSE:', round(mse, 3), "\n")
  return(mse)
}
MSE(y_test, y_pred)
## MSE: 10188573
## [1] 10188573
R2 <- function(y_true, y_pred) {
  r2 <- cor(y_true, y_pred) ^ 2
  cat('R2:', round(r2, 3), "\n")
  return(r2)
}
R2(y_test, y_pred)
## R2: 0.844
## [1] 0.8442901

9.2 For the Lasso Model

Lasso regression is a linear regression technique that performs both variable selection and regularization to improve the model’s prediction accuracy and interpretability. It penalizes the absolute size of the coefficients to encourage sparse solutions.

9.2.1 Load necessary libraries

library(glmnet)
## Loading required package: Matrix
## Loaded glmnet 4.1-8
library(ggplot2)
library(Metrics)

9.2.2 Fit Lasso regression model

lasso_model <- glmnet(x = as.matrix(data2[, c("bore", "stroke", "horsepower", "peak.rpm")]), 
                      y = data2$price, 
                      alpha = 1)  # Lasso regression (alpha = 1)

9.2.3 Summary of the Lasso regression model

print(lasso_model)
## 
## Call:  glmnet(x = as.matrix(data2[, c("bore", "stroke", "horsepower",      "peak.rpm")]), y = data2$price, alpha = 1) 
## 
##    Df  %Dev Lambda
## 1   0  0.00 6572.0
## 2   1 11.21 5989.0
## 3   1 20.51 5457.0
## 4   1 28.24 4972.0
## 5   1 34.65 4530.0
## 6   1 39.98 4128.0
## 7   1 44.40 3761.0
## 8   1 48.07 3427.0
## 9   1 51.12 3122.0
## 10  1 53.65 2845.0
## 11  1 55.75 2592.0
## 12  1 57.49 2362.0
## 13  1 58.94 2152.0
## 14  1 60.14 1961.0
## 15  1 61.14 1787.0
## 16  1 61.97 1628.0
## 17  2 62.72 1483.0
## 18  2 63.45 1352.0
## 19  3 64.42 1232.0
## 20  3 65.29 1122.0
## 21  3 66.02 1022.0
## 22  3 66.63  931.6
## 23  3 67.13  848.9
## 24  3 67.55  773.5
## 25  3 67.90  704.7
## 26  3 68.18  642.1
## 27  3 68.42  585.1
## 28  3 68.62  533.1
## 29  3 68.79  485.8
## 30  3 68.92  442.6
## 31  3 69.04  403.3
## 32  3 69.13  367.5
## 33  3 69.21  334.8
## 34  3 69.27  305.1
## 35  3 69.33  278.0
## 36  3 69.37  253.3
## 37  3 69.41  230.8
## 38  3 69.44  210.3
## 39  3 69.46  191.6
## 40  3 69.49  174.6
## 41  3 69.50  159.1
## 42  3 69.52  144.9
## 43  3 69.53  132.1
## 44  3 69.54  120.3
## 45  3 69.55  109.6
## 46  3 69.56   99.9
## 47  3 69.56   91.0
## 48  3 69.57   82.9
## 49  3 69.57   75.6
## 50  3 69.57   68.8
## 51  3 69.58   62.7
## 52  3 69.58   57.2
## 53  3 69.58   52.1
## 54  3 69.58   47.5
## 55  3 69.58   43.2
## 56  3 69.58   39.4
## 57  3 69.59   35.9
## 58  3 69.59   32.7
## 59  3 69.59   29.8

9.2.4 Visualize the coefficients

plot(lasso_model, xvar = "lambda", label = TRUE)

9.2.5 Evaluate the Lasso model

9.2.6 Predict on training data

lambda_min <- lasso_model$lambda.min  # Get lambda.min value from the model object
predicted <- predict(lasso_model, newx = as.matrix(data2[, c("bore", "stroke", "horsepower", "peak.rpm")]), s = lambda_min)

9.2.7 Check if ‘predicted’ is null

if (is.null(predicted)) {
  cat("Error: 'predicted' is null. Check predict function call.\n")
} else {
  # Mean Absolute Error (MAE)
  mae <- MAE(data2$price, predicted)
  cat("Mean Absolute Error (MAE):", mae, "\n")
  
  # Mean Squared Error (MSE)
  mse <- mean((data2$price - predicted)^2)
  cat("Mean Squared Error (MSE):", mse, "\n")
}
## Mean Absolute Error (MAE): 3572.318 
## Mean Squared Error (MSE): 25106915

###Third Trial ### Define the MSE function

MSE <- function(y_true, y_pred) {
  mse <- mean((y_true - y_pred) ^ 2)
  cat('MSE:', round(mse, 3), "\n")
  return(mse)
}

9.2.8 Define the R2 function

R2 <- function(y_true, y_pred) {
  r2 <- cor(y_true, y_pred) ^ 2
  cat('R2:', round(r2, 3), "\n")
  return(r2)
}

9.2.9 Calculate MSE and R2

MSE(y_test, y_pred)
## MSE: 10188573
## [1] 10188573
R2(y_test, y_pred)
## R2: 0.844
## [1] 0.8442901

9.2.10 Results Interpretation

It’s already a good result: R squared of 0.83-0.86 and the regression line in the scatter plot is straight and aligned with the diagonal it indicates that the model’s predictions are very close to the actual prices. The statistical metrics show low residual standard error, high R-squared values, and a significant F-statistic, it suggest that the model is performing well in predicting the prices. And there is a strong linear relationship between the predicted and actual prices.

9.2.11 Make predictions on the training set

train_preds <- predict(linear_model, newdata = data2)

9.2.12 Create a data frame with predictions and true values

preds <- data.frame(preds = train_preds, true = data2$price)
preds$residuals <- preds$true - preds$preds

9.2.13 Plot residuals

ggplot(preds, aes(x = preds, y = residuals)) +
  geom_point(color = "blue") +
  labs(title = "Residual Plot",
       x = "Predicted Values",
       y = "Residuals") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))  # Center the plot title

### For the Lasso Model Lasso regression is a linear regression technique that performs both variable selection and regularization to improve the model’s prediction accuracy and interpretability. It penalizes the absolute size of the coefficients to encourage sparse solutions.

9.2.14 Load necessary libraries

library(glmnet)
library(ggplot2)
library(Metrics)

9.2.15 Fit Lasso regression model

lasso_model <- glmnet(x = as.matrix(data2[, c("bore", "stroke", "horsepower", "peak.rpm")]), 
                      y = data2$price, 
                      alpha = 1)  # Lasso regression (alpha = 1)

9.2.16 Summary of the Lasso regression model

print(lasso_model)
## 
## Call:  glmnet(x = as.matrix(data2[, c("bore", "stroke", "horsepower",      "peak.rpm")]), y = data2$price, alpha = 1) 
## 
##    Df  %Dev Lambda
## 1   0  0.00 6572.0
## 2   1 11.21 5989.0
## 3   1 20.51 5457.0
## 4   1 28.24 4972.0
## 5   1 34.65 4530.0
## 6   1 39.98 4128.0
## 7   1 44.40 3761.0
## 8   1 48.07 3427.0
## 9   1 51.12 3122.0
## 10  1 53.65 2845.0
## 11  1 55.75 2592.0
## 12  1 57.49 2362.0
## 13  1 58.94 2152.0
## 14  1 60.14 1961.0
## 15  1 61.14 1787.0
## 16  1 61.97 1628.0
## 17  2 62.72 1483.0
## 18  2 63.45 1352.0
## 19  3 64.42 1232.0
## 20  3 65.29 1122.0
## 21  3 66.02 1022.0
## 22  3 66.63  931.6
## 23  3 67.13  848.9
## 24  3 67.55  773.5
## 25  3 67.90  704.7
## 26  3 68.18  642.1
## 27  3 68.42  585.1
## 28  3 68.62  533.1
## 29  3 68.79  485.8
## 30  3 68.92  442.6
## 31  3 69.04  403.3
## 32  3 69.13  367.5
## 33  3 69.21  334.8
## 34  3 69.27  305.1
## 35  3 69.33  278.0
## 36  3 69.37  253.3
## 37  3 69.41  230.8
## 38  3 69.44  210.3
## 39  3 69.46  191.6
## 40  3 69.49  174.6
## 41  3 69.50  159.1
## 42  3 69.52  144.9
## 43  3 69.53  132.1
## 44  3 69.54  120.3
## 45  3 69.55  109.6
## 46  3 69.56   99.9
## 47  3 69.56   91.0
## 48  3 69.57   82.9
## 49  3 69.57   75.6
## 50  3 69.57   68.8
## 51  3 69.58   62.7
## 52  3 69.58   57.2
## 53  3 69.58   52.1
## 54  3 69.58   47.5
## 55  3 69.58   43.2
## 56  3 69.58   39.4
## 57  3 69.59   35.9
## 58  3 69.59   32.7
## 59  3 69.59   29.8
library(glmnet)
X <- as.matrix(data2[, -ncol(data2)])  # All columns except the last one
y <- data2[, ncol(data2)]  # The last column as the response variable
set.seed(123)  # Set seed for reproducibility
lassocv <- cv.glmnet(X, y, alpha = 1)
## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion

## Warning in storage.mode(xd) <- "double": NAs introduced by coercion
## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion

## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion

## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion

## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion

## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion

## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion

## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion

## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion

## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion

## Warning in cbind2(1, newx) %*% nbeta: NAs introduced by coercion
# Get the best lambda value
best_lambda <- lassocv$lambda.min

# Print the best lambda value
print(best_lambda)
## [1] 0.001187405
coefs <- as.matrix(coef(lassocv, s = "lambda.min"))
coefs <- coefs[-1, ]  # Remove the intercept
# prints out the number of picked/eliminated features
cat("Lasso picked ", sum(coefs != 0), " features and eliminated the other ", 
    sum(coefs == 0), " features.\n")
## Lasso picked  16  features and eliminated the other  10  features.
# Create a data frame of coefficients and feature names
coefs_df <- data.frame(coef = coefs, feature = colnames(X))

# Print the number of picked/eliminated features
num_picked <- sum(coefs != 0)
num_eliminated <- sum(coefs == 0)

cat("Lasso picked ", num_picked, " features and eliminated the other ", num_eliminated, " features.\n")
## Lasso picked  16  features and eliminated the other  10  features.
# Take the first and last 5 coefficients for plotting
top_bottom_coefs <- rbind(head(coefs_df[order(coefs_df$coef),], 5),
                          tail(coefs_df[order(coefs_df$coef),], 5))

# Plot the coefficients
barplot(top_bottom_coefs$coef, horiz = TRUE, names.arg = top_bottom_coefs$feature,
        main = "Coefficients in the Lasso Model", col = "steelblue")

# Assuming 'data2' is already loaded into the environment
# Example: data2 <- read.csv('path_to_your_csv_file.csv')

# Define the target variable and feature dataframe
target_column_name <- "price"  # The target column name
if (!target_column_name %in% colnames(data2)) {
  stop("The target column 'price' is not found in the dataframe")
}
target <- data2[[target_column_name]]  # Ensure this column exists in your dataframe
features <- data2[, !names(data2) %in% target_column_name]
# Replace missing values with median
features <- na.omit(features)

# Categorical variables
classes <- c('make', 'fuel.type', 'aspiration', 'num.of.doors', 'body.style', 'drive.wheels', 'engine.location', 'engine.type', 'num.of.cylinders', 'fuel.system')
# Check if the classes exist in the features dataframe and print missing variables
missing_vars <- setdiff(classes, colnames(features))
if (length(missing_vars) > 0) {
  stop(paste("The following categorical variables are not found in the features dataframe:", paste(missing_vars, collapse = ", ")))
}
# Create dummy variables for categorical columns
dummies <- model.matrix(~ . - 1, data = features[, classes, drop = FALSE])
features <- cbind(features, dummies)
features <- features[, !colnames(features) %in% classes]

# Print dataset dimensions and first few rows
print(paste("In total:", dim(features)))
## [1] "In total: 192" "In total: 62"
head(features)
##   symboling normalized.losses wheel.base length width height curb.weight
## 1         3          174.3846       88.6  168.8  64.1   48.8        2548
## 2         1          128.1522       94.5  171.2  65.5   52.4        2823
## 3         2          164.0000       99.8  176.6  66.2   54.3        2337
## 4         2          164.0000       99.4  176.6  66.4   54.3        2824
## 5         2          125.6897       99.8  177.3  66.3   53.1        2507
## 6         1          158.0000      105.8  192.7  71.4   55.7        2844
##   engine.size bore stroke compression.ratio horsepower peak.rpm city.mpg
## 1         130 3.47   2.68               9.0        111     5000       21
## 2         152 2.68   3.47               9.0        154     5000       19
## 3         109 3.19   3.40              10.0        102     5500       24
## 4         136 3.19   3.40               8.0        115     5500       18
## 5         136 3.19   3.40               8.5        110     5500       19
## 6         136 3.19   3.40               8.5        110     5500       19
##   highway.mpg is_high_end makealfa-romero makeaudi makebmw makechevrolet
## 1          27           0               1        0       0             0
## 2          26           0               1        0       0             0
## 3          30           0               0        1       0             0
## 4          22           1               0        1       0             0
## 5          25           0               0        1       0             0
## 6          25           1               0        1       0             0
##   makedodge makehonda makeisuzu makejaguar makemazda makemercedes-benz
## 1         0         0         0          0         0                 0
## 2         0         0         0          0         0                 0
## 3         0         0         0          0         0                 0
## 4         0         0         0          0         0                 0
## 5         0         0         0          0         0                 0
## 6         0         0         0          0         0                 0
##   makemercury makemitsubishi makenissan makepeugot makeplymouth makeporsche
## 1           0              0          0          0            0           0
## 2           0              0          0          0            0           0
## 3           0              0          0          0            0           0
## 4           0              0          0          0            0           0
## 5           0              0          0          0            0           0
## 6           0              0          0          0            0           0
##   makesaab makesubaru maketoyota makevolkswagen makevolvo fuel.typegas
## 1        0          0          0              0         0            1
## 2        0          0          0              0         0            1
## 3        0          0          0              0         0            1
## 4        0          0          0              0         0            1
## 5        0          0          0              0         0            1
## 6        0          0          0              0         0            1
##   aspirationturbo num.of.doorstwo body.stylehardtop body.stylehatchback
## 1               0               1                 0                   0
## 2               0               1                 0                   1
## 3               0               0                 0                   0
## 4               0               0                 0                   0
## 5               0               1                 0                   0
## 6               0               0                 0                   0
##   body.stylesedan body.stylewagon drive.wheelsfwd drive.wheelsrwd
## 1               0               0               0               1
## 2               0               0               0               1
## 3               1               0               1               0
## 4               1               0               0               0
## 5               1               0               1               0
## 6               1               0               1               0
##   engine.locationrear engine.typel engine.typeohc engine.typeohcf
## 1                   0            0              0               0
## 2                   0            0              0               0
## 3                   0            0              1               0
## 4                   0            0              1               0
## 5                   0            0              1               0
## 6                   0            0              1               0
##   engine.typeohcv num.of.cylindersfive num.of.cylindersfour num.of.cylinderssix
## 1               0                    0                    1                   0
## 2               1                    0                    0                   1
## 3               0                    0                    1                   0
## 4               0                    1                    0                   0
## 5               0                    1                    0                   0
## 6               0                    1                    0                   0
##   num.of.cylindersthree num.of.cylinderstwelve fuel.system2bbl fuel.systemidi
## 1                     0                      0               0              0
## 2                     0                      0               0              0
## 3                     0                      0               0              0
## 4                     0                      0               0              0
## 5                     0                      0               0              0
## 6                     0                      0               0              0
##   fuel.systemmfi fuel.systemmpfi fuel.systemspdi fuel.systemspfi
## 1              0               1               0               0
## 2              0               1               0               0
## 3              0               1               0               0
## 4              0               1               0               0
## 5              0               1               0               0
## 6              0               1               0               0

9.2.17 Split the data into train/test set

set.seed(123)  # Setting seed for reproducibility
trainIndex <- createDataPartition(target, p = .7, list = FALSE, times = 1)
X_train <- features[trainIndex, ]
X_test <- features[-trainIndex, ]
y_train <- target[trainIndex]
y_test <- target[-trainIndex]

print(paste("Train", dim(X_train), "and test", dim(X_test)))
## [1] "Train 136 and test 56" "Train 62 and test 62"

9.2.18 Logarithmic scale: log base 2

alphas <- 2 ^ (2:11)
scores <- numeric(length(alphas))

for (i in seq_along(alphas)) {
  lasso <- glmnet(as.matrix(X_train), y_train, alpha = 1, lambda = alphas[i])
  y_pred <- predict(lasso, as.matrix(X_test), s = alphas[i], type = "response")
  scores[i] <- cor(y_test, y_pred) ^ 2
}

lassocv <- cv.glmnet(as.matrix(features), target, alpha = 1, nfolds = 10)
lassocv_score <- max(lassocv$cvm)
lassocv_alpha <- lassocv$lambda.min

plot(alphas, scores, type = "b", col = "black", xlab = expression(alpha), ylab = "CV Score", log = "x")
abline(h = lassocv_score, col = "red")

print(paste("CV results:", lassocv_score, lassocv_alpha))
## [1] "CV results: 65727012.0729188 43.0992704282378"

10 Discussions & Conclusion

In the earlier part of this project, where we have done an extensive data cleaning & data transformation, we have identified a substantial amount of data in the features that having missing values. This creates an attention for us to handle those data first. Feature that has a number of missing value is normalized.losses and we have taken care that by using summary statistics by symboling method, which typically being used in customer segmentation.

After we have identified the categorical features, we be able to make an univariate analysis on categorical and numerical features. Analysing univariate analysis on categorical features would help us to get a glimpse of most sold cars distribution based on the features . In addition to that, univariate analysis on numerical feature has helped us in determining the skewness and detecting outliers. Based on our analysis, we have observed a right-skewed distributions were observed for normalized losses, curb weight, engine size, horsepower, and price. This indicates that while most cars fall within a typical range, there are a few outliers with significantly higher values. Features such as engine size, horsepower, curb weight, and price are key indicators of a car’s performance and market segment. This information is vital in deciding method to use in the data normalisation, which will prepared us for the next stage of modelling.

In the correlation analysis, it’s worth mentioning that we have decided to go with Pearson’s Linear Correlation Test. From this test, we can conclude that Engine Size, Curb Weight, Horsepower, Width, Length have strong positive correlations with price, indicating that they are key factors in determining the price of a car with the degree of correlation is almost at 1. While on the other hand, Normalized Losses, Stroke, Compression Ratio, Height, Symboling, Peak RPM have weak to very weak correlations with price(almost 0), indicating they have minimal influence on the pricing of cars.

In data transformation, we have created a newly defined column which is called ‘is_high_end’ to group a certain threshold of car’s price to a single category, the threshold is anything above than 75% of maximum price which is $45400, will be considered as high end. This feature is going to be the target feature for prediction model.

In preparing the data for modelling, normalization for the numerical variable and creating dummy variables for categorical data have taken place so features can better represent the underlying problem or unseen data, resulting in improved model performance. With the data set has been split into 80-20, where 80% is used for training and 20% for testing.

In our project, model evaluation has highlighted substantial improvements in performance for the classifier model, particularly after applying repeated K-fold cross-validation. Given our dataset’s relatively small size, a simple split into training and testing sets may not provide enough data for robust training and evaluation. K-fold cross-validation mitigates this by dividing the data into K parts, training the model on K-1 parts, and testing it on the remaining part. Repeating this process multiple times with different data partitions helps to produce a more reliable performance estimate, reducing variability and providing a more comprehensive evaluation of the model.

This enhanced evaluation method is crucial because our project objectives extend beyond merely predicting the correct car prices. The classification model also plays a pivotal role in marketing segmentation. By accurately classifying cars into distinct price categories, we can effectively identify and target different market segments, facilitating more precise and effective marketing strategies.

Transitioning to the regression model, our focus shifts to predicting numerical values to deepen our analysis. While the classification model aids in market segmentation, the regression model is vital for pricing rationalization. This involves modeling the relationship between the dependent variable (‘price’) and several independent variables (‘bore’, ‘stroke’, ‘horsepower’, ‘peak.rpm’, ‘curb.weight’, and ‘engine.size’) by fitting a linear equation to the dataset ‘data2’. This approach allows us to predict car prices based on their features accurately.

Moreover, we validate these predictions using Lasso regression to handle potential variations and prevent overfitting, ensuring that our model generalizes well to new data. This analysis serves dual purposes:

Pricing Rationalization: It provides a data-driven basis for setting car prices, ensuring they reflect the actual value of the vehicle. This helps car companies to stay competitive in the market while optimizing their resources. Resource Optimization: By identifying which features most significantly influence car prices, companies can prioritize their production and marketing efforts on these aspects, aligning them more closely with market demand.

In summary, integrating both classification and regression models in our analysis not only enhances our ability to predict car prices but also supports effective market segmentation. These insights are invaluable for developing targeted marketing strategies and setting rational car prices that optimize competitive positioning and resource utilization.

In essence, this project showcases how data science models can transform various facets of business operations, from enhancing customer engagement to optimizing internal processes. The insights derived from these models will empower organizations to make informed decisions, anticipate market trends, and sustain competitive advantage in a dynamic environment.