将R中的一个变量关联到多个变量

sirbozc5  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(142)

我的问卷数据库由39个变量组成,每个变量有20个名义型响应。我正在为所有变量创建2 x 2列联表,由于样本数量较少,我愿意对计数数据应用Fisher精确检验。
现在我试着把变量1和其他38个变量联系起来,然后把变量2和其他37个变量联系起来,以此类推.
我可以做列联表和Fisher精确检验,就像下一个代码一样,但是有点单调。

Who_answers.age <- table(BD$Who_answers,BD$age, dnn = c("Who_answers","age"))
FT.Who_answers.age<-fisher.test(x = Who_answers.age, alternative = "two.sided")
FT.Who_answers.age$p.value

Who_answers.gender <- table(BD$Who_answers,BD$gender, dnn = c("Who_answers","gender"))
FT.Who_answers.gender<-fisher.test(x = Who_answers.gender, alternative = "two.sided")
FT.Who_answers.gender$p.value

Who_answers.diagnosis <- table(BD$Who_answers,BD$diagnosis, dnn = c("Who_answers","diagnosis"))
FT.Who_answers.diagnosis<-fisher.test(x = Who_answers.diagnosis, alternative = "two.sided")
FT.Who_answers.diagnosis$p.value

Who_answers.acomp_rutsu <- table(BD$Who_answers,BD$acomp_rutsu, dnn = c("Who_answers","acomp_rutsu"))
FT.Who_answers.acomp_rutsu<-fisher.test(x = Who_answers.acomp_rutsu, alternative = "two.sided")
FT.Who_answers.acomp_rutsu$p.value

Who_answers.Cuarto_propio <- table(BD$Who_answers,BD$Cuarto_propio, dnn = c("Who_answers","Cuarto_propio"))
FT.Who_answers.Cuarto_propio<-fisher.test(x = Who_answers.Cuarto_propio, alternative = "two.sided")
FT.Who_answers.Cuarto_propio$p.value

Who_answers.Duerme_con <- table(BD$Who_answers,BD$Duerme_con, dnn = c("Who_answers","Duerme_con"))
FT.Who_answers.Duerme_con<-fisher.test(x = Who_answers.Duerme_con, alternative = "two.sided")
FT.Who_answers.Duerme_con$p.value

有没有一种方法可以更容易地对所有这些关联和它们的Fisher精确检验进行编码?
正如我在前面的问题中提到的,我对R还是一个新手。非常感谢你的帮助。

3pvhb19x

3pvhb19x1#

纯粹技术性的回答从而避免了是否应该这样做的问题。

to_do_list <- c("age","gender","diagnosis") # extend as needed

(pval_results <- lapply(to_do_list,\(x){
  fisher.test(x =  table(BD$Who_answers,BD[[x]]),
                                  alternative = "two.sided")$p.value
}) |> setNames(to_do_list))

相关问题