<jupyter_start><jupyter_text>**Reading and checking the data**<jupyter_code># dowloading tidyverse packages and library 
install.packages('tidyverse')
library(tidyverse)

# Installing our ggbiplot visualisation package

library(devtools)
install_github("vqv/ggbiplot")


# Then Loading our ggbiplot library
#  
library(ggbiplot)
#Read the dataset 
salesdf1 <- read_csv("/content/Supermarket_Dataset_1 - Sales Data.csv")
#Checking the head and tail of the data 

head(salesdf1)
tail(salesdf1)
# Lists variables in the dataset
names(salesdf1) 
#The rows and columns in the data 
cat("The dataset has ", dim(salesdf1)[1], "rows and ", dim(salesdf1)[2], " columns")
#checking the datatypes on the columns 
sapply(salesdf1, class)<jupyter_output><empty_output><jupyter_text>**Tidying the dataset**<jupyter_code>## Importing packages for plotting 

library(tidyr)
library(ggplot2)
library(dplyr)
names(salesdf1)<- tolower(names(salesdf1)) # make the column names to lowercase 

names(salesdf1)<- str_replace_all(names(salesdf1), c(" " = "_")) # Replacing the white spaces in the column names 

head(salesdf1)
# Selecting the numerical data (excluding the categorical variables)



sale1 <- salesdf1[,c(6,7,12,14,15,16)]
head(sale1)

#Checking for outliers 
boxplot(sale1, col = rainbow(ncol(sale1)))
# Listing the outliers 
OutVals = boxplot(sale1, plot=FALSE)$out
OutVals
#Removing outliers 
sales <-salesdf1

sales<- salesdf1[-which(salesdf1$cogs %in% OutVals),]
sales<- salesdf1[-which(salesdf1$gross_income %in% OutVals),]
sales<- salesdf1[-which(salesdf1$total %in% OutVals),]
#check the difference 
print(dim(salesdf1))
print(dim(sales))





head(sales)
#checking for duplicates 

table(duplicated(sales))
# Checking the number of missing per column/variable
colSums(is.na(sales))<jupyter_output><empty_output><jupyter_text>**Exploratory Data Analysis**  **Univariate analysis**<jupyter_code>#summary of the dataset 
#Basic descriptive statistics and frequencies.

summary(salesdf1)   
#Descriptive statistics for each column 

desc_stats <- data.frame(
  Min = apply(sale1, 2, min),    # minimum
  Max = apply(sale1, 2, max),     # Maximum
  Med = apply(sale1, 2, median), # median
  Mean = apply(sale1, 2, mean),  # mean
  variance= apply(sale1, 2, var),  #Variance 
  SD = apply(sale1, 2, sd)   # Standard deviation
  ) 


#get the min,max, median, mean , variance and standard deviation of numerical variables 
desc_stats <- round(desc_stats, 1)
print(desc_stats)

#Get the mode 

getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}


attach(sale1)
 print("The mode of the unit_price column is " )
getmode(unit_price);

 print("The mode of the quantity column is " )
 getmode(quantity); 

print("The mode of the cogs column is " )
 getmode(cogs);


print("The mode of the gross_income column is " )
 getmode(gross_income);

print("The mode of the rating  column is " )
 getmode(rating);
 print("The mode of the total  column is " )
 getmode(total);
#See the  distribution of numeric variables
hist(sale1$unit_price, col='cadetblue')
hist(sale1$quantity, col='cadetblue')
hist(sale1$cogs, col='cadetblue')
hist(sale1$gross_income, col='cadetblue')
hist(sale1$rating, col='cadetblue')
hist(sale1$total, col='cadetblue')

