[R] help using tryCatch

Glenn Schultz glennmschultz at me.com
Thu Jan 19 17:39:50 CET 2017


All,
I have  a function that I would like to return 0 or NA on unit root error but I cannot figure out how to get tryCatch to work.  I have followed the examples in the R documentation as well as some online but I am missing something.  If I added the tryCatch code I only get the error value.  Any help is appreciated

Glenn

FindATOMSPrice <- function(StartPrice,
TBAPrice,
PriceInterval,
CurrFactor,
PrevFactor,
CarryAdj,
PDReturn,
CPNReturn,
MTDReturn){
ATOMSReturn <- function(ClosePrice,
StartPrice,
CurrFactor,
PrevFactor,
PDReturn, 
CPNReturn, 
MTDReturn){
SurvivalFactor = CurrFactor/PrevFactor
PrxReturn = ((ClosePrice - StartPrice)/StartPrice) * SurvivalFactor
ATOMSReturn = sum(PrxReturn, PDReturn, CPNReturn)
return(ATOMSReturn - MTDReturn)
}
TBALow = TBAPrice - PriceInterval
TBAHigh = TBAPrice + PriceInterval

CleanPrice = 
#tryCatch({
uniroot(ATOMSReturn,
interval = c(TBALow, TBAHigh),
tol = .000000001,
StartPrice = StartPrice,
CurrFactor = CurrFactor,
PrevFactor = PrevFactor,
PDReturn = PDReturn,
CPNReturn = CPNReturn,
MTDReturn = MTDReturn)$root
#}, error = return(TBAPrice))
return(CleanPrice - CarryAdj)
}


More information about the R-help mailing list