简体   繁体   English

基于不等式的条件累积和在R中

[英]Inequality based conditional cumulative sum in R

I have been trying to do code this: For each 我一直试图做这个代码:每个

方程?

So far, the best way I came up to do this is by using a loop.Here is an example 到目前为止,我做到这一点的最好方法是使用循环。这是一个例子

y=rnorm(10)
x=c(1,1,1,2,2,2,3,3,3,4)
z=c(5,5,6,6,7,7,8,8,9,9)
data=data.frame(y,x,z)
n=10

s=rep(NA,length(unique(x))*length(unique(z)))
dim(s)=c(length(unique(x)),length(unique(z)))
for (i in 1:length(unique(x))){
  for (j in 1:length(unique(z))){
       s[i,j]=sum(y*as.numeric((x<=unique(x)[i]))*
                    as.numeric((z<=unique(z)[j])))
}
}

The output is OK like this, but when my dimensions grows, this becomes inefficient. 输出就像这样,但是当我的尺寸增大时,效率会降低。 Since, for a given z, this looks like a conditional cumulative sum, I am 100% sure that there is a more efficient way of doing this, without the loop. 因为,对于给定的z,这看起来像条件​​累积和,我100%确定有一种更有效的方法,没有循环。

Would any of you have any suggestion? 你们有没有任何建议? If I didn't have z, I know I could use data.table: 如果我没有z,我知道我可以使用data.table:

 s=data[order(x)][,lapply(.SD, sum),by=c("x"), .SDcols=c("y")]
  s=s[,lapply(.SD, cumsum), .SDcols=c("y")]

but with more than one index (x and z, not just x) I was not able to formulate the program. 但是有多个索引(x和z,而不仅仅是x),我无法制定程序。

I don't think you require data.table for this, as you're using the whole of "y" for each group. 我不认为你需要data.table ,因为你正在为每个组使用整个“y”。 This'll be easier to accomplish through some linear algebra: 通过一些线性代数,这将更容易实现:

t(y*outer(x, unique(x), '<=')) %*% outer(z, unique(z), '<=')
          [,1]       [,2]       [,3]       [,4]       [,5]
[1,] 0.3538152  0.1762013  0.1762013  0.1762013  0.1762013
[2,] 0.3538152 -0.7308157 -1.2421102 -1.2421102 -1.2421102
[3,] 0.3538152 -0.7308157 -1.2421102 -1.1770919 -1.8315592
[4,] 0.3538152 -0.7308157 -1.2421102 -1.1770919 -4.1171477

Here's your version of code for 3-dimensions: 这是您的三维代码版本:

set.seed(1)
y=rnorm(10)
x=c(1,1,1,2,2,2,3,3,3,4)
z=c(5,5,6,6,7,7,8,8,9,9)
w=c(7,7,8,8,9,9,10,10,11,11)
n=10

s=rep(NA,length(unique(w))*length(unique(z))*length(unique(x)))
dim(s)=c(length(unique(w)),length(unique(z)), length(unique(x)))
for (i in 1:length(unique(w))) {
  for (j in 1:length(unique(z))) {
    for (k in 1:length(unique(x))) {
       s[i,j, k]=sum(y*as.numeric((w<=unique(w)[i]))*
                    as.numeric((z<=unique(z)[j]))*
                    as.numeric((x<=unique(x)[k])))
    }
  }
}

Here's how you can accomplish this with the same idea as my previous answer: 以下是您使用与我之前的答案相同的想法来实现这一目标的方法:

t1 <- outer(x, unique(x), '<=')
t2 <- outer(z, unique(z), '<=')
t3 <- outer(w, unique(w), '<=')
lapply(seq_along(unique(x)), function(idx) t(y*t1[,idx]*t2) %*% t3)

Here the output is a list (instead of array), but the output is identical, you may compare the results with "s". 这里的输出是一个列表(而不是数组),但输出是相同的,你可以将结果与“s”进行比较。 You should be able to take it from here. 你应该可以从这里拿走它。

Following @Arun argumets, I managed to nest two lapply functions to get the solution generalized to upper dimensions. 在@Arun争论之后,我设法嵌套了两个lapply函数,以便将解决方案推广到上层维度。

lapply(seq_along(unique(x)), function(idx){lapply(seq_along(unique(r)),
                                              function(idr) t(y*t1[,idx]*t2)%*%(t3
                                              *t4[,idr]))})

For adding other dimensions, I will keep nesting lapply functions. 为了添加其他维度,我将继续嵌套lapply函数。 Is there a cleaner way to do this? 有更清洁的方法吗?

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

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