[R] how to calculate derivative

Spencer Graves spencer.graves at structuremonitoring.com
Tue Nov 23 05:23:29 CET 2010

       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.

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: rvaradhan at jhmi.edu
> ----- Original Message -----
> From: Yogesh Tiwari<yogesh.mpi at googlemail.com>
> Date: Monday, November 22, 2010 5:14 pm
> Subject: [R] how to calculate derivative
> To: r-help<r-help at 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 at r-project.org mailing list
>>   PLEASE do read the posting guide
>>   and provide commented, minimal, self-contained, reproducible code.
> ______________________________________________
> R-help at 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

More information about the R-help mailing list