Limpiar Environment

rm(list=ls())

Habilitar Packages

library(data.table)
library(ggplot2)
library(ggvis)
library(plotly)
library(dplyr)
library(AER)
library(scales)
library(factoextra)
library(dbscan)

Crear un Path a las BBDDs

path <- "/Users/josecastex/Desktop/Sem 7/Data Science/Trabajo Final/"

Abrir BBDDs en R Studio formato data.table

Base_Fert <- data.table(readxl::read_xlsx(paste0(path,"Base_Fert.xlsx")))
Base_Alfa <- data.table(readxl::read_xlsx(paste0(path,"Base_Alfa.xlsx")))
Base_Mundo <- data.table(readxl::read_xlsx(paste0(path,"Base_Mundo.xlsx")))
Base_Cluster <- data.table(readxl::read_xlsx(paste0(path,"BBDD.xlsx")))

Crear gráficos para Tasa de Fertilidad (Nacimientos por cada 1000 mujeres entre 15 y 19 años)

ggplot(Base_Fert, aes(y=WLD, x=Year) ) + geom_point(color="blue3")+ theme_minimal() + labs(title="Tasa de Fertilidad en adolescentes Mundo", subtitle = "Nacimientos por cada 1000 mujeres entre 15 y 19 años", x="Años", y="Tasa de Fertilidad")+ geom_line(color="blue")

ggplot(Base_Fert, aes(y=Africa_al_sur_del_Sahara, x=Year) ) + geom_point(color="red3")+ theme_minimal() + labs(title="Tasa de Fertilidad en adolescentes Africa al Sur del Sahara", subtitle = "Nacimientos por cada 1000 mujeres entre 15 y 19 años", x="Años", y="Tasa de Fertilidad")+ geom_line(color="red")

ggplot(Base_Fert, aes(y=Asia_meridional, x=Year) ) + geom_point(color="orange3")+ theme_minimal() + labs(title="Tasa de Fertilidad en adolescentes Asia Meridional", subtitle = "Nacimientos por cada 1000 mujeres entre 15 y 19 años", x="Años", y="Tasa de Fertilidad")+ geom_line(color="orange")

ggplot(Base_Fert, aes(y=Oriente_Medio_y_Norte_de_Africa, x=Year) ) + geom_point(color="green3")+ theme_minimal() + labs(title="Tasa de Fertilidad en adolescentes Medio Oriente y Norte de África", subtitle = "Nacimientos por cada 1000 mujeres entre 15 y 19 años", x="Años", y="Tasa de Fertilidad")+ geom_line(color="green")

ggplot(Base_Fert, aes(y=America_Latina_y_el_Caribe, x=Year) ) + geom_point(color="yellow3")+ theme_minimal() + labs(title="Tasa de Fertilidad en adolescentes América Latina y el Caribe", subtitle = "Nacimientos por cada 1000 mujeres entre 15 y 19 años", x="Años", y="Tasa de Fertilidad")+ geom_line(color="yellow")

ggplot(Base_Fert, aes(y=Europa_y_Asia_central, x=Year) ) + geom_point(color="pink3")+ theme_minimal() + labs(title="Tasa de Fertilidad en adolescentes Europa y Asia central", subtitle = "Nacimientos por cada 1000 mujeres entre 15 y 19 años", x="Años", y="Tasa de Fertilidad")+ geom_line(color="pink")

ggplot(Base_Fert, aes(y=Asia_oriental_y_el_Pacifico, x=Year) ) + geom_point(color="purple3")+ theme_minimal() + labs(title="Tasa de Fertilidad en adolescentes Asia oriental y el Pacífico", subtitle = "Nacimientos por cada 1000 mujeres entre 15 y 19 años", x="Años", y="Tasa de Fertilidad")+ geom_line(color="purple")

ggplot(Base_Fert, aes(y=America_del_Norte, x=Year)) + geom_point()+ theme_minimal() + labs(title="Tasa de Fertilidad en adolescentes América del Norte", subtitle = "Nacimientos por cada 1000 mujeres entre 15 y 19 años", x="Años", y="Tasa de Fertilidad") + geom_line(color="gray")

