Calculate the derivative of a data-function in r

前端 未结 4 1714
忘了有多久
忘了有多久 2020-12-17 05:08

Is there an easy way to calculate the derivative of non-liner functions that are give by data?

for example:

x = 1 / c(1000:1)

y = x^-1.5
ycs = cumsu         


        
4条回答
  •  一生所求
    2020-12-17 05:35

    The derivative of a function is dy/dx, which can be approximated by Δy/Δx, that is, "change in y over change in x". This can be written in R as

    ycs.prime <- diff(ycs)/diff(x)
    

    and now ycs.prime contains an approximation to the derivative of the function at each x: however it is a vector of length 999, so you will need to shorten x (i.e. use x[1:999] or x[2:1000]) when doing any analysis or plotting.

提交回复
热议问题