#DAA ASSignment

#DATA LOAD
setwd("C:/Users/dclay/Downloads/MKT335")
library(readxl)
df = read_excel("Airline Customer Satisfaction Data 500Sample-1.xlsx")
View(df)

#OVERVIEW/PREPROCESS DATA
# Overview
str(df)
## tibble [500 × 23] (S3: tbl_df/tbl/data.frame)
##  $ satisfaction                     : chr [1:500] "satisfied" "dissatisfied" "dissatisfied" "dissatisfied" ...
##  $ Gender                           : chr [1:500] "Female" "Male" "Female" "Female" ...
##  $ Customer Type                    : chr [1:500] "disloyal Customer" "Loyal Customer" "Loyal Customer" "disloyal Customer" ...
##  $ Age                              : num [1:500] 24 40 23 25 57 33 37 34 46 33 ...
##  $ Type of Travel                   : chr [1:500] "Business travel" "Personal Travel" "Business travel" "Business travel" ...
##  $ Class                            : chr [1:500] "Business" "Eco" "Business" "Eco Plus" ...
##  $ Flight Distance                  : num [1:500] 1439 1758 3543 1553 1732 ...
##  $ Seat comfort                     : num [1:500] 4 3 3 4 1 3 5 3 4 1 ...
##  $ Departure/Arrival time convenient: num [1:500] 4 5 3 3 3 1 5 3 4 5 ...
##  $ Food and drink                   : num [1:500] 4 3 3 5 1 3 5 3 4 1 ...
##  $ Gate location                    : num [1:500] 1 3 3 4 1 3 5 3 3 2 ...
##  $ Inflight wifi service            : num [1:500] 1 3 3 2 2 5 2 5 5 2 ...
##  $ Inflight entertainment           : num [1:500] 4 3 3 5 5 4 3 3 4 1 ...
##  $ Online support                   : num [1:500] 1 4 3 3 4 5 5 5 5 2 ...
##  $ Ease of Online booking           : num [1:500] 1 3 3 2 4 4 5 5 5 2 ...
##  $ On-board service                 : num [1:500] 5 4 3 1 4 4 5 5 4 1 ...
##  $ Leg room service                 : num [1:500] 3 3 2 5 4 4 5 1 5 1 ...
##  $ Baggage handling                 : num [1:500] 5 5 4 3 4 4 5 5 4 1 ...
##  $ Checkin service                  : num [1:500] 3 4 4 2 5 5 4 3 3 4 ...
##  $ Cleanliness                      : num [1:500] 4 4 4 3 4 4 5 5 5 3 ...
##  $ Online boarding                  : num [1:500] 1 3 3 2 5 5 5 5 5 2 ...
##  $ Departure Delay in Minutes       : num [1:500] 0 0 4 25 15 0 0 0 0 0 ...
##  $ Arrival Delay in Minutes         : num [1:500] 0 0 0 19 13 0 0 0 0 0 ...
# Preprocess
# A) Correcting Variable Type
#    Switch to factors: satisfied, gender, customer type, type of travel, class
df$satisfaction = as.factor(df$satisfaction)
df$Gender = as.factor(df$Gender)
df$`Customer Type` = as.factor(df$`Customer Type`)
df$`Type of Travel` = as.factor(df$`Type of Travel`)
df$Class = as.factor(df$Class)

#    Creating dummy variables from above factors
library(fastDummies)
## Thank you for using fastDummies!
## To acknowledge our work, please cite the package:
## Kaplan, J. & Schlegel, B. (2023). fastDummies: Fast Creation of Dummy (Binary) Columns and Rows from Categorical Variables. Version 1.7.1. URL: https://github.com/jacobkap/fastDummies, https://jacobkap.github.io/fastDummies/.
df = dummy_cols(df, select_columns = c("satisfaction","Gender","Customer Type","Type of Travel","Class"))

# B) Subset numeric data
df_num = df[,sapply(df, is.numeric)]

#######################
#SUMMARIY OF VARIABLES#
#######################

#*important, summary of data and proportions*

