[英]R: Independent t-tests for multiple by groups
我有一個名為“ dat”的數據集,其中有5列:月; mean0; SD0; mean1; SD1。 看起來如下(但帶有數字):
月均值0 SD0均值1 SD1
1
2
3
..
48
我想使用一個獨立的(未配對的)t檢驗來比較1到48之間每個月的mean0和mean1。理想情況下,輸出將放在另一個名為'dat1'的數據框中,其列用於:t-statisitc ,自由度(DF); 和p值。 像這樣:
月t統計量DF p值
1
2
3
..
48
我嘗試使用dplyr和broom軟件包,但似乎無法弄清楚。 任何幫助,將不勝感激。
您還將需要兩個sd的n個值。 BSDA軟件包中的tsum.test
函數將幫助您進行t測試,而無需編寫自己的函數。
仍然存在更大的問題,即以這種方式進行大量比較的可取性。 此鏈接提供有關此信息。
有了這一警告,下面是如何對一些任意數據進行所需的操作:
dat <- data.frame(m1=c(24,11,34),
sd1=c(1.3,4.2,2.3),
n1=c(30, 31, 30),
m2=c(18,8,22),
sd2=c(1.8, 3.4, 1.8),
n2=c(30,31,30))
# user function to do t-test and return desired values
do.tsum <- function(x) {
# tsum.test is quirky, so you have to break out each column's value
results <- tsum.test(x[1],x[2],x[3],x[4],x[5],x[6],alternative='two.sided')
return(c(results$statistic, results$parameters, results$p.value))
}
# use apply to do the tsum.test on each row (1 for rows, 2 for cols)
# then, transpose the resulting matrix and use the data.frame function
t.results <- data.frame(t(apply, 1, do.tsum))s
# unfortunately the p-value is returned without no column name (it returns 'm1')
# use the names function to change the third column name.
names(t.results)[3] <- 'p.value'
輸出如下:
t df p.value
1 14.800910 52.78253 1.982944e-20
2 3.091083 57.50678 3.072783e-03
3 22.504396 54.83298 2.277676e-29
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.