rm(list = ls())
library(rio)
## Warning: package 'rio' was built under R version 4.3.3
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(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
library(psych)
library(GPArotation)
## Warning: package 'GPArotation' was built under R version 4.3.3
## 
## Attaching package: 'GPArotation'
## The following objects are masked from 'package:psych':
## 
##     equamax, varimin
setwd("C:/Users/moren/OneDrive/2024-2/ESTADÍSTICA 2/EXAMEN FINAL")

1.UNA vez tenga esas variables, obtenga los porcentajes de los conteos positivos para todos los casos (por ejemplo, porcentaje de hogares que sí usan electricidad). Para las preguntas, use esos sólo esos porcentajes.

dataenergía=import("reporte.xlsx")
## New names:
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
## • `` -> `...13`
## • `` -> `...14`
## • `` -> `...15`
dataenergía <- dataenergía[-c(1), ]

colnames(dataenergía) <- c("Titulo", "Codigo", "Provincia", "No_usa_electricidad", "Si_usa_electricidad",
                           "Total_electricidad", "No_usa_gas_GLP", "Si_usa_gas_GLP", "Total_gas_GLP",
                           "No_usa_carbon", "Si_usa_carbon", "Total_carbon", 
                           "No_usa_lena", "Si_usa_lena", "Total_lena")



dataenergía <- dataenergía[rowSums(is.na(dataenergía)) != ncol(dataenergía), ]
dataenergía <- dataenergía[, colSums(is.na(dataenergía)) != nrow(dataenergía)]

cols_to_numeric <- c("No_usa_electricidad", "Si_usa_electricidad", "Total_electricidad",
                     "No_usa_gas_GLP", "Si_usa_gas_GLP", "Total_gas_GLP", 
                     "No_usa_carbon", "Si_usa_carbon", "Total_carbon", 
                     "No_usa_lena", "Si_usa_lena", "Total_lena")

dataenergía[cols_to_numeric] <- lapply(dataenergía[cols_to_numeric], as.numeric)
## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción

## Warning in lapply(dataenergía[cols_to_numeric], as.numeric): NAs introducidos
## por coerción
# Mostrar las primeras filas del conjunto de datos limpio
head(dataenergía)
##                  Titulo Codigo                        Provincia
## 2       Área Geográfica   <NA>                             <NA>
## 3 Toda la Base de Datos   <NA>                             <NA>
## 5                  <NA> Código                        Provincia
## 6                  <NA>    101 Amazonas, provincia: Chachapoyas
## 7                  <NA>    102       Amazonas, provincia: Bagua
## 8                  <NA>    103     Amazonas, provincia: Bongara
##   No_usa_electricidad Si_usa_electricidad Total_electricidad No_usa_gas_GLP
## 2                  NA                  NA                 NA             NA
## 3                  NA                  NA                 NA             NA
## 5                  NA                  NA                 NA             NA
## 6               14763                 574              15337           4696
## 7               20313                 161              20474          10557
## 8                7689                 124               7813           3154
##   Si_usa_gas_GLP Total_gas_GLP No_usa_carbon Si_usa_carbon Total_carbon
## 2             NA            NA            NA            NA           NA
## 3             NA            NA            NA            NA           NA
## 5             NA            NA            NA            NA           NA
## 6          10641         15337         15161           176        15337
## 7           9917         20474         20185           289        20474
## 8           4659          7813          7755            58         7813
##   No_usa_lena Si_usa_lena Total_lena
## 2          NA          NA         NA
## 3          NA          NA         NA
## 5          NA          NA         NA
## 6        7236        8101      15337
## 7        7357       13117      20474
## 8        2345        5468       7813
dontselect=c("Titulo","Codigo", "Provincia")
select=setdiff(names(dataenergía),dontselect) 
dataenergía=dataenergía[,select]

dataenergía <- dataenergía[-c(1:3), ]

head(dataenergía)
##    No_usa_electricidad Si_usa_electricidad Total_electricidad No_usa_gas_GLP
## 6                14763                 574              15337           4696
## 7                20313                 161              20474          10557
## 8                 7689                 124               7813           3154
## 9                 9853                  14               9867           8331
## 10               13112                  90              13202           6863
## 11                9103                  65               9168           5387
##    Si_usa_gas_GLP Total_gas_GLP No_usa_carbon Si_usa_carbon Total_carbon
## 6           10641         15337         15161           176        15337
## 7            9917         20474         20185           289        20474
## 8            4659          7813          7755            58         7813
## 9            1536          9867          9841            26         9867
## 10           6339         13202         13169            33        13202
## 11           3781          9168          9142            26         9168
##    No_usa_lena Si_usa_lena Total_lena
## 6         7236        8101      15337
## 7         7357       13117      20474
## 8         2345        5468       7813
## 9         1059        8808       9867
## 10        1833       11369      13202
## 11        1824        7344       9168

EFA:

names(dataenergía)
##  [1] "No_usa_electricidad" "Si_usa_electricidad" "Total_electricidad" 
##  [4] "No_usa_gas_GLP"      "Si_usa_gas_GLP"      "Total_gas_GLP"      
##  [7] "No_usa_carbon"       "Si_usa_carbon"       "Total_carbon"       
## [10] "No_usa_lena"         "Si_usa_lena"         "Total_lena"
library(polycor)
## Warning: package 'polycor' was built under R version 4.3.3
## 
## Attaching package: 'polycor'
## The following object is masked from 'package:psych':
## 
##     polyserial
data=polycor::hetcor(dataenergía)$correlations
## Warning in hetcor.data.frame(dataenergía): the correlation matrix has been
## adjusted to make it positive-definite
round(dataenergía,2)
##     No_usa_electricidad Si_usa_electricidad Total_electricidad No_usa_gas_GLP
## 6                 14763                 574              15337           4696
## 7                 20313                 161              20474          10557
## 8                  7689                 124               7813           3154
## 9                  9853                  14               9867           8331
## 10                13112                  90              13202           6863
## 11                 9103                  65               9168           5387
## 12                31248                 255              31503          15425
## 13                45333                 921              46254          15947
## 14                 1989                  16               2005           1527
## 15                 3894                  33               3927           3151
## 16                 2347                  12               2359           1572
## 17                 7106                  81               7187           4779
## 18                13370                 122              13492           9878
## 19                 5289                  19               5308           4449
## 20                14869                 370              15239           5218
## 21                 2086                  17               2103           1593
## 22                16191                 123              16314          12787
## 23                 9365                 193               9558           2208
## 24                14868                 142              15010           9411
## 25                 6119                  14               6133           5573
## 26                 2591                  26               2617           1964
## 27                 6788                  24               6812           6010
## 28                 7258                  28               7286           6214
## 29                 5370                  53               5423           3585
## 30               119443                4473             123916          12659
## 31                 7825                  50               7875           6640
## 32                14723                 120              14843          10053
## 33                33780                 490              34270          10121
## 34                42100                 447              42547          26207
## 35                 4049                  31               4080           2594
## 36                 8676                  69               8745           4572
## 37                15051                 197              15248          10197
## 38                13708                  89              13797          10640
## 39                 7324                  37               7361           5273
## 40               316729               11031             327760          22740
## 41                18532                 537              19069           2367
## 42                12048                 218              12266           1729
## 43                10687                 110              10797           2855
## 44                28933                 488              29421           6093
## 45                 5021                  28               5049           1938
## 46                16335                 432              16767           2379
## 47                 4359                  34               4393           2608
## 48                76581                1685              78266          23608
## 49                10329                  90              10419           6626
## 50                 3060                  33               3093           1605
## 51                25715                 369              26084          12406
## 52                20883                 231              21114          12736
## 53                17989                 181              18170          10386
## 54                 8180                  67               8247           3499
## 55                 3288                  45               3333           1768
## 56                 3396                  16               3412           2184
## 57                 7293                  80               7373           4812
## 58                 6121                  49               6170           4642
## 59                94664                1513              96177          40346
## 60                20856                 176              21032          14308
## 61                23717                 181              23898          17599
## 62                44133                 360              44493          30604
## 63                 9041                  47               9088           6007
## 64                36425                 220              36645          25273
## 65                25234                 189              25423          19552
## 66                52299                 806              53105          20402
## 67                37095                 143              37238          23624
## 68                14783                 105              14888          10063
## 69                15344                  89              15433          11970
## 70                 6541                  21               6562           5131
## 71                11566                  60              11626           8750
## 72               254119               12109             266228          34865
## 73               122395                3968             126363          10803
## 74                 7037                 118               7155           4398
## 75                16623                 127              16750           9046
## 76                18947                 112              19059          10571
## 77                10663                  38              10701           8490
## 78                29852                 192              30044          11832
## 79                21086                 182              21268          14541
## 80                19020                 150              19170           8577
## 81                42272                 482              42754          21446
## 82                 8165                  35               8200           5671
## 83                12126                  58              12184           8703
## 84                24468                 235              24703          13578
## 85                16386                 132              16518           7353
## 86                33074                 312              33386          16364
## 87                11600                  70              11670           8253
## 88                14192                  87              14279          10203
## 89                 5184                  35               5219           3614
## 90                 9910                  57               9967           7834
## 91                 6552                  29               6581           4773
## 92                24721                 195              24916          18249
## 93                78943                1817              80760          26595
## 94                14714                 152              14866           7563
## 95                 9888                  40               9928           7682
## 96                 4408                  16               4424           3946
## 97                15424                  54              15478          12770
## 98                35024                 494              35518          12603
## 99                 7387                  37               7424           5590
## 100               13851                 129              13980           9152
## 101                8870                  53               8923           4834
## 102                5579                  16               5595           4835
## 103                6529                  26               6555           5917
## 104              106450                2878             109328          25668
## 105               58938                1814              60752          14541
## 106               19921                1088              21009           5339
## 107                4188                  56               4244           1509
## 108               41678                1027              42705          11337
## 109              147407                3266             150673          22505
## 110               16300                 127              16427           8585
## 111               43742                 738              44480          17351
## 112               25061                 242              25303          10436
## 113                7556                  38               7594           2192
## 114               51887                 367              52254          34435
## 115               27040                 388              27428           6585
## 116               10180                 452              10632           1546
## 117               14770                 152              14922           5042
## 118              239267               11014             250281          21836
## 119               30995                 721              31716           4443
## 120                4007                  35               4042           3267
## 121               22071                 478              22549           4941
## 122                8978                  28               9006           5482
## 123               23849                 208              24057          18819
## 124               28525                 914              29439           4373
## 125               18686                 185              18871          12857
## 126               38569                 312              38881          26190
## 127               14281                  81              14362          11775
## 128                7885                  74               7959           4591
## 129               22456                 408              22864           6395
## 130              201489                7042             208531          19661
## 131               25153                 268              25421           9008
## 132               73854                1181              75035          27326
## 133             2228751              125199            2353950         515782
## 134               39310                1140              40450           4172
## 135                2217                  18               2235           1626
## 136                3919                  59               3978           1363
## 137               64915                1986              66901           7277
## 138               48337                1662              49999           5809
## 139               17104                 338              17442           5291
## 140               61874                2745              64619           8498
## 141                4620                  58               4678           1932
## 142                6817                  53               6870           4213
## 143              104011                2835             106846          38157
## 144               28563                 446              29009          15387
## 145               13629                  97              13726          10490
## 146               10472                  78              10550           7842
## 147               12312                 113              12425           9684
## 148               12062                  94              12156           8375
## 149               10200                  49              10249           7994
## 150                1720                   4               1724           1297
## 151               31812                 877              32689           7101
## 152                5553                  50               5603           2389
## 153                3054                  66               3120            975
## 154               27647                1155              28802           6241
## 155                6370                  35               6405           4424
## 156               22935                 860              23795           1526
## 157               32847                 404              33251           7611
## 158               10955                  43              10998           7886
## 159               24702                 246              24948          12157
## 160              199828                5441             205269          57879
## 161               31348                 158              31506          22871
## 162               31747                 228              31975          24755
## 163               46124                 603              46727          21784
## 164               34293                 726              35019           7717
## 165               81663                1767              83430          21929
## 166               37246                1059              38305           5054
## 167               20080                 375              20455           5645
## 168               75362                 815              76177          27566
## 169               39548                 196              39744          24469
## 170               25178                 109              25287          16443
## 171               36616                 128              36744          22710
## 172               25929                 143              26072          12378
## 173               23625                 105              23730          12225
## 174               16114                  82              16196           8795
## 175               22505                 178              22683          10519
## 176                8963                  40               9003           6652
## 177               12597                 126              12723           5839
## 178               90494                1644              92138          13154
## 179               20030                  79              20109          12677
## 180               14298                  65              14363           6703
## 181               33006                 787              33793          11681
## 182               14703                 186              14889           7485
## 183                9321                  84               9405           5701
## 184                7969                 110               8079           3457
## 185               21691                 381              22072          11097
## 186               16791                 274              17065           6112
## 187               10862                  83              10945           4968
## 188               33646                 448              34094          12333
## 189               51603                1693              53296          11036
## 190               18520                 506              19026           6985
## 191               93554                2715              96269          10895
## 192                2551                  25               2576           1868
## 193                2470                 698               3168           1417
## 194                2440                  25               2465           1832
## 195               41377                1506              42883           4651
## 196                5680                 110               5790           1034
## 197               13636                 289              13925           1818
## 198               91586                2017              93603          20452
## 199               12227                  87              12314           8500
## 200               15483                 168              15651           5595
## 201                 687                   1                688            572
## 203                  NA                  NA                 NA             NA
## 204                  NA                  NA                 NA             NA
##     Si_usa_gas_GLP Total_gas_GLP No_usa_carbon Si_usa_carbon Total_carbon
## 6            10641         15337         15161           176        15337
## 7             9917         20474         20185           289        20474
## 8             4659          7813          7755            58         7813
## 9             1536          9867          9841            26         9867
## 10            6339         13202         13169            33        13202
## 11            3781          9168          9142            26         9168
## 12           16078         31503         31168           335        31503
## 13           30307         46254         46036           218        46254
## 14             478          2005          2001             4         2005
## 15             776          3927          3923             4         3927
## 16             787          2359          2352             7         2359
## 17            2408          7187          7090            97         7187
## 18            3614         13492         13470            22        13492
## 19             859          5308          5301             7         5308
## 20           10021         15239         15050           189        15239
## 21             510          2103          2096             7         2103
## 22            3527         16314         16284            30        16314
## 23            7350          9558          9360           198         9558
## 24            5599         15010         14978            32        15010
## 25             560          6133          6127             6         6133
## 26             653          2617          2603            14         2617
## 27             802          6812          6749            63         6812
## 28            1072          7286          7273            13         7286
## 29            1838          5423          5405            18         5423
## 30          111257        123916        122492          1424       123916
## 31            1235          7875          7860            15         7875
## 32            4790         14843         14803            40        14843
## 33           24149         34270         34111           159        34270
## 34           16340         42547         42441           106        42547
## 35            1486          4080          4067            13         4080
## 36            4173          8745          8712            33         8745
## 37            5051         15248         15143           105        15248
## 38            3157         13797         13762            35        13797
## 39            2088          7361          7341            20         7361
## 40          305020        327760        326375          1385       327760
## 41           16702         19069         18999            70        19069
## 42           10537         12266         12210            56        12266
## 43            7942         10797         10737            60        10797
## 44           23328         29421         29284           137        29421
## 45            3111          5049          5037            12         5049
## 46           14388         16767         16716            51        16767
## 47            1785          4393          4375            18         4393
## 48           54658         78266         77940           326        78266
## 49            3793         10419         10376            43        10419
## 50            1488          3093          3081            12         3093
## 51           13678         26084         26016            68        26084
## 52            8378         21114         21070            44        21114
## 53            7784         18170         18092            78        18170
## 54            4748          8247          8209            38         8247
## 55            1565          3333          3320            13         3333
## 56            1228          3412          3403             9         3412
## 57            2561          7373          7356            17         7373
## 58            1528          6170          6157            13         6170
## 59           55831         96177         95186           991        96177
## 60            6724         21032         20998            34        21032
## 61            6299         23898         23792           106        23898
## 62           13889         44493         43904           589        44493
## 63            3081          9088          9057            31         9088
## 64           11372         36645         36303           342        36645
## 65            5871         25423         25224           199        25423
## 66           32703         53105         52508           597        53105
## 67           13614         37238         37099           139        37238
## 68            4825         14888         14840            48        14888
## 69            3463         15433         15385            48        15433
## 70            1431          6562          6551            11         6562
## 71            2876         11626         11525           101        11626
## 72          231363        266228        264016          2212       266228
## 73          115560        126363        123607          2756       126363
## 74            2757          7155          7113            42         7155
## 75            7704         16750         16645           105        16750
## 76            8488         19059         18924           135        19059
## 77            2211         10701         10669            32        10701
## 78           18212         30044         29649           395        30044
## 79            6727         21268         21168           100        21268
## 80           10593         19170         19117            53        19170
## 81           21308         42754         42587           167        42754
## 82            2529          8200          8144            56         8200
## 83            3481         12184         12121            63        12184
## 84           11125         24703         24247           456        24703
## 85            9165         16518         16400           118        16518
## 86           17022         33386         33263           123        33386
## 87            3417         11670         11623            47        11670
## 88            4076         14279         14256            23        14279
## 89            1605          5219          5196            23         5219
## 90            2133          9967          9950            17         9967
## 91            1808          6581          6551            30         6581
## 92            6667         24916         24867            49        24916
## 93           54165         80760         80028           732        80760
## 94            7303         14866         14818            48        14866
## 95            2246          9928          9899            29         9928
## 96             478          4424          4420             4         4424
## 97            2708         15478         15430            48        15478
## 98           22915         35518         35151           367        35518
## 99            1834          7424          7399            25         7424
## 100           4828         13980         13952            28        13980
## 101           4089          8923          8865            58         8923
## 102            760          5595          5552            43         5595
## 103            638          6555          6531            24         6555
## 104          83660        109328        108458           870       109328
## 105          46211         60752         60535           217        60752
## 106          15670         21009         20793           216        21009
## 107           2735          4244          4215            29         4244
## 108          31368         42705         42512           193        42705
## 109         128168        150673        149502          1171       150673
## 110           7842         16427         16361            66        16427
## 111          27129         44480         44213           267        44480
## 112          14867         25303         25203           100        25303
## 113           5402          7594          7484           110         7594
## 114          17819         52254         51872           382        52254
## 115          20843         27428         27125           303        27428
## 116           9086         10632         10576            56        10632
## 117           9880         14922         14815           107        14922
## 118         228445        250281        247563          2718       250281
## 119          27273         31716         31525           191        31716
## 120            775          4042          4037             5         4042
## 121          17608         22549         21878           671        22549
## 122           3524          9006          8972            34         9006
## 123           5238         24057         23265           792        24057
## 124          25066         29439         28855           584        29439
## 125           6014         18871         18835            36        18871
## 126          12691         38881         37969           912        38881
## 127           2587         14362         12412          1950        14362
## 128           3368          7959          7946            13         7959
## 129          16469         22864         22742           122        22864
## 130         188870        208531        188768         19763       208531
## 131          16413         25421         23697          1724        25421
## 132          47709         75035         64791         10244        75035
## 133        1838168       2353950       2329530         24420      2353950
## 134          36278         40450         40132           318        40450
## 135            609          2235          2216            19         2235
## 136           2615          3978          3952            26         3978
## 137          59624         66901         66190           711        66901
## 138          44190         49999         49124           875        49999
## 139          12151         17442         17153           289        17442
## 140          56121         64619         63568          1051        64619
## 141           2746          4678          3755           923         4678
## 142           2657          6870          6854            16         6870
## 143          68689        106846         86667         20179       106846
## 144          13622         29009         26737          2272        29009
## 145           3236         13726         12576          1150        13726
## 146           2708         10550         10229           321        10550
## 147           2741         12425         11410          1015        12425
## 148           3781         12156         11869           287        12156
## 149           2255         10249         10108           141        10249
## 150            427          1724          1700            24         1724
## 151          25588         32689         26063          6626        32689
## 152           3214          5603          4895           708         5603
## 153           2145          3120          2166           954         3120
## 154          22561         28802         28656           146        28802
## 155           1981          6405          6391            14         6405
## 156          22269         23795         23712            83        23795
## 157          25640         33251         32923           328        33251
## 158           3112         10998         10754           244        10998
## 159          12791         24948         24828           120        24948
## 160         147390        205269        175981         29288       205269
## 161           8635         31506         30439          1067        31506
## 162           7220         31975         31494           481        31975
## 163          24943         46727         43850          2877        46727
## 164          27302         35019         25354          9665        35019
## 165          61501         83430         60628         22802        83430
## 166          33251         38305         32171          6134        38305
## 167          14810         20455         15805          4650        20455
## 168          48611         76177         75884           293        76177
## 169          15275         39744         39623           121        39744
## 170           8844         25287         25015           272        25287
## 171          14034         36744         36434           310        36744
## 172          13694         26072         25918           154        26072
## 173          11505         23730         23608           122        23730
## 174           7401         16196         16141            55        16196
## 175          12164         22683         22604            79        22683
## 176           2351          9003          8982            21         9003
## 177           6884         12723         12678            45        12723
## 178          78984         92138         91525           613        92138
## 179           7432         20109         20030            79        20109
## 180           7660         14363         14278            85        14363
## 181          22112         33793         33523           270        33793
## 182           7404         14889         14766           123        14889
## 183           3704          9405          9315            90         9405
## 184           4622          8079          8046            33         8079
## 185          10975         22072         21835           237        22072
## 186          10953         17065         16902           163        17065
## 187           5977         10945         10791           154        10945
## 188          21761         34094         33851           243        34094
## 189          42260         53296         52235          1061        53296
## 190          12041         19026         18920           106        19026
## 191          85374         96269         94426          1843        96269
## 192            708          2576          2565            11         2576
## 193           1751          3168          3125            43         3168
## 194            633          2465          2459             6         2465
## 195          38232         42883         41037          1846        42883
## 196           4756          5790          4870           920         5790
## 197          12107         13925         13631           294        13925
## 198          73151         93603         86264          7339        93603
## 199           3814         12314         12064           250        12314
## 200          10056         15651         15359           292        15651
## 201            116           688           600            88          688
## 203             NA            NA            NA            NA           NA
## 204             NA            NA            NA            NA           NA
##     No_usa_lena Si_usa_lena Total_lena
## 6          7236        8101      15337
## 7          7357       13117      20474
## 8          2345        5468       7813
## 9          1059        8808       9867
## 10         1833       11369      13202
## 11         1824        7344       9168
## 12        10790       20713      31503
## 13        24753       21501      46254
## 14          219        1786       2005
## 15          351        3576       3927
## 16          175        2184       2359
## 17         1771        5416       7187
## 18         2109       11383      13492
## 19          354        4954       5308
## 20         8088        7151      15239
## 21          221        1882       2103
## 22         1883       14431      16314
## 23         6874        2684       9558
## 24         3659       11351      15010
## 25          194        5939       6133
## 26          377        2240       2617
## 27          507        6305       6812
## 28          633        6653       7286
## 29         1050        4373       5423
## 30       108350       15566     123916
## 31          708        7167       7875
## 32         2098       12745      14843
## 33        16860       17410      34270
## 34         9749       32798      42547
## 35          921        3159       4080
## 36         1184        7561       8745
## 37         5496        9752      15248
## 38         1508       12289      13797
## 39         1133        6228       7361
## 40       298529       29231     327760
## 41        16372        2697      19069
## 42         9864        2402      12266
## 43         5475        5322      10797
## 44        19746        9675      29421
## 45         2563        2486       5049
## 46        14151        2616      16767
## 47          707        3686       4393
## 48        40674       37592      78266
## 49         1136        9283      10419
## 50          541        2552       3093
## 51         8068       18016      26084
## 52         5098       16016      21114
## 53         4969       13201      18170
## 54         3356        4891       8247
## 55          767        2566       3333
## 56          517        2895       3412
## 57          950        6423       7373
## 58          474        5696       6170
## 59        42808       53369      96177
## 60         3242       17790      21032
## 61         3534       20364      23898
## 62         7191       37302      44493
## 63         2314        6774       9088
## 64         6100       30545      36645
## 65         3591       21832      25423
## 66        25524       27581      53105
## 67         7707       29531      37238
## 68         1811       13077      14888
## 69         1598       13835      15433
## 70          562        6000       6562
## 71         1523       10103      11626
## 72       260422        5806     266228
## 73       111327       15036     126363
## 74          841        6314       7155
## 75         2906       13844      16750
## 76         5598       13461      19059
## 77         8599        2102      10701
## 78        14616       15428      30044
## 79        10732       10536      21268
## 80        16953        2217      19170
## 81        12695       30059      42754
## 82          833        7367       8200
## 83         1620       10564      12184
## 84         7972       16731      24703
## 85         5875       10643      16518
## 86        15598       17788      33386
## 87         1878        9792      11670
## 88         3723       10556      14279
## 89         1133        4086       5219
## 90         1146        8821       9967
## 91         1770        4811       6581
## 92         3490       21426      24916
## 93        47247       33513      80760
## 94         3771       11095      14866
## 95         1657        8271       9928
## 96          296        4128       4424
## 97         1983       13495      15478
## 98        18982       16536      35518
## 99         1066        6358       7424
## 100        2481       11499      13980
## 101        3177        5746       8923
## 102        1840        3755       5595
## 103         622        5933       6555
## 104       94131       15197     109328
## 105       51759        8993      60752
## 106       16931        4078      21009
## 107        2278        1966       4244
## 108       36055        6650      42705
## 109      110906       39767     150673
## 110        5212       11215      16427
## 111       23081       21399      44480
## 112        9256       16047      25303
## 113        5616        1978       7594
## 114       13883       38371      52254
## 115       15304       12124      27428
## 116        9657         975      10632
## 117        5784        9138      14922
## 118      221454       28827     250281
## 119       25513        6203      31716
## 120         363        3679       4042
## 121       15324        7225      22549
## 122         799        8207       9006
## 123        3514       20543      24057
## 124       21736        7703      29439
## 125        3920       14951      18871
## 126        6659       32222      38881
## 127        3340       11022      14362
## 128        1590        6369       7959
## 129       12420       10444      22864
## 130      187620       20911     208531
## 131       13750       11671      25421
## 132       42995       32040      75035
## 133     2310046       43904    2353950
## 134       33825        6625      40450
## 135         337        1898       2235
## 136        2098        1880       3978
## 137       52796       14105      66901
## 138       38641       11358      49999
## 139       10019        7423      17442
## 140       52932       11687      64619
## 141        2625        2053       4678
## 142        1378        5492       6870
## 143       74346       32500     106846
## 144       11404       17605      29009
## 145        2835       10891      13726
## 146        1738        8812      10550
## 147        2071       10354      12425
## 148        2574        9582      12156
## 149        1471        8778      10249
## 150         277        1447       1724
## 151       29699        2990      32689
## 152        3621        1982       5603
## 153        2483         637       3120
## 154       21527        7275      28802
## 155        1156        5249       6405
## 156       23138         657      23795
## 157       24237        9014      33251
## 158        3508        7490      10998
## 159       10860       14088      24948
## 160      147059       58210     205269
## 161        4147       27359      31506
## 162        3520       28455      31975
## 163       18734       27993      46727
## 164       31181        3838      35019
## 165       70482       12948      83430
## 166       37720         585      38305
## 167       13867        6588      20455
## 168       56356       19821      76177
## 169       34522        5222      39744
## 170       12165       13122      25287
## 171       24334       12410      36744
## 172       21103        4969      26072
## 173       15229        8501      23730
## 174       12733        3463      16196
## 175       20906        1777      22683
## 176        3431        5572       9003
## 177       10942        1781      12723
## 178       75714       16424      92138
## 179        3355       16754      20109
## 180        4904        9459      14363
## 181       15040       18753      33793
## 182        4999        9890      14889
## 183        1820        7585       9405
## 184        2549        5530       8079
## 185        5237       16835      22072
## 186        7095        9970      17065
## 187        3484        7461      10945
## 188       14120       19974      34094
## 189       38092       15204      53296
## 190        9016       10010      19026
## 191       83870       12399      96269
## 192         327        2249       2576
## 193        1915        1253       3168
## 194         325        2140       2465
## 195       37890        4993      42883
## 196        4735        1055       5790
## 197       11535        2390      13925
## 198       63982       29621      93603
## 199        3172        9142      12314
## 200        7604        8047      15651
## 201         178         510        688
## 203          NA          NA         NA
## 204          NA          NA         NA
cor_matrix <- cor(dataenergía, use = "pairwise.complete.obs")

library(caret)
## Warning: package 'caret' was built under R version 4.3.3
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
## Loading required package: lattice
redundant_vars <- findCorrelation(cor_matrix, cutoff = 0.9)  # Variables con correlación > 0.9
dataenergía <- dataenergía[, -redundant_vars]  # Eliminar variables redundantes
nzv <- nearZeroVar(dataenergía, saveMetrics = TRUE)
print(nzv)
##                     freqRatio percentUnique zeroVar   nzv
## Si_usa_electricidad      1.00      82.32323   FALSE FALSE
## Si_usa_carbon            1.25      77.77778   FALSE FALSE
## Si_usa_lena              1.00      98.98990   FALSE FALSE
dataenergía <- dataenergía[, !nzv$nzv]

dataenergía <- data.frame(lapply(dataenergía, function(x) {
  ifelse(is.na(x), mean(x, na.rm = TRUE), x)
}))
library(psych)
psych::KMO(dataenergía) 
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = dataenergía)
## Overall MSA =  0.62
## MSA for each item = 
## Si_usa_electricidad       Si_usa_carbon         Si_usa_lena 
##                0.63                0.59                0.68
fa_oblimin <- fa(data, nfactors = 1, rotate = "oblimin", fm = "ml")
fa_varimax <- fa(data, nfactors = 1, rotate = "varimax", fm = "ml")

