library(tidyverse)
library(lubridate)
# 1. Base: promedio de las encuestas ####
encuestas_2018 <- readr::read_csv("https://raw.githubusercontent.com/nelsonamayad/Elecciones-presidenciales-2018/master/Elecciones%202018/encuestas2018.csv")
# 1.2 Sancocho ####
encuestas_sancocho_2018 <- encuestas_2018 %>%
dplyr::filter(between(as_date(fecha),as_date("2018-03-01"),as_date("2018-05-19"))) %>%
dplyr::select(ivan_duque,gustavo_petro,sergio_fajardo,german_vargas_lleras,humberto_delacalle,fecha) %>%
tidyr::pivot_longer(cols = c("ivan_duque","gustavo_petro","sergio_fajardo","german_vargas_lleras","humberto_delacalle"),names_to = "candidato",values_to="int_voto") %>%
dplyr::group_by(candidato) %>%
dplyr::summarize(int_voto=mean(int_voto)) %>%
dplyr::mutate(fecha = "2018-05-19",
int_voto=round(int_voto,digits = 0),
candidato = case_when(candidato=="ivan_duque" ~ "Ivan Duque",
candidato=="gustavo_petro" ~ "Gustavo Petro",
candidato=="sergio_fajardo" ~ "Sergio Fajardo",
candidato=="german_vargas_lleras" ~ "German Vargas Lleras",
candidato=="humberto_delacalle" ~ "Humberto de la Calle"),
fuente = "18 Encuestas")
# 2. Cifras y Conceptos ####
cyc <- tribble(~fecha, ~candidato, ~int_voto_max,~int_voto_min,
"2018-04-04","Ivan Duque",37.7,37.2,
"2018-04-04","Gustavo Petro",17.4,15.7,
"2018-04-04","Sergio Fajardo",13.3,12.8,
"2018-04-04","German Vargas Lleras",21.0,18.2,
"2018-04-04","Humberto de la Calle",6.3,5.9,
"2018-04-18","Ivan Duque",36.4,35.6,
"2018-04-18","Gustavo Petro",18.9,17,
"2018-04-18","Sergio Fajardo",12.4,12.3,
"2018-04-18","German Vargas Lleras",23.8,20.9,
"2018-04-18","Humberto de la Calle",5.1,4.8,
"2018-04-18","Ivan Duque",36.9,33.7,
"2018-04-18","Gustavo Petro",21.6,20.8,
"2018-04-18","Sergio Fajardo",14.2,11.3,
"2018-04-18","German Vargas Lleras",24.1,20.8,
"2018-04-18","Humberto de la Calle",5.1,3.3,
"2018-05-16","Ivan Duque",36.9,32.9,
"2018-05-16","Gustavo Petro",19.9,16.8,
"2018-05-16","Sergio Fajardo",15.5,12.6,
"2018-05-16","German Vargas Lleras",23.5,20.3,
"2018-05-16","Humberto de la Calle",5,3.4,
"2018-05-20","Ivan Duque",37.2,33.7,
"2018-05-20","Gustavo Petro",20.8,18.1,
"2018-05-20","Sergio Fajardo",17.3,14.6,
"2018-05-20","German Vargas Lleras",21.7,19,
"2018-05-20","Humberto de la Calle",4.7,3.5
) %>%
dplyr::mutate(int_voto=(int_voto_max+int_voto_min)*0.5,
fuente = "Cifras y Conceptos")
# 3. El País ####
elpais <- tribble(~fecha,~candidato,~int_voto,
"2018-05-19","Sergio Fajardo",15.8,
"2018-05-19","Gustavo Petro",27,
"2018-05-19","Ivan Duque",37,
"2018-05-19","Humberto de la Calle",3,
"2018-05-19","German Vargas Lleras",10.5) %>%
dplyr::mutate(fuente= "El Pais")
# 4. ANIF ####
anif <- tribble(~fecha,~candidato,~int_voto,
"2018-05-07","Ivan Duque",(5/17)*100,
"2018-05-07","German Vargas Lleras",(4.5/17)*100,
"2018-05-07","Gustavo Petro",(3.2/17)*100,
"2018-05-07","Sergio Fajardo",(2.3/17)*100,
"2018-05-07","Humberto de la Calle",(1/17)*100) %>%
dplyr::mutate(fuente= "ANIF")
# 5. Recetas propias ####
recetas_2018 <- readr::read_csv("simple-2018/simple_2018_resultados.csv") %>%
dplyr::group_by(nombre) %>%
dplyr::summarise(m_all = median(value),
p10 = quantile(value,0.1),
p90 = quantile(value,0.9)) %>%
dplyr::mutate(fuente="Simple") %>%
dplyr::select(fuente,candidato=nombre,int_voto=m_all,int_voto_min=p10,int_voto_max=p90) %>%
dplyr::mutate(candidato=factor(candidato,levels=c("Humberto de la Calle","German Vargas Lleras","Sergio Fajardo","Gustavo Petro","Ivan Duque"))) %>%
dplyr::bind_rows(
readr::read_csv("mixto-2018/mixto_2018_resultados.csv") %>%
dplyr::group_by(nombre) %>%
dplyr::summarise(m_all = median(value),
p10 = quantile(value,0.1),
p90 = quantile(value,0.9)) %>%
dplyr::mutate(fuente="Mixto") %>%
dplyr::select(fuente,candidato=nombre,int_voto=m_all,int_voto_min=p10,int_voto_max=p90) %>%
dplyr::mutate(candidato=factor(candidato,levels=c("Humberto de la Calle","German Vargas Lleras","Sergio Fajardo","Gustavo Petro","Ivan Duque")))
) %>%
dplyr::mutate(fecha="2018-05-20")
# 6. Resultado 1era vuelta ####
resultado_1era_vuelta_2018 <- tribble(~fecha,~candidato,~int_voto,
"2018-05-27","Ivan Duque",39.14,
"2018-05-27","German Vargas Lleras",7.28,
"2018-05-27","Gustavo Petro",25.08,
"2018-05-27","Sergio Fajardo",23.73,
"2018-05-27","Humberto de la Calle",2.06) %>%
dplyr::mutate(fuente = "Resultado")
# 7. Preparar ####
sancocho_2018 <- dplyr::bind_rows(encuestas_sancocho_2018,
recetas_2018,
anif,
elpais,
resultado_1era_vuelta_2018,
cyc %>%
dplyr::filter(fecha==lubridate::as_date("2018-05-20"))) %>%
dplyr::mutate(candidato=factor(candidato,levels=c("Humberto de la Calle","German Vargas Lleras","Sergio Fajardo","Gustavo Petro","Ivan Duque")))
sancocho_2018 %>%
readr::write_csv("sancocho_2018.csv")