trouble with exiting loop if condition is met

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

trouble with exiting loop if condition is met

c98249123
I am working on a clinical trial simulation with two groups, treatment and
placebo, and the outcome is dichotomous (recovery or no recovery) . I would
like to stop my loop if either of my conditions are met:

1) futility - there are no responses to treatment in the treatment group.
2) the p-value is significant (<=0.01).

The problem I am having is my loop continues to run 10,000 times even though
I am sure that at least one of the conditions are met in some instances. It
appears the main problem is that my if loop is not adequately filtering the
conditions I specified in it.

library(magrittr)
library(dplyr)

nSims <- 10000 #number of simulated experiments
futility1 <-numeric(nSims) #set up empty container for all simulated
futility
futility2 <-numeric(nSims) #set up empty container for all simulated
futility

significant1 <-numeric(nSims) #set up empty container for all simulated
significance
significant2 <-numeric(nSims) #set up empty container for all simulated
significance

for(i in 1:nSims){ #for each simulated experiment
 # Year 1

 # p1<-response in controls
 # p2<-response in treated
 # Generating random deviates from a Uniform(0,1) distribution
 control.year1<-(runif(16, min = 0, max = 1))
 treat.year1<-(runif(16, min = 0, max = 1))

 #Generating dichotomous response variables for each group
 control.respond1<-ifelse(control.year1<=0.05,1,0)
 treat.respond1<-ifelse(treat.year1<=0.30,1,0)

 #Summing number of responses from each group
 control.no1<-sum(control.respond1==0)
 control.yes1<-sum(control.respond1==1)
 treat.no1<-sum(treat.respond1==0)
 treat.yes1<-sum(treat.respond1==1)

 #Perform the Fisher's exact test (one sided) with p<0.01

fisher<-matrix(c(control.no1,control.yes1,treat.no1,treat.yes1),nrow=2,ncol=2)
 f<-fisher.test(fisher,alternative = "greater")

 #year 2
 if (f$p.value>0.01 && treat.yes1!=0){
   # Generating random deviates from a Uniform(0,1) distribution
   control.year2<-(runif(16, min = 0, max = 1))
   treat.year2<-(runif(16, min = 0, max = 1))

   #Generating dichotomous response variables for each group
   control.respond2<-ifelse(control.year2<=0.05,1,0)
   treat.respond2<-ifelse(treat.year2<=0.30,1,0)

   #Summing number of responses from each group
   control.no2<-sum(control.respond2==0)
   control.yes2<-sum(control.respond2==1)
   treat.no2<-sum(treat.respond2==0)
   treat.yes2<-sum(treat.respond2==1)

   #Perform the Fisher's exact test (one sided) with p<0.01

fisher2<-matrix(c(control.no2,control.yes2,treat.no2,treat.yes2),nrow=2,ncol=2)
   f2<-fisher.test(fisher2,alternative = "greater")
 }


 significant2[i]<-ifelse(f2$p.value<0.01,1,0)
 futility2[i]<-ifelse(treat.yes2==0,1,0)
}

table(significant1)
table(futility1)

table(significant2)
table(futility2)

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: trouble with exiting loop if condition is met

R help mailing list-2
Does this example help?

> for (ii in 1:10) { cat( ii,'\n') ; if (ii >3) break }
1
2
3
4