Obtener porcentaje de analfbetismo creando nueva variable (100-X)

Base_Alfa[,Analfabetismo_Asia_meridional:=100-Asia_meridional]
Base_Alfa[,Analfabetismo_Europa_y_Asia_central:=100-Europa_y_Asia_central]
Base_Alfa[,Analfabetismo_America_Latina_y_el_Caribe:=100-America_Latina_y_el_Caribe]
Base_Alfa[,Analfabetismo_Oriente_Medio_y_Norte_de_Africa:=100-Oriente_Medio_y_Norte_de_Africa]
Base_Alfa[,Analfabetismo_Asia_oriental_y_el_Pacifico:=100-Asia_oriental_y_el_Pacifico]
Base_Alfa[,Analfabetismo_Africa_al_sur_del_Sahara:=100-Africa_al_sur_del_Sahara]
Base_Alfa[,Analfabetismo_Mundo:=100-Mundo]

Crear gráficos para Analfabetismo (Jóvenes entre 15 y 24 de años)

ggplot(Base_Alfa, aes(y=Analfabetismo_Mundo, x=Year) ) + geom_point(color="blue3")+ theme_minimal() + labs(title="Tasa de Analfabetismo Mundo", subtitle = "Porcentaje en Jóvenes entre 15 y 24 años", x="Años", y="Tasa de Analfabetismo")+ geom_line(color="blue")

ggplot(Base_Alfa, aes(y=Analfabetismo_Africa_al_sur_del_Sahara, x=Year) ) + geom_point(color="red3")+ theme_minimal() + labs(title="Tasa de Analfabetismo África al sur del Sahara", subtitle = "Porcentaje en Jóvenes entre 15 y 24 años", x="Años", y="Tasa de Analfabetismo")+ geom_line(color="red")

ggplot(Base_Alfa, aes(y=Analfabetismo_Asia_meridional, x=Year) ) + geom_point(color="orange3")+ theme_minimal() + labs(title="Tasa de Analfabetismo Asia meridional", subtitle = "Porcentaje en Jóvenes entre 15 y 24 años", x="Años", y="Tasa de Analfabetismo")+ geom_line(color="orange")

ggplot(Base_Alfa, aes(y=Analfabetismo_Oriente_Medio_y_Norte_de_Africa, x=Year) ) + geom_point(color="green3")+ theme_minimal() + labs(title="Tasa de Analfabetismo Oriente Medio y Norte de África", subtitle = "Porcentaje en Jóvenes entre 15 y 24 años", x="Años", y="Tasa de Analfabetismo") + geom_line(color="green")

ggplot(Base_Alfa, aes(y=Analfabetismo_America_Latina_y_el_Caribe, x=Year) ) + geom_point(color="yellow3")+ theme_minimal() + labs(title="Tasa de Analfabetismo América Latina y el Caribe", subtitle = "Porcentaje en Jóvenes entre 15 y 24 años", x="Años", y="Tasa de Analfabetismo") + geom_line(color="yellow")

ggplot(Base_Alfa, aes(y=Analfabetismo_Europa_y_Asia_central, x=Year) ) + geom_point(color="pink3")+ theme_minimal() + labs(title="Tasa de Analfabetismo Europa y Asia_central", subtitle = "Porcentaje en Jóvenes entre 15 y 24 años", x="Años", y="Tasa de Analfabetismo") + geom_line(color="pink")

ggplot(Base_Alfa, aes(y=Analfabetismo_Asia_oriental_y_el_Pacifico, x=Year) ) + geom_point(color="purple3")+ theme_minimal() + labs(title="Tasa de Analfabetismo Asia oriental y el Pacífico", subtitle = "Porcentaje en Jóvenes entre 15 y 24 años", x="Años", y="Tasa de Analfabetismo") + geom_line(color="purple")

Obtener porcentaje de analfbetismo creando nueva variable en Base_Cluster, para luego crear Clusters para mostrar la correlación entre Analfabetismo y Embarazo Adolescente

