[R] Change log(J) to log(J+1) to stop log(0) from occurring in harModel

Joshua Ulrich josh.m.ulrich at gmail.com
Thu Jul 19 22:24:28 CEST 2012


Same post on Stack Overflow (again):
http://stackoverflow.com/q/11567745/271616
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com


On Thu, Jul 19, 2012 at 11:15 AM, cursethiscure
<caolan.harvey6 at mail.dcu.ie> wrote:
> I think the code is part of the RTAQ package but is not included in it, as I
> obtained it from
> https://r-forge.r-project.org/scm/viewvc.php/pkg/RTAQ/R/HAR_model.R?view=markup&root=blotter&sortby=author&pathrev=1028.
>
> It is not my code and I make no claim to other's good work, and apologize if
> I should even be posting it I am not sure, but in the transform function it
> allows to change the model to `log` or `sqrt`, but when then model is
> changed to log and the model used is either "HARRVJ" or "HARRVCJ" it will
> return the following error:
>
> 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 model should take log(J +
> 1) in case of a 0 value for the J in the time series, 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.
>
>
<snip>
>
>  I have tried changing
>
>   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;*
>
> but it returns to large of scores when the regressions are run. Note I would
> change it back to how it is for `transform=NULL` and `transform="sqrt" as
> they compute fine for this.
>
> I don't think the log tests can work without the change though.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Change-log-J-to-log-J-1-to-stop-log-0-from-occurring-in-harModel-tp4637072.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