1. Análisis exploratorio de datos demográficos

Demográficas: Edad Sexo Zonas de Área Metropolitana Estado Civil

2. Análisis exploratorio de variables VI y VD.

Variable Dependiente: Rotación de personal Variables Independiente: Salario Grado de es Horario laboral tudios finalizado Condiciones laborales (clima, baños, etc.) Ubicación de vivienda

3. Regresión Lineal

1. Insertar base de datos

bd <- read.csv("/Users/valeriavillarreal/Documents/FORM_BasedeDatosCorrecta.csv")

2. Entender base de datos

summary(bd)
##     Puesto            Antigüedad     Motivacion           Salario     
##  Length:106         Min.   : 1.00   Length:106         Min.   :1.000  
##  Class :character   1st Qu.: 1.00   Class :character   1st Qu.:3.000  
##  Mode  :character   Median : 9.00   Mode  :character   Median :4.000  
##                     Mean   :14.08                      Mean   :3.792  
##                     3rd Qu.:34.50                      3rd Qu.:5.000  
##                     Max.   :36.00                      Max.   :5.000  
##   Prestaciones   Jornada_Laboral  Herramientas    Temperatura   
##  Min.   :1.000   Min.   :1.00    Min.   :1.000   Min.   :1.000  
##  1st Qu.:2.000   1st Qu.:4.00    1st Qu.:3.000   1st Qu.:1.000  
##  Median :4.000   Median :5.00    Median :5.000   Median :3.000  
##  Mean   :3.274   Mean   :4.16    Mean   :3.877   Mean   :3.123  
##  3rd Qu.:5.000   3rd Qu.:5.00    3rd Qu.:5.000   3rd Qu.:5.000  
##  Max.   :5.000   Max.   :5.00    Max.   :5.000   Max.   :5.000  
##      Estres        Transporte    Instalaciones     Y_Rotacion   
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:3.000   1st Qu.:4.000   1st Qu.:4.000   1st Qu.:3.000  
##  Median :4.000   Median :5.000   Median :5.000   Median :5.000  
##  Mean   :3.679   Mean   :4.009   Mean   :4.311   Mean   :4.038  
##  3rd Qu.:5.000   3rd Qu.:5.000   3rd Qu.:5.000   3rd Qu.:5.000  
##  Max.   :5.000   Max.   :5.000   Max.   :5.000   Max.   :5.000  
##  Conflicto_Acoso    Satisfaccion.en.el.trabajo   Emocion         
##  Length:106         Length:106                 Length:106        
##  Class :character   Class :character           Class :character  
##  Mode  :character   Mode  :character           Mode  :character  
##                                                                  
##                                                                  
##                                                                  
##       Edad          Genero          Estado.civil        Municipio        
##  Min.   :18.00   Length:106         Length:106         Length:106        
##  1st Qu.:25.25   Class :character   Class :character   Class :character  
##  Median :33.50   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :35.62                                                           
##  3rd Qu.:45.00                                                           
##  Max.   :68.00                                                           
##  Escolaridad         Dependientes  
##  Length:106         Min.   :0.000  
##  Class :character   1st Qu.:0.000  
##  Mode  :character   Median :1.000  
##                     Mean   :1.085  
##                     3rd Qu.:2.000  
##                     Max.   :3.000

Observaciones:
1. Las caracteristicas con las que están mas de acuerdo los empleados son las instalaciones y jornada laboral.
2. Las caracteristicas con las que mas están en desacuerdo los empleados son la temperatura y prestaciones.

3. Generar Regresión Lineal

regresion <- lm(Y_Rotacion ~ Antigüedad + Prestaciones + Jornada_Laboral + Herramientas + Temperatura + Estres + Transporte + Instalaciones + Salario + Edad, data = bd)
summary(regresion)
## 
## Call:
## lm(formula = Y_Rotacion ~ Antigüedad + Prestaciones + Jornada_Laboral + 
##     Herramientas + Temperatura + Estres + Transporte + Instalaciones + 
##     Salario + Edad, data = bd)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8344 -0.5643  0.1252  0.6257  1.9155 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)   
## (Intercept)      0.894242   0.599325   1.492  0.13899   
## Antigüedad       0.015886   0.007311   2.173  0.03226 * 
## Prestaciones     0.205520   0.074818   2.747  0.00720 **
## Jornada_Laboral -0.014858   0.087595  -0.170  0.86567   
## Herramientas     0.135305   0.081450   1.661  0.09997 . 
## Temperatura     -0.178823   0.057157  -3.129  0.00233 **
## Estres           0.026761   0.075244   0.356  0.72289   
## Transporte       0.107300   0.070103   1.531  0.12919   
## Instalaciones    0.021391   0.100965   0.212  0.83266   
## Salario          0.190279   0.086419   2.202  0.03010 * 
## Edad             0.028077   0.008638   3.251  0.00159 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9836 on 95 degrees of freedom
## Multiple R-squared:  0.4652, Adjusted R-squared:  0.4089 
## F-statistic: 8.264 on 10 and 95 DF,  p-value: 1.647e-09

4. Generar Regresión Lineal

