【问题标题】:ROSE: data argument is of the wrong type in RROSE:数据参数在 R 中的类型错误
【发布时间】:2019-08-09 23:11:34
【问题描述】:

我正在尝试执行如下函数来平衡火车组与包 ROSE:

library(ROSE)

rose <- function(df){
  str(df)
  set.seed(124)
  intrain <- sample(seq_len(nrow(df)), size = floor(0.7 * nrow(df)))
  train <- df[intrain,]
  train.rose <- ovun.sample(cls ~ ., data=train, N=nrow(train), p=0.5, seed=1, method="both")$data
  return(train.rose)
}

data(hacide)
df <- rbind(hacide.train, hacide.test) # just to simulate a complete dataset

rose(df)

调用上述脚本会产生以下错误信息:

Error in terms.formula(formula, data = frml.env) : 
 'data' argument is of the wrong type 

相反,当我在本地函数rose 之外调用ovun.sample(...) 函数时,一切正常,即:

library(ROSE)

data(hacide)
df <- rbind(hacide.train, hacide.test) # just to simulate a complete dataset

str(df)
set.seed(124)
intrain <- sample(seq_len(nrow(df)), size = floor(0.7 * nrow(df)))
train <- df[intrain,]
train.rose <- ovun.sample(cls ~ ., data=train, N=nrow(train), p=0.5, seed=1, method="both")$data

我知道在 rose() 内调用函数 ovun.sample(..., data=train,...) 时会出现问题,但我不知道为什么。会不会是环境变量的问题?

有什么想法吗?

【问题讨论】:

    标签: r rstudio rebalancing


    【解决方案1】:

    我在没有set.seed(1234) 的情况下执行了代码,它对我有用,你应该在函数中设置一个种子。另外,也许您激活了一些库,导致R 出现混乱。

    rose <- function(df){
      str(df)
      intrain <- sample(seq_len(nrow(df)), size = floor(0.7 * nrow(df)))
      train <- df[intrain,]
      train.rose <- ovun.sample(cls ~ ., data=train, N=nrow(train), p=0.5, seed=1, method="both")$data
      return(train.rose)
    }
    
    data(hacide)
    df <- rbind(hacide.train, hacide.test) # just to simulate a complete dataset
    
    
    set.seed(1234)
    head(rose(df))
    'data.frame':   1250 obs. of  3 variables:
     $ cls: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
     $ x1 : num  0.2008 0.0166 0.2287 0.1264 0.6008 ...
     $ x2 : num  0.678 1.5766 -0.5595 -0.0938 -0.2984 ...
      cls         x1         x2
    1   0 -0.2247632  0.6806409
    2   0  0.3437585 -1.0202996
    3   0 -1.0226182  1.9629034
    4   0  0.7245372 -0.2494658
    5   0 -0.8972314  0.2397664
    6   0  0.3361091 -0.2661655
    

    此外,出现的str 指的是原始df,而不是关于转换。

    【讨论】:

    • 我看不出将种子移出函数或删除它如何使代码工作。无论如何,我想这可能是图书馆的问题,因为问题仍然存在。谢谢你的回复,顺便说一句!
    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 2019-09-16
    • 2011-12-27
    • 2021-05-04
    • 2014-03-28
    • 2015-02-06
    相关资源
    最近更新 更多