Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中列联表(4x2)的卡方检验_R_Chi Squared_Contingency - Fatal编程技术网

R中列联表(4x2)的卡方检验

R中列联表(4x2)的卡方检验,r,chi-squared,contingency,R,Chi Squared,Contingency,我在计算R中4x2列联表的卡方检验时遇到问题。我的脚本如下所示: # Read data read.table("Mortality_test.txt") # Assign a name to the data mortality<- read.table("Mortality_test.txt", ,col.names=c('treatment','dead'), header=TRUE, sep="\t", na.strings="NA", dec=",", strip.white=

我在计算R中4x2列联表的卡方检验时遇到问题。我的脚本如下所示:

# Read data
read.table("Mortality_test.txt")

# Assign a name to the data
mortality<- read.table("Mortality_test.txt", ,col.names=c('treatment','dead'), header=TRUE, sep="\t", na.strings="NA", dec=",", strip.white=TRUE)

table(mortality)
    dead
treatment no yes
     A    63   7
     B    61   9
     C    68   2
     D    63   7

我现在的问题是,我想比较不同治疗(A、B、C、D)之间的死亡率是否有统计学差异。如果我没弄错的话,我可以在桌上做一个卡方检验。然而,我不确定下一步该采取哪一步

您有一个函数
chisq.test
,用于在一个接触表上执行chi测试

来,拿着你的桌子

dead <- read.table(text = "treatment no yes
                   A    63   7
                   B    61   9
                   C    68   2
                   D    63   7",header = T)

> dead
  treatment no yes
1         A 40  15
2         B 61   9
3         C 68   2
4         D 63   7
这两种治疗方法之间没有区别。要查看另一个不同的示例,请执行以下操作:

dead <- read.table(text = "treatment no yes
                   A    55   12
                   B    61   9
                   C    68   2
                   D    63   7",header = T)

我们只需在
表()上应用
summary()
,就可以方便地得到卡方检验

实例 注:“卡方近似可能不正确”是因为本例中只有32个观测值


有了你的数据,
总结(表(死亡率))
应该是有效的。

这很简单。非常感谢您的快速回答:-)
dead <- read.table(text = "treatment no yes
                   A    55   12
                   B    61   9
                   C    68   2
                   D    63   7",header = T)
    Pearson's Chi-squared test

data:  dead[, 2:3]
X-squared = 8.4334, df = 3, p-value = 0.03785
with(mtcars, table(cyl, gear))
#    gear
# cyl  3  4  5
#   4  1  8  2
#   6  2  4  1

summary(with(mtcars, table(cyl, gear)))
# Number of cases in table: 32 
# Number of factors: 2 
# Test for independence of all factors:
#         Chisq = 18.036, df = 4, p-value = 0.001214
#         Chi-squared approximation may be incorrect