regresion <- lm(Y_Rotacion ~ Edad + Temperatura + Prestaciones + Salario + Antigüedad, data = bd)
summary(regresion)
## 
## Call:
## lm(formula = Y_Rotacion ~ Edad + Temperatura + Prestaciones + 
##     Salario + Antigüedad, data = bd)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8072 -0.5906  0.1915  0.5880  2.0290 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   1.555513   0.437849   3.553 0.000584 ***
## Edad          0.023381   0.008475   2.759 0.006903 ** 
## Temperatura  -0.160980   0.056977  -2.825 0.005703 ** 
## Prestaciones  0.257666   0.068795   3.745 0.000301 ***
## Salario       0.276159   0.079077   3.492 0.000715 ***
## Antigüedad    0.018558   0.007198   2.578 0.011390 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9953 on 100 degrees of freedom
## Multiple R-squared:  0.4235, Adjusted R-squared:  0.3947 
## F-statistic: 14.69 on 5 and 100 DF,  p-value: 8.679e-11

Insights:
Los factores principales que afectan si un empleado decide quedarse en FORM son las prestaciones y el salario, seguido de edad, temperatura y antiguedad, esta última no decido sin embargo es tomada en cuenta al momento de tomar una decisión.

4. Clusters

1. Importar Base de datos

clusterbase <- bd[,c("Salario", "Prestaciones", "Jornada_Laboral", "Herramientas", "Temperatura", "Estres", "Transporte", "Instalaciones")]
clusterbase
##     Salario Prestaciones Jornada_Laboral Herramientas Temperatura Estres
## 1         5            4               5            5           2      5
## 2         4            4               5            4           3      2
## 3         2            1               1            1           3      1
## 4         5            4               5            5           4      5
## 5         3            1               5            5           4      3
## 6         4            5               5            5           2      4
## 7         2            3               4            4           2      5
## 8         5            4               5            5           4      4
## 9         4            1               4            1           5      4
## 10        5            4               5            5           1      5
## 11        5            5               1            5           5      5
## 12        5            4               5            5           5      4
## 13        4            5               5            5           1      5
## 14        4            4               5            5           1      5
## 15        5            5               5            5           1      5
## 16        4            2               5            5           4      2
## 17        2            2               5            4           3      4
## 18        5            5               5            5           5      5
## 19        1            1               4            1           4      2
## 20        4            1               5            3           3      1
## 21        4            1               5            4           1      5
## 22        4            1               2            4           5      2
## 23        4            3               4            5           1      2
## 24        4            1               3            4           3      4
## 25        4            4               5            5           4      4
## 26        5            2               4            1           1      3
## 27        4            4               5            5           1      5
## 28        3            1               3            4           3      5
## 29        4            5               4            1           1      1
## 30        3            4               5            5           5      3
## 31        5            4               5            5           5      5
## 32        1            5               5            1           1      3
## 33        5            2               5            1           5      5
## 34        4            2               5            5           2      5
## 35        5            4               4            5           3      4
## 36        4            2               3            5           1      5
## 37        1            1               3            1           5      5
## 38        4            5               5            1           5      3
## 39        4            1               5            1           1      1
## 40        5            5               3            5           4      4
## 41        5            5               5            5           5      2
## 42        2            4               4            2           2      4
## 43        1            5               1            1           1      3
## 44        4            3               4            4           1      1
## 45        5            5               5            5           5      3
## 46        5            2               5            3           1      1
## 47        2            2               5            1           5      1
## 48        4            3               5            3           1      3
## 49        1            2               4            1           5      4
## 50        5            5               5            5           5      5
## 51        5            5               1            5           1      5
## 52        3            3               4            5           4      5
## 53        5            5               5            5           5      5
## 54        4            2               4            5           4      3
## 55        3            2               4            4           4      3
## 56        4            1               2            1           1      1
## 57        4            4               4            5           5      4
## 58        4            5               3            5           1      1
## 59        5            5               5            1           5      5
## 60        5            5               5            5           5      5
## 61        5            5               5            5           1      3
## 62        2            1               1            5           1      5
## 63        2            5               5            5           1      4
## 64        5            5               5            5           1      5
## 65        4            4               4            1           1      5
## 66        4            2               4            4           1      5
## 67        1            3               3            3           1      5
## 68        5            5               5            5           5      4
## 69        4            2               2            3           3      4
## 70        2            2               4            5           1      1
## 71        1            4               5            5           4      5
## 72        5            5               5            5           5      5
## 73        5            5               5            3           3      2
## 74        3            2               1            2           3      4
## 75        1            1               5            1           5      1
## 76        1            3               5            5           5      1
## 77        1            1               3            1           1      3
## 78        5            1               3            5           3      3
## 79        1            1               2            1           4      4
## 80        5            5               4            5           5      5
## 81        4            4               5            5           5      4
## 82        5            5               5            5           5      5
## 83        5            5               5            5           1      5
## 84        5            1               1            1           1      5
## 85        5            5               5            5           2      5
## 86        5            1               5            5           1      5
## 87        5            4               5            5           5      3
## 88        5            5               5            5           5      3
## 89        5            4               5            5           2      5
## 90        5            1               5            5           5      1
## 91        4            4               5            5           5      4
## 92        3            5               5            4           5      5
## 93        3            3               4            4           4      3
## 94        2            5               5            5           5      3
## 95        2            2               2            3           5      4
## 96        4            4               2            4           4      2
## 97        5            3               5            3           1      5
## 98        4            2               1            5           5      5
## 99        5            4               5            4           5      5
## 100       4            5               5            5           1      5
## 101       4            1               5            5           5      4
## 102       5            5               5            5           5      3
## 103       2            2               3            4           3      2
## 104       4            2               5            3           1      3
## 105       5            3               5            5           1      3
## 106       4            5               5            5           5      5
##     Transporte Instalaciones
## 1            4             5
## 2            2             4
## 3            4             4
## 4            5             5
## 5            1             5
## 6            5             4
## 7            1             4
## 8            5             5
## 9            5             5
## 10           2             5
## 11           1             5
## 12           5             5
## 13           5             5
## 14           5             5
## 15           5             5
## 16           5             5
## 17           5             4
## 18           4             5
## 19           1             3
## 20           5             2
## 21           5             5
## 22           4             5
## 23           1             4
## 24           2             2
## 25           4             5
## 26           1             4
## 27           5             5
## 28           5             4
## 29           5             4
## 30           5             5
## 31           5             5
## 32           4             4
## 33           5             5
## 34           5             5
## 35           5             5
## 36           1             5
## 37           1             1
## 38           5             4
## 39           5             2
## 40           5             5
## 41           5             5
## 42           5             5
## 43           1             1
## 44           4             5
## 45           5             5
## 46           5             5
## 47           5             5
## 48           5             2
## 49           5             1
## 50           5             5
## 51           1             5
## 52           5             5
## 53           5             5
## 54           4             4
## 55           5             4
## 56           2             1
## 57           4             4
## 58           5             5
## 59           5             1
## 60           2             5
## 61           5             5
## 62           5             5
## 63           5             1
## 64           2             5
## 65           1             5
## 66           4             3
## 67           4             4
## 68           5             5
## 69           5             5
## 70           5             2
## 71           4             5
## 72           4             5
## 73           5             5
## 74           1             3
## 75           1             1
## 76           5             5
## 77           5             3
## 78           3             5
## 79           1             4
## 80           5             5
## 81           5             5
## 82           5             5
## 83           5             5
## 84           5             1
## 85           5             5
## 86           5             5
## 87           2             5
## 88           5             5
## 89           5             5
## 90           5             5
## 91           5             5
## 92           2             5
## 93           5             5
## 94           5             5
## 95           2             4
## 96           5             4
## 97           1             5
## 98           5             5
## 99           5             5
## 100          3             5
## 101          5             5
## 102          5             5
## 103          4             5
## 104          5             5
## 105          5             5
## 106          5             5
# Library
library(hclust1d)
## Loaded hclust1d version 0.1.1
## To acknowledge my work please cite hclust1d in publications as:
## Nowakowski, Szymon (2023). hclust1d: Hierarchical Clustering of Univariate (1d) Data. https://cran.r-project.org/package=hclust1d

