【问题标题】:John Tukey "median median" (or "resistant line") statistical test for R and linear regressionR 和线性回归的 John Tukey “中位数”(或“阻力线”)统计检验
【发布时间】:2011-03-14 13:48:56
【问题描述】:

我正在搜索 John Tukey 算法,该算法使用 R 在我的线性回归上计算“阻力线”或“中位数-中位数线”。

一个邮件列表上的学生用这些术语解释了这个算法:

"它的计算方式是除以 将数据分成三组,找到 x 中值和 y 中值(称为 每个组的总结点),以及 然后使用这三个总结点 确定线路。外二 汇总点确定斜率, 和他们所有人的平均值 确定截距。”

关于 John tukey 的好奇中位数的文章:http://www.johndcook.com/blog/2009/06/23/tukey-median-ninther/

你知道我在哪里可以找到这个算法或 R 函数吗?在哪些包裹中, 非常感谢!

【问题讨论】:

  • 实际上,函数line 就是这样做的。或者应该...

标签: algorithm function r testing linear-regression


【解决方案1】:

这里有关于如何计算中位数线here 的说明。一个 R 实现是

median_median_line <- function(x, y, data)
{
  if(!missing(data))
  {
    x <- eval(substitute(x), data) 
    y <- eval(substitute(y), data) 
  }
  
  stopifnot(length(x) == length(y))

  #Step 1
  one_third_length <- floor(length(x) / 3)
  groups <- rep(1:3, times = switch((length(x) %% 3) + 1,
     one_third_length,
     c(one_third_length, one_third_length + 1, one_third_length),
     c(one_third_length + 1, one_third_length, one_third_length + 1)
  ))

  #Step 2
  x <- sort(x)
  y <- sort(y)
  
  #Step 3
  median_x <- tapply(x, groups, median)                                 
  median_y <- tapply(y, groups, median)

  #Step 4
  slope <- (median_y[3] - median_y[1]) / (median_x[3] - median_x[1])
  intercept <- median_y[1] - slope * median_x[1]

  #Step 5
  middle_prediction <- intercept + slope * median_x[2]
  intercept <- intercept + (median_y[2] - middle_prediction) / 3
  c(intercept = unname(intercept), slope = unname(slope))
}

为了测试它,这里有一个例子:

dfr <- data.frame(
  time = c(.16, .24, .25, .30, .30, .32, .36, .36, .50, .50, .57, .61, .61, .68, .72, .72, .83, .88, .89),
  distance = c(12.1, 29.8, 32.7, 42.8, 44.2, 55.8, 63.5, 65.1, 124.6, 129.7, 150.2, 182.2, 189.4, 220.4, 250.4, 261.0, 334.5, 375.5, 399.1))
  
median_median_line(time, distance, dfr) 
#intercept     slope 
#   -113.6     520.0

请注意指定组的方式有点奇怪。说明对于如何定义组大小非常挑剔,因此cut(x, quantile(x, seq.int(0, 1, 1/3))) 更明显的方法不起作用。

【讨论】:

  • 哇!谢谢很多里奇棉花!这是完美的;)
  • dfr
  • 第一行的链接现在好像坏了。
【解决方案2】:

作为R Core团队的一员,我现在已经深入挖掘了源代码,也研究了它的历史。

结论:在 19961997 年添加的源 C 源代码,当时 R 仍被称为 alpha(和 0.14alpha 版本左右)已经计算了不太正确的分位数......对于某些样本大小。

在 R 邮件列表中了解更多信息(还没有)。

【讨论】:

    【解决方案3】:

    我参加聚会有点晚了,但你尝试过 stats 包中的 line() 吗?

    来自帮助文件:

    价值

    “tukeyline”类的对象。

    参考文献

    图基,J. W. (1977)。探索性数据分析,阅读马萨诸塞州:Addison-Wesley。

    【讨论】:

    • 我在 1 月 16 日的原始评论的修订版:在 9 个数据点上对line 进行了一些试验,这表明这可能不是通常给出的中位数线。事实上,如果你让 x 和 y 都为 1:9(所有点都位于斜率为 1 的直线上),那么直线的斜率为 1.2!虽然我最初认为该函数可能实现了除 Tukey 抗性线(中位数线)之外的其他线,但我现在怀疑它是有意成为那条线的,这只是 line 函数中的一个错误。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 2018-12-04
    • 2013-02-11
    • 1970-01-01
    • 2016-08-25
    • 2010-11-13
    相关资源
    最近更新 更多