#Hacer las variables numéricas
data$edad <- as.numeric(data$edad)
## Warning: NAs introducidos por coerción
data$imc <- as.numeric(data$imc)
## Warning: NAs introducidos por coerción
data$hijos <- as.numeric(data$hijos)
data$clm <- as.numeric(data$clm)
## Warning: NAs introducidos por coerción
#verificar datos duplicados
sum(duplicated(data) | duplicated(data, fromLast = TRUE))
## [1] 2
datosduplicados <- data[duplicated(data), ]
print(datosduplicados)
## edad sexo imc hijos fumador region clm
## 582 19 masculino 30.59 0 no noroeste 1639.563
#eliminar datos duplicados
newdata <- data %>%
distinct()
#identificar los NA's
sum(is.na(newdata))
## [1] 152
colSums(is.na(newdata))
## edad sexo imc hijos fumador region clm
## 72 0 39 0 0 0 41
#Eliminar los datos faltantes con interpolación
newdatainterpol<-data.frame(lapply(newdata,
function(x) ifelse(is.na(x),
na.approx(x, na.rm=T), x)))
str(newdatainterpol)
## 'data.frame': 1337 obs. of 7 variables:
## $ edad : num 19 18 28 33 32 31 46 37 37 60 ...
## $ sexo : chr "femenino" "masculino" "masculino" "masculino" ...
## $ imc : num 27.9 33.8 33 22.7 28.9 ...
## $ hijos : num 0 1 3 0 0 0 1 3 2 0 ...
## $ fumador: chr "yes" "no" "no" "no" ...
## $ region : chr "suroeste" "sureste" "sureste" "noroeste" ...
## $ clm : num 16885 1726 4449 21984 3867 ...
#comprobarción de los NA's
sum(is.na(newdatainterpol))
## [1] 0
colSums(is.na(newdatainterpol))
## edad sexo imc hijos fumador region clm
## 0 0 0 0 0 0 0
#Tabla resumen
summary(newdatainterpol)
## edad sexo imc hijos
## Min. :18.00 Length:1337 Min. :15.96 Min. :0.000
## 1st Qu.:27.00 Class :character 1st Qu.:26.22 1st Qu.:0.000
## Median :39.00 Mode :character Median :30.40 Median :1.000
## Mean :39.29 Mean :30.62 Mean :1.096
## 3rd Qu.:51.00 3rd Qu.:34.58 3rd Qu.:2.000
## Max. :64.00 Max. :53.13 Max. :5.000
## fumador region clm
## Length:1337 Length:1337 Min. : 1122
## Class :character Class :character 1st Qu.: 4796
## Mode :character Mode :character Median : 9411
## Mean :13274
## 3rd Qu.:16819
## Max. :63770
Se utilizó en método de interpolacion dado que es bueno ya que permite estimar los datos faltantes en la base de una forma coherente porque mantiene la relación entre las variables.
# Seleccionar solo las variables numéricas
datanumer <- newdatainterpol %>% select(edad, imc, hijos, clm)
# Convertir a formato largo para ggplot
datanumerlong <- pivot_longer(datanumer, cols = everything(), names_to = "Variable", values_to = "Valor")
#A) Crear histogramas con facet_wrap()
ggplot(datanumerlong, aes(x = Valor)) +
geom_histogram(bins = 30, fill = "red", color = "black", alpha=0.7) +
facet_wrap(~ Variable, scales = "free") + # Un panel por variable
theme_minimal() +
labs(title = "Histogramas de Variables Numéricas", x = "Valor", y = "Frecuencia")
Un histograma es una herramienta clave en el análisis univariante, ya que facilita la visualización clara de la distribución de una variable
#B) Media del monto de reclamacion por sexo
newdatainterpol %>%
group_by(sexo) %>%
summarise(media_clm = mean(clm, na.rm = TRUE))
## # A tibble: 2 × 2
## sexo media_clm
## <chr> <dbl>
## 1 femenino 12517.
## 2 masculino 14017.
ggplot(newdatainterpol, aes(x = sexo, y = clm, fill = sexo)) +
geom_col(alpha = 0.7) +
theme_minimal() +
labs(title = "Distribución del monto de reclamación por Sexo", x = "Sexo", y = "Monto de Reclamacion") +
scale_fill_manual(values = c("lightblue", "lightpink"))
#C) Media del monto de reclamacion por fumador y sexo
newdatainterpol %>%
group_by(sexo, fumador) %>%
summarise(media_clm = mean(clm, na.rm = TRUE))
## `summarise()` has grouped output by 'sexo'. You can override using the
## `.groups` argument.
## # A tibble: 4 × 3
## # Groups: sexo [2]
## sexo fumador media_clm
## <chr> <chr> <dbl>
## 1 femenino no 8935.
## 2 femenino yes 29556.
## 3 masculino no 8206.
## 4 masculino yes 32874.
ggplot(newdatainterpol, aes(x=reorder(fumador, -clm), y=clm, fill=sexo))+
geom_bar(stat="identity", position = "dodge")+
labs(title="Monto de Reclamaciones por Fumador y Sexo",
x= "Fumador",
y="Monto de Reclamaciones")+
theme_light()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
scale_fill_manual(values = c("lightblue","lightpink"))
#D) Region con mayor monto de reclamacion
regionclm<- newdatainterpol %>%
group_by(region)%>%
summarise(n=mean(clm))%>%
top_n(1)%>%
arrange(desc(n))
## Selecting by n
regionclm
## # A tibble: 1 × 2
## region n
## <chr> <dbl>
## 1 sureste 14588.
regionclm1<- newdatainterpol %>%
group_by(region)%>%
summarise(n=mean(clm))
ggplot(regionclm1, aes(x = reorder(region, -n), y = n, fill = region)) +
geom_col() +
theme_minimal() +
labs(title = "Promedio del Monto de reclamación por Región",
x = "Región",
y = "Monto de reclamacion") +
theme(legend.position = "none")
#E) Crear variable obesisdad si el imc>30
newdataobe <- newdatainterpol %>%
mutate(obesidad = ifelse(imc > 30, "Sí", "No"))
#F) Top 10 de personas con más obesidad indicando el sexo, edad, hijos, clm y región
top_obesos <- newdataobe %>%
filter(imc > 30) %>%
arrange(desc(imc)) %>%
select(sexo, edad, hijos, clm, region, imc)%>%
top_n(10)
## Selecting by imc
print(top_obesos)
## sexo edad hijos clm region imc
## 1 masculino 18 0 1163.463 sureste 53.13
## 2 masculino 22 1 44501.398 sureste 52.58
## 3 masculino 23 1 2438.055 sureste 50.38
## 4 masculino 58 0 11381.325 sureste 49.06
## 5 masculino 52 1 9748.911 sureste 47.74
## 6 femenino 37 2 46113.511 suroeste 47.60
## 7 masculino 47 1 8083.920 sureste 47.52
## 8 femenino 54 0 63770.428 sureste 47.41
## 9 femenino 52 5 12592.534 sureste 46.75
## 10 femenino 54 2 11538.421 suroeste 46.70