
## [1] "~/Desktop/ikea.gif"
# 1. Crear base de datos
df <- data.frame(x=c(2,2,8,5,7,6,1,4),
y=c (10,5,4,8,5,4,2,9))
# 2. Determinar el número de grupos
grupos <- 3
# 3. Realizar la clasificación
segmentos <- kmeans(df,grupos)
segmentos## K-means clustering with 3 clusters of sizes 3, 3, 2
##
## Cluster means:
## x y
## 1 3.666667 9.000000
## 2 7.000000 4.333333
## 3 1.500000 3.500000
##
## Clustering vector:
## [1] 1 3 2 1 2 2 3 1
##
## Within cluster sum of squares by cluster:
## [1] 6.666667 2.666667 5.000000
## (between_SS / total_SS = 85.8 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
## x y cluster
## 1 2 10 1
## 2 2 5 3
## 3 8 4 2
## 4 5 8 1
## 5 7 5 2
## 6 6 4 2
## 7 1 2 3
## 8 4 9 1
# 5. Graficar resultados
#install.packages("ggplot2)
library(ggplot2)
#install.packages("factoextra")
library(factoextra)## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_cluster(segmentos, data=df,
palette=c("red", "blue", "black", "green", "yellow"),
ellpise.type="euclid",
star.plot= T,
repel= T,
ggtheme= theme())# 6. Optimizar cantidad de grupos
library(cluster)
library(data.table)
set.seed(123)
optimizacion <- clusGap(df, FUN=kmeans, nstart=1, K.max = 7)
plot(optimizacion, xlab= "Número de clusters K")## BillNo Itemname Quantity Date
## Length:522064 Length:522064 Min. :-9600.00 Length:522064
## Class :character Class :character 1st Qu.: 1.00 Class :character
## Mode :character Mode :character Median : 3.00 Mode :character
## Mean : 10.09
## 3rd Qu.: 10.00
## Max. :80995.00
##
## Hour Price CustomerID Country
## Length:522064 Min. :-11062.060 Min. :12346 Length:522064
## Class :character 1st Qu.: 1.250 1st Qu.:13950 Class :character
## Mode :character Median : 2.080 Median :15265 Mode :character
## Mean : 3.827 Mean :15317
## 3rd Qu.: 4.130 3rd Qu.:16837
## Max. : 13541.330 Max. :18287
## NA's :134041
## Total
## Min. :-11062.06
## 1st Qu.: 3.75
## Median : 9.78
## Mean : 19.69
## 3rd Qu.: 17.40
## Max. :168469.60
##
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
##
## between, first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#count(bd,BillNo, sort=TRUE)
#count(bd,Itemname, sort=TRUE)
#count(bd,Date, sort=TRUE)
#count(bd,Hour, sort=TRUE)
#count(bd,CustomerID, sort=TRUE)
#count(bd, Country, sort=TRUE)Observaciones: 1. Tenemos cantidades, precios y totales negativos. 2. Fecha y hora no tienen formato adecuado. 3. Tenemos NA’s en CustomerID
## [1] 134041
## BillNo Itemname Quantity Date Hour Price CustomerID
## 0 0 0 0 0 0 134041
## Country Total
## 0 0
#Eliminar Totales negativos
bd <- bd[bd$Total > 0,]
#Identificar outliers
boxplot(bd$Total, horizontal = TRUE)
Observaciones: 4. Tenemos presencia de datos fuera de lo normal
(outliers).
# Obtener el total por ticket
ticket_promedio <- aggregate(Total ~ CustomerID + BillNo, data = bd, sum)
#Obtener el ticket promedio
ticket_promedio <- aggregate(Total ~ CustomerID, data=ticket_promedio, mean)
colnames(ticket_promedio) <- c("CustomerID", "TicketPromedio")
library(dplyr)
#Obtener cantidad de visitas por cliente
visitas <- group_by(bd, CustomerID) %>% summarize(Visitas= n_distinct(BillNo))
#Juntar las tables Ticket Promedio y Visitas
objetos <- merge(ticket_promedio, visitas,by="CustomerID")
#Llamar a los renglones como CustomerID
rownames(objetos) <- objetos$CustomerID
objetos <- subset(objetos, select = -c(CustomerID))
#Eliminar datos fuera de lo normal
#Los datos fuera de lo normal están fuera de los siguientes limites:
#Limite inferior = Q1 - 1.5*IQR
#lIMITE Superior = Q3 + 1.5*IQR
#Q1: Cuartil 1, Q3: Cuartil 3, IQR Rango Intercuartil
#Columna de Ticket Promedio
IQR_TP <- IQR(objetos$TicketPromedio)
IQR_TP## [1] 248.3318
## TicketPromedio Visitas
## Min. : 3.45 Min. : 1.000
## 1st Qu.: 178.30 1st Qu.: 1.000
## Median : 292.00 Median : 2.000
## Mean : 415.62 Mean : 4.227
## 3rd Qu.: 426.63 3rd Qu.: 5.000
## Max. :84236.25 Max. :209.000
## [1] -194.1977
## [1] 799.1277
## [1] 4
## [1] -20
## [1] 11
# 0. Normalizar variables
objetos <- as.data.frame(scale(objetos))
# 1. Crear base de datos
df <- objetos
# 2. Determinar el número de grupos
grupos <- 4
# 3. Realizar la clasificación
segmentos <- kmeans(df, grupos)
# 4. Revisar la asignción de grupos
asignacion <- cbind(df, cluster=segmentos$cluster)
# 5. Graficar resultados
# instal ggplot2 y factoextra
library(ggplot2)
library(factoextra)
fviz_cluster(segmentos, data = df,
palette = c("red", "blue", "yellow","green"),
ellpise.type = "euclid",
star.plot = TRUE,
repel = TRUE,
ggtheme = theme())# 6. Optimizar cantidad de grupos
library(cluster)
library(data.table)
set.seed(123)
optimizacion <- clusGap(df, FUN=kmeans, nstart=1,K.max = 7)
plot(optimizacion, xlab="Número de clusters K")