The loop won't stop unless you tell it to stop, and I don't see any place where you told it to stop.

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 6/28/18, 12:53 PM, "R-help on behalf of Kelly Wu" <[hidden email] on behalf of [hidden email]> wrote:

    I am working on a clinical trial simulation with two groups, treatment and
    placebo, and the outcome is dichotomous (recovery or no recovery) . I would
    like to stop my loop if either of my conditions are met:
   
    1) futility - there are no responses to treatment in the treatment group.
    2) the p-value is significant (<=0.01).
   
    The problem I am having is my loop continues to run 10,000 times even though
    I am sure that at least one of the conditions are met in some instances. It
    appears the main problem is that my if loop is not adequately filtering the
    conditions I specified in it.
   
    library(magrittr)
    library(dplyr)
   
    nSims <- 10000 #number of simulated experiments
    futility1 <-numeric(nSims) #set up empty container for all simulated
    futility
    futility2 <-numeric(nSims) #set up empty container for all simulated
    futility
   
    significant1 <-numeric(nSims) #set up empty container for all simulated
    significance
    significant2 <-numeric(nSims) #set up empty container for all simulated
    significance
   
    for(i in 1:nSims){ #for each simulated experiment
     # Year 1
   
     # p1<-response in controls
     # p2<-response in treated
     # Generating random deviates from a Uniform(0,1) distribution
     control.year1<-(runif(16, min = 0, max = 1))
     treat.year1<-(runif(16, min = 0, max = 1))
   
     #Generating dichotomous response variables for each group
     control.respond1<-ifelse(control.year1<=0.05,1,0)
     treat.respond1<-ifelse(treat.year1<=0.30,1,0)
   
     #Summing number of responses from each group
     control.no1<-sum(control.respond1==0)
     control.yes1<-sum(control.respond1==1)
     treat.no1<-sum(treat.respond1==0)
     treat.yes1<-sum(treat.respond1==1)
   
     #Perform the Fisher's exact test (one sided) with p<0.01
   
    fisher<-matrix(c(control.no1,control.yes1,treat.no1,treat.yes1),nrow=2,ncol=2)
     f<-fisher.test(fisher,alternative = "greater")
   
     #year 2
     if (f$p.value>0.01 && treat.yes1!=0){
       # Generating random deviates from a Uniform(0,1) distribution
       control.year2<-(runif(16, min = 0, max = 1))
       treat.year2<-(runif(16, min = 0, max = 1))
   
       #Generating dichotomous response variables for each group
       control.respond2<-ifelse(control.year2<=0.05,1,0)
       treat.respond2<-ifelse(treat.year2<=0.30,1,0)
   
       #Summing number of responses from each group
       control.no2<-sum(control.respond2==0)
       control.yes2<-sum(control.respond2==1)
       treat.no2<-sum(treat.respond2==0)
       treat.yes2<-sum(treat.respond2==1)
   
       #Perform the Fisher's exact test (one sided) with p<0.01
   
    fisher2<-matrix(c(control.no2,control.yes2,treat.no2,treat.yes2),nrow=2,ncol=2)
       f2<-fisher.test(fisher2,alternative = "greater")
     }
   
   
     significant2[i]<-ifelse(f2$p.value<0.01,1,0)
     futility2[i]<-ifelse(treat.yes2==0,1,0)
    }
   
    table(significant1)
    table(futility1)
   
    table(significant2)
    table(futility2)
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: trouble with exiting loop if condition is met

Jim Lemon-4
In reply to this post by c98249123
Hi Kelly,
You seem to be testing the two "years" independently, so here is a
possible solution:

npg<-16
futility1<-futility2<-psignif1<-psignif2<-nrep<-0
while(!futility1 && !futility2 && !psignif1 && !psignif2 && nrep < 10000) {
 control_respond1<-sum(runif(npg,0,1) < 0.05)
 control_respond2<-sum(runif(npg,0,1) < 0.05)
 treat_respond1<-sum(runif(npg,0,1) < 0.3)
 treat_respond2<-sum(runif(npg,0,1) < 0.3)
 futility1<-treat_respond1 == 0
 futility2<-treat_respond2 == 0
 psignif1<-fisher.test(matrix(c(control_respond1,npg-control_respond1,
  treat_respond1,npg-treat_respond1),nrow=2),
  alternative="greater")$p.value < 0.01
 psignif2<-fisher.test(matrix(c(control_respond2,npg-control_respond2,
  treat_respond2,npg-treat_respond2),nrow=2),
  alternative="greater")$p.value < 0.01
 nrep<-nrep+1
}
cat("futility1",futility1,"futility2",futility2,"psignif1",psignif1,
 "psignif2",psignif2,"nrep",nrep,"\n")

The output tells you which condition was met and on which repetition
it occurred. The outcomes for all previous repetitions will be FALSE
and for the remaining repetitions, undefined (unless you have a cat
named Schrodinger)..

