[R] looping problem

jim holtman jholtman at gmail.com
Fri Feb 12 14:07:42 CET 2010


Exactly what are you expecting the 'if' statement to do?  By
definition, the  'if' only takes a single value and your expression is
probably a vector with many values and the error message is just
saying it will use only the first value to determine what to do.  Are
you trying to check if 'any' or 'all' of the values are out of
tolerance?  If so, check out the 'any' and 'all' functions.

On Fri, Feb 12, 2010 at 6:49 AM, Roslina Zakaria <zroslina at yahoo.com> wrote:
> Hi r-users,
>
>
> I have this code below but I don't understand the error message:
>
> cumdensity <- function(z)
> { alp  <- 2.0165;
>   rho  <- 0.868;
>
> # simplified expressions
>   a      <- alp-0.5
>   c1     <- sqrt(pi)/(gamma(alp)*(1-rho)^alp)
>   c2     <- sqrt(rho)/(1-rho)
>   t1     <- exp(-z/(1-rho))
>   t2     <- (z/(2*c2))^a
>   bes1   <- besselI(z*c2,a)
>   t1bes1 <- t1*bes1
>   cden   <- c1*t1bes1*t2
>   cden
> }
>
> pdensity <- function(z)
> { alp  <- 2.0165;  rho  <- 0.868;  a <- alp-0.5;
>   k1   <- sqrt(pi)/(gamma(alp)*(1-rho)^alp)
>   k2   <- sqrt(rho)/(1-rho)
>   t1   <- exp(-z/(1-rho))
>   t2   <- (z/(2*k2))^a
>   bes1 <- besselI(z*k2,a)
>   bes2 <- besselI(z*k2,alp+0.5)
>   pp   <- k1*t1*t2*(bes1/(rho-1) + a*bes1/z + k2*bes2 + a*bes1/z)
>   pp
> }
>
> ## Newton iteration
> newton_gam <- function(z)
> { n   <- length(z)
>   r   <- runif(n)
>   tol <- 1E-6
>   cdf <- vector(length=n, mode="numeric")
>   fprime <- vector(length=n, mode="numeric")
>   f <- vector(length=n, mode="numeric")
>
>  ## Newton loop
>   for (i in 1:1000)
>   { cdf    <- cumdensity(z)
>     fprime <- pdensity(z)
>     f      <- cdf - r
>     # Newton method
>     z      <- z - f/fprime
>
>     if (f < tol) break
>    }
>   cbind(z,cdf)
> }
>
> alp  <- 2.0165
> bt1 <- 29.107 ; bt2 <- 41.517
> r1 <- rgamma(10, shape=alp, scale = bt1)
> r2 <- rgamma(10, shape=alp, scale = bt2)
> z <- (r1/bt1)+(r2/bt2); sort(z)
>
> OUTPUT
>  > z <- (r1/bt1)+(r2/bt2); sort(z)
>  [1] 0.9344932 1.3117707 1.4514991 2.2637735 2.2795669 3.0548586 3.4485707 4.1837583 4.2139719 4.5334232
>
>> newton_gam(z)
>                z       cdf
>  [1,] -25.540473 0.1883006
>  [2,]  -2.079104 0.1725552
>  [3,]  -2.878791 0.1331209
>  [4,] -81.317382 0.1881811
>  [5,]   8.395880 0.1376294
>  [6,] -20.381830 0.1601000
>  [7,]   7.124515 0.1682288
>  [8,]  16.099559 0.1755200
>  [9,]  -2.635536 0.1218351
> [10,]   6.438700 0.1341994
> Warning message:
> In if (f < tol) break :
>   the condition has length > 1 and only the first element will be used
>
> What does this mean?
>
> Thank you for any help given.
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org 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.
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list