The "fda" package includes various implementations for smoothing data and differentiating the smooth, per Ravi's alternative approach. This is generally preferable to using first differences of raw data, because differencing raw data amplifies noise, while appropriate smooths eliminate much of the noise, leaving you with what you most want. Ramsay and Silverman (2005) Functional Data Analysis, 2nd ed. (Springer) suggest that if you want a second derivative, it is often wise to use quintic splines, because then the second derivative are cubic splines. (The first derivative of a spline of order k is a spline of order k-1.) An example is provided in Figure 1.2 of Ramsay, Hooker and Graves (2009) Functional Data Analysis with R and Matlab (Springer).

However, you don't have to get to book to see that example. Just work through the script file "fdarm-ch01.R" in system.file('scripts', package='fda') on any computer with the 'fda' package installed.


      Hope this helps.
      Spencer


On 11/22/2010 7:49 PM, Ravi Varadhan wrote:
Here is a simple approach:

data<- read.table("test-data.txt")

deriv<- diff(data$V2) / diff(data$V1)

times<- (data$V1[-1] + data$V1[-3545])/2

plot(times, deriv, type="l")

Another approach is to smooth the original data and then obtain derivatives 
from the smooth

Ravi.

____________________________________________________________________

Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University

Ph. (410) 502-2619
email: rvarad...@jhmi.edu


----- Original Message -----
From: Yogesh Tiwari<yogesh....@googlemail.com>
Date: Monday, November 22, 2010 5:14 pm
Subject: [R] how to calculate derivative
To: r-help<r-h...@stat.math.ethz.ch>


Dear R Users,

  I have trend of two time series of CO2 each 10  years of data. One is
  varying
  weekly and another is bi-weekly. I want to calculate Growth rate ppmv
/ year
  of these CO2 trends. Therefore I want to calculate  time derivative
ppmv /
  year.

  How to do it in R?

  Here I attached example data file, I would appreciate if any one
kindly can
  help on it.

  Thanks,

  Regards,
  Yogesh
______________________________________________
  R-help@r-project.org mailing list

  PLEASE do read the posting guide
  and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
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.



--
Spencer Graves, PE, PhD
President and Chief Operating Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567

______________________________________________
R-help@r-project.org mailing list
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 via email to