R 中意外事件 table (4x2) 的卡方检验
Chi-square test on contingency table (4x2) in R
我在 R 中计算 4x2 意外事件 table 的卡方检验时遇到问题。我的脚本如下所示:
# 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)
当我 运行 table(mortality)
我得到一个意外事件 table 如下所示:
dead
treatment no yes
A 63 7
B 61 9
C 68 2
D 63 7
我现在的问题是,我想比较不同治疗(A、B、C、D)之间的死亡人数是否存在统计差异。如果我没记错的话,我可以通过 table 上的卡方检验来做到这一点。但是,我不确定下一步要采取哪一步。
您有一个函数 chisq.test
可以对偶发事件 table 进行卡方测试。
在这里,和你的table
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
你需要取 no 和 yes 这两列:
chisq.test(dead[,2:3])
Pearson's Chi-squared test
data: dead[, 2:3]
X-squared = 4.6996, df = 3, p-value = 0.1952
这在治疗之间没有区别。要查看另一个不同的示例:
dead <- read.table(text = "treatment no yes
A 55 12
B 61 9
C 68 2
D 63 7",header = T)
A处理真正不同的地方:
Pearson's Chi-squared test
data: dead[, 2:3]
X-squared = 8.4334, df = 3, p-value = 0.03785
我们可以通过简单地在 table()
.
上应用 summary()
来方便地得到卡方检验
例子
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
注意:“卡方近似值可能不正确”是因为本例中只有 32 个观测值。
根据您的数据 summary(table(mortality))
应该可以。
我在 R 中计算 4x2 意外事件 table 的卡方检验时遇到问题。我的脚本如下所示:
# 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)
当我 运行 table(mortality)
我得到一个意外事件 table 如下所示:
dead
treatment no yes
A 63 7
B 61 9
C 68 2
D 63 7
我现在的问题是,我想比较不同治疗(A、B、C、D)之间的死亡人数是否存在统计差异。如果我没记错的话,我可以通过 table 上的卡方检验来做到这一点。但是,我不确定下一步要采取哪一步。
您有一个函数 chisq.test
可以对偶发事件 table 进行卡方测试。
在这里,和你的table
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
你需要取 no 和 yes 这两列:
chisq.test(dead[,2:3])
Pearson's Chi-squared test
data: dead[, 2:3]
X-squared = 4.6996, df = 3, p-value = 0.1952
这在治疗之间没有区别。要查看另一个不同的示例:
dead <- read.table(text = "treatment no yes
A 55 12
B 61 9
C 68 2
D 63 7",header = T)
A处理真正不同的地方:
Pearson's Chi-squared test
data: dead[, 2:3]
X-squared = 8.4334, df = 3, p-value = 0.03785
我们可以通过简单地在 table()
.
summary()
来方便地得到卡方检验
例子
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
注意:“卡方近似值可能不正确”是因为本例中只有 32 个观测值。
根据您的数据 summary(table(mortality))
应该可以。