【问题标题】:R Chi-Squared Table FormatR卡方表格式
【发布时间】:2014-10-14 03:35:16
【问题描述】:

所以我有一些格式如下的数据:

header1    header2
"nocandy"  "nocandy"
"nocandy"  "nocandy"
"nocandy"  "nocandy"
"nocandy"    "candy"
"nocandy"    "candy"
"candy"    "candy"
etc...

我用candytext <- read.table("candytest.txt", header=TRUE) 导入了它 我想做一个卡方检验,看看两组之间是否存在差异。 当我使用函数table(candytest) 时,我得到如下信息:

         header2
header1   candy nocandy
  candy     112      39
  nocandy     4      82

但是如果我运行 summary(candytest) 我会得到这样的结果:

    header1       header2   
 candy  :151   candy  :116  
 nocandy: 86   nocandy:121 

如您所见,这两个表格的格式不同。但是,我可以在第一个表上运行卡方检验,但不能在第二个表上运行。然而,汇总表更像是我需要用来执行chisq.test() 的表。第二个表看起来像是假设数据是配对的,但数据不是配对的。如果它是配对的,那就没问题了,我可以在table(candytest) 的输出上使用 McNemars 测试,但它没有配对。那么如何创建一个看起来像汇总表的 2 × 2 矩阵,而无需手动输入。我意识到我可以将汇总表复制到矩阵中,但是我想知道如何在 R 中正确地将其转换为函数。

谢谢!

【问题讨论】:

  • summary 不是为做交叉表或卡方统计而设计的。它不返回表对象。它逐列返回文本输出。请阅读帮助页面。
  • 您可以通过summary(table(df)) 获得卡方检验结果。但您可能需要将数据重新排列成组
  • @RichardScriven - 注意它们不一样:summary(table(df)) 等价于chisq.test(tab, correct=FALSE),其中correct=TRUE 是默认值。
  • 我想我应该写到summary.data.frame 不是这样设计的。
  • 键入 methods(summary) 以查看其他一些函数名称。

标签: r statistics


【解决方案1】:

在这里,假设classes 列是因子,我试图使用lapplydf1 的每一列上获取summary。从帖子来看,我猜是这样的。在list 输出上使用do.call(data.frame,将其转换为data.frame

  do.call(data.frame,lapply(df1, summary)) #in case a matrix output is needed, just replace `data.frame` with `cbind`
  #          header1 header2
  #candy         1       3
  #nocandy       5       3


  summary(df1)
  #   header1     header2 
  #candy  :1   candy  :3  
  #nocandy:5   nocandy:3  

如果您只需要从数据集中的许多列中选择列,

  nm1 <- paste0("header",1:2) #names of columns to do the summary
   do.call(`cbind`, lapply(df1[nm1], summary))
   #        header1 header2
   #candy         1       3
   #nocandy       5       3

你也可以用data.tablesummary

  library(data.table)
  DT <- setDT(df1)[, lapply(.SD, summary)]   #or

 #DT <- setDT(df1)[, lapply(.SD, table)] 
  DT
   #    header1 header2
   #1:       1       3
   #2:       5       3

 chisq.test(DT)

 #    Pearson's Chi-squared test with Yates' continuity correction

  #data:  DT
  #X-squared = 0.375, df = 1, p-value = 0.5403

  #Warning message:
  #In chisq.test(DT) : Chi-squared approximation may be incorrect

数据

df1 <- structure(list(header1 = structure(c(2L, 2L, 2L, 2L, 2L, 1L), .Label = c("candy", 
"nocandy"), class = "factor"), header2 = structure(c(2L, 2L, 
2L, 1L, 1L, 1L), .Label = c("candy", "nocandy"), class = "factor")), .Names = c("header1", 
"header2"), row.names = c(NA, -6L), class = "data.frame")

【讨论】:

  • 漂亮!现在你能解释一下这是做什么的吗?对我来说它看起来像巫术,我对 R 比较陌生。有没有更好的方法可以导入它,或者只有更好的方法可以进行测试?
  • @jelijelidjango 我添加了一些解释。
【解决方案2】:

听起来您想将列视为独立样本。如果是这样,这可能不是最好的数据结构。但你可以这样做

#sample data
candytext<-read.table(text='header1    header2
 "nocandy"  "nocandy"
 "nocandy"  "nocandy"
 "nocandy"  "nocandy"
 "nocandy"    "candy"
 "nocandy"    "candy"
 "candy"    "candy"', header=T)

#summarize
do.call(cbind, lapply(candytext, table))
#         header1 header2
# candy         1       3
# nocandy       5       3

【讨论】:

  • 是的,我认为这不是最好的数据结构。但我刚刚熟悉 R。你会推荐使用什么?我应该如何开始?
  • 两列:一列用于观察(即“candy”、“nocandy”)和一列用于组(“group1”与“group2”)
【解决方案3】:

试试:

> dd = data.frame(sapply(candytext, summary))
> dd
        header1 header2
candy         1       3
nocandy       5       3

> chisq.test(dd)                
        Pearson's Chi-squared test with Yates' continuity correction                                                    

data:  dd                                                                                                               
X-squared = 0.375, df = 1, p-value = 0.5403                                                                             

Warning message:                                                                                                        
In chisq.test(dd) : Chi-squared approximation may be incorrect                                                          
>                                                                               

如果要从多列数据框中选择 2 列:

> cc = cbind(summary(candytext$header1), summary(candytext$header2))

> cc
        [,1] [,2]
candy      1    3
nocandy    5    3

> chisq.test(cc)

        Pearson's Chi-squared test with Yates' continuity correction

data:  cc
X-squared = 0.375, df = 1, p-value = 0.5403

Warning message:
In chisq.test(cc) : Chi-squared approximation may be incorrect

在下面的表格中,表格和摘要是一样的:

> cbind(table(candytext$header1), table(candytext$header2))
        [,1] [,2]
candy      1    3
nocandy    5    3
> 
> cbind(summary(candytext$header1), summary(candytext$header2))
        [,1] [,2]
candy      1    3
nocandy    5    3

【讨论】:

    猜你喜欢
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    相关资源
    最近更新 更多