我想使用Gini()从DescTools中计算基尼系数(因为它提供了一种用权重、置信区间等来计算“无偏”基尼系数的简便方法),但是当我使用这个函数时,当我使用“大”样本时,会出现一些错误。下面是一个简单的示例,它会在我这边产生错误:
library("DescTools")
x1 <- sample(c(1:100000), 50) #Here I create a sample of 50 cases varying from 1 to 100,000
Gini(x1) #Here I use the Gini function without any parameters, and it returns the Gini coefficient as expected:
[1] 0.3153713
x2 <- sample(c(1:100000), 500) #Now, I create a sample of 500 cases varying from 1 to 100,000
Gini(x2) #And if I compute the Gini coefficient with the same parameters, I get the following error:
[1] NA 警告消息: 1: In sum(x * 1:n):整数溢出-使用sum(as.numeric(.)) 2: In n* sum(x):NAs
我不知道是什么问题,知道吗?我使用的是R版本3.3.1 (2016-06-21) --“头发上的虫子”,RStudio版本为0.99.903,“DescTools”版本为0.99.17。
编辑:哦,好吧,把我的数字从整数转换成数字似乎就能完成任务(但我还是不明白.):
x2 <- as.numeric(x2) #Now, Gini() will work... 发布于 2016-11-06 15:23:27
基于对这篇文章的一些反思,我将函数DescTools::Gini()更改为默认情况下将整数转换为数字(如DescTools 0.99.18)。手术很便宜,性能的损失不值得.
set.seed(1984)
x <- sample(c(1:100000), 500)
Gini(x)
# [1] 0.3360882https://stackoverflow.com/questions/39579029
复制相似问题