summary(df)
##        satisfaction    Gender              Customer Type      Age      
##  dissatisfied:232   Female:264   disloyal Customer:101   Min.   :10.0  
##  satisfied   :268   Male  :236   Loyal Customer   :399   1st Qu.:26.0  
##                                                          Median :38.0  
##                                                          Mean   :39.2  
##                                                          3rd Qu.:51.0  
##                                                          Max.   :79.0  
##          Type of Travel      Class     Flight Distance  Seat comfort 
##  Business travel:346    Business:245   Min.   :  50    Min.   :0.00  
##  Personal Travel:154    Eco     :226   1st Qu.:1393    1st Qu.:2.00  
##                         Eco Plus: 29   Median :1844    Median :3.00  
##                                        Mean   :1941    Mean   :2.83  
##                                        3rd Qu.:2491    3rd Qu.:4.00  
##                                        Max.   :5306    Max.   :5.00  
##  Departure/Arrival time convenient Food and drink  Gate location  
##  Min.   :0.000                     Min.   :0.000   Min.   :1.000  
##  1st Qu.:2.000                     1st Qu.:2.000   1st Qu.:2.000  
##  Median :3.000                     Median :3.000   Median :3.000  
##  Mean   :2.924                     Mean   :2.868   Mean   :2.964  
##  3rd Qu.:4.000                     3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :5.000                     Max.   :5.000   Max.   :5.000  
##  Inflight wifi service Inflight entertainment Online support 
##  Min.   :1.000         Min.   :0.000          Min.   :1.000  
##  1st Qu.:2.000         1st Qu.:2.000          1st Qu.:3.000  
##  Median :3.000         Median :4.000          Median :4.000  
##  Mean   :3.252         Mean   :3.402          Mean   :3.526  
##  3rd Qu.:4.000         3rd Qu.:4.000          3rd Qu.:5.000  
##  Max.   :5.000         Max.   :5.000          Max.   :5.000  
##  Ease of Online booking On-board service Leg room service Baggage handling
##  Min.   :1.00           Min.   :1.000    Min.   :0.000    Min.   :1.000   
##  1st Qu.:2.00           1st Qu.:3.000    1st Qu.:2.000    1st Qu.:3.000   
##  Median :4.00           Median :4.000    Median :4.000    Median :4.000   
##  Mean   :3.45           Mean   :3.424    Mean   :3.452    Mean   :3.678   
##  3rd Qu.:5.00           3rd Qu.:4.000    3rd Qu.:5.000    3rd Qu.:5.000   
##  Max.   :5.00           Max.   :5.000    Max.   :5.000    Max.   :5.000   
##  Checkin service  Cleanliness    Online boarding Departure Delay in Minutes
##  Min.   :1.00    Min.   :1.000   Min.   :1.000   Min.   :  0.00            
##  1st Qu.:3.00    1st Qu.:3.000   1st Qu.:2.000   1st Qu.:  0.00            
##  Median :3.00    Median :4.000   Median :4.000   Median :  0.00            
##  Mean   :3.33    Mean   :3.736   Mean   :3.406   Mean   : 12.28            
##  3rd Qu.:4.00    3rd Qu.:5.000   3rd Qu.:5.000   3rd Qu.: 10.00            
##  Max.   :5.00    Max.   :5.000   Max.   :5.000   Max.   :392.00            
##  Arrival Delay in Minutes satisfaction_dissatisfied satisfaction_satisfied
##  Min.   :  0.00           Min.   :0.000             Min.   :0.000         
##  1st Qu.:  0.00           1st Qu.:0.000             1st Qu.:0.000         
##  Median :  0.00           Median :0.000             Median :1.000         
##  Mean   : 12.35           Mean   :0.464             Mean   :0.536         
##  3rd Qu.: 10.00           3rd Qu.:1.000             3rd Qu.:1.000         
##  Max.   :401.00           Max.   :1.000             Max.   :1.000         
##  Gender_Female    Gender_Male    Customer Type_disloyal Customer
##  Min.   :0.000   Min.   :0.000   Min.   :0.000                  
##  1st Qu.:0.000   1st Qu.:0.000   1st Qu.:0.000                  
##  Median :1.000   Median :0.000   Median :0.000                  
##  Mean   :0.528   Mean   :0.472   Mean   :0.202                  
##  3rd Qu.:1.000   3rd Qu.:1.000   3rd Qu.:0.000                  
##  Max.   :1.000   Max.   :1.000   Max.   :1.000                  
##  Customer Type_Loyal Customer Type of Travel_Business travel
##  Min.   :0.000                Min.   :0.000                 
##  1st Qu.:1.000                1st Qu.:0.000                 
##  Median :1.000                Median :1.000                 
##  Mean   :0.798                Mean   :0.692                 
##  3rd Qu.:1.000                3rd Qu.:1.000                 
##  Max.   :1.000                Max.   :1.000                 
##  Type of Travel_Personal Travel Class_Business   Class_Eco     Class_Eco Plus 
##  Min.   :0.000                  Min.   :0.00   Min.   :0.000   Min.   :0.000  
##  1st Qu.:0.000                  1st Qu.:0.00   1st Qu.:0.000   1st Qu.:0.000  
##  Median :0.000                  Median :0.00   Median :0.000   Median :0.000  
##  Mean   :0.308                  Mean   :0.49   Mean   :0.452   Mean   :0.058  
##  3rd Qu.:1.000                  3rd Qu.:1.00   3rd Qu.:1.000   3rd Qu.:0.000  
##  Max.   :1.000                  Max.   :1.00   Max.   :1.000   Max.   :1.000
# A) One Way Tablutions
# Statisfaction
tbl.1 = table(df$satisfaction)
tbl.1
## 
## dissatisfied    satisfied 
##          232          268
prop.table(tbl.1)
## 
## dissatisfied    satisfied 
##        0.464        0.536
# Gender
tbl.2 = table(df$Gender)
tbl.2
## 
## Female   Male 
##    264    236
prop.table(tbl.2)
## 
## Female   Male 
##  0.528  0.472
# Customer Type
tbl.3 = table(df$`Customer Type`)
tbl.3
## 
## disloyal Customer    Loyal Customer 
##               101               399
prop.table(tbl.3)
## 
## disloyal Customer    Loyal Customer 
##             0.202             0.798
# Type of Travel
tbl.4 = table(df$`Type of Travel`)
tbl.4
## 
## Business travel Personal Travel 
##             346             154
prop.table(tbl.4)
## 
## Business travel Personal Travel 
##           0.692           0.308
# Class
tbl.5 = table(df$Class)
tbl.5
## 
## Business      Eco Eco Plus 
##      245      226       29
prop.table(tbl.5)
## 
## Business      Eco Eco Plus 
##    0.490    0.452    0.058
# B) Measure of Central Tendencies
library(psych)
ds_table = describe(df_num)
library(DT)
datatable(ds_table)
# CORRELATION MATRIX *HEATMAP GRAPHIC, shows relationship between all variables*
library(heatmaply)
## Loading required package: plotly
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
## Loading required package: viridis
## Loading required package: viridisLite
## 
## ======================
## Welcome to heatmaply version 1.5.0
## 
## Type citation('heatmaply') for how to cite the package.
## Type ?heatmaply for the main documentation.
## 
## The github page is: https://github.com/talgalili/heatmaply/
## Please submit your suggestions and bug-reports at: https://github.com/talgalili/heatmaply/issues
## You may ask questions at stackoverflow, use the r and heatmaply tags: 
##   https://stackoverflow.com/questions/tagged/heatmaply
## ======================
heatmaply_cor(
  cor(df_num, use="pairwise.complete.obs") ,
  show_dendrogram= c(F,F)
)
#######################################
# RELATIONSHIP BETWEEN VARIABLES TESTS#
#######################################

