19

I would calculate the first derivative (dpH/dtime) of time series using two variables, time and pH.

Are there any kind of functions to do this in R or should I compute an extra function to do this?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
alexmulo
  • 751
  • 4
  • 11
  • 23
  • `diff.ts` comes to mind. – Matthew Lundberg Dec 29 '12 at 14:23
  • 1
    or more crudely `diff(pH)/diff(time)`; it depends also whether you want to do some kind of smoothing. – Ben Bolker Dec 29 '12 at 14:37
  • I used the function diff, e.g. derivative <-diff(pH)/diff(time) but I get other values in comparison with the manual calculation with excel. In excel I made it so: (pH2-pH1)/(time2-time1). Why? – alexmulo Dec 29 '12 at 14:39
  • 1
    almost impossible to say without a reproducible example ( http://tinyurl.com/reproducible-000 ). Assuming that `pH1` is a lagged version of `pH2` and the same for `time1`/`time2`, your calculations *should* give the same result ... – Ben Bolker Dec 29 '12 at 15:37
  • you are right, I had a export problem in R. In excel the time difference was 0.16667 but I export the time serie only with one decimal number. Sorry for the mistake. – alexmulo Dec 29 '12 at 16:19
  • I think either we should close this question, or you should post a worked example showing what you did that worked (i.e., really just an example of `diff(pH)/diff(time)` ...) – Ben Bolker Dec 29 '12 at 23:46

3 Answers3

19

Assuming pH and time are plain vectors try this:

library(pspline)
predict(sm.spline(time, pH), time, 1)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
12

You might want to start with stats::deriv or diff.ts as Matt L suggested. Just keep in mind what a professor of mine used to tell all his students: numeric differentiation is known as "error multiplier."

EDIT: To clarify -- what he was warning about was that any noise in your data can throw the derivative estimate way off. It's been said that integration is a low-pass filter and differentiation is a high-pass filter. So, the important thing is to do some smoothing on your data before calculating a derivative. Hence Gabor's excellent suggestion to use predict.spline . But keep in mind that modifying the spline parameters will smooth your data to different levels, so always look at the results to make sure you removed apparent noise but not desired features.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
0

Here's a link to "Numerical Differentiation".

http://en.wikipedia.org/wiki/Numerical_differentiation

Here's a link describing a method based on Taylor Series Expansions:

http://ocw.usu.edu/civil_and_environmental_engineering/numerical_methods_in_civil_engineering/ODEsMatlab.pdf

bill_080
  • 4,692
  • 1
  • 23
  • 30
  • 1
    this is useful information but not, I think, relevant to the OP's question (which is about finding the derivative of time series data, not about finding/approximating the derivative of an explicitly defined function ...) – Ben Bolker Dec 29 '12 at 23:45
  • @BenBolker - You're right. `numDeriv` is not the right package. I can't find the right package at the moment (maybe I'm remembering a package from a different language?), but the technique is just standard Taylor Series methods. I'll replace the `numDeriv` link. – bill_080 Dec 30 '12 at 01:23