Correlaciones entre variables del CENSO de Viviendas, Hogares y Personas e Ingresos promedios comunales de la CASEN 2017.

VE-CC-AJ

DataIntelligence

Viernes 09-07-2021

1 Nivel nacional URBANO (código 1)

1.1 Pregunta P03C: Material de construcción del piso

Esta pregunta posee 5 categorias de respuesta:

1 Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera
2 Radier sin revestimiento
3 Baldosa de cemento
4 Capa de cemento sobre tierra
5 Tierra

Leemos las respuestas a la pregunta P03C del censo de viviendas 2017 y obtenemos la tabla de frecuencias por categoría:

tabla_con_clave <- readRDS("censos/censo_viviendas_con_clave_17.rds")
tabla_con_clave_u <- filter(tabla_con_clave, tabla_con_clave$AREA == 1)
b <- tabla_con_clave_u$COMUNA
c <- tabla_con_clave_u$P03C
cross_tab =  xtabs( ~ unlist(b) + unlist(c))
tabla <- as.data.frame(cross_tab)
d <-tabla[!(tabla$Freq == 0),]
d$anio <- "2017"

categorias <- sort(unique(tabla_con_clave_u$P03C ))
categorias <- as.data.frame(categorias)
names(categorias)[1] <- "cat"

categorias <- filter(categorias, categorias$cat != 99)
categorias <- filter(categorias, categorias$cat != 98)

d_t <- filter(d,d$unlist.c. == categorias[1,1])
for(i in categorias[2,1]:categorias[nrow(categorias),1]){
  d_i <- filter(d,d$unlist.c. == i)
  d_t = merge( x = d_t, y = d_i, by = "unlist.b.", all.x = TRUE)
}
 
# Agregamos un cero a los códigos comunales de 4 dígitos, que queda en la columna llamada **código**:
codigos <- d_t$unlist.b.
rango <- seq(1:nrow(d_t))
cadena <- paste("0",codigos[rango], sep = "")
cadena <- substr(cadena,(nchar(cadena)[rango])-(4),6)
codigos <- as.data.frame(codigos)
cadena <- as.data.frame(cadena)
comuna_corr <- cbind(d_t,cadena)
comuna_corr <- comuna_corr[,-c(1),drop=FALSE] 
 
names(comuna_corr)[ncol(comuna_corr)] <- "código"

quitar <- seq(3,(ncol(comuna_corr)-1),3)
comuna_corr <- comuna_corr[,-c(quitar),drop=FALSE]


names(comuna_corr)[2] <- "Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera"
names(comuna_corr)[4] <- "Radier sin revestimiento"
names(comuna_corr)[6] <- "Baldosa de cemento"
names(comuna_corr)[8] <- "Capa de cemento sobre tierra"
names(comuna_corr)[10] <- "Tierra"


renombrar <- seq(1,(ncol(comuna_corr)-2),2)
vv <- 0
for (v in renombrar) {
  vv <- vv+1
  contador <- paste0("categoria_",vv)
  names(comuna_corr)[v] <- contador
}

ingresos_expandidos_2017 <- readRDS("Ingresos_expandidos_urbano_17.rds")

df_2017_2 = merge( x = comuna_corr, y = ingresos_expandidos_2017, by = "código", all.x = TRUE)
#df_2017_2 <- filter(df_2017_2, df_2017_2$Ingresos_expandidos != 'is.na')
df_2017_2 <- df_2017_2[,-c(2,4,6,8,10,12)]
union_final_urb <- df_2017_2 

kbl(union_final_urb) %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  kable_paper() %>%
  scroll_box(width = "100%", height = "300px")
código Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera Radier sin revestimiento Baldosa de cemento Capa de cemento sobre tierra Tierra ingresos_expandidos
01101 49859 1037 2489 831 249 71930106513
01107 21446 1486 2477 1469 1206 33766585496
01401 2057 248 220 152 39 4966851883
01404 236 24 53 24 13 NA
01405 909 148 128 60 15 3068247619
02101 87176 3136 4361 1589 888 133249367039
02102 2420 172 169 96 25 4979702302
02104 2603 160 185 155 51 5109282942
02201 39580 1288 2708 792 320 71981127235
02203 954 270 127 112 45 4869699464
02301 6880 127 395 95 33 7207910819
02302 1309 6 24 6 11 3084818966
03101 39450 1059 1731 1300 516 52819016037
03102 3978 280 293 237 16 5628052276
03103 2245 58 274 234 31 4671058718
03201 2874 104 179 72 19 3499391196
03202 3868 94 100 116 18 4895805596
03301 11065 626 1046 757 170 16404810756
03303 1068 92 87 116 17 2035200054
03304 2305 172 219 115 9 3424422750
04101 57216 916 2111 943 81 61749247282
04102 57404 1356 2714 1285 196 61277269093
04103 2519 94 190 202 38 2855312920
04104 317 10 29 35 2 908664019
04106 4524 163 421 194 31 7058750373
04201 6024 135 337 208 37 8703433491
04202 552 34 47 53 9 2122281844
04203 5163 152 235 179 13 6098444926
04204 4129 239 334 229 44 7690585032
04301 23568 508 1728 655 103 31197719080
04302 1611 127 174 148 23 3124505460
04303 3402 273 581 469 65 6930326684
04304 1329 162 192 197 25 2328107498
05101 92530 745 2439 846 84 90946261553
05102 5345 84 167 102 5 9352095757
05103 12187 129 375 127 10 14075920021
05105 4784 200 272 97 10 5490274928
05107 7851 264 409 178 16 9839456903
05109 110022 1173 2969 1154 91 118563074323
05201 1986 120 114 51 10 NA
05301 17576 164 1231 199 21 23711104774
05302 2929 89 193 83 11 3654416747
05303 2165 68 149 90 5 2855998928
05304 3261 81 193 75 3 4140022481
05401 7528 144 604 242 23 9183080280
05402 3258 80 274 201 11 5094117762
05403 1592 50 70 39 2 1921527704
05404 1152 45 128 97 4 2333781007
05405 1483 56 72 45 1 2160521991
05501 23343 296 922 297 27 26131733924
05502 14009 178 619 310 34 14297866792
05503 3235 87 130 96 5 4828872604
05504 5971 26 153 73 7 7414857001
05506 5243 112 214 140 18 5749381300
05601 24827 507 930 344 11 22527241144
05602 3615 83 165 37 4 5398446270
05603 6262 222 364 94 20 5569658994
05604 4946 104 242 60 6 4315799297
05605 4250 155 263 66 19 3816682340
05606 1931 16 47 13 1 4408732520
05701 20014 244 1652 389 66 23208536043
05702 2077 62 189 81 3 3264869972
05703 5104 160 321 113 8 7275684301
05704 993 36 111 48 NA 2385858928
05705 1609 52 227 91 15 5187514898
05706 2781 69 153 118 21 3907844674
05801 46170 436 1769 495 45 52247193426
05802 11069 237 545 246 37 14176705125
05803 3551 133 225 135 24 5149662271
05804 37648 354 1481 409 56 45800670899
06101 72092 1299 3036 593 138 76977097284
06102 1689 150 170 118 11 3758801352
06103 941 68 42 34 6 1651985453
06104 2977 233 153 121 8 5466110795
06105 4506 201 364 140 17 6402533884
06106 8121 324 408 194 16 10426820415
06107 2608 157 179 130 10 6894533314
06108 15114 368 519 188 31 16602037093
06109 1460 81 81 78 5 2863689033
06110 5367 465 280 291 34 7392597596
06111 2446 147 157 128 20 4054025678
06112 3133 173 270 137 3 3559462966
06113 2272 220 214 221 10 4616762518
06114 1841 143 162 144 7 2741286093
06115 12599 726 807 508 52 17273974762
06116 4246 164 261 163 12 8078983811
06117 7294 303 478 374 38 13358975033
06201 4122 168 220 57 6 5643258336
06202 379 16 21 6 1 891866686
06203 1033 72 47 24 4 1881627117
06204 1038 38 44 38 2 2458260033
06206 624 22 14 6 2 1475951353
06301 18801 709 1099 392 62 24041131495
06302 2339 186 140 160 17 3691714537
06303 5594 407 331 269 15 9228754903
06304 677 76 39 44 3 1611947197
06305 3075 235 201 131 1 4386786331
06306 880 70 55 70 4 3079870843
06307 1954 112 70 71 5 2923796850
06308 666 30 21 29 3 2102134220
06310 6807 213 274 103 1 11393463346
07101 62934 1541 2841 658 50 67732753814
07102 11594 135 473 88 11 12932986800
07103 1123 70 25 18 8 2662971120
07104 906 22 53 20 1 866652110
07105 10792 602 603 182 13 12182624190
07106 633 96 63 43 6 1825700105
07107 949 71 35 28 9 1926795579
07108 1149 132 81 74 8 3126961590
07109 5746 538 344 205 12 10687595452
07110 1298 159 85 58 6 2294886656
07201 9719 435 815 344 47 9515918892
07202 1150 50 68 31 5 2234922252
07203 1259 34 98 31 5 1534908448
07301 38787 1020 2408 752 43 42117028333
07302 1782 83 98 67 7 2928781043
07303 1570 45 85 28 6 1741750148
07304 10380 468 823 372 8 12009998195
07305 1529 143 81 85 2 2845428741
07306 1799 96 116 43 NA 4085560646
07307 2142 140 166 121 18 4611045339
07308 2647 177 165 104 3 7579820261
07309 379 13 8 8 4 943414066
07401 23108 539 1261 484 48 25291751487
07402 2626 221 159 78 6 4173410967
07403 2475 107 186 138 18 6597394825
07404 9100 491 635 297 31 11091040324
07405 2144 87 139 72 11 4508431050
07406 9437 393 529 252 10 12687530322
07407 2951 162 183 122 10 4251702731
07408 1624 145 153 78 14 4412680158
08101 69525 853 2193 590 23 72227728923
08102 31129 458 2813 623 17 32278209118
08103 24474 401 1182 299 13 25641323296
08104 1324 48 126 13 NA 2469551785
08105 5569 150 524 124 6 5651905803
08106 11685 165 1402 273 9 12339953990
08107 13031 316 793 248 1 12561435651
08108 37950 1018 1938 515 4 36167321662
08109 2793 58 188 47 3 3582304723
08110 42706 734 2152 555 11 48602104064
08111 14896 235 848 176 17 15133299927
08112 26228 376 1410 233 6 26380344663
08201 6095 145 766 143 5 6534231082
08202 7305 113 968 162 4 11466769473
08203 5729 72 410 66 2 8327773342
08204 974 6 52 3 NA 1284670805
08205 8173 87 996 151 3 8488900056
08206 5506 62 384 65 5 4840107033
08207 1147 20 60 15 1 2305775206
08301 43843 1056 2594 564 22 60441208918
08302 638 21 31 7 NA 781935233
08303 6119 206 500 140 7 6433682620
08304 4557 163 505 121 7 5024717382
08305 6549 198 585 175 6 7299407611
08306 6232 125 558 228 12 7697899431
08307 1671 60 150 32 4 1916060576
08308 574 18 32 15 NA 784682868
08309 1654 48 78 32 4 1935917806
08310 873 13 72 19 3 705390056
08311 2293 77 166 49 1 3454949584
08312 3541 125 256 123 4 3035048397
08313 3666 172 234 111 13 4693613938
09101 80348 1376 3403 631 29 83174794799
09102 3576 120 356 93 4 5824543339
09103 2491 99 234 65 1 4330659433
09104 647 10 67 17 NA 1529109215
09105 2238 71 111 33 1 7518158340
09106 1111 48 80 38 NA 2930258102
09107 2982 83 312 43 2 3670206245
09108 7520 254 636 198 11 11267725602
09109 4960 158 408 80 2 5049235445
09110 751 32 87 41 3 1301137941
09111 4992 162 457 89 3 7867935676
09112 12764 346 965 136 10 21191399108
09113 1033 31 71 26 NA 1799419624
09114 4705 122 417 120 2 6204576082
09115 5151 99 456 93 2 7443587942
09116 857 28 53 3 2 2856420491
09117 1706 35 103 28 NA 3380311968
09118 1247 13 54 8 1 2135976054
09119 4509 99 349 77 NA 4860243131
09120 10531 167 918 142 7 13650235814
09121 1063 42 119 36 1 2940207311
09201 13797 474 903 227 21 14296297282
09202 5131 130 515 144 10 6098134776
09203 3867 109 374 90 8 4084643011
09204 1093 38 68 24 4 2072995481
09205 1017 38 142 24 6 2568496128
09206 1169 29 213 46 7 1547086780
09207 1306 45 50 13 4 2697526159
09208 2240 50 253 38 3 2757928013
09209 2398 98 222 64 1 2726714090
09210 4266 221 266 78 1 4863416659
09211 7275 128 543 105 3 7723760970
10101 63143 1545 3927 1088 14 74854925754
10102 4756 35 226 65 NA 9545646863
10104 2136 30 203 80 1 2742371891
10105 3872 74 294 97 2 5188291726
10106 2461 48 118 43 NA 3980600731
10107 3775 107 398 75 1 4422233283
10108 2186 21 47 16 NA 3825279050
10109 9409 174 474 103 2 13920663786
10201 10194 82 601 151 1 15304799118
10202 8595 133 386 183 4 8960055930
10203 1754 13 27 24 3 3559637517
10204 347 NA 14 16 NA NA
10205 2111 6 97 45 NA 4477578923
10207 712 1 25 39 NA 987644627
10208 5354 61 288 145 5 6579532876
10209 812 11 26 24 NA 2572844097
10210 1035 4 30 15 1 2996397098
10301 44788 1010 2609 532 13 43850482486
10302 615 4 16 7 NA 2359078294
10303 4039 128 389 127 NA 6161043438
10304 1449 12 105 8 NA 2689725003
10305 2044 84 154 71 NA 3896715111
10306 425 2 3 3 NA 1674503801
10307 1316 37 123 45 1 1962995435
10401 570 6 10 1 NA NA
10402 654 11 37 6 NA NA
10403 1216 6 28 4 NA NA
10404 420 1 9 4 2 NA
11101 14936 312 1106 154 12 18912283227
11201 5663 120 618 156 3 7375332218
11202 1281 20 30 11 1 1642095149
11203 454 NA 2 NA 2 NA
11301 841 31 77 15 3 1224029692
11401 980 30 86 33 1 1622211456
12101 38151 565 1284 449 22 51552266922
12201 518 4 12 NA NA NA
12301 1716 36 76 10 NA 3034981682
12401 5798 87 472 150 2 7233637635
13101 158608 584 1897 453 50 182367246208
13102 21611 551 705 204 26 22371586546
13103 31419 2021 1812 633 74 35892031153
13104 31139 1010 2901 421 39 39397353402
13105 41317 1739 1697 587 46 45770170398
13106 39677 1053 1860 512 40 50093952387
13107 24312 815 1382 269 27 31106038806
13108 30263 303 721 183 19 37720956327
13109 26739 426 732 276 22 33097323323
13110 104880 1890 3666 710 69 128231071590
13111 27737 2003 2173 677 36 35760286668
13112 39452 3089 2946 1022 87 41256447003
13113 26946 169 620 102 17 40307459856
13114 103210 140 411 69 2 134598169599
13115 24935 354 484 133 16 36968385127
13116 22380 1337 1284 434 34 26099479542
13117 25626 682 1463 319 26 29397444939
13118 36202 532 1202 190 24 40285970358
13119 141696 1285 6572 583 79 187034167391
13120 78686 108 844 38 NA 88804766896
13121 25296 907 1354 334 32 32058321741
13122 61009 2663 2261 883 76 77691132095
13123 59113 21 127 39 1 73330144381
13124 58178 1253 2529 500 57 73825647438
13125 54711 407 3070 212 23 80689241762
13126 31174 700 1085 330 52 34298531093
13127 41812 1431 1975 661 71 54458123369
13128 36878 1297 2245 545 42 43262464632
13129 25518 616 1158 269 24 31753732439
13130 35881 259 457 124 6 37960091353
13131 19499 1341 1402 401 56 23331343432
13132 27408 35 126 7 1 42430139879
13201 147225 2869 9177 1019 67 186533464474
13202 2725 218 199 113 15 8817024774
13203 2856 159 190 99 11 6933981276
13301 28815 1064 1227 444 53 43951136523
13302 20685 959 1007 300 63 38020316317
13303 3283 159 271 91 8 6325126322
13401 74178 2203 4405 935 115 86474375157
13402 21811 783 1111 396 26 30431412042
13403 2863 150 194 62 12 7803125477
13404 11605 597 853 279 28 24020488982
13501 23550 979 1254 483 32 36054817558
13502 635 43 85 39 3 2251756129
13503 5101 285 445 206 12 8766848005
13504 1676 139 115 64 13 3451350898
13601 15373 375 1019 289 19 29299162746
13602 7430 520 726 233 18 10693979408
13603 6719 369 507 284 15 8304441408
13604 14199 631 791 355 15 17555873230
13605 21801 648 1161 337 25 31711490484
14101 46462 690 2129 401 11 51277944139
14102 1135 9 20 5 NA 1179821617
14103 3450 51 266 79 5 4477574931
14104 2849 48 185 54 NA 4159328181
14105 1139 35 136 35 NA 2235081533
14106 2601 93 272 120 NA 5342147079
14107 3486 28 351 63 1 4508111622
14108 4507 55 325 90 2 9938682028
14201 7643 157 810 218 2 9405987850
14202 2449 41 150 45 3 3627119212
14203 821 10 22 11 NA 2445838259
14204 5000 188 563 146 5 8405637271
15101 48243 3296 3731 1492 527 68625788545
15201 150 33 39 49 24 784324030
16101 51064 1092 2193 480 33 50965643906
16102 4043 137 185 70 3 4829367278
16103 8133 151 431 98 15 8022762560
16104 1329 65 125 63 7 2596282563
16105 1140 58 71 50 1 2213691761
16106 1670 54 101 39 5 1901248804
16107 3012 158 311 89 17 4477425886
16108 1953 121 144 80 19 3269367252
16109 4012 112 342 117 12 4599738091
16201 3100 83 173 49 10 2932397811
16202 488 19 32 7 2 1300549630
16203 3344 50 143 62 3 4748629723
16204 434 33 38 14 NA 1571687052
16205 527 36 37 20 3 957181342
16206 487 21 21 10 2 1650320432
16207 546 7 42 17 2 1181211462
16301 10072 301 624 221 17 13391296803
16302 2589 118 106 50 2 5741254097
16303 374 37 4 6 NA 2639471976
16304 530 29 49 14 1 1118324609
16305 1407 86 91 25 7 3088800683

