Milla LOSTAUNAU, Brayan 18020469
library(htmltab)
finales = htmltab(doc = "https://es.wikipedia.org/wiki/%C3%8Dndice_global_de_felicidad",
which = '//*[@id="mw-content-text"]/div/table' )
str(finales)
## '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.560" "7.555" "7.495" ...
## $ PIB per cápita : chr "1.305" "1.372" "1.351" "1.343" ...
## $ Apoyo social : chr "1.592" "1.595" "1.590" "1.644" ...
## $ Esperanza de años de vida saludable : chr "0.874" "0.870" "0.868" "0.914" ...
## $ Libertad para tomar decisiones vitales: chr "0.681" "0.685" "0.683" "0.677" ...
## $ Generosidad : chr "0.192" "0.285" "0.284" "0.353" ...
## $ Percepción de la corrupción : chr "0.393" "0.410" "0.408" "0.138" ...
finales[,-c(2)]=lapply(finales[,-c(2)],as.numeric)
names(finales)[6]="Esperanza"
names(finales)[9]="Corrupcion"
names(finales)[5]="Apoyo"
names(finales)[4]="PIB"
names(finales)[7]="Libertad"
names(finales)[8]="Generosidad"
names(finales)[9]="Percepción"
str(finales)
## 'data.frame': 156 obs. of 9 variables:
## $ № : num 1 2 3 4 5 6 7 8 9 10 ...
## $ País : chr "Finlandia" "Noruega" "Dinamarca" "Islandia" ...
## $ Puntuación : num 7.63 7.56 7.55 7.5 7.49 ...
## $ PIB : num 1.3 1.37 1.35 1.34 1.42 ...
## $ Apoyo : num 1.59 1.59 1.59 1.64 1.55 ...
## $ Esperanza : num 0.874 0.87 0.868 0.914 0.927 0.878 0.896 0.876 0.913 0.91 ...
## $ Libertad : num 0.681 0.685 0.683 0.677 0.66 0.638 0.653 0.669 0.659 0.647 ...
## $ Generosidad: num 0.192 0.285 0.284 0.353 0.256 0.333 0.321 0.365 0.285 0.361 ...
## $ Percepción : num 0.393 0.41 0.408 0.138 0.357 0.295 0.291 0.389 0.383 0.302 ...
La correlación entre PIB y GENEROSIDAD es LA 1 Y 2 POR REGRESIÓN
hipo3= lm(Percepción~Generosidad+Esperanza+PIB+Apoyo+Libertad, data=finales)
summary(hipo3)
##
## Call:
## lm(formula = Percepción ~ Generosidad + Esperanza + PIB + Apoyo +
## Libertad, data = finales)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.18413 -0.05413 -0.01276 0.04097 0.31134
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.04356 0.03013 -1.446 0.150266
## Generosidad 0.25261 0.07090 3.563 0.000492 ***
## Esperanza 0.04928 0.05160 0.955 0.341075
## PIB 0.04141 0.03269 1.267 0.207190
## Apoyo -0.03841 0.03153 -1.218 0.225052
## Libertad 0.19913 0.04727 4.213 4.34e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.08212 on 150 degrees of freedom
## Multiple R-squared: 0.3098, Adjusted R-squared: 0.2868
## F-statistic: 13.47 on 5 and 150 DF, p-value: 7.529e-11
Si tomamos de nuestras variable de interés a esperanza de años de vida saludable como la dependiente, y a las demás como las independientes, puede concluir que:
hipo=lm(PIB~Generosidad, data=finales)
summary(hipo)
##
## Call:
## lm(formula = PIB ~ Generosidad, data = finales)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.89300 -0.27786 0.05455 0.30668 1.20199
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.89766 0.06661 13.477 <2e-16 ***
## Generosidad -0.01958 0.32185 -0.061 0.952
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.395 on 154 degrees of freedom
## Multiple R-squared: 2.404e-05, Adjusted R-squared: -0.006469
## F-statistic: 0.003702 on 1 and 154 DF, p-value: 0.9516
Solo 2 variables tienen efecto
SIGUIENTE ANALISIS:
library(dlookr)
## Loading required package: mice
## Loading required package: lattice
##
## Attaching package: 'mice'
## The following objects are masked from 'package:base':
##
## cbind, rbind
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'car':
## method from
## influence.merMod lme4
## cooks.distance.influence.merMod lme4
## dfbeta.influence.merMod lme4
## dfbetas.influence.merMod lme4
## Warning in fun(libname, pkgname): couldn't connect to display ":0"
##
## Attaching package: 'dlookr'
## The following object is masked from 'package:base':
##
## transform
Niguna es mayor a 0.05, por ende, se va por el camino no paramétrico (spearman).
Corroborar hipótesis: H0 = No hay correlación
frog = formula(~ PIB + Generosidad)
library (ggplot2)
library(magrittr)
library(ggpubr)
GAAAA=ggscatter(finales, x = "Generosidad", y = "PIB", cor.coef = TRUE, cor.method = "spearman",
add = "reg.line", add.params = list(color = "blue", fill = "lightgray"), conf.int = TRUE)
GAAAA
Si tomamos de nuestras variable de interés a percepcion de la corrupción como la dependiente, y a las demás como las independientes, puede concluir que:
Si aumenta la generosidad en un punto, la percepcion sube en promedio 0.25 baja, no significativa, calculada mediante tecnica no parametrica
#DATA 2
link2= "https://docs.google.com/spreadsheets/d/e/2PACX-1vSdSLln0Wbf-DqOnlmnIVLR3BVUlr-IQe4PfNwe6Rw9DkgrdDiB0p-DfRx6yUqc7z5awD--PCetyDEu/pub?gid=1643178799&single=true&output=csv"
dat1=read.csv(link2, stringsAsFactors = F,na.strings = '')
str(dat1)
## 'data.frame': 195 obs. of 13 variables:
## $ regionUbigeo : int 10000 10000 10000 10000 10000 10000 10000 20000 20000 20000 ...
## $ provinciaUbigeo: int 10100 10200 10300 10400 10500 10600 10700 20100 20200 20300 ...
## $ PROVINCIA : chr "CHACHAPOYAS" "BAGUA" "BONGARA" "CONDORCANQUI" ...
## $ IDE : num 0.774 0.662 0.632 0.46 0.605 ...
## $ identidad : num 98.6 94.6 97.5 86.2 96.2 ...
## $ salud : num 25.45 14.61 9.01 8.56 12.42 ...
## $ educacion : num 91.5 79.8 76.4 52.2 74.7 ...
## $ saneamiento : num 70.3 64.5 54.8 37.7 43.3 ...
## $ electrificacion: num 84 67.9 72.2 39.5 67.4 ...
## $ poblacion : int 54783 77438 32317 51802 52185 30236 118747 161003 7974 16879 ...
## $ costa : chr "NO" "NO" "NO" "NO" ...
## $ capital : chr "SI" "NO" "NO" "NO" ...
## $ tamano : chr "Pequena" "Pequena" "Muy pequena" "Pequena" ...
names(dat1)
## [1] "regionUbigeo" "provinciaUbigeo" "PROVINCIA"
## [4] "IDE" "identidad" "salud"
## [7] "educacion" "saneamiento" "electrificacion"
## [10] "poblacion" "costa" "capital"
## [13] "tamano"
dat1=dat1[,c(5,6,7,8,9,10,11,12,13)]
str(dat1)
## 'data.frame': 195 obs. of 9 variables:
## $ identidad : num 98.6 94.6 97.5 86.2 96.2 ...
## $ salud : num 25.45 14.61 9.01 8.56 12.42 ...
## $ educacion : num 91.5 79.8 76.4 52.2 74.7 ...
## $ saneamiento : num 70.3 64.5 54.8 37.7 43.3 ...
## $ electrificacion: num 84 67.9 72.2 39.5 67.4 ...
## $ poblacion : int 54783 77438 32317 51802 52185 30236 118747 161003 7974 16879 ...
## $ costa : chr "NO" "NO" "NO" "NO" ...
## $ capital : chr "SI" "NO" "NO" "NO" ...
## $ tamano : chr "Pequena" "Pequena" "Muy pequena" "Pequena" ...
names(dat1)
## [1] "identidad" "salud" "educacion" "saneamiento"
## [5] "electrificacion" "poblacion" "costa" "capital"
## [9] "tamano"
todasControl=lm(salud~identidad+educacion+saneamiento+electrificacion+poblacion+costa+capital+tamano, data=dat1)
summary(todasControl)
##
## Call:
## lm(formula = salud ~ identidad + educacion + saneamiento + electrificacion +
## poblacion + costa + capital + tamano, data = dat1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.3402 -3.2121 -0.2517 2.6801 19.4999
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.176e+00 1.662e+01 0.251 0.8019
## identidad -2.388e-02 1.886e-01 -0.127 0.8994
## educacion 1.140e-02 4.858e-02 0.235 0.8147
## saneamiento 1.351e-01 2.594e-02 5.209 5.07e-07 ***
## electrificacion 2.556e-02 3.751e-02 0.681 0.4965
## poblacion 1.399e-06 4.946e-06 0.283 0.7776
## costaSI 2.171e+00 1.218e+00 1.783 0.0763 .
## capitalSI 7.036e+00 1.546e+00 4.551 9.73e-06 ***
## tamanoMediana -3.462e+00 2.507e+00 -1.381 0.1690
## tamanoMuy grande -1.258e+00 3.940e+01 -0.032 0.9746
## tamanoMuy pequena -3.255e-01 3.043e+00 -0.107 0.9149
## tamanoPequena -2.534e+00 2.859e+00 -0.887 0.3765
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.86 on 183 degrees of freedom
## Multiple R-squared: 0.5837, Adjusted R-squared: 0.5587
## F-statistic: 23.33 on 11 and 183 DF, p-value: < 2.2e-16
todasControl=lm(identidad~salud+educacion+saneamiento+electrificacion+poblacion+costa+capital+tamano, data=dat1)
summary(todasControl)
##
## Call:
## lm(formula = identidad ~ salud + educacion + saneamiento + electrificacion +
## poblacion + costa + capital + tamano, data = dat1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.1774 -0.8996 0.2286 1.1466 4.0155
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.530e+01 1.642e+00 51.935 < 2e-16 ***
## salud -3.668e-03 2.897e-02 -0.127 0.89938
## educacion 1.190e-01 1.689e-02 7.045 3.64e-11 ***
## saneamiento 2.971e-02 1.067e-02 2.785 0.00592 **
## electrificacion -8.407e-03 1.470e-02 -0.572 0.56819
## poblacion 9.183e-07 1.937e-06 0.474 0.63607
## costaSI 1.979e-01 4.812e-01 0.411 0.68139
## capitalSI 2.562e-01 6.391e-01 0.401 0.68898
## tamanoMediana 1.178e+00 9.838e-01 1.198 0.23259
## tamanoMuy grande -6.758e+00 1.543e+01 -0.438 0.66192
## tamanoMuy pequena 1.767e+00 1.185e+00 1.491 0.13777
## tamanoPequena 1.030e+00 1.120e+00 0.920 0.35884
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.905 on 183 degrees of freedom
## Multiple R-squared: 0.4343, Adjusted R-squared: 0.4003
## F-statistic: 12.77 on 11 and 183 DF, p-value: < 2.2e-16
Ha=formula(saneamiento ~ costa)
aggregate(Ha, data=dat1,median)
## costa saneamiento
## 1 NO 51.3358
## 2 SI 84.3393
library(ggpubr)
ggqqplot(data=dat1,x="saneamiento") + facet_grid(. ~ costa)
normalidadTest=function(x) {y =shapiro.test(x);
c(y$statistic, y$p.value)}
resultado= aggregate(Ha, dat1,
FUN = normalidadTest)
library(knitr)
shapiroTest=as.data.frame(resultado[,2])
names(shapiroTest)=c("SW_Statistic","Probabilidad")
kable(cbind(resultado[1],shapiroTest))
| costa | SW_Statistic | Probabilidad |
|---|---|---|
| NO | 0.9949525 | 0.8529651 |
| SI | 0.9580179 | 0.2422331 |
Si tomamos de nuestras variable de interés a identidad como la dependiente, y a las demás como las independientes, puede concluir que: Ninguna variable con efecto inverso tiene efecto significativo
IDE (costa: provincia esta en la costa / capital: es capital de region): PRUEBA T.TEST
t.test(Ha,dat1)
##
## Welch Two Sample t-test
##
## data: saneamiento by costa
## t = -14.131, df = 73.273, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -36.77164 -27.68210
## sample estimates:
## mean in group NO mean in group SI
## 50.97556 83.20243
normalidadTest=function(x) {y =shapiro.test(x);
c(y$statistic, y$p.value)}
resultado= aggregate(Ha, dat1,
FUN = normalidadTest)
library(knitr)
shapiroTest=as.data.frame(resultado[,2])
names(shapiroTest)=c("SW_Statistic","Probabilidad")
kable(cbind(resultado[1],shapiroTest))
| costa | SW_Statistic | Probabilidad |
|---|---|---|
| NO | 0.9949525 | 0.8529651 |
| SI | 0.9580179 | 0.2422331 |