#The introduction of Black Friday as a major sales event can be attributed to retailers who saw it as a way to attract customers with deeply discounted prices, leading to a surge in sales and marking the beginning of the holiday shopping season. Over the years, Black Friday has grown in scale and popularity, with retailers and consumers alike eagerly anticipating the deals and promotions offered on this day. Black Friday sales typically occur on the Friday following Thanksgiving Day in the United States, which falls on the fourth Thursday of November. So, Black Friday usually takes place in late November each year.


 getwd()
## [1] "C:/Users/HP/Desktop"
setwd("C:/Users/HP/Desktop")
train <- read.csv("train.csv", header = TRUE)

# Check if the dataset is loaded properly
str(train)
## 'data.frame':    550068 obs. of  12 variables:
##  $ User_ID                   : int  1000001 1000001 1000001 1000001 1000002 1000003 1000004 1000004 1000004 1000005 ...
##  $ Product_ID                : chr  "P00069042" "P00248942" "P00087842" "P00085442" ...
##  $ Gender                    : chr  "F" "F" "F" "F" ...
##  $ Age                       : chr  "0-17" "0-17" "0-17" "0-17" ...
##  $ Occupation                : int  10 10 10 10 16 15 7 7 7 20 ...
##  $ City_Category             : chr  "A" "A" "A" "A" ...
##  $ Stay_In_Current_City_Years: chr  "2" "2" "2" "2" ...
##  $ Marital_Status            : int  0 0 0 0 0 0 1 1 1 1 ...
##  $ Product_Category_1        : int  3 1 12 12 8 1 1 1 1 8 ...
##  $ Product_Category_2        : int  NA 6 NA 14 NA 2 8 15 16 NA ...
##  $ Product_Category_3        : int  NA 14 NA NA NA NA 17 NA NA NA ...
##  $ Purchase                  : int  8370 15200 1422 1057 7969 15227 19215 15854 15686 7871 ...
data <- read.csv("train.csv")

# Dimensions of the dataset
dim(data)
## [1] 550068     12
# Column names and data types
str(data)
## 'data.frame':    550068 obs. of  12 variables:
##  $ User_ID                   : int  1000001 1000001 1000001 1000001 1000002 1000003 1000004 1000004 1000004 1000005 ...
##  $ Product_ID                : chr  "P00069042" "P00248942" "P00087842" "P00085442" ...
##  $ Gender                    : chr  "F" "F" "F" "F" ...
##  $ Age                       : chr  "0-17" "0-17" "0-17" "0-17" ...
##  $ Occupation                : int  10 10 10 10 16 15 7 7 7 20 ...
##  $ City_Category             : chr  "A" "A" "A" "A" ...
##  $ Stay_In_Current_City_Years: chr  "2" "2" "2" "2" ...
##  $ Marital_Status            : int  0 0 0 0 0 0 1 1 1 1 ...
##  $ Product_Category_1        : int  3 1 12 12 8 1 1 1 1 8 ...
##  $ Product_Category_2        : int  NA 6 NA 14 NA 2 8 15 16 NA ...
##  $ Product_Category_3        : int  NA 14 NA NA NA NA 17 NA NA NA ...
##  $ Purchase                  : int  8370 15200 1422 1057 7969 15227 19215 15854 15686 7871 ...
#2 Missing Values Handling
#How many missing values are there in each column?
#Remove or impute missing values as necessary.


# Check for missing values
colSums(is.na(data))
##                    User_ID                 Product_ID 
##                          0                          0 
##                     Gender                        Age 
##                          0                          0 
##                 Occupation              City_Category 
##                          0                          0 
## Stay_In_Current_City_Years             Marital_Status 
##                          0                          0 
##         Product_Category_1         Product_Category_2 
##                          0                     173638 
##         Product_Category_3                   Purchase 
##                     383247                          0
# Remove rows with missing values
data <- na.omit(data)
#3Summary Statistics
#Calculate the average Purchase amount.
#Calculate the average Purchase amount by Gender

# Average Purchase
mean(data$Purchase)
## [1] 11658.11
# Average Purchase by Gender
aggregate(Purchase ~ Gender, data = data, FUN = mean)
##   Gender Purchase
## 1      F 11084.72
## 2      M 11824.92
#4 Marital Status Analysis
#What is the distribution of Marital_Status in the dataset?
#Explore the average Purchase amount by marital status.
# Distribution of Marital_Status
table(data$Marital_Status)
## 
##     0     1 
## 99619 67202
# Average Purchase by Marital_Status
aggregate(Purchase ~ Marital_Status, data = data, FUN = mean)
##   Marital_Status Purchase
## 1              0  11638.9
## 2              1  11686.6
# Boxplot of Purchase by Marital Status
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
ggplot(data, aes(x = factor(Marital_Status), y = Purchase)) +
  geom_boxplot(fill = "skyblue") +
  labs(title = "Purchase by Marital Status", x = "Marital Status", y = "Purchase")

