USING R FOR ARCH MODELING Setting up:
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 can be downloaded freely from http://cran.r-project.org/ and installed on your computer. R is also available for use on apps.stern.nyu.edu
For Mac users, it is recommended (to avoid unforeseen issues) to work 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.
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.
Basics:
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 http://cran.r-project.org/
Reading/writing data:
To input/output data, you need to specify where your file is located. You can either do this by typing in the exact directory of the file, or you can use the command
‘file.choose()’, where a window will pop up for you to select your file.
Reminder: If you work with R locally, save all your output files to your local drive, even when you use MINITAB on the server to fit ARIMA. However, if your prefer using the server version of R, then save your files to the server drive “H:/…”.
The command for inputting data into R is >scan(). We will use it in one of the following ways:
Suppose the residual file, RES.TXT, was exported from Minitab.
Then in R, read the file:
>x = scan(“filepath/RES.TXT”, what=”list”) # need to specify exact directory or:
>x = scan(file.choose(), what=”list”) # choose the file from the pop-up window Check the first 5 entries of x:
>head(x)
Notice that you may observe entries that are not numbers. This is because that Minitab normally outputs the name of the variable (“RESI1”). Meanwhile, it may also output a star (“*”) when you work with differenced data. You need to remove the non-numeric elements in x before doing the analysis. For example, if the first two entries of x are
“RESI1” and “*”, to remove them
>x = x[−c(1,2)]
Then we convert x to numeric format:
>x=as.numeric(x)
Now we are ready to fit a GARCH model on the residuals x.
Similarly, a useful command for outputting data from R is >write().
Say you want to output the conditional variance, ht, into a file named ‘htfile.txt’:
>write(c(“ht”,“*”, ht), “filepath/htfile.txt”, 1) # need to specify exact directory or:
>write(c(“ht”,“*”, ht), file.choose(), 1) # choose the file from the pop-up window
Note from above that to be aligned with Minitab, you may want to append a header (‘ht’) and a star ‘*’ to the head of ht, where the latter depends on whether your original ARIMA model had d=1. If in your case d=0, simply remove the ‘*’, i.e.
>write(c(“ht”, ht), …, 1)
Then in Minitab, you can read in ‘htfile.txt’ to do further analysis. Note that in Minitab, the command file −> open has a default option ‘treat the first element as the header’.
That’s why we added “ht” when writing the file. If you follow the default in Minitab, you will have a new spreadsheet with one column named “ht”. The first element may or may not be “*” depending on the value of d.
R packages:
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)