【问题标题】:R error message in my bootstrapping & regression code我的引导和回归代码中的 R 错误消息
【发布时间】:2020-05-14 12:01:37
【问题描述】:

我想使用引导程序并运行回归,但由于某种原因,它不起作用。我总是收到错误消息“Anzahl der zu ersetzenden Elemente ist kein Vielfaches der Ersetzungslänge”(英文:显然我有两组元素,我想将一组元素更改为另一组,但它们不匹配)。

有谁知道这里出了什么问题:

    bootReg <- function(formula,data,indices)
    {
    d <- data[indices,]
    fit <- lm(formula, data=d)
    return(coef(fit))  
    }

    results <- boot(statistic = bootReg, 
    formula = RT ~ Code + Situation + Block, data = reg_df, R = 2000)



    #RT = reaction times (--> numeric)
    #Situation = "lab" or "online" 
    #Block = either 0,1,2 or 3 (--> as characters)
    #Code = each subject's individual code

组中的数据是相关的(= 在每种情况 X 块组合中每个受试者都有 RT)

提前致谢!

P.S.:我用谷歌搜索了错误消息并将我的代码与其他人的(工作)方法进行了比较,但不知道这里发生了什么。

【问题讨论】:

  • 在不查看数据的情况下很难解决。 lm(RT ~ Code + Situation + Block, data = reg_df) 会报错吗? bootReg(RT ~ Code + Situation + Block, data=reg_df, indices = 1:5) 怎么样?
  • 第一个有效!!!非常感谢!!!
  • 第一个命令只是一个没有引导程序的普通 OLS。我不确定这是否是您要找的东西?如果您使用 ?dput() 将部分数据附加到问题中,也许您会得到更好的答案
  • 我知道,但是我通过添加 lm() 修复了 boot() 函数中的公式(我认为 fit &lt;- lm(formula, data=d) 部分会自动执行,但显然它没有)现在它可以工作了. :-)
  • @OttoKässi 请发布您的解决方案作为答案。这确保了它对未来读者的可用性。

标签: r regression statistics-bootstrap


【解决方案1】:

这对我有用,你必须在 boot() 的公式中添加 lm()。

bootReg <- function(formula, 
                    data, 
                    indices)
{
  d <- data[indices,]
  fit <- lm(formula, data=d)
  return(coef(fit))  
}

results <- boot(statistic = bootReg, formula = lm(RT ~ Code + Situation + Block, data = df), data = df, R = 2000)

【讨论】:

    【解决方案2】:

    (我意识到这是一条评论,但我想继续作为解决问题的方法的答案。如果 OP 提供更多源信息,我将添加具体诊断)

    调试通用教程:

    首先,尝试traceback() 看看是内部调用引发了错误还是boot 本身引发了错误。

    接下来,看看你的bootReg 函数返回的对象的类和大小。 boot 将接受 statistic 输入吗?您的formula 是否返回您期望它返回的内容(再次,类和长度)?您确定dataindices 输入以正确的顺序输入到您的formula 吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 2011-07-10
      • 2016-07-06
      • 2021-07-09
      相关资源
      最近更新 更多