AIB TERM PAPER

By

ANANTHANARAYAN R ( CB.BU.P2ASB22029 )

SUCHISMITA BISWAL ( CB.BU.P2ASB22167 )

Introduction

The provided R code constitutes an analytical endeavor focused on constructing and evaluating neural network models tailored to predict the power factor of cars based on a comprehensive set of features. Power factor, a crucial metric in automotive engineering, encapsulates various aspects of a vehicle’s performance and efficiency. By harnessing machine learning techniques, particularly neural networks, this analysis aims to uncover intricate patterns and relationships within the dataset “Car_sales.csv” to enable accurate power factor predictions.

Step 1 – collecting data

The dataset used in the analysis, titled “Car_sales.csv,” likely comprises a collection of data points representing various cars along with associated attributes. These attributes are presumably collected from diverse sources such as manufacturers, dealerships, or automotive industry databases. Here’s an explanation of the potential data collected based on the variable names and their descriptions provided in the code snippet:

  1. Sales_in_thousands:

    • This variable likely represents the sales volume of each car model in thousands. It provides insights into the popularity and market demand for different vehicles.
  2. year_resale_value:

    • This variable could denote the estimated resale value of the car after a certain number of years. It’s a crucial metric for consumers and dealerships in assessing the long-term value retention of a vehicle.
  3. Price_in_thousands:

    • This variable likely represents the price of the car in thousands of dollars. It’s a fundamental attribute influencing purchasing decisions and reflects the perceived value of the vehicle.
  4. Engine_size:

    • Engine size typically refers to the displacement volume of the engine, measured in liters or cubic centimeters. It’s an indicator of the power and performance capabilities of the vehicle.
  5. Horsepower:

    • Horsepower denotes the engine’s power output, measured in units of mechanical horsepower. It’s a key performance metric influencing acceleration, towing capacity, and overall driving experience.
  6. Wheelbase, Width, Length, Curb_weight:

    • These variables likely represent physical dimensions and weight specifications of the vehicle, including wheelbase (distance between the centers of front and rear wheels), width, length, and curb weight (weight of the vehicle without occupants or cargo). These attributes contribute to aspects such as stability, interior space, and fuel efficiency.
  7. Fuel_capacity, Fuel_efficiency:

    • Fuel capacity denotes the volume of fuel the vehicle’s tank can hold, typically measured in gallons or liters. Fuel efficiency represents the distance the vehicle can travel per unit of fuel (e.g., miles per gallon or kilometers per liter). These attributes are crucial for assessing operating costs and environmental impact.
  8. Power_Factor:

    • This variable likely represents a composite metric or index reflecting the overall performance or efficiency of the vehicle. It could be derived from a combination of the aforementioned attributes and other factors pertinent to automotive performance and utility.

By analyzing these diverse attributes, the dataset enables comprehensive insights into the characteristics, performance, and market positioning of various car models, facilitating informed decision-making for consumers, manufacturers, and industry stakeholders within the automotive domain.

Step 2 – exploring and preparing the data

sales <- read.csv("C:/Users/Nandu/Downloads/Car_sales.csv")
str(sales)
'data.frame':   155 obs. of  12 variables:
 $ Sales_in_thousands: num  16.92 39.38 14.11 8.59 20.4 ...
 $ year_resale_value : num  16.4 19.9 18.2 29.7 22.3 ...
 $ Price_in_thousands: num  21.5 28.4 55 42 24 ...
 $ Engine_size       : num  1.8 3.2 3.2 3.5 1.8 2.8 4.2 2.5 2.8 2.8 ...
 $ Horsepower        : int  140 225 225 210 150 200 310 170 193 193 ...
 $ Wheelbase         : num  101 108 107 115 103 ...
 $ Width             : num  67.3 70.3 70.6 71.4 68.2 76.1 74 68.4 68.5 70.9 ...
 $ Length            : num  172 193 192 197 178 ...
 $ Curb_weight       : num  2.64 3.52 3.47 3.85 3 ...
 $ Fuel_capacity     : num  13.2 17.2 17.2 18 16.4 18.5 23.7 16.6 16.6 18.5 ...
 $ Fuel_efficiency   : int  28 25 26 22 27 22 21 26 24 25 ...
 $ Power_Factor      : num  58.3 91.4 62.4 91.4 62.8 ...

This line reads a CSV file containing car sales data into a data frame named sales

The str() function provides a compact display of the structure of the data frame. It shows the dimensions and data types of each variable.

normalize <- function(x) { 
  return((x - min(x)) / (max(x) - min(x)))
}

This defines a normalization function that takes a vector x as input and returns the normalized values between 0 and 1.

