library(rio)
library(htmltab)
link='https://github.com/JoseManuelMagallanes/Estadistica_Para_AnalisisPolitico/raw/master/lapop17.dta'
lapop17=import(link)
library(stringr)
names(lapop17)=str_split(names(lapop17)," ",simplify = T)[,1]
names(lapop17)=str_replace_all(names(lapop17), "[^[:ascii:]]", "")
lapop17[,]=lapply(lapop17[,], str_replace_all,"[^[:ascii:]]","")
names(lapop17)
[1] "pais" "sobremuestra" "idnum" "uniq_id" "upm" "prov" "municipio" "cluster"
[9] "ur" "tamano" "idiomaq" "fecha" "wt" "estratopri" "estratosec" "q2"
[17] "q1" "perprov" "ls3" "a4" "soct2" "idio2" "np1" "sgl1"
[25] "cp6" "cp7" "cp8" "cp13" "cp20" "it1" "l1" "prot3"
[33] "jc10" "jc13" "jc15a" "vic1ext" "vic1exta" "vic2new" "aoj11" "aoj12"
[41] "b1" "b2" "b3" "b4" "b6" "b43" "b12" "b13"
[49] "b18" "b21" "b21a" "b32" "b37" "b47a" "m1" "m2"
[57] "sd2new2" "sd3new2" "sd6new2" "infrax" "infra3" "ros1" "ros4" "ing4"
[65] "eff1" "eff2" "aoj22new" "media3" "media4" "exp_a" "dst1b" "drk1"
[73] "env1c" "env2b" "envp3l" "envp3l_o" "envp3n" "envp3n_o" "envp41" "envp42"
[81] "pn4" "w14a" "e5" "e15" "e16" "d1" "d2" "d3"
[89] "d4" "d5" "d6" "ivv3" "lib1" "lib2b" "lib2c" "lib4"
[97] "exc2" "exc6" "exc20" "exc11" "exc13" "exc14" "exc15" "exc16"
[105] "exc18" "exc7" "exc7new" "vb1" "vb2" "vb3n" "vb10" "vb11"
[113] "pol1" "vb20" "dis7a" "dis8a" "dis9a" "dis10a" "dis11a" "for5"
[121] "mil10a" "mil10e" "ccq1" "ccq2" "ccq3" "ccq4" "ie1" "ie2"
[129] "ie3" "ie6" "ie9" "ie10" "envp8" "wf1" "cct1b" "ed"
[137] "ed2" "q5a" "q5b" "q3c" "ocup4a" "ocup1a" "q10g" "q10new"
[145] "q14" "q10d" "q10e" "q11n" "q12c" "q12bn" "q12" "q12m"
[153] "q12f" "vac1" "etid" "iiet1" "iiet2" "iiet3" "www1" "gi0"
[161] "pr1" "r3" "r4" "r4a" "r5" "r6" "r7" "r8"
[169] "r12" "r14" "r15" "r18" "r1" "r16" "colorr" "conocim"
[177] "iarea1" "iarea2" "iarea3" "iarea4" "iarea6" "iarea7" "sexi" "colori"
[185] "srvyrid" "nationality" "formatq" "sex"
library(htmltab)
links=list(web="https://es.wikipedia.org/wiki/%C3%8Dndice_global_de_felicidad",
xpath ='//*[@id="mw-content-text"]/div/table')
feli<- htmltab(doc = links$web, which =links$xpath)
str(feli)
'data.frame': 156 obs. of 9 variables:
$ № : chr "1" "2" "3" "4" ...
$ País : chr "Finlandia" "Noruega" "Dinamarca" "Islandia" ...
$ Puntuación : chr "7.633" "7.594" "7.555" "7.495" ...
$ PIB per cápita : chr "1.305" "1.456" "1.351" "1.343" ...
$ Apoyo social : chr "1.592" "1.582" "1.590" "1.644" ...
$ Esperanza de años de vida saludable : chr "0.874" "0.861" "0.868" "0.914" ...
$ Libertad para tomar decisiones vitales: chr "0.681" "0.686" "0.683" "0.677" ...
$ Generosidad : chr "0.192" "0.286" "0.284" "0.353" ...
$ Percepción de la corrupción : chr "0.393" "0.340" "0.408" "0.138" ...
names(feli)
[1] "№" "País" "Puntuación"
[4] "PIB per cápita" "Apoyo social" "Esperanza de años de vida saludable"
[7] "Libertad para tomar decisiones vitales" "Generosidad" "Percepción de la corrupción"
library(stringr)
names(feli)=str_split(names(feli)," ",simplify = T)[,1]
names(feli)=str_replace_all(names(feli), "[^[:ascii:]]", "")
feli[,]=lapply(feli[,], str_replace_all,"[^[:ascii:]]","")
feli$Rank=NULL
str(feli)
'data.frame': 156 obs. of 9 variables:
$ : chr "1" "2" "3" "4" ...
$ Pas : chr "Finlandia" "Noruega" "Dinamarca" "Islandia" ...
$ Puntuacin : chr "7.633" "7.594" "7.555" "7.495" ...
$ PIB : chr "1.305" "1.456" "1.351" "1.343" ...
$ Apoyo : chr "1.592" "1.582" "1.590" "1.644" ...
$ Esperanza : chr "0.874" "0.861" "0.868" "0.914" ...
$ Libertad : chr "0.681" "0.686" "0.683" "0.677" ...
$ Generosidad: chr "0.192" "0.286" "0.284" "0.353" ...
$ Percepcin : chr "0.393" "0.340" "0.408" "0.138" ...
feli[,c(3:9)]=lapply(feli[,c(3:9)],parse_number)
1.La puntuación de la felicidad se distribuye asimetricamente?
str(demo)
'data.frame': 167 obs. of 9 variables:
$ Country : chr "Norway" "Iceland" "Sweden" "New Zealand" ...
$ Score : num 9.87 9.58 9.39 9.26 9.22 9.15 9.15 9.14 9.09 9.03 ...
$ Electoral : num 10 10 9.58 10 10 9.58 9.58 10 10 9.58 ...
$ Functioning : num 9.64 9.29 9.64 9.29 9.29 7.86 9.64 8.93 8.93 9.29 ...
$ Politicalparticipation: num 10 8.89 8.33 8.89 8.33 8.33 7.78 8.33 7.78 7.78 ...
$ Politicalculture : num 10 10 10 8.13 9.38 10 8.75 8.75 8.75 9.38 ...
$ Civilliberties : num 9.71 9.71 9.41 10 9.12 10 10 9.71 10 9.12 ...
$ Regimetype : Ord.factor w/ 4 levels "Authoritarian"<..: 4 4 4 4 4 4 4 4 4 4 ...
$ Continent : Factor w/ 6 levels "Africa","Asia",..: 3 3 3 5 3 3 4 3 5 3 ...
library(ggplot2)
histNum=ggplot(feli,aes(x=Puntuacin))+ geom_histogram(bins=5)
histNum
Podemos decir del gráfico anterior que la distribución no es simétrica
2.¿El valor representativo de indice de felicidad es robusto?
summary(feli$Puntuacin)
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.905 4.454 5.378 5.376 6.168 7.633
basen=ggplot(data=feli,aes(x=Puntuacin))
basen + geom_histogram(bins=10)
Skew(feli$Puntuacin,conf.level = 0.05)
skew lwr.ci upr.ci
0.01494113 0.00845982 0.02190004
Gini(feli$Puntuacin)
[1] 0.1196207
La desigualdad es cuando se acerca a 1
library(ggplot2)
library(gglorenz)
ggplot(feli,aes(x=Puntuacin))+ gglorenz::stat_lorenz(color='red') +
geom_abline(linetype = "dashed") + coord_fixed() +
labs(x = "% Paises ordenados por Indice de felicidad",
y = "% Acumulado de Puntuacin de IF",
title = "Relación pais/Indice de Felicidad",
caption = "Astor, Maggie (14 de marzo de 2018)") +
scale_y_continuous(breaks=seq(0,1,0.15)) +
scale_x_continuous(breaks=seq(0,1,0.2))
library(ggplot2)
basep=ggplot(data=feli, aes(y=as.numeric(Puntuacin)))
basep + geom_boxplot() + coord_flip()
y clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.