rey*_*n64 7 testing algorithm r function linear-regression
我正在搜索John Tukey算法,该算法使用R进行线性回归计算"抗性线"或"中位数线".
列表中的学生用以下术语解释此算法:
"它的计算方法是将数据分成三组,找到每组的x中位数和y中位数值(称为汇总点),然后使用这三个汇总点来确定该线.外部两个汇总点确定斜率,所有点的平均值决定了截距."
关于约翰·图基的好奇心中位数的文章:http://www.johndcook.com/blog/2009/06/23/tukey-median-ninther/
你知道我在哪里可以找到这个算法或R函数吗?在哪个包中,非常感谢!
Ric*_*ton 11
还有如何计算中位数中线的描述在这里.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))
}
Run Code Online (Sandbox Code Playgroud)
要测试它,这是该页面的第二个示例:
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
Run Code Online (Sandbox Code Playgroud)
请注意指定组的奇怪方法.关于如何定义组大小的说明非常挑剔,因此更明显的方法cut(x, quantile(x, seq.int(0, 1, 1/3)))不起作用.