2. Realiza un agrupamiento jerárquico

dist_matrix <- dist(clusterbase)

hclust_result <- hclust1d(dist_matrix)

3. Grafica el dendrograma

plot(hclust_result, cex = 0.6, hang = -1)

4. Determinar el número de grupos

grupos <- 5

5. Realizar la clasificación

segmentos <- kmeans(clusterbase, grupos)
segmentos
## K-means clustering with 5 clusters of sizes 27, 40, 13, 13, 13
## 
## Cluster means:
##    Salario Prestaciones Jornada_Laboral Herramientas Temperatura   Estres
## 1 3.555556     2.074074        3.777778     4.333333    2.703704 3.111111
## 2 4.450000     4.525000        4.850000     4.950000    3.900000 4.325000
## 3 2.076923     1.538462        2.461538     1.461538    3.153846 3.153846
## 4 4.230769     3.538462        3.923077     4.076923    1.769231 4.230769
## 5 3.538462     3.384615        4.769231     1.846154    2.923077 2.846154
##   Transporte Instalaciones
## 1   4.703704      4.592593
## 2   4.625000      4.950000
## 3   2.384615      2.230769
## 4   1.384615      4.692308
## 5   4.923077      3.461538
## 
## Clustering vector:
##   [1] 2 4 3 2 4 2 4 2 5 4 4 2 2 2 2 1 1 2 3 5 1 1 4 3 2 4 2 1 5 2 2 5 5 1 2 4 3
##  [38] 5 5 2 2 5 3 1 2 1 5 5 3 2 4 2 2 1 1 3 2 1 5 2 2 1 5 4 4 1 1 2 1 1 2 2 5 3
##  [75] 3 1 3 1 3 2 2 2 2 3 2 1 2 2 2 1 2 2 1 2 3 1 4 1 2 4 1 2 1 1 1 2
## 
## Within cluster sum of squares by cluster:
## [1] 273.6296 206.5250 187.3846 131.8462 181.0769
##  (between_SS / total_SS =  44.7 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

6. Revisar la asignación de grupos

