primero instalamos los paquetes

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(descriptr)

library(ggplot2)

library(datos)

library(fdth)
## 
## Attaching package: 'fdth'
## The following objects are masked from 'package:stats':
## 
##     sd, var

luego sacamos una muestra y sembramos una semilla

ola <- na.omit(datos_credito)

set.seed(2210)

muestra_credito <- ola[sample(nrow(ola),size=80),1:14]

sacamos tablas de estado civil y vivienda relacionadas y luego como data frame

estcivil.vivienda <- table(muestra_credito$EstadoCivil, muestra_credito$Vivienda)
estcivil.vivienda
##             
##              ignorar otra propietario padres privado alquila
##   divorciado       0    0           0      0       0       2
##   casado           1    5          33      4       1      12
##   separado         0    0           0      0       0       1
##   soltero          0    2           1     13       1       3
##   viudo            0    0           1      0       0       0
estadocivil <- table(muestra_credito$EstadoCivil)
estadocivil
## 
## divorciado     casado   separado    soltero      viudo 
##          2         56          1         20          1
vivienda <- table(muestra_credito$Vivienda)
vivienda
## 
##     ignorar        otra propietario      padres     privado     alquila 
##           1           7          35          17           2          18
estadocivil1 <- as.data.frame(estadocivil)
estadocivil1
##         Var1 Freq
## 1 divorciado    2
## 2     casado   56
## 3   separado    1
## 4    soltero   20
## 5      viudo    1
vivienda1 <- as.data.frame(vivienda)
vivienda1
##          Var1 Freq
## 1     ignorar    1
## 2        otra    7
## 3 propietario   35
## 4      padres   17
## 5     privado    2
## 6     alquila   18

diagrama de barras de estado civil

estadocivil2 <- rename(estadocivil1, estadoC=Var1, Frecuencia=Freq)

barras_estado <- ggplot(estadocivil2, aes(x = estadoC, y = Frecuencia, fill=estadoC)) +
  geom_bar(stat = 'identity') +
  geom_text(aes(label = estadoC), vjust = -1, colour = "black") +
  ylim(c(0,75)) 
barras_estado

Asignamos colores y hacemos grafico de torta

colores <- hcl.colors(length(estadocivil))
colores
## [1] "#4B0055" "#00588B" "#009B95" "#53CC67" "#FDE333"
torta1 <- pie(estadocivil,main="Estado Civil",radius = 1, col = colores, labels = c(
  "Divorciado","casado","Separado","soltero","viudo"))

finalmente hacemos el grafico de donas

hsize <- 2

df <- estadocivil1 %>% 
  mutate(x = hsize)

ggplot(df, aes(x = hsize, y = Freq, fill= Var1)) +
  geom_col(color= "black") +
  geom_label(aes(label = Freq),
             position = position_stack(vjust = 0.5),
             show.legend = FALSE) +
  coord_polar(theta = "y") +
  scale_fill_brewer(palette = "GnBu") +
  xlim(c(0.2, hsize + 0.5))+
  theme(panel.background = element_rect(fill = "pink"))+
  guides(fill = guide_legend(title = "Estado Civil"))

Hacemos el mismo proceso anterior con vivienda

vivienda2 <- rename(vivienda1, Vivienda=Var1, Frecuencia=Freq)

barras_vivienda <- ggplot(vivienda2, aes(x = Vivienda, y = Frecuencia, fill=Vivienda)) +
  geom_bar(stat = 'identity') +
  geom_text(aes(label = Vivienda), vjust = -1, colour = "black") +
  ylim(c(0,50))
barras_vivienda

colores <- hcl.colors(length(vivienda))
torta1 <- pie(vivienda,main="Vivienda",radius = 1, col = colores, labels = c(
  "Ignorar","Otra","Propietario","Padres","Privado","Alquila"))

hsize <- 2

df2 <- vivienda1 %>% 
  mutate(x = hsize)

ggplot(df2, aes(x = hsize, y = Freq, fill= Var1)) +
  geom_col(color= "black") +
  geom_label(aes(label = Freq),
             position = position_stack(vjust = 0.5),
             show.legend = FALSE) +
  coord_polar(theta = "y") +
  scale_fill_brewer(palette = "GnBu") +
  xlim(c(0.2, hsize + 0.5))+
  theme(panel.background = element_rect(fill = "pink"))+
  guides(fill = guide_legend(title = "Vivienda"))

Creamos una tabla relacionando los ingresos con el trabajo

