4

boot() 在一个数据集上失败并在另一个数据集上成功......一定是数据问题吗?我只是无法弄清楚区别。但至少现在我认为我已经得到了它的重现性。在这两种情况下,整数和因子变量之间的交互作用都会回归 (lm) 到数值因变量上。boot() 命令失败并出现以下错误:

Error in boot(data = data, statistic = bs_p, R = 1000) : 
  number of items to replace is not a multiple of replacement length

我返回 p 值的统计函数是:

    bs_p <- function (data, i) {
      d <- data[i,]
      fit <- lm (y~x*fac, data=d)
      return(summary(fit)$coefficients[,4])
    }

当我生成随机数据以便在此处重现和发布问题时,如下所示:

    L3 <- LETTERS[1:3]
    data <- data.frame(x=1:50, y=rnorm(1:50), fac=as.factor(sample(L3, 50, replace = TRUE)))

然后引导:

    results <- boot(data=data, statistic=bs_p, R=1000)

引导工作;没有错误;生成的统计数据。但是对于我自己的数据(如下),相同类型的,boot() 返回错误。

    y <- c(17.820, 13.764, 18.880, 25.830, 26.576, 29.832, 22.610, 24.180, 26.572, 26.030, 29.200, 28.560, 28.600, 16.614, 16.302, 18.080, 22.704, 28.101, 38.280, 17.100, 19.292, 33.165, 18.395, 19.434, 27.544, 17.010, 21.560, 28.120, 17.513, 21.646,24.060, 27.984, 20.830, 21.588, 26.280, 29.640, 17.313, 16.344, 16.362, 34.496, 22.785, 20.203, 29.040, 19.092, 20.890,20.739, 17.700, 17.424, 28.737, 18.318, 39.470, 28.072, 17.176, 28.098)
    x <- as.integer(c(9,  5,  0,  8,  3,  4,  9,  6,  9,  2, 15, 10,  5,  1, 11, 11,  4, 8, 13,  1,  2,  4,  7,  7, 12,  1,  6,  6,  4,  3,  5,  5,  7,  9,  8, 3, 3, 14,  6,  4,  3,  6, 17,  3,  6,  6,  7,  1,  6, 10 , 2, 14 , 5,  8))
    fac <- as.factor(c("F", "F", "F", "F", "F", "Ds", "F", "Ds","F","F","F","E", "Ds","F", "F", "E", "Ds","F", "Ds", "F", "Ds","E", "F", "E", "F", "Ds", "E", "Ds","F", "F", "F",  "Ds","Ds", "F", "Ds","F", "F", "E", "F","F","F", "F", "F", "Ds","F", "F", "F", "F", "Ds", "E", "F", "F", "F", "E"))
    data <- data.frame(x=x, y=y, fac=fac)

线性模型可以单独使用这些数据运行良好。traceback() 除了启动调用之外什么都不产生。请,欢迎任何想法。我在 MAC OSX 上使用 R 3.0.1。谢谢!

4

2 回答 2

3

一些(或至少一个)bootstrap 重采样不包含所有因子水平,导致系数(和相应的 p 值)数量较少,从而导致组合 bootstrap 结果时出现错误。我想你需要残差的分层引导或引导(假设引导 p 值是明智的,我对此表示怀疑)。

于 2013-09-15T20:10:40.403 回答
0

我有一个类似的错误,我用这个手工代码解决了,我希望它对某人有所帮助。

bs_p <- function (data, i) {
  d <- data[i,]
  fit <- lm (y~x*fac, data=d)

  cf <- coef(fit)

  # identify differing coefficients and create dummy ones
  df <- setdiff(colnames(d), names(cf))
  ad <- rep(0, length(df))
  names(ad) <- df

  return(c(cf, ad))
}
于 2020-04-01T16:25:54.550 回答