Loop Issue

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

Loop Issue

Ricardo Rocha
Hi everybody.

Consider the following exampling code:

x=numeric()
  for(i in 1:10){
    u=runif(1,-1,2)
    x[i]=log(u)
  }
This code, in each interation, generates a random value in the (-1,2) interval and then calculates the log of the value.
When the generated value is less than 0 the log produces a NaN, which gives a warning.

What I want is to make it start over when a warning is produced, in order to repeat it until a positive value is generated and therefore the log is calculated. Logically, would be like: "if there's a warning here, go back at the beginning and start over", without changing the iteration.

Could someone help me with some directions?

Thanks a lot,

Ricardo    
        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Loop Issue

YRichard
Hi Ricardo
Assuming you have a good reason to use such approach (what are you
trying to do ultimately?), you can just increment your counter when
you get a good value, i.e.:

x <- numeric()
n <- 0
while (n < 10) {
    u <- log(runif(1, -1, 2))
    if (is.finite(u)) {
        n <- n+1
        x[n] <- u
    }
}
x

On 23 May 2014 12:11, Ricardo Rocha <[hidden email]> wrote:

> Hi everybody.
>
> Consider the following exampling code:
>
> x=numeric()
>   for(i in 1:10){
>     u=runif(1,-1,2)
>     x[i]=log(u)
>   }
> This code, in each interation, generates a random value in the (-1,2) interval and then calculates the log of the value.
> When the generated value is less than 0 the log produces a NaN, which gives a warning.
>
> What I want is to make it start over when a warning is produced, in order to repeat it until a positive value is generated and therefore the log is calculated. Logically, would be like: "if there's a warning here, go back at the beginning and start over", without changing the iteration.
>
> Could someone help me with some directions?
>
> Thanks a lot,
>
> Ricardo
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.



--
Yvan Richard

      DRAGONFLY Science

Physical address: Level 5, 158 Victoria St, Te Aro, Wellington
Postal address: PO Box 27535, Wellington 6141
New Zealand
Ph: 04.385.9285
web page

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Loop Issue

Jim Lemon
In reply to this post by Ricardo Rocha
On Thu, 22 May 2014 09:11:43 PM Ricardo Rocha wrote:

> Hi everybody.
>
> Consider the following exampling code:
>
> x=numeric()
>   for(i in 1:10){
>     u=runif(1,-1,2)
>     x[i]=log(u)
>   }
> This code, in each interation, generates a random value in the (-1,2)
> interval and then calculates the log of the value. When the generated
value
> is less than 0 the log produces a NaN, which gives a warning.
>
> What I want is to make it start over when a warning is produced, in
order to
> repeat it until a positive value is generated and therefore the log is
> calculated. Logically, would be like: "if there's a warning here, go back
> at the beginning and start over", without changing the iteration.
>
> Could someone help me with some directions?
>
Hi Ricardo,
Perhaps what you want is something like this:

i<-1
while(I < 11) {
 u<-runif(1,-1,2)
 if(u >= 0) {
  x[i]<-log(u)
  i<-i+1
 }
}

Jim

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Loop Issue

arun kirshna
Hi,
You may also try:
fun1 <- function(n, repl, val1, val2) {
    mat1 <- suppressWarnings(replicate(repl, log(runif(n, val1, val2))))
    mat1[!is.na(mat1)][seq(n)]
}

#Jim's function
fun2 <- function(init, final, val1, val2) {
    i <- init
    while (i < final) {
        u <- runif(1, val1, val2)
        if (u >= 0) {
            x[i] <- log(u)
            i <- i + 1
        }
    }
    x
}


set.seed(49)
 x1 <- fun1(10,3,-1,2)
 set.seed(49)
 x2 <- fun2(1,11,-1,2)
 identical(x1,x2)
#[1] TRUE

###Speed comparison
set.seed(58)
 system.time(x1 <- fun1(1e5,100, -1,2))
#   user  system elapsed
#  1.392   0.105   1.500


set.seed(58)
 system.time(x2 <- fun2(1,1e5+1,-1,2))
#   user  system elapsed
# 31.930   0.988  32.973

identical(x1,x2)
#[1] TRUE


A.K.








On Friday, May 23, 2014 4:19 AM, Jim Lemon <[hidden email]> wrote:
On Thu, 22 May 2014 09:11:43 PM Ricardo Rocha wrote:

> Hi everybody.
>
> Consider the following exampling code:
>
> x=numeric()
>   for(i in 1:10){
>     u=runif(1,-1,2)
>     x[i]=log(u)
>   }
> This code, in each interation, generates a random value in the (-1,2)
> interval and then calculates the log of the value. When the generated
value
> is less than 0 the log produces a NaN, which gives a warning.
>
> What I want is to make it start over when a warning is produced, in
order to
> repeat it until a positive value is generated and therefore the log is
> calculated. Logically, would be like: "if there's a warning here, go back
> at the beginning and start over", without changing the iteration.
>
> Could someone help me with some directions?
>
Hi Ricardo,
Perhaps what you want is something like this:

i<-1
while(I < 11) {
u<-runif(1,-1,2)
if(u >= 0) {
  x[i]<-log(u)
  i<-i+1
}
}

Jim


______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.