# *Use can use these tests for any variables you want to plug in*
# *These are just some examples, not sure what exact variables we want to test* 

# A) Two Factor Variables *crosstabs*
xtable.1 = xtabs(~ Gender + Class, df)
xtable.1
##         Class
## Gender   Business Eco Eco Plus
##   Female      128 120       16
##   Male        117 106       13
prop.table(xtable.1)
##         Class
## Gender   Business   Eco Eco Plus
##   Female    0.256 0.240    0.032
##   Male      0.234 0.212    0.026
## Chi squared test will give P value
chisq.test(xtable.1)
## 
##  Pearson's Chi-squared test
## 
## data:  xtable.1
## X-squared = 0.1038, df = 2, p-value = 0.9494
# B) Numeric and Factor Variables *anova*
aggregate(Cleanliness ~ Class, df, mean)
##      Class Cleanliness
## 1 Business    3.906122
## 2      Eco    3.628319
## 3 Eco Plus    3.137931
res.aov = aov(Cleanliness ~ Class, df)
summary(res.aov)
##              Df Sum Sq Mean Sq F value   Pr(>F)    
## Class         2   20.1   10.04   8.036 0.000367 ***
## Residuals   497  621.1    1.25                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(res.aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Cleanliness ~ Class, data = df)
## 
## $Class
##                         diff        lwr         upr     p adj
## Eco-Business      -0.2778039 -0.5201699 -0.03543780 0.0199077
## Eco Plus-Business -0.7681914 -1.2842415 -0.25214132 0.0014730
## Eco Plus-Eco      -0.4903875 -1.0087285  0.02795337 0.0681645
# C) Correlation Analysis
cor.test(df$`Flight Distance`, df$`Seat comfort`, method="pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  df$`Flight Distance` and df$`Seat comfort`
## t = -2.0791, df = 498, p-value = 0.03812
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.178999532 -0.005116067
## sample estimates:
##         cor 
## -0.09276504
# D) Regression Analysis
#when changing variables, put dependant variables first
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
model.1 = lm(Cleanliness ~ Gender, data=df)
cohens_f_squared(model.1)
## For one-way between subjects designs, partial eta squared is equivalent
##   to eta squared. Returning eta squared.
## # Effect Size for ANOVA
## 
## Parameter | Cohen's f2 |      95% CI
## ------------------------------------
## Gender    |   2.94e-03 | [0.00, Inf]
## 
## - One-sided CIs: upper bound fixed at [Inf].