0

I have a code that i use to generate balanced incomplete block design in R. We have value of trt as 14 and k as 4 which are fixed. However we change value of b in the find.bib() function to generate a design and check if that design is balanced incomplete block design using isGYD() function.

bibd <- find.BIB(trt = 14, k = 4, b = 64)
bibd
isGYD(bibd)

The problem is i have to do this multiple times and may be hundreds of times until we get confirmation from isGYD function that this design is a balanced incomplete block design. We do this task manually!

My question is can we automate this with a combination of for loop and If Then condition? my algorithm is, for i =1 to 100, create a block design and then test if that design is balanced incomplete design. If it is, save this design and exit from loop. If it was not balanced incomplete design, continue to next iteration of loop.

# run this 1 to 100 times
for (i in 1:100) {
#create balanced incomplete block design by passing value of i fo b
bibd <- find.BIB(trt = 14, k = 4, b = i)
#check if this design is a balanced incomplete block design
if (isGYD(bibd)) {
 #save this design and exit loop
}
#if this iteration didn't give us balanced incomplete block design, take     us to next iteration
}

Any help?

StatguyUser
  • 2,595
  • 2
  • 22
  • 45

1 Answers1

0

Yes, you can simply assign to a variable and break the loop:

bibd.good <- NULL;
for (i in 1:100) {
    bibd <- find.BIB(trt = 14, k = 4, b = i);
    if (isGYD(bibd)) {
        bibd.good <- bibd;
        break;
    };
};
if (!is.null(bibd.good)) {
    ## ... use bibd.good ...
};
bgoldst
  • 34,190
  • 6
  • 38
  • 64
  • Thanks for you comment! I am however getting below error when i ru the code. Error in optBlock(~., withinData = factor(1:trt), blocksizes = rep(k, : The number of withinData rows is not large enough to support the blocked model. – StatguyUser Feb 19 '15 at 18:37
  • It is impossible for me to diagnose that error with the information I have. If you want, post another question on Stack Overflow, and include all the code that is relevant to the error. – bgoldst Feb 19 '15 at 18:42
  • Ignore the above comment. Thanks for you code!! Just not sure where will the validated design be saved? i understand that first if condition is validating whether design is BIBD or not. so i assume just before the "break;" statement, i should add a code to save it? and what is the use of second if statement? is it used when design is not BIBD and so that bibd.good be again null and be send to next iteration? – StatguyUser Feb 19 '15 at 18:47
  • I think there may be some confusion here about the meaning of "save". I took "save" to mean "capture the object in a variable", which `bibd.good <- bibd` accomplishes. But now I'm getting the sense that you want to save the object to a disk file. Is that correct? You can accomplish that with `save(bibd, file='myfile.r' );`, although it will use a binary file format. – bgoldst Feb 19 '15 at 18:48
  • let me rephrase what i wanted to say. i want to know out of 1 to 100 iterations, in which iteration code gave us BIBD. any way i can know that? may be a mesage box or resultant dataset or anything that gives me a hint of the result? – StatguyUser Feb 19 '15 at 18:52
  • Ah, so what you want is to *print* the iteration number when you got the BIBD. You can accomplish that with `print(i);` in the first if block. (The assignment to a variable and the second if block are no longer necessary, since it appears that you don't actually need the BIBD object after you've found it.) – bgoldst Feb 19 '15 at 18:53
  • This is what i did. and i am getting i as 1L. What does that mean? bibd.good <- NULL; for (i in 1:100) { bibd <- find.BIB(trt = 14, k = 4, b = i); if (isGYD(bibd)) { bibd.good <- bibd; print(i) break; }; }; – StatguyUser Feb 19 '15 at 19:12