1.1.0.1 Tabla a correlacionar:

#union_final_urb_corr  <- union_final_urb[,-c(1)]
write_xlsx(union_final_urb, "P03C_urbano.xlsx")

1.1.0.2 Estadísticos básicos de nuestras frecuencias

data_sum <- summary(union_final_urb)

kbl(head(data_sum)) %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  kable_paper() %>%
  scroll_box(width = "100%", height = "500px")
código Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera Radier sin revestimiento Baldosa de cemento Capa de cemento sobre tierra Tierra ingresos_expandidos
Length:319 Min. : 150 Min. : 1.0 Min. : 2.0 Min. : 1.0 Min. : 1.00 Min. :7.054e+08
Class :character 1st Qu.: 1581 1st Qu.: 52.0 1st Qu.: 105.5 1st Qu.: 45.0 1st Qu.: 3.50 1st Qu.:2.954e+09
Mode :character Median : 3868 Median : 137.0 Median : 266.0 Median : 112.0 Median : 11.00 Median :5.697e+09
NA Mean : 13726 Mean : 335.9 Mean : 672.2 Mean : 206.8 Mean : 31.73 Mean :1.784e+10
NA 3rd Qu.: 13903 3rd Qu.: 354.0 3rd Qu.: 800.0 3rd Qu.: 242.0 3rd Qu.: 24.50 3rd Qu.:1.857e+10
NA Max. :158608 Max. :3296.0 Max. :9177.0 Max. :1589.0 Max. :1206.00 Max. :1.870e+11

