R - 对一系列数据点进行递归二分

3
# start with 3 points
# calculate their mid point, assumming normal distribution
start_init <- 10
mid_init <- 20
end_init <- 50
start <- start_init
end <- mid_init
mid_1 <- mean(c(start,end))

start <- mid_init
end <- end_init
mid_2 <- mean(c(start,end))

# now we have 3 points
c(start_init, mid_1, mid_init, mid_2, end_init)

# further calculate the mid points between
start <- start_init
end <- mid_1
mid_1_1 <- mean(c(start,end))

start <- mid_1
end <- mid_init
mid_1_2 <- mean(c(start,end))

start <- mid_init
end <- mid_2
mid_2_1 <- mean(c(start,end))

start <- mid_2
end <- end_init
mid_2_2 <- mean(c(start,end))

# now we have 9 data points
res <- c(start_init, mid_1_1 ,mid_1, mid_1_2, mid_init, mid_2_1, mid_2, mid_2_2, end_init)

我想压缩上面的代码,这样递归深度就可以被定义。

例如,当深度等于1时,我们需要为所有可用的连续点的组合生成1个新点。

如上例所示,我们从3个点(A,B,C)开始,因此当深度等于1时,我们最多会有2个额外的新点,即在A和B之间以及B和C之间。


只是想确认一下我提出的函数对你是否有帮助。有用吗? :) - Ric S
2个回答

2
使用自定义辅助函数 cross_vectors,您可以创建另一个自定义函数(我将其命名为 recursive_dichotomy),该函数使用包 zoo 计算两个连续点之间的平均值。
以下是代码:
# helper function
cross_vectors <- function(x, y){
    c(x, y)[order(c(ceiling(seq_along(x) / 1), seq_along(y)))]
}

recursive_dichotomy <- function(v, depth){
  # initialization
  require(zoo)
  current_depth <- 1

  # while cycle for the depth of the splitting
  while(current_depth <= depth){
    v <- cross_vectors(v, rollmean(v, k = 2))
    current_depth <- current_depth + 1
  }

  return(v)
}

输出

recursive_dichotomy(c(10, 20, 50), depth = 2)
# [1] 10.0 12.5 15.0 17.5 20.0 27.5 35.0 42.5 50.0

你的解决方案让我在计算平均值时拥有更多的灵活性,这使得操作更加灵活。再次感谢! - Afiq Johari
很高兴听到它有用! :) - Ric S

1

这里有另一种看起来(经过几次尝试和错误)有效的替代方案:

ff = function(x, depth)
{
    nbetween = 2 ^ depth
    means = (x[-1] - x[-length(x)]) / nbetween
    diffs = c(x[1], rep(means, each = nbetween))
    return(cumsum(diffs))
}

ff(c(10, 20, 50), 2)
#[1] 10.0 12.5 15.0 17.5 20.0 27.5 35.0 42.5 50.0
ff(c(10, 20, -5, 5), 1)
#[1] 10.0 15.0 20.0  7.5 -5.0  0.0  5.0
ff(c(10, 20, -5, 5), 2)
#[1] 10.00 12.50 15.00 17.50 20.00 13.75  7.50  1.25 -5.00 -2.50  0.00  2.50  5.00

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接