# Numerical Derivatives in R

11 messages
Open this post in threaded view
|

## Numerical Derivatives in R

 Hi, Suppose I have an arbitrary function: arbfun<-function(x) {...} Is there a robust implementation of a numerical derivative routine in R which I can use to take it's derivative ? Something a bit more than simple division by delta of the difference of evaluating the function at x and x+delta... Perhaps there is a way to do this using D or deriv but I could not figure it out. Trying: eval(deriv(function(x) arbfun(x),"x"),1) does not seem to work. Thanks, Tolga ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Open this post in threaded view
|

## Re: Numerical Derivatives in R

 Tolga, Look at numericDeriv. > arbfun <- function(x) x^2 > x <- 3 > numericDeriv(quote(arbfun(x)), "x") [1] 9 attr(,"gradient")      [,1] [1,]    6 --Gray On 3/12/06, Tolga Uzuner <[hidden email]> wrote: > Hi, > > Suppose I have an arbitrary function: > > arbfun<-function(x) {...} > > Is there a robust implementation of a numerical derivative routine in R > which I can use to take it's derivative ? Something a bit more than > simple division by delta of the difference of evaluating the function at > x and x+delta... > > Perhaps there is a way to do this using D or deriv but I could not > figure it out. Trying: > > eval(deriv(function(x) arbfun(x),"x"),1) > > does not seem to work. > > Thanks, > Tolga > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html> -- Gray Calhoun Economics Department UC San Diego ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Open this post in threaded view
|

## Re: Numerical Derivatives in R

 Thanks Gray, Tolga Gray Calhoun wrote: >Tolga, > >Look at numericDeriv. > >   > >>arbfun <- function(x) x^2 >>x <- 3 >>numericDeriv(quote(arbfun(x)), "x") >>     >> >[1] 9 >attr(,"gradient") >     [,1] >[1,]    6 > >--Gray > >On 3/12/06, Tolga Uzuner <[hidden email]> wrote: >   > >>Hi, >> >>Suppose I have an arbitrary function: >> >>arbfun<-function(x) {...} >> >>Is there a robust implementation of a numerical derivative routine in R >>which I can use to take it's derivative ? Something a bit more than >>simple division by delta of the difference of evaluating the function at >>x and x+delta... >> >>Perhaps there is a way to do this using D or deriv but I could not >>figure it out. Trying: >> >>eval(deriv(function(x) arbfun(x),"x"),1) >> >>does not seem to work. >> >>Thanks, >>Tolga >> >>______________________________________________ >>[hidden email] mailing list >>https://stat.ethz.ch/mailman/listinfo/r-help>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html>> >>     >> > > >-- >Gray Calhoun > >Economics Department >UC San Diego > >   > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Open this post in threaded view
|

## Re: Numerical Derivatives in R

 In reply to this post by Gray Calhoun "Gray Calhoun" <[hidden email]> writes: > Tolga, > > Look at numericDeriv. > > > arbfun <- function(x) x^2 > > x <- 3 > > numericDeriv(quote(arbfun(x)), "x") > [1] 9 > attr(,"gradient") >      [,1] > [1,]    6 However, numericDeriv is not particularly intelligent. It is effectively doing what Tolga was trying not to. A more refined function could be a good idea, e.g. implementing higher order approximations, a tunable stepsize, box constraints...   > --Gray > > On 3/12/06, Tolga Uzuner <[hidden email]> wrote: > > Hi, > > > > Suppose I have an arbitrary function: > > > > arbfun<-function(x) {...} > > > > Is there a robust implementation of a numerical derivative routine in R > > which I can use to take it's derivative ? Something a bit more than > > simple division by delta of the difference of evaluating the function at > > x and x+delta... > > > > Perhaps there is a way to do this using D or deriv but I could not > > figure it out. Trying: > > > > eval(deriv(function(x) arbfun(x),"x"),1) > > > > does not seem to work. > > > > Thanks, > > Tolga > > > > ______________________________________________ > > [hidden email] mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html> > > > > -- > Gray Calhoun > > Economics Department > UC San Diego > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html> --    O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K  (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918 ~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Open this post in threaded view
|

## Re: Numerical Derivatives in R

Open this post in threaded view
|

## Re: Numerical Derivatives in R

Open this post in threaded view
|

## Re: Numerical Derivatives in R

Open this post in threaded view
|

## Re: Numerical Derivatives in R

Open this post in threaded view
|