Computing with the R Statistical Program
Installation binaries from CRAN (Comprehensive R Archive Network) Mirrors
Download the R program from one of the following mirrors (I have found UCLA to be the fastest, and Carnegie Mellon the slowest):
UCLA --
Berkeley --
Case --
Iowa State --
Carnegie Mellon
Follow the instructions below for a quick setup; read the more extensive
R installation guide for options and to try to solve any problems with installation.
Setting up your R directory
-
Navigate to your Documents directory (on Windows 7) or to your "My Documents" directory on XP; for Linux, open a terminal window and type "cd" at the prompt to get to your home directory.
- RIGHT CLICK the link below and copy both the Stat1040.R and the .Rprofile files to your Documents directory. (Choose the "Save file as..." item that comes up when you right click, and navigate to your home or Documents directory).
IF USING Windows, be sure to save the files as TYPE="allfiles" when given the option to save as a "text" file -- if you do, the file will be saved as "Rprofile.txt" and you will have to rename it.
These two files will define several useful functions for making the R program work more easily.
- If it doesn't load automatically when you run R, use the command:
> source("Stat1040.R")
>
R installation notes How to obtain and install R.
Read this very carefully if something goes wrong with your installation.
R data files
Copy the files below to your home directory.
This can be done automatically with the command:
> Download.Data()
You can load these files into R, but it will require one of several commands, depending on the file type:
- Penn World Tables OECD subset
Provides a subset of macroeconomic data from the OECD countries (Organization of Economic Cooperation and Development, primarily the more developed countries).
this is an .Rdata file, and is loaded into R with the command:
>>> load("PWT7.OECD.Rdata")
- U.S. Monthly Macroeconomic Data from the FRED (Federal Reserve Economic Data) database. Also a .Rdata file; load with the command:
>>> load("FredMonthly.Rdata")
- Stock Market Data, 1871-2011 from Robert Shiller, at his website Irrational Exuberance. The website will have updated data in the form of an Excel file.
To load the data into R after downloading to the R directory, use the command:
>>> shiller <- read.ts("Shiller.csv", 12)
We use "read.ts" for time series data; the 12 indicates monthly data, 4 would indicate quarterly data, and 1 annual data.
- Economic Growth Data from N.Gregory Mankiw, David Romer, and David N. Weil, A Contribution to the Empirics of Economic Growth , Quarterly Journal of Economics, 107:2(May, 1992), pp.407-437.
To load this data into R, use the command:
>>> load("ecgrow.Rdata")
- Pearson's Height Data for fathers and sons, from late 19th century England. This data is used by the textbook as its dust jacket, and extensively in chapters 9-12 in explaining regression and correlation.
To load this data into R, use the command:
>>> pearson <- read.table("Pearson.dat", header=T)
- R introductory session
My tutorial offering a first exploration of R. Don't worry about every detail the first time through, but be sure to take a second and third trip through this handout. The material treated will be included in the first exam.
- Other recommended tutorials and websites:
- R Tutorials by Deepayan Sarkar. Excellent brief introduction to R with exercises. You should work through the first 3 tutorials carefully as soon as possible.
- Tutorial by Kelly Black, Clarkson U.
Especially good walkthrough of probability and statistics commands.
The homework exercises at the end of the tutorial should be done: download the data, read it in, and try the exercises for Case Study I.
- R Graph Gallery
One of R's great strengths is its graphic capabilities, illustrated here.
- SimpleR: Using R for Introductory Statistics by John Verzani.
An early version of Verzani's text, available from Amazon.
R websites