【问题标题】:Issue while using weighted regression (lm) with boot package in R在 R 中将加权回归 (lm) 与引导包一起使用时出现问题
【发布时间】:2017-05-12 21:30:00
【问题描述】:

我在使用 boot 函数将参数传递给统计数据(我有一个函数)时遇到问题。这是一个例子:

rm(list=ls())
library(faraway)
library(boot)
data(strongx)
g3 <- lm(crossx ~ energy, strongx, weights=rep(1, nrow(strongx)))

bootReg <- function(formulatostat, data, ind, weitostat) {
  weig <- weitostat[ind]
  fit <- lm(formula=formulatostat, data = data[ind, ], weights = weig)
  return(coef(fit))
}
wei <- rep(1, nrow(strongx))

bootResults <-
  boot(
    data = strongx,
    statistic = bootReg,
    R = 2000,
    formulatostat = crossx ~ energy,
    weitostat = wei)

eval 中的错误(expr、envir、enclos):找不到对象“weig”

当然,我的权重是 1,所以加权部分是不必要的。但是在使用实际权重时问题仍然存在。这证明了我在使用其他数据时遇到的问题。 boot 函数声明:“任何进一步的参数都可以通过 ... 参数传递给 statistic。”看起来,formulatostat 被传递给 bootReg 而不是权重位。比较没有权重:

bootReg <- function(formulatostat, data, ind) {
  fit <- lm(formula=formulatostat, data = data[ind, ])
  return(coef(fit))
}
wei <- rep(1, nrow(strongx))

bootResults <-
  boot(
    data = strongx,
    statistic = bootReg,
    R = 2000,
    formulatostat = crossx ~ energy)

这行得通。任何有关如何合并权重的建议都将不胜感激。

【问题讨论】:

  • 这是本网站的主题。
  • 欢迎使用交叉验证。请参阅引导手册:“传递的第一个参数将始终是原始数据。第二个参数将是定义引导样本的索引、频率或权重的向量。” >> 尝试将“data”和“ind”参数切换到函数中的第 1 位和第 2 位。顺便说一句:这不是调试问题的正确论坛(统计问题,所以我想这个 q 很快就会被删除)+如果您提供运行代码(包括示例数据),您可以增加将来获得好的答案的机会。干杯
  • 道歉。我发布在 stats.stackexchange 上。这怎么跑题了?抱歉我的无知,但这是我第一次在这里发帖。
  • 致 mzunhammer,谢谢。我试过你的建议,但它不起作用。我提出的示例代码实际上是完全可重现的。上面的数据如其所示来自遥远的包裹。
  • 仅关于 R 代码如何工作的问题通常不在此处讨论。这是一个关于统计问题的网站。由于这有一个可重现的例子,这应该是关于Stack Overflow 的主题,这是一个关于代码的站点。如果您等待,我们可以尝试为您迁移。

标签: r regression lm


【解决方案1】:

似乎stats::model.frame 函数(在lm 函数中调用)正在为data 参数提供的对象中寻找名为weig 的对象,并且在父环境中公式 (crossx ~ energy) 已定义。

data 的 data.frame 中没有名为 weig 的列。而且由于您在bootReg 函数之外定义了公式crossx ~ energy,因此lm 函数无法找到weig 对象。

您可以通过将权重添加到给定给data 参数的对象,或者通过在调用lm 函数的相同环境中创建公式对象来修复错误。

解决方案 1:

rm(list=ls())
library(faraway)
library(boot)
data(strongx)
g3 <- lm(crossx ~ energy, strongx, weights=rep(1, nrow(strongx)))

bootReg <- function(formulatostat, data, ind, weitostat) {
  input_to_lm <- data[ind, ]
  input_to_lm$weig <- weitostat[ind]
  fit <- lm(formula=formulatostat, data = input_to_lm, weights = weig)
  return(coef(fit))
}
wei <- rep(1, nrow(strongx))

bootResults <-
  boot(
    data = strongx,
    statistic = bootReg,
    R = 2000,
    formulatostat = crossx ~ energy,
    weitostat = wei)

解决方案 2:

rm(list=ls())
library(faraway)
library(boot)
data(strongx)
g3 <- lm(crossx ~ energy, strongx, weights=rep(1, nrow(strongx)))

bootReg <- function(formulatostat, data, ind, weitostat) {
  weig <- weitostat[ind]
  fit <- lm(formula=as.formula(formulatostat), data = data[ind, ], weights = weig)
  return(coef(fit))
}
wei <- rep(1, nrow(strongx))

bootResults <-
  boot(
    data = strongx,
    statistic = bootReg,
    R = 2000,
    formulatostat = 'crossx ~ energy',
    weitostat = wei)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-26
    • 2011-03-15
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    相关资源
    最近更新 更多