Hi listers, I am working on a program of statistical analysis of simulated data and I've been searching the error at the program, but I didn't find it! It is something about the WHILE procedure, the error says: Error in while (ecart >= d) { : missing value where TRUE/FALSE needed Thanks in advance! M?rcio k<-100 d<-0.00112 z<-rnorm(100, 0, 1) prop<-rep(0,100) for (i in 1:100){ if (z[i]>1.75){ prop[i]<-1} else{prop[i]<-0}} proportion<-mean(prop) ecart<-sqrt((proportion*(1-proportion))/k) while(ecart>=d){ prop_<-0 w<- rnorm(1, 0, 1) z<-c(z,w) {if (w>1.75){ prop_<-1} else{ prop_<-0} } prop<-c(prop, prop_) proportion<-proportion+(prop[k+1]-proportion/(k+1)) ecart<-sqrt((proportion*(1-proportion))/(k+1)) k<-k+1 } -- View this message in context: http://www.nabble.com/Simulation-of-data-tp20082754p20082754.html Sent from the R help mailing list archive at Nabble.com.
2008/10/21 Marcioestat <marcioestat at pop.com.br>:> > Hi listers, > I am working on a program of statistical analysis of simulated data and I've > been searching the error at the program, but I didn't find it! > It is something about the WHILE procedure, the error says: Error in while > (ecart >= d) { : missing value where TRUE/FALSE neededHow much do you know about debugging programs? Read the R help for "debug" and "browser". These tools let you inspect your program at any point so you can see what the values of variables are. Anyway, for you error message, I'd guess there was a missing value in 'ecart' or 'd' or both: > ecart [1] NaN > d [1] 0.00112 so, ecart is Not A Number. How could that happen? Well, ecart is a square root of something, so maybe that something is negative: > proportion*(1-proportion) [1] -0.01874365 Hmmm > proportion [1] 1.018405 I would bet that proportion isn't supposed to be more than 1 (or less than 0). How did that happen? This line looks a bit dodgy: proportion<-proportion+(prop[k+1]-proportion/(k+1)) since you're adding something to a proportion... Should that division be outside the parentheses? Hard to tell without knowing exactly what the code is trying to do. The proportion goes >1 when w is over your 1.75 threshold. Are you just trying to update proportion=mean(prop) again? Why not do that? If I replace your line with proportion mean(prop) then it runs and terminates after about 32000 iterations. Protip: Updating a mean like this is probably quicker but if you ever write a tricky bit of code test it first! Barry