【问题标题】:Chi Square Analysis using for loop in R在 R 中使用 for 循环进行卡方分析
【发布时间】:2011-09-11 23:36:03
【问题描述】:

我正在尝试对数据中的所有变量组合进行卡方分析,我的代码是:

Data <- esoph[ , 1:3]
OldStatistic <- NA
for(i in 1:(ncol(Data)-1)){
for(j in (i+1):ncol(Data)){
Statistic <- data.frame("Row"=colnames(Data)[i], "Column"=colnames(Data)[j],
                     "Chi.Square"=round(chisq.test(Data[ ,i], Data[ ,j])$statistic, 3),
                     "df"=chisq.test(Data[ ,i], Data[ ,j])$parameter,
                     "p.value"=round(chisq.test(Data[ ,i], Data[ ,j])$p.value, 3),
                      row.names=NULL)
temp <- rbind(OldStatistic, Statistic)
OldStatistic <- Statistic
Statistic <- temp
}
}

str(Data)
'data.frame':   88 obs. of  3 variables:
 $ agegp: Ord.factor w/ 6 levels "25-34"<"35-44"<..: 1 1 1 1 1 1 1 1 1 1 ...
 $ alcgp: Ord.factor w/ 4 levels "0-39g/day"<"40-79"<..: 1 1 1 1 2 2 2 2 3 3 ...
 $ tobgp: Ord.factor w/ 4 levels "0-9g/day"<"10-19"<..: 1 2 3 4 1 2 3 4 1 2 ...


Statistic
    Row Column Chi.Square df p.value
1 agegp  tobgp      2.400 15       1
2 alcgp  tobgp      0.619  9       1

我的代码给出了变量 1 与变量 3 以及变量 2 与变量 3 的卡方分析输出,而变量 1 与变量 2 则缺失。我努力尝试但无法修复代码。任何意见和建议将不胜感激。我想对所有可能的组合进行交叉制表。提前致谢。

编辑

我以前在 SPSS 中做这种分析,但现在我想切换到 R。

【问题讨论】:

    标签: r chi-squared


    【解决方案1】:

    您的数据样本将不胜感激,但我认为这对您有用。首先,使用combn 创建所有列的组合。然后编写一个函数以与apply 函数一起使用来遍历组合。我喜欢使用plyr,因为很容易在后端指定你想要的数据结构。另请注意,您只需为每个列组合计算一次卡方检验,这也应该会加快速度。

    library(plyr)
    
    combos <- combn(ncol(Dat),2)
    
    adply(combos, 2, function(x) {
      test <- chisq.test(Dat[, x[1]], Dat[, x[2]])
    
      out <- data.frame("Row" = colnames(Dat)[x[1]]
                        , "Column" = colnames(Dat[x[2]])
                        , "Chi.Square" = round(test$statistic,3)
                        ,  "df"= test$parameter
                        ,  "p.value" = round(test$p.value, 3)
                        )
      return(out)
    
    })  
    

    【讨论】:

    • +1 感谢 Chase 的有用回答。如果您指导我如何使用所有可能的组合进行交叉制表,我将不胜感激。谢谢
    • 我用 'alply' 功能得到了它。谢谢
    【解决方案2】:

    我编写了自己的函数。它创建了一个矩阵,其中所有名义变量都进行了相互测试。它还可以将结果保存为 excel 文件。它显示所有小于 5% 的 pvalue。

    funMassChi <- function (x,delFirst=0,xlsxpath=FALSE) {
      options(scipen = 999)
    
      start <- (delFirst+1)
      ds <- x[,start:ncol(x)]
    
      cATeND <- ncol(ds)
      catID  <- 1:cATeND
    
      resMat <- ds[1:cATeND,1:(cATeND-1)]
      resMat[,] <- NA
    
        for(nCc in 1:(length(catID)-1)){
          for(nDc in (nCc+1):length(catID)){
            tryCatch({
              chiRes <- chisq.test(ds[,catID[nCc]],ds[,catID[nDc]])
              resMat[nDc,nCc]<- chiRes[[3]]
            }, error=function(e){cat(paste("ERROR :","at",nCc,nDc, sep=" "),conditionMessage(e), "\n")})
          }
        }
      resMat[resMat > 0.05] <- "" 
      Ergebnis <- cbind(CatNames=names(ds),resMat)
      Ergebnis <<- Ergebnis[-1,] 
    
      if (!(xlsxpath==FALSE)) {
         write.xlsx(x = Ergebnis, file = paste(xlsxpath,"ALLChi-",Sys.Date(),".xlsx",sep=""),
                 sheetName = "Tabelle1", row.names = FALSE)
      }
    }
    
    funMassChi(categorialDATA,delFirst=3,xlsxpath="C:/folder1/folder2/")
    

    delFirst 可以删除前 n 列。所以如果你有一个计数指数或者你不想测试的东西。

    我希望这可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多