Evaluate output after each rep()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Evaluate output after each rep()

Matthew MacManes
Hi R-Help,

I'm trying a develop a test simulation where i evaluate the probability
of not getting a value of 100 from the function rbinom(6000, 200, .5)
[indeed, a very small probability].  At the end of each rep, I would
like to evaluate the output, continue with the loop if the output
contains the value 100, stop if the output lacks a 100.

How do I get R to evaluate the output after each rep?


 >sim <- function(nn){
 >   for (ii in 1:nn){
 >      ee=rep(rbinom(6000, 200, .5), ii)
 >         if (any(ee==100))
 >      }

Thanks,
Matt MacManes
********************************************************
Matthew D. MacManes
PhD Student
UC- Berkeley
Department of Integrative Biology
Museum of Vertebrate Zoology
3101 VLSB #3140
Berkeley, CA 94720
(510)642-7782
EMAIL: [hidden email]
WEBSITE: http://ib.berkeley.edu/labs/lacey/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Evaluate output after each rep()

Adaikalavan Ramasamy
I do not fully understand what you mean by "stop". If you mean terminate
the whole function, then something like

 sim <- function(nn, mustExist=100){

   for (ii in 1:nn){
       ee <- rep(rbinom(6000, 200, .5), ii)

       if( any(ee!=mustExist) )
           stop( paste("Iteration", ii, "did not contain", mustExist,
                       ". Terminating function \n") )
   }
 
   ## Do something further ##
 }


But generally, I want to just resample again till I get the desired length.

 sim <- function(nn, mustExist=100){

   counter <- 0;  ii <- 1

   while( counter <= nn ){

       ee <- rep(rbinom(6000, 200, .5), ii)

       if( any(ee!=mustExist) ){
           warning(paste( "Iteration", ii, "did not contain", mustExist,
                           ". Resampling again\n") )
       } else {
         counter <- counter + 1
         ii      <- ii + 1
         ## Do something further ##
      }
   }
 }

You can turn off the branch that returns the warnings if it gets annoying.

BTW, why do you want to use rep(.., ii) ?

Regards, Adai




If you want to count how many times out



On Mon, 2006-02-06 at 09:03 -0800, Matthew MacManes wrote:

> Hi R-Help,
>
> I'm trying a develop a test simulation where i evaluate the probability
> of not getting a value of 100 from the function rbinom(6000, 200, .5)
> [indeed, a very small probability].  At the end of each rep, I would
> like to evaluate the output, continue with the loop if the output
> contains the value 100, stop if the output lacks a 100.
>
> How do I get R to evaluate the output after each rep?
>
>
>  >sim <- function(nn){
>  >   for (ii in 1:nn){
>  >      ee=rep(rbinom(6000, 200, .5), ii)
>  >         if (any(ee==100))
>  >      }
>
> Thanks,
> Matt MacManes
> ********************************************************
> Matthew D. MacManes
> PhD Student
> UC- Berkeley
> Department of Integrative Biology
> Museum of Vertebrate Zoology
> 3101 VLSB #3140
> Berkeley, CA 94720
> (510)642-7782
> EMAIL: [hidden email]
> WEBSITE: http://ib.berkeley.edu/labs/lacey/
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Evaluate output after each rep()

Bert Gunter
Of course I assume that everyone realizes that the simulation estimates (1 -
dbinom(100,200,p=.5))^6000. The loge of this quantity is -348, btw. So this
is trivially computable, but I assume that this is an exercise of some sort.

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of
> Adaikalavan Ramasamy
> Sent: Monday, February 06, 2006 9:35 AM
> To: [hidden email]
> Cc: [hidden email]
> Subject: Re: [R] Evaluate output after each rep()
>
> I do not fully understand what you mean by "stop". If you
> mean terminate
> the whole function, then something like
>
>  sim <- function(nn, mustExist=100){
>
>    for (ii in 1:nn){
>        ee <- rep(rbinom(6000, 200, .5), ii)
>
>        if( any(ee!=mustExist) )
>            stop( paste("Iteration", ii, "did not contain", mustExist,
>                        ". Terminating function \n") )
>    }
>  
>    ## Do something further ##
>  }
>
>
> But generally, I want to just resample again till I get the
> desired length.
>
>  sim <- function(nn, mustExist=100){
>
>    counter <- 0;  ii <- 1
>
>    while( counter <= nn ){
>
>        ee <- rep(rbinom(6000, 200, .5), ii)
>
>        if( any(ee!=mustExist) ){
>            warning(paste( "Iteration", ii, "did not contain",
> mustExist,
>                            ". Resampling again\n") )
>        } else {
>          counter <- counter + 1
>          ii      <- ii + 1
>          ## Do something further ##
>       }
>    }
>  }
>
> You can turn off the branch that returns the warnings if it
> gets annoying.
>
> BTW, why do you want to use rep(.., ii) ?
>
> Regards, Adai
>
>
>
>
> If you want to count how many times out
>
>
>
> On Mon, 2006-02-06 at 09:03 -0800, Matthew MacManes wrote:
> > Hi R-Help,
> >
> > I'm trying a develop a test simulation where i evaluate the
> probability
> > of not getting a value of 100 from the function
> rbinom(6000, 200, .5)
> > [indeed, a very small probability].  At the end of each
> rep, I would
> > like to evaluate the output, continue with the loop if the output
> > contains the value 100, stop if the output lacks a 100.
> >
> > How do I get R to evaluate the output after each rep?
> >
> >
> >  >sim <- function(nn){
> >  >   for (ii in 1:nn){
> >  >      ee=rep(rbinom(6000, 200, .5), ii)
> >  >         if (any(ee==100))
> >  >      }
> >
> > Thanks,
> > Matt MacManes
> > ********************************************************
> > Matthew D. MacManes
> > PhD Student
> > UC- Berkeley
> > Department of Integrative Biology
> > Museum of Vertebrate Zoology
> > 3101 VLSB #3140
> > Berkeley, CA 94720
> > (510)642-7782
> > EMAIL: [hidden email]
> > WEBSITE: http://ib.berkeley.edu/labs/lacey/
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html