0

I have a smoothed time series and want to find the instantaneous velocity of the function at any point along the line.

What I want to do is take a series of values: ex(1,6,5,4,3,5,6,7,1) and return the derivative of each relative to the function of the entire series, such that at every point in time, I know what direction the line is trending.

I am new to R, but know there must be a way.

Any tips?

Ex:

library(smoother)
data(BJsales)
m <- data.frame(BJsales)

x.smth <- as.data.frame(smth.gaussian(m$BJsales,tails=TRUE,alpha = 5))

x.smth.ts <- cbind(seq(1:nrow(m)),x.smth)

colnames(x.smth.ts) <- c("x","y")

x.smth.ts

plot(x.smth.ts$y~x.smth.ts$x)

Desired output:

df with 2 columns: x, deriv.of.y

Edit: Final Result thanks to G5W

TS with Color by Derivative

UBHDNNX
  • 61
  • 1
  • 9
  • 1
    Do you just have a single set of observations? Are they taken at a fixed interval? How do you want to model the change between observations? Do you want to fit your data to some model first? Otherwise what assumptions do you want to make to calculate a derivative? Your data should be smooth; the example is not. Do you know how to do this in languages other than R? – MrFlick Nov 01 '18 at 20:49
  • The example I provided was not smooth, but my data is. The data I have are gaussian-esque and are continuous at every point along the line. The end result I want is to color the line by +/-/0 derivative, such that greens are climbs, red's are descents, etc. All I need to do this is the derivative of the line at each x value, which I suppose means I need to find the equation of the entire line, then evaluate at each point. Not sure how to do so... and no, R is my first attempt at programming, though I suppose I can do it by hand. – UBHDNNX Nov 01 '18 at 20:55
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 01 '18 at 20:56
  • So using data similar to that here: (link)https://fred.stlouisfed.org/series/PHOE004UR smooth using any number of R functions (smooth,supsmu,smoother)... and find derivative for every x value such that you end up with 2 columns, x and derivative – UBHDNNX Nov 01 '18 at 21:01
  • 1
    Please provide some reproducible data in the question, and not as a link. Links go bad, at which point the question and answers become unreproducible. If you can do something like pasting the output from `dput(x)`, where `x` is a *representative* sample of your data but not too large, that would help. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Nov 01 '18 at 21:02
  • Do the edits help? Thanks for all the feedback. – UBHDNNX Nov 01 '18 at 21:20

1 Answers1

2

Your proposed example using the BJSales data is decidedly not differentiable, BJSales

so instead I will show the derivative of a much smoother function. If your real data is smooth, this should work for you.

The simplest way to approximate the derivative is simply to use finite differences.
f'(x) ≈ (f(x+h) - f(x))/h

## Smooth sample function
x = seq(0,10,0.1)
y = x/2 + sin(x)
plot(x,y, pch=20)

## Simplest - first difference
d1 = diff(y)/diff(x)
d1 = c(d1[1],d1)

Let's use it to plot a tangent line as an error check. I picked a place to draw the tangent line arbitrarily: the 18th point, x=1.7

plot(x,y, type="l")
abline(y[18]-x[18]*d1[18], d1[18]) 

Tangent Line

To get the data.frame that you requested, you just need

Derivative = data.frame(x, d1)
G5W
  • 36,531
  • 10
  • 47
  • 80