如何计算h点 [英] how to calculate h-point
问题描述
我正在尝试编写一个计算h点的函数.该功能是在秩频率数据帧上定义的.考虑以下data.frame:
I am trying to write a function to calculate h-point. the function is defined over a rank frequency data frame. consider the following data.frame :
DATA <-data.frame(frequency=c(49,48,46,38,29,24,23,22,15,12,12,10,10,9,9), rank=c(seq(1, 15)))
,h点的公式是:
如果{存在r = f(r),h点= r }否则{h-point = f(i)j-f(j)i/j-i + f(i)-f(j)} 其中 f(i)和 f(j)是ith和j等级的相应频率, i 和 j 是 i< f(i)和 j> f(j)的相邻等级.
if {there is an r = f(r), h-point = r } else { h-point = f(i)j-f(j)i / j-i+f(i)-f(j) } where f(i) and f(j) are corresponding frequencies for ith and jth ranks and i and j are adjacent ranks that i<f(i) and j>f(j).
现在,我已经尝试了以下代码:
NOW, i have tried the following codes :
fr <-function(x){d <-DATA$frequency[x]
return(d)}
for (i in 1:length(DATA$rank)) {
j <- i+1
if (i==fr(i))
return(i)
else(i<fr(i) && j>fr(j)) {
s <-fr(i)*j-fr(j)*i/j-i+fr(i)-fr(j)
return(s)
}}
我也尝试过:
for (i in 1:length(DATA$rank)) {
j <- i+1
if (i==fr(i))
return(i)
if (i<fr(i) while(j>fr(j))) {
s <-fr(i)*j-fr(j)*i/j-i+fr(i)-fr(j)
return(s)
}}
,它们都不起作用.对于 DATA ,所需结果将是 i = 11 和 j = 12 ,因此:h点= 12×12-10×11/12-11 + 12-10
and neither of them works. for the DATA ,the desired result would be i=11 and j=12, so: h-point=12×12 - 10×11 / 12 - 11 + 12 - 10
您能告诉我我在做什么错吗?
can you please tell me what I`m doing wrong here?
推荐答案
我想我已经弄清楚了您要实现的目标.如果给定行的 rank == frequency
,我的循环将遍历DATA并在任何时候中断.如果更谨慎地使用 DATA $ rank [i] == fr(i)
进行测试,而不是依赖于i,以防排名受限等情况.
I think I have figured out what you are trying to achieve. My loop will go through DATA and break at any point if rank == frequency
for a given row. If might be more prudent to explicitly test this with DATA$rank[i] == fr(i)
rather than relying on i, in case tied ranks etc.
如果行i的等级低于频率,行j的等级更高,则第二条if语句将为行i和j计算h点( s
).
The second if statement calculates h-point (s
) for rows i and j if row i has rank that is lower than freq and row j has a rank that is higher.
这是您想要的吗?
DATA <-data.frame(frequency=c(49,48,46,38,29,24,23,22,15,12,12,10,10,9,9), rank=c(seq(1, 15)))
fr <-function(x){d <-DATA$frequency[x]
return(d)}
for(i in 1:nrow(DATA)){
j <- i+1
if (i==fr(i)){
s <- list(ij=c(i=i,j=j), h=i)
break
}else if(i <fr(i) && j>fr(j)){
s <-list(ij=c(i=i,j=j),h=fr(i)*j-fr(j)*i/j-i+fr(i)-fr(j))
}}
我不确定公式是否正确,在您的循环中您有j-i,但在解释中它是i-j.不确定整个 i-j + fr(i)-fr(j)
是否是分母,并且分子是否类似.简单修复.
I am not sure the formula is correct, in your loop you had j-i but in explanation it was i-j. Not sure if the entire i-j+fr(i)-fr(j)
is the denominator and similarly for the numerator. Simple fixes.
这篇关于如何计算h点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!