Skip to content

Instantly share code, notes, and snippets.

@mmmayo13
Created May 23, 2018 02:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mmmayo13/8dbbafa1e82b918716f28b480b09ee3c to your computer and use it in GitHub Desktop.
Save mmmayo13/8dbbafa1e82b918716f28b480b09ee3c to your computer and use it in GitHub Desktop.
#Install the Ecdat package
install.packages("Ecdat")
#Loading the library and the Garch dataset
library(Ecdat)
mydata=Garch
#Look at the dataset
str(mydata)
#Correct the data types of date and day
#Correcting date fixes it to some arbitrary date such that the trend is same but the mapping is different
mydata$date=as.Date(mydata$date, origin = "01-02-1980")
mydata$day=as.factor(mydata$day)
#Install packages and load them
install.packages("tseries")
install.packages("urca")
install.packages("fUnitRoots")
install.packages("forecast")
install.packages("fGarch")
library(fGarch) # estimate GARCH and Forecast
library(tseries) #used for time series data
library(urca) #Used for checking Unit root Cointegration
library(fUnitRoots) #Used for conducting unit root test
library(forecast) #Used for forecasting ARIMA model
#Converting Dollar - Deutsche mark exchange rate to time series
exchange_rate_dollar_deutsch_mark <- ts(mydata$dm, start=c(1980, 1), end=c(1987, 5), frequency=266)
#Plot the time series
plot.ts(exchange_rate_dollar_deutsch_mark, main="exchange_rate_dollar_deutsch_mark")
#Calculate inflation as difference of log of exchange rate and then multiplied by 100
inflation_series<-(diff(log(exchange_rate_dollar_deutsch_mark)))*100
#Plot the inflation
plot.ts(inflation_series, main="Inflation of exchange rate")
summary(inflation_series)
#ACF and PACF Plots
acf(inflation_series)
pacf(inflation_series)
#Model fitting. We have selected the ARIMA(5,0,0) model
Arima_5_0_0<- arima(inflation_series[1:499], order = c(5,0,0))
#Check out the residuals
residual <- Arima_5_0_0$resid
acf(residual)
pacf(residual)
#Perform the Ljung Box test
Box.test(residual,c(20),"Ljung-Box")
#Fitting GARCH over the first 500 data points
garch.fit <- garchFit(formula = ~arma(5,0)+garch(1,1), data = inflation_series[1:500])
#Plot the model
plot(garch.fit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment