Consecutive exceedance above a threshold and addit

2019-07-29 13:08发布

问题:

I would like to get the time step in a time series satisfying the following conditions using R (should be the first time step satisfying the following conditions):

[1] V1 > 0 at the time step
[2] V1 > 0 in at least 3 consecutive time steps from the timestep obtained in [1]
[3] Accumulated value of the next four timesteps following [1] should be greater than 1.

Here's the data

structure(list(V1 = c(-3.85326, -2.88262, -4.1405, -3.95193, 
-6.68925, -2.04202, -2.47597, -4.91161, -2.5946, -2.82873, 2.68839, 
-4.1287, -4.50296, -0.143476, -1.12174, -0.756168, -1.67556, 
-1.92704, -1.89279, -2.37569, -5.71746, -2.7247, -4.12986, -2.29769, 
-1.52835, -2.63623, -2.31461, 2.32796, 4.14354, 4.47055, -0.557311, 
-0.425266, -2.37455, -5.97684, -5.22391, 0.374004, -0.986549, 
 2.36419, 0.218283, 2.66014, -3.44225, 3.46593, 1.3309, 0.679601, 
 5.42195, 10.6555, 8.34144, 1.64939, -1.64558, -0.754001, -4.77503, 
-6.66197, -4.07188, -1.72996, -1.15338, -8.05588, -6.58208, 1.32375, 
-3.69241, -5.23582, -4.33509, -7.43028, -3.57103, -10.4991, -8.68752, 
-8.98304, -8.96825, -7.99087, -8.25109, -6.48483, -6.09004, -7.05249, 
-4.78267)), class = "data.frame", row.names = c(NA, -73L))

What I have so far

I was able to combine conditions 1 and 2. Here's the script.

first_exceed_seq <- function(x, thresh = 0, len = 3)
{

# Logical vector, does x exceed the threshold
exceed_thresh <- x > thresh

# Indices of transition points; where exceed_thresh[i - 1] != 
exceed_thresh[i]
transition <- which(diff(c(0, exceed_thresh)) != 0)

# Reference index, grouping observations after each transition
index <- vector("numeric", length(x))
index[transition] <- 1
index <- cumsum(index)

# Break x into groups following the transitions
exceed_list <- split(exceed_thresh, index)

# Get the number of values exceeded in each index period
num_exceed <- vapply(exceed_list, sum, numeric(1))

# Get the starting index of the first sequence where more then len 
exceed thresh
transition[as.numeric(names(which(num_exceed >= len))[1])]
}

Then, using the function above, just type:

first_exceed_seq(dat[,1])

This gives 28. This should be the correct answer but I was wondering of the following problem.

Problem

1) I want to add the third condition in the above function such that the sum from 29 to 32 is greater than 1. From the above function, I set the minimum length to 3.I will be applying this to multiple time series and I might encounter a time series that has four consecutive positive values or more and the first time step from this does not satisfy [3] rather it is the 2nd or 3rd timesteps etc.

Any suggestion on how to do this R? I'll appreciate any help.

Update: I tried the solution below but the dplyr gives warning messages.

1: In filter_impl(.data, quo) : hybrid evaluation forced for lead. Please use dplyr::lead() or library(dplyr) to remove this warning.

Also the correct answer should be that 28 because it satisfied first all three conditions.

回答1:

here is a solution using the dplyr package and lead function. In the following code, x is the data you have provided:

library(dplyr)
newx <- x %>% as_tibble() %>%
  mutate(time = 1: n()) %>%  
  filter(V1 > 0, lead(V1, 1) > 0, lead(V1, 2) > 0,
         lead(V1, 1) + lead(V1, 2) + lead(V1, 3) + lead(V1, 4) > 1)
# A tibble: 7 x 2
      V1   idx
   <dbl> <int>
1  2.33     28
2  2.36     38
3  3.47     42
4  1.33     43
5  0.680    44
6  5.42     45
7 10.7      46

If you only want the first occurence you can use slice:

slice(newx, 1)
    # A tibble: 1 x 2
     V1   idx
  <dbl> <int>
1  2.33    28

Concerning the error: either include the dplyr package like I did, or replace lead by filter::lead.