Carga de datos

Link="https://en.wikipedia.org/wiki/Democracy_Index"
Path='//*[@id="mw-content-text"]/div[1]/table[2]'
Idemocracia=htmltab::htmltab(Link,Path)

Revisamos los nombres

names(Idemocracia)
##  [1] "Rank >> Rank"                                                    
##  [2] "Country >> Country"                                              
##  [3] "Score >> Score"                                                  
##  [4] "Electoral processand pluralism >> Electoral processand pluralism"
##  [5] "Functio­ning ofgovern­ment >> Functio­ning ofgovern­ment"        
##  [6] "Politicalpartici­pation >> Politicalpartici­pation"              
##  [7] "Politicalculture >> Politicalculture"                            
##  [8] "Civilliberties >> Civilliberties"                                
##  [9] "Regimetype >> Regimetype"                                        
## [10] "Region >> Region"                                                
## [11] "Changes fromlast year >> Changes fromlast year"

Eliminamos columnas

Idemocracia=Idemocracia[,-c(1,3,11)]

Cambiamos nombres

names(Idemocracia)=c("country","elec","funct","partic","cult","civil",
                     "regime","region")

Covertimos a numericos

str(Idemocracia)
## 'data.frame':    167 obs. of  8 variables:
##  $ country: chr  " Norway" " Iceland" " Sweden" " New Zealand" ...
##  $ elec   : chr  "10.00" "10.00" "9.58" "10.00" ...
##  $ funct  : chr  "9.64" "9.29" "9.64" "9.29" ...
##  $ partic : chr  "10.00" "8.89" "8.33" "8.89" ...
##  $ cult   : chr  "10.00" "10.00" "10.00" "8.13" ...
##  $ civil  : chr  "9.71" "9.71" "9.41" "10.00" ...
##  $ regime : chr  "Full democracy" "Full democracy" "Full democracy" "Full democracy" ...
##  $ region : chr  "Western Europe" "Western Europe" "Western Europe" "Asia & Australasia" ...
Idemocracia[,2:6]=lapply(Idemocracia[,2:6],as.numeric)

Categorico numerica

fcatnum=formula(elec ~ region)
aggregate(fcatnum, Idemocracia, mean)
##                       region     elec
## 1         Asia & Australasia 6.122143
## 2             Eastern Europe 6.309286
## 3              Latin America 7.475833
## 4 Middle East & North Africa 2.495500
## 5              North America 9.375000
## 6         Sub-Saharan Africa 3.993182
## 7             Western Europe 9.351429

Formula 2

f2=formula(civil ~ regime)
aggregate(f2, Idemocracia, mean)
##             regime    civil
## 1    Authoritarian 2.576481
## 2 Flawed democracy 7.652778
## 3   Full democracy 9.399545
## 4    Hybrid regime 5.389459
library(ggpubr)
## Loading required package: ggplot2
ggqqplot(data=Idemocracia,x="civil") + facet_grid(. ~ regime)

Shapiro

# funcion ad-hoc
normalidadTest=function(x) {y =shapiro.test(x); 
                            c(y$statistic, y$p.value)}
# calculando
resultado= aggregate(f2, Idemocracia,
                     FUN = normalidadTest) 


# mostrando resultado
library(knitr)

shapiroTest=as.data.frame(resultado[,2])
names(shapiroTest)=c("SW_Statistic","Probabilidad")
kable(cbind(resultado[1],shapiroTest))
regime SW_Statistic Probabilidad
Authoritarian 0.9587312 0.0604616
Flawed democracy 0.9583597 0.0581949
Full democracy 0.9323359 0.1372425
Hybrid regime 0.9759198 0.5904018

F

summary(aov(f2, data=Idemocracia))
##              Df Sum Sq Mean Sq F value Pr(>F)    
## regime        3 1037.2   345.7   341.2 <2e-16 ***
## Residuals   163  165.1     1.0                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(ggpubr)
ggerrorplot(Idemocracia, x = "regime", 
            y = "civil", 
            desc_stat = "mean_ci"
            )

Num num

library(ggpubr)
p1=ggscatter(Idemocracia, 
          x = "elec", y = "civil",
          cor.coef = TRUE, 
          cor.method = "spearman") # spearman?

p1

library(dlookr)
## Loading required package: mice
## 
## Attaching package: 'mice'
## The following object is masked from 'package:stats':
## 
##     filter
## The following objects are masked from 'package:base':
## 
##     cbind, rbind
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Warning in fun(libname, pkgname): couldn't connect to display ":0"
## 
## Attaching package: 'dlookr'
## The following object is masked from 'package:base':
## 
##     transform
normality(Idemocracia[,2:6])
## # A tibble: 5 x 4
##   vars   statistic  p_value sample
##   <chr>      <dbl>    <dbl>  <dbl>
## 1 elec       0.858 2.09e-11    167
## 2 funct      0.974 3.55e- 3    167
## 3 partic     0.978 1.06e- 2    167
## 4 cult       0.964 2.67e- 4    167
## 5 civil      0.955 3.10e- 5    167