asignacion <- cbind(clusterbase, cluster=segmentos$cluster)
asignacion
##     Salario Prestaciones Jornada_Laboral Herramientas Temperatura Estres
## 1         5            4               5            5           2      5
## 2         4            4               5            4           3      2
## 3         2            1               1            1           3      1
## 4         5            4               5            5           4      5
## 5         3            1               5            5           4      3
## 6         4            5               5            5           2      4
## 7         2            3               4            4           2      5
## 8         5            4               5            5           4      4
## 9         4            1               4            1           5      4
## 10        5            4               5            5           1      5
## 11        5            5               1            5           5      5
## 12        5            4               5            5           5      4
## 13        4            5               5            5           1      5
## 14        4            4               5            5           1      5
## 15        5            5               5            5           1      5
## 16        4            2               5            5           4      2
## 17        2            2               5            4           3      4
## 18        5            5               5            5           5      5
## 19        1            1               4            1           4      2
## 20        4            1               5            3           3      1
## 21        4            1               5            4           1      5
## 22        4            1               2            4           5      2
## 23        4            3               4            5           1      2
## 24        4            1               3            4           3      4
## 25        4            4               5            5           4      4
## 26        5            2               4            1           1      3
## 27        4            4               5            5           1      5
## 28        3            1               3            4           3      5
## 29        4            5               4            1           1      1
## 30        3            4               5            5           5      3
## 31        5            4               5            5           5      5
## 32        1            5               5            1           1      3
## 33        5            2               5            1           5      5
## 34        4            2               5            5           2      5
## 35        5            4               4            5           3      4
## 36        4            2               3            5           1      5
## 37        1            1               3            1           5      5
## 38        4            5               5            1           5      3
## 39        4            1               5            1           1      1
## 40        5            5               3            5           4      4
## 41        5            5               5            5           5      2
## 42        2            4               4            2           2      4
## 43        1            5               1            1           1      3
## 44        4            3               4            4           1      1
## 45        5            5               5            5           5      3
## 46        5            2               5            3           1      1
## 47        2            2               5            1           5      1
## 48        4            3               5            3           1      3
## 49        1            2               4            1           5      4
## 50        5            5               5            5           5      5
## 51        5            5               1            5           1      5
## 52        3            3               4            5           4      5
## 53        5            5               5            5           5      5
## 54        4            2               4            5           4      3
## 55        3            2               4            4           4      3
## 56        4            1               2            1           1      1
## 57        4            4               4            5           5      4
## 58        4            5               3            5           1      1
## 59        5            5               5            1           5      5
## 60        5            5               5            5           5      5
## 61        5            5               5            5           1      3
## 62        2            1               1            5           1      5
## 63        2            5               5            5           1      4
## 64        5            5               5            5           1      5
## 65        4            4               4            1           1      5
## 66        4            2               4            4           1      5
## 67        1            3               3            3           1      5
## 68        5            5               5            5           5      4
## 69        4            2               2            3           3      4
## 70        2            2               4            5           1      1
## 71        1            4               5            5           4      5
## 72        5            5               5            5           5      5
## 73        5            5               5            3           3      2
## 74        3            2               1            2           3      4
## 75        1            1               5            1           5      1
## 76        1            3               5            5           5      1
## 77        1            1               3            1           1      3
## 78        5            1               3            5           3      3
## 79        1            1               2            1           4      4
## 80        5            5               4            5           5      5
## 81        4            4               5            5           5      4
## 82        5            5               5            5           5      5
## 83        5            5               5            5           1      5
## 84        5            1               1            1           1      5
## 85        5            5               5            5           2      5
## 86        5            1               5            5           1      5
## 87        5            4               5            5           5      3
## 88        5            5               5            5           5      3
## 89        5            4               5            5           2      5
## 90        5            1               5            5           5      1
## 91        4            4               5            5           5      4
## 92        3            5               5            4           5      5
## 93        3            3               4            4           4      3
## 94        2            5               5            5           5      3
## 95        2            2               2            3           5      4
## 96        4            4               2            4           4      2
## 97        5            3               5            3           1      5
## 98        4            2               1            5           5      5
## 99        5            4               5            4           5      5
## 100       4            5               5            5           1      5
## 101       4            1               5            5           5      4
## 102       5            5               5            5           5      3
## 103       2            2               3            4           3      2
## 104       4            2               5            3           1      3
## 105       5            3               5            5           1      3
## 106       4            5               5            5           5      5
##     Transporte Instalaciones cluster
## 1            4             5       2
## 2            2             4       4
## 3            4             4       3
## 4            5             5       2
## 5            1             5       4
## 6            5             4       2
## 7            1             4       4
## 8            5             5       2
## 9            5             5       5
## 10           2             5       4
## 11           1             5       4
## 12           5             5       2
## 13           5             5       2
## 14           5             5       2
## 15           5             5       2
## 16           5             5       1
## 17           5             4       1
## 18           4             5       2
## 19           1             3       3
## 20           5             2       5
## 21           5             5       1
## 22           4             5       1
## 23           1             4       4
## 24           2             2       3
## 25           4             5       2
## 26           1             4       4
## 27           5             5       2
## 28           5             4       1
## 29           5             4       5
## 30           5             5       2
## 31           5             5       2
## 32           4             4       5
## 33           5             5       5
## 34           5             5       1
## 35           5             5       2
## 36           1             5       4
## 37           1             1       3
## 38           5             4       5
## 39           5             2       5
## 40           5             5       2
## 41           5             5       2
## 42           5             5       5
## 43           1             1       3
## 44           4             5       1
## 45           5             5       2
## 46           5             5       1
## 47           5             5       5
## 48           5             2       5
## 49           5             1       3
## 50           5             5       2
## 51           1             5       4
## 52           5             5       2
## 53           5             5       2
## 54           4             4       1
## 55           5             4       1
## 56           2             1       3
## 57           4             4       2
## 58           5             5       1
## 59           5             1       5
## 60           2             5       2
## 61           5             5       2
## 62           5             5       1
## 63           5             1       5
## 64           2             5       4
## 65           1             5       4
## 66           4             3       1
## 67           4             4       1
## 68           5             5       2
## 69           5             5       1
## 70           5             2       1
## 71           4             5       2
## 72           4             5       2
## 73           5             5       5
## 74           1             3       3
## 75           1             1       3
## 76           5             5       1
## 77           5             3       3
## 78           3             5       1
## 79           1             4       3
## 80           5             5       2
## 81           5             5       2
## 82           5             5       2
## 83           5             5       2
## 84           5             1       3
## 85           5             5       2
## 86           5             5       1
## 87           2             5       2
## 88           5             5       2
## 89           5             5       2
## 90           5             5       1
## 91           5             5       2
## 92           2             5       2
## 93           5             5       1
## 94           5             5       2
## 95           2             4       3
## 96           5             4       1
## 97           1             5       4
## 98           5             5       1
## 99           5             5       2
## 100          3             5       4
## 101          5             5       1
## 102          5             5       2
## 103          4             5       1
## 104          5             5       1
## 105          5             5       1
## 106          5             5       2