x <-muestra_credito$Ingresos
y <- muestra_credito$Trabajo
df3 <- data.frame(x = x, y = y)
df3
##      x              y
## 1   80           fijo
## 2  125 tiempo parcial
## 3  138           fijo
## 4  118           fijo
## 5   52           fijo
## 6  178           fijo
## 7   98           fijo
## 8  227           fijo
## 9   49 tiempo parcial
## 10 207           fijo
## 11 300      freelance
## 12 100           fijo
## 13  95           fijo
## 14  67           fijo
## 15 178           fijo
## 16 170           fijo
## 17  85           fijo
## 18 137           fijo
## 19 205           fijo
## 20 191      freelance
## 21 250      freelance
## 22 245           fijo
## 23  62           fijo
## 24 222          otros
## 25 224           fijo
## 26  82 tiempo parcial
## 27 128      freelance
## 28 101           fijo
## 29 150           fijo
## 30 230           fijo
## 31 117           fijo
## 32 185           fijo
## 33 150           fijo
## 34 130           fijo
## 35 169           fijo
## 36 110           fijo
## 37 126 tiempo parcial
## 38 160      freelance
## 39 100           fijo
## 40 112           fijo
## 41  90 tiempo parcial
## 42 145           fijo
## 43 120           fijo
## 44  52          otros
## 45 187           fijo
## 46 142           fijo
## 47 400           fijo
## 48 129      freelance
## 49  45 tiempo parcial
## 50 137          otros
## 51 100           fijo
## 52 110      freelance
## 53  65           fijo
## 54 119           fijo
## 55  50      freelance
## 56 212      freelance
## 57 245      freelance
## 58 202           fijo
## 59  77           fijo
## 60  70           fijo
## 61 170      freelance
## 62 125           fijo
## 63 128           fijo
## 64 170      freelance
## 65  65           fijo
## 66  60           fijo
## 67 110           fijo
## 68 160           fijo
## 69  42      freelance
## 70 150           fijo
## 71  91      freelance
## 72  61      freelance
## 73 160           fijo
## 74 250           fijo
## 75 131 tiempo parcial
## 76 139           fijo
## 77 178      freelance
## 78 200           fijo
## 79  80           fijo
## 80 120           fijo

posteriormente lo graficamos en un grafico de caja

p <- ggplot(df3, aes(x = x, y = y)) + labs(x = "Ingresos", y = "Trabajo") +
  theme(axis.title.x = element_text(size = 15,
                                    color = "blue",
                                    face = "bold"),
        axis.title.y = element_text(size = 15,
                                    color = "red",
                                    face = "italic"))+
  stat_boxplot(geom = "errorbar",
               width = 5) +
  geom_boxplot(fill = "dodgerblue1",
               colour = "black",
               alpha = 5,
               outlier.colour = "tomato2")
p

creamos una tabla de frecuencias para precio y creamos su grafica de histograma y poligonos

tabla1 <- fdt(muestra_credito$Precio)
plot(tabla1, col="cadetblue",type = "rfh", main = "Histograma del precio")

grafico_poligono <- plot(tabla1, col="cadetblue",type = "fp", main = "grafico de poligono del precio")

Hacemos un analisis cuantitativo completo para 3 variables numéricas resumidas en una sola tabla.

empezando por gastos

gastos <- muestra_credito$Gastos

promedio <- sum(gastos)/length(gastos)

gast.ord <- sort(x=gastos,decreasing = FALSE)

mediana <- median(gastos)

moda <- ds_mode(gastos)

rango <- range(gastos,na.rm = FALSE)

varianza_gastos <- round(var(gastos),5)

desvest_gastos <- round(sd(gastos),5)

coefvar_gastos <- round(ds_cvar(gastos),3)

cuartil1_gastos <-  quantile(x = gastos,probs = c(0.25,0.5,0.75,1),type = 6)

cuartil1_gastos <-  quantile(x = gastos,probs = c(0.25),type = 6)

cuartil2_gastos <-  quantile(x = gastos,probs = c(0.5),type = 6)

cuartil3_gastos <-  quantile(x = gastos,probs = c(0.75),type = 6)

cuartil4_gastos <-  quantile(x = gastos,probs = c(1),type = 6)

tabla.gastos<- matrix(c(53.45,45,35,95,392.83291,19.82001,37.081,35,45,60,130),nrow = 11)

rownames(tabla.gastos) <- c("Promedio","Mediana","Moda","rango(35-130)","varianza",
                            "desviacion estandar","coeficiente de variacion",
                            "cuartil 1","cuartil 2", "cuartil 3", "cuartil 4")

