[R] Please read **Urgent** Need help with R
Mehmet Doğan
mehmetdogan_07 at windowslive.com
Sun Sep 13 14:31:15 CEST 2015
Dear All,
I can NOT format and create an xts object of my data in order to forecast Volatility. Moreover I can NOT plot the data. basically i cant work with the data i have. I need to finish my project till tomorrow. Could you please help me with those problems?
I also attached my data. the problem could be also with the format of excel.
when i could like to plot data, the following error appears:Error in plot.window(...) : need finite 'ylim' valuesIn addition: Warning messages:1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion2: In min(x) : no non-missing arguments to min; returning Inf3: In max(x) : no non-missing arguments to max; returning -Inf
I have following codes.
I can NOT format the date after these codes ****# function assumes that the data is stored in the subdirectory called "data"SP500<-read.csv("Data_Forecast/EURUSD.csv")
SP500<-read.csv("Data_Forecast/EURUSD.csv",stringsAsFactors=F)****
setwd("D:\\NewThesis")
# install and load needed librariesinstall.packages("fBasics")install.packages("tseries")install.packages("car")install.packages("FinTS")install.packages("fGarch")install.packages("rugarch")
# rugarch# http://cran.r-project.org/web/packages/rugarch/vignettes/Introduction_to_the_rugarch_package.pdf
library(xts)library(fBasics) # e.g. basicStats()library(tseries)# e.g. jarque.bera.test()library(car) # e.g. durbinWatsonTest()library(FinTS) # e.g. ArchTest()library(fGarch) # e.g. garchFit()library(rugarch) # e.g. ugarchfit()
# !!! Introduction to the rugarch package# http://cran.r-project.org/web/packages/rugarch/vignettes/Introduction_to_the_rugarch_package.pdf
# lets load additional functions prepared by the lecturerssource("functions/TSA_lab06_functions.R")
#################### Example #1. # stylized facts ###################
#1.# To import data we will use a function import_data()# written by lecturers and stored in TSA_lab06_functions.R# (already loaded)
rm(list=ls())
# function assumes that the data is stored in the subdirectory called "data"SP500<-read.csv("Data_Forecast/EURUSD.csv")
SP500<-read.csv("Data_Forecast/EURUSD.csv",stringsAsFactors=F)
#############################################################################3SP500$Date<-as.Date(SP500$Date,format="%Y-%m-%d" , "h:m:s")
#format(chron(0, 0), c("d/m/yy", "h:m:s"), sep = " ", enclose = c("", ""))###################################################################################
str(SP500)# Date column is already converted to Date format
#First 6 head(SP500)
#Last 6tail(SP500)
# lets leave just date and close priceSP500<-SP500[,c(1,5)]
# and change the name of the last column into SP500names(SP500)[2]<-"SP500"
SP500.xts<-xts(SP500$SP500,SP500$Date)
#First 6 head(SP500)
#Last 6tail(SP500)
#############################################################################333attach(SP500)x<-Timey<-Closedetach(SP500)plot(x,y)##############################################################################plot(SP500)
plot(SP500$Date,SP500$SP500,type="l", main="Daily close price of SP500")
# lets add log-returns to the dataSP500$r<-diff.xts(log(SP500$SP500))
# lets limit our data to days since the beginning of 2000SP500<-SP500[SP500$Date>=as.Date("04-09-2015"),]
# lets plot the close priceplot(SP500$Date,SP500$SP500,type="l", main="Daily close price of SP500")
# ... and it's log-returnsplot(SP500$Date,SP500$r,type="l", col="red", main="Log-returns of SP500")abline(h=0,col="gray",lty=2) #abline functions adds line to the plot
# lets also plot the ACF function of log-returnsacf(SP500$r,lag.max=36,na.action=na.pass, ylim=c(-0.1,0.1), # we rescale the vertical axis col="darkblue",lwd=7,main="ACF of log-returns of SP500")
# this indicates some autoregressive relations among returns# which can be used to build an ARIMA model
# and do the same for the quared log-returnsacf(SP500$r^2,lag.max=36,na.action=na.pass, ylim=c(0,0.5), # we rescale the vertical axis col="darkblue",lwd=7,main="ACF of log-returns of SP500")
# this in turn indicates some autoregressive relations among SQUARED returns# (their variance!) which can be used to build a (G)ARCH model
# 2.# Do log-returns come from normal distribution?
basicStats(SP500$r)
hist(SP500$r,prob=T,breaks=40)# lets add a normal density curve for # sample mean and variance # dnorm() generates density function for a specified value# and parameters (mean, sd)
curve(dnorm(x, # data vector # dnorm creates a density value for the mean=mean(SP500$r,na.rm=T), # mean # normal distribution sd=sd(SP500$r,na.rm=T)), # sd col="darkblue", lwd=2, add=TRUE)
# 3.# Let's also examine:# *) Jarque-Bera statistic
jarque.bera.test(SP500$r)
# the function doesn't accept missing values
# lets omit the missing valuesjarque.bera.test(na.omit(SP500$r)) # na.omit functions omits the missing values
# null about normality strongly rejected
# *) Durbin-Watson statistic# the durbinWatsonTest() function requires # the linear model as an argument# lets simulate a model with just a constant term
durbinWatsonTest(lm(SP500$r~1), # here 1 means a constant max.lag=5) # lets check first 5 orders.
# lack of autocorrelation in returns strongly rejected # for lag 1,2 and 5
# *) ARCH effects among log-returns
ArchTest(SP500$r,lags=5) #(first 5 lags together)
# null stongly rejected
# we can also apply a DW test for squared returnsdurbinWatsonTest(lm(SP500$r^2~1), max.lag=5) # lets check first 5 orders# all pvalues 0. It means in all lags wereject autocorrelation # now ALL lags significant !!!
################################################# Example #2 # Choosing the proper order of GARCH(p,q) model ################################################
# 1.# ARCH(1)
k.arch1<-garchFit(SP500$r~garch(1,0), # we assume that returns follow an ARCH(1) process cond.dist= "norm", # conditional distribution of errors trace=FALSE) # if we don't want to see history of iterations
# summary of results and some diagnostic testssummary(k.arch1)
# The intercept in the model above is not significatn so we can drop it.k.arch1<-garchFit(SP500$r~garch(1,0),include.mean=F, cond.dist= "norm", # conditional distribution of errors trace=FALSE) # if we don't want to see history of iterations
# summary of results and some diagnostic testssummary(k.arch1)
# Let's examine squares of standardized residuals.
# we can automatically plot several parts of results# (lets select 11: ACF of Squared Standardized Residuals, # 10: ACF of Standardized Residuals,# and 11: Conditional SD; to quit press ESC)plot(k.arch1)
# squared residuals still have significant ACF for lags: 2,3,5,7,8,9,10
# 2.# lets try if ARCH(5) is enough to catch these phenomenonk.arch5<-garchFit(SP500$r~garch(5,0),include.mean=F, cond.dist= "norm", # conditional distribution of errors trace=FALSE) # if we don't want to see history of iterations
summary(k.arch5)
# all parameters significant# Ljung-box test for R^2 shows there is no more autocorrelation # between the current and past standardized squared residuals (variance)
# lets plot the ACF of standardized residuals# and standardized squared residuals (plots 10, 11)# ESC to exit
plot(k.arch5)
# lag nr 10 in ACF for squared residuals seems to be still significant, but# lets ignore it based on previous tests (LB)
# 4.# lets compare it with GARCH(1,1)k.garch11<-garchFit(SP500$r~garch(1,1),include.mean=F, cond.dist= "norm", # conditional distribution of errors trace=FALSE) # if we don't want to see history of iterations
summary(k.garch11)
# all parameters significant# Ljung-box test for R^2 shows there is no more autocorrelation # between the current and past standardized squared residuals (variance)
# lets plot the ACF of standardized residuals# and standardized squared residuals (plots 10, 11)# ESC to exit
plot(k.garch11)
# 5.# lets check higher order GARCH(1,2)k.garch12<-garchFit(SP500$r~garch(1,2),include.mean=F, cond.dist= "norm", # conditional distribution of errors trace=F) # if we don't want to see history of iterations
summary(k.garch12)
# lets plot the ACF of standardized residuals# and standardized squared residuals (plots 10, 11)# ESC to exit
plot(k.garch12)
# all parameters significant# Ljung-box test for R^2 shows there is no more autocorrelation # between the current and past standardized squared residuals (variance)
# Which model has most favourable information criteria AIC and SBC?
# function written by the lecturers# requires a list of names of EXISTING # (g)arch model results as an argumentcompare.ICs(c("k.arch1","k.arch5","k.garch11","k.garch12"))
# Does the best model have all parameters significant?
# 6.# Let's assume that the final model is GARCH(1,2)
str(k.garch12)
# 7.# Plot of conditional variance estimatespar(mfrow=c(2,1))plot(k.garch12 at data, # @data = original data values type="l",col="red",ylab="r",main="Log-returns of SP500")plot(k.garch12 at h.t, # @h.t = conditional variance type="l",col="darkgreen", ylab="cvar",main="Conditional variance from GARCH(1,2)")par(mfrow=c(1,1))
# 8.# Do standardized residuals come from normal distribution?stdres<-k.garch12 at residuals/sqrt(k.garch12 at h.t)
hist(stdres,breaks=20,prob=T, main="Histogram of standardized residuals \n from GARCH(1,2) for SP500")# lets add a normal density curve for # sample mean and variance curve(dnorm(x, mean=mean(stdres,na.rm=T), sd=sd(stdres,na.rm=T)), col="darkblue", lwd=2, add=TRUE)
# Jarque-Bera testjarque.bera.test(stdres)
# normalit yrejected
# Durbin Watson testdurbinWatsonTest(lm(stdres~1), max.lag=5) # lets check first 5 orders
# the first seems to be significant - indicates we can add # an ARIMA component in the mean equation
# ARCH effects among standardized residualsArchTest(stdres,lags=5)
Thanks in advance,
Yours sincerely,
Mehmet Dogan
More information about the R-help
mailing list