【发布时间】:2016-06-29 17:48:57
【问题描述】:
我正在编写一个代码,该代码将计算具有多个参数的统计模型,我想在此过程中对其进行更改。然后,根据模型的性能,我会选择最好的。
我写了这么一段伪代码来说明问题:
## vectors with values of parameters
const1 <- c(300, 500)
const2 <- c(1, 2, 3, 4, 5)
const3 <- c(30, 50, 70, 90, 110, 130)
## loop counter
i = 1
for (j in 1:length(const1)){
for (k in 1:length(const2)){
for (l in 1:length(const3)){
## i-th model
model <- stat.model(x = train,
y = target,
param1 = const1,
param2 = const2,
param3 = const3)
## ... outputing model results to a data table
## printing the number of iteration
cat("iteration =", i)
i <- i+1
## calling garbage collector to assure free space in RAM
gc()
}
}
}
如您所见,我使用嵌套的“for”循环,这可能不是 R 中最有效的编程方式。有什么方法可以缩短处理时间(并可能节省代码的易读性)?
【问题讨论】:
标签: r performance loops for-loop time