# [R] Problem in applying conditional looping

Date: Thu, 03 Jul 2008 10:25:07 +0100

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).

Best Regards,