2017-12-10 29 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) 



} 

ではなく、単に私はpredict関数の結果を格納したいと、ループのすべての50回の反復confusionMatrix機能を実装しmyconfを印刷します。

答えて

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はまったく必要ありません。

関連する問題