In BTRY 6020 all homework will be submitted as pdfs. These pdfs can be created in one of two ways:

  1. The .pdf file is knitted directly using R Markdown in RStudio

  2. A .docx file knitted using R Markdown in RStudio is saved as a pdf

You are welcome to choose whichever option works best for you.

R and RStudio are required for this course. You can either download these applications to your personal computer or use the computers in Mann Library. The use of Latex in this course is optional. If you have Latex, you can create the pdfs for this class using option 1 above. If you do not have Latex, you can still create the pdfs for this class using option 2 above. Note: If you already downloaded these applications in Fall, it is recommended that you redownload R and RStudio to get the latest versions. You do not need to redownload MacTex or MikTex.

If you have any trouble downloading these applications, please go to a TA’s office hours for help. It is crucial that you are properly set up for this class by the time the first homework is due so that you do not fall behind.

Downloading R, RStudio, and LaTex

In this class we will primarily be using RStudio for data analysis. R is required to run RStudio; RStudio is only a convenient interface for R. A Latex editor is not required for this course. However, with Latex, pdf documents can be knitted directly in RStudio. Here we will go through the steps to download R, RStudio, and a Latex editor. If you choose not to download a version of Latex, just skip any steps below associated with downloading MacTex or MikTex.

For Macs:

  1. We will first download R. Go to the wesite, http://cran.r-project.org/bin/macosx.

  2. Click on either R-3.1.2-snowleopard.pkg or R-3.1.2-mavericks.pkg depending on your operating system, see descriptions. This will download the R package on your computer. It will take a few minutes or significantly more depending on your downloading speed.

  3. Go to your downloads folder and double click on the package you downloaded. This will start the installer for R. Go through the steps to start the installation. When you have choices, unless you prefer otherwise, choose the defaults. This will take a couple of minutes.

  4. R should now be in your “Applications” folder which can again be found on the left hand side of your Finder window.

  5. Next we will download MacTex, the LaTex editor for Mac. If you already have MacTex, you do not need to download it again. The steps are similar to those used for downloading R. You can find MacTex at http://www.tug.org/mactex/index.html.

  6. To download MacTeX, click on MacTeX.pkg on this page. Again, this will take a few minutes or longer to download.

  7. Once MacTeX.pkg is downloaded, you will again see it in “Downloads”. Double click on this package to start the installation.

  8. Now MacTex will also be in your “Applications” folder.

  9. Finally, we will download RStudio, our main tool for this class. Go to the website http://www.rstudio.com/products/rstudio/download/.

  10. Click on RStudio 0.98.1091 - Mac OS X 10.6+ (64-bit). This will download RStudio and a folder will pop up that includes both the “Applications” folder and RStudio. Drag RStudio into the “Applications” folder and close this window.

  11. Likely after step 10, you will have an RStudio icon on your desktop. You can right click on this icon and choose Eject to get rid of this icon.

  12. You are now ready to use RStudio.

For Windows:

  1. We will first download R. Go to the wesite, http://cran.r-project.org/bin/windows/base/.

  2. Click on Download R-3.1.2 for Windows. At the bottom of the Explorer window, you will be asked whether you want to run or save this file. You can go ahead and run it unless you would like to start the installation at a later time. After selecting to run this program, you may be asked if you want this program to make changes to your computer. Click “Yes.”

  3. This will start the installer for R. Go through the steps to start the installation. When you have choices, unless you prefer otherwise, choose the defaults. This will take a few minutes.

  4. R should now be in your “Programs” folder. It can be accessed by clicking on the “Start” icon in the bottom left corner of your screen and choosing “All Programs”. You should see a folder for R.

  5. Next we will download MiKTex, the LaTex editor for Windows. If you already have MikTex, you do not need to download it again. The steps are similar to those used for downloading R. You can find MiKTex at http://miktex.org/download.

  6. To download MiKTeX, click on the blue box under “Recommended Download.” Again, you will be asked whether you want to run or save this file. After selecting to run the program, you may be asked whether you would like it to make changes to your computer. Choose “Yes.”

  7. This will start the installer for MiKTeX. Go through the steps to start the installation. When you have choices, unless you prefer otherwise, choose the defaults. This will take a few minutes.

  8. MiKTeX should now be in your “Programs” folder. It can be accessed by clicking on the “Start” icon in the bottom left corner of your screen and choosing “All Programs”. You should see a folder for MiKTeX.

  9. Finally, we will download RStudio, our main tool for this class. Go to the website http://www.rstudio.com/products/rstudio/download/.

  10. Click on RStudio 0.98.1091 - Windows XP/Vista/7/8. Again, at the bottom of the Explorer window you will be asked whether you want to run or save this file. Go ahead and run it. You will likely be asked if you want this program to make changes to your computer. Choose, “Yes.”

  11. This will start an installer for RStudio. Unless you prefer otherwise, choose the defaults for the installation process. This will take a few minutes.

  12. Once RStudio is installed, it also should be found in your program files.

  13. You are now ready to use RStudio.

