R:保存循环结果 [英] R: Saving the Results of a Loop

查看:56
本文介绍了R:保存循环结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言.我正在学习循环以及如何存储循环结果.例如,我编写了以下代码来循环一个函数(生成随机数据并适合不同的决策树):

I am using the R programming language. I am learning about loops and how to store the results of a loop. For instance, I wrote the following code that loops a function (generates random data and fits different decision trees):

#load libraries    
    library(caret)
library(rpart)

#generate data

a = rnorm(1000, 10, 10)

b = rnorm(1000, 10, 5)

c = rnorm(1000, 5, 10)

group <- sample( LETTERS[1:2], 1000, replace=TRUE, prob=c(0.5,0.5) )
group_1 <- 1:1000

#put data into a frame
d = data.frame(a,b,c, group, group_1)

d$group = as.factor(d$group)

#place results in table
#final_table = matrix(1, nrow = 10, ncol=10)


e <- d



vec1 <- sample(200:300, 5)
vec2 <- sample(400:500,5)
vec3 <- sample(700:800,5)

for (i in seq_along(vec1)) { 
    for (j in seq_along(vec2)) {
        for (k in seq_along(vec3)) {
            # d <- e
            d$group_2 = as.integer(ifelse(d$group_1 < vec1[i] , 0, ifelse(d$group_1 >vec1[i]  & d$group_1 < vec2[j] , 1, ifelse(d$group_1 >vec2[j]  & d$group_1 < vec3[k] , 2,3))))
            
            
            
            
            d$group_2 = as.factor(d$group_2)
            
            fitControl <- trainControl(## 10-fold CV
                method = "repeatedcv",
                number = 2,
                ## repeated ten times
                repeats = 1)
            
            TreeFit <- train(group_2 ~ ., data = d[,-5],
                             method = "rpart",
                             trControl = fitControl)
            
            pred <- predict(
                TreeFit,
                d[,-5])
            
            con <- confusionMatrix(
                d$group_2,
                pred) 
            
            #update results into table
            #final_table[i,j] = con$overall[1]
            acc=c(vec1[i],vec2[j],vec3[k],con$overall[1])
            print(acc)
            
            
        }
    }
}

我有兴趣保存"acc"结果并保存到放入表格(或数据框)中.我能够打印"acc"的所有值,但是当我正式查看"acc"的结果时,:仅显示最后一行.

I am interested in saving the results of "acc" into a table (or a data frame). I am able to print all values of "acc", but when I formally view the results of "acc" : only the last line is displayed.

我的问题:是否可以提取整个打印输出(即"acc")并将其存储在表格中?

My question: is it possible to take the entire printed output (i.e. "acc") and store it into a table?

谢谢

推荐答案

您可以使用 expand.grid 创建 vec1 vec2 vec3 ,并在数据帧中的每次迭代中保存 con $ overall [1] .

You can use expand.grid to create all possible combinations of vec1, vec2 and vec3 and save con$overall[1] on each iteration in the dataframe.

library(caret)
library(rpart)

#generate data

a = rnorm(1000, 10, 10)
b = rnorm(1000, 10, 5)
c = rnorm(1000, 5, 10)
group <- sample( LETTERS[1:2], 1000, replace=TRUE, prob=c(0.5,0.5))
group_1 <- 1:1000

#put data into a frame
d = data.frame(a,b,c, group, group_1)
d$group = as.factor(d$group)

e <- d
vec1 <- sample(200:300, 5)
vec2 <- sample(400:500,5)
vec3 <- sample(700:800,5)
z <- 0
df <- expand.grid(vec1, vec2, vec3)
df$Accuracy <- NA

for (i in seq_along(vec1)) { 
  for (j in seq_along(vec2)) {
    for (k in seq_along(vec3)) {
      # d <- e
      d$group_2 = as.integer(ifelse(d$group_1 < vec1[i] , 0, ifelse(d$group_1 >vec1[i]  & d$group_1 < vec2[j] , 1, ifelse(d$group_1 >vec2[j]  & d$group_1 < vec3[k] , 2,3))))
      
      d$group_2 = as.factor(d$group_2)
      
      fitControl <- trainControl(## 10-fold CV
        method = "repeatedcv",
        number = 2,
        ## repeated ten times
        repeats = 1)
      
      TreeFit <- train(group_2 ~ ., data = d[,-5],
                       method = "rpart",
                       trControl = fitControl)
      
      pred <- predict(
        TreeFit,
        d[,-5])
      
      con <- confusionMatrix(
        d$group_2,
        pred) 
      
      #update results into table
      #final_table[i,j] = con$overall[1]
      z <- z + 1
      df$Accuracy[z] <- con$overall[1]
    }
  }
}

head(df)

#  Var1 Var2 Var3 Accuracy
#1  300  492  767    0.299
#2  202  492  767    0.299
#3  232  492  767    0.299
#4  293  492  767    0.376
#5  231  492  767    0.299
#6  300  435  767    0.331

这篇关于R:保存循环结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