Using R for Arch Modeling (Mac Users)

Using R for Arch Modeling (Mac Users)

USING R FOR ARCH MODELING (MAC USERS)

R is a free software environment for statistical computing and graphics. It was originally designed as a freeware version of S-plus. It is generally considered user-friendly and executes code line-by-line.

R for MAC OSX can be downloaded freely from and installed on your computer. R is also available for use on apps.stern.nyu.edu

Even though R can be downloaded for Mac, it is strongly urged (to avoid unforeseen issues) that students using Macs stick to working with R and Minitab on apps.stern.nyu.edu.

R can be used for a variety of applications. We will be using R in this course to estimate ARCH/GARCH models.

Using Minitab to estimate ARIMA models and using R to estimate ARCH models based on the ARIMA residuals involves passing data back and forth between Minitab and R. To do this we will have to save the output of one program and open it as an input to the other. Therefore it is important to understand where we save data and how to retrieve it. It is important to decide whether to save output on a local computer drive or on the H: drive available to Stern students. Wherever you save it, however, you must be able to specify a path to the file. So if you intend to create a file called filename in a folder called ‘Data’ on your H: drive, your path would be H:/Data/filename.

If you will be running R directly your personal computer you should save files locally (on your computer) whereas if you are running R remotely on apps.stern.nyu.edu, you should save files on your H: drive. This will ease the burden of figuring out what the correct file path would be.

R is built around functions that can be used by typing in commands next to the > prompt.

The R commands take form

>functionname(argument1, argument2, argument3,…, argument).

R comes with a very vast library of functions preinstalled. Users can also download and load packages containing other functions or create their own functions. If you type the name of a variable, R will output the contents of that variable. For example typing the commands

> x=2+2

> x

Will result in the output 4.

You can list all variables with >ls() and you can remove a variable (say x) with >rm(x).

Preinstalled and downloaded functions generally come with helpfiles. In order to access the helpfiles (to find out about arguments that need to be passed, or output that one can expect) the command is >help(“functionname”)

or simply

>?functionname

If one wishes to edit a function the command is

>fix(functionname)

For further explanation regarding R please browse:

“Manuals” at

The command for inputting data into R is >scan(). We will use it in one of the following ways:

If the residual file (say, RES.DAT) that was exported from Minitab has an asterisk (*) in the first row (due to the ARIMA model having d=1) use the following R commands:

>x= scan(“filepath/RES.DAT”, what=”list”)[-1]

>x=as.numeric(x)

If the residual column that was exported from Minitab does not have an asterisk (*) in the first row (due to the ARIMA model having d=0) use the R command

>x= scan(“filepath/RES.DAT”)

When this line is executed R creates a dataset called x that stores the data found in the file RES.DAT.

Similarly, a useful command for outputting data from R is >write().

If our original ARIMA model had d=1 we can write the conditional variances ht to the file HTFILE.DAT using:

>write(c(“*”, ht), “filepath/HTFILE.DAT”, 1)

If our original ARIMA model had d=0 we will write in R:

>write(ht, “filepath/HTFILE.DAT”)

When this line is executed R creates a new file called ‘htfile.dat’ located in ‘filepath’ that contains data that in the data set ‘ht’.

There are various possible combinations of using apps.stern and the personal computer. The following illustrates how data should be passed back and forth for the most common possibilities:

MINITAB and R both installed on your computer:

All data from Minitab and R should be saved on a local drive such as your ‘Desktop’. The typical filepath that we would use in scan() and write() is /Users/username/Desktop/filename.

MINITAB on apps.stern.nyu.edu and R on your computer:

All data from Minitab and R should be saved on a local drive such as on your ‘desktop’. Since R is located on your computer, it can only access data that is locally stored. You should be able to read and write data to your local ‘Desktop’ from Minitab. The typical filepath that we would use in scan() and write() is /Users/username/Desktop/filename.

MINITAB and R both on apps.stern.nyu.edu:

All data from Minitab and R should be saved on your H: Drive, which should always be available within apps.stern. Since R is located on apps.stern.nyu.edu it will be much easier to write a path to data located on your H: drive rather than your local ‘desktop’. Please do not attempt to save data on your local ‘desktop’ unless you are an experienced computer user. The typical filepath that we would use in scan() and write() is H:/filename.

There is no preinstalled function for estimating ARCH and GARCH models in R. Therefore the user will have to first download a package that has such a function. We will use the tseries package for this purpose. To download tseries, establish an internet connection, start R, and click on

R on your computer:

Packages & Data → Package Installer →Get List→USA(MA) → tseries (check box ‘Install Dependencies’) →Install Selected

R on apps.stern.nyu.edu

Packages → Install packages →USA(MA) → tseries

Next you must load the package. One way to do this is with the command

>library(tseries)

You will need to install and load tseries whenever you start R, unless you open a previously saved R workspace that had the package already installed.

The garch command (which requires tseries to be installed and loaded) is described in the handout “Estimation and automatic selection of ARCH models”, and in Homework 8. To suppress some unnecessary output when running the garch command, use the option trace=F. For example, to fit an ARCH(2) to x and suppress the unnecessary output, use

>model=garch(x,c(0,2),trace=F)