R:计算点累积和的变化?

时间:2013-06-30 19:26:12

标签: r statistics

有没有人知道用一个明确的例子计算CUSUM的包?我找到了“qcc”,它描绘了CUSUM。但是,我希望提交的数据范围的CUSUM值不是图表。另一个新的好包“changepoint”,它根据不同的分布计算CUSUM。我需要一个简单的包或函数来根据CUSUM的基本定义计算CUSUM-没有任何参数:

As its name implies, CUSUM involves the calculation of a cumulative sum 
which is what makes it "sequential"). 
Samples from a process are assigned weights, 
and summed as follows:
For samples i = 0 ->infinity
   C0 = 0 :
   Ci+1 = max(0;Ci + Xi - mean(Xi) )

Wiki

中定义

计算基本CUSUM的任何包或函数?

1 个答案:

答案 0 :(得分:3)

为什么不是一个简单的for-loop?像这样,例如:

set.seed(20)
x <- rnorm(10, 1, 3)
mx <- mean(x)
CUMSUM <- function(x) {
    res <- numeric(length(x))
    for (i in seq_along(x) + 1) {
        res[i] <- max(0, res[i-1] + x[i-1] - mx)
    }
    res
}
CUMSUM(x)