[R] how to get higher derivatives with "deriv"
peter dalgaard
pdalgd at gmail.com
Fri Jul 30 17:02:00 CEST 2010
On Jul 30, 2010, at 4:28 PM, Ravi Varadhan wrote:
> I am not as lazy as Peter (but neither am I as good as him)! So, please
> allow me to take a stab at completing his exercise:
>
> Deriv <- function(fn, order=1) {
> ddf <- fn
> body(ddf) <- D(body(f),"x")
### Typo: body(ddf) or body(fn)
> if (order > 1) {
> for (i in 2:order) {
> body(ddf) <- D(body(ddf),"x")
> }
> }
> ddf
> }
Yes.
One thing: body() and body<-() are relatively expensive operations, and only really needed on the way in and the way out, so you might want to use a temporary variable.
--
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list