1.1.0.3 Gráficas:

library(plotly)
df_2017_fig <- union_final_urb 

fig <- plot_ly(df_2017_fig, x = df_2017_fig$código, y = df_2017_fig[,2]
, name = colnames(df_2017_fig[2]), type = 'scatter', mode = 'lines',
                 width=7000, height=400) 

grafica_fn <- function(g){
  fig <<- fig %>% add_trace(y = ~df_2017_fig[,g]
, name = colnames(df_2017_fig[g]), mode = 'lines',
                 width=7000, height=400) 
}

for (g in 3:(ncol(union_final_urb))) {
grafica_fn(g)
}

fig <- fig %>% layout(autosize = T)%>%
  layout(xaxis = list(rangeslider = list()))
fig 

2 \(\tau\)

df_2017_2f <- filter(df_2017_fig, df_2017_fig$ingresos_expandidos != 'is.na')
III <- seq(2,(ncol(df_2017_2f)-1),1)
my_data <- df_2017_2f[, c(III)]
tabla <- cor(x=my_data, y=df_2017_2f$ingresos_expandidos, method=c("kendall"), use = "pairwise")
tabla <- as.data.frame(tabla)
names(tabla)[1] <- "Correlación"
saveRDS(tabla,"tablas_de_corr/C_P03C_URBANO.rds")
tabla %>% rownames_to_column("Material de construcción del piso")%>%  
  mutate(Correlación = cell_spec(Correlación, background=ifelse(Correlación == max(Correlación), "#fc0303", "#5cb81f"))) %>% 
  kbl(booktabs = T, linesep = "", escape=FALSE) %>% 
    kable_paper(full_width = F) %>%
    column_spec(1, color = "black")%>%
    column_spec(2, color = "white")
Material de construcción del piso Correlación
Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera 0.855189117940599
Radier sin revestimiento 0.670325886668692
Baldosa de cemento 0.75627299926997
Capa de cemento sobre tierra 0.678146484217867
Tierra 0.461968457096345

2.1 Kendall

