# INDEPENDENT T-TEST & MANN-WHITNEY U TEST

# HYPOTHESIS TESTED:
# To determine if Medication A is more effective at reducing headaches than the current medication on the market (Medication B).

# H0:There is no difference in the number of headaches between the group taking Medication A and the group taking Medication B.
# H1: There is a difference in the number of headaches between the group taking Medication A and the group taking Medication B.


options(repos = c(CRAN = "https://cloud.r-project.org"))

install.packages("readxl")
## Installing package into 'C:/Users/mnava/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'readxl' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\mnava\AppData\Local\Temp\Rtmp8eTFrs\downloaded_packages
library(readxl)

A6R1 <- read_excel("D:/Ms Analytics 2025/Fall 1/Applied Analytics &Methods 1/Week 6/A6R1.xlsx")


# DESCRIPTIVE STATISTICS

install.packages("dplyr")#
## Installing package into 'C:/Users/mnava/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'dplyr' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\mnava\AppData\Local\Temp\Rtmp8eTFrs\downloaded_packages
# LOAD THE PACKAGE

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# CALCULATE THE DESCRIPTIVE STATISTICS

A6R1 %>%
  group_by(Medication) %>%
  summarise(
    Mean = mean(HeadacheDays, na.rm = TRUE),
    Median = median(HeadacheDays, na.rm = TRUE),
    SD = sd(HeadacheDays, na.rm = TRUE),
    N = n()
  )
## # A tibble: 2 × 5
##   Medication  Mean Median    SD     N
##   <chr>      <dbl>  <dbl> <dbl> <int>
## 1 A            8.1    8    2.81    50
## 2 B           12.6   12.5  3.59    50
# HISTOGRAMS

hist(A6R1$HeadacheDays[A6R1$Medication == "A"],
     main = "Histogram Medication A",
     xlab = "Value",
     ylab = "Frequency",
     col = "lightblue",
     border = "black",
     breaks = 20)

hist(A6R1$HeadacheDays[A6R1$Medication == "B"],
     main = "Histogram of medication B",
     xlab = "Value",
     ylab = "Frequency",
     col = "lightgreen",
     border = "black",
     breaks = 20)

# 1)The SKEWNESS of the VARIABLE 1 histogram.
# The histogram of VARIABLE 1 (Group 1 Scores) appears fairly symmetrical with a single peak.
# Therefore, it is likely to have skewness close to zero and looks symmetrical.

# 2 The KURTOSIS of the VARIABLE 1 histogram.
# The histogram has a moderate peak and tails, suggesting a mesokurtic distribution.
# It resembles a proper bell curve.

# 3 The SKEWNESS of the VARIABLE 2 histogram.
# The histogram of VARIABLE 2 (Group 2 Scores) shows multiple peaks and a slight right tail.
# This suggests it is slightly positively skewed.

# 4 The KURTOSIS of the VARIABLE 2 histogram.
# The histogram has sharper peaks and heavier tails, indicating a leptokurtic distribution.
# It looks too tall compared to a normal bell curve.

# SHAPIRO-WILK TEST
# Purpose: Check the normality for each group's score statistically.
# The Shapiro-Wilk Test is a test that checks skewness and kurtosis at the same time.
# The test is checking "Is this variable the SAME as normal data (null hypothesis) or DIFFERENT from normal data (alternate hypothesis)?"
# For this test, if p is GREATER than .05 (p > .05), the data is NORMAL.
# If p is LESS than .05 (p < .05), the data is NOT normal.


shapiro.test(A6R1$HeadacheDays[A6R1$Medication == "A"])
## 
##  Shapiro-Wilk normality test
## 
## data:  A6R1$HeadacheDays[A6R1$Medication == "A"]
## W = 0.97852, p-value = 0.4913
shapiro.test(A6R1$HeadacheDays[A6R1$Medication == "B"])
## 
##  Shapiro-Wilk normality test
## 
## data:  A6R1$HeadacheDays[A6R1$Medication == "B"]
## W = 0.98758, p-value = 0.8741
#  The data normally distributed for Variable 1
#  The data normally distributed for Variable 2

# BOXPLOT
install.packages("ggplot2")#
## Installing package into 'C:/Users/mnava/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'ggplot2' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\mnava\AppData\Local\Temp\Rtmp8eTFrs\downloaded_packages
install.packages("ggpubr")#
## Installing package into 'C:/Users/mnava/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'ggpubr' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\mnava\AppData\Local\Temp\Rtmp8eTFrs\downloaded_packages
# LOAD THE PACKAGE
library(ggplot2)

library(ggpubr)

ggboxplot(A6R1, x = "Medication", y = "HeadacheDays",
          color = "Medication",
          palette = "jco",
          add = "jitter")

# Q1) Were there any dots outside of the boxplots? These dots represent participants with extreme scores.
# Yes, both boxplots (for Medication A and B) show several dots outside the boxes, indicating the presence of outliers.

# Q2) If there are outliers, in your opinion are the scores of those dots changing the mean so much that the mean no longer accurately represents the average score?
# Although there are outliers in both groups, they are relatively few compared to the total number of data points.
# Therefore, the mean still appears to be a reasonably accurate representation of the average score.

# If there were no extreme outliers, this means the data is NORMAL. Continue to the Independent t-test.


# INDEPENDENT T-TEST 
#==========================================================

# PURPOSE: Test if there was a difference between the means of the two groups.

t.test(HeadacheDays ~ Medication, data = A6R1, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  HeadacheDays by Medication
## t = -6.9862, df = 98, p-value = 3.431e-10
## alternative hypothesis: true difference in means between group A and group B is not equal to 0
## 95 percent confidence interval:
##  -5.778247 -3.221753
## sample estimates:
## mean in group A mean in group B 
##             8.1            12.6
# DETERMINE STATISTICAL SIGNIFICANCE

install.packages("effectsize")
## Installing package into 'C:/Users/mnava/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'effectsize' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\mnava\AppData\Local\Temp\Rtmp8eTFrs\downloaded_packages
library(effectsize)


# COHEN’S D

cohen_d_result <- cohens_d(HeadacheDays ~ Medication, data = A6R1, pooled_sd = TRUE)
print(cohen_d_result)
## Cohen's d |         95% CI
## --------------------------
## -1.40     | [-1.83, -0.96]
## 
## - Estimated using pooled SD.
# Q1) What is the size of the effect?

#  very large =-1.40 indicates the difference between the group A&B

# Q2) Group B had the higher average score?


# WRITTEN REPORT FOR INDEPENDENT T-TEST

#  OUTPUT
#    1. The name of the inferential test used (Independent t-test)
#    2. The names of the IV- Medication and DV-HeadacheDays 
#    3. The sample size for each group =50
#    4. The inferential test results were statistically significant (p < .05) 
#    5. mean 8.1 and SD 2.81 for A
#       mean 12.6 and SD 3.59 for B
#    7. Degrees of freedom =98
#    8. t-value = -6.9862
#    9. EXACT p-value to three decimals= p < .001
#   10. Effect size (Cohen’s d) =-1.40

# REPORT

# An Independent t-test was conducted to compare 
# the number of headache days between participants who took Medication A (n = 50) and those who took Medication B (n = 50). 
# Participants taking Medication A reported significantly fewer headache days (M = 8.10, SD = 2.81) than 
# those taking Medication B (M = 12.60, SD = 3.59), t(98) = -6.99, p < .001.
# The effect size was very large (d = -1.40), indicating a substantial difference in headache reduction between the two medications.
# Overall, Medication A was more effective at reducing headache days than Medication B..