#Market Basket Analysis
#Instalar paquetes y llamar librerías
#install.packages("plyr")
library(plyr)
#install.packages("Matrix")
library(Matrix)
#install.packages("arules")
library(arules)
##
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
##
## abbreviate, write
#install.packages("arulesViz")
library(arulesViz)
#install.packages("datasets")
library(datasets)
#Importar la base de datos
file.choose()
## [1] "C:\\Users\\User-1\\Downloads\\TEC 4TO\\Nueva carpeta\\Supermercado_bdlimpia.csv"
bd_limpia<- read.csv("C:\\Users\\User-1\\Downloads\\TEC 4TO\\Nueva carpeta\\Supermercado_bdlimpia.csv")
#Acomodar de menor a mayor los tickets
#bd_limpia <- bd_limpia[order(bd_limpia$F.Ticket),]
head(bd_limpia)
## BillNo Itemname Quantity Date Price
## 1 536365 WHITE HANGING HEART T-LIGHT HOLDER 6 01/12/2010 08:26 2.55
## 2 536365 WHITE METAL LANTERN 6 01/12/2010 08:26 3.39
## 3 536365 CREAM CUPID HEARTS COAT HANGER 8 01/12/2010 08:26 2.75
## 4 536365 KNITTED UNION FLAG HOT WATER BOTTLE 6 01/12/2010 08:26 3.39
## 5 536365 RED WOOLLY HOTTIE WHITE HEART. 6 01/12/2010 08:26 3.39
## 6 536365 SET 7 BABUSHKA NESTING BOXES 2 01/12/2010 08:26 7.65
## CustomerID Country
## 1 17850 United Kingdom
## 2 17850 United Kingdom
## 3 17850 United Kingdom
## 4 17850 United Kingdom
## 5 17850 United Kingdom
## 6 17850 United Kingdom
tail(bd_limpia)
## BillNo Itemname Quantity Date Price
## 388018 581587 CHILDRENS CUTLERY SPACEBOY 4 09/12/2011 12:50 4.15
## 388019 581587 PACK OF 20 SPACEBOY NAPKINS 12 09/12/2011 12:50 0.85
## 388020 581587 CHILDREN'S APRON DOLLY GIRL 6 09/12/2011 12:50 2.10
## 388021 581587 CHILDRENS CUTLERY DOLLY GIRL 4 09/12/2011 12:50 4.15
## 388022 581587 CHILDRENS CUTLERY CIRCUS PARADE 4 09/12/2011 12:50 4.15
## 388023 581587 BAKING SET 9 PIECE RETROSPOT 3 09/12/2011 12:50 4.95
## CustomerID Country
## 388018 12680 France
## 388019 12680 France
## 388020 12680 France
## 388021 12680 France
## 388022 12680 France
## 388023 12680 France
#Extraer productos por ticket
super <-ddply(bd_limpia,c("BillNo"),function(bd_limpia)paste(bd_limpia$Itenname,collapse=","))
#Eliminar número de ticket
#basket$BillNo<- NULL
#Renombrar el nombre de la columna marca
#colnames(basket) <- c("Itenname")
#Exportar basket
#write.csv(basket,"Super2.csv",quote =FALSE, row.names =FALSE)
#Importar transacciones
file.choose()
## [1] "C:\\Users\\User-1\\Downloads\\TEC 4TO\\Nueva carpeta\\Super2.csv"
tr<-read.transactions("C:\\Users\\User-1\\Downloads\\TEC 4TO\\Nueva carpeta\\Super2.csv",format="basket",sep=",")
#Generar las reglas de asociación
#reglas_asociacion<-apriori(tr,parameter=list(supp=0.001,conf=0.2, maxlen=10))
#summary(reglas_asociacion)
#inspect(reglas_asociacion)
#reglas_asociacion <-sort(reglas_asociacion, by="confidence", decreasing=TRUE)
#summary(reglas_asociacion)
#inspect(reglas_asociacion)
#top10reglas <-head(reglas_asociacion,n=5, by="confidence")
#plot(top10reglas, method="graph",engine="htmlwidget")