Eric Doviak  Doviak.net 
Economics and Public Policy Analysis
 
Working Papers:
 
Income Inequality
 
Health Insurance
 
Ridge Regression

For Students:
DEVELOPMENT 730
 
TRADE 76.3 and 727
 
MICRO 20.1
 
micro lecture notes
MACRO 10.1
 
macro lecture notes

Economic Links 



Software Corner

Why you should use GNU/Linux
The Alternative to Anarchy

Memory Usage:
MS Windows vs. GNU/Linux


Getting Started with R 



EMAIL ME!
Getting Started with R
printer-friendly format
 
R is very intimidating for a first-time user. When you open R, all you see is a command prompt. The experienced user begins typing. The first-time user is left with that sinking feeling of: "Now what?" and quits. I was so intimidated that a year passed between the time I installed it and the time I began using it.
 
I've written this page to help the first-time user who has to learn R quickly.
 
The first step is to download R and install it on your computer, so (if you haven't already done so) go to the R-project homepage and select the CRAN mirror nearest you. You'll immediately be taken to a download page. GNU/Linux users: you know what to do. MS Windows users: you want the "base" package. Mac users: I haven't used a Mac in over 10 years, so you're on your own.
 
MS Windows users should be able to use the program immediately after installing it (or after that annoying reboot). GNU/Linux users will be able to access the R prompt from the command line. In both cases, I recommend getting a better text editor for writing R scripts, like Emacs with ESS or Tinn-R and new users should install a graphical front-end, like RKWard or R Commander (editors and front-ends are also discussed below).
 
One word of caution:  R and MS Windows are both very memory-intensive and they will compete with each other for memory when you work with large datasets. There are three ways to overcome the memory deficiency. I suggest using all of them. The first way is to add more physical RAM to your computer. The second is to use a GNU/Linux operating system instead of MS Windows. GNU/Linux is much lighter on system resources and can make use of a swap partition (virtual memory) to extend your computer's physical memory. Finally, the third is to "vectorize" your code (i.e. avoid loops whenever possible).
 
Comparison of memory usage in MS Windows and GNU/Linux 

Learning the R Language
 
Once you have installed R, the next step is to learn the R language. Fortunately, there is a great introduction for first-time users. To get it, go to your local CRAN mirror and then click on the "Contributed" link, which is below "Documentation" in the menu on the left-hand side of the page. At the "Contributed Documentation" page, look for "The Friendly Beginners' R Course" by Toby Marthews and download the ZIP file.
 
If you print out Marthews' PDF file and follow his instructions, you'll quickly learn the R language. The "Contributed Documentation" page also has some other great documents. In particular, you should also read at "A Guide for the Unwilling S User" by Patrick Burns. It's a short 8-page introduction that discusses the language in more detail than Marthews' course.
 
I also found the reference cards, "R for Beginners" by Emmanuel Paradis and "Simple R" by John Verzani to be very helpful when I began using R. You can also find those documents on the "Contributed Documentation" page. Here's a PDF file with my favorite reference cards.
 
Emacs with ESS
 
Once you have developed a good knowledge of the R language, take a look at Emacs with ESS. You'll learn the keystrokes very quickly and -- because your hands never leave the keyboard -- you'll be able to type code much faster. In time, you'll feel disoriented every time you have to grab the mouse.
 
The Emacs tutorial (C-h t) will help you get started quickly. You can also learn a lot of tricks from the Emacs reference card and the ESS reference card.
 
Vincent Goulet distributes a nice version of Emacs with ESS for MS Windows. His page also contains a nice set of .emacs initialization files for GNU/Linux, MS Windows and Mac.
  
RKWard
 
I prefer to use Emacs with ESS, but some people prefer a graphical front-end. A good front-end combines ease of use with R's awesome power.
 
There is no better front-end than RKWard. RKWard has the drop-down menus that you need to get up and running in R and it also provides the R console, the KATE text editor, very clever data editor and a GUI for routine tasks (producing descriptive statistics, correlation matrix, etc.). It's a great package.

The plugins (that you access from the drop down menus) provide a terrific library of functions that you can immediately use. Best of all, the R code appears at the bottom of each plugin window. By studying and modifying the code that appears beneath each window, you can quickly learn how to write your own scripts.
 
RKWard is already a complete replacement for commercial statistical packages and it keeps getting better with each new release. One of the goals of the RKWard project is to provide seamless integration with an office-suite, so that you can easily document and publish your results.
 
 
R Commander
 
RKWard has not been ported to MS Windows yet. In the meantime, the nearest replacement is R Commander. Like RKWard, it provides a large number of drop down menus and dialog boxes to help you load data, compute statistics, run regressions, etc. (By the way, R Commander can be also run in GNU/Linux).
 
Before installing R Commander, you should read about the package and read the installation notes. For those of you who are too impatient to read the directions, here's an abbreviated set:
  • Right-click on your desktop shortcut to R and select "Properties."
  • At the end of the "Target" to your RGui add the text " --sdi" Drop the quotes and don't forget to include the SPACE before the first of the TWO dashes!
  • Next, double-click on the shortcut (to open the R Console), go to the "Packages" drop-down menu and select "Install package(s)..."
  • Choose a nearby CRAN mirror and then select "Rcmdr."
Once you've installed the package, you can either load it from the R Console or you can add a few lines to the Rprofile.site file. To load it from the R Console, go to the "Packages" drop-down menu and select "Load package..." and select "Rcmdr" from the dialog box. To start R commander every time you use R, add the following lines to the Rprofile.site file in the "etc" folder.
 
local({
old <- getOption("defaultPackages")
options(defaultPackages = c(old, "Rcmdr"))
})
 
Tinn-R
 
R Commander's drop down menus are great, but soon you'll want to write a script. At this point, MS Windows users can either use Emacs (discussed above) or Tinn-R.
 
Tinn-R wraps text, highlights parenthesis, brackets, etc. and changes the color of commands and words in quotes. Such features are very helpful when you're writing a long R script. There's also a set of tools on the right-hand side that helps you find the commands you need.
 
The annoying thing is that word-wrapping is turned off by default. To turn it on, look for the "Toggle (show/hide) word wrap" button in the middle of the upper toolbar. On my computer, it's just below the "Window" drop-down menu.
 
You also need to set the path to the RGui, so that you can pass commands from Tinn-R to the R console. To do that, go to the "Options" drop-down menu and select "Main" and then select "Application." A dialog box will open. Choose the "R" tab and click on "Path to your preferred Rgui." It should be something along the lines of: "C:\Program Files\R\R-2.4.1\bin\Rgui.exe"
 
Once you've set the path to the Rgui, you can open the R console from Tinn-R by clicking on the little R button on the lower toolbar and you can pass commands from Tinn-R to the R Console by pressing the buttons in lower toolbar.
 
Now you're ready to write a script, but what command should you call? The "R card" in the Tools menu gives you a large number of commands that you can easily insert into your script.
 
With a little practice, you'll quickly become an R expert.
 
Have Fun!