A6U1

Equipo 4: Carlos Angulo, Fernando Bocardo, Josue Flores, Oscar Roman

10/3/2022

Caso de estudio: Wage

El set de datos Wage que ya tiene R tiene información sobre unos trabajadores de EEUU. La cual hay 11 variables que son:

  • Año
  • Edad
  • Estado Civil
  • Raza
  • Educación
  • Región
  • Tipo de trabajo
  • Salud
  • Seguro medico
  • Salario logaritmico
  • Sueldo

Por lo que, se utilizara k-means para ver si se puede predecir si una persona tiene o no un seguro médico con base a su salario.

La incognita para este caso de estudio es: ¿Influirá el salario en que las personas tengan seguro?

("")

Datos y paquetes

library(pacman)
p_load("ISLR", "DT", "xfun", "dplyr", "psych", "GGally", "ggplot2","readr","stringr","vembedr", "xfun","gridExtra","corrplot","cluster")

Datos

Se importara los datos a utilizar para el caso de estudio y se creara una tabla para ver mejor los datos.

data("Wage")
datos<-Wage;
datatable(Wage)

Aqui se muestran las 11 variables del caso de estudio:

df<- Wage
head(Wage)
##        year age           maritl     race       education             region
## 231655 2006  18 1. Never Married 1. White    1. < HS Grad 2. Middle Atlantic
## 86582  2004  24 1. Never Married 1. White 4. College Grad 2. Middle Atlantic
## 161300 2003  45       2. Married 1. White 3. Some College 2. Middle Atlantic
## 155159 2003  43       2. Married 3. Asian 4. College Grad 2. Middle Atlantic
## 11443  2005  50      4. Divorced 1. White      2. HS Grad 2. Middle Atlantic
## 376662 2008  54       2. Married 1. White 4. College Grad 2. Middle Atlantic
##              jobclass         health health_ins  logwage      wage
## 231655  1. Industrial      1. <=Good      2. No 4.318063  75.04315
## 86582  2. Information 2. >=Very Good      2. No 4.255273  70.47602
## 161300  1. Industrial      1. <=Good     1. Yes 4.875061 130.98218
## 155159 2. Information 2. >=Very Good     1. Yes 5.041393 154.68529
## 11443  2. Information      1. <=Good     1. Yes 4.318063  75.04315
## 376662 2. Information 2. >=Very Good     1. Yes 4.845098 127.11574

Grafica sobre la relacion entre salario y la edad

ggplot(df, aes(wage,age)) + geom_point(aes (col=health_ins), size=3   )

Aqui se muede ver los 2 grupos de personas, los rojos representan las personas con seguro medico y los azules los que no tienen seguro.

Ademas, se puede observar que los puntos rojos estan mas a la derecha, lo que significa que son los que tienen mas salario, mientras que los que estan a la izquierda son los de menor salario.

Por lo que a simple vista se puede decir que las personas con mas salario son las que si tienen seguro.## Usar clusters para clasificar

wageCluster <-kmeans(data.frame(x=datos$wage,y=datos$age), center=2, nstart = 20 )
wageCluster
## K-means clustering with 2 clusters of sizes 739, 2261
## 
## Cluster means:
##           x        y
## 1 165.70429 45.38295
## 2  94.05367 41.44449
## 
## Clustering vector:
##    [1] 2 2 1 1 2 2 1 2 2 2 2 2 2 1 1 2 2 1 2 2 1 2 2 1 2 1 2 2 2 2 1 2 2 2 2 2 2
##   [38] 2 2 2 2 2 2 2 2 1 1 1 2 2 1 2 1 2 2 2 1 1 2 2 1 2 2 2 2 2 1 1 2 2 2 2 2 2
##   [75] 2 2 1 2 2 2 2 2 2 2 2 2 2 2 1 2 1 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 2 2 2 2 2
##  [112] 2 2 2 2 2 2 1 1 2 2 2 1 1 2 2 1 2 2 2 2 1 1 2 1 2 2 2 2 2 2 2 2 2 2 2 1 1
##  [149] 1 2 2 1 2 2 1 1 2 1 2 2 2 2 2 2 1 2 2 2 2 2 2 2 1 1 1 2 1 2 1 2 2 2 2 2 1
##  [186] 2 2 2 2 2 1 2 2 2 1 1 2 1 2 2 2 2 2 2 2 2 1 1 1 2 1 2 1 2 2 2 1 2 2 2 2 2
##  [223] 2 1 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 2 1 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 1
##  [260] 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 2 1 2 2 2
##  [297] 2 2 1 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 1 2
##  [334] 2 1 2 2 2 2 2 2 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1 2 2 1 2 2 1 2 2 2 1 2 2 1 1 2 1 1 1 1 2 2 2 2 1 2 2 2 2
## [2739] 2 2 2 1 2 2 2 2 2 2 1 2 2 2 1 1 2 2 2 2 2 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 1
## [2776] 2 1 2 2 1 2 2 1 2 1 2 2 2 2 1 2 2 2 2 1 2 1 2 2 2 2 1 2 1 1 1 2 2 1 2 2 2
## [2813] 2 2 1 2 2 2 2 2 1 2 2 2 2 2 1 2 2 1 1 2 1 1 1 2 1 2 2 2 1 1 1 2 2 2 1 2 2
## [2850] 1 2 2 2 2 2 2 1 1 1 2 2 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 1 2 1 2 2 2 2 2
## [2887] 2 2 1 1 2 2 2 1 2 2 2 2 2 1 2 1 2 1 2 2 2 2 2 1 2 1 2 2 2 2 1 2 2 2 1 2 2
## [2924] 2 1 1 2 2 1 2 2 2 2 2 2 1 2 1 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 2 2 2 2 2
## [2961] 2 2 2 2 2 2 2 2 2 2 1 1 2 1 2 2 2 2 2 1 2 2 1 2 2 2 1 1 2 1 1 2 2 2 1 1 2
## [2998] 2 2 2
## 
## Within cluster sum of squares by cluster:
## [1] 1410685 1342984
##  (between_SS / total_SS =  51.0 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

Comparando los clusters con los datos originales

table(wageCluster$cluster, df$health_ins)
##    
##     1. Yes 2. No
##   1    646    93
##   2   1437   824

Juntando los datos originales en clusters

clusplot(data.frame(x=datos$wage,y=datos$age), wageCluster$cluster, color=T, shade=T, lines=0)

Juntando los datos por clusters se puede ver que hay mucha casualidad entre estos 2 datos, por lo la incognita se responde que si tiene que ver tu salario con el seguro.

Conlclusión

Al analizar todo lo anteriore se puede concluir que si tiene que ver tu salario con tener seguro medico o no, ya que, en los estados unidos el tener un seguro medico es muy costoso mantener un seguro y el tener un salario bajo no alcanza para un seguro.

Descarga este código

xfun::embed_file("A6U1.Rmd")

Download A6U1.Rmd