我有一个代码用于在R中生成平衡的不完整块设计。我们将trt的值设为14,将k设置为4,这是固定的。但是,我们在find.bib()函数中更改b的值以生成设计,并使用isGYD()函数检查该设计是否是平衡的不完整块设计。
bibd <- find.BIB(trt = 14, k = 4, b = 64)
bibd
isGYD(bibd)
问题是我必须多次这样做,并且可能需要数百次,直到我们从isGYD函数确认此设计是平衡的不完整块设计。我们手动完成这项任务!
我的问题是我们可以通过for循环和If Then条件的组合自动化吗?我的算法是,对于i = 1到100,创建一个块设计,然后测试该设计是否是平衡的不完整设计。如果是,请保存此设计并退出循环。如果不是平衡不完整的设计,继续循环的下一次迭代。
# 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
}
任何帮助?
答案 0 :(得分:0)
是的,你可以简单地分配一个变量并打破循环:
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 ...
};