【问题标题】:2-group heterogeneity index2组异质性指数
【发布时间】:2021-07-15 22:52:49
【问题描述】:

我有一个数据集,其中包含两个不同的组(A 和 B),属于 3 个不同的类别(1、2、3):

library(tidyverse)
set.seed(100)
df <- tibble(Group = sample(c(1, 2, 3), 20, replace = T),Company = sample(c('A', 'B'), 20, replace = T))

我想提出一个衡量跨时间跨度组组成特征的指标。

到目前为止,我使用了一个基于香农指数的指数,它给出了在 0 和 1 之间变化的异质性度量。1 是完全异质的(每个组的相等表示),0 是完全同质的(只有 1 个组是代表):

df %>% 
   group_by(Group, Company) %>% 
   summarise(n=n()) %>% 
   mutate(p = n / sum(n)) %>% 
   mutate(Shannon = -(p*log2(p) + (1-p)))

产量:

   Group    Company n          p    Shannon
   <dbl>    <chr>   <int>   <dbl>   <dbl>
    1         A     2   0.6666667   0.05664167
    1         B     1   0.3333333   -0.13834583
    2         A     4   0.5000000   0.00000000
    2         B     4   0.5000000   0.00000000
    3         A     1   0.1111111   -0.53667500
    3         B     8   0.8888889   0.03993333

但是,我正在寻找 [-1, +1] 之间的索引。其中,当某个时间点仅存在组 A 时,索引产生 -1,当某个时间点仅存在组 B 时,索引产生 +1,0 表示相等。

如何创建这样的索引?我曾将 Moran 的 I 等措施视为灵感,但它们似乎不适合需要。

【问题讨论】:

    标签: r dplyr statistics


    【解决方案1】:

    一个简单的解决方案可能是计算平均值。
    我将 Company 转换为 value,其中 A = -1 和 B = 1,并按 Group 计算平均值。
    结果将是每个组的索引,当Company 只有“A”时为 -1,当只有“B”时为 1。

    数据

    df <- structure(list(Group = c(2, 2, 3, 3, 1, 2, 3, 1, 1, 3, 3, 1, 
                                   2, 2, 3, 2, 2, 1, 1, 3), Company = c("A", "A", "A", "A", "B", 
                                                                        "B", "B", "B", "A", "B", "B", "B", "A", "A", "B", "A", "B", "B", 
                                                                        "A", "B")), row.names = c(NA, -20L), class = c("tbl_df", "tbl", 
                                                                                                                       "data.frame"))
    

    代码

    df %>% 
      mutate(value = ifelse(Company == "A", -1, 1)) %>%
      group_by(Group) %>%
      summarise(index = mean(value))
    

    输出

    # A tibble: 3 x 2
      Group  index
      <dbl>  <dbl>
    1     1  0.333
    2     2 -0.429
    3     3  0.429
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多