Due Date: September 2, 2015, 1:00pm


Getting Started

There are four main goals of this first homework:

  1. For you to download R, RStudio, and LaTeX (optional) so that you are set up for all future homework and labs.

  2. To introduce R and RStudio with some basic exploratory work.

  3. To help you organize your files.

  4. To get you into the habit of downloading all lab files BEFORE you get to lab either on a PC or a thumbdrive.

If you have trouble with (1), please go to a TA’s or Dr. Earls’ office hours for help.

Lab 1 Preparation

An introduction to R and RStudio will be given in lab 1. In lab 1 you will also receive help editing the .Rmd document you are required to submit for homework 1. 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.

You must have the following completed BEFORE lab 1:

  1. Download R and Rstudio.

  2. Download all the files for homework 1 AND lab 1 in appropriately designated folders either on a thumbdrive or a PC (depending on whether you plan to use your own computer in lab 1 or the lab computers). More instructions will be given on how to download these files in the section below on file management.

  3. Knit your HW1-15.Rmd file at least once.

  4. Go through all of the steps listed in this webpage up to the point where you would start editing your homework R Markdown file. You can go further than this on your own, but you must have at least this much done before lab. Your lab TAs will assume that you have these files available at the start of lab and will not take time for you to download them.

Should I download LaTeX?

First note, for this class you are required to submit a pdf homework document.

Advantages of downloading LaTeX:

  1. You can create your pdf homework document directly through R Markdown. Otherwise you will need to knit a Word document and subsequently save it as a pdf before you upload your homework.

  2. LaTeX has a great equation editor and is often used to write technical papers.

Disadvantage of downloading LaTeX:

  1. LaTeX requires a substantial amount of computer memory (100 MB more or less). It may not be worth the time and effort to download LaTeX if you only plan on using it for this class.

Blackboard

For this class, homework will be uploaded onto Blackboard. For homework 1 the following should be uploaded onto Blackboard:

  1. A screen shot of RStudio from your computer where the “Files” tab is selected and the folder in which you are saving all of your files for HW 1 is open. This folder should include the files created for your first homework. This folder should be nested within another folder labeled “BTRY6010”. Creating these folders is part of your first assignment under “File Management” below.

  2. Your LastF-HW1-15.Rmd and LastF-HW1-15.pdf files where LastF is replaced by your last name and the first letter of your first name. In this homework, you will be given instructions on how to create these files.

For information on how to upload files to blackboard see http://bbhelp.cit.cornell.edu/assignments/.

File Management

In this class, we will be downloading and uploading data and files from Blackboard and possibly other sources. It is important that you know where these files are located. Having a good system for organizing your files on the computer can go a long way to ensuring that you can find what you need from old projects years later (imagine a reader asks you for your code five years after you published a paper). Here we will create a folder for BTRY 6010 on your computer.

Please complete the following tasks using the appropriate instructions for your computer (i.e. Mac or Windows).

For Macs: Every file you have on your computer can be referenced by a file path. If you open Finder which should be on your toolbar. It will look something like this, where your home directory will be labeled differently than the one shown here and you may have different folders listed.

Finder

  1. All of your computer files can be accessed through Finder. The home directory here is called “cecilia”. In this directory is a folder labeled “Documents”. For most of you, you likely have your documents folder under your home directory. If not, please create a folder under your home directory called “Documents.” You can do this by clicking on your home directory on the left column of your Finder window. Then from the Finder menu at the top of your screen, choose File -> New Folder. This will create a new folder in your home directory. You will need to rename it by clicking on the name it currently has and changing it to “Documents”.

  2. Once you have a “Documents” folder, create a new folder within “Documents” called “BTRY6010.” Again this is done by double clicking on the folder “Documents” to open it and choosing File -> New Folder from the menu bar. Again, rename this folder as indicated above.

  3. We suggest you create a new folder for every assignment in this class where all the files for that assignment are kept. Use the directions in step 2 to create a folder for homework 1 in your BTRY 6010 folder.

  4. On the left hand side of your Finder window, the folder “Downloads” should be listed. If not, it should be found under your home directory. This is an important folder as everything you download will be put in that folder. Now open your internet browser. We suggest that you use Firefox or Chrome (both are compatible with Blackboard).

  5. In Blackboard, go to the folder for homework 1 and right click on the HW1-15.Rmd file. Choose “Download Linked File.” This will send this file to your “Downloads” folder.

  6. Now put HW1-15.Rmd in your homework 1 folder. You can do this by opening two Finder windows. A second Finder window can be opened by choosing File -> New Finder Window from the menu bar for Finder. Open one Finder window to “Downloads”. Open the other Finder window to your folder for HW1. Click on the HW1-15.Rmd file in “Downloads” and drag it into the folder for homework 1. Rename this file as “LastF-HW1-15.Rmd” (e.g. EarlsC-HW1-15.Rmd).

  7. Repeat the process above, except this time create a folder for Lab 1 in your BTRY 6010 folder. Download all 3 of the files in the Lab 1 folder on Blackboard into this folder.

  8. Proceed to the instructions for Macs to download R, RStudio, and LaTex.

