Skip to content
Prev 318190 / 398503 Next

cumulative sum by group and under some criteria

Hi,
d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L = c(0.9025,
0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H = c(0.9025,
0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1",
"n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names = c(NA,
3L), class = "data.frame")
d2<- data.frame()
for (m1 in 2:3) {
??? for (n1 in 2:3) {
??????? for (x1 in 0:(m1-1)) {
??????????? for (y1 in 0:(n1-1)) {
??????? for (m in (m1+2): (7-n1)){
?????????????? for (n in (n1+2):(9-m)){
?????????????? for (x in x1:(x1+m-m1)){
???????????? for(y in y1:(y1+n-n1)){???? 
?d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y))
?}}}}}}}}
colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y")? 
?#or
?
res1<-do.call(rbind,lapply(unique(d3$m1),function(m1)
?do.call(rbind,lapply(unique(d3$n1),function(n1)
?do.call(rbind,lapply(0:(m1-1),function(x1)
?do.call(rbind,lapply(0:(n1-1),function(y1)
?do.call(rbind,lapply((m1+2):(7-n1),function(m)
?do.call(rbind,lapply((n1+2):(9-m),function(n)
?do.call(rbind,lapply(x1:(x1+m-m1), function(x)
?do.call(rbind,lapply(y1:(y1+n-n1), function(y)
?expand.grid(m1,n1,x1,y1,m,n,x,y)) )))))))))))))))
?names(res1)<- c("m1","n1","x1","y1","m","n","x","y")
?attr(res1,"out.attrs")<-NULL
res1[]<- sapply(res1,as.integer)

library(plyr)
res2<- join(res1,d3,by=c("m1","n1"),type="inner")

#Assuming that these are the values you used:

p0L<-0.05
p0H<-0.05
p1L<-0.20
p1H<-0.20
res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE);Pm2<-rbeta(240, 0.2+x, 0.8+m-x);Pn2<-rbeta(240, 0.2+y, 0.8+n-y)})
Fm2<- ecdf(res2$Pm2)
Fn2<- ecdf(res2$Pn2)

res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- (Fmm2+Fnn2)/2}) #not sure about this step especially the Fm2() or Fn2()
res3$Fmm_f2<-apply(res3[,c("R2","Fmm2")],1,min)
?res3$Fnn_f2<-apply(res3[,c("R2","Fnn2")],1,max)
res3<- within(res3,{Qm2<- 1-Fmm_f2;Qn2<- 1-Fnn_f2})
head(res3)
#? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H???????? Pn2
#1? 2? 2? 0? 0 4 4 0 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.001084648
#2? 2? 2? 0? 0 4 4 0 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.504593909
#3? 2? 2? 0? 0 4 4 0 2???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.541379357
#4? 2? 2? 0? 0 4 4 1 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.138947785
#5? 2? 2? 0? 0 4 4 1 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.272364957
#6? 2? 2? 0? 0 4 4 1 2???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.761635059
#?????????? Pm2?? term2_p1???? term2_p0?? p2?? p1??????? R2????? Fnn2 Fmm2
#1 1.212348e-05 0.16777216 0.6634204313 0.00 0.00 0.0000000 0.0000000? 0.0
#2 1.007697e-03 0.08388608 0.0698337296 0.25 0.00 0.1791667 0.3583333? 0.0
#3 1.106946e-05 0.01048576 0.0018377297 0.50 0.00 0.3479167 0.6958333? 0.0
# 2.086758e-01 0.08388608 0.0698337296 0.00 0.25 0.2000000 0.0000000? 0.4
#5 2.382179e-01 0.04194304 0.0073509189 0.25 0.25 0.3791667 0.3583333? 0.4
#6 4.494673e-01 0.00524288 0.0001934452 0.50 0.25 0.5479167 0.6958333? 0.4
#???? Fmm_f2??? Fnn_f2?????? Qn2?????? Qm2
#1 0.0000000 0.0000000 1.0000000 1.0000000
#2 0.0000000 0.3583333 0.6416667 1.0000000
#3 0.0000000 0.6958333 0.3041667 1.0000000
#4 0.2000000 0.2000000 0.8000000 0.8000000
#5 0.3791667 0.3791667 0.6208333 0.6208333
#6 0.4000000 0.6958333 0.3041667 0.6000000


A.K.