Chisq.test/prop.test 对不同长度的向量

Posted

技术标签:

【中文标题】Chisq.test/prop.test 对不同长度的向量【英文标题】:Chisq.test/prop.test on vectors of different lengths 【发布时间】:2022-01-22 02:21:57 【问题描述】:

我正在尝试对一些患者进行手术而其他患者不进行手术的数据进行 chisq.test 或 prop.test。我想看看两组中两种性别的比例是否相等。在我的手术组和非手术组大小不相等的情况下,最合适的测试是什么?

我正在尝试使用以下代码,使用 data.table 但我也得到了以下错误:

> chisq.test(lateral[Surgery == "No", "sex",with=F], lateral[Surgery == "Yes", "sex",with=F])

Error in chisq.test(lateral[Surgery == "No", "sex", with = F], lateral[Surgery ==  : 
  'x' and 'y' must have the same length

For prop.test():
Error in complete.cases(x, n) : not all arguments have the same length

数据

setDT(structure(list(sex = c(2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L), Surgery = c("No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes" )), row.names = c(NA, -239L), class = c("data.table", "data.frame" )))

【问题讨论】:

你能提供你的数据dput(your_df_goes_here)吗? 已添加!谢谢! 虽然我们清理起来并不难,但当您发布data.tabledput(.) 输出时,它包含一个无法使用的组件.internal.selfref = <pointer: ...>。我正在编辑您的问题以将其删除并包含在setDT(.) 中,以便它可以按原样有效使用。请在未来的问题中考虑这一点。谢谢! 底线:使用chisq.test(table(lateral)) 【参考方案1】:

您可以将表格传递给函数chisq.test,使其更简单。例如

df <- structure(list(sex = c(2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 
                       1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 
                       2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
                       1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 
                       1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
                       2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
                       1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
                       2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 
                       1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                       2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
                       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
                       2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                       1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 
                       1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 
                       2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
                       2L, 2L, 2L, 1L, 2L), Surgery = c("No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
                                                        "No", "No", "No", "No", "No", "No", "Yes", "Yes", "Yes", "Yes", 
                                                        "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
                                                        "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
                                                        "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
                                                        "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
                                                        "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
                                                        "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
                                                        "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
                                                        "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
                                                        "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"
                       )), row.names = c(NA, -239L), class = c("data.table", "data.frame"))

df$sex <- as.factor(df$sex)
df$Surgery <- as.factor(df$Surgery)
(prop_table <- table(df))
# Surgery
# sex No Yes
# 1 97  66
# 2 57  19
mod1 <- chisq.test(prop_table)
mod1
# Pearson's Chi-squared test with Yates' continuity correction
# 
# data:  prop_table
# X-squared = 4.7727, df = 1, p-value = 0.02892
str(mod1)
# List of 9
# $ statistic: Named num 4.77
# ..- attr(*, "names")= chr "X-squared"
# $ parameter: Named int 1
# ..- attr(*, "names")= chr "df"
# $ p.value  : num 0.0289
# $ method   : chr "Pearson's Chi-squared test with Yates' continuity correction"
# $ data.name: chr "prop_table"
# $ observed : 'table' int [1:2, 1:2] 97 57 66 19
# ..- attr(*, "dimnames")=List of 2
# .. ..$ sex    : chr [1:2] "1" "2"
# .. ..$ Surgery: chr [1:2] "No" "Yes"
# $ expected : num [1:2, 1:2] 105 49 58 27
# ..- attr(*, "dimnames")=List of 2
# .. ..$ sex    : chr [1:2] "1" "2"
# .. ..$ Surgery: chr [1:2] "No" "Yes"
# $ residuals: 'table' num [1:2, 1:2] -0.783 1.147 1.055 -1.544
# ..- attr(*, "dimnames")=List of 2
# .. ..$ sex    : chr [1:2] "1" "2"
# .. ..$ Surgery: chr [1:2] "No" "Yes"
# $ stdres   : 'table' num [1:2, 1:2] -2.33 2.33 2.33 -2.33
# ..- attr(*, "dimnames")=List of 2
# .. ..$ sex    : chr [1:2] "1" "2"
# .. ..$ Surgery: chr [1:2] "No" "Yes"
# - attr(*, "class")= chr "htest"


【讨论】:

以上是关于Chisq.test/prop.test 对不同长度的向量的主要内容,如果未能解决你的问题,请参考以下文章

数据类型--数字

字符串split操作到底有多少坑

在多个 UITextView 中显示长文本

Http协议不同版本的区别

在编译时避免变长堆栈数组

长脖子鹿放置(最大流)