在R中创建多个Chisq / t检验的更有效方法是什么? 使用泰坦尼克号数据

问题描述

我有一些非常基本的代码,可以为钛酸数据集中的某些变量生成chisq测试。我想有一种区分分类变量与数字变量/连续变量的方法,因此,如果有数字变量,它只会对分类变量或t.tests进行chisq检验。

我对能够比较幸存者和未幸存者之间的多个等级感兴趣,例如:

道具幸存女性vs道具未幸存女性, 道具生存1级vs道具生存1级 等等。

表格子集用于比较幸存/未幸存的女性

library(Titanic)

titanic <- as.data.frame(Titanic)
names <- names(titanic)
names(cars)

for (var in names) { 
  tabla<-table(titanic$Survived,titanic[[var]])
  tabla<-addmargins(tabla)
  print(tab)
  res<-prop.test(x = c(tabla[1,2],tabla[2,2]),n = c(tabla[1,3],3]),correct = F)
  print(var)
  print(res)

}
}

Thank you

解决方法

我建议您使用检测变量类的函数。我已草绘了一个功能,您可以根据需要进行修改。它需要两个参数,数据框和变量名称。

library(titanic)
#Data
data("Titanic")
titanic <- as.data.frame(Titanic)
#Function
mytest <- function(data,x)
{
  #Detect the type of var
  if(is.numeric(data[[x]]))
  {
    #Build variables x and y
    a <- data[[x]][data$Survived=='No']
    b <- data[[x]][data$Survived=='Yes']
    #Apply the test
    Res <- t.test(a,b)
    print(Res)
  } else
  {
    #Create table
    tab <- table(data$Survived,data[[x]])
    #Split in a list of vectors
    L1 <- lapply(1:ncol(tab),function(i) {tab[,i] })
    names(L1) <- dimnames(tab)[[2]]
    #Margins
    Margins <- rowSums(tab)
    #Test
    L2 <- lapply(L1,function(z) {prop.test(x = z,n = Margins,correct = F)})
    print(L2)
  }
}

一些例子:

#Apply the function
mytest(data = titanic,x = 'Sex')
mytest(data = titanic,x = 'Freq')

输出:

mytest(data = titanic,x = 'Sex')

$Male

    2-sample test for equality of proportions without continuity correction

data:  z out of Margins
X-squared = 0,df = 1,p-value = 1
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.346476  0.346476
sample estimates:
prop 1 prop 2 
   0.5    0.5 


$Female

    2-sample test for equality of proportions without continuity correction

data:  z out of Margins
X-squared = 0,p-value = 1
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.346476  0.346476
sample estimates:
prop 1 prop 2 
   0.5    0.5 

第二个输出:

mytest(data = titanic,x = 'Freq')

Welch Two Sample t-test

data:  a and b
t = 1.013,df = 17.768,p-value = 0.3246
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -52.38066 149.75566
sample estimates:
mean of x mean of y 
  93.1250   44.4375