我試圖對我的數據框的所有列(每次兩個)進行t檢驗,並只提取p值。以下是我想出了:R:對所有列進行t檢驗
for (i in c(5:525)) {
t_test_p.value =sapply(Data[5:525], function(x) t.test(Data[,i],x, na.rm=TRUE)$p.value)
}
我的問題是:1。 是有辦法做到這一點沒有一個循環? 2.如何捕獲t檢驗的結果。
我試圖對我的數據框的所有列(每次兩個)進行t檢驗,並只提取p值。以下是我想出了:R:對所有列進行t檢驗
for (i in c(5:525)) {
t_test_p.value =sapply(Data[5:525], function(x) t.test(Data[,i],x, na.rm=TRUE)$p.value)
}
我的問題是:1。 是有辦法做到這一點沒有一個循環? 2.如何捕獲t檢驗的結果。
試試這個
X <- rnorm(n=50, mean = 10, sd = 5)
Y <- rnorm(n=50, mean = 15, sd = 6)
Z <- rnorm(n=50, mean = 20, sd = 5)
Data <- data.frame(X, Y, Z)
library(plyr)
combos <- combn(ncol(Data),2)
adply(combos, 2, function(x) {
test <- t.test(Data[, x[1]], Data[, x[2]])
out <- data.frame("var1" = colnames(Data)[x[1]]
, "var2" = colnames(Data[x[2]])
, "t.value" = sprintf("%.3f", test$statistic)
, "df"= test$parameter
, "p.value" = sprintf("%.3f", test$p.value)
)
return(out)
})
X1 var1 var2 t.value df p.value
1 1 X Y -5.598 92.74744 0.000
2 2 X Z -9.361 90.12561 0.000
3 3 Y Z -3.601 97.62511 0.000
假設你的數據幀看起來是這樣的:
df = data.frame(a=runif(100),
b=runif(100),
c=runif(100),
d=runif(100),
e=runif(100),
f=runif(100))
了以下
tests = lapply(seq(1,length(df),by=2),function(x){t.test(df[,x],df[,x+1])})
會給你每一組列的測試。請注意,這隻會給你一個t.test一個& b,c & d和e & f。 如果你想要一個& B,B & C,C & d,d & E,和E &男,那麼你就必須做:
tests = lapply(seq(1,(length(df)-1)),function(x){t.test(df[,x],df[,x+1])})
最後如果讓我們說你只是想從P值你的測試,那麼你可以這樣做:
pvals = sapply(tests, function(x){x$p.value})
如果你不知道如何與對象的工作,嘗試輸入彙總(測試),和STR(測試[[1]]) - 在這種情況下測試一個htest對象的列表,並且你想知道htest對象的結構,不一定是l IST。
希望這有助於!
這是另一種解決方案,與outer
。
outer(
1:ncol(Data), 1:ncol(Data),
Vectorize(
function (i,j) t.test(Data[,i], Data[,j])$p.value
)
)
我會推薦給你的數據幀轉換爲長格式和使用pairwise.t.test
適當p.adjust
:
> library(reshape2)
>
> df <- data.frame(a=runif(100),
+ b=runif(100),
+ c=runif(100)+0.5,
+ d=runif(100)+0.5,
+ e=runif(100)+1,
+ f=runif(100)+1)
>
> d <- melt(df)
Using as id variables
>
> pairwise.t.test(d$value, d$variable, p.adjust = "none")
Pairwise comparisons using t tests with pooled SD
data: d$value and d$variable
a b c d e
b 0.86 - - - -
c <2e-16 <2e-16 - - -
d <2e-16 <2e-16 0.73 - -
e <2e-16 <2e-16 <2e-16 <2e-16 -
f <2e-16 <2e-16 <2e-16 <2e-16 0.63
P value adjustment method: none
> pairwise.t.test(d$value, d$variable, p.adjust = "bon")
Pairwise comparisons using t tests with pooled SD
data: d$value and d$variable
a b c d e
b 1 - - - -
c <2e-16 <2e-16 - - -
d <2e-16 <2e-16 1 - -
e <2e-16 <2e-16 <2e-16 <2e-16 -
f <2e-16 <2e-16 <2e-16 <2e-16 1
P value adjustment method: bonferroni
我運行此:
tres<-apply(x,1,t.test)
pval<-vapply(tres, "[[", 0, i = "p.value")
我花了一段時間來占卜「vapply」技巧將pvals從t.test結果對象列表中提取出來。 (由於下面Henrik的評論我編輯了'sapply')
如果它是一個配對t檢驗,你可以減去並測試means = 0,它給出完全相同的結果(這是一個配對t。測試是):
tres<-apply(y-x,1,t.test)
pval<-vapply(tres, "[[", 0, i = "p.value")
再次,這是對所有列進行的每行t檢驗。
不要使用'sapply',使用'vapply'。你不需要'unlist',並且如果數據不符合預期,它將會出錯。此外,你可以使用''[[「'以及。所以我會這樣做:'vapply(tres,「[[」,0,i =「p.value」)'('0'只是表示應該返回一個數字) – Henrik 2012-09-21 20:36:12
MYassen208的回答比較好。一般來說,應儘可能使用plyr包裝。死的方便! – 2012-03-12 04:02:50
也只是意識到,如果你想要所有的成對組合,那麼MYaseen208的答案還會告訴你如何使用combn() – 2012-03-12 04:05:53
謝謝,這就像魅力。我確實有一個後續問題:http:// stackoverflow。com/q/9669411/612191 – ery 2012-03-12 14:58:15