The 'error' argument to tryCatch must be a function (of one argument,
the exception).  Its return value will be the return value of tryCatch
if there is an error.  Hence you should change the present
    error = return(TBAPrice)
to
    error = function(exception) TBAPrice

You can use
    error = function(exception) return(TBAPrice)
if you prefer to use explicit return statements.

You could also examine conditionMessage(exception) or class(exception)
to see if it is the error you expect.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Thu, Jan 19, 2017 at 8:39 AM, Glenn Schultz <glennmschu...@me.com> wrote:
> 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)
> }
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to