我想将适合AIC的循环结果存储到数据框中,当我尝试在块内打印时,数据框已成功创建,但是当我在循环外部调用数据框时,它似乎是空的,我我想知道可能是什么问题。这是代码。
A<-data.frame(AIC_ori=numeric(0),AIC_len=numeric(0),AIC_2dl=numeric(0))
for (i in 1:17){
fit.ori <- glc(key_resp_3.keys ~ ori_transform, data=d[d$block== i,], category=d[d$block== i,]$corrAns, zlimit=7)
fit.len <- glc(key_resp_3.keys ~ length, data=d[d$block== i,], category=d[d$block== i,]$corrAns, zlimit=7)
fit.2dl <- glc(key_resp_3.keys ~ ori_transform + length, data=d[d$block== i,], category=d[d$block== i,]$corrAns, zlimit=7)
#This code compares the three by calculating an AIC. Lower is better
A[i,1]=AIC(fit.ori)
A[i,2]= AIC(fit.len)
A[i,3]=AIC(fit.2dl)
print(A)
}
答案 0 :(得分:1)
没有数据,以下内容未经测试,但我相信它可以完成for
循环应该做的事情。
A <- lapply(1:17, function(i){
k <- d$block == i
fit.ori <- glc(key_resp_3.keys ~ ori_transform, data = d[k, ], category = d$corrAns[k], zlimit = 7)
fit.len <- glc(key_resp_3.keys ~ length, data = d[k, ], category = d$corrAns[k], zlimit = 7)
fit.2dl <- glc(key_resp_3.keys ~ ori_transform + length, data = d[k, ], category = d$corrAns[k], zlimit = 7)
data.frame(AIC_ori = AIC(fit.ori), AIC_len = AIC(fit.len), AIC_2dl = AIC(fit.2dl))
})
A <- do.call(rbind, A)