我DescTools
用来计算基尼指数,一种衡量不平等的方法,效果很好。但我似乎无法弄清楚如何使用它来计算倒数:我应该输入什么值才能使基尼指数相等。
数据
为了重现性,这是我正在使用的数据-
# setup
set.seed(123)
library(DescTools)
library(dplyr)
# data
df <-
structure(list(share = c(
1.0927902450891e-05, 1.15255254587552e-05,
1.17490961074116e-05, 2.94139776697196e-05, 0.00011539470233412,
1.9005230595808e-05, 1.30015962776165e-05, 2.78830621259284e-05,
3.60539655756737e-06, 3.52621581472531e-06, 2.08516461722044e-06,
3.71562392174051e-06, 5.9923585443842e-06, 1.81981353418487e-06,
4.34979294985559e-06, 3.02671726234962e-06, 2.12453772387389e-06,
2.11908550914134e-06, 1.00308086256127e-06, 1.80107488148927e-06,
2.60305223492859e-06, 6.26982073798782e-07, 9.59182708805635e-07,
2.94622403616777e-06, 6.90271043800262e-07, 2.93824099499653e-07,
8.21549067353436e-07, 2.72552493097834e-07, 7.89679523466669e-07,
3.48883857629005e-07, 8.09840547160032e-07, 2.15137191096772e-07,
1.64298848805113e-06, 3.97217885926968e-08, 7.77111892663095e-07,
6.98248286041764e-07, 6.63616790078154e-07, 2.27849808697301e-07,
7.89749220781519e-07, 6.66388374298488e-07
), share_hr = c(
19488,
18316, 16035, 6052, 1025, 6318, 17448, 5086, 30818, 13213, 58788,
15319, 8972, 136088, 35123, 6874, 79538, 75868, 152369, 138806,
72289, 131665, 241332, 53906, 633809, 236347, 616133, 276469,
604729, 168079, 562280, 277543, 376314, 541400, 543215, 182714,
523227, 182869, 454487, 479647
), mode = structure(c(
1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L
), .Label = c(
"mode-1", "mode-2", "mode-3", "mode-4"
), class = "factor")), row.names = c(NA, -40L), class = c("tbl_df", "tbl", "data.frame"))
问题
我现在可以使用以下方法计算每种模式的基尼指数(作为衡量不平等的指标)DescTools
:
df %>%
dplyr::group_by(mode) %>%
dplyr::summarise(Gini = DescTools::Gini(x = share, n = share_hr)) %>%
dplyr::ungroup(.)
#> # A tibble: 4 x 2
#> mode Gini
#> <fct> <dbl>
#> 1 mode-1 0.229
#> 2 mode-2 0.208
#> 3 mode-3 0.264
#> 4 mode-4 0.261
但是我还想计算相反的值:列中
的值应该是什么,share
以便不观察到这种不等式(这意味着相同的基尼指数)。
请注意,我希望share_hr
保持不变。
#> # A tibble: 4 x 2
#> mode Gini
#> <fct> <dbl>
#> 1 mode-1 0.25
#> 2 mode-2 0.25
#> 3 mode-3 0.25
#> 4 mode-4 0.25
有没有办法使用DescTools
包或任何其他包来做到这一点?