Derivative of a set of points

So I know you can find the derivative of something like: "x^3-6*x^2" by doing: D(expression(x^3-6*x^2), 'x'), but what if I need to find the first derivative maximum of a list of values such as:

value <- c(610,618,627,632,628,634,634,628,634,642,637,643,653,666,684,717,787,923,1197,1716,2638,4077,5461,7007,8561,9994,11278,12382,13382,14252)

these values are the y coordinate and the x coordinate starts at 1 and increments by 1. IE the first point is (1,610) second is (2,618) etc. -Thanks

Answers


Consider using the package numDerive from CRAN. It has a function grad that computes derivative of a function at a point. Example:

f = function(x) x^3 - 6*x^2
library(numDeriv)
grad(f, 1) #derivative of f at x=1

To solve your problem with a list of values, use a for loop:

xval <- c(YOUR VALUES HERE)
xval.derivatives <- c() #empty vector to hold
for(i in 1:length(xval)) xval.derivatives[i] <- grad(f,xval[i])

The gradient function from the pracma package calculates the derivative from a vector of values.

library(pracma)

value <- c(610,618,627,632,628,634,634,628,634,642,637,643,653,666,684,717,787,923,1197,1716,2638,4077,5461,7007,8561,9994,11278,12382,13382,14252)
value_prime <- pracma::gradient(value, h1 = 1)
plot(value_prime)

Alternatively, fit a spline.

spl <- smooth.spline(1:length(value), y=value)
pred <- predict(spl)
pred.prime <- predict(spl, deriv=1)
plot(pred.prime, type = 'b')

If you are interested in higher derivatives, check the pspline package.


Need Your Help

Thinking sphinx doesn't start - "Failed to start searchd daemon"

ruby-on-rails-3.1 sphinx thinking-sphinx ruby-1.9.3

I try to start thinking sphinx on my server but it doesn't want to work.