df_2017_fig <- df_2017_fig[,-c(1)]
chart.Correlation(df_2017_fig, histogram=TRUE, method = c( "kendall"), pch=20)

2.2 Pearson

chart.Correlation(df_2017_fig, histogram=TRUE, method = c( "pearson"), pch=20)

2.3 Spearman

chart.Correlation(df_2017_fig, histogram=TRUE, method = c( "spearman"), pch=20)




3 Nivel nacional RURAL (código 2)

3.1 Pregunta P03C: Material de construcción del piso

Esta pregunta posee 5 categorias de respuesta:

1 Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera
2 Radier sin revestimiento
3 Baldosa de cemento
4 Capa de cemento sobre tierra
5 Tierra

Leemos las respuestas a la pregunta P03C del censo de viviendas 2017 y obtenemos la tabla de frecuencias por categoría:

tabla_con_clave <- readRDS("censos/censo_viviendas_con_clave_17.rds")
tabla_con_clave_u <- filter(tabla_con_clave, tabla_con_clave$AREA == 2)
b <- tabla_con_clave_u$COMUNA
c <- tabla_con_clave_u$P03C
cross_tab =  xtabs( ~ unlist(b) + unlist(c))
tabla <- as.data.frame(cross_tab)
d <-tabla[!(tabla$Freq == 0),]
d$anio <- "2017"

categorias <- sort(unique(tabla_con_clave_u$P03C ))
categorias <- as.data.frame(categorias)
names(categorias)[1] <- "cat"

categorias <- filter(categorias, categorias$cat != 99)
categorias <- filter(categorias, categorias$cat != 98)

d_t <- filter(d,d$unlist.c. == categorias[1,1])
for(i in categorias[2,1]:categorias[nrow(categorias),1]){
  d_i <- filter(d,d$unlist.c. == i)
  d_t = merge( x = d_t, y = d_i, by = "unlist.b.", all.x = TRUE)
}
 
# Agregamos un cero a los códigos comunales de 4 dígitos, que queda en la columna llamada **código**:
codigos <- d_t$unlist.b.
rango <- seq(1:nrow(d_t))
cadena <- paste("0",codigos[rango], sep = "")
cadena <- substr(cadena,(nchar(cadena)[rango])-(4),6)
codigos <- as.data.frame(codigos)
cadena <- as.data.frame(cadena)
comuna_corr <- cbind(d_t,cadena)
comuna_corr <- comuna_corr[,-c(1),drop=FALSE] 
 
names(comuna_corr)[ncol(comuna_corr)] <- "código"

quitar <- seq(3,(ncol(comuna_corr)-1),3)
comuna_corr <- comuna_corr[,-c(quitar),drop=FALSE]


names(comuna_corr)[2] <- "Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera"
names(comuna_corr)[4] <- "Radier sin revestimiento"
names(comuna_corr)[6] <- "Baldosa de cemento"
names(comuna_corr)[8] <- "Capa de cemento sobre tierra"
names(comuna_corr)[10] <- "Tierra"


renombrar <- seq(1,(ncol(comuna_corr)-2),2)
vv <- 0
for (v in renombrar) {
  vv <- vv+1
  contador <- paste0("categoria_",vv)
  names(comuna_corr)[v] <- contador
}

ingresos_expandidos_2017 <- readRDS("Ingresos_expandidos_rural_17.rds")

df_2017_2 = merge( x = comuna_corr, y = ingresos_expandidos_2017, by = "código", all.x = TRUE)
#df_2017_2 <- filter(df_2017_2, df_2017_2$Ingresos_expandidos != 'is.na')
df_2017_2 <- df_2017_2[,-c(2,4,6,8,10,12)]
union_final_urb <- df_2017_2 

kbl(union_final_urb) %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  kable_paper() %>%
  scroll_box(width = "100%", height = "300px")
código Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera Radier sin revestimiento Baldosa de cemento Capa de cemento sobre tierra Tierra ingresos_expandidos
01101 374 96 87 121 87 52180713221
01107 13 7 4 7 16 NA
01401 684 236 172 106 133 3822052676
01402 250 58 47 93 21 283538750
01403 55 63 47 116 174 NA
01404 214 138 49 80 108 645917134
01405 194 39 31 39 22 2502464414
02101 181 20 19 30 58 NA
02102 92 23 5 10 6 NA
02103 284 29 9 15 5 3290056742
02104 153 24 14 23 85 3844002134
02201 420 155 111 122 142 39457387800
02202 71 13 3 4 3 NA
02203 750 282 137 88 70 2985112297
02301 138 10 18 7 35 4183793832
02302 22 12 8 11 4 NA
03101 727 59 50 55 60 38699138722
03102 621 25 23 43 79 NA
03103 660 62 54 103 43 4034940816
03201 319 20 30 17 21 NA
03202 172 24 17 19 16 4545663075
03301 1248 199 136 142 193 11299454698
03302 783 260 276 241 180 1039186477
03303 524 99 70 79 94 1425547554
03304 284 44 29 35 108 2089066548
04101 4345 369 403 379 243 44274327972
04102 3178 284 291 216 88 46918711304
04103 234 34 24 42 75 2397612293
04104 769 60 74 76 83 982530309
04105 1050 129 189 175 52 786383423
04106 2402 185 339 298 120 4695441470
04201 2364 224 299 339 135 5109649759
04202 1496 238 294 372 279 1558270441
04203 1128 71 93 84 58 3704185607
04204 2280 357 522 419 100 5681637894
04301 5256 538 756 856 436 25683781418
04302 1432 232 336 482 293 2300832587
04303 3532 412 467 611 358 5835357638
04304 998 140 191 250 251 1817183694
04305 1122 90 133 197 132 778712384
05101 236 4 11 20 6 74756602991
05102 2559 101 166 99 15 6779018483
05103 676 48 31 18 5 NA
05104 345 2 1 2 NA NA
05105 782 58 69 35 21 4295363979
05107 1464 36 60 31 6 9102071069
05201 119 12 10 1 NA NA
05301 1003 85 58 62 12 18714795984
05302 1040 84 123 55 11 3471349123
05303 532 51 32 33 12 2512319225
05304 2018 92 155 106 48 3995512770
05401 2711 94 214 162 19 6111000517
05402 1880 88 147 140 31 4129354103
05403 146 7 6 12 3 NA
05404 1482 90 207 188 31 2654393853
05405 714 28 34 25 5 1729518700
05501 3337 81 138 107 16 19195726144
05502 506 32 27 25 4 11471016698
05503 1789 80 92 65 7 3874650405
05504 744 10 46 10 6 5377180726
05506 962 34 52 38 9 4861992055
05601 1440 58 97 39 20 21034388728
05602 922 65 69 23 4 3012893845
05603 533 41 33 27 6 5605324190
05604 337 16 13 3 1 NA
05605 202 8 17 5 5 NA
05606 1358 61 80 27 3 2729409577
05701 1787 97 140 142 26 18507290899
05702 1657 71 132 132 17 2868237147
05703 1850 71 90 72 18 6324771348
05704 878 47 49 43 1 1532009468
05705 2591 197 133 243 67 3471806107
05706 1442 68 114 114 12 3884985562
05801 621 33 34 49 16 44984360344
05802 1917 100 128 46 7 11607834893
05803 1492 117 131 81 17 3473287749
05804 328 23 27 18 7 31609146219
06101 1414 139 88 92 20 58924531866
06102 1462 114 106 65 16 3438412620
06103 1111 55 49 64 11 1293816308
06104 2191 207 154 155 26 4977769953
06105 1248 110 67 39 20 4145787348
06106 902 66 48 42 9 8305580885
06107 3889 313 379 270 35 4971665251
06108 272 29 23 9 4 13233865906
06109 2194 171 115 158 26 3361017589
06110 1047 139 77 66 25 6697593734
06111 1057 76 88 62 9 3487795575
06112 524 62 61 43 6 3305417128
06113 2558 213 253 283 25 4282095940
06114 1523 109 101 85 11 2648154389
06115 3227 274 282 267 32 14737488444
06116 2940 208 159 184 21 6847572657
06117 5422 511 359 406 53 11357872282
06201 1105 80 53 21 22 3776560181
06202 537 64 36 39 18 642943494
06203 849 112 55 73 17 1497845780
06204 1109 124 65 79 6 1738201845
06205 2108 166 103 98 43 1099452202
06206 1295 91 54 96 37 1201375821
06301 2574 211 189 170 54 17733143348
06302 1569 180 142 163 52 3115559148
06303 3789 297 309 353 34 8060942027
06304 1109 165 110 113 25 1431112941
06305 1693 149 103 107 14 3810465416
06306 2234 197 153 214 7 2877725100
06307 1168 112 70 109 6 2550270534
06308 1526 197 111 140 19 1934232402
06309 933 106 51 89 14 818883984
06310 3978 417 223 250 70 8495489945
07101 2403 191 141 95 50 53912095394
07102 2523 111 130 44 67 9135962663
07103 1633 259 113 115 201 1811588746
07104 322 26 30 13 16 714199777
07105 2888 200 237 99 33 9705908393
07106 1478 177 123 87 31 1575963241
07107 1315 142 103 109 59 1821794345
07108 2547 250 220 183 49 2733081178
07109 5464 803 309 394 176 7753001772
07110 1118 132 78 59 23 1800047360
07201 1924 300 153 190 160 6171477801
07202 1308 89 130 114 51 1151552040
07203 1059 81 123 49 16 862993347
07301 4484 286 328 227 62 39566034949
07302 1060 111 94 71 43 1622060226
07303 506 29 39 14 10 1197005482
07304 2321 194 218 142 44 10475401720
07305 1363 107 132 107 47 2062408371
07306 2210 180 232 114 28 3316239205
07307 2750 209 253 175 36 3800089672
07308 5088 322 500 336 47 7240913928
07309 1067 86 54 57 27 777679695
07401 4062 425 358 306 217 18044885598
07402 3129 312 247 168 84 3348358419
07403 5312 441 639 533 136 5087351933
07404 2969 354 280 209 71 7624714509
07405 3596 258 279 222 59 2924321333
07406 3381 411 273 280 120 7768163327
07407 1772 122 156 121 37 2895229121
07408 3092 253 255 223 55 3670461912
08101 1294 52 46 20 5 44183983882
08102 935 19 58 16 3 25230952648
08103 18 2 NA 1 NA NA
08104 1728 119 180 90 50 1566245750
08105 1002 83 58 64 50 4932666876
08106 36 NA 7 1 NA NA
08107 133 7 10 7 5 9246639961
08108 33 1 2 1 NA NA
08109 1377 66 71 44 49 2728477197
08110 113 2 1 2 4 24542535584
08111 1569 85 103 44 27 11541584520
08112 186 3 20 12 1 NA
08201 683 9 35 7 6 3620107931
08202 2613 32 238 24 4 6702069405
08203 4504 31 186 20 14 6455391501
08204 954 9 24 6 7 793386801
08205 662 5 52 5 13 8138946477
08206 655 15 35 7 3 3984647129
08207 1941 9 68 8 15 1510411713
08301 13690 815 1132 467 95 38606846296
08302 611 43 33 27 1 634012722
08303 2073 147 133 77 9 7119335384
08304 1695 128 190 97 28 3905739533
08305 1643 52 114 43 8 5873792045
08306 895 103 90 74 96 4626944798
08307 965 44 99 61 3 2112926492
08308 699 20 51 24 6 666419314
08309 1212 60 92 42 8 2129051929
08310 171 5 22 14 10 566092732
08311 1691 76 111 45 15 2424192819
08312 744 25 52 31 7 2198382777
08313 2530 239 195 113 100 2936241535
08314 1420 31 87 46 156 773205492
09101 5190 313 286 80 31 48946498862
09102 3455 58 83 33 52 3138372109
09103 2790 102 148 63 12 2749522512
09104 1522 49 81 51 17 1014167156
09105 4791 253 357 139 38 4857867695
09106 2257 49 93 34 37 1769627798
09107 1493 29 85 21 4 2032338344
09108 2755 244 198 82 27 10738525406
09109 2371 38 81 31 12 3795451798
09110 980 65 63 58 15 1010744848
09111 3959 201 190 148 71 5142978907
09112 7282 544 552 255 89 12882320064
09113 986 78 53 29 1 1071011969
09114 2639 156 186 75 8 5105439315
09115 3116 128 162 43 11 5355614570
09116 3095 54 81 34 57 1628156299
09117 3083 86 146 97 31 2089663239
09118 1859 35 81 23 5 1106284328
09119 3409 191 265 127 31 3817354634
09120 5932 176 311 58 14 11026000004
09121 1926 116 130 54 53 1336465909
09201 1159 97 78 50 87 9234420713
09202 1740 106 104 49 37 4484784762
09203 1396 48 85 27 5 3249351008
09204 1147 27 50 22 27 1056936411
09205 1798 71 126 95 17 1422283764
09206 806 57 49 40 29 1035906610
09207 1620 36 42 24 55 1628298886
09208 1274 20 37 16 14 1572938990
09209 493 26 38 22 5 2243930000
09210 1247 47 48 20 19 3966946195
09211 2708 124 138 81 34 6414689393
10101 8272 88 195 53 6 43337141298
10102 5788 31 107 44 5 5282762017
10103 1556 10 17 3 3 NA
10104 1843 8 12 11 1 2255743895
10105 1830 23 24 16 2 3222744874
10106 3208 7 27 18 5 3291484556
10107 1109 13 22 15 1 2626930838
10108 2762 16 21 21 3 1956316762
10109 3820 52 72 17 3 9799987895
10201 3247 14 32 6 5 8048100927
10202 3872 18 27 18 2 6313036958
10203 3130 4 22 30 4 2877146807
10204 911 2 6 1 NA 681378864
10205 2212 8 20 4 1 2858609503
10206 1302 2 11 2 NA 816645370
10207 1057 10 6 1 2 815746659
10208 3112 1 20 7 9 4668472212
10209 2072 6 26 7 3 1020807718
10210 1615 7 21 12 1 969367811
10301 4302 66 101 24 5 31744688808
10302 2151 37 107 23 1 1997605810
10303 2004 9 30 5 2 3803288945
10304 2130 13 35 9 3 2053472049
10305 2347 19 34 6 4 2205262341
10306 2338 8 7 13 19 1146887184
10307 1910 12 34 13 7 1819558805
10401 1008 5 4 4 11 NA
10402 260 2 19 7 NA NA
10403 1794 5 12 8 2 NA
10404 268 2 5 1 1 NA
11101 2458 99 260 42 8 13298894369
11102 312 17 30 4 NA NA
11201 860 24 64 9 1 5908637554
11202 597 1 22 1 1 1710143349
11203 45 NA NA NA 3 NA
11301 207 19 4 3 3 738667487
11302 213 5 8 2 1 NA
11303 181 NA NA 1 1 NA
11401 483 33 97 22 6 919065674
11402 894 50 69 37 7 456727447
12101 1548 36 51 30 4 33806414442
12102 103 NA 1 NA NA NA
12103 85 1 2 1 NA NA
12104 143 1 1 1 NA NA
12201 27 NA 1 NA NA NA
12202 8 NA NA NA NA NA
12301 241 2 5 NA NA 2593419712
12302 180 2 4 1 1 NA
12303 82 1 3 NA 1 NA
12401 677 10 31 1 NA 6489647004
12402 124 1 1 1 NA NA
13107 22 8 NA NA NA NA
13110 23 4 5 NA NA NA
13115 284 19 13 2 2 NA
13119 666 91 55 40 20 NA
13124 1043 75 39 28 19 NA
13125 141 19 2 10 16 NA
13201 2 NA NA 1 NA NA
13202 3703 225 202 106 14 7284672878
13203 1466 150 74 49 43 6272964115
13301 6539 236 232 94 15 37337421744
13302 5254 354 315 134 36 24837699582
13303 1485 157 105 80 25 5113717064
13401 1154 95 109 56 17 75849003232
13402 3239 199 198 120 23 28006850165
13403 3369 125 145 58 2 7577982724
13404 6571 498 442 245 25 20538478428
13501 10357 511 585 331 42 24616837833
13502 807 122 89 59 11 1564887792
13503 3381 204 159 116 23 7199638514
13504 1850 111 134 44 6 2691680700
13505 2459 259 212 223 33 2250885401
13601 3780 101 192 72 14 17129031774
13602 1564 66 64 43 5 7236496479
13603 2374 153 163 71 4 8424384020
13604 1854 112 61 47 11 14664233522
13605 1780 68 53 36 9 22536570306
14101 3314 53 108 22 5 35164529745
14102 641 8 4 2 1 834683963
14103 1671 31 57 8 5 3094599901
14104 3115 35 87 31 2 3740075550
14105 889 11 34 15 NA 1279152079
14106 3445 66 134 34 4 3979945072
14107 2352 32 87 34 4 3307473487
14108 6041 137 254 64 23 6230498948
14201 3733 47 91 23 6 7682327556
14202 2046 33 41 12 NA 2832109866
14203 2550 27 44 20 8 1846550611
14204 4842 53 104 28 9 5783758517
15101 1322 584 609 782 790 55532177025
15102 150 91 53 54 80 279202446
15201 68 15 24 15 121 537221762
15202 45 8 14 10 134 NA
16101 4507 299 245 121 38 42867130063
16102 2246 126 121 83 21 3604229178
16103 924 102 53 56 14 5558803478
16104 1912 197 160 147 45 1820386198
16105 1191 84 84 71 16 1283161238
16106 1599 96 106 57 16 1659661870
16107 2140 216 199 120 61 2333895558
16108 2511 261 213 185 57 2806409365
16109 1329 117 117 62 14 3450799686
16201 646 43 71 31 45 1802251665
16202 1030 107 106 98 48 614036495
16203 1367 32 117 69 14 2783932983
16204 1049 94 118 82 32 842304828
16205 861 86 68 43 34 819710106
16206 1352 91 55 33 13 1277329463
16207 1130 47 105 49 19 965503625
16301 5557 394 407 318 105 9289995173
16302 5045 211 271 144 31 4700239750
16303 2840 267 234 155 71 2105832760
16304 729 51 41 44 15 680747063
16305 1810 201 140 126 46 2096375354

