#Case Introduction:

In an effort to understand the potential influence of discounts on customer spending behavior, e-commerce platform has implemented a strategic initiative and assigned discounts to a subset of customers, regardless of their membership type.

##Hypothesis:

Null Hypothesis (H0): There is no significant difference in spending between customers who received a discount and those who did not.

Alternative Hypothesis (H1): There is a significant difference in spending between customers who received a discount and those who did not.

###Data source: https://www.kaggle.com/datasets/uom190346a/e-commerce-customer-behavior-dataset/data

#importing data
data <- read_csv("~/Desktop/E-commerce Customer Behavior - Sheet1.csv")
## Rows: 350 Columns: 11
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): Gender, City, Membership Type, Satisfaction Level
## dbl (6): Customer ID, Age, Total Spend, Items Purchased, Average Rating, Day...
## lgl (1): Discount Applied
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#reviewing first 1o rows
head(data)
## # A tibble: 6 × 11
##   `Customer ID` Gender   Age City          `Membership Type` `Total Spend`
##           <dbl> <chr>  <dbl> <chr>         <chr>                     <dbl>
## 1           101 Female    29 New York      Gold                      1120.
## 2           102 Male      34 Los Angeles   Silver                     780.
## 3           103 Female    43 Chicago       Bronze                     511.
## 4           104 Male      30 San Francisco Gold                      1480.
## 5           105 Male      27 Miami         Silver                     720.
## 6           106 Female    37 Houston       Bronze                     441.
## # ℹ 5 more variables: `Items Purchased` <dbl>, `Average Rating` <dbl>,
## #   `Discount Applied` <lgl>, `Days Since Last Purchase` <dbl>,
## #   `Satisfaction Level` <chr>

Description:

#Random data sampling
set.seed(1)
rawdata <- data[sample(nrow(data),150),] #ramdomly selecting 150 samples out of 350
#Use the describe() function to obtain a comprehensive statistical summary
describe (rawdata)
## Warning in FUN(newX[, i], ...): no non-missing arguments to min; returning Inf
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf
##                          vars   n   mean     sd median trimmed    mad   min
## Customer ID                 1 150 276.12 103.34  271.0  276.02 132.69 101.0
## Gender*                     2 150   1.48   0.50    1.0    1.47   0.00   1.0
## Age                         3 150  33.75   4.73   33.0   33.45   4.45  26.0
## City*                       4 150   3.51   1.74    3.0    3.52   2.97   1.0
## Membership Type*            5 150   1.96   0.80    2.0    1.95   1.48   1.0
## Total Spend                 6 150 856.65 368.31  785.2  832.52 466.57 410.8
## Items Purchased             7 150  12.69   4.16   12.0   12.38   4.45   7.0
## Average Rating              8 150   4.02   0.60    4.1    4.04   0.74   3.0
## Discount Applied            9 150    NaN     NA     NA     NaN     NA   Inf
## Days Since Last Purchase   10 150  25.43  12.55   21.5   24.11  12.60   9.0
## Satisfaction Level*        11 149   1.98   0.78    2.0    1.98   1.48   1.0
##                             max  range  skew kurtosis    se
## Customer ID               449.0  348.0  0.03    -1.31  8.44
## Gender*                     2.0    1.0  0.08    -2.01  0.04
## Age                        43.0   17.0  0.54    -0.69  0.39
## City*                       6.0    5.0  0.00    -1.35  0.14
## Membership Type*            3.0    2.0  0.07    -1.45  0.07
## Total Spend              1500.1 1089.3  0.45    -1.26 30.07
## Items Purchased            21.0   14.0  0.55    -0.88  0.34
## Average Rating              4.9    1.9 -0.20    -1.32  0.05
## Discount Applied           -Inf   -Inf    NA       NA    NA
## Days Since Last Purchase   61.0   52.0  0.79    -0.24  1.02
## Satisfaction Level*         3.0    2.0  0.03    -1.38  0.06

Explanation of ststistical summary (only relevant variables):

Age: The age distribution exhibits a slight right-skewness, characterized by a mean of 33.75 and a median of 33.0. The range of ages spans from 17 to 43, indicating variability across the dataset. The standard deviation of 4.73 suggests that around 68% of the data falls within the range of 29.02 to 38.48.

Total Spend: The total spend distribution displays a right-skewed pattern, with a mean of 856.65 and a median of 785.2. The spending range extends from 410.8 to 1500.1, highlighting considerable variability. The standard deviation of 368.31 indicates that approximately 68% of the data falls within the range of 488.34 to 1224.96.

Items Purchased: The average number of items purchased is 12.69, with a moderate standard deviation of 4.16. The distribution is right-skewed, evidenced by a skewness value of 0.55. The range of items purchased spans from 7.0 to 21.0, indicating the region where roughly 68% of the data lies.

Average Rating: The average rating is 4.02, with a relatively small standard deviation of 0.60. The distribution appears fairly symmetrical, with a skewness value of -0.20. The range of ratings spans from 3.0 to 4.9, capturing approximately 68% of the data.

Days Since Last Purchase: On average, customers make a purchase every 25.43 days, with a standard deviation of 12.55. The distribution is right-skewed (skewness = 0.79), and the range of days since the last purchase spans from 9.0 to 61.0. Approximately 68% of the data falls within the range of 12.88 to 38.98 days.

