About uniroot error

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

About uniroot error

ttmm
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
______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: About uniroot error

J C Nash
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
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: About uniroot error

Michael Dewey-3
In reply to this post by ttmm
Dear Tania

Without your dataset I am not sure but a comment below to suggest where
to look next.

On 24/09/2018 18:37, 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
>          }

Are you sure that FCOS(x1) - df$carbono returns a scalar? It looks as
though it returns a vector to me but without your data I am not sure so
ignore my post if it does.

>
> 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
> ______________________________________________
> [hidden email] 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.
>

--
Michael
http://www.dewey.myzen.co.uk/home.html

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: About uniroot error

ttmm
Thanks for your reply. You're right, return a vector. An example of the
dataset:
ts pu carbono
1 15 2.04
1 37 1.27
1 55 0.93
1 80 0.5
1 105 0.49
1 22 2.08
1 41 1.43
1 65 0.78

The data that originate the FCOS and FRCOS pattern functions are others,
which I only use to obtain the functions.

Thanks again

El mar., 25 sept. 2018 a las 10:29, Michael Dewey (<[hidden email]>)
escribió:

> Dear Tania
>
> Without your dataset I am not sure but a comment below to suggest where
> to look next.
>
> On 24/09/2018 18:37, 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
> >          }
>
> Are you sure that FCOS(x1) - df$carbono returns a scalar? It looks as
> though it returns a vector to me but without your data I am not sure so
> ignore my post if it does.
> >
> > 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
> > ______________________________________________
> > [hidden email] 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.
> >
>
> --
> Michael
> http://www.dewey.myzen.co.uk/home.html
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.