Name: Hasan Md Khalid
Student ID: M24W0391

Installing Packages and Loading Necessary Libraries

# install.packages("gridExtra") # Uncomment if not already installed
library(ggplot2)
library(gridExtra)

Loading the mtcars Dataset

data(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Dataset Overview

The mtcars dataset includes the following 11 variables:

The dataset shows wide variation in fuel efficiency, engine size, and other mechanical attributes.


Exercise 1: Handling Alpha Values in Statistical Tests

Student Name: HASAN MD KHALID
Student ID: M24W0391

# Split mpg values based on transmission type
automatic_mpg <- mtcars$mpg[mtcars$am == 0]
manual_mpg <- mtcars$mpg[mtcars$am == 1]

# Perform t-test
t_test_result <- t.test(automatic_mpg, manual_mpg)
p_value <- t_test_result$p.value
alpha <- 0.05

# Print results
print(t_test_result)
## 
##  Welch Two Sample t-test
## 
## data:  automatic_mpg and manual_mpg
## t = -3.7671, df = 18.332, p-value = 0.001374
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -11.280194  -3.209684
## sample estimates:
## mean of x mean of y 
##  17.14737  24.39231
# Interpret result
if (p_value < alpha) {
  cat("Result is statistically significant. We reject the null hypothesis.\n")
} else {
  cat("Result is not statistically significant. We fail to reject the null hypothesis.\n")
}
## Result is statistically significant. We reject the null hypothesis.

Analysis:
The p-value (≈ 0.0014) is less than 0.05, so we reject the null hypothesis. Manual cars show significantly better fuel efficiency (mpg) than automatic cars.


Exercise 2: Multiple Comparisons and Bonferroni Control

Student Name: HASAN MD KHALID
Student ID: M24W0391

T-Tests for MPG by Cylinder Group

# Unique cylinder groups
cylinder_groups <- unique(mtcars$cyl)

# Initialize results dataframe
mpg_test_results <- data.frame()
overall_mean_mpg <- mean(mtcars$mpg)

# Run t-tests
for (cyl in cylinder_groups) {
  group_data <- mtcars$mpg[mtcars$cyl == cyl]
  if (length(group_data) > 1) {
    t_test <- t.test(group_data, mu = overall_mean_mpg, alternative = "two.sided")
    mpg_test_results <- rbind(mpg_test_results, data.frame(
      CylinderGroup = cyl,
      P_Value = t_test$p.value
    ))
  }
}

# Bonferroni adjustment
mpg_test_results$Adjusted_P_Value <- p.adjust(mpg_test_results$P_Value, method = "bonferroni")
print(mpg_test_results)
##   CylinderGroup      P_Value Adjusted_P_Value
## 1             6 5.500829e-01     1.000000e+00
## 2             4 6.877132e-04     2.063140e-03
## 3             8 6.055524e-06     1.816657e-05

T-Tests for HP by Cylinder Group

# Initialize results dataframe
hp_test_results <- data.frame()
overall_mean_hp <- mean(mtcars$hp)

# Run t-tests
for (cyl in cylinder_groups) {
  group_data <- mtcars$hp[mtcars$cyl == cyl]
  if (length(group_data) > 1) {
    t_test <- t.test(group_data, mu = overall_mean_hp, alternative = "two.sided")
    hp_test_results <- rbind(hp_test_results, data.frame(
      CylinderGroup = cyl,
      P_Value = t_test$p.value
    ))
  }
}

# Bonferroni adjustment
hp_test_results$Adjusted_P_Value <- p.adjust(hp_test_results$P_Value, method = "bonferroni")
print(hp_test_results)
##   CylinderGroup      P_Value Adjusted_P_Value
## 1             6 3.746594e-02     1.123978e-01
## 2             4 1.389669e-06     4.169007e-06
## 3             8 5.073765e-04     1.522129e-03

Bar Plots of Adjusted P-Values

# Bar plot for mpg results
mpg_plot <- ggplot(mpg_test_results, aes(x = factor(CylinderGroup), y = Adjusted_P_Value)) +
  geom_bar(stat = "identity", fill = "blue") +
  coord_flip() +
  labs(title = "Adjusted P-Values for 'mpg' T-Tests",
       x = "Cylinder Group", y = "Adjusted P-Value")

# Bar plot for hp results
hp_plot <- ggplot(hp_test_results, aes(x = factor(CylinderGroup), y = Adjusted_P_Value)) +
  geom_bar(stat = "identity", fill = "green") +
  coord_flip() +
  labs(title = "Adjusted P-Values for 'hp' T-Tests",
       x = "Cylinder Group", y = "Adjusted P-Value")

# Display both plots
grid.arrange(mpg_plot, hp_plot, ncol = 2)


Conclusion

Student Name: HASAN MD KHALID
Student ID: M24W0391