dplyr t 检验 "mu = greater than",按组

dplyr t-test with "mu = greater than", by group

如何在 dplyr 中按组 运行 使用 mu-参数 = "greater than" 进行 t 检验? 这是我的数据框

structure(list(Lattobacilli = c(6.39794000867204, 4.91381385238372, 
7.7160033436348, 7.91907809237607, 6.6232492903979, 7.63346845557959, 
7.27875360095283, 7.43136376415899, 5.54406804435028, 6.36172783601759, 
4.55630250076729, 8.38021124171161, 7.94939000664491, 7.04139268515823
), Starter = structure(c(3L, 3L, 6L, 6L, 7L, 7L, 8L, 8L, 4L, 
4L, 4L, 5L, 5L, 5L), .Label = c("B", "C", "Ch1", "Ds1", "Ds2", 
"Sa1", "Sa2", "Sa3"), class = "factor"), Giorni = structure(c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("0", 
"1", "7", "35"), class = "factor")), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), .Names = c("Lattobacilli", 
"Starter", "Giorni"), vars = "Starter", drop = TRUE, indices = list(
    0:1, 8:10, 11:13, 2:3, 4:5, 6:7), group_sizes = c(2L, 3L, 
3L, 2L, 2L, 2L), biggest_group_size = 3L, labels = structure(list(
    Starter = structure(3:8, .Label = c("B", "C", "Ch1", "Ds1", 
    "Ds2", "Sa1", "Sa2", "Sa3"), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L), vars = "Starter", drop = TRUE, .Names = "Starter"))

我找到了这个解决方案

lattotest<-df%>% 
select(Lattobacilli,Starter,Giorni)%>% 
filter(Giorni=="35",!is.na(Lattobacilli))%>% 
group_by(Starter)%>% 
mutate(p_value=t.test(Lattobacilli, mu = 6,alternative="greater")$p.value, t_value=t.test(Lattobacilli, mu = 6,alternative="greater")$statistic)

> lattotest
# A tibble: 14 x 5
# Groups:   Starter [6]
   Lattobacilli Starter Giorni p_value t_value
          <dbl> <fct>   <fct>    <dbl>   <dbl>
 1         6.40 Ch1     35      0.638   -0.464
 2         4.91 Ch1     35      0.638   -0.464
 3         7.72 Sa1     35      0.0178  17.9  
 4         7.92 Sa1     35      0.0178  17.9  
 5         6.62 Sa2     35      0.134    2.23 
 6         7.63 Sa2     35      0.134    2.23 
 7         7.28 Sa3     35      0.0179  17.8  
 8         7.43 Sa3     35      0.0179  17.8  
 9         5.54 Ds1     35      0.785   -0.982
10         6.36 Ds1     35      0.785   -0.982
11         4.56 Ds1     35      0.785   -0.982
12         8.38 Ds2     35      0.0226   4.54 
13         7.95 Ds2     35      0.0226   4.54 
14         7.04 Ds2     35      0.0226   4.54

我想使用 stat_pvalue_manual 在 ggbarplot 中添加结果,但是,不可能只添加 p 值,但我还需要插入我没有的括号,因为我比较了固定值的均值,而不是组之间

一种选择是使用 ggplotgeom_text 来注释。 首先,您需要创建一个新的数据框,其中包含每个 "x"

的 p 值
library(dplyr)
library(ggplot2) 

 # I used your data frame "lattotest"

latto_p <- lattotest %>% group_by(Starter) %>% summarise(p = mean(p_value))

ggplot() +
  geom_bar(data = lattotest, aes(Starter, Lattobacilli), stat = 'identity') +
  geom_text(data = latto_p, aes(label = round(p,3), x = Starter, y = 1))

您可以通过向数据框添加特定的 y 值来尝试,并且可以通过在标签参数

中使用 paste 添加任何您想要的文本