Setting up R

Why R

  • Free and open source (think of science in developing countries)
  • Good online-documentation (much better than some commercial software such as Mplus)
  • Lively community of users (forums etc.)
  • Visualization capabilities (ggplot …)
  • Cooperates with other programs and programming language (e.g. Python, )
  • Popularity (See popularity statistics on books, blogs, forums)
  • RStudio as powerful integrated development environment (IDE) for R
  • Evolves into a scientific work suite optimizing workflow (replication, reproducability etc.)
  • Institutions/people (Gary King, Andrew Gelman etc.)
  • Economic power (Revolution Analytics, Microsoft R Open)

Where to study it

Install R on your Machine

Below some notes on the installation and setup of R and relevant packages on your own computer:

  • Install Rtools for Windows machines from CRAN (https://cran.r-project.org/bin/windows/Rtools/).
  • If you are using OS X, you will need to to install XCode, available for free from the App Store. This will install a compiler (if you don’t have a compiler installed) which will be needed when installing packages from GitHub that require compilation from C++ source code.
  • Install the latest version of R from CRAN (https://cran.r-project.org/).
  • Install the latest version of RStudio (https://www.rstudio.com/products/RStudio/). RStudio is the editor we’ll rely on, i.e. we’ll write code in RStudio which is subsequently sent to and run within R.
  • Install the latest versions of various packages that we need. You can also update your packages by running “update.packages(ask = FALSE)” in the R commandline.

Set up the Environment on your local machine

  • Once R-Studio is installed copy and paste the following code
  • Be sure everything run smoothly
  • Try to familiarize with R if needed
  • If you are not able to figure it out you can create an account on https://rstudio.cloud/
    • It is R in the cloud (Virtual Machine)
    • It usually resolves most of the incompatibilities
  • If you are still not able to run R and R Studio
    • We will figure it out in class ;)
# A function to install the required packages
# you don't need to understand what it does
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE,repos = c(
  CRAN = 'https://cran.rstudio.com'
  #CRANextra = 'https://macos.rbind.io'
)
      )
  sapply(pkg, require, character.only = TRUE)
}


packages_needed <- c("foreign", 
                     "readr",
                     "plyr",
                     "dplyr",
                     "tidyr",
                     "tibble", 
                     "knitr",
                     "printr",
                     "stargazer",
                     "ggplot2",
                     "MatchIt",
                     "rmarkdown",
                     "repmis")

ipak(packages_needed)
##   foreign     readr      plyr     dplyr     tidyr    tibble     knitr    printr 
##      TRUE      TRUE      TRUE      TRUE      TRUE      TRUE      TRUE      TRUE 
## stargazer   ggplot2   MatchIt rmarkdown    repmis 
##      TRUE      TRUE      TRUE      TRUE      TRUE
  # Data import
    library(foreign)
    library(haven)
    library(readr)

  # Data management
    library(plyr)
    library(dplyr)
    library(tidyr)
    library(tibble)
    library(reshape2)
    library(tidyselect)
    library(knitr)
    library(repmis)
  # Visualization 
    library(ggplot2)
    library(stargazer)
  # Report 
    library(report)
  # Matching 
    library(MatchIt)