# Exercise 4.2
# Ann Michael
# April 6, 2018

# Create an R script that computes the measures of central tendency and measures of variability and 
# the relationships for each of the seven variables in the attitude dataset. 
# Use the functions below:
# mean, median, mode, max, min, range, quantile, IQR, var( ), sd( ), and cor( )

# Check your work by using the summary and/or describe functions.

setwd("~/Dropbox (Personal)/NYU MSBA/MyData2")

mean(attitude$rating)
## [1] 64.63333
mean(attitude$complaints)
## [1] 66.6
mean(attitude$privileges)
## [1] 53.13333
mean(attitude$learning)
## [1] 56.36667
mean(attitude$raises)
## [1] 64.63333
mean(attitude$critical)
## [1] 74.76667
mean(attitude$advance)
## [1] 42.93333
median(attitude$rating)
## [1] 65.5
median(attitude$complaints)
## [1] 65
median(attitude$privileges)
## [1] 51.5
median(attitude$learning)
## [1] 56.5
median(attitude$raises)
## [1] 63.5
median(attitude$critical)
## [1] 77.5
median(attitude$advance)
## [1] 41
modeforrating <- table(as.vector(attitude$rating))
names(modeforrating)[modeforrating == max(modeforrating)] 
## [1] "43" "50" "63" "64" "65" "67" "71" "81"
modeforcomplaints <- table(as.vector(attitude$complaints))
names(modeforcomplaints)[modeforcomplaints == max(modeforcomplaints)] 
## [1] "60" "61" "70" "75" "77" "82" "85"
modeforprivileges <- table(as.vector(attitude$privileges))
names(modeforprivileges)[modeforprivileges == max(modeforprivileges)] 
## [1] "42"
modeforlearning <- table(as.vector(attitude$learning))
names(modeforlearning)[modeforlearning == max(modeforlearning)] 
## [1] "39" "45" "47" "54" "58" "69" "72"
modeforraises <- table(as.vector(attitude$raises))
names(modeforraises)[modeforraises == max(modeforraises)] 
## [1] "66"
modeforcritical <- table(as.vector(attitude$critical))
names(modeforcritical)[modeforcritical == max(modeforcritical)] 
## [1] "80"
modeforadvance <- table(as.vector(attitude$advance))
names(modeforadvance)[modeforadvance == max(modeforadvance)] 
## [1] "41"
max(attitude$rating)
## [1] 85
max(attitude$complaints)
## [1] 90
max(attitude$privileges)
## [1] 83
max(attitude$learning)
## [1] 75
max(attitude$raises)
## [1] 88
max(attitude$critical)
## [1] 92
max(attitude$advance)
## [1] 72
min(attitude$rating)
## [1] 40
min(attitude$complaints)
## [1] 37
min(attitude$privileges)
## [1] 30
min(attitude$learning)
## [1] 34
min(attitude$raises)
## [1] 43
min(attitude$critical)
## [1] 49
min(attitude$advance)
## [1] 25
range(attitude$rating)
## [1] 40 85
range(attitude$complaints)
## [1] 37 90
range(attitude$privileges)
## [1] 30 83
range(attitude$learning)
## [1] 34 75
range(attitude$raises)
## [1] 43 88
range(attitude$critical)
## [1] 49 92
range(attitude$advance)
## [1] 25 72
quantile(attitude$rating)
##    0%   25%   50%   75%  100% 
## 40.00 58.75 65.50 71.75 85.00
quantile(attitude$complaints)
##   0%  25%  50%  75% 100% 
## 37.0 58.5 65.0 77.0 90.0
quantile(attitude$privileges)
##   0%  25%  50%  75% 100% 
## 30.0 45.0 51.5 62.5 83.0
quantile(attitude$learning)
##    0%   25%   50%   75%  100% 
## 34.00 47.00 56.50 66.75 75.00
quantile(attitude$raises)
##    0%   25%   50%   75%  100% 
## 43.00 58.25 63.50 71.00 88.00
quantile(attitude$critical)
##    0%   25%   50%   75%  100% 
## 49.00 69.25 77.50 80.00 92.00
quantile(attitude$advance)
##    0%   25%   50%   75%  100% 
## 25.00 35.00 41.00 47.75 72.00
IQR(attitude$rating)
## [1] 13
IQR(attitude$complaints)
## [1] 18.5
IQR(attitude$privileges)
## [1] 17.5
IQR(attitude$learning)
## [1] 19.75
IQR(attitude$raises)
## [1] 12.75
IQR(attitude$critical)
## [1] 10.75
IQR(attitude$advance)
## [1] 12.75
var(attitude$rating)
## [1] 148.1713
var(attitude$complaints)
## [1] 177.2828
var(attitude$privileges)
## [1] 149.7057
var(attitude$learning)
## [1] 137.7575
var(attitude$raises)
## [1] 108.1023
var(attitude$critical)
## [1] 97.9092
var(attitude$advance)
## [1] 105.8575
sd(attitude$rating)
## [1] 12.17256
sd(attitude$complaints)
## [1] 13.31476
sd(attitude$privileges)
## [1] 12.23543
sd(attitude$learning)
## [1] 11.73701
sd(attitude$raises)
## [1] 10.39723
sd(attitude$critical)
## [1] 9.894908
sd(attitude$advance)
## [1] 10.28871
# check work using summary function