print("Cargas factoriales - Oblimin")
## [1] "Cargas factoriales - Oblimin"
print(fa_oblimin$loadings)
## 
## Loadings:
##                     ML1  
## No_usa_electricidad 1.000
## Si_usa_electricidad 0.995
## Total_electricidad  1.000
## No_usa_gas_GLP      0.977
## Si_usa_gas_GLP      0.998
## Total_gas_GLP       1.000
## No_usa_carbon       1.000
## Si_usa_carbon       0.523
## Total_carbon        1.000
## No_usa_lena         0.999
## Si_usa_lena         0.350
## Total_lena          1.000
## 
##                   ML1
## SS loadings    10.330
## Proportion Var  0.861
print("Cargas factoriales - Varimax")
## [1] "Cargas factoriales - Varimax"
print(fa_varimax$loadings)
## 
## Loadings:
##                     ML1  
## No_usa_electricidad 1.000
## Si_usa_electricidad 0.995
## Total_electricidad  1.000
## No_usa_gas_GLP      0.977
## Si_usa_gas_GLP      0.998
## Total_gas_GLP       1.000
## No_usa_carbon       1.000
## Si_usa_carbon       0.523
## Total_carbon        1.000
## No_usa_lena         0.999
## Si_usa_lena         0.350
## Total_lena          1.000
## 
##                   ML1
## SS loadings    10.330
## Proportion Var  0.861
# Paso 3: Verificar la varianza explicada
print("Varianza explicada - Oblimin")
## [1] "Varianza explicada - Oblimin"
print(fa_oblimin$Vaccounted)
##                       ML1
## SS loadings    10.3301314
## Proportion Var  0.8608443
print("Varianza explicada - Varimax")
## [1] "Varianza explicada - Varimax"
print(fa_varimax$Vaccounted)
##                       ML1
## SS loadings    10.3301314
## Proportion Var  0.8608443

RPTA: ES VIABLE RESUMIRLO EN UN FACTOR