2014-01-15 26 views
4

我有一個數據幀有幾個因素和兩種表型[R t檢驗給出錯誤「聚合因子必須只有2級」

freq sampleID status score snpsincluded 
0.5 0001 case 100 all 
0.2 0001 case 30 all 
0.5 0002 control 110 all 
0.5 0003 case 100 del 
etc 

我願做一個比較t.test每個病例和對照一套相關因素。我曾嘗試以下:

o2 <- ddply(df, c("freq","snpsincluded"), summarise, pval=t.test(score, status)$p.value) 

但抱怨說,「聚合因子必須只有2級」

我沒有缺失值,NAS和伊夫檢查:

levels(df$status) 
[1] "case" "control" 

上午我錯過了一些愚蠢的東西? 謝謝!

回答

4

,因爲你得到至少一個子組,獨特的地位值對所有得分的你會得到一個錯誤。

這會重現錯誤,所有分數的狀態都是唯一的(等於1)。

dx = read.table(text=' score status 
1 1 1 
2 2 1 
3 3 1 ') 

t.test(score ~ status, data = dx) 
Error in t.test.formula(score ~ status, data = dx) : 
    grouping factor must have exactly 2 levels 

這個正確的問題,但與t.test創建另一個已知的問題,你應該有足夠的意見(我想> = 2):

dx = read.table(text=' score status 
1 1 1 
2 2 1 
3 3 2 ') 

t.test(score ~ status, data = dx) 
Error in t.test.default(x = 1:2, y = 3L) : not enough 'y' observations 

最後這個正確的所有問題:

dx = read.table(text=' score status 
1 1 1 
2 2 1 
3 3 2 
4 4 2') 

t.test(score ~ status, data = dx) 

Welch Two Sample t-test 

data: score by status 
t = -2.8284, df = 2, p-value = 0.1056 
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval: 
-5.042435 1.042435 
sample estimates: 
mean in group 1 mean in group 2 
      1.5    3.5 

編輯我沒有給出解決方案就解釋了問題,因爲您沒有提供可重複的示例。

一個解決方案是唯一的好做組計算:

ddply(df, c("freq","snpsincluded"), function(x) 
     { 
     if(length(unique(x$status)==2) 
     pval=t.test(score~status,data=x)$p.value 
    }) 
+0

感謝您的快速答覆!我不確定自己理解「你對所有分數都有獨特的地位價值」,但我對這兩種情況和控制都有大約700行。另外,我還要補充一點,爲了清楚起見,我省略了一些其他因素,但這些是我不喜歡對每個組合的數據子集進行t檢驗的原因。如果我對整個數據集進行ttest(不考慮因素),它就可以工作。再次感謝您的幫助。 – madieke

+0

@madieke我添加一些評論來澄清這一點。最好看我的例子。 – agstudy

相關問題