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



我有一些非常基本的代码,用于为titanic数据集中的一些变量生成chisq测试。我想有一种方法来区分分类变量和数字/控制变量,所以它只会对分类变量进行chisq测试,或者如果有数字变量,则进行t.test。

我感兴趣的是能够比较幸存和未幸存群体之间的多个级别,比如:

道具幸存女性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], tabla[2,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, 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 

第二个输出:

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 

最新更新