WILCOXON SIGNED RANK — SALES CAMPAIGN

These paired tests evaluate whether mean monthly sales for the same stores changed after the marketing campaign compared with before it.

H0: The population mean difference in sales (After − Before) equals zero; the campaign has no effect.

H1: The population mean difference in sales (After − Before) is not zero; the campaign changes sales.

############################################################
# Assignment 6 — Research Scenario 4
# Campaign: Pre vs Post Sales
# WILCOXON SIGNED-RANK TEST & NORMALITY CHECK
############################################################

# NULL HYPOTHESIS (H0)
# There is no difference between the PreCampaignSales and PostCampaignSales.

# ALTERNATE HYPOTHESIS (H1)
# There is a difference between the PreCampaignSales and PostCampaignSales.


############################
# IMPORT EXCEL FILE
############################

# Load required package
# install.packages("readxl")   # if not already installed

library(readxl)

# Import dataset (update path as needed)
dataset <- read_excel("A6R4.xlsx")


Before <- dataset$PreCampaignSales
After  <- dataset$PostCampaignSales

# Difference scores: After minus Before
Differences <- After - Before


############################
# HISTOGRAM OF SALES DIFFERENCES
############################

hist(Differences,
     main   = "Histogram of Sales Difference",
     xlab   = "After - Before",
     ylab   = "Frequency",
     col    = "blue",
     border = "black",
     breaks = 20)

# QUESTION 1: Is the histogram symmetrical, positively skewed, or negatively skewed?
# ANSWER: positive Skew

############################
# SHAPIRO-WILK NORMALITY TEST
############################

shapiro.test(Differences)
## 
##  Shapiro-Wilk normality test
## 
## data:  Differences
## W = 0.94747, p-value = 0.01186
## From the output:
##  Shapiro-Wilk normality test
##  W = 0.94747, p-value = 0.01186

# QUESTION: Was the data normally distributed or abnormally distributed?
# If p > 0.05 → NORMAL (use Dependent t-test).
# If p < 0.05 → NOT normal (use Wilcoxon Signed-Rank).
# ANSWER: The data were abnormally distributed (W = 0.947, p = 0.0119 < .05),
#         so a Wilcoxon Signed-Rank test is more appropriate than a Dependent t-test.


############################
# BOXPLOT OF SALES DIFFERENCES
############################

boxplot(Differences,
        main   = "Distribution of Sales Differences (After - Before)",
        ylab   = "Difference in Sales",
        col    = "blue",
        border = "darkblue")

# QUESTION 1: How many dots are in your boxplot?
# A) No dots.
# B) One or two dots.
# C) Many dots.
# ANSWER: B

# QUESTION 2: Where are the dots in your boxplot?
# A) There are no dots.
# B) Very close to the whiskers (lines of the boxplot).
# C) Far from the whiskers (lines of the boxplot).
# ANSWER: B

# QUESTION 3: Based on the dots and their location, is the data normal?
# ANSWER: The presence and location of outliers suggest the data deviate from normality,
#         which is consistent with the Shapiro-Wilk result (p < .05).


############################################################
# WILCOXON SIGNED-RANK TEST
############################################################

# For non-normal paired data, use Wilcoxon Signed-Rank instead of Dependent t-test.

# t.test(Before, After, paired = TRUE)  # (commented out)
wilcox.test(Before, After, paired = TRUE)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  Before and After
## V = 640, p-value = 0.0433
## alternative hypothesis: true location shift is not equal to 0
## From the Rq4 HTML output:
##  Wilcoxon signed rank test with continuity correction
##  data:  Before and After
##  V = 640, p-value = 0.0433
##  alternative hypothesis: true location shift is not equal to 0

# INTERPRETATION:
# Since p = 0.0433 < .05, there is a statistically significant difference
# between pre-campaign and post-campaign sales.


############################################################
# EFFECT SIZE (currently using Cohen’s d from effectsize output)
############################################################

# NOTE: Your Wilcoxon template usually uses Rank Biserial Correlation (wilcox_effsize).
# In the HTML for Rq4, you ran Cohen’s d instead. We’ll document that here.

# install.packages("effectsize")   # if not installed
library(effectsize)

cohens_d(Before, After, paired = TRUE)
## For paired samples, 'repeated_measures_d()' provides more options.
## Cohen's d |         95% CI
## --------------------------
## -0.32     | [-0.58, -0.06]
## From the output:
## Cohen's d |         95% CI
## --------------------------
## -0.32     | [-0.58, -0.06]

# QUESTION 1: What is the size of the effect?
# (Using Cohen’s d cutoffs:)
# ± 0.00 to 0.19 = ignore
# ± 0.20 to 0.49 = small
# ± 0.50 to 0.79 = moderate
# ± 0.80 to 1.29 = large
# ± 1.30 and above = very large
# ANSWER: Cohen’s d was -0.32, which indicates a SMALL effect size.

# QUESTION 2: Which group had the higher average score (sales)?
# With Differences = After - Before:
# - If the mean difference is positive → After > Before.
# - If the mean difference is negative → Before > After.
# Here, Cohen’s d is negative, indicating the After scores are lower than Before.
# ANSWER: PreCampaignSales (Before) were higher on average than PostCampaignSales (After).


############################################################
# SUMMARY OF RESULTS: WILCOXON SIGNED-RANK TEST
############################################################


# A Wilcoxon Signed-Rank Test was conducted to compare sales before and after the 
# marketing campaign. The results indicated a statistically significant change in sales, 
# V = 640, p = .043. Median post-campaign sales were (insert Md After) compared to 
# median pre-campaign sales of (insert Md Before), indicating that sales 
# (increased/decreased) following the campaign. The effect size (e.g., rank biserial r) 
# can then be interpreted as small, moderate, large, or very large once computed.