7. 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=clusterbase,
             palette=c("red","blue","green","purple", "pink"),
             ellipse.type = "euclid",
             star.plot= T,
             repel= T,
             ggtheme = theme()
             )

## 8. Optimizar cantidad de grupos

library(cluster)
library(data.table)
set.seed(123)

9. aleatorio el 123

optimizacion <- clusGap(clusterbase, FUN= kmeans, nstart=1, K.max=10)
plot(optimizacion, xlab="Número de clusters k")

# El punto mas alto de la grafica indica la cantidad de grupos óptimo

5. Perfilado de segmentos

library(dplyr)
## 
## 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
library(cluster)

datos_numericos <- bd[sapply(bd, is.numeric)]

datos_escalados <- scale(datos_numericos)

medianas <- apply(datos_escalados, 2, median)
set.seed(123) # Para reproducibilidad
k <- 3 # Cambia este valor por el número de clusters que deseas
indices_centroides_iniciales <- sample(1:nrow(datos_escalados), k)
centroides_iniciales <- datos_escalados[indices_centroides_iniciales, ]

1. Ejecutar k-means con los centroides iniciales

resultado_kmeans <- kmeans(datos_escalados, centers = centroides_iniciales)

print(resultado_kmeans)
## K-means clustering with 3 clusters of sizes 65, 16, 25
## 
## Cluster means:
##    Antigüedad    Salario Prestaciones Jornada_Laboral Herramientas Temperatura
## 1  0.04581365  0.3626082    0.4480907       0.4960589    0.4534784  0.08958709
## 2 -0.25138960 -1.2012483   -0.9008792      -0.1771124   -1.4441653  0.14653768
## 3  0.04177386 -0.1739824   -0.5884731      -1.1764012   -0.2547780 -0.32671056
##       Estres Transporte Instalaciones  Y_Rotacion         Edad Dependientes
## 1  0.1524490  0.3710519     0.3370262  0.36734886  0.005728939  0.006750785
## 2 -0.9779247 -0.2133885    -1.1244849 -1.39741185 -0.313323737  0.207570080
## 3  0.2295043 -0.8281663    -0.1565978 -0.06076345  0.185631950 -0.150396891
## 
## Clustering vector:
##   [1] 1 1 2 1 3 1 3 1 3 1 3 1 1 1 1 1 1 1 2 2 1 3 3 3 1 3 1 3 1 1 1 2 2 1 1 3 2
##  [38] 2 2 1 1 1 3 1 1 1 2 1 2 1 3 1 1 1 1 2 1 1 1 1 1 3 1 1 3 3 3 1 3 2 1 1 1 3
##  [75] 2 2 2 3 2 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 3 3 3 3 1 1 1 1 3 1 1 1
## 
## Within cluster sum of squares by cluster:
## [1] 500.5013 198.3966 272.8571
##  (between_SS / total_SS =  22.9 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

2. Ajustar el modelo k-means para diferentes valores de k

set.seed(123) # Para reproducibilidad
wcss <- vector() # Inicializar vector para almacenar los valores de WCSS
max_k <- 10 # Puedes cambiar esto para probar con más clústeres si lo deseas
for (k in 1:max_k) {
  kmeans_model <- kmeans(datos_escalados, centers = k, nstart = 25)
  wcss[k] <- kmeans_model$tot.withinss
}

