Importing Libraries

library(ggplot2)
library(lattice)
library(caret)
## Warning: package 'caret' was built under R version 4.3.3
library(e1071)
## Warning: package 'e1071' was built under R version 4.3.3

Importing Dataset

df<-read.csv("C:/Users/ONKAR/OneDrive/Desktop/SEM-4/Projects/Data Science/dataset.csv")
head(df,2)

Spliting Dataset into train and test set

set.seed(123)
data_split_index <-sample(2,nrow(df),replace = TRUE,prob<-c(0.8,0.2))
train<-df[data_split_index==1,-12]
train_labels<-df[data_split_index==1,12]
test_demo<-df[data_split_index==2,]
target<-as.factor(test_demo[,12])
test<-df[data_split_index==2,-12]

Applying NaiveBayes Algorithm

naive_bayes_model <- naiveBayes(x=train,y=train_labels)

Prediction Using NaiveBayes

nb_prediction<-predict(naive_bayes_model,test)
nb_prediction
##  [1] Neutral    Neutral    Neutral    Close      Close      Neutral   
##  [7] Very close Very close Neutral    Close      Close      Close     
## [13] Close      Close      Neutral    Neutral    Close      Close     
## [19] Neutral    Close      Close      Neutral    Very close Close     
## [25] Very close Close      Close      Very close Close      Close     
## [31] Very close Close      Close      Close      Neutral    Neutral   
## [37] Very close Neutral    Very close Neutral    Very close Neutral   
## [43] Neutral    Very close Close      Close      Close      Very close
## [49] Very close Neutral    Neutral    Close      Very close Very close
## [55] Very close Very close Close      Neutral    Neutral    Close     
## [61] Very close Neutral    Neutral    Close      Neutral    Neutral   
## [67] Very close Close      Close      Close      Neutral    Close     
## [73] Very close Very close Close      Neutral    Very close Very close
## [79] Close      Very close Neutral    Very close Close      Very close
## [85] Very close Close      Close      Very close Close      Close     
## [91] Close      Neutral    Close      Very close
## Levels: Close Neutral Very close

Confusion Matrix

nb_confusion_matrix<-confusionMatrix(nb_prediction,target)
nb_confusion_matrix
## Confusion Matrix and Statistics
## 
##             Reference
## Prediction   Close Neutral Very close
##   Close         17      12         10
##   Neutral       11      12          4
##   Very close     8       1         19
## 
## Overall Statistics
##                                           
##                Accuracy : 0.5106          
##                  95% CI : (0.4054, 0.6152)
##     No Information Rate : 0.383           
##     P-Value [Acc > NIR] : 0.007958        
##                                           
##                   Kappa : 0.2587          
##                                           
##  Mcnemar's Test P-Value : 0.558883        
## 
## Statistics by Class:
## 
##                      Class: Close Class: Neutral Class: Very close
## Sensitivity                0.4722         0.4800            0.5758
## Specificity                0.6207         0.7826            0.8525
## Pos Pred Value             0.4359         0.4444            0.6786
## Neg Pred Value             0.6545         0.8060            0.7879
## Prevalence                 0.3830         0.2660            0.3511
## Detection Rate             0.1809         0.1277            0.2021
## Detection Prevalence       0.4149         0.2872            0.2979
## Balanced Accuracy          0.5465         0.6313            0.7141

Accuracy

accuracy<-nb_confusion_matrix$overall["Accuracy"]*100
cat("Accuracy of model is ",accuracy," %")
## Accuracy of model is  51.06383  %

Sample Prediction

data_vector <- c(gender = "Male",
                 age = 20,
                 native_place = "City",
                 quality_time = "Regularly",
                 qualification_of_father = "Graduate",
                 sharing_emotions = "Yes",
                 deep_conversations = "Regularly",
                 role_father = "Consultant (offering advice)",
                 advice_from_father = "Regularly",
                 improve_relationship = "Yes",
                 relationship_in_future = "More open communication and honesty"
                 )

test_data_sample = data.frame(t(data_vector))
test_data_sample$age<-as.numeric(test_data_sample$age)

Result of Sample Prediction

sample_predictions<-predict(naive_bayes_model,newdata = test_data_sample)
sample_predictions
## [1] Very close
## Levels: Close Neutral Very close