cars <- read.csv("mode_of_transport.csv")
View(cars)
str(cars)
'data.frame': 444 obs. of 9 variables:
$ Age : int 28 23 29 28 27 26 28 26 22 27 ...
$ Gender : chr "Male" "Female" "Male" "Female" ...
$ Engineer : int 0 1 1 1 1 1 1 1 1 1 ...
$ MBA : int 0 0 0 1 0 0 0 0 0 0 ...
$ Work.Exp : int 4 4 7 5 4 4 5 3 1 4 ...
$ Salary : num 14.3 8.3 13.4 13.4 13.4 12.3 14.4 10.5 7.5 13.5 ...
$ Distance : num 3.2 3.3 4.1 4.5 4.6 4.8 5.1 5.1 5.1 5.2 ...
$ license : int 0 0 0 0 0 1 0 0 0 0 ...
$ Transport: chr "Public Transport" "Public Transport" "Public Transport" "Public Transport" ...
Variables like Engineer, MBA, license contains category values. So, we need to convert them into factors.
cars$Engineer = as.factor(cars$Engineer)
cars$MBA = as.factor(cars$MBA)
cars$license = as.factor(cars$license)
cars$Gender = as.factor(cars$Gender)
cars$Transport = as.factor(cars$Transport)
summary(cars)
Age Gender Engineer MBA Work.Exp Salary
Min. :18.00 Female:128 0:109 0:332 Min. : 0.0 Min. : 6.50
1st Qu.:25.00 Male :316 1:335 1:112 1st Qu.: 3.0 1st Qu.: 9.80
Median :27.00 Median : 5.0 Median :13.60
Mean :27.75 Mean : 6.3 Mean :16.24
3rd Qu.:30.00 3rd Qu.: 8.0 3rd Qu.:15.72
Max. :43.00 Max. :24.0 Max. :57.00
Distance license Transport
Min. : 3.20 0:340 Private Transport:144
1st Qu.: 8.80 1:104 Public Transport :300
Median :11.00
Mean :11.32
3rd Qu.:13.43
Max. :23.40
Here, the minimum age is 18, maximum age is 43 and average age is 27. We conclude that the majority is of males of approximately 70-77%. Here, there are more Engineers than MBA’s. And, the most common mode of transport is public transportation.
boxplot(cars$Age ~ cars$Engineer, main="Engineer's Age")
boxplot(cars$Age ~ cars$MBA, main="MBA's Age")
There’s no much difference in the age of Engineers and MBA’s.
Let’s find differences in the average salary for two professions.
boxplot(cars$Salary ~ cars$Engineer, main="Engineer's Salary")
boxplot(cars$Salary ~ cars$MBA, main="MBA's Salary")
There’s isn’t any major difference in the salary.
Let’s see the Work Exp column
hist(cars$Work.Exp, main="Distribution of Work Experience")
This column is right skewed, this is expected as there would be more juniors then seniors in any firm.
boxplot(cars$Work.Exp ~ cars$Gender)
The box plot shows that there is an equal distribution of male and female with no such difference in work experience.
table(cars$license, cars$Transport)
Private Transport Public Transport
0 73 267
1 71 33
The table clearly shows that those who don’t have license uses public transport more.
boxplot(cars$Salary ~ cars$Transport, main="Mode of transport according to the salary")
Plot clearly shows that as the salary increases the number of individuals with private transport also increases.
Let’s see if the age and work experience can be usage of private transport increases
cor(cars$Age, cars$Work.Exp)
[1] 0.9322364
boxplot(cars$Age ~ cars$Transport, main="Age and Transport")
The plot clearly explains that the higher age prefers private transport.
Let’s see with the increase in distance would the employees prefer private or public transport.
boxplot(cars$Distance ~ cars$Transport, main="Preference of transport according to distance")
In this plot the pattern shows that for longer distance private transport is the preference of employees.
Let’s see whether female employees prefer private or public transport
table(cars$Gender, cars$Transport)
Private Transport Public Transport
Female 51 77
Male 93 223
From the above data it is visible that more females prefers Public Transport.
Only 40% female uses private transport and only 30% male uses private transport.
Let’s find if there is any missing values in the data set.
anyNA(cars)
[1] FALSE
set.seed(1234)
pd = sample(2, nrow(cars), replace=TRUE, prob = c(0.7, 0.3))
carstrain = cars[pd==1,]
carstest = cars[pd==2,]
carstest$Salary = log(carstest$Salary)
carstest$Engineer = as.factor(carstest$Engineer)
carstest$MBA = as.factor(carstest$MBA)
carstest$license = as.factor(carstest$license)
There isn’t any missing values in this data set.
library(caret)
random <- createDataPartition(cars$Transport, p=0.70, list=FALSE)
train_set = cars[random,]
test_set = cars[-random,]
library(e1071)
nb_model <- naiveBayes(train_set$Transport ~ ., data = train_set)
nb_model
Naive Bayes Classifier for Discrete Predictors
Call:
naiveBayes.default(x = X, y = Y, laplace = laplace)
A-priori probabilities:
Y
Private Transport Public Transport
0.3247588 0.6752412
Conditional probabilities:
Age
Y [,1] [,2]
Private Transport 29.31683 5.847958
Public Transport 26.72381 3.081028
Gender
Y Female Male
Private Transport 0.3861386 0.6138614
Public Transport 0.2428571 0.7571429
Engineer
Y 0 1
Private Transport 0.2079208 0.7920792
Public Transport 0.2190476 0.7809524
MBA
Y 0 1
Private Transport 0.7920792 0.2079208
Public Transport 0.6952381 0.3047619
Work.Exp
Y [,1] [,2]
Private Transport 8.326733 6.855813
Public Transport 4.952381 3.220392
Salary
Y [,1] [,2]
Private Transport 21.67624 14.923787
Public Transport 13.19238 4.956069
Distance
Y [,1] [,2]
Private Transport 13.45545 3.483345
Public Transport 10.39000 2.915762
license
Y 0 1
Private Transport 0.5346535 0.4653465
Public Transport 0.8857143 0.1142857
nb_predictions <- predict(nb_model, test_set)
table(nb_predictions, test_set$Transport)
nb_predictions Private Transport
Private Transport 21
Public Transport 22
nb_predictions Public Transport
Private Transport 3
Public Transport 87
logistic_model <- glm(Transport~., data = train_set, family = binomial(link = "logit"))
test_set$log.pred <- predict(logistic_model, test_set[1:8], type="response")
table(test_set$Transport, test_set$log.pred>0.5)
FALSE TRUE
Private Transport 24 19
Public Transport 8 82
trControl <- trainControl(method = "cv", number = 10)
fit.knn <- train(Transport ~ ., method = "knn",
tuneGrid = expand.grid(k=2:20),
trControl=trControl,
metric = "Accuracy",
preProcess = c("center", "scale"),
data = train_set)
fit.knn
k-Nearest Neighbors
311 samples
8 predictor
2 classes: 'Private Transport', 'Public Transport'
Pre-processing: centered (8), scaled (8)
Resampling: Cross-Validated (10 fold)
Summary of sample sizes: 280, 280, 280, 280, 280, 280, ...
Resampling results across tuning parameters:
k Accuracy Kappa
2 0.7617944 0.4418982
3 0.8039315 0.5306667
4 0.7913306 0.5027636
5 0.7912298 0.4954522
6 0.7750000 0.4515462
7 0.7910282 0.4862478
8 0.7748992 0.4463433
9 0.8007056 0.4927173
10 0.7784274 0.4417140
11 0.7847782 0.4481347
12 0.7814516 0.4294389
13 0.7911290 0.4533349
14 0.8104839 0.5020040
15 0.8007056 0.4790518
16 0.7878024 0.4438665
17 0.7813508 0.4223460
18 0.7782258 0.4135288
19 0.7781250 0.4167973
20 0.7717742 0.3926983
Accuracy was used to select the optimal
model using the largest value.
The final value used for the model was k = 14.
knn_pred_train <- predict(fit.knn, train_set)
table(knn_pred_train, train_set$Transport)
knn_pred_train Private Transport
Private Transport 54
Public Transport 47
knn_pred_train Public Transport
Private Transport 7
Public Transport 203
knn_pred_test <- predict(fit.knn, test_set)
table(knn_pred_test, test_set$Transport)
knn_pred_test Private Transport
Private Transport 23
Public Transport 20
knn_pred_test Public Transport
Private Transport 2
Public Transport 88
predict(fit.knn, carstest)
[1] Public Transport Public Transport
[3] Public Transport Public Transport
[5] Public Transport Public Transport
[7] Public Transport Public Transport
[9] Public Transport Public Transport
[11] Public Transport Public Transport
[13] Public Transport Public Transport
[15] Public Transport Public Transport
[17] Public Transport Public Transport
[19] Public Transport Public Transport
[21] Public Transport Public Transport
[23] Public Transport Public Transport
[25] Public Transport Public Transport
[27] Public Transport Public Transport
[29] Public Transport Public Transport
[31] Public Transport Public Transport
[33] Public Transport Public Transport
[35] Public Transport Public Transport
[37] Public Transport Public Transport
[39] Public Transport Public Transport
[41] Public Transport Public Transport
[43] Public Transport Public Transport
[45] Private Transport Public Transport
[47] Public Transport Public Transport
[49] Private Transport Public Transport
[51] Public Transport Public Transport
[53] Private Transport Public Transport
[55] Public Transport Public Transport
[57] Public Transport Public Transport
[59] Public Transport Public Transport
[61] Public Transport Public Transport
[63] Public Transport Public Transport
[65] Private Transport Public Transport
[67] Public Transport Public Transport
[69] Public Transport Private Transport
[71] Public Transport Private Transport
[73] Public Transport Public Transport
[75] Private Transport Public Transport
[77] Private Transport Public Transport
[79] Public Transport Public Transport
[81] Public Transport Public Transport
[83] Public Transport Public Transport
[85] Public Transport Public Transport
[87] Public Transport Public Transport
[89] Public Transport Public Transport
[91] Public Transport Public Transport
[93] Public Transport Private Transport
[95] Public Transport Public Transport
[97] Public Transport Public Transport
[99] Public Transport Private Transport
[101] Private Transport Public Transport
[103] Private Transport Public Transport
[105] Private Transport Private Transport
[107] Public Transport Public Transport
[109] Public Transport Private Transport
[111] Private Transport Private Transport
[113] Private Transport Public Transport
[115] Private Transport Private Transport
[117] Private Transport Public Transport
[119] Public Transport Private Transport
[121] Private Transport Public Transport
[123] Public Transport Public Transport
[125] Private Transport Private Transport
[127] Private Transport Private Transport
[129] Private Transport Private Transport
[131] Private Transport Private Transport
[133] Private Transport
Levels: Private Transport Public Transport