Base_Cluster[,Analfabetismo_Asia_meridional:=100-Asia_meridional.A]
Base_Cluster[,Analfabetismo_Europa_y_Asia_central:=100-Europa_y_Asia_central.A]
Base_Cluster[,Analfabetismo_America_Latina_y_el_Caribe:=100-America_Latina_y_el_Caribe.A]
Base_Cluster[,Analfabetismo_Oriente_Medio_y_Norte_de_Africa:=100-Oriente_Medio_y_Norte_de_Africa.A]
Base_Cluster[,Analfabetismo_Asia_oriental_y_el_Pacifico:=100-Asia_oriental_y_el_Pacifico.A]
Base_Cluster[,Analfabetismo_Africa_al_sur_del_Sahara:=100-Africa_al_sur_del_Sahara.A]
Base_Cluster[,Analfabetismo_Mundo:=100-Mundo.A]

Crear bases más pequeñas con variable de analfabetismo y con nacimientos por cada 1000 adolescentes

BBDD_Cluster_Mundo<-Base_Cluster[,.(WLD.F,Analfabetismo_Mundo)]
BBDD_Cluster_Asia_oriental_y_el_Pacifico<-Base_Cluster[,.(Asia_oriental_y_el_Pacifico.F,Analfabetismo_Asia_oriental_y_el_Pacifico)]
BBDD_Cluster_Asia_meridional<-Base_Cluster[,.(Asia_meridional.F,Analfabetismo_Asia_meridional)]
BBDD_Cluster_Europa_y_Asia_central<-Base_Cluster[,.(Europa_y_Asia_central.F,Analfabetismo_Europa_y_Asia_central)]
BBDD_Cluster_America_Latina_y_el_Caribe<-Base_Cluster[,.(America_Latina_y_el_Caribe.F,Analfabetismo_America_Latina_y_el_Caribe)]
BBDD_Cluster_Oriente_Medio_y_Norte_de_Africa<-Base_Cluster[,.(Oriente_Medio_y_Norte_de_Africa.F,Analfabetismo_Oriente_Medio_y_Norte_de_Africa)]
BBDD_Cluster_Africa_al_sur_del_Sahara<-Base_Cluster[,.(Africa_al_sur_del_Sahara.F,Analfabetismo_Africa_al_sur_del_Sahara)]

Eliminar filas con N.A de las BBDD para poder realizar Cluster

BBDD_Cluster_Mundo <-BBDD_Cluster_Mundo[!is.na(BBDD_Cluster_Mundo[,Analfabetismo_Mundo]),]
BBDD_Cluster_Asia_oriental_y_el_Pacifico <-BBDD_Cluster_Asia_oriental_y_el_Pacifico[!is.na(BBDD_Cluster_Asia_oriental_y_el_Pacifico[,Analfabetismo_Asia_oriental_y_el_Pacifico]),]
BBDD_Cluster_Asia_meridional <-BBDD_Cluster_Asia_meridional[!is.na(BBDD_Cluster_Asia_meridional[,Analfabetismo_Asia_meridional]),]
BBDD_Cluster_Europa_y_Asia_central <-BBDD_Cluster_Europa_y_Asia_central[!is.na(BBDD_Cluster_Europa_y_Asia_central[,Analfabetismo_Europa_y_Asia_central]),]
BBDD_Cluster_America_Latina_y_el_Caribe <-BBDD_Cluster_America_Latina_y_el_Caribe[!is.na(BBDD_Cluster_America_Latina_y_el_Caribe[,Analfabetismo_America_Latina_y_el_Caribe]),]
BBDD_Cluster_Oriente_Medio_y_Norte_de_Africa <-BBDD_Cluster_Oriente_Medio_y_Norte_de_Africa[!is.na(BBDD_Cluster_Oriente_Medio_y_Norte_de_Africa[,Analfabetismo_Oriente_Medio_y_Norte_de_Africa]),]
BBDD_Cluster_Africa_al_sur_del_Sahara <-BBDD_Cluster_Africa_al_sur_del_Sahara[!is.na(BBDD_Cluster_Africa_al_sur_del_Sahara[,Analfabetismo_Africa_al_sur_del_Sahara]),]

Clusters

