Can someone help me with the if loop below? In the subroutine, I initialize all of (joint12,marg1,marg2,cond12,cond21) as FALSE, and call with only one of them being TRUE: ,...,joint12=FALSE,marg1=FALSE,marg2=FALSE,cond12=FALSE,cond21=FALSE,,,, joint12 seems to always kick in, even though I call with, e.g., marg1 being TRUE and everything else being FALSE. My attempts with if... else if were not useful. Please help. Thanks. v1<-cprob(z1,x1,a,b,mu1,mu2,rho,j+1,k+1) ??? v0<-cprob(z0,x0,a,b,mu1,mu2,rho,j+1,k+1) ?? ... ??? me1<-me0<-NULL ??? if(joint12) {me1<-cbind(me1,v1$p12); me0<-cbind(me0,v0$p12)} ??? if(marg1)?? {me1<-cbind(me1,v1$p1); me0<-cbind(me0,v0$p1)} ??? if(marg2)?? {me1<-cbind(me1,v1$p2); me0<-cbind(me0,v0$p2)} ??? if(cond12)? {me1<-cbind(me1,v1$pc12); me0<-cbind(me0,v0$pc12)} ??? if(cond21)? {me1<-cbind(me1,v1$pc21); me0<-cbind(me0,v0$pc21)} ??? ... ? labels<-NULL ? if(joint12) labels<-c(labels,lab.p12) ? if(marg1)?? labels<-c(labels,lab.p1) ? if(marg2)?? labels<-c(labels,lab.p2) ? if(cond12)? labels<-c(labels,lab.pc12) ? if(cond21)? labels<-c(labels,lab.pc21)
The following (using if else) did not help. Seemed like joint12 always kicked in. ??? me1<-me0<-NULL. ??? if(joint12){ ????? {me1<-cbind(me1,v1$p12);? me0<-cbind(me0,v0$p12)} ??? } else if(marg1) { ????? {me1<-cbind(me1,v1$p1);?? me0<-cbind(me0,v0$p1)} ??? } else if(marg2) { ????? {me1<-cbind(me1,v1$p2);?? me0<-cbind(me0,v0$p2)} ??? } else if(cond12){ ????? {me1<-cbind(me1,v1$pc12); me0<-cbind(me0,v0$pc12)} ??? } else { ????? {me1<-cbind(me1,v1$pc21); me0<-cbind(me0,v0$pc21)} ??? } ... ? labels<-NULL ? if(joint12){ ??? labels<-c(labels,lab.p12) ? } else if(marg1) { ??? labels<-c(labels,lab.p1) ? } else if(marg2) { ??? labels<-c(labels,lab.p2) ? } else if(cond12){ ??? labels<-c(labels,lab.pc12) ? } else { ??? labels<-c(labels,lab.pc21) ? } On 8/9/2024 11:44 AM, Steven Yen wrote:> Can someone help me with the if loop below? In the subroutine, I > initialize all of (joint12,marg1,marg2,cond12,cond21) as FALSE, and > call with only one of them being TRUE: > > ,...,joint12=FALSE,marg1=FALSE,marg2=FALSE,cond12=FALSE,cond21=FALSE,,,, > > joint12 seems to always kick in, even though I call with, e.g., marg1 > being TRUE and everything else being FALSE. My attempts with if... > else if were not useful. Please help. Thanks. > > v1<-cprob(z1,x1,a,b,mu1,mu2,rho,j+1,k+1) > ??? v0<-cprob(z0,x0,a,b,mu1,mu2,rho,j+1,k+1) > > ?? ... > > ??? me1<-me0<-NULL > ??? if(joint12) {me1<-cbind(me1,v1$p12); me0<-cbind(me0,v0$p12)} > ??? if(marg1)?? {me1<-cbind(me1,v1$p1); me0<-cbind(me0,v0$p1)} > ??? if(marg2)?? {me1<-cbind(me1,v1$p2); me0<-cbind(me0,v0$p2)} > ??? if(cond12)? {me1<-cbind(me1,v1$pc12); me0<-cbind(me0,v0$pc12)} > ??? if(cond21)? {me1<-cbind(me1,v1$pc21); me0<-cbind(me0,v0$pc21)} > ??? ... > > ? labels<-NULL > ? if(joint12) labels<-c(labels,lab.p12) > ? if(marg1)?? labels<-c(labels,lab.p1) > ? if(marg2)?? labels<-c(labels,lab.p2) > ? if(cond12)? labels<-c(labels,lab.pc12) > ? if(cond21)? labels<-c(labels,lab.pc21) >
1. I think to get useful help you will have to provide a small reproducible example. In any case, I was not able to figure out your problem from what you provided (perhaps others may). 2. R is a (mostly) functional language. It does not have "subroutines" . Cheers, Bert On Thu, Aug 8, 2024 at 8:45?PM Steven Yen <styen at ntu.edu.tw> wrote:> > Can someone help me with the if loop below? In the subroutine, I > initialize all of (joint12,marg1,marg2,cond12,cond21) as FALSE, and call > with only one of them being TRUE: > > ,...,joint12=FALSE,marg1=FALSE,marg2=FALSE,cond12=FALSE,cond21=FALSE,,,, > > joint12 seems to always kick in, even though I call with, e.g., marg1 > being TRUE and everything else being FALSE. My attempts with if... else > if were not useful. Please help. Thanks. > > v1<-cprob(z1,x1,a,b,mu1,mu2,rho,j+1,k+1) > v0<-cprob(z0,x0,a,b,mu1,mu2,rho,j+1,k+1) > > ... > > me1<-me0<-NULL > if(joint12) {me1<-cbind(me1,v1$p12); me0<-cbind(me0,v0$p12)} > if(marg1) {me1<-cbind(me1,v1$p1); me0<-cbind(me0,v0$p1)} > if(marg2) {me1<-cbind(me1,v1$p2); me0<-cbind(me0,v0$p2)} > if(cond12) {me1<-cbind(me1,v1$pc12); me0<-cbind(me0,v0$pc12)} > if(cond21) {me1<-cbind(me1,v1$pc21); me0<-cbind(me0,v0$pc21)} > ... > > labels<-NULL > if(joint12) labels<-c(labels,lab.p12) > if(marg1) labels<-c(labels,lab.p1) > if(marg2) labels<-c(labels,lab.p2) > if(cond12) labels<-c(labels,lab.pc12) > if(cond21) labels<-c(labels,lab.pc21) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Is something wrong in the initialisation part that we don't see? joint12 <- marg1 <-F marg1 <-T if (joint12) { print ("joint 12") cat (joint12) } if (marg1) { print("marg 1") cat(marg1) } Would probably be my diagnostic approach On Fri, 9 Aug 2024, 04:45 Steven Yen, <styen at ntu.edu.tw> wrote:> Can someone help me with the if loop below? In the subroutine, I > initialize all of (joint12,marg1,marg2,cond12,cond21) as FALSE, and call > with only one of them being TRUE: > > ,...,joint12=FALSE,marg1=FALSE,marg2=FALSE,cond12=FALSE,cond21=FALSE,,,, > > joint12 seems to always kick in, even though I call with, e.g., marg1 > being TRUE and everything else being FALSE. My attempts with if... else > if were not useful. Please help. Thanks. > > v1<-cprob(z1,x1,a,b,mu1,mu2,rho,j+1,k+1) > v0<-cprob(z0,x0,a,b,mu1,mu2,rho,j+1,k+1) > > ... > > me1<-me0<-NULL > if(joint12) {me1<-cbind(me1,v1$p12); me0<-cbind(me0,v0$p12)} > if(marg1) {me1<-cbind(me1,v1$p1); me0<-cbind(me0,v0$p1)} > if(marg2) {me1<-cbind(me1,v1$p2); me0<-cbind(me0,v0$p2)} > if(cond12) {me1<-cbind(me1,v1$pc12); me0<-cbind(me0,v0$pc12)} > if(cond21) {me1<-cbind(me1,v1$pc21); me0<-cbind(me0,v0$pc21)} > ... > > labels<-NULL > if(joint12) labels<-c(labels,lab.p12) > if(marg1) labels<-c(labels,lab.p1) > if(marg2) labels<-c(labels,lab.p2) > if(cond12) labels<-c(labels,lab.pc12) > if(cond21) labels<-c(labels,lab.pc21) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]