【问题标题】:Subsetting in Logistic Regression modeling in RR中逻辑回归建模中的子集
【发布时间】:2021-07-01 13:56:56
【问题描述】:

在 R 中的逻辑回归过程中拆分和子集数据时收到以下错误消息。我卡在“子集”步骤。

    library(caTools)
    split <-sample.split(df1, SplitRatio = 0.5)
    split
    training <- subset(df1, split == "TRUE")
    testing <- subset(df1, split == "FALSE")

错误:

错误:必须使用有效的下标向量对行进行子集化。 i 逻辑 下标必须与索引输入的大小相匹配。 x 输入有大小 333030 但下标 i 的大小为 9。运行 rlang::last_error() 来查看 发生错误的位置。

【问题讨论】:

  • 永远不要将 "TRUE" 和 "FALSE"` 放在引号中 - 这会使它们成为字符串,而不是逻辑值。这不一定是您的问题,但这是最佳做法。
  • 我会尝试 training = df1[split, ]testing = df1[!split, ] 但可能会检查 head(split) 以确保它是真/假值,并将 length(split)nrow(df1) 相比以确保大小正确.
  • 在这种情况下,删除引号并不能解决问题。 split 有 9 个值,TRUE 或 FALSE。我认为这是造成问题的原因。如错误消息所示,我在 df1 中有 333,030 行。同时,我应该使用什么分流比?谢谢!

标签: r regression


【解决方案1】:

您正在拆分列。如果您阅读了帮助页面:

用法:

  sample.split( Y, SplitRatio = 2/3, group = NULL )
  Arguments:

   Y: Vector of data labels. If there are only a few labels (as is
      expected) than relative ratio of data in both subsets will be
      the same.

您提供的是整个数据框,它以列表的形式读取。因此,如果您有一个因变量,例如 y ,它将是:

split <-sample.split(df1$y, SplitRatio = 0.5)
training <- df1[split,]
testing <- df1[!split,]

或者

split <-sample.split(1:nrow(df1), SplitRatio = 0.5)
training <- df1[split,]
testing <- df1[!split,]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2016-12-11
    • 2018-02-12
    • 2014-06-20
    相关资源
    最近更新 更多