Pregunta 1:
library (rio)
elec=import("Electricity - installed generating capacity.csv")
names(elec)
## [1] "name" "slug" "kW"
## [4] "date_of_information" "ranking" "region"
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
df=elec%>%
select("name", "kW")
produc=import("Refined petroleum products - production.csv")
names(produc)
## [1] "name" "slug" "bbl/day"
## [4] "date_of_information" "ranking" "region"
df2=produc%>%
select("name", "bbl/day")
carbon=import("Carbon dioxide emissions.csv")
names(carbon)
## [1] "name" "slug" "metric tonnes of CO2"
## [4] "date_of_information" "ranking" "region"
df3=carbon%>%
select("name", "metric tonnes of CO2")
energy=import("Energy consumption per capita.csv")
names(energy)
## [1] "name" "slug" "Btu/person"
## [4] "date_of_information" "ranking" "region"
df4=energy%>%
select("name", "Btu/person")
telefix=import("Telephones - fixed lines.csv")
names(telefix)
## [1] "name" "slug" "value"
## [4] "date_of_information" "ranking" "region"
df5=telefix%>%
select("name", "value")
names(df5)[names(df5)=="value"]="valuefix"
telemob=import("Telephones - mobile cellular.csv")
names(telemob)
## [1] "name" "slug" "value"
## [4] "date_of_information" "ranking" "region"
df6=telemob%>%
select("name", "value")
names(df6)[names(df6)=="value"]="valuemob"
broad=import("Broadband - fixed subscriptions.csv")
names(broad)
## [1] "name" "slug" "value"
## [4] "date_of_information" "ranking" "region"
df11=broad%>%
select("name", "value")
names(df11)[names(df11)=="value"]="valuebroad"
inflat=import("Inflation rate (consumer prices).csv")
names(inflat)
## [1] "name" "slug" "%"
## [4] "date_of_information" "ranking" "region"
df7=inflat%>%
select("name", "%")
names(df7)[names(df7)=="%"]="% of inflation"
youth=import("Youth unemployment rate (ages 15-24).csv")
names(youth)
## [1] "name" "slug" "%"
## [4] "date_of_information" "ranking" "region"
df8=youth%>%
select("name", "%")
names(df8)[names(df8)=="%"]="% of youth unemployment"
debt=import("Public debt.csv")
names(debt)
## [1] "name" "slug" "% of GDP"
## [4] "date_of_information" "ranking" "region"
df9=debt%>%
select("name", "% of GDP")
names(df9)[names(df9)=="% of GDP"]="% public debt"
debtex=import("Public debt.csv")
names(debtex)
## [1] "name" "slug" "% of GDP"
## [4] "date_of_information" "ranking" "region"
df10=debtex%>%
select("name", "% of GDP")
names(df10)[names(df10)=="% of GDP"]="% public debt-external"
datafinal<-merge(df, df2, by = "name")
print(datafinal)
## name kW bbl/day
## 1 Afghanistan 776,000 0
## 2 Albania 2,531,000 5,638
## 3 Algeria 21,694,000 627,900
## 4 American Samoa 47,000 0
## 5 Angola 7,344,000 53,480
## 6 Antigua and Barbuda 117,000 0
## 7 Argentina 44,731,000 669,800
## 8 Armenia 3,633,000 0
## 9 Aruba 296,000 0
## 10 Australia 82,517,000 462,500
## 11 Austria 28,376,000 186,500
## 12 Azerbaijan 7,677,000 138,900
## 13 Bahamas, The 578,000 0
## 14 Bahrain 6,982,000 274,500
## 15 Bangladesh 18,461,000 26,280
## 16 Barbados 311,000 0
## 17 Belarus 11,360,000 477,200
## 18 Belgium 26,929,000 731,700
## 19 Belize 204,000 36
## 20 Benin 475,000 0
## 21 Bermuda 172,000 0
## 22 Bhutan 2,334,000 0
## 23 Bolivia 3,834,000 65,960
## 24 Bosnia and Herzegovina 4,775,000 0
## 25 Botswana 766,000 0
## 26 Brazil 195,037,000 2,811,000
## 27 British Virgin Islands 33,000 0
## 28 Brunei 1,261,000 10,310
## 29 Bulgaria 11,097,000 144,300
## 30 Burkina Faso 392,000 0
## 31 Burma 7,247,000 13,330
## 32 Burundi 100,000 0
## 33 Cabo Verde 205,000 0
## 34 Cambodia 2,954,000 0
## 35 Cameroon 1,754,000 39,080
## 36 Canada 153,251,000 2,009,000
## 37 Cayman Islands 174,000 0
## 38 Central African Republic 38,000 0
## 39 Chad 87,000 0
## 40 Chile 29,808,000 216,200
## 41 China 2,217,925,000 11,510,000
## 42 Colombia 19,769,000 303,600
## 43 Comoros 35,000 0
## 44 Congo, Democratic Republic of the 2,919,000 0
## 45 Congo, Republic of the 629,000 15,760
## 46 Cook Islands 18,000 0
## 47 Costa Rica 3,674,000 0
## 48 Cote d'Ivoire 2,197,000 69,360
## 49 Croatia 4,940,000 74,620
## 50 Cuba 7,479,000 104,100
## 51 Cyprus 1,881,000 0
## 52 Czechia 22,485,000 177,500
## 53 Denmark 17,655,000 183,900
## 54 Djibouti 130,000 0
## 55 Dominica 42,000 0
## 56 Dominican Republic 5,674,000 16,060
## 57 Ecuador 9,354,000 137,400
## 58 Egypt 59,826,000 547,500
## 59 El Salvador 2,586,000 0
## 60 Equatorial Guinea 349,000 0
## 61 Eritrea 228,000 0
## 62 Estonia 3,030,000 0
## 63 Eswatini 286,000 0
## 64 Ethiopia 4,856,000 0
## 65 Falkland Islands (Islas Malvinas) 11,000 0
## 66 Faroe Islands 128,000 0
## 67 Fiji 393,000 0
## 68 Finland 20,418,000 310,600
## 69 France 138,611,000 1,311,000
## 70 French Polynesia 272,000 0
## 71 Gabon 784,000 16,580
## 72 Gambia, The 137,000 0
## 73 Georgia 4,579,000 247
## 74 Germany 248,265,000 2,158,000
## 75 Ghana 5,312,000 2,073
## 76 Gibraltar 43,000 0
## 77 Greece 21,545,000 655,400
## 78 Greenland 187,000 0
## 79 Grenada 55,000 0
## 80 Guam 455,000 0
## 81 Guatemala 5,185,000 1,162
## 82 Guinea 992,000 0
## 83 Guinea-Bissau 28,000 0
## 84 Guyana 380,000 0
## 85 Haiti 3,453,000 0
## 86 Honduras 3,991,000 0
## 87 Hong Kong 14,168,000 0
## 88 Hungary 10,873,000 152,400
## 89 Iceland 2,967,000 0
## 90 India 432,768,000 4,897,000
## 91 Indonesia 69,065,000 950,000
## 92 Iran 80,553,000 1,764,000
## 93 Iraq 28,369,000 398,000
## 94 Ireland 11,430,000 64,970
## 95 Israel 18,993,000 294,300
## 96 Italy 121,442,000 1,607,000
## 97 Jamaica 1,216,000 24,250
## 98 Japan 348,666,000 3,467,000
## 99 Jordan 5,644,000 67,240
## 100 Kazakhstan 25,022,000 290,700
## 101 Kenya 3,304,000 13,960
## 102 Kiribati 11,000 0
## 103 Korea, North 8,413,000 11,270
## 104 Korea, South 135,789,000 3,302,000
## 105 Kosovo 1,424,000 0
## 106 Kuwait 19,371,000 915,800
## 107 Kyrgyzstan 4,626,000 6,996
## 108 Laos 9,346,000 0
## 109 Latvia 3,089,000 0
## 110 Lebanon 3,768,000 0
## 111 Lesotho 74,000 0
## 112 Liberia 196,000 0
## 113 Libya 10,516,000 89,620
## 114 Lithuania 3,512,000 196,500
## 115 Luxembourg 1,899,000 0
## 116 Macau 478,000 0
## 117 Madagascar 587,000 0
## 118 Malawi 618,000 0
## 119 Malaysia 34,959,000 528,300
## 120 Maldives 545,000 0
## 121 Mali 890,000 0
## 122 Malta 784,000 0
## 123 Mauritania 656,000 0
## 124 Mauritius 936,000 0
## 125 Mexico 93,430,000 844,600
## 126 Moldova 594,000 232
## 127 Mongolia 1,479,000 0
## 128 Montenegro 1,007,000 0
## 129 Montserrat 5,000 0
## 130 Morocco 14,187,000 66,230
## 131 Mozambique 2,765,000 0
## 132 Namibia 640,000 0
## 133 Nauru 15,000 0
## 134 Nepal 1,392,000 0
## 135 Netherlands 43,409,000 1,282,000
## 136 New Caledonia 1,071,000 0
## 137 New Zealand 9,615,000 115,100
## 138 Nicaragua 1,837,000 14,720
## 139 Niger 324,000 15,280
## 140 Nigeria 11,691,000 35,010
## 141 Niue 3,000 0
## 142 North Macedonia 1,928,000 0
## 143 Norway 38,360,000 371,600
## 144 Oman 8,601,000 229,600
## 145 Pakistan 39,925,000 291,200
## 146 Panama 4,106,000 0
## 147 Papua New Guinea 1,139,000 22,170
## 148 Paraguay 8,831,000 0
## 149 Peru 15,340,000 166,600
## 150 Philippines 27,885,000 215,500
## 151 Poland 47,269,000 554,200
## 152 Portugal 22,364,000 323,000
## 153 Puerto Rico 6,180,000 0
## 154 Qatar 10,633,000 273,800
## 155 Romania 20,528,000 232,600
## 156 Russia 276,463,000 6,076,000
## 157 Rwanda 265,000 0
## 158 Saint Helena, Ascension, and Tristan da Cunha 8,000 0
## 159 Saint Kitts and Nevis 71,000 0
## 160 Saint Lucia 92,000 0
## 161 Saint Pierre and Miquelon 26,000 0
## 162 Saint Vincent and the Grenadines 49,000 0
## 163 Samoa 50,000 0
## 164 Sao Tome and Principe 28,000 0
## 165 Saudi Arabia 76,785,000 2,476,000
## 166 Senegal 1,312,000 17,590
## 167 Serbia 8,986,000 74,350
## 168 Seychelles 157,000 0
## 169 Sierra Leone 180,000 0
## 170 Singapore 12,240,000 755,000
## 171 Slovakia 7,868,000 131,300
## 172 Slovenia 4,062,000 0
## 173 Solomon Islands 40,000 0
## 174 Somalia 91,000 0
## 175 South Africa 62,728,000 487,100
## 176 South Sudan 121,000 0
## 177 Spain 115,837,000 1,361,000
## 178 Sri Lanka 4,527,000 34,210
## 179 Sudan 4,354,000 94,830
## 180 Suriname 542,000 7,571
## 181 Sweden 43,499,000 413,200
## 182 Switzerland 22,921,000 61,550
## 183 Syria 10,082,000 111,600
## 184 Taiwan 57,738,000 924,000
## 185 Tajikistan 7,114,000 172
## 186 Tanzania 1,623,000 0
## 187 Thailand 53,130,000 1,328,000
## 188 Timor-Leste 284,000 0
## 189 Togo 210,000 0
## 190 Tonga 26,000 0
## 191 Trinidad and Tobago 2,123,000 134,700
## 192 Tunisia 5,777,000 27,770
## 193 Turkey (Turkiye) 96,846,000 657,900
## 194 Turkmenistan 5,205,000 191,100
## 195 Turks and Caicos Islands 85,000 0
## 196 Uganda 2,397,000 0
## 197 Ukraine 56,816,000 63,670
## 198 United Arab Emirates 35,173,000 943,500
## 199 United Kingdom 113,153,000 1,290,000
## 200 United States 1,143,266,000 20,300,000
## 201 Uruguay 5,348,000 42,220
## 202 Uzbekistan 16,042,000 61,740
## 203 Vanuatu 35,000 0
## 204 Venezuela 32,956,000 926,300
## 205 Vietnam 65,283,000 153,800
## 206 Virgin Islands 321,000 0
## 207 West Bank 215,000 0
## 208 Yemen 1,772,000 20,180
## 209 Zambia 3,065,000 13,120
## 210 Zimbabwe 2,473,000 0
datafinal2=merge(datafinal, df3, by ="name")
datafinal3=merge(datafinal2, df4, by ="name")
datafinal4=merge(datafinal3, df5, by ="name")
datafinal5=merge(datafinal4, df6, by ="name")
datafinal6=merge(datafinal5, df7, by ="name")
datafinal7=merge(datafinal6, df8, by ="name")
datafinal8=merge(datafinal7, df9, by ="name")
datafinall=merge(datafinal8, df10, by ="name")
data12=merge(datafinall, df11, by="name")
Análisis factorial:
Se encuentra:
names(data12)
## [1] "name" "kW"
## [3] "bbl/day" "metric tonnes of CO2"
## [5] "Btu/person" "valuefix"
## [7] "valuemob" "% of inflation"
## [9] "% of youth unemployment" "% public debt"
## [11] "% public debt-external" "valuebroad"
summary(data12)
## name kW bbl/day metric tonnes of CO2
## Length:181 Length:181 Length:181 Length:181
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## Btu/person valuefix valuemob % of inflation
## Length:181 Length:181 Length:181 Length:181
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## % of youth unemployment % public debt % public debt-external
## Min. : 1.00 Min. : 0.00 Min. : 0.00
## 1st Qu.: 9.80 1st Qu.: 34.70 1st Qu.: 34.70
## Median :16.50 Median : 50.60 Median : 50.60
## Mean :19.36 Mean : 57.68 Mean : 57.68
## 3rd Qu.:27.20 3rd Qu.: 71.41 3rd Qu.: 71.41
## Max. :79.90 Max. :237.35 Max. :237.35
## valuebroad
## Length:181
## Class :character
## Mode :character
##
##
##
str(data12)
## 'data.frame': 181 obs. of 12 variables:
## $ name : chr "Afghanistan" "Albania" "Algeria" "Angola" ...
## $ kW : chr "776,000" "2,531,000" "21,694,000" "7,344,000" ...
## $ bbl/day : chr "0" "5,638" "627,900" "53,480" ...
## $ metric tonnes of CO2 : chr "7,893,000" "3,794,000" "151,633,000" "19,362,000" ...
## $ Btu/person : chr "3,227,000" "38,442,000" "61,433,000" "11,693,000" ...
## $ valuefix : chr "146,000" "177,000" "5,576,000" "94,000" ...
## $ valuemob : chr "22,678,000" "2,782,000" "49,019,000" "23,978,000" ...
## $ % of inflation : chr "2.3" "6.73" "9.27" "25.75" ...
## $ % of youth unemployment: num 20.2 27.8 31.9 18.5 29.9 36.1 10.8 11.4 16.5 30.8 ...
## $ % public debt : num 7 82.4 27.5 65 57.6 ...
## $ % public debt-external : num 7 82.4 27.5 65 57.6 ...
## $ valuebroad : chr "26,570" "508,937" "3,790,459" "230,610" ...
data12=na.omit(data12)
data12 <- data12 %>%
mutate_if(is.character, as.factor)
library(psych)
library(GPArotation)
##
## Attaching package: 'GPArotation'
## The following objects are masked from 'package:psych':
##
## equamax, varimin
data12=data12%>%
mutate_if(is.character, ~ factor(.,ordered=TRUE))
names(data12)
## [1] "name" "kW"
## [3] "bbl/day" "metric tonnes of CO2"
## [5] "Btu/person" "valuefix"
## [7] "valuemob" "% of inflation"
## [9] "% of youth unemployment" "% public debt"
## [11] "% public debt-external" "valuebroad"
dont=c("name", "kW", "bbl/day", "kW", "bbl/day", "metric tonnes of CO2", "Btu/person", "valuefix", "valuemob")
select=setdiff(names(data12), dont)
theData=data12[,select]
library(magrittr)
head(theData,10)%>%
rmarkdown::paged_table()
theData=theData%>%
mutate_if(is.character, ~ factor(., ordered = TRUE))
str(theData)
## 'data.frame': 181 obs. of 5 variables:
## $ % of inflation : Factor w/ 170 levels "0.96","1","1.05",..: 57 125 162 71 70 156 121 155 34 108 ...
## $ % of youth unemployment: num 20.2 27.8 31.9 18.5 29.9 36.1 10.8 11.4 16.5 30.8 ...
## $ % public debt : num 7 82.4 27.5 65 57.6 ...
## $ % public debt-external : num 7 82.4 27.5 65 57.6 ...
## $ valuebroad : Factor w/ 173 levels "1,000","1,030,973",..: 75 129 91 66 168 118 165 52 19 164 ...
library(polycor)
##
## Attaching package: 'polycor'
## The following object is masked from 'package:psych':
##
## polyserial
corMatrix=polycor::hetcor(theData)$correlations
## Warning in FUN(X[[i]], ...): polychoric correlation between variables % of inflation and valuebroad produced warnings:
## Se han producido NaNs
## Se han producido NaNs
## Warning in hetcor.data.frame(theData): the correlation matrix has been adjusted
## to make it positive-definite
round(corMatrix,2)
## % of inflation % of youth unemployment % public debt
## % of inflation 1.00 0.01 0.20
## % of youth unemployment 0.01 1.00 0.10
## % public debt 0.20 0.10 1.00
## % public debt-external 0.20 0.10 1.00
## valuebroad -0.07 0.10 -0.07
## % public debt-external valuebroad
## % of inflation 0.20 -0.07
## % of youth unemployment 0.10 0.10
## % public debt 1.00 -0.07
## % public debt-external 1.00 -0.07
## valuebroad -0.07 1.00
library(ggcorrplot)
## Loading required package: ggplot2
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggcorrplot(corMatrix)
library(psych)
psych::KMO(corMatrix)
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA = 0.53
## MSA for each item =
## % of inflation % of youth unemployment % public debt
## 0.96 0.73 0.51
## % public debt-external valuebroad
## 0.51 0.62
cortest.bartlett(corMatrix,n=nrow(theData))$p.value>0.05
## [1] FALSE
Es una matriz singular
library(matrixcalc)
is.singular.matrix(corMatrix)
## [1] FALSE
datafinall <- datafinall %>%
mutate_if(is.character, ~ factor(., ordered = TRUE))
#KMO(data12)
Clúster aglomerativo jerárquico eligiendo cada grupo: Econ/Comms/Energy
boxplot(data12[,c(4:7)],horizontal = F,las=2,cex.axis = 0.5)
library(BBmisc)
##
## Attaching package: 'BBmisc'
## The following objects are masked from 'package:dplyr':
##
## coalesce, collapse, symdiff
## The following object is masked from 'package:base':
##
## isFALSE
data12[,c(2:11)]=normalize(data12[,c(2:11)],method='standardize')
names(data12)
## [1] "name" "kW"
## [3] "bbl/day" "metric tonnes of CO2"
## [5] "Btu/person" "valuefix"
## [7] "valuemob" "% of inflation"
## [9] "% of youth unemployment" "% public debt"
## [11] "% public debt-external" "valuebroad"
dataenergy=data12[,c(2:4)]
row.names(dataenergy)=data12$name
datacomm=data12[,c(5:7)]
row.names(datacomm)=data12$name
dataeconomy=data12[,c(8:12)]
row.names(dataeconomy)=data12$name
library(cluster)
#g.dist = daisy(dataClus, metric="gower")
PAM:
library(kableExtra)
##
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
#set.seed(123)
#res.pam=pam(g.dist,3,cluster.only = F)
#dataenergy$pam=res.pam$cluster
#head(dataenergy,15)%>%kbl()%>%kable_styling()
#set.seed(123)
#res.pam=pam(g.dist,3,cluster.only = F)
#dataeconomy$pam=res.pam$cluster
#head(dataClus,15)%>%kbl()%>%kable_styling()