For Windows: Every file you have on your computer can be referenced by a file path.

  1. The lower left corner of your screen contains the “Start” icon for Windows.

Start

Clicking on this icon gives you access to all of the files on your computer. On the right hand side after you click on Start will be your “Documents” folder. Open this folder by clicking on “Documents”.

  1. Create a new folder within “Documents” called “BTRY6010.” You can do this by clicking on “New Folder” on the menu on the top of the window for your “Documents” folder. After the folder is created, change the name of this folder to “BTRY6010.”

  2. Click on this new folder. We suggest you create a new folder for every assignment in this class where all the files for that assignment are kept. Use the directions in step 2 to create a folder for homework 1 in your BTRY 6010 folder.

  3. Note that in the window containing your “Documents” folder, on the left hand side are a list of other folders that you can also access. One of these folders is called “Downloads”. Anything you download off of the internet will be placed in this folder unless you specify otherwise. We recommend using Firefox or Chrome as your browser (both are compatible with blackboard).

  4. In Blackboard, go to the folder for homework 1 and right click on the HW1-15.Rmd file. Choose Save target as…. A window will pop up in which you can specify where this file is saved. If you are not already in the folder you created for homework 1, select “Documents” from the left hand side of this window. Then double click on the BTRY 6010 folder you created within “Documents”, and finally double click on the homework 1 folder that you created within your BTRY 6010 folder. Change the name of this .Rmd file to “LastF-HW1-15.Rmd” (e.g. EarlsC-HW1-15.Rmd) before you save it. Save this document in your folder for homework 1 by clicking on “Save” at the bottom of this window.

  5. Repeat the process above, except this time create a folder for Lab 1 in your BTRY 6010 folder. Download all 3 of the files in the Lab 1 folder on Blackboard into this folder.

  6. Proceed to the instructions for Windows to download R, RStudio, and LaTeX.

Downloading R, RStudio, and LaTeX

In this class we will primarily be using RStudio for data analysis. RStudio depends on two other applications, R and LaTeX (if you want to knit pdfs). Here we will go through the steps to download all three of these applications. Note: If you already have these applications, please download the latest versions so that RStudio has the same look and functionality for everyone.

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.2.2.pkg or R-3.2.1-snowleopard.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.

Note: Steps 5-8 are optional. If you choose not to download LaTex, proceed to step 9.

  1. Next we will download MacTeX, the LaTeX editor for Mac. The steps are similar to those used for downloading R. You can find MacTeX at http://www.tug.org/mactex/index.html.

  2. When downloading MacTeX, you will need to choose whether you would like to download a basic version or the complete version of MacTex. I would choose the basic version. This will require you to install some packages as you need them, but it decreases your need to have a fast and reliable internet connection to use MacTeX. You will end up either choosing the link Smaller Download or the link MacTeX Download. Both will send you to a webpage where you can download your desired version of MacTeX. Depending on which you chose, on the page you are directed to you will either click on BasicTeX.pkg or MacTeX.pkg to download MacTeX. It will take a few minutes or longer to download.

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

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

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

  6. Click on RStudio 0.99.473 - 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.

  7. 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.

  8. You are now ready to use RStudio.

