Calculate rolling slope and determining when the slope changes direction and above a threshold value

Hi,
I am trying to calculate slope for fixed intervals (for example every 20 minutes). The data is time series data every minute. First I created an xtsobject with the code below

xtsobject<-as.xts(b03_NA_time_clean_ts)
df_DHP<-data.frame(xtsobject[,"B03_DH-P"], Time=index(xtsobject))

First attempt of code to calculate rolling slope
datatest_2<-rollapply(zoo(df_DHP), width=50,
FUN=function(z)
{z=lm(formula = B03_DH.P~Time, data = df_DHP);
return(z$coef)}, by=1, by.column=FALSE, align='left')

Basically what I am trying to do is calculate slope every 20 minutes and then determine the time when the slope for every of these fixed intervals changes from negative to positive and changes above a particular threshold level. This threshold is an input and could be any number. This is shown in the figure below.An example of the data is below:


B03_DH.P Time
1/1/2019 0:00 1825.172974 1/1/2019 0:00
1/1/2019 0:01 1824.477661 1/1/2019 0:01
1/1/2019 0:02 1824.619141 1/1/2019 0:02
1/1/2019 0:03 1825.024658 1/1/2019 0:03
1/1/2019 0:04 1825.042603 1/1/2019 0:04
1/1/2019 0:05 1825.372314 1/1/2019 0:05
1/1/2019 0:06 1826.260864 1/1/2019 0:06
1/1/2019 0:07 1826.512085 1/1/2019 0:07
1/1/2019 0:08 1826.893433 1/1/2019 0:08
1/1/2019 0:09 1826.947021 1/1/2019 0:09
1/1/2019 0:10 1826.788208 1/1/2019 0:10
1/1/2019 0:11 1826.549561 1/1/2019 0:11
1/1/2019 0:12 1826.833618 1/1/2019 0:12
1/1/2019 0:13 1826.407715 1/1/2019 0:13
1/1/2019 0:14 1825.698975 1/1/2019 0:14
1/1/2019 0:15 1825.571533 1/1/2019 0:15
1/1/2019 0:16 1824.858887 1/1/2019 0:16
1/1/2019 0:17 1824.73291 1/1/2019 0:17
1/1/2019 0:18 1824.743408 1/1/2019 0:18
1/1/2019 0:19 1824.289429 1/1/2019 0:19
1/1/2019 0:20 1824.704834 1/1/2019 0:20
1/1/2019 0:21 1824.692993 1/1/2019 0:21
1/1/2019 0:22 1825.315918 1/1/2019 0:22
1/1/2019 0:23 1825.980713 1/1/2019 0:23
1/1/2019 0:24 1825.9823 1/1/2019 0:24
1/1/2019 0:25 1826.842896 1/1/2019 0:25
1/1/2019 0:26 1827.014282 1/1/2019 0:26
1/1/2019 0:27 1826.567627 1/1/2019 0:27
1/1/2019 0:28 1826.380005 1/1/2019 0:28
1/1/2019 0:29 1826.473389 1/1/2019 0:29
1/1/2019 0:30 1825.875244 1/1/2019 0:30
1/1/2019 0:31 1825.342651 1/1/2019 0:31
1/1/2019 0:32 1824.757202 1/1/2019 0:32
1/1/2019 0:33 1824.400269 1/1/2019 0:33
1/1/2019 0:34 1824.792114 1/1/2019 0:34
1/1/2019 0:35 1824.221924 1/1/2019 0:35
1/1/2019 0:36 1823.633423 1/1/2019 0:36
1/1/2019 0:37 1823.302734 1/1/2019 0:37
1/1/2019 0:38 1823.632446 1/1/2019 0:38
1/1/2019 0:39 1823.893311 1/1/2019 0:39
1/1/2019 0:40 1824.71582 1/1/2019 0:40
1/1/2019 0:41 1825.580078 1/1/2019 0:41
1/1/2019 0:42 1826.426392 1/1/2019 0:42
1/1/2019 0:43 1826.783081 1/1/2019 0:43
1/1/2019 0:44 1826.50293 1/1/2019 0:44
1/1/2019 0:45 1826.534302 1/1/2019 0:45
1/1/2019 0:46 1825.882568 1/1/2019 0:46
1/1/2019 0:47 1825.71936 1/1/2019 0:47
1/1/2019 0:48 1825.538086 1/1/2019 0:48
1/1/2019 0:49 1825.326172 1/1/2019 0:49
1/1/2019 0:50 1824.711914 1/1/2019 0:50
1/1/2019 0:51 1824.138062 1/1/2019 0:51
1/1/2019 0:52 1824.055664 1/1/2019 0:52
1/1/2019 0:53 1823.755981 1/1/2019 0:53
1/1/2019 0:54 1824.174561 1/1/2019 0:54
1/1/2019 0:55 1824.211792 1/1/2019 0:55
1/1/2019 0:56 1823.881592 1/1/2019 0:56
1/1/2019 0:57 1824.325562 1/1/2019 0:57
1/1/2019 0:58 1825.561646 1/1/2019 0:58
1/1/2019 0:59 1825.561646 1/1/2019 0:59
1/1/2019 1:00 1825.561646 1/1/2019 1:00
1/1/2019 1:01 1825.561646 1/1/2019 1:01
1/1/2019 1:02 1825.561646 1/1/2019 1:02
1/1/2019 1:03 1825.561646 1/1/2019 1:03
1/1/2019 1:04 1825.561646 1/1/2019 1:04
1/1/2019 1:05 1825.561646 1/1/2019 1:05
1/1/2019 1:06 1825.561646 1/1/2019 1:06
1/1/2019 1:07 1825.561646 1/1/2019 1:07
1/1/2019 1:08 1825.561646 1/1/2019 1:08
1/1/2019 1:09 1825.561646 1/1/2019 1:09
1/1/2019 1:10 1825.561646 1/1/2019 1:10
1/1/2019 1:11 1825.561646 1/1/2019 1:11
1/1/2019 1:12 1825.561646 1/1/2019 1:12
1/1/2019 1:13 1825.561646 1/1/2019 1:13
1/1/2019 1:14 1825.561646 1/1/2019 1:14
1/1/2019 1:15 1825.561646 1/1/2019 1:15
1/1/2019 1:16 1825.561646 1/1/2019 1:16
1/1/2019 1:17 1825.561646 1/1/2019 1:17
1/1/2019 1:18 1825.561646 1/1/2019 1:18
1/1/2019 1:19 1825.561646 1/1/2019 1:19
1/1/2019 1:20 1825.561646 1/1/2019 1:20
1/1/2019 1:21 1825.561646 1/1/2019 1:21
1/1/2019 1:22 1825.561646 1/1/2019 1:22
1/1/2019 1:23 1825.561646 1/1/2019 1:23
1/1/2019 1:24 1825.561646 1/1/2019 1:24
1/1/2019 1:25 1825.561646 1/1/2019 1:25
1/1/2019 1:26 1825.561646 1/1/2019 1:26
1/1/2019 1:27 1825.561646 1/1/2019 1:27
1/1/2019 1:28 1825.561646 1/1/2019 1:28
1/1/2019 1:29 1825.561646 1/1/2019 1:29
1/1/2019 1:30 1825.561646 1/1/2019 1:30
1/1/2019 1:31 1825.561646 1/1/2019 1:31
1/1/2019 1:32 1825.561646 1/1/2019 1:32
1/1/2019 1:33 1825.561646 1/1/2019 1:33
1/1/2019 1:34 1825.561646 1/1/2019 1:34
1/1/2019 1:35 1825.561646 1/1/2019 1:35

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.