R中ntile和cut之间的区别,然后分位数()函数 [英] Difference between ntile and cut and then quantile() function in R

查看:594
本文介绍了R中ntile和cut之间的区别,然后分位数()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该主题上发现了两个线程来计算R中的十分位数。但是,这两种方法,即 dplyr :: ntile quantile()产生不同的输出。实际上, dplyr :: ntile()无法输出适当的decil。

I found two threads on this topic for calculating deciles in R. However, both the methods i.e. dplyr::ntile and quantile() yield different output. In fact, dplyr::ntile() fails to output proper deciles.

方法1:使用ntile()
来自 R:将数据集拆分为四分位数/十分位数。什么是正确的方法?线程,我们可以使用 ntile()

Method 1: Using ntile() From R: splitting dataset into quartiles/deciles. What is the right method? thread, we could use ntile().

这是我的代码:

vector<-c(0.0242034679584454, 0.0240411606258083, 0.00519255930109344, 
  0.00948031338483081, 0.000549450549450549, 0.085972850678733, 
  0.00231687756193192, NA, 0.1131625967838, 0.00539244534707915, 
  0.0604885614579294, 0.0352030947775629, 0.00935626135385923, 
  0.401201201201201, 0.0208212839791787, NA, 0.0462887301644538, 
  0.0224952741020794, NA, NA, 0.000984952654008562)

ntile(vector,10)

输出为:

ntile(vector,10)
5  5  2  3  1  7  1 NA  8  2  7  6  3  8  4 NA  6  4 NA NA  1

如果我们对此进行分析,我们会发现不是第十位!

If we analyze this, we see that there is no 10th quantile!

方法2:使用分位数()
现在,让我们使用如何通过排序列快速形成组(四分位数,十分位数等)( s)在数据帧中线程。

这是我的代码:

as.numeric(cut(vector, breaks=quantile(vector, probs=seq(0,1, length  = 11), na.rm=TRUE),include.lowest=TRUE))

输出为:

 7  6  2  4  1  9  2 NA 10  3  9  7  4 10  5 NA  8  5 NA NA  1

我们可以看到,输出是完全不同。我在这里想念什么?

As we can see, the outputs are completely different. What am I missing here? I'd appreciate any thoughts.

这是 ntile()函数中的错误吗?

推荐答案

dplyr :: ntile NA 始终是最后一个(最高排名),这就是为什么在这种情况下看不到十分位数的原因。如果您不希望decil考虑使用 NA ,则可以定义一个函数,例如此处,我接下来使用:

In dplyr::ntile NA is always last (highest rank), and that is why you don't see the 10th decile in this case. If you want the deciles not to consider NAs, you can define a function like the one here which I use next:

ntile_na <- function(x,n)
{
  notna <- !is.na(x)
  out <- rep(NA_real_,length(x))
  out[notna] <- ntile(x[notna],n)
  return(out)
}

ntile_na(vector, 10)
# [1]  6  6  2  4  1  9  2 NA  9  3  8  7  3 10  5 NA  8  5 NA NA  1

也, quantile 有9种计算分位数的方式,您使用的是默认方式,即数字7(您可以检查?stats :: quantile 用于不同的类型,并在此处进行有关他们)。

Also, quantile has 9 ways of computing quantiles, you are using the default, which is the number 7 (you can check ?stats::quantile for the different types, and here for the discussion about them).

如果您尝试

as.numeric(cut(vector, 
               breaks = quantile(vector, 
                                 probs = seq(0, 1, length = 11), 
                                 na.rm = TRUE,
                                 type = 2),
               include.lowest = TRUE))
# [1]  6  6  2  4  1  9  2 NA  9  3  8  7  3 10  5 NA  8  5 NA NA  1

与使用 ntile 的结果相同。

总而言之:这不是错误,只是实现它们的方式不同。

In summary: it is not a bug, it is just the different ways they are implemented.

这篇关于R中ntile和cut之间的区别,然后分位数()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