I am attaching a function that I have written sometime ago for extracting some of the features of a noisy, discretely sampled times series. In order to extract the features, I first estimate the smoothed function and smoothed (first and second order) derivatives.
Here is a simple demo of how to use it: source("h:/features.R") n <- 200 x <- sort(runif(n)) y <- exp(-0.2 * sin(2*pi*x)) + rnorm(n, sd=0.05) ans <- features(x, y, fits.return=TRUE, control=list(plot.it=TRUE)) fits <- attr(ans, "fits") plot(fits$x, fits$fits1, type="l", xlab="x", ylab="First derivative") yexact <- -0.2 * 2*pi * cos(2*pi*x) * exp(-0.2 * sin(2*pi*x)) lines(x, yexact, col=2) # Similarly, you can also look at the smoothed second derivative Hope this is helpful, 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: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of nandan amar Sent: Friday, May 27, 2011 10:44 AM To: r-help; Dennis Murphy Subject: Re: [R] finding derivative of a data series in R However if we have some discrete data set like daily temperature etc how can we can differentiate it because as.expression(D()) will not work then On 27 May 2011 16:52, nandan amar <nandan.a...@gmail.com> wrote: > Thanks Dennis. > I got you. > My main consern was how to differentiate a series correctly. > I think the first procedure is more accurate. > Regards. > > On 27 May 2011 16:27, Dennis Murphy <djmu...@gmail.com> wrote: >> Hi: >> >> A function and its spline approximation are not equivalent functions, >> hence neither are their corresponding derivatives. I modified an >> example from the splinefun() help page to illustrate this. >> >> op <- par(mfrow = c(2, 1)) >> x <- seq(1, 9, by = 0.01) >> # u is the true function, u1-u3 are its symbolic derivatives wrt x >> # u1 = f', u2 = f'', u3 = f''' >> u <- expression(sin(pi * (x - 0.5))) >> u1 <- as.expression(D(u, 'x')) >> u2 <- as.expression(D(u1, 'x')) >> u3 <- as.expression(D(u2, 'x')) >> plot(x, eval(u), type = 'l', ylim = c(-30, 30), >> ylab = "", xlab = "", >> main = expression(f(x) == sin(pi * (x - 0.5)))) >> lines(x, eval(u1), type = 'l', col = 'red') >> lines(x, eval(u2), type = 'l', col = 'blue') >> lines(x, eval(u3), type = 'l', col = 'green') >> # legend('topright', legend = c("f", "f'", "f''", "f'''"), >> # col = c('black', 'red', 'blue', 'green'), lwd = 2) >> >> # y2 is an evaluation of u at each x point >> y2 <- sin((x-0.5)*pi) >> # Construct the spline function approximation to u >> f <- splinefun(x, y2) >> # Plot the interpolation function and its first three derivatives >> curve(f(x), 1, 10, col = "black", lwd = 1.5, ylim = c(-30, 30), >> ylab = "", main = 'Spline interpolation of f') >> curve(f(x, deriv=1), 1, 10, col = 'red', lwd = 1.5, add = TRUE) >> curve(f(x, deriv=2), 1, 10, col = 'blue', lwd = 1.5, add = TRUE) >> curve(f(x, deriv=3), 1, 10, col = 'green', lwd = 1.5, add = TRUE) >> par(op) >> >> Notice that the peaks and troughs of the derivatives of the spline >> approximation are not at the same x locations as in the original >> function. Also notice the linearity in the derivatives when x is >> between 9 and 10. >> >> I suppose you could improve the approximations by setting some knot >> points, but I don't have the time to chase down that hypothesis for >> you right now. I'll leave that as homework :) >> >> Your example is simpler since it is polynomial, but the concept is the >> same: the derivative of the interpolator shouldn't necessarily match >> the derivative of the function exactly. Obviously, though, you want >> them to be close. >> >> HTH, >> Dennis >> >> On Fri, May 27, 2011 at 1:23 AM, nandan amar <nandan.a...@gmail.com> wrote: >>> Dear All, >>> I tried following for getting derivative of a polynomial in R >>> >>> i<- -10:10 >>> x<-i*i*i+3*i*i+2 >>> fun_spline<-splinefun(i,x) >>> plot(x,type="l") >>> lines(x,fx_spline(x, deriv=1), col='green') >>> lines(x,fx_spline(x, deriv=2), col='green') >>> >>> Now when I plot >>> 3*i*i + 6*i and 6*i + 6 >>> the plot was not same for first deivative. >>> where as the 2nd derivative was same >>> >>> Is this a correct method for getting derivative. >>> where am I doing wrong as first derivative lines(x,fx_spline(x, >>> deriv=1), col='green') is not correct. >>> >>> amar >>> -- >>> Amar Kumar Nandan >>> >>> ______________________________________________ >>> 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. >>> >> > > > > -- > Amar Kumar Nandan > Karnataka, India, 560100 > http://aknandan.co.nr > -- Amar Kumar Nandan Karnataka, India, 560100 http://aknandan.co.nr ______________________________________________ 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.
______________________________________________ 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.