3

我有一个数据框,其中包含与组相关的个人的值。我想确定那些平均值大于平均值加上整个数据集的一个标准差的组。为此,我正在计算整个数据帧的平均值和标准差,然后运行成对 t 检验以与每个组的平均值进行比较。我在输出结果时遇到了麻烦。

> head(df)

  individual  group  value
1 11559638    75     0.371
2 11559641    75     0.367
3 11559648    75     0.410
4 11559650    75     0.417
5 11559652    75     0.440
6 11559654    75     0.395

> allvalues <- data.frame(mean=rep(mean(df$value), length(df$individual)), sd=rep(sd(df$value),  length(df$individual)))

> valueplus <- with(df, by(df, df$individual, function(x) t.test(allvalues$mean + allvalues$sd, df$value, data=x)))

> tmpplus

-------------------------------------------------------------------------- 
df$individuals: 10
NULL
-------------------------------------------------------------------------- 
df$individuals: 20
NULL
-------------------------------------------------------------------------- 
df$individuals: 21

    Welch Two Sample t-test

data:  allvalues$mean + allvalues$sd and df$value 
t = 84.5217, df = 4999, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 0.04676957 0.04899068 
sample estimates:
mean of x mean of y 
0.4719964 0.4241162 

如何将结果放入数据框中?我希望输出看起来像这样:

      groups  t        df     p-value  mean.x    mean.y
    1 10      NULL     NULL   NULL     NULL      NULL
    2 20      NULL     NULL   NULL     NULL      NULL 
    3 21      84.5217  4999   2.2e-16  0.4719964 0.4241162
4

1 回答 1

8

从纯粹的编程角度来看,您是在询问如何将输出t.test转换为data.frame. 尝试以下,使用mtcars

library(broom)
tidy(t.test(mtcars$mpg))
  estimate statistic      p.value parameter conf.low conf.high
1 20.09062  18.85693 1.526151e-18        31 17.91768  22.26357

或者对于多个组:

library(dplyr)
mtcars %>% group_by(vs) %>% do(tidy(t.test(.$mpg)))
# A tibble: 2 x 9
# Groups:   vs [2]
     vs estimate statistic  p.value parameter conf.low conf.high method            alternative
  <dbl>    <dbl>     <dbl>    <dbl>     <dbl>    <dbl>     <dbl> <chr>             <chr>      
1     0     16.6      18.3 1.32e-12        17     14.7      18.5 One Sample t-test two.sided  
2     1     24.6      17.1 2.75e-10        13     21.5      27.7 One Sample t-test two.sided  

不用说,您需要调整代码以适合您的特定设置。

于 2016-05-31T19:53:10.117 回答