class: center, middle, inverse, title-slide # Herramientas Cuantitativas para el Análisis Político ## Maestría en Ciencia Política [CP44] ### ### Universidad Torcuato Di Tella ### 21/09/2021 --- layout: true <div class="my-footer"><span>Juan Pablo Ruiz Nicolini | @TuQmano | <a href="https://tuqmano.ar/">www.tuqmano.ar</a></span></div> --- class: center, middle # Sesión II ### Intro a `R` --- class: left # Hoja de Ruta + _Dialectos_: `base` VS _Tidyverse_ -- + Organización: proyectos, `{here}`, *.git* -- + Reproducibilidad y Comunicación (`.R` y `.Rmd`) --- class: center, middle background-image: url(https://github.com/rstudio/hex-stickers/raw/master/PNG/tidyverse.png) background-position: 95% 5% background-size: 10% # Dialéctos para `R` # **R** `base` vs _Tidyverse_ -- ```r curso <- data.frame(nombre= c("Juan", "Pedro", "María", "José", "Enzo", "Ariel", "Eva"), edad= c(25, 32, 21,40, 30, 28, 37), nacim= c(1993, 1986, 1997, 1978, 1988, 1990, 1981), software.primario= c("spss", "stata", "stata", "excel", "R", "stata", "spss"), nivel= c(3, 5,7, 6, 2, 6, 8) ) ``` --- background-image: url(https://github.com/rstudio/hex-stickers/raw/master/PNG/tidyverse.png) background-position: 95% 5% background-size: 10% # **R** `base` vs _Tidyverse_ ## Edad promedio del curso > **`base`** -- ```r # BASE # sum(curso$edad)/length(curso$edad) ## [1] 30.42857 ``` --- background-image: url(https://github.com/rstudio/hex-stickers/raw/master/PNG/tidyverse.png) background-position: 95% 5% background-size: 10% # **R** `base` vs _Tidyverse_ ## Edad promedio del curso > _**Tidy**_ -- ```r # Tidy / {dplyr} library(tidyverse) # cargo paqueteS curso %>% summarise(promedio = sum(edad)/length(edad)) ## promedio ## 1 30.42857 ``` --- background-image: url(https://github.com/politicaargentina/data_warehouse/blob/master/hex/electorAr.png?raw=true) background-position: 95% 5% background-size: 10% # Elección Presidencial 2015: ### ¿Parámetros de la elección? ```r library(electorAr) show_available_elections(source = "data") ## # A tibble: 426 x 5 ## district category round year NOMBRE ## <chr> <chr> <chr> <chr> <chr> ## 1 arg presi balota 2015 ARGENTINA ## 2 arg presi gral 2003 ARGENTINA ## 3 arg presi gral 2007 ARGENTINA ## 4 arg presi gral 2011 ARGENTINA *## 5 arg presi gral 2015 ARGENTINA ## 6 arg presi gral 2019 ARGENTINA ## 7 arg presi paso 2011 ARGENTINA ## 8 arg presi paso 2015 ARGENTINA ## 9 arg presi paso 2019 ARGENTINA ## 10 caba dip gral 2005 CABA ## # ... with 416 more rows ``` --- background-image: url(https://github.com/politicaargentina/data_warehouse/blob/master/hex/electorAr.png?raw=true) background-position: 95% 5% background-size: 10% ## Descarga resultados ### Presidente 2015 ```r gral2015 <- get_election_data(district = 'arg', category = 'presi', round = 'gral', year = 2015, level = "departamento") %>% get_names() gral2015 ## # A tibble: 4,216 x 11 ## # Groups: codprov, depto, coddepto [527] ## category round year codprov name_prov depto coddepto electores listas votos ## <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> ## 1 presi gral 2015 01 CABA Comun~ 001 179292 0131 33224 ## 2 presi gral 2015 01 CABA Comun~ 001 179292 0132 5032 ## 3 presi gral 2015 01 CABA Comun~ 001 179292 0133 944 ## 4 presi gral 2015 01 CABA Comun~ 001 179292 0135 64042 ## 5 presi gral 2015 01 CABA Comun~ 001 179292 0137 5362 ## 6 presi gral 2015 01 CABA Comun~ 001 179292 0138 17227 ## 7 presi gral 2015 01 CABA Comun~ 001 179292 blanc~ 1330 ## 8 presi gral 2015 01 CABA Comun~ 001 179292 nulos 782 ## 9 presi gral 2015 01 CABA Comun~ 002 145810 0131 15324 ## 10 presi gral 2015 01 CABA Comun~ 002 145810 0132 4862 ## # ... with 4,206 more rows, and 1 more variable: nombre_lista <chr> ``` --- background-image: url(https://github.com/politicaargentina/data_warehouse/blob/master/hex/electorAr.png?raw=true) background-position: 95% 5% background-size: 10% ### Dónde cosechó más votos el FIT? #### Departamentos con votos `max()` -- ```r fit <- gral2015[gral2015$nombre_lista == 'ALIANZA FRENTE DE IZQUIERDA Y DE LOS TRABAJADORES', ] # > BASE fit[fit$votos == max(fit$votos),] ## # A tibble: 1 x 11 ## # Groups: codprov, depto, coddepto [1] ## category round year codprov name_prov depto coddepto electores listas votos ## <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> ## 1 presi gral 2015 04 CORDOBA Capital 001 1077907 0137 37769 ## # ... with 1 more variable: nombre_lista <chr> ``` -- ```r gral2015 %>% # > TIDY filter(nombre_lista == 'ALIANZA FRENTE DE IZQUIERDA Y DE LOS TRABAJADORES') %>% ungroup() %>% slice_max(votos) ## # A tibble: 1 x 11 ## category round year codprov name_prov depto coddepto electores listas votos ## <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> ## 1 presi gral 2015 04 CORDOBA Capital 001 1077907 0137 37769 ## # ... with 1 more variable: nombre_lista <chr> ``` --- background-image: url(https://github.com/rstudio/hex-stickers/raw/master/PNG/tidyverse.png) background-position: 95% 5% background-size: 10% #
vs
<img src="https://github.com/TuQmano/MetodosCiPol/raw/master/docs/2020/Clase02/fig/computer_human.png" width="200%" /> --- background-image: url(https://github.com/rstudio/hex-stickers/raw/master/PNG/tidyverse.png) background-position: 95% 5% background-size: 10% # Tidyverse 1. Reutilizar estructuras de datos existentes 2. Armar funciones simples con la _pipa_ (`%<%`) 3. Adoptar la programación funcional 4. Diseñado para humanos! [
The Manifesto](https://tidyverse.tidyverse.org/articles/manifesto.html) --- background-image: url(https://github.com/rstudio/hex-stickers/raw/master/PNG/tidyverse.png) background-position: 95% 5% background-size: 10% ## El nucleo _Tidyverse_ <img src="https://github.com/TuQmano/MetodosCiPol/blob/master/docs/2020/Clase02/fig/tidy_hex.PNG?raw=true" width="60%" /> Fuente: [https://evamaerey.github.io/little_flipbooks_library/ggtextures/ggtextures#1](https://evamaerey.github.io/little_flipbooks_library/ggtextures/ggtextures#1) -- ```r # Install from CRAN install.packages("tidyverse") ``` ### [
The Tidyverse](https://tidyverse.tidyverse.org) --- class: center, middle, inverse background-image: url(https://d33wubrfki0l68.cloudfront.net/521a038ed009b97bf73eb0a653b1cb7e66645231/8e3fd/assets/img/rstudio-icon.png) background-position: 95% 5% background-size: 10% ## Organización de Proyectos (I) ### `.Rproj` <img src="http://www.rstudio.com/images/docs/projects_new.png" width="60%" /> #### [
Using Projects](https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects) --- class: center, middle, inverse background-image: url(https://d33wubrfki0l68.cloudfront.net/521a038ed009b97bf73eb0a653b1cb7e66645231/8e3fd/assets/img/rstudio-icon.png) background-position: 95% 5% background-size: 10% <img src="https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/here.png" width="60%" /> #### [
{here}](https://here.r-lib.org/index.html) ## Organización de Proyectos (II) --- class: center, middle, inverse ## Organización de Proyectos (III) ## `.git` --- # `.git ` ![](https://yabellini.netlify.app/img/git_concept_map.png)<!-- --> **Fuente**: https://yabellini.netlify.app/es/post/githubconr/ --- # `.git ` #### varbos + usados * git status -- * git pull -- * git add <file> / git add -A (para agregar todos) -- * git commit -m 'agregar mensaje' -- * git push -- #### [
Happy Git](https://happygitwithr.com/) --- class: inverse, center, middle background-image: url(https://d33wubrfki0l68.cloudfront.net/521a038ed009b97bf73eb0a653b1cb7e66645231/8e3fd/assets/img/rstudio-icon.png) background-position: 95% 5% background-size: 10% # `script.R` vs `script.Rmd` --- background-image: url(https://d33wubrfki0l68.cloudfront.net/521a038ed009b97bf73eb0a653b1cb7e66645231/8e3fd/assets/img/rstudio-icon.png) background-position: 95% 5% background-size: 10% # R Markdown <img src="https://d33wubrfki0l68.cloudfront.net/3215c7166555d2ac02ef678fd025c171f90db23c/4e60a/images/bandone.png" width="60%" /> ### [
Comunicación](https://rmarkdown.rstudio.com/)