按年和小组t.test

时间:2014-04-19 16:20:50

标签: r

我想进行t检验。对不起,我用英语和R很尴尬。希望你理解我的话。

我的数据如下 每年,数据数量为100。

year     group   x
2000     1      0.5
2000     0       2.3
2001    1       0.4
2001     0       0.3
2002     1       0.5
2002     0       2.3
2013     0       1.4

我希望按年份和小组运行t.test。

t.test(x~group) #######by year

1 个答案:

答案 0 :(得分:1)

我觉得有一种更简单的方法可以做到这一点,但这是一种方式......

# sample dataset - you already have this
set.seed(1)
df <- data.frame(year=rep(2000:2010,each=100),
                 group=rep(0:1,each=50),
                 x = rnorm(1100,1,.2))

# you start here
result <- sapply(unique(df$year),
                 function(yr)c(year=yr,t.test(x~group,df[df$year==yr,])))
result <- data.frame(t(result))
result[1:3,]
#   year   statistic parameter   p.value                conf.int             estimate null.value alternative                  method  data.name
# 1 2000 -0.09348405   95.7926 0.9257143 -0.07505382, 0.06830255   1.020090, 1.023465          0   two.sided Welch Two Sample t-test x by group
# 2 2001   -1.199856   96.7473 0.2331252 -0.12175014, 0.03000825 0.9695029, 1.0153739          0   two.sided Welch Two Sample t-test x by group
# 3 2002  -0.5876951  96.76143  0.558106 -0.10677870, 0.05799052 0.9937377, 1.0181318          0   two.sided Welch Two Sample t-test x by group

例如,如果您只想要p值,请执行以下操作:

result <- sapply(unique(df$year),
                 function(yr)c(year=yr,p.value=t.test(x~group,df[df$year==yr,])$p.value))
result <- data.frame(t(result))
head(result)
#   year   p.value
# 1 2000 0.9257143
# 2 2001 0.2331252
# 3 2002 0.5581060
# 4 2003 0.5621920
# 5 2004 0.4849300
# 6 2005 0.8650815