[R] About uniroot error

J C Nash pro|jcn@@h @end|ng |rom gm@||@com
Mon Sep 24 19:46:57 CEST 2018


uniroot REQUIRES that the function be of opposite sign at each end
of the starting interval.

I won't address the other issues raised, but you can use simple stepping
from a starting argument until a sign change occurs. Or you could try a
different type of rootfinder, such as newtonRaphson in package pracma.

There's some discussion in the (still in draft) vignette

https://gitlab.com/nashjc/histoRicalg/blob/master/provenance-of-rootfinding/rootfinder.pdf

by Oliver Dechant and myself as part of an investigation of "older"
codes.

JN


On 2018-09-24 01:37 PM, Tania Morgado Garcia wrote:
> Thanks for your answers. I continue to learn R and now I am detained in an
> error with uniroot that I see happens to others but I can not find the
> solution. Next the code
> 
> x1 <- BAaxOrd$V1
> y1 <- BAaxOrd$V2
> x1R <- BAaxOrdRCOS$V1
> y1R <- BAaxOrdRCOS$V2
> FCOS1 <- splinefun(smooth.spline(x1,y1))
> FRCOS1 <- splinefun(smooth.spline(x1R,y1R))
> FCOS1 <- Vectorize(FCOS1)
> FRCOS1 <- Vectorize(FRCOS1)
> 
> req(input$file1)
>       tryCatch(
>       {
>         df <- read.csv(input$file1$datapath,
>                        header = input$header,
>                        sep = "\t",
>                        quote = '"')
>       },
>       error = function(e) {
>         # return a safeError if a parsing error occurs
>         stop(safeError(e))
>       }
>     )
> 
>     #if(input$disp == "head") {
>      # return(head(df))
>     #}
>     #else {
> 
>       # Determine Carbon Reserve
>       for (row in 1:nrow(df)) {
>         if(df$ts==1) {
>            prof <-
> uniroot(f=function(x){FCOS1(x1)-df$carbono},interval=c(0,20))$root
>            limsup <- prof + df$pu
>            reserva <- integrate(FRCOS1,prof,limsup)$value
>         }
> 
> The if is because there are several types of soil, but I only put one.  The
> error is
> 
> Warning in if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
>   the condition has length > 1 and only the first element will be used
> Warning in if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
>   the condition has length > 1 and only the first element will be used
> Warning: Error in uniroot: f() values at end points not of opposite sign
> 
> The file that I load with data has a single row with the values ts = 1,
> carbon = 2.04 and pu = 15 (I left only that row to be able to determine the
> origin of the error). The functions FCOS1 and FRCOS1 are monotone
> decreasing.Graphic attachment of FCOS1
> 
> I would appreciate some help in this regard
> 
> thanks a lot
> ______________________________________________
> R-help using 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.
>




More information about the R-help mailing list