# Boxplot of Purchase by Gender
ggplot(data, aes(x = Gender, y = Purchase, fill = Gender)) +
  geom_boxplot() +
  labs(title = "Purchase by Gender", x = "Gender", y = "Purchase")

# Scatterplot of Age vs. Purchase
ggplot(data, aes(x = Age, y = Purchase)) +
  geom_point(color = "skyblue") +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  labs(title = "Scatterplot of Age vs. Purchase", x = "Age", y = "Purchase")
## `geom_smooth()` using formula = 'y ~ x'

# Pie chart of Gender
gender_counts <- table(data$Gender)
pie(gender_counts, labels = c("Female", "Male"), col = c("skyblue", "salmon"), main = "Gender Distribution")

# Fit a multiple linear regression model with Age, Occupation, and City_Category as predictors and Purchase as the response variable.
lm_model <- lm(Purchase ~ Age + Occupation + City_Category, data = data)
summary(lm_model)
## 
## Call:
## lm(formula = Purchase ~ Age + Occupation + City_Category, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12137.3  -3861.6    162.2   4013.4  13245.7 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    10435.756     78.338 133.214  < 2e-16 ***
## Age18-25         589.366     78.182   7.538 4.78e-14 ***
## Age26-35         646.620     75.376   8.579  < 2e-16 ***
## Age36-45         669.357     77.741   8.610  < 2e-16 ***
## Age46-50         550.679     84.751   6.498 8.18e-11 ***
## Age51-55         926.612     86.968  10.655  < 2e-16 ***
## Age55+           599.269     98.217   6.102 1.05e-09 ***
## Occupation        16.555      1.926   8.596  < 2e-16 ***
## City_CategoryB   283.782     31.557   8.993  < 2e-16 ***
## City_CategoryC  1006.601     33.222  30.299  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5063 on 166811 degrees of freedom
## Multiple R-squared:  0.007608,   Adjusted R-squared:  0.007554 
## F-statistic: 142.1 on 9 and 166811 DF,  p-value: < 2.2e-16
#How does age affect purchasing behavior?
age_plot <- ggplot(data=train, aes(x=Age, fill=Age)) + 
            geom_bar() +
            labs(title="Distribution of Purchases by Age")
print(age_plot)

#What are the most purchased product categories?
product_cat_plot <- ggplot(data=train, aes(x=Product_Category_1)) + 
                    geom_bar() +
                    labs(title="Most Purchased Product Categories")
print(product_cat_plot)

# Visualize the distribution of stay in current city years among buyers
stay_years_plot <- ggplot(data = train, aes(x = Stay_In_Current_City_Years, fill = Stay_In_Current_City_Years)) + 
                   geom_bar() +
                   labs(title = "Distribution of Stay in Current City Years")
print(stay_years_plot)

#Is there a significant difference in purchase amount between different age groups?

age_group_purchase <- aggregate(Purchase ~ Age, data=train, FUN=mean, na.rm=TRUE)
print(age_group_purchase)
##     Age Purchase
## 1  0-17 8933.465
## 2 18-25 9169.664
## 3 26-35 9252.691
## 4 36-45 9331.351
## 5 46-50 9208.626
## 6 51-55 9534.808
## 7   55+ 9336.280
#How does marital status affect purchase amount?
marital_status_purchase <- aggregate(Purchase ~ Marital_Status, data=train, FUN=mean, na.rm=TRUE)
print(marital_status_purchase)
##   Marital_Status Purchase
## 1              0 9265.908
## 2              1 9261.175
#What is the regression model for predicting purchase amount based on age and occupation?
lm_model <- lm(Purchase ~ Age + Occupation, data=train, na.action=na.omit)
summary(lm_model)
## 
## Call:
## lm(formula = Purchase ~ Age + Occupation, data = train, na.action = na.omit)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##  -9681  -3578  -1196   2817  15153 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 8800.019     41.872 210.167  < 2e-16 ***
## Age18-25     267.038     43.895   6.084 1.18e-09 ***
## Age26-35     332.387     42.249   7.867 3.63e-15 ***
## Age36-45     396.723     43.572   9.105  < 2e-16 ***
## Age46-50     278.877     47.128   5.917 3.27e-09 ***
## Age51-55     600.596     48.210  12.458  < 2e-16 ***
## Age55+       391.519     53.313   7.344 2.08e-13 ***
## Occupation    15.232      1.045  14.572  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5021 on 550060 degrees of freedom
## Multiple R-squared:  0.0008281,  Adjusted R-squared:  0.0008154 
## F-statistic: 65.13 on 7 and 550060 DF,  p-value: < 2.2e-16
#After thorough analysis of the Black Friday sales data, several key insights have emerged. Firstly, it was observed that the sales volume significantly increased during the Black Friday period compared to regular days, indicating the importance of this event for retailers. Additionally, customer demographics played a crucial role, with distinct purchasing patterns identified among different age groups, genders, and income levels.