首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中超过阈值和附加条件的连续超越

R中超过阈值和附加条件的连续超越
EN

Stack Overflow用户
提问于 2019-04-04 09:20:18
回答 1查看 346关注 0票数 2

我想使用R(应该是满足以下条件的第一个时间步骤)在时间序列中得到满足以下条件的时间步骤:

代码语言:javascript
复制
[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.

这里是数据

代码语言:javascript
复制
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))

我到目前为止有什么

我能把条件1和2结合起来。这是脚本。

代码语言:javascript
复制
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])]
}

然后,使用上面的函数,只需键入:

代码语言:javascript
复制
first_exceed_seq(dat[,1])

这个数字是28。这应该是正确的答案,但我想知道以下问题。

问题

1)在上面的函数中添加第三个条件,使29到32的和大于1。从上面的函数中,我将最小长度设为3。我将这个条件应用于多个时间序列,我可能会遇到一个连续四个正值或更多的时间序列,从它开始的第一步不满足3,而是第2或第3步,等等。

对如何做R有什么建议吗?我会感谢你的帮助。

更新:我尝试了下面的解决方案,但是dplyr给出了警告消息。

1:在filter_impl(.data,quo)中:lead强制进行混合评价。请使用dplyr::铅()或库(Dplyr)删除此警告。

同样正确的答案应该是28,因为它首先满足了三个条件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-04 09:55:48

下面是使用dplyr包和lead函数的解决方案。在下面的代码中,x是您提供的数据:

代码语言:javascript
复制
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

如果您只想第一次出现,可以使用slice

代码语言:javascript
复制
slice(newx, 1)
    # A tibble: 1 x 2
     V1   idx
  <dbl> <int>
1  2.33    28

关于错误:要么像我一样包含dplyr包,要么用filter::lead替换lead

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55512219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档