#Exploring structure of dataset
str(rawdata)
## tibble [150 × 11] (S3: tbl_df/tbl/data.frame)
##  $ Customer ID             : num [1:150] 424 267 229 399 370 287 407 185 377 430 ...
##  $ Gender                  : chr [1:150] "Female" "Male" "Female" "Female" ...
##  $ Age                     : num [1:150] 30 26 43 36 31 31 35 31 33 31 ...
##  $ City                    : chr [1:150] "New York" "Miami" "Chicago" "Houston" ...
##  $ Membership Type         : chr [1:150] "Gold" "Silver" "Bronze" "Bronze" ...
##  $ Total Spend             : num [1:150] 1191 691 501 470 1141 ...
##  $ Items Purchased         : num [1:150] 16 12 10 8 15 15 12 15 11 15 ...
##  $ Average Rating          : num [1:150] 4.5 3.9 3.3 3 4.5 4.5 4.3 4.6 4.2 4.5 ...
##  $ Discount Applied        : logi [1:150] TRUE TRUE TRUE FALSE TRUE TRUE ...
##  $ Days Since Last Purchase: num [1:150] 27 47 40 22 30 26 16 27 18 35 ...
##  $ Satisfaction Level      : chr [1:150] "Satisfied" "Unsatisfied" "Unsatisfied" "Neutral" ...
#rename column
colnames(rawdata)[9]="Discount_Applied"
colnames(rawdata)[1]="ID"
colnames(rawdata)[6]="Total_Spend"

#data manipulation.changing format
rawdata$Discount_F <- factor(rawdata$Discount_Applied,
                             levels = c(FALSE, TRUE),
                             labels = c("No", "Yes"))
#visualizing the data distribution through plotting for visual inspection
Discount_no <- ggplot(rawdata[rawdata$Discount_F=="No",  ], aes(x = Total_Spend)) +
  theme_linedraw() + 
  geom_histogram(binwidth = 30) +  
  ggtitle("Not Discount")

Discount_yes <- ggplot(rawdata[rawdata$Discount_F=="Yes",  ], aes(x = Total_Spend)) +
  theme_linedraw() + 
  geom_histogram(binwidth = 30) +  
  ggtitle("Discount")

#arranging two ggplots in a single plot grid
ggarrange(Discount_no, Discount_yes,
          ncol= 2, nrow = 1) 

#result: Distribution is not normal
#checking for outliers
ggqqplot(rawdata,
         "Total_Spend",
         facet.by = "Discount_F") #The plot exhibits outliers

#checking for normal distribution with Shapiro test. 
#assumption: 
#null Hypothesis (H0): The data follows a normal distribution. 
#alternative: The data is not normally distributed
rawdata%>%
  group_by(Discount_F) %>%
  shapiro_test(Total_Spend) #Data is not normally distributed. We have to do non-parametric test.Wilcoxon Rank Sum Test
## # A tibble: 2 × 4
##   Discount_F variable    statistic             p
##   <fct>      <chr>           <dbl>         <dbl>
## 1 No         Total_Spend     0.809 0.0000000121 
## 2 Yes        Total_Spend     0.784 0.00000000662
#paramentric test is done solely in order to meet HW requirements 
t.test(rawdata$Total_Spend ~ rawdata$Discount_F, 
       paired = FALSE,
       var.equal = FALSE,
       alternative = "two.sided") #This result is not reliable, switching to non-parametric test
## 
##  Welch Two Sample t-test
## 
## data:  rawdata$Total_Spend by rawdata$Discount_F
## t = 1.5726, df = 139.05, p-value = 0.1181
## alternative hypothesis: true difference in means between group No and group Yes is not equal to 0
## 95 percent confidence interval:
##  -23.91748 209.85840
## sample estimates:
##  mean in group No mean in group Yes 
##          901.2795          808.3090
#wilcoxon Rank Sum Test (Mann-Whitney U-test)
result <- wilcox.test(Total_Spend ~ Discount_F, data = rawdata,
                      paired=FALSE,
                      correct= FALSE,
                      exact=FALSE,
                   alternative="two.sided")
result #print results
## 
##  Wilcoxon rank sum test
## 
## data:  Total_Spend by Discount_F
## W = 3016, p-value = 0.4338
## alternative hypothesis: true location shift is not equal to 0
#find effect size
effectsize (wilcox.test(rawdata$Total_Spend ~ rawdata$Discount_F,
                        paired= FALSE,
                        correct=FALSE,
                        exact=FALSE,
                        alternative="two.sided"))
## r (rank biserial) |        95% CI
## ---------------------------------
## 0.07              | [-0.11, 0.25]
interpret_rank_biserial(0.07)
## [1] "very small"
## (Rules: funder2019)

#Concluding Remarks: The effect size of 0.07, along with the p-value of 0.01181 from the Wilcoxon rank sum test, indicates that there is not enough evidence to reject the null hypothesis. While the p-value is greater than the conventional significance level of 0.05, suggesting a lack of statistical significance. The effect size of 0.07 suggests a small to negligible practical significance in the difference in spending between customers who received a discount and those who did not. Therefore, the results suggest that, while there may be a subtle difference in spending behavior, it is not practically significant in this context.