R语言:将循环结果存储到表中 [英] R Language: Storing Results of a Loop into a Table

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

问题描述

我正在使用R编程语言.我正在学习如何循环过程并将结果存储到表中.对于此示例,我首先生成了一些数据:

I am using the R programming language. I am learning about how to loop a procedure and store the results into a table. For this example, I first generated some data:

#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)

然后,我创建一个最终表,希望将循环的结果存储在该表中:

Then, I create the final table where I want the results from the loop to be stored:

#create the final results table in which the results of the loop will be stored
final_table = matrix(1, nrow = 6, ncol=2)

这是我要循环的过程.基本上,我想在此数据上拟合决策树模型.我想适合6种不同的决策树:变量"group_1"如果"group_1>",则响应变量(响应变量)变为因子变量("1"或"0").我"."i"表示变量采用6个值(400,401,402,403,404,405).因此,决策树适合6次.我想将每个决策树的准确性存储到"final_table"中:

Here is the procedure that I want to loop. Basically, I want to fit a decision tree model on this data. I want to fit 6 different decision trees: the variable "group_1" (the response variable) becomes a factor variable ("1" or "0") if "group_1 > i". The "i" variable takes 6 values (400,401,402,403,404,405). Thus, the decision tree is fit 6 times. I want to store the accuracy of each one of these decision trees into the "final_table":

for (i in 400:405) 
{
  d$group_1 = ifelse(d$group_1 > i, "1","0")
  d$group_1 = as.factor(d$group_1)
  
  
  trainIndex <- createDataPartition(d$group_1, p = .8,
                                    list = FALSE,
                                    times = 1)
  training = d[ trainIndex,]
  test  <- d[-trainIndex,]
  
  
  fitControl <- trainControl(## 10-fold CV
    method = "repeatedcv",
    number = 10,
    ## repeated ten times
    repeats = 10)
  
  TreeFit <- train(group_1 ~ ., data = training,
                   method = "rpart2",
                   trControl = fitControl)
  
  pred = predict(TreeFit, test, type = "prob")
  labels = as.factor(ifelse(pred[,2]>0.5, "1", "0"))
  con = confusionMatrix(labels, test$group_1)
  
  #update results into table
  row = i - 399
  final_table[row,1] = con$overall[1]
  final_table[row,2] = i
}

但是,这给了我以下错误:

However, this gives me the following errors:

Error in na.fail.default(list(group = c(2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,  : 
      missing values in object
    In addition: Warning message:
    In Ops.factor(d$group_1, i) : ‘>’ not meaningful for factors

有人可以告诉我我在做什么错吗?

Can someone please tell me what I am doing wrong?

谢谢

推荐答案

您可以在任何其他变量中创建原始数据框的副本,该变量可用于在每次迭代中覆盖更改的数据框.

You can create a copy of your original dataframe in any other variable which can be used to overwrite the changed dataframe in every iteration.

library(caret)
library(rpart)

e <- d

for (i in 400:405) {
  d <- e
  d$group_1 = as.integer(d$group_1 > i)
  d$group_1 = as.factor(d$group_1)
  
  trainIndex <- createDataPartition(d$group_1, p = .8,list = FALSE,times = 1)
  training = d[ trainIndex,]
  test  <- d[-trainIndex,]
  
  
  fitControl <- trainControl(## 10-fold CV
    method = "repeatedcv",
    number = 10,
    ## repeated ten times
    repeats = 10)
  
  TreeFit <- train(group_1 ~ ., data = training,
                   method = "rpart2",
                   trControl = fitControl)
  
  pred = predict(TreeFit, test, type = "prob")
  labels = as.factor(ifelse(pred[,2]>0.5, "1", "0"))
  con = confusionMatrix(labels, test$group_1)
  
  #update results into table
  row = i - 399
  final_table[row,1] = con$overall[1]
  final_table[row,2] = i
  
}

final_table
#      [,1] [,2]
#[1,] 0.585  400
#[2,] 0.618  401
#[3,] 0.598  402
#[4,] 0.608  403
#[5,] 0.533  404
#[6,] 0.570  405

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

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