Please note (for later): When you create your first pdf or Word file using R Markdown, you may need to both install extra R packages (choose to do this) and download more files for MacTeX. 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.

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.2.2 for Windows. At the bottom of the Chrome window left click on the icon for the .exe file to open it. You may be asked if you want this program to make changes to your computer. Click “Yes.” It may also ask you what language to use during the installation.

  3. This will start the “Setup Wizard” for R. Go through the steps to start the installation. In general, when you have choices, unless you prefer otherwise, choose the defaults. However, when you get to the window to “Select Components”, choose 32-bit Files if your operating system is 32-bit and 64-bit Files if your operating system is 64-bit. Basically, this will involve “unselecting” either 32 or 64 bit files. You can find out which operating system you have by going back to the “Start” icon and selecting “Computer”. At the top of the window that pops up, choose “System Properties” from the menu. The operating system you have is listed here. The whole process will take a few minutes. At the end, you will click on “Finish”.

  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. It will either be listed up front or will be in the “R” folder under “All Programs” (or both).

Note: Steps 5-8 are optional. If you choose not to download LaTex, proceed to step 9.

  1. Next we will download MiKTex, the LaTex editor for Windows. The steps are similar to those used for downloading R. You can find MiKTex at http://miktex.org/download.

  2. When downloading MiKTeX, you have two choices to make in regard to the version of MikTeX that you download. First: choose a version of MikTeX that is compatible with your operating system (32 or 64 bit). Second: you will need to choose whether to download a complete LaTeX/MikTeX installation or a basic installation. I would recommend the basic installation. This will require you to install some packages as you need them, but it decreases your need to have a fast and reliable internet connection to use MikTeX. You will end up either choosing the download under “Recommended Download” or one of the first three options under “Other Downloads”. Once you have downloaded the .exe file, you can left click on the icon of this file in the lower left corner of your browser. This will bring up a window asking if you want to allow this program to make changes to your computer. Choose “Yes.”

  3. 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. You will need to specify where to install MikTex. Choose “Program Files” and then type in a backslash and “MikTex” to create a folder for MikTeX under “Program Files”. You may want to also choose to install necessary packages “on-the-fly”; it is up to you. The installation process will likely take several minutes.

  4. 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 2.9.

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

  6. Click on RStudio 0.99.473 - Windows Vista/7/8/10. Again, left click on the downloaded .exe file. You will likely be asked if you want this program to make changes to your computer. Choose, “Yes.”

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

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

  9. You are now ready to use RStudio.

Please note (for later): When you create your first pdf or Word file using R Markdown, you may need to both install extra R packages (choose to do this) and 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

The goal of this homework is to introduce you to R and RStudio, which you’ll be using throughout the 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.

Some Basic information on RStudio

Here is what RStudio should look like when you open it:

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.

We will explore RStudio further in lab 1.

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 and Directions to Start Homework 1

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 pdf or Word files “knitted” by R Markdown. In this section of your homework, you will produce your first homework file through R Markdown.

Complete the following steps to create your first homework document. Steps 1-5 should be completed BEFORE the first lab. It is really important that you knit your first homework document at least one time prior to lab to make sure you will not have any trouble knitting in the lab. If you do have trouble knitting, attend office hours BEFORE lab to get this sorted out. After a brief introduction to R and RStudio, your lab TA will start with #6 below.

  1. For this homework, you will modify a previously created R Markdown document. We will use the HW1-15.Rmd file downloaded in the first section of this homework. This file should now be named “LastF-HW1-15.Rmd” and be found in your folder for homework 1.

  2. In RStudio, in the lower right window click on the tab “Files.” In “Files” navigate to your .Rmd file for homework 1. 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. Click on the window of your .Rmd file. You can edit this file. In lines 13 and 14 of this file, put in your name and your NetID.

  4. 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. For now, choose “Knit PDF” or “Knit Word” to knit this document into a pdf or Word file. The knitted file will have the same name as your homework file with the new extension, .pdf or .docx. By default it is saved in the same folder as the .Rmd file from which it was generated. Look in your folder for your first homework using the lower right window of RStudio to verify your knitted homework file is now there. This file is saved as the newest version every time you re-knit your .Rmd document.

  5. If you knitted a Word document in (4), save a pdf version of this document in the same folder.

  6. For this homework, you will perform a number of basic tasks in R through R Markdown. After each step (or maybe after a couple of steps), you should knit your pdf or Word file to make sure your code ran correctly. At the end of this homework you will upload both your homework .Rmd file and a pdf file of your knitted .Rmd document to blackboard.

For the rest of homework 1, follow the instructions in your .Rmd file for homework 1.