วิธีทำการทดสอบ Chi-Sqr บน tibble / dataframe ใน R

ฉันได้ปรึกษาสิ่งนี้แล้ว แต่ไม่สามารถทำงานได้

นี่คือ tibble ของฉัน:

Source: local data frame [10 x 4]
Groups: word [10]

        word Detractor Passive Promoter
*      <chr>     <int>   <int>    <int>
1  broadband       833     766      507
2       call       441     348      118
3    cheaper       641     949      182
4   customer      1563    1128      758
5   internet       297     277      195
6       line       389     392      182
7      price      1022    1212      549
8   reliable       230     316      743
9    service      1546    1231     2119
10     speed       262     228      194

นี่คือสิ่งที่ฉันได้ลอง:

csv%>%
      select(word,NPS_Level,total_word_count_by_cust)%>%
      spread(NPS_Level,total_word_count_by_cust)%>%
      rowwise()%>% 
  mutate(
    test_stat = chisq.test(c(word, Detractor))$statistic,
    p_val = chisq.test(c(word, Detractor))$p.value
    )

ได้รับข้อผิดพลาดต่อไปนี้:

Error in mutate_impl(.data, dots) : invalid 'type' (character) of argument

นี่คือเอาต์พุต dput():

structure(list(word = c("broadband", "call", "cheaper", "customer", 
"internet", "line", "price", "reliable", "service", "speed"), 
    Detractor = c(833L, 441L, 641L, 1563L, 297L, 389L, 1022L, 
    230L, 1546L, 262L), Passive = c(766L, 348L, 949L, 1128L, 
    277L, 392L, 1212L, 316L, 1231L, 228L), Promoter = c(507L, 
    118L, 182L, 758L, 195L, 182L, 549L, 743L, 2119L, 194L)), .Names = c("word", 
"Detractor", "Passive", "Promoter"), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, 10L), vars = list(
    word), drop = TRUE, indices = list(0L, 1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
    word = c("broadband", "call", "cheaper", "customer", "internet", 
    "line", "price", "reliable", "service", "speed")), class = "data.frame", row.names = c(NA, 
-10L), vars = list(word), drop = TRUE, .Names = "word"))

มีวิธีที่ฉันสามารถแสดงผลลัพธ์ของตัวแปรทั้งหมดเหมือนกับที่ Minitab ทำได้หรือไม่? เช่น.

    Chi-Square Test for Association: word, Worksheet columns 

Rows: word   Columns: Worksheet columns

             Detractor  Passive  Promoter    All

broadband          833      766       507   2106
                 775.5    735.0     595.5
                 4.263    1.305    13.145

call               441      348       118    907
                 334.0    316.6     256.5
                34.288    3.123    74.749

cheaper            641      949       182   1772
                 652.5    618.5     501.0
                 0.203  176.664   203.145

customer          1563     1128       758   3449
                1270.0   1203.8     975.2
                67.579    4.768    48.378

internet           297      277       195    769
                 283.2    268.4     217.4
                 0.675    0.276     2.315

line               389      392       182    963
                 354.6    336.1     272.3
                 3.335    9.296    29.939

price             1022     1212       549   2783
                1024.8    971.3     786.9
                 0.008   59.642    71.921

reliable           230      316       743   1289
                 474.7    449.9     364.5
               126.103   39.842   393.147

service           1546     1231      2119   4896
                1802.9   1708.8    1384.3
                36.598  133.590   389.870

speed              262      228       194    684
                 251.9    238.7     193.4
                 0.407    0.482     0.002

All               7224     6847      5547  19618

Cell Contents:      Count
                    Expected count
                    Contribution to Chi-square


Pearson Chi-Square = 1929.058, DF = 18, P-Value = 0.000
Likelihood Ratio Chi-Square = 1898.013, DF = 18, P-Value = 0.000

person Shery    schedule 28.06.2017    source แหล่งที่มา
comment
โปรดระบุตัวอย่างที่สามารถทำซ้ำได้   -  person Roman    schedule 28.06.2017
comment
@Jimbou เพิ่ม dput() เอาต์พุต   -  person Shery    schedule 28.06.2017
comment
rownames(dat) = dat$word ; chisq.test(dat[-1]) ให้จำนวนนับ / จำนวนที่คาดหวัง   -  person user20650    schedule 28.06.2017
comment
@ user20650 dat คืออะไร? เมื่อฉันทำ csv%>% select(word,NPS_Level,total_word_count_by_cust)%>% spread(NPS_Level,total_word_count_by_cust)%>% row.names(word) มันทำให้เกิดข้อผิดพลาด: Error in row.names(., word) : unused argument (word)   -  person Shery    schedule 28.06.2017
comment
dput ของคุณใช้งานไม่ได้สำหรับฉัน (อาจเป็นเพราะฉันไม่ได้ติดตั้งแพ็คเกจที่เป็นระเบียบเรียบร้อยเหล่านี้) แต่ถ้าข้อมูลของคุณเป็นดาต้าเฟรม (ลองแปลงข้อมูลของคุณด้วย dat <- as.data.frame(yourtibble)) ความคิดเห็นข้างต้นจะสร้างผลลัพธ์ chisq.test   -  person user20650    schedule 28.06.2017
comment
มันได้ผล! มันค่อนข้างเป็นวิธีที่ทำได้ยาก หวังว่าเราจะทำมันได้ในรูปแบบที่เป็นระเบียบเรียบร้อย   -  person Shery    schedule 28.06.2017
comment
ฟินนิกี้ ;) ยังไง?? คุณสามารถใช้ chisq.test บนดาต้าเฟรมของคุณโดยตรงซึ่งดูเหมาะที่สุด   -  person user20650    schedule 28.06.2017
comment
@ user20650 ฉันเพิ่งคุ้นเคยกับรูปแบบที่เป็นระเบียบเรียบร้อย: D คุณรู้วิธีแยกคะแนน Contribution to Chi-square หรือไม่ ฉันสามารถรับค่า statistics และ expected ได้ แต่ดูเหมือนจะไม่พบค่า Contribution to Chi-square ตามตัวแปรของคะแนนการทดสอบโดยรวม   -  person Shery    schedule 28.06.2017


คำตอบ (1)


ขอบคุณความคิดเห็นด้านล่าง ฉันทำดังนี้:

ในการทำการทดสอบไคสแควร์ เราจำเป็นต้องมีแถวและคอลัมน์ตามตัวเลข จากนั้นแปลงคอลัมน์แถวเป็นชื่อแถว (Tibble จะไม่อนุญาตให้ทำเช่นนี้ดังนั้นจะต้องแปลงเป็น dataframe ก่อนที่จะเรียกใช้เมธอด chi-sq)

m = csv%>%
      select(word,NPS_Level,total_word_count_by_cust)%>%
      spread(NPS_Level,total_word_count_by_cust)

rownames(m) = m$word

m <- m%>%
      as.data.frame()

Xsq <- chisq.test(m[-1]) #exclude the row column i.e. word as already converted to rownames
person Shery    schedule 28.06.2017