% Setting Up R % DPI R Bootcamp Dec. 3rd to 5th, 2012 % Jared Knowles

# Getting Ready for the DPI R Bootcamp

• How to get up and running with R in no time!

# Materials

• RStudio
• We'll also want to install a few basic packages. This can be done on the fly later, but a few include ggplot2;knitr;plyr

# Step 1: Install R

• Simply run the installer. If you have a 64-bit machine, choose 64 bit R, if you don't know, just choose the default
• The install should be less than 100mb
• There is a short video on the next slide for help installing on Windows machines

# Installing R on a Mac

• Get the install file from CRAN
• Get RStudio
• Run the R installer, and then run the RStudio installer

# Step 2: RStudio

• Now run the RStudio installer
• RStudio will automatically find your R installation

# Step 3: Install packages

First, watch this tutorial

# Install Packages

• Are you running RStudio in administrator mode on Windows Vista or Windows 7?
• If not, you need to be to install packages (in most cases)
• When you launch RStudio, right click on the icon for RStudio and then click “Run as Administrator”
• If you are on a Mac or Windows XP you can disregard this

# Install Packages (2)

• Now, copy and paste the code below into the bottom left window in RStudio (the R terminal):
install_new<-function(mypkg){
if (mypkg %in% installed.packages()) cat("Package already installed")
Press CTRL C to abort.")
Sys.sleep(5)
install.packages(mypkg,repos="http://cran.wustl.edu/")
}
}

install_new('plyr')
install_new('lmtest')
install_new('ggplot2')
install_new('gridExtra')
install_new('eeptools')
install_new('stringr')
install_new('knitr')
install_new('quantreg')
install_new('xtable')
install_new('lme4')
install_new('caret')



# Alternate Install

• If you want or if you have troubles with the above, you can overwrite any existing versions of those packages and do the install in one line, shown below:
install.packages(c('plyr','lmtest','ggplot2','gridExtra','stringr',
'knitr','quantreg','xtable','lme4','eeptools','caret'),
repos="http://cran.wustl.edu/")


# Run RStudio

• You should be able to run RStudio now and load any of the packages above.
• Run the code below in the RStudio terminal panel (bottom left) to test this:
library(lmtest)
example(gqtest)


# And you should see…

##
## gqtest> ## generate a regressor
## gqtest> x <- rep(c(-1,1), 50)
##
## gqtest> ## generate heteroskedastic and homoskedastic disturbances
## gqtest> err1 <- c(rnorm(50, sd=1), rnorm(50, sd=2))
##
## gqtest> err2 <- rnorm(100)
##
## gqtest> ## generate a linear relationship
## gqtest> y1 <- 1 + x + err1
##
## gqtest> y2 <- 1 + x + err2
##
## gqtest> ## perform Goldfeld-Quandt test
## gqtest> gqtest(y1 ~ x)
##
##  Goldfeld-Quandt test
##
## data:  y1 ~ x
## GQ = 2.542, df1 = 48, df2 = 48, p-value = 0.0007882
##
##
## gqtest> gqtest(y2 ~ x)
##
##  Goldfeld-Quandt test
##
## data:  y2 ~ x
## GQ = 0.7892, df1 = 48, df2 = 48, p-value = 0.7924


# And test the graphics…

Type in in the terminal (or copy and paste)

library(ggplot2)
y <- rt(200, df = 5)
qplot(sample = y, stat = "qq")


# Results


library(ggplot2)
y <- rt(200, df = 5)
qplot(sample = y, stat = "qq")


# That's all

• Did you see the output like the slides above?
• Did you see the plot in the lower right?

# Optional

• If you want to see the cutting edge of R development–interactive web-based data applications, you can install the following beta software
options(repos = c(RStudio = "http://rstudio.org/_packages", getOption("repos")))
install.packages("shiny")


# Optional Cont.

• To check if it worked (and it might not depending on your machine's configuration)
library(shiny)
runExample("06_tabsets")

• Does a new browser window or tab open up?
• In RStudio hit the “stop” button in the terminal panel to exit the demo

# You are ready to go!

See you on December 3rd to the 5th to find out how to go from these basic steps to using R to learn from your data.

Can't wait to see you in Madison!