3.1.0.1 Tabla a correlacionar:

#union_final_urb_corr  <- union_final_urb[,-c(1)]
write_xlsx(union_final_urb, "P03C_rural.xlsx")

3.1.0.2 Estadísticos básicos de nuestras frecuencias

data_sum <- summary(union_final_urb)

kbl(head(data_sum)) %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  kable_paper() %>%
  scroll_box(width = "100%", height = "500px")
código Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera Radier sin revestimiento Baldosa de cemento Capa de cemento sobre tierra Tierra ingresos_expandidos
Length:319 Min. : 2.0 Min. : 1.00 Min. : 1.0 Min. : 1.00 Min. : 1.00 Min. :2.792e+08
Class :character 1st Qu.: 683.5 1st Qu.: 25.75 1st Qu.: 34.0 1st Qu.: 20.00 1st Qu.: 6.00 1st Qu.:1.809e+09
Mode :character Median : 1420.0 Median : 71.00 Median : 85.0 Median : 49.00 Median : 17.00 Median :3.546e+09
NA Mean : 1807.3 Mean :112.02 Mean : 119.7 Mean : 87.94 Mean : 40.59 Mean :8.206e+09
NA 3rd Qu.: 2458.5 3rd Qu.:150.75 3rd Qu.: 153.0 3rd Qu.:109.00 3rd Qu.: 47.00 3rd Qu.:7.252e+09
NA Max. :13690.0 Max. :815.00 Max. :1132.0 Max. :856.00 Max. :790.00 Max. :7.585e+10