3. Graficar el método del codo

par(mar = c(4, 4, 1, 1)) # Los números representan los márgenes inferior, izquierdo, superior y derecho respectivamente.
plot(1:max_k, wcss, type = "b", xlab = "Numero de Clusters", ylab = "WCSS", main = "Metodo del Codo", pch = 19, frame = FALSE)

4. Establecer una semilla para la reproducibilidad

set.seed(178)

5. Realizar K-means con 4 clusters

kmeans_result <- kmeans(datos_escalados, centers = 4)
kmeans_result$centers
##   Antigüedad    Salario Prestaciones Jornada_Laboral Herramientas Temperatura
## 1  0.5807662 -0.6829375  -0.45964588      -0.4135247   -0.4343081 -0.44284509
## 2 -0.6797783  0.3870086   0.03848613       0.3077267    0.3534262 -0.22329552
## 3  0.6678854  0.5157127   0.87347752       0.4317614    0.5298353  0.74602847
## 4 -0.3334477 -1.0920974  -1.14243027      -1.1869967   -1.5243127  0.02556421
##       Estres  Transporte Instalaciones Y_Rotacion       Edad Dependientes
## 1 -0.5718708  0.07328684    -0.1565978  0.1893696  0.3895726    0.5062559
## 2  0.3317205  0.04109209     0.3882955 -0.1597744 -0.6072619   -0.3380120
## 3  0.2560048  0.33743816     0.3817994  0.6363666  0.6584478    0.1927920
## 4 -0.5456353 -1.05573909    -1.8918372 -1.2671342 -0.1677008   -0.3054399

6. Añadir la columna de clusters al DataFrame original

bd1 <- bd
bd1$Cluster <- kmeans_result$cluster

6. Árbol de Decisiones

1. Seleccionar variables de interes

variables <- bd[,c("Y_Rotacion","Salario", "Temperatura", "Edad", "Prestaciones")]
variables
##     Y_Rotacion Salario Temperatura Edad Prestaciones
## 1            4       5           2   30            4
## 2            5       4           3   54            4
## 3            1       2           3   21            1
## 4            4       5           4   20            4
## 5            3       3           4   43            1
## 6            5       4           2   61            5
## 7            3       2           2   55            3
## 8            5       5           4   29            4
## 9            5       4           5   56            1
## 10           5       5           1   36            4
## 11           5       5           5   56            5
## 12           5       5           5   36            4
## 13           5       4           1   30            5
## 14           4       4           1   24            4
## 15           4       5           1   42            5
## 16           3       4           4   27            2
## 17           4       2           3   26            2
## 18           5       5           5   45            5
## 19           1       1           4   38            1
## 20           2       4           3   21            1
## 21           5       4           1   26            1
## 22           3       4           5   35            1
## 23           3       4           1   22            3
## 24           4       4           3   24            1
## 25           5       4           4   51            4
## 26           5       5           1   46            2
## 27           5       4           1   30            4
## 28           3       3           3   25            1
## 29           5       4           1   54            5
## 30           5       3           5   58            4
## 31           5       5           5   50            4
## 32           4       1           1   25            5
## 33           1       5           5   20            2
## 34           2       4           2   24            2
## 35           3       5           3   27            4
## 36           5       4           1   51            2
## 37           1       1           5   43            1
## 38           1       4           5   26            5
## 39           5       4           1   50            1
## 40           5       5           4   34            5
## 41           5       5           5   30            5
## 42           5       2           2   34            4
## 43           5       1           1   43            5
## 44           4       4           1   51            3
## 45           4       5           5   25            5
## 46           5       5           1   21            2
## 47           2       2           5   28            2
## 48           5       4           1   58            3
## 49           1       1           5   26            2
## 50           5       5           5   36            5
## 51           5       5           1   21            5
## 52           5       3           4   25            3
## 53           5       5           5   68            5
## 54           5       4           4   18            2
## 55           5       3           4   20            2
## 56           3       4           1   53            1
## 57           4       4           5   30            4
## 58           5       4           1   39            5
## 59           5       5           5   49            5
## 60           5       5           5   47            5
## 61           4       5           1   36            5
## 62           3       2           1   26            1
## 63           5       2           1   43            5
## 64           5       5           1   23            5
## 65           4       4           1   22            4
## 66           3       4           1   24            2
## 67           5       1           1   46            3
## 68           3       5           5   30            5
## 69           4       4           3   33            2
## 70           4       2           1   37            2
## 71           4       1           4   54            4
## 72           5       5           5   31            5
## 73           5       5           3   53            5
## 74           3       3           3   46            2
## 75           1       1           5   18            1
## 76           3       1           5   40            3
## 77           4       1           1   35            1
## 78           5       5           3   44            1
## 79           2       1           4   28            1
## 80           5       5           5   42            5
## 81           5       4           5   33            4
## 82           5       5           5   24            5
## 83           5       5           1   29            5
## 84           5       5           1   42            1
## 85           5       5           2   51            5
## 86           2       5           1   20            1
## 87           1       5           5   22            4
## 88           5       5           5   33            5
## 89           5       5           2   45            4
## 90           5       5           5   28            1
## 91           5       4           5   56            4
## 92           3       3           5   28            5
## 93           5       3           4   34            3
## 94           5       2           5   23            5
## 95           4       2           5   41            2
## 96           4       4           4   26            4
## 97           5       5           1   49            3
## 98           1       4           5   43            2
## 99           5       5           5   48            4
## 100          4       4           1   27            5
## 101          4       4           5   20            1
## 102          5       5           5   41            5
## 103          4       2           3   28            2
## 104          3       4           1   21            2
## 105          5       5           1   39            3
## 106          5       4           5   21            5
sapply(variables, function(x) sum(is.na(x)))
##   Y_Rotacion      Salario  Temperatura         Edad Prestaciones 
##            0            0            0            0            0
variables <- na.omit(variables)

