2016-12-14 43 views
0

首先,我很抱歉,如果它似乎基本問題。我堆疊用一個簡單的chisq.test,我認爲它鏈接到我的DATAS類(data.frame),即使我認爲,這是確定與形狀:dataframe vs應急表&chisq.test()

cat3=structure(list(`1` = c(1, 3, 0, 0, 0, 2, 0, 3, 0, 0, 1.5, 4, 
0, 0, 0, 0, 0, 0, 9, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 
5, 0, 0, 0, 0, 0, 0.5), `2` = c(1, 0, 0, 0, 0, 2, 0, 2, 0.5, 
0, 2.5, 6, 0, 0, 0, 0, 0, 0, 11.5, 1, 2, 1.5, 0, 0, 1, 0, 0, 
0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 1.5), `3` = c(0, 0, 0, 0, 
2.5, 2, 0, 0, 0, 0, 0, 5.5, 2, 0, 1, 0, 0, 0, 3, 1, 0, 1, 0, 
1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 0, 0, 0, 0, 0, 3.5), `4` = c(0, 
1, 0.5, 0, 0, 1, 0, 1, 0, 0, 0, 3, 1, 0, 3, 0, 0, 0, 1, 7, 0, 
2, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0.5, 0, 0, 3.5, 4), 
    `5` = c(0, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1.5, 0, 2, 0, 
    0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 2.5, 4), `6` = c(0, 0, 0, 0, 0, 2.5, 0, 1, 0, 1, 
    0, 1.5, 0, 0, 3.5, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 
    1, 0, 0, 0, 0.5, 0, 2.33333333333333, 0, 0, 0, 2, 1), `7` = c(1, 
    1, 1, 0, 0, 3.5, 0, 0, 0, 0, 0, 1.5, 0, 0, 1, 0, 0, 0, 0, 
    4, 0, 0, 0, 0, 0, 1, 0.5, 0, 0.5, 0, 1, 0, 0, 0, 3, 1, 2, 
    0, 1.5, 0), `8` = c(1, 0, 0, 0, 0, 1.5, 0, 0, 0, 2, 0, 1, 
    0, 0, 0.5, 2, 1, 0, 0, 0.5, 0, 0, 0, 0.333333333333333, 0.5, 
    0, 0, 0, 1.5, 0, 0, 0, 0, 0, 1.5, 1.5, 0, 0, 1, 0), `9` = c(2, 
    0, 0, 0, 0.5, 0.5, 0, 3.5, 0, 0, 0, 0.5, 0, 0, 0, 0, 1, 0, 
    0, 3, 0, 0, 0, 0, 0, 0.5, 1, 1, 2, 0, 0, 0, 0, 3, 3, 3, 0, 
    0, 0, 0.5), `10` = c(2, 0, 0, 0, 0, 0, 0, 2.5, 0, 0, 0, 0, 
    0, 0, 0, 1.5, 1.5, 1, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0.333333333333333, 
    7, 0, 0, 0, 0, 0, 2, 3.5, 0, 0, 0, 0), `11` = c(1, 0, 0, 
    0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0.333333333333333, 
    4.33333333333333, 0, 0, 0, 0), `12` = c(1, 0, 0, 0.5, 0, 
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 
    0, 7, 0, 0, 1, 0, 0, 0, 0, 0, 0, 4.5, 0, 0, 0, 0)), .Names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "data.frame", row.names = c(NA, 
-40L)) 

我試圖運行chisq.test(cat3)功能,但我沒有p值,也沒有X²。

 Pearson's Chi-squared test 

data: cat3 
X-squared = NaN, df = 429, p-value = NA 

我發現沒有辦法將數據幀轉換爲應急表以便使用chisq.test函數。這是因爲我的班級表嗎?否則有什麼問題,爲什麼我對p和X²沒有價值?

非常感謝。

回答

1

的問題是,你有你的數據全部爲0(空)行:

> any(rowSums(cat3) == 0) 
[1] TRUE 

您可以刪除空行:

> cat3_sub <- cat3[rowSums(cat3) != 0, ] 
> chisq.test(cat3_sub) 

Pearson's Chi-squared test 

data: cat3_sub 
X-squared = 731.21, df = 407, p-value < 2.2e-16 

Warning message: 
In chisq.test(cat3_sub) : Chi-squared approximation may be incorrect 
+0

如果我沒理解好了,這是不可能運行如果至少有一行全是0,chisq.test?不知道這件事。非常感謝 ! – ranell

+1

空行給出0分,這就是爲什麼你得到NaN結果。 – Istrel