What functions exist for differentiating a numeric vector (in my case
spectral data)? That is, experimental data without an analytical
function. ie,
x <- seq(1,10,0.1)
y=x^3+rnorm(length(x),sd=0.01) #although the real function would be nothing simple like x^3...
derivy <- ....
I know I could just use diff(y) but it would be nice to estimate
derivatives at the endpoints, calculate higher-order derivatives, and
maybe have some smoothing options ie by differentiating a spline or
something like that.
Have you looked at the 'fda' package? It has many functions for
doing what you want. A strength is that it is a companion package for
two books on that and related issues, and includes script files under
"~R.installation.directory\library\fda\scripts" to reproduce some of the
analyses. It may include more than you want to consider, but for me,
too much is usually better than nothing.
hope this helps.
Spencer Graves
p.s. If you try it and have trouble, please submit another question
including commented, minimal, self-contained, reproducible code, as
requested in the posting guide
http://www.R-project.org/posting-guide.html.
Levi Waldron wrote:
What functions exist for differentiating a numeric vector (in my case
spectral data)? That is, experimental data without an analytical
function. ie,
x <- seq(1,10,0.1)
y=x^3+rnorm(length(x),sd=0.01) #although the real function would be nothing simple like x^3...
derivy <- ....
I know I could just use diff(y) but it would be nice to estimate
derivatives at the endpoints, calculate higher-order derivatives, and
maybe have some smoothing options ie by differentiating a spline or
something like that.
On Wed, Mar 5, 2008 at 3:18 PM, Levi Waldron <leviwaldron at gmail.com> wrote:
What functions exist for differentiating a numeric vector (in my case
spectral data)? That is, experimental data without an analytical
function. ie,
x <- seq(1,10,0.1)
y=x^3+rnorm(length(x),sd=0.01) #although the real function would be nothing simple like x^3...
derivy <- ....
I know I could just use diff(y) but it would be nice to estimate
derivatives at the endpoints, calculate higher-order derivatives, and
maybe have some smoothing options ie by differentiating a spline or
something like that.
Hi,
Here is another approach, in addition to the suggestions made by Spencer and
Gabor. It uses the spm() function in SemiPar package. An advantage of this
approach is that the smoothing parameter is automatically estimated using
REML (here I use default knot locations, but this can be specified
explicitly).
You also need to source in the plot.spm() function that I have created by
slightly modifying the plot.spm() that comes with SemiPar. This is
necessary to obtain numerical values of derivatives at x-locations in
addition to simply plotting the derivative curves.
library(SemiPar)
source("plotspm.r")
# An example
k <- 10
x <- sqrt(runif(500))
y <- pnorm(x) + sin(k*pi*x^2) + rnorm(500,mean=0,sd=0.5)
fit<-spm(y ~ f(x, basis="trunc.poly", degree=3), omit.missing=TRUE)
deriv <- plot(fit, drv=1) # plot and store first derivative
deriv2 <- plot(fit, drv=2) # plot and store second derivative
Hope this helps,
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Spencer Graves
Sent: Wednesday, March 05, 2008 4:11 PM
To: Levi Waldron
Cc: R-help mailing list
Subject: Re: [R] differentiating a numeric vector
Have you looked at the 'fda' package? It has many functions for
doing what you want. A strength is that it is a companion package for
two books on that and related issues, and includes script files under
"~R.installation.directory\library\fda\scripts" to reproduce some of the
analyses. It may include more than you want to consider, but for me,
too much is usually better than nothing.
hope this helps.
Spencer Graves
p.s. If you try it and have trouble, please submit another question
including commented, minimal, self-contained, reproducible code, as
requested in the posting guide
http://www.R-project.org/posting-guide.html.
Levi Waldron wrote:
What functions exist for differentiating a numeric vector (in my case
spectral data)? That is, experimental data without an analytical
function. ie,
x <- seq(1,10,0.1)
y=x^3+rnorm(length(x),sd=0.01) #although the real function would be
nothing simple like x^3...
derivy <- ....
I know I could just use diff(y) but it would be nice to estimate
derivatives at the endpoints, calculate higher-order derivatives, and
maybe have some smoothing options ie by differentiating a spline or
something like that.
Hi,
If you didn't receive the attachment properly, here it is again.
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Ravi Varadhan
Sent: Wednesday, March 05, 2008 4:58 PM
To: 'Spencer Graves'; 'Levi Waldron'
Cc: 'R-help mailing list'
Subject: Re: [R] differentiating a numeric vector
Hi,
Here is another approach, in addition to the suggestions made by Spencer and
Gabor. It uses the spm() function in SemiPar package. An advantage of this
approach is that the smoothing parameter is automatically estimated using
REML (here I use default knot locations, but this can be specified
explicitly).
You also need to source in the plot.spm() function that I have created by
slightly modifying the plot.spm() that comes with SemiPar. This is
necessary to obtain numerical values of derivatives at x-locations in
addition to simply plotting the derivative curves.
library(SemiPar)
source("plotspm.r")
# An example
k <- 10
x <- sqrt(runif(500))
y <- pnorm(x) + sin(k*pi*x^2) + rnorm(500,mean=0,sd=0.5)
fit<-spm(y ~ f(x, basis="trunc.poly", degree=3), omit.missing=TRUE)
deriv <- plot(fit, drv=1) # plot and store first derivative
deriv2 <- plot(fit, drv=2) # plot and store second derivative
Hope this helps,
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Spencer Graves
Sent: Wednesday, March 05, 2008 4:11 PM
To: Levi Waldron
Cc: R-help mailing list
Subject: Re: [R] differentiating a numeric vector
Have you looked at the 'fda' package? It has many functions for
doing what you want. A strength is that it is a companion package for
two books on that and related issues, and includes script files under
"~R.installation.directory\library\fda\scripts" to reproduce some of the
analyses. It may include more than you want to consider, but for me,
too much is usually better than nothing.
hope this helps.
Spencer Graves
p.s. If you try it and have trouble, please submit another question
including commented, minimal, self-contained, reproducible code, as
requested in the posting guide
http://www.R-project.org/posting-guide.html.
Levi Waldron wrote:
What functions exist for differentiating a numeric vector (in my case
spectral data)? That is, experimental data without an analytical
function. ie,
x <- seq(1,10,0.1)
y=x^3+rnorm(length(x),sd=0.01) #although the real function would be
nothing simple like x^3...
derivy <- ....
I know I could just use diff(y) but it would be nice to estimate
derivatives at the endpoints, calculate higher-order derivatives, and
maybe have some smoothing options ie by differentiating a spline or
something like that.
Thanks for all the ideas. splinefun looks like the simplest way to
achieve what I need:
x <- 1:10
y <- x^2
f <- splinefun(x,y)
f(3,deriv=0)
[1] 9
f(3,deriv=1)
[1] 6
f(3,2)
[1] 2
f(3,3)
[1] -3.330669e-16
The fda package has a function bsplineS which does nearly the same
thing, and the SemiPar solution looks good for irregular x and y
values, but the built-in function seems to work for my purposes.