2. Construir arbol de decisión

library(rpart)
library(rpart.plot)
library(dplyr)

arbol <- rpart(formula = Y_Rotacion ~ ., data=variables)
rpart.plot(arbol, box.palette = "Blues")

arbol
## n= 106 
## 
## node), split, n, deviance, yval
##       * denotes terminal node
## 
##  1) root 106 171.849100 4.037736  
##    2) Prestaciones< 2.5 40  81.975000 3.275000  
##      4) Salario< 2.5 13  23.230770 2.461538 *
##      5) Salario>=2.5 27  46.000000 3.666667  
##       10) Edad< 43.5 20  34.800000 3.400000 *
##       11) Edad>=43.5 7   5.714286 4.428571 *
##    3) Prestaciones>=2.5 66  52.500000 4.500000  
##      6) Edad< 28.5 18  26.500000 3.833333 *
##      7) Edad>=28.5 48  15.000000 4.750000  
##       14) Salario< 2.5 7   5.428571 4.285714 *
##       15) Salario>=2.5 41   7.804878 4.829268 *
# Seleccionar variables de interes

variableshm <- bd[,c("Salario","Prestaciones","Jornada_Laboral", "Herramientas", "Temperatura", "Estres", "Transporte", "Instalaciones")]
variableshm
##     Salario Prestaciones Jornada_Laboral Herramientas Temperatura Estres
## 1         5            4               5            5           2      5
## 2         4            4               5            4           3      2
## 3         2            1               1            1           3      1
## 4         5            4               5            5           4      5
## 5         3            1               5            5           4      3
## 6         4            5               5            5           2      4
## 7         2            3               4            4           2      5
## 8         5            4               5            5           4      4
## 9         4            1               4            1           5      4
## 10        5            4               5            5           1      5
## 11        5            5               1            5           5      5
## 12        5            4               5            5           5      4
## 13        4            5               5            5           1      5
## 14        4            4               5            5           1      5
## 15        5            5               5            5           1      5
## 16        4            2               5            5           4      2
## 17        2            2               5            4           3      4
## 18        5            5               5            5           5      5
## 19        1            1               4            1           4      2
## 20        4            1               5            3           3      1
## 21        4            1               5            4           1      5
## 22        4            1               2            4           5      2
## 23        4            3               4            5           1      2
## 24        4            1               3            4           3      4
## 25        4            4               5            5           4      4
## 26        5            2               4            1           1      3
## 27        4            4               5            5           1      5
## 28        3            1               3            4           3      5
## 29        4            5               4            1           1      1
## 30        3            4               5            5           5      3
## 31        5            4               5            5           5      5
## 32        1            5               5            1           1      3
## 33        5            2               5            1           5      5
## 34        4            2               5            5           2      5
## 35        5            4               4            5           3      4
## 36        4            2               3            5           1      5
## 37        1            1               3            1           5      5
## 38        4            5               5            1           5      3
## 39        4            1               5            1           1      1
## 40        5            5               3            5           4      4
## 41        5            5               5            5           5      2
## 42        2            4               4            2           2      4
## 43        1            5               1            1           1      3
## 44        4            3               4            4           1      1
## 45        5            5               5            5           5      3
## 46        5            2               5            3           1      1
## 47        2            2               5            1           5      1
## 48        4            3               5            3           1      3
## 49        1            2               4            1           5      4
## 50        5            5               5            5           5      5
## 51        5            5               1            5           1      5
## 52        3            3               4            5           4      5
## 53        5            5               5            5           5      5
## 54        4            2               4            5           4      3
## 55        3            2               4            4           4      3
## 56        4            1               2            1           1      1
## 57        4            4               4            5           5      4
## 58        4            5               3            5           1      1
## 59        5            5               5            1           5      5
## 60        5            5               5            5           5      5
## 61        5            5               5            5           1      3
## 62        2            1               1            5           1      5
## 63        2            5               5            5           1      4
## 64        5            5               5            5           1      5
## 65        4            4               4            1           1      5
## 66        4            2               4            4           1      5
## 67        1            3               3            3           1      5
## 68        5            5               5            5           5      4
## 69        4            2               2            3           3      4
## 70        2            2               4            5           1      1
## 71        1            4               5            5           4      5
## 72        5            5               5            5           5      5
## 73        5            5               5            3           3      2
## 74        3            2               1            2           3      4
## 75        1            1               5            1           5      1
## 76        1            3               5            5           5      1
## 77        1            1               3            1           1      3
## 78        5            1               3            5           3      3
## 79        1            1               2            1           4      4
## 80        5            5               4            5           5      5
## 81        4            4               5            5           5      4
## 82        5            5               5            5           5      5
## 83        5            5               5            5           1      5
## 84        5            1               1            1           1      5
## 85        5            5               5            5           2      5
## 86        5            1               5            5           1      5
## 87        5            4               5            5           5      3
## 88        5            5               5            5           5      3
## 89        5            4               5            5           2      5
## 90        5            1               5            5           5      1
## 91        4            4               5            5           5      4
## 92        3            5               5            4           5      5
## 93        3            3               4            4           4      3
## 94        2            5               5            5           5      3
## 95        2            2               2            3           5      4
## 96        4            4               2            4           4      2
## 97        5            3               5            3           1      5
## 98        4            2               1            5           5      5
## 99        5            4               5            4           5      5
## 100       4            5               5            5           1      5
## 101       4            1               5            5           5      4
## 102       5            5               5            5           5      3
## 103       2            2               3            4           3      2
## 104       4            2               5            3           1      3
## 105       5            3               5            5           1      3
## 106       4            5               5            5           5      5
##     Transporte Instalaciones
## 1            4             5
## 2            2             4
## 3            4             4
## 4            5             5
## 5            1             5
## 6            5             4
## 7            1             4
## 8            5             5
## 9            5             5
## 10           2             5
## 11           1             5
## 12           5             5
## 13           5             5
## 14           5             5
## 15           5             5
## 16           5             5
## 17           5             4
## 18           4             5
## 19           1             3
## 20           5             2
## 21           5             5
## 22           4             5
## 23           1             4
## 24           2             2
## 25           4             5
## 26           1             4
## 27           5             5
## 28           5             4
## 29           5             4
## 30           5             5
## 31           5             5
## 32           4             4
## 33           5             5
## 34           5             5
## 35           5             5
## 36           1             5
## 37           1             1
## 38           5             4
## 39           5             2
## 40           5             5
## 41           5             5
## 42           5             5
## 43           1             1
## 44           4             5
## 45           5             5
## 46           5             5
## 47           5             5
## 48           5             2
## 49           5             1
## 50           5             5
## 51           1             5
## 52           5             5
## 53           5             5
## 54           4             4
## 55           5             4
## 56           2             1
## 57           4             4
## 58           5             5
## 59           5             1
## 60           2             5
## 61           5             5
## 62           5             5
## 63           5             1
## 64           2             5
## 65           1             5
## 66           4             3
## 67           4             4
## 68           5             5
## 69           5             5
## 70           5             2
## 71           4             5
## 72           4             5
## 73           5             5
## 74           1             3
## 75           1             1
## 76           5             5
## 77           5             3
## 78           3             5
## 79           1             4
## 80           5             5
## 81           5             5
## 82           5             5
## 83           5             5
## 84           5             1
## 85           5             5
## 86           5             5
## 87           2             5
## 88           5             5
## 89           5             5
## 90           5             5
## 91           5             5
## 92           2             5
## 93           5             5
## 94           5             5
## 95           2             4
## 96           5             4
## 97           1             5
## 98           5             5
## 99           5             5
## 100          3             5
## 101          5             5
## 102          5             5
## 103          4             5
## 104          5             5
## 105          5             5
## 106          5             5
# Construcción de heat map
cor_matrix <- cor(variableshm) 

