zeugner.eu
Home Life Studies Research Resources Contact

An Introduction to R for Econometrics

This webpage has accompanied an introduction to R, delivered in the TA sessions for the 'Econometrics I' course of Jesús Crespo-Cuaresma at IHS Vienna in 2007.
It remains posted as a reference but is not as up-to-date as its collection of web references.


Contents:


Installing R and the SciViews-R editor (under Windows)

  1. Installing R

  2. Install SciViews-R for Windows:

    Tinn-R is a nice graphical user interface (GUI) that outperforms R´s default GUI since it offers highlighting, context help, etc. Its custom R console is also a bit more intuitive, similar in style to that of Matlab. If you are running a different operating system, you might use JGR, which is quite handy; KDE-Linux uses may like RKward.
    1. Download the 'stable' version of Tinn-R from SciViews (preferably not to your desktop, but your C drive): Select the right version for your operating system.
    2. After the download, double-click the file (it will open automatically) and follow the installer.
    3. Now if you want to use R, go to to your Windows menu, and open 'R Console' in the folder 'SciViews-R'. Now you can type your commands in the box at the bottom and execute them directly with Shift+Enter. (Of course, you can still use the standard R GUI, by clicking on the 'R' icon on your desktop.)
    4. For longer programming tasks, it is nice to open 'Tinn-R' (also in the folder 'SciViews-R' in your Windows menu) in addtion to the console, which is a fully-fledged editor for R programming.
      In order to execute selected code immediately, it is useful to define a hotkey like Ctrl+R for 'Send: selection' under the Tinn-R menu -> 'R' -> 'Hotkeys of R'.

    Note: previously, I recommended JGR as my preferred R editor, since it runs under all kinds of operating systems, and is quite handy for editing. In daily use under Windows, however, JGR proved quite buggy with big R programming tasks, leading to slowdowns and system crashes quite often. Nevertheless it remains a viable alternative, in particular if you are only interested in working with basic routines, such as the ones on this page.


Some useful references for starting with R



Tutorials on R

'Lecture' material:

Information on lecture rooms

Most tutorials will be held in 6., Stumpergasse 56, mainly in the 'EDV Raum' at ground floor.
Only one lecture room (namely 'Schottenfeldgasse') is at 7., Schottenfeldgasse 29 in the third floor (ring if the office door is not open).

Homeworks in R

Requirements:

Homeworks in R have to be submitted as an R file via e-mail. This R file should work by direct execution, i.e. I should be able to type source("yourfile.R") and get the result printed on the console or into an output file. For the first homework, you may as well submit the necessary data file such that the routine works. For the subsequent homework, your R file should directly handle the data set given to you.

Homeworks will be graded along four dimensions:

Homework sample solutions:

  1. Homework sheet 1, no R exercises.
     
  2. Homework sheet 2, exercise 2: The data file in tab-separated format and a sample .R script exemplify the solution to the exercise.
    To examine it, download the .R file and the data file, and type in R setwd("path to which you downloaded the files") and then source("es2ex2.R").
     
  3. Homework sheet 3, exercise 3: The data file in CSV format, simply stored out of the original Excel file.
    This data contains many non-availables (NAs), and there are, in principle, two methods to adress this problem:
     
    • Cleaning the sample regressions from NAs individually:
      This approach was chosen by all of the students. A commented sample solution outlining this approach can be found in SalaIMartin_es3_3.R.
      To examine it, download it and the the data file and execute it via the source() command as above. Execution takes about 50 seconds on a Pentium M.
       
    • Cleaning the data first, and then do the individual regressions:
      This is the 'more right' approach, in my opinion. Moreover it lends itself to the use of the Frisch-Waugh theorem for the fixed regressors - which in turn enables the entire simulation to run in a few seconds. However, there are only 32 countries which are clean of NAs. So the data basis is quite weak.
      Sample code for this approach is in the file SalaIMartinFrischWaugh_es3_3.R. It is quite similar to the solution above.