summary(attitude)
##      rating        complaints     privileges       learning    
##  Min.   :40.00   Min.   :37.0   Min.   :30.00   Min.   :34.00  
##  1st Qu.:58.75   1st Qu.:58.5   1st Qu.:45.00   1st Qu.:47.00  
##  Median :65.50   Median :65.0   Median :51.50   Median :56.50  
##  Mean   :64.63   Mean   :66.6   Mean   :53.13   Mean   :56.37  
##  3rd Qu.:71.75   3rd Qu.:77.0   3rd Qu.:62.50   3rd Qu.:66.75  
##  Max.   :85.00   Max.   :90.0   Max.   :83.00   Max.   :75.00  
##      raises         critical        advance     
##  Min.   :43.00   Min.   :49.00   Min.   :25.00  
##  1st Qu.:58.25   1st Qu.:69.25   1st Qu.:35.00  
##  Median :63.50   Median :77.50   Median :41.00  
##  Mean   :64.63   Mean   :74.77   Mean   :42.93  
##  3rd Qu.:71.00   3rd Qu.:80.00   3rd Qu.:47.75  
##  Max.   :88.00   Max.   :92.00   Max.   :72.00
cor(attitude$rating, attitude$raises)
## [1] 0.590139
cor(attitude$rating, attitude$complaints)
## [1] 0.8254176
cor(attitude$rating, attitude$privileges)
## [1] 0.4261169
cor(attitude$rating, attitude$learning)
## [1] 0.6236782
cor(attitude$rating, attitude$critical)
## [1] 0.1564392
cor(attitude$rating, attitude$advance)
## [1] 0.1550863
cor(attitude$complaints, attitude$raises)
## [1] 0.6691975
cor(attitude$complaints, attitude$privileges)
## [1] 0.5582882
cor(attitude$complaints, attitude$learning)
## [1] 0.5967358
cor(attitude$complaints, attitude$critical)
## [1] 0.1877143
cor(attitude$complaints, attitude$advance)
## [1] 0.2245796
cor(attitude$privileges, attitude$raises)
## [1] 0.4454779
cor(attitude$privileges, attitude$learning)
## [1] 0.493331
cor(attitude$privileges, attitude$critical)
## [1] 0.1472331
cor(attitude$privileges, attitude$advance)
## [1] 0.3432934
cor(attitude$raises, attitude$learning)
## [1] 0.6403144
cor(attitude$raises, attitude$critical)
## [1] 0.376883
cor(attitude$raises, attitude$advance)
## [1] 0.5741862
cor(attitude$learning, attitude$critical)
## [1] 0.1159652
cor(attitude$learning, attitude$advance)
## [1] 0.5316198
cor(attitude$advance, attitude$critical)
## [1] 0.2833432
# Some correlation coefficients are more meaningful than others
# The correlation between Rating and Complaints is .825, which seems logical
# The balance of the values are lower than .7 and many are lower than .4.