#See the  distribution of categorical variables 
ggplot(salesdf1,aes(x=toupper(payment)))+geom_bar()+xlab(label = "Modes of Payment")+ylab(label = "Frequency of modes of payment")
ggplot(salesdf1,aes(x=toupper(gender)))+geom_bar()+xlab(label = "Males and Females")+ylab(label = "Frequency of males and females")
ggplot(salesdf1,aes(x=toupper(branch)))+geom_bar()+xlab(label = "Types of branches")+ylab(label = "Frequency of various branches")
ggplot(salesdf1,aes(x=toupper(customer_type)))+geom_bar()+xlab(label = "Customer type")+ylab(label = "Frequency of customers")
# Margins area
par(oma=c(0,0,0,0)) # all sides have 3 lines of space
par(mar=c(0,0,0,0) + 0.1)
ggplot(salesdf1,aes(x=toupper(product_line)))+geom_bar()+xlab(label = "Customer type")+ylab(label = "Frequency of customers")<jupyter_output><empty_output><jupyter_text>**Multivariate analysis**<jupyter_code>install.packages("GGally")
library(GGally)

ggpairs(sale1)
M<-cor(sale1) #find the correlation
install.packages("corrplot")
library(corrplot)
# Margins area
par(oma=c(0,0,0,0)) # all sides have 3 lines of space
par(mar=c(0,0,0,0) + 0.1)
corrplot(M, method="number") #Compute and visualize the correlation co-eeficients<jupyter_output>Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

corrplot 0.84 loaded<jupyter_text>**PCA**<jupyter_code>#Convert the data to factors and integers 
sales$branch <- (as.integer(as.factor(sales$branch)))
sales$customer_type <- (as.integer(as.factor(sales$customer_type)))
sales$gender <- (as.integer(as.factor(sales$gender)))
sales$product_line <- (as.integer(as.factor(sales$product_line)))
sales$payment <- (as.integer(as.factor(sales$payment)))


head(sales)
# Selecting the numerical data (excluding the categorical variables ) 
# we also remove the factor variable total

df <- sales[,c(2:8,11,12, 14,15)]
head(df)
# We then pass df to the prcomp(). We also set two arguments, center and scale, 
# to be TRUE then preview our object with summary

df.pca <- prcomp(df, center = TRUE, scale. = TRUE)

#Visualize our PCA cumulative of variance
fviz_eig(df.pca, addlabels = TRUE,center = TRUE, scale. = TRUE, ylim = c(0, 50))

# As a result we obtain 12 principal components, 
# each which explain a percentate of the total variation of the dataset
# PC1 explains 35.5% of the total variance, which means that nearly half
# of the information in the dataset (12 variables) can be encapsulated 
# by just that one Principal Component. PC2 explains 10.3% of the variance. etc




# Calling str() to have a look at your PCA object
# ---
# 
str(df.pca)

# Installing our ggbiplot visualisation package

library(devtools)
install_github("vqv/ggbiplot")
# To plot variables that show PCA1 and PCA2 
#  
fviz_pca_var(df.pca, col.var = "black")<jupyter_output><empty_output><jupyter_text>**Variable correlation map** Positively correlated variables are grouped together. grosss income and unit priceNegatively correlated variables are positioned on opposite sides of the plot origin (opposed quadrants). product line and branchThe distance between variables and the origin measures the quality of the variables on the factor map. Variables that are away from the origin are well represented on the factor map.  From the graph we will see that the variables unit price, gross income and quantity contribute to PC1, with higher values in those variables moving the samples to the right on the plot.<jupyter_code>#The quality of representation of the variables on factor map is called cos2
# Total cos2 of variables on PCA.1 and PCA.2
fviz_cos2(df.pca, choice = "var", axes = 1:2)<jupyter_output><empty_output><jupyter_text>If a variable is perfectly represented by only two principal components , the sum of the cos2 on these two PCs is equal to one. In this case the variables will be gross income,tax and cogs.<jupyter_code>var <- get_pca_var(df.pca)
var
#The most important (or, contributing) variables can be highlighted on the correlation plot
fviz_pca_var(df.pca, col.var = "contrib",
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11704145218098076, 'fraction_numerical': 0.018694120834462206, 'mean_word_length': 5.912921348314606, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '415153', 'license': 'MIT', 'n_tokens_mistral': 2654, 'n_tokens_neox': 2426, 'n_words': 736, 'path': 'Unsupervised_Learning_PCA_R.ipynb', 'repo_name': 'ValentineKarimi/Carrefour-Kenya-Analysis-', 'size': '7382'}