[R] Problem in applying conditional looping
jim holtman
jholtman at gmail.com
Thu Jul 3 13:58:52 CEST 2008
You can use something like this to keep running your algorithm
# repeat until all values are positive
repeat{
x <- rnorm(10)
if (all(x >= 0)) break # conditions met; quit
}
On Thu, Jul 3, 2008 at 5:25 AM, Nadeem Shafique
<nadeemshafique at gmail.com> wrote:
> Respected All,
>
> I hope you are enjoying good health, I am tring to write a program in R but
> could not be very sucessful. My program draws random sample form bivariate
> normal distribution and then compute a variable PIJ. For certian samples
> some entries of variable PIJ is apearing as negative, which result
> in negative variance estimator. I want to introduce a loop in my program
> that verify the each value of PIJ and if any value in PIJ is negitive return
> back and draw another sample until a sample is selected that produces all
> values of PIJ positive.
>
> library(mvtnorm)
> library(combinat)
> yg <- function(n,m,s) {
> n<-10
> m<-c(1,2)
> s<- matrix(c(4,.2,.2,3), ncol=2)
> x <- rmvnorm(n=n, mean=m, sigma=s)
> pi <- x[,2]/sum(x[,2])
> P <- pi*((1-2*pi)/(1-pi)+sum(pi/(1-pi)))
> Pi<-t(combn(pi,2))
> y<-t(combn(x[,1],2))
> PI<-t(combn(P,2))
> yht <- y[,1]/PI[,1]+ y[,2]/PI[,2]
>
> *PIJ <- (Pi[,1]*Pi[,2]*(2-Pi[,1]-Pi[,2]))/((1-Pi[,1])*(1-Pi[,2]))*
>
>
> Eyht <- sum(yht*PIJ)
> E2yht <- sum(yht^2*PIJ)
> var1 <- E2yht - Eyht^2
> var1
> }
> I want to impose the condition on PIJ(which is a vector). If any value in
> the PIJ is negative then go back and take another sample until a sample is
> selected which produces the PIJ positive(all values in PIJ).
>
> I hope now you can understand.
>
> Best Regards,
>
> Nadeem Shafique Butt
>
> [[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 you are trying to solve?
More information about the R-help
mailing list