如何使用这些数据运行 chisq.test() ?

Ben*_*Ben 3 statistics r chi-squared

我有这些数据:

> dput(df)
structure(list(Freq = c(41L, 31L, 11L, 0L), group = structure(c(1L, 
1L, 2L, 2L), .Label = c("A", "B"), class = "factor"), Survived = structure(c(2L, 
1L, 2L, 1L), .Label = c("No", "Yes"), class = "factor")), row.names = c(NA, 
4L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
  Freq group Survived
1   41     A      Yes
2   31     A       No
3   11     B      Yes
4    0     B       No
Run Code Online (Sandbox Code Playgroud)

我尝试遵循https://data-flair.training/blogs/chi-square-test-in-r/但我不确定如何使用这些数据。例如,当我使用时chisq.test(df$group, df$Survived)我收到

> chisq.test(df$group, df$Survived)

    Pearson's Chi-squared test

data:  df$group and df$Survived
X-squared = 0, df = 1, p-value = 1
Run Code Online (Sandbox Code Playgroud)

这是没有意义的(并且没有考虑到Freq,对吧?)?A我想知道和之间是否有区别B

Dar*_*sai 5

首先,您需要将数据框转换为列联表:

tab <- xtabs(Freq ~ ., df) # Specifically, xtabs(Freq ~ group + Survived, df)

#      Survived
# group No Yes
#     A 31  41
#     B  0  11
Run Code Online (Sandbox Code Playgroud)

然后将其传递到chisq.test()

chisq.test(tab)

#   Pearson's Chi-squared test with Yates' continuity correction
# 
# data:  tab
# X-squared = 5.8315, df = 1, p-value = 0.01574
Run Code Online (Sandbox Code Playgroud)