Conceptos que contribuyen a la identificación de clusters usando analítica de datos.
I. K- Means Clustering
K- means clustering tiene como objetivo agrupar datos con
características similares en el mismo cluster. Uno de los beneficios más
grandes de la herramienta es que se pueden resumir datos de gran
dimensión.
II. Unsupervised Learning
No se asignan etiquetas al algoritmo de aprendizaje, dejándolo solo para
encontrar la estructura en su entrada. El aprendizaje no supervisado
puede ser un fin en sí mismo (descubrir patrones ocultos en los datos) o
un medio para un fin (aprendizaje de funciones).
III. Eucliedean Distance
La distancia Euclidiana es el cálculo de distancia comúnmente utilizado
para medir la distancia entre dos puntos de datos.
Instalar librerias
library(foreign)
library(dplyr) # data manipulation
library(ggplot2) # data visualization
#install.packages("psych")
library(psych) # functions for multivariate analysis
library(corrplot) # correlation plots
#install.packages("jtools")
library(jtools) # presentation of regression analysis
library(lmtest) # diagnostic checks - linear regression analysis
library(car) # diagnostic checks - linear regression analysis
library(factoextra) # provides functions to extract and visualize the output of exploratory multivariate data analyses
#install.packages("ggfortify")
library(ggfortify) # data visualization tools for statistical analysis results
library(plyr)
library(base)
library(tibble)bajasdata <-read.csv("/Users/ivannagarza/Downloads/RHBAJASLIMPIA1.csv")
summary(bajasdata)## Nom Edad Gen Fecha_alta
## Length:237 Min. : 0.00 Length:237 Length:237
## Class :character 1st Qu.:23.00 Class :character Class :character
## Mode :character Median :29.00 Mode :character Mode :character
## Mean :30.52
## 3rd Qu.:37.00
## Max. :61.00
## MB Días_trab Baja PuestDes
## Length:237 Min. : 0.00 Length:237 Length:237
## Class :character 1st Qu.: 9.00 Class :character Class :character
## Mode :character Median : 21.00 Mode :character Mode :character
## Mean : 83.42
## 3rd Qu.: 49.00
## Max. :1966.00
## Sal_IMSS Col Mun Estado
## Min. :144.4 Length:237 Length:237 Length:237
## 1st Qu.:180.7 Class :character Class :character Class :character
## Median :180.7 Mode :character Mode :character Mode :character
## Mean :178.6
## 3rd Qu.:180.7
## Max. :500.0
## EstCiv
## Length:237
## Class :character
## Mode :character
##
##
##
bajasdata1<-bajasdata
bajasdata1<-subset(bajasdata,select = -c(Gen,Fecha_alta,MB,PuestDes,Estado,Nom,Col,Mun,Baja, EstCiv, Días_trab))
summary(bajasdata1)## Edad Sal_IMSS
## Min. : 0.00 Min. :144.4
## 1st Qu.:23.00 1st Qu.:180.7
## Median :29.00 Median :180.7
## Mean :30.52 Mean :178.6
## 3rd Qu.:37.00 3rd Qu.:180.7
## Max. :61.00 Max. :500.0
bajasdata2 <- bajasdata1
bajasdata2 <- bajasdata2 [bajasdata2$Edad> min(1), ]
summary(bajasdata2)## Edad Sal_IMSS
## Min. :18.00 Min. :144.4
## 1st Qu.:23.00 1st Qu.:180.7
## Median :29.00 Median :180.7
## Mean :30.78 Mean :178.6
## 3rd Qu.:37.00 3rd Qu.:180.7
## Max. :61.00 Max. :500.0
bajas_data_norm <- scale(bajasdata2[1:2])fviz_nbclust(bajas_data_norm, kmeans, method="wss")+
geom_vline(xintercept=4, linetype=2)+
labs(subtitle = "Elbow method") edad_cluster1<-kmeans(bajas_data_norm,3)
edad_cluster1## K-means clustering with 3 clusters of sizes 69, 121, 45
##
## Cluster means:
## Edad Sal_IMSS
## 1 0.2811317 0.2784633
## 2 -0.7792176 -0.1288413
## 3 1.6641609 -0.0805373
##
## Clustering vector:
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## 1 1 2 2 2 3 2 2 3 2 2 1 2 1 1 2 2 2 2 2
## 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
## 2 3 2 1 3 2 1 2 2 2 1 2 2 2 1 2 2 2 1 2
## 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
## 2 2 2 2 2 2 2 2 3 3 3 3 1 3 2 2 3 2 2 3
## 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
## 2 2 1 2 2 3 2 2 2 2 2 2 1 2 1 2 1 2 2 3
## 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 98 99 100 101
## 2 2 2 2 3 2 3 2 2 2 1 3 3 2 2 2 3 2 2 1
## 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
## 2 2 3 1 2 2 1 2 1 1 2 1 1 2 1 1 1 3 3 3
## 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
## 2 2 2 2 2 1 2 1 3 2 1 2 3 2 2 2 1 3 1 3
## 142 143 144 145 146 147 148 150 151 152 153 154 155 156 157 158 159 160 161 162
## 1 1 1 3 3 2 1 1 1 2 2 2 1 3 3 2 1 2 1 3
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
## 3 3 3 1 3 2 2 1 2 2 3 1 3 2 3 1 1 3 1 1
## 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
## 2 2 2 1 3 3 1 2 2 1 1 1 1 1 1 1 1 2 1 2
## 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
## 1 1 2 1 3 1 2 2 2 3 2 2 2 1 1 2 2 1 1 1
## 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
## 2 2 1 2 2 2 2 3 1 3 2 2 2 2 2
##
## Within cluster sum of squares by cluster:
## [1] 204.19421 35.55499 17.05330
## (between_SS / total_SS = 45.1 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
fviz_cluster(edad_cluster1,data=bajas_data_norm)bajasdata3<- bajasdata2
bajasdata3$Clusters<-edad_cluster1$cluster
bajasdata4<-bajasdata3 %>% group_by(Clusters) %>% summarise(Edad=max(Edad)) %>% arrange(desc(Edad))
bajasdata3$Cluster_Names<-factor(bajasdata3$Clusters,levels = c(1,2,3),
labels=c("Adulto", "Joven", "Jubilación"))bajasdata5 <- bajasdata3 %>% dplyr:: group_by(Cluster_Names) %>% dplyr:: summarize(edad_años=max(Edad),
Sal_IMSS=mean(Sal_IMSS),
count=n())clusters<-as.data.frame(bajasdata5)
clusters## Cluster_Names edad_años Sal_IMSS count
## 1 Adulto 40 185.7365 69
## 2 Joven 32 175.3303 121
## 3 Jubilación 61 176.5644 45
ggplot(bajasdata5, aes(x=Cluster_Names,y=edad_años,fill= Cluster_Names,label=round(edad_años,digits=2))) +
geom_col() +
geom_text()ggplot(bajasdata5,aes(x=Cluster_Names,y=Sal_IMSS ,fill= Cluster_Names,label=round(Sal_IMSS,digits=2))) +
geom_col() +
geom_text()ggplot(bajasdata3, aes(x=Cluster_Names, y=Edad, fill=Cluster_Names)) +
geom_boxplot()+
ggtitle("Dispersion of 'Edad' by Clusters Names")ggplot(bajasdata3, aes(x=Cluster_Names, y=Sal_IMSS, fill=Cluster_Names)) +
geom_boxplot()+
ggtitle("Dispersion of 'Salario_Diario' by Clusters Names")RHBAJAS <-read.csv("/Users/ivannagarza/Downloads/RHBAJASLIMPIA1.csv")summary(RHBAJAS)## Nom Edad Gen Fecha_alta
## Length:237 Min. : 0.00 Length:237 Length:237
## Class :character 1st Qu.:23.00 Class :character Class :character
## Mode :character Median :29.00 Mode :character Mode :character
## Mean :30.52
## 3rd Qu.:37.00
## Max. :61.00
## MB Días_trab Baja PuestDes
## Length:237 Min. : 0.00 Length:237 Length:237
## Class :character 1st Qu.: 9.00 Class :character Class :character
## Mode :character Median : 21.00 Mode :character Mode :character
## Mean : 83.42
## 3rd Qu.: 49.00
## Max. :1966.00
## Sal_IMSS Col Mun Estado
## Min. :144.4 Length:237 Length:237 Length:237
## 1st Qu.:180.7 Class :character Class :character Class :character
## Median :180.7 Mode :character Mode :character Mode :character
## Mean :178.6
## 3rd Qu.:180.7
## Max. :500.0
## EstCiv
## Length:237
## Class :character
## Mode :character
##
##
##
str(RHBAJAS)## 'data.frame': 237 obs. of 13 variables:
## $ Nom : chr "MARIO VALDEZ ORTIZ" "ISABEL BARRIOS MENDEZ" "MARIA ELIZABETH GOMEZ HERNANDEZ" "ALONDRA ABIGAIL ESCARCIA GOMEZ" ...
## $ Edad : int 32 36 23 21 29 46 29 31 50 19 ...
## $ Gen : chr "MASCULINO" "FEMENINO" "FEMENINO" "FEMENINO" ...
## $ Fecha_alta: chr "2020-03-09" "2021-11-09" "2021-11-10" "2021-11-10" ...
## $ MB : chr "RENUNCIA VOLUNTARIA" "RENUNCIA VOLUNTARIA" "RENUNCIA VOLUNTARIA" "RENUNCIA VOLUNTARIA" ...
## $ Días_trab : int 628 60 59 59 51 37 37 31 18 224 ...
## $ Baja : chr "2021-11-27" "2022-01-08" "2022-01-08" "2022-01-08" ...
## $ PuestDes : chr "DISEÑO" "AYUDANTE GENERAL" "AYUDANTE GENERAL" "AYUDANTE GENERAL" ...
## $ Sal_IMSS : num 500 152 152 152 152 ...
## $ Col : chr "SAN NICOLAS DE LOS G" "COLINAS DEL AEROPÑUERTO" "PUEBLO NUEVO" "PUEBLO NUEVO" ...
## $ Mun : chr "SAN NICOLAS DE LOS G" "PESQUERIA" "APODACA" "APODACA" ...
## $ Estado : chr "NUEVO LEÓN" "NUEVO LEÓN" "NUEVO LEÓN" "NUEVO LEÓN" ...
## $ EstCiv : chr "SOLTERO" "UNIÓN LIBRE" "CASADO" "SOLTERO" ...
describe(RHBAJAS)## vars n mean sd median trimmed mad min max range
## Nom* 1 237 117.03 67.48 116.00 116.92 85.99 1.00 234 233.00
## Edad 2 237 30.52 10.02 29.00 29.69 10.38 0.00 61 61.00
## Gen* 3 237 1.41 0.49 1.00 1.39 0.00 1.00 2 1.00
## Fecha_alta* 4 237 76.80 37.34 81.00 78.16 45.96 1.00 137 136.00
## MB* 5 237 2.83 1.04 2.00 2.75 0.00 1.00 5 4.00
## Días_trab 6 237 83.42 226.88 21.00 33.08 22.24 0.00 1966 1966.00
## Baja* 7 237 45.59 27.90 46.00 45.31 34.10 1.00 95 94.00
## PuestDes* 8 237 11.35 6.47 9.00 9.92 0.00 1.00 31 30.00
## Sal_IMSS 9 237 178.64 25.44 180.68 179.15 0.00 144.45 500 355.55
## Col* 10 237 57.98 26.64 72.00 59.83 23.72 1.00 98 97.00
## Mun* 11 237 2.76 2.83 1.00 2.27 0.00 1.00 13 12.00
## Estado* 12 237 1.97 0.20 2.00 2.00 0.00 1.00 3 2.00
## EstCiv* 13 237 2.95 1.45 3.00 2.94 2.97 1.00 5 4.00
## skew kurtosis se
## Nom* 0.01 -1.21 4.38
## Edad 0.53 0.16 0.65
## Gen* 0.37 -1.87 0.03
## Fecha_alta* -0.24 -1.11 2.43
## MB* 0.50 -1.50 0.07
## Días_trab 5.33 32.14 14.74
## Baja* 0.02 -1.18 1.81
## PuestDes* 2.00 3.09 0.42
## Sal_IMSS 9.39 111.15 1.65
## Col* -0.64 -0.85 1.73
## Mun* 1.33 0.64 0.18
## Estado* -3.53 19.31 0.01
## EstCiv* 0.05 -1.11 0.09
RHBAJAS1<-RHBAJAS
RHBAJAS1<-subset(RHBAJAS1,select = -c(Gen,Fecha_alta,MB,PuestDes,Estado,Nom,Col,Mun,Baja))
summary(RHBAJAS1)## Edad Días_trab Sal_IMSS EstCiv
## Min. : 0.00 Min. : 0.00 Min. :144.4 Length:237
## 1st Qu.:23.00 1st Qu.: 9.00 1st Qu.:180.7 Class :character
## Median :29.00 Median : 21.00 Median :180.7 Mode :character
## Mean :30.52 Mean : 83.42 Mean :178.6
## 3rd Qu.:37.00 3rd Qu.: 49.00 3rd Qu.:180.7
## Max. :61.00 Max. :1966.00 Max. :500.0
Eliminamos dos reglones que tenian como edad (0)
RHBAJAS2 <- RHBAJAS1
RHBAJAS2 <- RHBAJAS2 [RHBAJAS2$Edad> min(1), ]
summary(RHBAJAS2)## Edad Días_trab Sal_IMSS EstCiv
## Min. :18.00 Min. : 0.00 Min. :144.4 Length:235
## 1st Qu.:23.00 1st Qu.: 9.50 1st Qu.:180.7 Class :character
## Median :29.00 Median : 21.00 Median :180.7 Mode :character
## Mean :30.78 Mean : 84.07 Mean :178.6
## 3rd Qu.:37.00 3rd Qu.: 49.50 3rd Qu.:180.7
## Max. :61.00 Max. :1966.00 Max. :500.0
RH_EDAD_NORM<-scale(RHBAJAS2[1:2])fviz_nbclust(RH_EDAD_NORM, kmeans, method="wss")+
geom_vline(xintercept=4, linetype=2)+
labs(subtitle = "Elbow method") RHEDAD_Cluster1<-kmeans(RH_EDAD_NORM,4)
RHEDAD_Cluster1## K-means clustering with 4 clusters of sizes 43, 107, 5, 80
##
## Cluster means:
## Edad Días_trab
## 1 1.6031879 -0.11611923
## 2 -0.8512555 -0.17238996
## 3 1.2245019 5.86438920
## 4 0.2003094 -0.07353867
##
## Clustering vector:
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## 4 4 2 2 4 1 4 4 1 2 2 4 2 4 4 2 2 2 2 2
## 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
## 4 3 2 4 1 2 4 2 2 2 4 2 2 2 4 2 2 4 4 2
## 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
## 2 2 2 2 2 2 2 2 1 1 1 1 4 1 2 2 1 2 2 3
## 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
## 2 2 4 2 2 1 2 2 4 2 2 2 4 2 4 2 4 2 2 1
## 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 98 99 100 101
## 3 2 2 4 1 2 1 2 2 2 4 1 1 2 2 4 1 4 2 4
## 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
## 2 2 1 4 2 2 4 3 4 4 2 4 4 2 4 4 4 1 1 1
## 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
## 2 2 2 2 2 4 2 4 1 2 4 2 1 2 4 2 4 1 4 1
## 142 143 144 145 146 147 148 150 151 152 153 154 155 156 157 158 159 160 161 162
## 4 4 1 1 1 2 4 4 4 2 2 2 4 1 1 2 4 2 4 1
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
## 1 1 1 4 1 2 2 4 2 2 1 4 1 2 1 4 4 1 4 4
## 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
## 2 2 2 4 1 3 4 2 2 4 4 4 4 4 4 4 4 2 4 4
## 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
## 4 4 2 4 1 4 2 2 2 1 2 2 2 4 4 2 2 4 4 4
## 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
## 2 2 4 2 2 2 4 1 4 1 2 2 2 2 2
##
## Within cluster sum of squares by cluster:
## [1] 19.40753 15.18543 18.67351 39.82387
## (between_SS / total_SS = 80.1 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
fviz_cluster(RHEDAD_Cluster1,data=RH_EDAD_NORM)RHBAJAS3 <- RHBAJAS2
RHBAJAS3$Clusters<-RHEDAD_Cluster1$cluster
RHBAJAS4<-RHBAJAS3 %>% group_by(Clusters) %>% summarise(Edad=max(Edad)) %>% arrange(desc(Edad))
RHBAJAS3$Cluster_Names<-factor(RHBAJAS3$Clusters,levels = c(1,2,3,4),
labels=c("Adulto", "Principiante", "Mayor", "Juvenil"))RHBAJAS6 <- RHBAJAS3 %>% group_by(Cluster_Names) %>% dplyr:: summarize(edad_años=max(Edad),
Días_trab=mean(Días_trab),
Count=n())clusters<-as.data.frame(RHBAJAS6)
clusters## Cluster_Names edad_años Días_trab Count
## 1 Adulto 57 57.62791 43
## 2 Principiante 27 44.81308 107
## 3 Mayor 61 1419.60000 5
## 4 Juvenil 39 67.32500 80
ggplot(RHBAJAS6,aes(x=reorder(Cluster_Names,Count),y=Count,fill=Cluster_Names)) +
geom_bar(stat="identity")ggplot(RHBAJAS6, aes(x=Cluster_Names,y=edad_años,fill= Cluster_Names,label=round(edad_años,digits=2))) +
geom_col() +
geom_text()ggplot(RHBAJAS6,aes(x=Cluster_Names,y=Días_trab,fill= Cluster_Names,label=round(Días_trab,digits=2))) +
geom_col() +
geom_text()ggplot(RHBAJAS3, aes(x=Cluster_Names, y=Edad, fill=Cluster_Names)) +
geom_boxplot()+
ggtitle("Edad (Dispersion)")Durante esta actividad realizamos un análisis en la base de datos de Recursos Humanos, enfocados en las variables de Edad, Salario y Días Trabajados, por lo que se decidió definir varios pasos a seguir para llegar al objetivo deseado que era finalmente llegar a la construcción de los clusters.
Tomando en cuenta los hallazgos de la edad y el salario diario, se llego a la conclusión de lo siguiente:
Hay más bajas de adultos con el rango de edades de 41 a 61, el cuál se identifico como jubilación.
Los adultos de 33-40 años ganan más con un sueldo diario de 186.16 en promedio.
De igual manera podemos observar cuatro Clusters que toman en cuenta la edad y los días laborados:
El grupo que cuenta con más días laborados es el de edad máxima,
siendo los adultos, de igual manera estos cuentan con un salario
mayor.
Por último podemos observar que en los gráficos que se analizan con
estas variables tenemos los siguientes puntos a consideración:
- Edad Máxima
- Días Laborados Máximos
- Recuento de los grupos que presentan mayor actividad en la
empresa.
- La dispersión de edad que existe entre cada uno de los grupos.