[R] Switching log(J) to log(J+1) to avoid log(0) in HAR-RVJ model
Joshua Ulrich
josh.m.ulrich at gmail.com
Thu Jul 19 22:20:27 CEST 2012
Cross-posted on Stack Overflow:
http://stackoverflow.com/q/11567745/271616
--
Joshua Ulrich | FOSS Trading: www.fosstrading.com
On Thu, Jul 19, 2012 at 12:23 PM, cursethiscure
<caolan.harvey6 at mail.dcu.ie> wrote:
> I am working with xts dependent data, and my code is as follows (the problem
> is explained throughout):
>
> dat <- getdat("prices")
> dat <- read.zoo(dat, sep = "",format="%d/%m/%Y %H:%M",
> tz="", FUN=NULL, regular=TRUE,
> header=TRUE, index.column=1, colClasses=c("character",
> "numeric"))
> dat <- as.xts(dat)
> ## cleaned data here to get
> daylist <- lapply(split(dat, "days"), function(x) {
> if(NROW(x) >= 10) x
> })
> do.call(rbind, daylist) -> dat
> makeReturns(dat) -> dat
>
> the code I am running to get the HAR regressions (full code for it shown
> below) is
>
> x = harModel(dat, periods = c(1,5,22), periodsJ=c(1), RVest =
> c("RCov","RBPCov"),
> type="HARRVJ", h=5, transform="log") ; # Estimate the HAR model
> of type HARRVJ
>
> The three HAR models on paper are:
>
> 1.〖RV〗_(t,t+h) = β_0+ β_D 〖RV〗_t+ β_W 〖RV〗_(t-5)+ β_M 〖RV〗_(t-22) + β_J J_t
> + ε_(t,t+h) # NULL model in code at bottom
>
> 2. (RV〗_(t,t+h) )^(1/2) = β_0+ β_D 〖〖RV〗_t〗^(1/2)+ β_W (〖RV〗_(t-5) )^(1/2)+
> β_M (〖RV〗_(t-22) )^(1/2) + β_J (〖 J〗_t )^(1/2) + ε_(t,t+h)
>
> 3. log(RV〗_(t,t+h) )= β_0+ β_D.log(〖RV〗_t )+ β_W.log(〖RV〗_(t-5) )+
> β_M.log(〖RV〗_(t-22) ) + β_J log(J_t + 1) + ε_(t,t+h)
>
> Basically the harModel in the code allows you to transform the regressions
> from NULL to "sqrt" or "log", but when `transform="log"` is chosen it gives
> the following error message.
>
> x = harModel(dat, periods = c(1,5,22), periodsJ=c(1), RVest =
> c("RCov","RBPCov"),
> + type="HARRVJ", h=22, transform="log") ; # Estimate ....
> [TRUNCATED]
> Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
> NA/NaN/Inf in foreign function call (arg 1)
>
> which is due to it taking the log(0), the actual log model should take
> log(J + 1) in case of a 0 value for the J in the time series ( if J does not
> occur it should return a 0, otherwise it should return J), but unfortunately
> I do not know how to rectify this. I was wondering if any one could tell me
> how how I can achieve this as I am very naive with R still. I have tried
> some modifications you will see at the bottom of the question, but they do
> not compute the regression correctly though they do allow it to run without
> 'error'
>
> A sample of the xts returns are
>
<snip>
>
> The full code is just below this code and what I have tried to do within it
> is change the following two lines when running transform="log" but this
> gives unrealistic regression output such as values over 3:
>
> if( type == "HARRVJ" ){
> J = J[(maxp:(n-h)),];
> *x = cbind(x1,J);* # bind jumps to RV data
> if(!is.null(transform)){ y = Ftransform(y); x = Ftransform(x); }
> x = cbind(x,rmin);
> model = estimhar(y=y,x=x);
> model$transform = transform; model$h = h; model$type = "HARRVJ";
> model$dates = alldates[(maxp+h):n];
> class(model) = c("harModel","lm");
> return( model )
> }#End HAR-RV-J if cond
>
> to
>
> if( type == "HARRVJ" ){
> J = J[(maxp:(n-h)),];
> *x = cbind(x1,J+1); * # bind jumps to RV data
> if(!is.null(transform)){ y = Ftransform(y); x = Ftransform(x); }
> x = cbind(x,rmin);
> model = estimhar(y=y,x=x);
> model$transform = transform; model$h = h; model$type = "HARRVJ";
> model$dates = alldates[(maxp+h):n];
> class(model) = c("harModel","lm");
> return( model )
> }#End HAR-RV-J if cond
>
> and this
>
> if( type == "HARRVCJ" ){
> # Are the jumps significant? if not set to zero:
> if( jumptest=="ABDJumptest" ){
> TQ = apply.daily(data, TQfun);
> J = J[,1];
> teststats = ABDJumptest(RV=RM1,BPV=RM2,TQ=TQ );
> }else{ jtest = match.fun(jumptest); teststats = jtest(data,...) }
> Jindicators = teststats > qnorm(1-alpha);
> *J[!Jindicators] = 0;*
> to
> if( type == "HARRVCJ" ){
> # Are the jumps significant? if not set to zero:
> if( jumptest=="ABDJumptest" ){
> TQ = apply.daily(data, TQfun);
> J = J[,1];
> teststats = ABDJumptest(RV=RM1,BPV=RM2,TQ=TQ );
> }else{ jtest = match.fun(jumptest); teststats = jtest(data,...) }
> Jindicators = teststats > qnorm(1-alpha);
> * J[!Jindicators] = 1;*
>
> the full code is (which I take no credit for):
>
<snip>
>
> If anyone could help me with as to how J should be modified when the model
> is in log form it would be hugely appreciated, Thanks in advance.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Switching-log-J-to-log-J-1-to-avoid-log-0-in-HAR-RVJ-model-tp4637088.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list