Please note (for later): When you create your first .pdf file using R Markdown, you may need to download more files for MiKTeX. You will have an option to download them from the internet. Choose this option. It will give you a number of places where these files can be downloaded from. Choose something in the US.

Introduction to R and R Studio

You’ll be using R and RStudio throughout this course both to learn the statistical concepts discussed in the texbook and also to analyze real data and come to informed conclusions. To straighten out which is which: R is the name of the programming language itself and RStudio is a convenient environment for writing and running R code.

As the course progresses, you are encouraged to explore beyond what the labs and homework dictate; a willingness to experiment will make you a much better programmer. Before we get to that stage, however, you need to build some basic fluency in R. Today we begin with the fundamental building blocks of R and RStudio: the interface and basic commands.

rinterface

The panel in the upper right contains your workspace as well as a history of the commands that you’ve previously entered. Any plots that you generate will show up in the panel in the lower right corner.

The panel on the left is where the action happens. It’s called the console. Everytime you launch RStudio, it will have the same text at the top of the console telling you the version of R that you’re running. Below that information is the prompt, >. As its name suggests, this prompt is really a request, a request for a command. Initially, interacting with R is all about typing commands and interpreting the output. These commands and their syntax have evolved over decades (literally) and now provide what many users feel is a fairly natural way to access data and organize, describe, and invoke statistical computations.

Some Basic Information on R

R is a programming language primarily used for data analysis. To use R, you need to use the proper syntax for coding in R. In this class we do not assume prior knowledge of this syntax, and you will be given a lot of code that you only need to run. In lab, you may be given code to use for a particular dataset that you can edit for use in your homework when applying this code to a different dataset.

Many “libraries” or “packages” have been developed for R. These libraries contain functions that can be used directly. For example, the function plot(x,y) will plot points on a graph where the domain and range have been user defined.

You will not have all packages for R installed on your computer, but many basic functions are available after an initial installation of R. If we need more functions as the semester progresses, you will be told how to download and access these functions.

R Markdown

If you have used R before, the console in RStudio has the same functionality as the basic R console. Any command in R can be used in the exact same way in the RStudio console. RStudio, however, is designed to be more user friendly and to provide an easy way to reproduce any previous work in R through R Markdown. In this class, your homework will consist of either a pdf or Word file “knitted” by R Markdown. If you are knitting Word documents, your final submission to blackboard must be a pdf of your knitted Word document.

Testing RStudio

To assure that you are properly set up for this class, the file, test.Rmd, is provided for you to knit and save as a pdf. Complete the following steps to test your installation of R ansd RStudio:

  1. Download test.Rmd in the folder that you created for BTRY 6020.

  2. In RStudio, in the lower right window click on the tab “Files.” In “Files” navigate to the test.Rmd file. Click the highlighted name of this file to open it in RStudio. This file will now be opened in RStudio in a new window on the left above your R Console.

  3. R Markdown can create (or “knit”) .html, .pdf, and Word documents from a .Rmd file. On the menu for your .Rmd file, click the arrow next to knit. This dropdown menu allows you to choose whether you want to knit a pdf, html, or Word document. Choose one of the following options to create a pdf file from test.Rmd:

    1. If you have installed either MacTex or MikTex, choose “Knit PDF” to knit this document into a .pdf file. The .pdf file will have the same name as the .Rmd file with the new extension, .pdf. By default it is saved in the same folder as the .Rmd file from which it was generated. Look in your folder for BTRY 6020 using the lower right window of RStudio to verify your .pdf lab file is now there. This pdf file is saved as the newest version every time you re-knit your .Rmd file.

    2. If you have not installed a version of Latex, choose “Knit Word” to knit this document into a Word document. The Word document will have the same name as the .Rmd file with the new extension, .doc(x). By default it is saved in the same folder as the .Rmd file from which it was generated. Look in your folder for BTRY 6020 using the lower right window of RStudio to verify your Word document is now there. This Word document is saved as the newest version every time you re-knit your .Rmd file. Now, create a pdf of your Word document. For this class, your final homework document must be submitted as a pdf.