sales_norm <- as.data.frame(lapply(sales, normalize))

This applies the normalization function to each column of the sales data frame and stores the normalized values in a new data frame named sales_norm.

summary(sales_norm$Power_Factor)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.0000  0.2252  0.2945  0.3246  0.3957  1.0000 
summary(sales$Power_Factor)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  23.28   60.41   71.84   76.79   88.52  188.14 

These lines provide summary statistics for the variable Power_Factor before and after normalization. It shows minimum, maximum, median, mean, and quartiles.

concrete_train <- sales_norm[1:108, ]
concrete_test <- sales_norm[109:155, ]

These lines split the normalized data into training and testing sets. The first 108 rows are used for training (concrete_train), and the rest are used for testing (concrete_test).

Step 3 – training a model on the data

library(neuralnet)
Warning: package 'neuralnet' was built under R version 4.3.3

This line loads the neuralnet package, which provides functions for training and testing neural networks.

set.seed(12345) # to guarantee repeatable results
concrete_model <- neuralnet(Power_Factor ~ Sales_in_thousands+year_resale_value+Engine_size+Horsepower+Wheelbase+Width+Length+Curb_weight+Fuel_capacity+Fuel_efficiency, data = concrete_train)

This line creates a neural network model using the neuralnet function. It specifies the formula for predicting Power_Factor based on the input features and uses the training data (concrete_train) for model training.

plot(concrete_model)

This line plots the neural network model.

Step 4 – evaluating model performance

# obtain model results
model_results <- compute(concrete_model, concrete_test[1:11])
# obtain predicted strength values
predicted_strength <- model_results$net.result
# examine the correlation between predicted and actual values
cor(predicted_strength, concrete_test$Power_Factor)
          [,1]
[1,] 0.9970031

hese lines compute the predicted values of Power_Factor using the neural network model (concrete_model) and the testing data (concrete_test). It then calculates the correlation between the predicted values and the actual Power_Factor values

Step 5 – improving model performance

set.seed(12345) # to guarantee repeatable results
concrete_model2 <- neuralnet(Power_Factor ~ Sales_in_thousands+year_resale_value+Engine_size+Horsepower+Wheelbase+Width+Length+Curb_weight+Fuel_capacity+Fuel_efficiency,
                               data = concrete_train, hidden = 5)

This line creates another neural network model (concrete_model2) with a hidden layer containing 5 neurons.

plot(concrete_model2)

This line plots the second neural network model.

model_results2 <- compute(concrete_model2, concrete_test[1:11])
predicted_strength2 <- model_results2$net.result
cor(predicted_strength2, concrete_test$strength)
     [,1]
[1,]    1

These lines compute the predicted values of Power_Factor using the second neural network model (concrete_model2) and the testing data (concrete_test). It then calculates the correlation between the predicted values and the actual Power_Factor values.

This code essentially performs data preprocessing, builds neural network models, evaluates their performance, and compares them based on the correlation between predicted and actual values.

ANALYSIS

The provided R code revolves around the construction and evaluation of neural network models aimed at predicting the power factor of cars based on a set of features.

Initially, the dataset “Car_sales.csv” is loaded into a dataframe named sales, and its structure is examined to understand the variables it contains. This step ensures familiarity with the data before proceeding further. Subsequently, a normalization function is defined to scale all variables between 0 and 1, facilitating consistent training across different features. This normalized dataframe is stored in sales_norm.

The data is then partitioned into training and testing sets, denoted as concrete_train and concrete_test respectively. This division enables the assessment of model performance on unseen data, which is crucial for evaluating its generalizability.

With the data prepared, the neuralnet library is imported to construct neural network models. The first model, concrete_model, is formulated using the neuralnet() function, specifying the target variable (Power_Factor) and predictors. This model is trained on the training data to learn the underlying patterns and relationships between features and the target variable.

Visualization of the neural network structure (plot(concrete_model)) provides insights into its architecture and complexity, aiding in understanding its inner workings. Subsequently, predictions are made on the test data using the trained model, and the correlation between predicted and actual power factor values is calculated.

Following this, a second neural network model (concrete_model2) is created, this time with a hidden layer size of 5 neurons. The model training, visualization, prediction, and correlation calculation steps are repeated for this model, enabling a comparative analysis with the first model.

Upon evaluating the models’ performance, it is observed that the first model exhibits a remarkably high correlation (0.997) between predicted and actual power factor values on the test data, indicating robust predictive capabilities. However, the second model demonstrates a perfect correlation (1) between predicted and actual values, which may suggest potential overfitting to the training data.

In conclusion, the provided R code effectively constructs and assesses neural network models for predicting car power factor, offering valuable insights into their predictive accuracy and generalizability.