2017-12-10 26 views
0

혼돈 행렬의 결과를 for 루프에 저장하려고합니다. 지금 루프는 각 반복마다 분석 결과를 인쇄합니다. 혼돈 행렬의 결과를 저장하는 루프

for(i in 1:50){ 
train.cpg.t2 <- sample(nrow(cpg.updated.2), 2/3 * nrow(cpg.updated.2)) 
cpg.train.t2 <- cpg.updated.2[train.cpg.t2, ] 
cpg.test.t2 <- cpg.updated.2[-train.cpg.t2, ] 

model.nb2 <- naiveBayes(group ~ ., data = cpg.train.t2) 
naive.cpg2<-predict(model.nb2, cpg.test.t2) 
myconf<-confusionMatrix(predict(model.nb2, cpg.test.t2), cpg.test.t2$group) 

print(myconf) 



} 

보다는

단순히 myconf 나는 confusionMatrix 기능을 루프의 50 반복을 구현 한 후 저장에게 predict 함수의 결과가 마음에와 것 인쇄.

답변

0

이 질문을 개선하는 방법은 여러 가지가 있지만 내가하려는 것은 replicate입니다.

f <- function(cpg.updated.2) { 
train.cpg.t2 <- sample(nrow(cpg.updated.2), 2/3 * nrow(cpg.updated.2)) 
cpg.train.t2 <- cpg.updated.2[train.cpg.t2, ] 
cpg.test.t2 <- cpg.updated.2[-train.cpg.t2, ] 

model.nb2 <- naiveBayes(group ~ ., data = cpg.train.t2) 
naive.cpg2<-predict(model.nb2, cpg.test.t2) 
myconf<-confusionMatrix(predict(model.nb2, cpg.test.t2), cpg.test.t2$group) 
myconf 
} 

results <- replicate(n = 50, expr = f(cpg.updated.2)) 

조언 단어. 질문을 조금만 개선하면 더 나은 품질의 답변을 얻을 수 있습니다. 우선, confusionMatrix은 익숙한 패키지에 들어 있지 않으므로 추가 패키지를 설치하지 않고 코드를 실행하는 것은 불가능합니다. 그다지 좋지 않습니다. 그러나 질문은 크게 단순화 될 수 있으므로 루프 출력을 저장하는 것이므로 confusionMatrix은 전혀 필요하지 않습니다.