heatmap(cor_matrix, 
        col = colorRampPalette(c("blue", "white", "red"))(20),
        main = "Heatmap de correlación entre causas de rotación",
        margins = c(12, 9))

7. Perfilado de nodos

8. Nuevas preguntas, nuevos insights, nuevas visualizaciones

Objetivo: ¿Qué explica la Rotación de Personal?

Sub-objetivos: - Conocer la relación entre el puesto y la probabilidad de que se quede en la empresa - ¿En base al puesto, edad, etc., la persona puede que se quede más tiempo en FORM o no? - Detectar causas relevantes que expliquen el fenómeno de la rotación de personal - Obtener resultados de tiempo en la empresa, utilizando la variables obtenidas en las respuestas de las encuestas

Insights:
Regresión Lineal:
Los factores principales que afectan si un empleado decide quedarse en FORM son las prestaciones y el salario, seguido de edad, temperatura y antigüedad, esta última no decido sin embargo es tomada en cuenta al momento de tomar una decisión.

Árbol de Decisiones:
- Hay mayor probabilidad de que las personas que están satisfechas con las prestaciones sean mayores a 29 años.
- Hay mayor probabilidad de que los colaborares mayores a 29 estén insatisfechos con su salario al percibirlo injusto.

Heatmap: - Hay una correlación positiva fuerte entre la variable de estres con la jornada laboral y el trasnporte, lo cual puede significar que las largas jornadas de trabajo y la complicación de transporte a la planta puede generar grandes niveles de estres.
- La temperatura es uno de los factores que mayormente genera descontento entre los colabores y se relacionan con las variables de herramientas e instalaciones principalmente. Esto quiere decir que las condiciones laborales son percibidas como desfavorables.

9. Dashboard de encuesta de FORM

Imágen de AVANCE del dashboard