简体   繁体   English

递归ewma计算和比较

[英]recursive ewma calculation and comparison

I have data which contains two column (Time , Result). 我有包含两列(时间,结果)的数据。 For every second , I have different value for 'Result'. 对于“每秒”,我具有不同的“结果”值。 I want to check the value of 'Result' in every second if it's exceeded a given condition. 如果超出指定条件,我想每秒钟检查一次“结果”的值。 The condition is changing for every value of 'Result' based on the previous mean. 根据先前的平均值,条件对于“结果”的每个值都在变化。 The previous mean is calculated based on the exponentially weighted moving average (EWMA) from the following : 先前的平均值是根据以下指数加权移动平均值(EWMA)计算的:

μn = μn−1 + (1 − lambda)Xn , 

lambda is the EWMA factor (for this example use 0.2) 
μn−1 is the mean value calculated from measurements prior to record n.
μn   is the mean. 
Xn   is the value of 'Result' in the nth record.
n    is number of records in the df

The condition is : 条件是:

g is variable that incremented for each time the condition is true. g是每次条件成立时都会增加的变量。

if (Xn > (1.5)μn−1) {
  g<-g+1
}

This logic has to be carried out over all of the records in the data. 必须对数据中的所有记录执行此逻辑。

Here is MWE: 这是MWE:

readFile<- read.table("data.tr",header=F, stringsAsFactor=F)
colnames(readFile)<-c("time","Results")
df<-data.frame(Time=readFile$time,Results=readFile$Results)

#The data looks like (df);
 Time Results
   1     10
   2     15
   3     15
   4     10
   5     10
   6     30
   7     15
   8     25
   9     40
  10     22
  11     48
  12     50
  13     30
  14     40
  15     64
  16     46
  17     30
  18     10
  19     17
  20     53
  #define variables
  g<-0
  result<-0
  previousAverage<-0

  for(i in df){
   result<-df&Results[i]
   # Here I'm confused how to make the recursive call !!
   #I'm assuming the average should be returned from a separate method 
   #(i.e AverageCalculation) and use in the condition

   condition <- (1.5) * previousAverage
   if ( result > condition){
       g<-g+1
    }
  }

I found that "qcc" package calculates the EWMA which should simplify the calculation. 我发现“ qcc”包计算了EWMA,这应该简化了计算。 However, I want to use the equation above. 但是,我想使用上面的公式。 The difficult part for me is how to calculate the mean from the first record to the n-1 th record and keep shifting? 对我来说最困难的部分是如何计算从第一条记录到第n-1条记录的均值并保持不变? how I can hold the current record value. 如何保存当前的记录值。

Any suggestions?!!! 有什么建议么?!!!

outside your loop, initalize mu and a column for the lag. 在循环之外,初始化mu和一列延迟。

mu = 0
df$prevmu <- 0

then loop over the rows, 然后遍历行,

for(i in 1:nrow(df)) {
  df$prevmu[i] <- mu
  mu <- mu + (1 - lambda) * df$Result[i]
}

Now you can calculate g: 现在您可以计算g了:

g <- with(df, sum(Results > 1.5 * prevmu))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM