parte 1
library(htmltab)
democracia= htmltab(doc = "https://es.wikipedia.org/wiki/%C3%8Dndice_de_democracia",
which ="//*/div/table[2]",
encoding = "UTF-8")
names(democracia)
[1] "Puesto" "País" "Puntuación"
[4] "Proceso electoraly pluralismo" "Funcionamientodel gobierno" "Participaciónpolítica"
[7] "Culturapolítica" "Derechosciviles" "Categoría"
me quedo con pais y score
democracia=democracia[,c(2,3)]
names(democracia)
[1] "País" "Puntuación"
str(democracia)
'data.frame': 167 obs. of 2 variables:
$ País : chr "Noruega" " Islandia" "Suecia" " Nueva Zelanda" ...
$ Puntuación: chr "9.87" "9.58" "9.39" "9.26" ...
democracia$Puntuación=as.numeric(democracia$Puntuación)
str(democracia)
'data.frame': 167 obs. of 2 variables:
$ País : chr "Noruega" " Islandia" "Suecia" " Nueva Zelanda" ...
$ Puntuación: num 9.87 9.58 9.39 9.26 9.22 9.15 9.15 9.14 9.09 9.03 ...
row.names(democracia)=NULL
Historiograma
library(ggplot2)
histNum=ggplot(democracia,aes(x=Puntuación))+ geom_histogram(bins=5)
histNum
Del gráfico anterior, podemos decir que la distribución no es simétrica por lo tanto la MEDIA no será una medida representativa (la MEDIANA será mejor opción Boxplot Esto es mejor para revisar los valores perdidos
box=ggplot(democracia,aes(y=Puntuación)) + geom_boxplot() + coord_flip()
box
Estadigrafos
summary(democracia$Puntuación)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.080 3.545 5.690 5.479 7.175 9.870
library(ggplot2)
estadigrafos=round(as.vector(summary(democracia$Puntuación)),10)
box + scale_y_continuous(breaks = estadigrafos)
En este caso, observamos que la media es mayor a la mediana, eso significa que se tiene una asimetría positiva.
Ahora podemos confirmarla con el siguiente codigo
library(DescTools)
Skew(democracia$Puntuación,conf.level = 0.05)
skew lwr.ci upr.ci
-0.07107195 -0.08745086 -0.07241390
distancia intercuartilica
IQR(democracia$Puntuación)
[1] 3.63
Es decir, entre el primer y tercer cuartil hay 3.63 valores. Así, podemos proponer que un atípico es aquel que está a una distancia lejana de estos valores centrales.
Ahora podemos representarlos por GINI
Gini(democracia$Puntuación,conf.level=0.95)
gini lwr.ci upr.ci
0.2316285 0.2089967 0.2574779
Grafiquemos en la curva de Lorentz
library(ggplot2)
library(gglorenz)
ggplot(democracia,aes(x=Puntuación))+ gglorenz::stat_lorenz(color='purple') +
geom_abline(linetype = "dashed") + coord_fixed() +
labs(x = "% Paises ordenados por puntuación",
y = " Puntuación",
title = "Relación pais/puntuación",
caption = "Fuente: Wikipedia")
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.