# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Lab 2: Descriptive Statistics                                             ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# This script includes the basic descriptive analysis of the data set cars.txt

# Each variable requires a different description, depending on whether it is
# quantitative or qualitative.

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Import the Data        ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Read the data (the first row contains variable names)
cars <- read.table(file.path("data", "cars.txt"), header = TRUE)
head(cars)

# Use the $ symbol to select a variable
price <- cars$Price
acc <- cars$Acceleration
year <- cars$Year
brand <- cars$Brand

# Price and acceleration are quantitative variables, while year and brand are
# qualitative. Specifically, year is ordinal and brand is nominal.

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Qualitative Variables  ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~

# The descriptive statistics for a qualitative variable include a frequency
# table and a barplot (or pie chart).

# Frequency table
year_freq <- table(year)
year_freq
brand_freq <- table(brand)
brand_freq

# Bar plot
barplot(year_freq,
        main = "Year of Release",
        xlab = "Number of Cars",
        col = "black")

barplot(brand_freq,
        main = "Brands",
        xlab = "Number of Cars",
        names.arg = c("BMW", "Datsun", "Mercedes-Benz"),
        col = "black",
        width = c(2, 2),
        xlim = c (0, 10))

# Pie chart
pie(year_freq,
    main = "Year of Release",
    col = c("grey10", "grey20", "grey30", "grey40",
            "grey50", "grey60", "grey70", "grey80"))

pie(brand_freq,
    main = "Brands",
    labels = c("BMW", "Datsun", "Mercedes-Benz"),
    col = c("grey50", "grey70", "grey100"))

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Quantitative Variables ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~

# The descriptive statistics of a quantitative variable depend on whether its
# population can be reasonably described by the Normal distribution or not.

# Normality Check
  # Plots: Histogram, Boxplot, Q-Q Plot
  # Test: Shapiro-Wilk or Kolmogorov-Smirnov

# Descriptives for Normal Distributed Variables
  # Plots: Histogram
  # Measures of Position: mean
  # Measures of Dispersion: standard deviation

# Descriptives for Non-Normal Distributed Variables
  # Plots: Histogram
  # Measures of Position: median
  # Measures of Dispersion: min, max, quantiles


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Normality Check        ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Plots: Divide display in 2 columns
par(mfrow = c(1,2))

# Histogram
hist(price, main = "Price", breaks = 10, col = "#AAAAAA", freq = FALSE)
lines(density(price))

hist(acc, main = "Acceleration", breaks = 20, col = "#AAAAAA", freq = FALSE)
lines(density(acc))

# Box plot
boxplot(price, main = "Price")
boxplot(acc, main = "Acceleration")

# Q-Q Plot
qqnorm(price)
qqline(price)

qqnorm(acc)
qqline(acc)

# Tests

# Shapiro - Wilk
  # H0: Normal Distributed
  # H1: Every Other Alternative
shapiro.test(price)
shapiro.test(acc)

sw_result <- shapiro.test(price)
p_value <- sw_result$p.value

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Descriptives           ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~

# For acceleration (Normal Distributed)

# Mean
mu <- mean(acc)
mu
# or
n <- length(acc)
mu <- sum(acc) / n
mu

# Variance
s2 <- var(acc)
s2
# or
s2 <- sum((acc - mu) ^ 2) / (n - 1)
s2

# Standard Deviation
s <- sd(acc)
s
# or
s <- sqrt(s2)
s

# For price (Non-Normal Distributed)

# Median
median(price)
# or (for n odd number)
a <- sort(price)
a[ceiling(n / 2)]

# Quantiles
quantile(price)