If you want to test the two "years" as sequential observations, it
will only require a minor modification.

Jim

On Fri, Jun 29, 2018 at 5:53 AM, Kelly Wu <[hidden email]> wrote:

> I am working on a clinical trial simulation with two groups, treatment and
> placebo, and the outcome is dichotomous (recovery or no recovery) . I would
> like to stop my loop if either of my conditions are met:
>
> 1) futility - there are no responses to treatment in the treatment group.
> 2) the p-value is significant (<=0.01).
>
> The problem I am having is my loop continues to run 10,000 times even though
> I am sure that at least one of the conditions are met in some instances. It
> appears the main problem is that my if loop is not adequately filtering the
> conditions I specified in it.
>
> library(magrittr)
> library(dplyr)
>
> nSims <- 10000 #number of simulated experiments
> futility1 <-numeric(nSims) #set up empty container for all simulated
> futility
> futility2 <-numeric(nSims) #set up empty container for all simulated
> futility
>
> significant1 <-numeric(nSims) #set up empty container for all simulated
> significance
> significant2 <-numeric(nSims) #set up empty container for all simulated
> significance
>
> for(i in 1:nSims){ #for each simulated experiment
>  # Year 1
>
>  # p1<-response in controls
>  # p2<-response in treated
>  # Generating random deviates from a Uniform(0,1) distribution
>  control.year1<-(runif(16, min = 0, max = 1))
>  treat.year1<-(runif(16, min = 0, max = 1))
>
>  #Generating dichotomous response variables for each group
>  control.respond1<-ifelse(control.year1<=0.05,1,0)
>  treat.respond1<-ifelse(treat.year1<=0.30,1,0)
>
>  #Summing number of responses from each group
>  control.no1<-sum(control.respond1==0)
>  control.yes1<-sum(control.respond1==1)
>  treat.no1<-sum(treat.respond1==0)
>  treat.yes1<-sum(treat.respond1==1)
>
>  #Perform the Fisher's exact test (one sided) with p<0.01
>
> fisher<-matrix(c(control.no1,control.yes1,treat.no1,treat.yes1),nrow=2,ncol=2)
>  f<-fisher.test(fisher,alternative = "greater")
>
>  #year 2
>  if (f$p.value>0.01 && treat.yes1!=0){
>    # Generating random deviates from a Uniform(0,1) distribution
>    control.year2<-(runif(16, min = 0, max = 1))
>    treat.year2<-(runif(16, min = 0, max = 1))
>
>    #Generating dichotomous response variables for each group
>    control.respond2<-ifelse(control.year2<=0.05,1,0)
>    treat.respond2<-ifelse(treat.year2<=0.30,1,0)
>
>    #Summing number of responses from each group
>    control.no2<-sum(control.respond2==0)
>    control.yes2<-sum(control.respond2==1)
>    treat.no2<-sum(treat.respond2==0)
>    treat.yes2<-sum(treat.respond2==1)
>
>    #Perform the Fisher's exact test (one sided) with p<0.01
>
> fisher2<-matrix(c(control.no2,control.yes2,treat.no2,treat.yes2),nrow=2,ncol=2)
>    f2<-fisher.test(fisher2,alternative = "greater")
>  }
>
>
>  significant2[i]<-ifelse(f2$p.value<0.01,1,0)
>  futility2[i]<-ifelse(treat.yes2==0,1,0)
> }
>
> table(significant1)
> table(futility1)
>
> table(significant2)
> table(futility2)
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: trouble with exiting loop if condition is met

c98249123
Hi Jim & Don,


I was trying to use the break command originally before posting but for some reason it was making almost all of the p-values in the replications non significant. I think I am going to change the flow of the loop so I don’t have to use a break, such as the code Jim wrote. Thanks for your detailed response!


Kelly


> On Jun 28, 2018, at 3:18 PM, Jim Lemon <[hidden email]> wrote:
>
> " as sequential observations, it
> will only require a minor modification.
>
> Jim
>
> On Fri, Jun 29, 2018 at 5:53 AM, Kelly Wu <[hidden email]> wrote:
>


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.