k_BBDD_Cluster_Mundo<-kmeans(x=BBDD_Cluster_Mundo,centers=1,nstart=5)
fviz_cluster(k_BBDD_Cluster_Mundo,data=BBDD_Cluster_Mundo,geom = "point", fill="blue", colour="blue")+ theme_minimal() + labs(title="Cluster Mundial", subtitle = "Relación entre Analfabetismo y Embarazo Juenil", x="Fertilidad", y="Analfabetismo")+ geom_line(color="blue")

k_BBDD_Cluster_Asia_oriental_y_el_Pacifico<-kmeans(x=BBDD_Cluster_Asia_oriental_y_el_Pacifico,centers=1,nstart=5)
fviz_cluster(k_BBDD_Cluster_Asia_oriental_y_el_Pacifico,data=BBDD_Cluster_Asia_oriental_y_el_Pacifico,geom = "point", fill="purple", colour="purple")+ theme_minimal() + labs(title="Cluster Asia Oriental y el Pacífico", subtitle = "Relación entre Analfabetismo y Embarazo Juenil", x="Fertilidad", y="Analfabetismo")+ geom_line(color="purple")

k_BBDD_Cluster_Asia_meridional<-kmeans(x=BBDD_Cluster_Asia_meridional,centers=1,nstart=5)
fviz_cluster(k_BBDD_Cluster_Asia_meridional,data=BBDD_Cluster_Asia_meridional,geom = "point", fill="orange", colour="orange")+ theme_minimal() + labs(title="Cluster Asia Meridonial", subtitle = "Relación entre Analfabetismo y Embarazo Juenil", x="Fertilidad", y="Analfabetismo")+ geom_line(color="orange")

k_BBDD_Cluster_Europa_y_Asia_central<-kmeans(x=BBDD_Cluster_Europa_y_Asia_central,centers=1,nstart=5)
fviz_cluster(k_BBDD_Cluster_Europa_y_Asia_central,data=BBDD_Cluster_Europa_y_Asia_central,geom = "point", fill="pink2", colour="pink")+ theme_minimal() + labs(title="Cluster Europa y Asia Central", subtitle = "Relación entre Analfabetismo y Embarazo Juenil", x="Fertilidad", y="Analfabetismo")+ geom_line(color="pink")

k_BBDD_Cluster_America_Latina_y_el_Caribe<-kmeans(x=BBDD_Cluster_America_Latina_y_el_Caribe,centers=1,nstart=5)
fviz_cluster(k_BBDD_Cluster_America_Latina_y_el_Caribe,data=BBDD_Cluster_America_Latina_y_el_Caribe,geom = "point", fill="yellow", colour="yellow3")+ theme_minimal()+ labs(title="Cluster América Latina y el Caribe", subtitle = "Relación entre Analfabetismo y Embarazo Juenil", x="Fertilidad", y="Analfabetismo")+ geom_line(color="yellow")

k_BBDD_Cluster_Oriente_Medio_y_Norte_de_Africa<-kmeans(x=BBDD_Cluster_Oriente_Medio_y_Norte_de_Africa,centers=1,nstart=5)
fviz_cluster(k_BBDD_Cluster_Oriente_Medio_y_Norte_de_Africa,data=BBDD_Cluster_Oriente_Medio_y_Norte_de_Africa,geom = "point", fill="green", colour="green")+ theme_minimal() + labs(title="Cluster Oriente Medio y Norte de África", subtitle = "Relación entre Analfabetismo y Embarazo Juenil", x="Fertilidad", y="Analfabetismo")+ geom_line(color="green")

k_BBDD_Cluster_Africa_al_sur_del_Sahara<-kmeans(x=BBDD_Cluster_Africa_al_sur_del_Sahara,centers=1,nstart=5)
fviz_cluster(k_BBDD_Cluster_Africa_al_sur_del_Sahara,data=BBDD_Cluster_Africa_al_sur_del_Sahara,geom = "point", fill="red", colour="red")+ theme_minimal() + labs(title="Cluster África al sur del Sahara", subtitle = "Relación entre Analfabetismo y Embarazo Juenil", x="Fertilidad", y="Analfabetismo")+ geom_line(color="red")