##########################################################################
# 統計學 (一)                                                            #
# Anderson et al., Statistics for Business & Economics (14th Edition)    #
# Chapter 2                                                              #
# 吳漢銘 國立政治大學統計學系                                            #
# https://hmwu.idv.tw/                                                   #
##########################################################################

# basic R

1 + 3
1:10
sum(1:10)
cat("Hello~")

score <- sample(0:100, 10, T)
score <- sample(x = 0:100, size = 10, replace = T)
score

m <- mean(score)
s <- sd(score)

f <- function(x){
  x ^ 2 + 1
}
f(3)

info <- data.frame(height = c(157, 180, 164),
                   weight = c(58, 76, 50),
                   gender = c("f", "m", "m"),
                   pass = c(T, F, T))
info
info[2,3]
info[2,]
info[,3]
info$height


######################################
# Section 2.1                        #
######################################
# install.packages("readxl")
library(readxl)

SoftDrink.tmp <- read_excel("data/chap02/SoftDrink.xlsx")
SoftDrink.tmp
print(SoftDrink.tmp, max = nrow(SoftDrink.tmp))

# convert a tibble object to a vector object
SoftDrink <- SoftDrink.tmp[[1]]
SoftDrink

# total
n <- length(SoftDrink)
n

# Frequency
SoftDrink.tb <- table(SoftDrink)
SoftDrink.tb

# Relative Frequency
SoftDrink.tb/n

# Percent Frequency
SoftDrink.tb/n *100

# Bar Chart
barplot(SoftDrink.tb, xlab = "Soft Drink", ylab = "Frequency", 
        main = "Bar Chart of Soft Drink Purchases")

# Pie Chart
pie(SoftDrink.tb)
pie(SoftDrink.tb, labels = paste0(names(SoftDrink.tb), ": ",  
                                 SoftDrink.tb/n *100, "%"), 
    main = "Pie Chart of Soft Drink Purchases")

# 3D Pie Chart
library(plotrix)
pie3D(SoftDrink.tb, labels = paste0(names(SoftDrink.tb), ": ",  
                                 SoftDrink.tb/n *100, "%"), 
    main = "3D Pie Chart of Soft Drink Purchases")



######################################
# Section 2.2                        #
######################################
#  Year-End Audit Times (In Days)
Audit.tmp <- read_excel("data/chap02/Audit.xlsx")
Audit.tmp
Audit <- Audit.tmp[[1]]
Audit
length(Audit)
bks <- c(9, 14, 19, 24, 29, 34)
Audit.cut <- cut(Audit, breaks = bks)
Audit.cut

# Frequency
table(Audit.cut)

# Cumulative Frequency
cumsum(table(Audit.cut))

# Dot Plot
dotchart(Audit, labels = 1:length(Audit), 
         xlab = "Audit Time (days)", main = "Dot Plot for the Audit Time Data")

# Histogram
hist(Audit, 
     xlab = "Audit Time (days)", 
     main = "Histogram for the Audit Time Data")


hist(Audit, breaks = 10,
     xlab = "Audit Time (days)", 
     main = "Histogram for the Audit Time Data")


# Stem-and-Leaf Display
AptitudeTest.tmp <- read_excel("data/chap02/AptitudeTest.xlsx")
AptitudeTest.tmp
AptitudeTest <- AptitudeTest.tmp[[1]]
AptitudeTest
length(AptitudeTest)

stem(AptitudeTest, scale = 1)
stem(AptitudeTest, scale = 2)



######################################
# Section 2.3/2.4                    #
######################################

# Crosstableation
Restaurant <- read_excel("data/chap02/Restaurant.xlsx")
dim(Restaurant)
head(Restaurant)
MealPrice <- cut(Restaurant[,3][[1]], breaks = c(9, 19, 29, 39, 49))
QualityRating <- factor(Restaurant[,2][[1]], 
                        levels = c("Good", "Very Good", "Excellent"),
                        ordered = T)
Restaurant.tb <- table(QualityRating, MealPrice)
Restaurant.tb
margin.table(Restaurant.tb, 1) 
margin.table(Restaurant.tb, 2) 


# Side-by-side Bar Chart
barplot(Restaurant.tb, beside = T, col = 2:4,
        xlab = "Meal Price", 
        main = "Side-by-side Bar Chart for the Quality and Meal Price Data")
legend("topright", legend = levels(QualityRating), 
       col = 2:4, pch = 15, bty = "n")


# Stacked Bar Chart
Restaurant.tb.percent <- apply(Restaurant.tb, 2, 
                               function(x){x*100/sum(x)})
barplot(Restaurant.tb.percent, col = 2:4,
        xlab = "Meal Price", xlim = c(0, 5.5),
        main = "Side-by-side Bar Chart for the Quality and Meal Price Data")
legend("topright", legend = levels(QualityRating), 
       col = 2:4, pch = 15, bty = "n")




######################################
# Section 2.4                        #
######################################

# Scatter plot
Electronics <- read_excel("data/chap02/Electronics.xlsx")
Electronics
names(Electronics)
Electronics$`No. of Commercials`

plot(Electronics$`No. of Commercials`, Electronics$`Sales Volume`,
     main = "Scatter Diagram for the San Francisco Electronics Store")
abline(lm(Electronics$`Sales Volume` ~ Electronics$`No. of Commercials`),
       lwd = 2, col = "blue")



