如何计算h点(在R中)

how to calculate h-point (in R)

我正在尝试编写一个函数来计算 h 点。该函数是在排名频率数据帧上定义的。考虑以下 data.frame :

DATA <-data.frame(frequency=c(64,58,54,32,29,29,25,17,17,15,12,12,10), rank=c(seq(1, 13)))

h点的公式为:

if {有一个 r = f(r), h-point = r } else { h-point = f(i)j-f(j)i / j-i+f(i)-f( j) } 其中 f(i) 和 f(j) 是第 i 个和第 j 个等级的相应频率,i 和 j 是 if(j) 的相邻等级。

这是我到目前为止所做的:

h_point <- function(data){
  x <- seq(nrow(data))
  f_x <- data[["frequency"]][x]
  h <- which(x == f_x)
  if(length(h)>1) h
  else{
    i <- which(x < f_x)
    j <- which(x > f_x)
    s <- which(outer(i,j,"-") == -1, TRUE)
    i <- i[s[,1]]
    j <- j[s[,2]]
    cat("i: ",i, "j: ", j,"\n")
    f_x[i]*j - f_x[j]*i / (i-j + f_x[i]-f_x[j])
  }
}

DATA 中,h 点是 12——因为 x = f_x。然而,

h_point(DATA)
i:   j:   
numeric(0)

我做错了什么?

我看过你之前的 post how to calculate h-point 但必须说我不太按照你的方法计算 h 点。

根据我找到的h点的定义

参考:https://www.researchgate.net/figure/The-definition-of-the-h-point-cf-Popescu-Altmann-2006-25_fig1_281010850

我认为更简单的方法是使用approxfun创建函数频率(等级),然后使用uniroot找到h点:

get_h_point <- function(DATA) {
    fn_interp <- approxfun(DATA$rank, DATA$frequency)
    fn_root <- function(x) fn_interp(x) - x
    uniroot(fn_root, range(DATA$rank))$root
}

get_h_point(DATA)
#[1] 12