3.1.0.3 Gráficas:

library(plotly)
df_2017_fig <- union_final_urb 

fig <- plot_ly(df_2017_fig, x = df_2017_fig$código, y = df_2017_fig[,2]
, name = colnames(df_2017_fig[2]), type = 'scatter', mode = 'lines',
                 width=7000, height=400) 

grafica_fn <- function(g){
  fig <<- fig %>% add_trace(y = ~df_2017_fig[,g]
, name = colnames(df_2017_fig[g]), mode = 'lines',
                 width=7000, height=400) 
}

for (g in 3:(ncol(union_final_urb))) {
grafica_fn(g)
}

fig <- fig %>% layout(autosize = T)%>%
  layout(xaxis = list(rangeslider = list()))
fig 

4 \(\tau\)

df_2017_2f <- filter(df_2017_fig, df_2017_fig$ingresos_expandidos != 'is.na')
III <- seq(2,(ncol(df_2017_2f)-1),1)
my_data <- df_2017_2f[, c(III)]
tabla <- cor(x=my_data, y=df_2017_2f$ingresos_expandidos, method=c("kendall"), use = "pairwise")
tabla <- as.data.frame(tabla)
names(tabla)[1] <- "Correlación"
saveRDS(tabla,"tablas_de_corr/C_P03C_RURAL.rds")
tabla %>% rownames_to_column("Material de construcción del piso")%>%  
  mutate(Correlación = cell_spec(Correlación, background=ifelse(Correlación == max(Correlación), "#fc0303", "#5cb81f"))) %>% 
  kbl(booktabs = T, linesep = "", escape=FALSE) %>% 
    kable_paper(full_width = F) %>%
    column_spec(1, color = "black")%>%
    column_spec(2, color = "white")
Material de construcción del piso Correlación
Parquet, piso flotante, cerámico, madera, alfombra, flexit, cubrepiso u otro similar, sobre radier o vigas de madera 0.31488509775923
Radier sin revestimiento 0.22072054070365
Baldosa de cemento 0.259722201804308
Capa de cemento sobre tierra 0.159717435093895
Tierra 0.0448975278172948

4.1 Kendall

df_2017_fig <- df_2017_fig[,-c(1)]
chart.Correlation(df_2017_fig, histogram=TRUE, method = c( "kendall"), pch=20)

4.2 Pearson

chart.Correlation(df_2017_fig, histogram=TRUE, method = c( "pearson"), pch=20)

4.3 Spearman

chart.Correlation(df_2017_fig, histogram=TRUE, method = c( "spearman"), pch=20)