colnames(tabla.gastos) <- c("Gastos")

tabla.gastos <- data.frame(tabla.gastos)
tabla.gastos
##                             Gastos
## Promedio                  53.45000
## Mediana                   45.00000
## Moda                      35.00000
## rango(35-130)             95.00000
## varianza                 392.83291
## desviacion estandar       19.82001
## coeficiente de variacion  37.08100
## cuartil 1                 35.00000
## cuartil 2                 45.00000
## cuartil 3                 60.00000
## cuartil 4                130.00000

seguimos con ingresos

ingresos <- muestra_credito$Ingresos

promedioi <- sum(ingresos)/length(ingresos)

medianai <- median(ingresos)

modai <- ds_mode(ingresos)

rangoi <- range(ingresos,na.rm = FALSE)

varianza_ingresos <- round(var(ingresos),5)

desvest_ingresos <- round(sd(ingresos),5)

coefvar_ingresos <- round(ds_cvar(ingresos),3)

cuartil1_ingresos <-  quantile(x = ingresos,probs = c(0.25,0.5,0.75,1),type = 6)

cuartil1_ingresos <-  quantile(x = ingresos,probs = c(0.25),type = 6)

cuartil2_ingresos <-  quantile(x = ingresos,probs = c(0.5),type = 6)

cuartil3_ingresos <-  quantile(x = ingresos,probs = c(0.75),type = 6)

cuartil4_ingresos <-  quantile(x = ingresos,probs = c(1),type = 6)

tabla.ingresos<- matrix(c(170.3375,143,90,462,8798.22642,93.79886,55.066,94.5,143,230,500),nrow = 11)

rownames(tabla.ingresos) <- c("Promedio","Mediana","Moda","rango(38-500)","varianza",
                            "desviacion estandar","coeficiente de variacion",
                            "cuartil 1","cuartil 2", "cuartil 3", "cuartil 4")

colnames(tabla.ingresos) <- c("ingresos")

tabla.ingresos <- data.frame(tabla.ingresos)
tabla.ingresos
##                            ingresos
## Promedio                  170.33750
## Mediana                   143.00000
## Moda                       90.00000
## rango(38-500)             462.00000
## varianza                 8798.22642
## desviacion estandar        93.79886
## coeficiente de variacion   55.06600
## cuartil 1                  94.50000
## cuartil 2                 143.00000
## cuartil 3                 230.00000
## cuartil 4                 500.00000

y finalmente con cantidad

cantidad <- muestra_credito$Cantidad

promedioc <- sum(cantidad)/length(cantidad)

medianai <- median(cantidad)

modac <- ds_mode(cantidad)

rangoc <- range(cantidad,na.rm = FALSE)

varianza_cantidad <- round(var(cantidad),5)

desvest_cantidad <- round(sd(cantidad),5)

coefvar_cantidad <- round(ds_cvar(cantidad),3)

cuartil1_cantidad <-  quantile(x = cantidad,probs = c(0.25,0.5,0.75,1),type = 6)

cuartil1_cantidad <-  quantile(x = cantidad,probs = c(0.25),type = 6)

cuartil2_cantidad <-  quantile(x = cantidad,probs = c(0.5),type = 6)

cuartil3_cantidad <-  quantile(x = cantidad,probs = c(0.75),type = 6)

cuartil4_cantidad <-  quantile(x = cantidad,probs = c(1),type = 6)

tabla.cantidad<- matrix(c(1045.7125,1000,1300,4300,391269.44794,625.51535,59.817,625,1000,1300,4500),nrow = 11)

rownames(tabla.cantidad) <- c("Promedio","Mediana","Moda","rango(200-4500)","varianza",
                              "desviacion estandar","coeficiente de variacion",
                              "cuartil 1","cuartil 2", "cuartil 3", "cuartil 4")

colnames(tabla.cantidad) <- c("cantidad")

tabla.cantidad <- data.frame(tabla.cantidad)
tabla.cantidad
##                             cantidad
## Promedio                   1045.7125
## Mediana                    1000.0000
## Moda                       1300.0000
## rango(200-4500)            4300.0000
## varianza                 391269.4479
## desviacion estandar         625.5154
## coeficiente de variacion     59.8170
## cuartil 1                   625.0000
## cuartil 2                  1000.0000
## cuartil 3                  1300.0000
## cuartil 4                  4500.0000