在数据帧上执行chisq.test以进行多个成对比较

时间:2017-09-21 10:47:41

标签: r dataframe chi-squared

我有以下数据框:

species <- c("a","a","a","b","b","b","c","c","c","d","d","d","e","e","e","f","f","f","g","h","h","h","i","i","i")
category <- c("h","l","m","h","l","m","h","l","m","h","l","m","h","l","m","h","l","m","l","h","l","m","h","l","m")
minus <- c(31,14,260,100,70,200,91,152,842,16,25,75,60,97,300,125,80,701,104,70,7,124,24,47,251)
plus <- c(2,0,5,0,1,1,4,4,30,1,0,0,2,0,5,0,0,3,0,0,0,0,0,0,4)
df <- cbind(species, category, minus, plus)
df<-as.data.frame(df)

我想为每个类别物种组合做一个chisq.test,如下所示:

物种a,类别h和l:p值

物种a,类别h和m:p值

物种a,类别l和m:p值

物种b,......等等

使用以下chisq.test(虚拟代码):

chisq.test(c(minus(cat1, cat2),plus(cat1, cat2)))$p.value

我想最终得到一个表格,其中显示每个比较的每个chisq.test p值,如下所示:

Species   Category1  Category2   p-value
a         h          l           0.05
a         h          m           0.2
a         l          m           0.1
b...

类别和类别2是chisq.test中的比较类别。

使用dplyr可以吗?我已经尝试调整herehere中提到的内容,但我们看到的并没有真正适用于这个问题。

编辑:我还想看看如何为以下数据集做到这一点:

species <- c(1:11)
minus <- c(132,78,254,12,45,76,89,90,100,42,120)
plus <- c(1,2,0,0,0,3,2,5,6,4,0)

我想做一个chisq。对表中每个物种的测试与表中的每个其他物种进行比较(所有物种的每个物种之间的成对比较)。我想最终得到这样的东西:

species1  species2  p-value
1         2         0.5
1         3         0.7
1         4         0.2
...
11        10        0.02

我尝试将上面的代码更改为以下内容:

species_chisq %>%
do(data_frame(species1 = first(.$species),
            species2 = last(.$species),
            data = list(matrix(c(.$minus, .$plus), ncol = 2)))) %>%
mutate(chi_test = map(data, chisq.test, correct = FALSE)) %>%
mutate(p.value = map_dbl(chi_test, "p.value")) %>%
ungroup() %>%
select(species1, species2, p.value) %>%

然而,这只创建了一个表格,其中每个物种只与自身进行比较,而不是其他物种。我不太明白在@ycw给出的原始代码中它指定哪些是比较的。

编辑2:

我设法通过找到的代码here执行此操作。

2 个答案:

答案 0 :(得分:2)

来自<base_folder_name>dplyr的解决方案。请注意,我不熟悉卡方检验,但我遵循您在@Vincent Bonhomme的帖子中指定的方式:purrr

此外,要创建示例数据框,不需要使用chisq.test(test, correct = FALSE),只需cbind即可。 data.frame对于防止列成为因素非常重要。

stringsAsFactors = FALSE

答案 1 :(得分:1)

首先,您应该使用data.frame创建data.frame,否则将minusplus列转换为factor s。

species <- c("a","a","a","b","b","b","c","c","c","d","d","d","e","e","e","f","f","f","g","h","h","h","i","i","i")
category <- c("h","l","m","h","l","m","h","l","m","h","l","m","h","l","m","h","l","m","l","h","l","m","h","l","m")
minus <- c(31,14,260,100,70,200,91,152,842,16,25,75,60,97,300,125,80,701,104,70,7,124,24,47,251)
plus <- c(2,0,5,0,1,1,4,4,30,1,0,0,2,0,5,0,0,3,0,0,0,0,0,0,4)
df <- data.frame(species=species, category=category, minus=minus, plus=plus)

然后,我不确定是否有一种纯粹的dplyr方法可以做到这一点(很高兴能够反过来表示),但我认为这是部分 - dplyr方式它:

df_combinations <-
  # create a df with all interactions
  expand.grid(df$species, df$category, df$category)) %>% 
  # rename columns
  `colnames<-`(c("species", "category1", "category2")) %>% 
  # 3 lines below:
  # manage to only retain within a species, category(1 and 2) columns
  # with different values
  unique %>% 
  group_by(species) %>% 
  filter(category1 != category2) %>% 
  # cosmetics
  arrange(species, category1, category2) %>%
  ungroup() %>% 
  # prepare an empty column
  mutate(p.value=NA)

# now we loop to fill your result data.frame
for (i in 1:nrow(df_combinations)){
  # filter appropriate lines
  cat1 <- filter(df,
                 species==df_combinations$species[i],
                 category==df_combinations$category1[i])
  cat2 <- filter(df,
                 species==df_combinations$species[i],
                 category==df_combinations$category2[i])
  # calculate the chisq.test and assign its p-value to the right line
  df_combinations$p.value[i] <- chisq.test(c(cat1$minus, cat2$minus,
                                             cat1$plus, cat2$plus))$p.value  

}

让我们看看结果data.frame

head(df_combinations)
# A tibble: 6 x 4
# A tibble: 6 x 4
# Groups:   species [1]
species category1 category2       p.value
<fctr>    <fctr>    <fctr>         <dbl>
1       a         h         l  3.290167e-11
2       a         h         m 1.225872e-134
3       a         l         h  3.290167e-11
4       a         l         m 5.824842e-150
5       a         m         h 1.225872e-134
6       a         m         l 5.824842e-150

检查第一行:     chisq.test(c(31,14,2,0))$ p.value     [1] 3.290167e-11

这是你想要的吗?