如何在 R 中循环这个高阶函数?

How can you loop this higher-order function in R?

这个问题与我收到的回复有关 with a nice little function from thelatemail。 我使用的数据框不是最优的,但它是我所拥有的,我只是想在所有行中循环这个函数。

这是我的df

dput(SO_Example_v1)
structure(list(Type = structure(c(3L, 1L, 2L), .Label = c("Community", 
"Contaminant", "Healthcare"), class = "factor"), hosp1_WoundAssocType = c(464L, 
285L, 24L), hosp1_BloodAssocType = c(73L, 40L, 26L), hosp1_UrineAssocType = c(75L, 
37L, 18L), hosp1_RespAssocType = c(137L, 77L, 2L), hosp1_CathAssocType = c(80L, 
34L, 24L), hosp2_WoundAssocType = c(171L, 115L, 17L), hosp2_BloodAssocType = c(127L, 
62L, 12L), hosp2_UrineAssocType = c(50L, 29L, 6L), hosp2_RespAssocType = c(135L, 
142L, 6L), hosp2_CathAssocType = c(95L, 24L, 12L)), .Names = c("Type", 
"hosp1_WoundAssocType", "hosp1_BloodAssocType", "hosp1_UrineAssocType", 
"hosp1_RespAssocType", "hosp1_CathAssocType", "hosp2_WoundAssocType", 
"hosp2_BloodAssocType", "hosp2_UrineAssocType", "hosp2_RespAssocType", 
"hosp2_CathAssocType"), class = "data.frame", row.names = c(NA, 
-3L))
####################
#what it looks like#
####################
require(dplyr)
df <- tbl_df(SO_Example_v1)
head(df)
         Type hosp1_WoundAssocType hosp1_BloodAssocType hosp1_UrineAssocType
1  Healthcare                  464                   73                   75
2   Community                  285                   40                   37
3 Contaminant                   24                   26                   18
Variables not shown: hosp1_RespAssocType (int), hosp1_CathAssocType (int), hosp2_WoundAssocType
  (int), hosp2_BloodAssocType (int), hosp2_UrineAssocType (int), hosp2_RespAssocType (int),
  hosp2_CathAssocType (int)

我的功能是对 df$Type 中的所有类别执行 chisq.test。理想情况下,如果单元格计数小于 5,则函数应切换为 fisher.test(),但这是一个单独的问题(不过,为想出如何做到这一点的人提供额外的加分)。

这是我用来逐行查看的函数

func <- Map(
  function(x,y) {
    out <- cbind(x,y)
    final <- rbind(out[1,],colSums(out[2:3,]))
    chisq <- chisq.test(final,correct=FALSE)
    chisq$p.value
  },
  SO_Example_v1[grepl("^hosp1",names(SO_Example_v1))],
  SO_Example_v1[grepl("^hosp2",names(SO_Example_v1))] 
)
func

但理想情况下,我希望它是这样的

for(i in 1:nrow(df)){func}

但这不起作用。另一个钩子是,例如,当采用第二行时,final 调用看起来像这样

func <- Map(
  function(x,y) {
    out <- cbind(x,y)
    final <- rbind(out[2,],colSums(out[c(1,3),]))
    chisq <- chisq.test(final,correct=FALSE)
    chisq$p.value
  },
  SO_Example_v1[grepl("^hosp1",names(SO_Example_v1))],
  SO_Example_v1[grepl("^hosp2",names(SO_Example_v1))] 
)
func

所以该函数应该明白它为 out[x,] 获取的单元格计数必须从 colSums() 中排除。这个 data.frame 只有 3 行,所以很简单,但是我已经尝试将这个函数应用到一个单独的 data.frame 我有超过 200 行,所以能够循环这个会很好不知何故。

感谢任何帮助。

干杯

您遗漏了两件事:

  1. 到 select 行 i 和 select 除了你要使用的这一行 u[i]u[-i]
  2. 如果一个项目的长度与给定给Map的其他项目的长度不同,它就会被回收,这是一种非常通用的属性语言。然后,您只需向函数添加一个参数,该参数对应于您想要反对其他行的行,它将被传递的向量的所有项目回收。

以下内容满足您的要求

    # the function doing the stats
    FisherOrChisq <- function(x,y,lineComp) {
        out <- cbind(x,y)
        final <- rbind(out[lineComp,],colSums(out[-lineComp,]))
        test <- chisq.test(final,correct=FALSE)

        return(test$p.value)
    }

    # test of the stat function
    FisherOrChisq(SO_Example_v1[grep("^hosp1",names(SO_Example_v1))[1]],
    SO_Example_v1[grep("^hosp2",names(SO_Example_v1))[1]],2)

    # making the loop
    result <- c()
    for(type in SO_Example_v1$Type){
        line <- which(SO_Example_v1$Type==type)
        res <- Map(FisherOrChisq,
                    SO_Example_v1[grepl("^hosp1",names(SO_Example_v1))],
                    SO_Example_v1[grepl("^hosp2",names(SO_Example_v1))],
                    line
                )
        result <- rbind(result,res)
    }
    colnames(result) <- gsub("^hosp[0-9]+","",colnames(result))
    rownames(result) <- SO_Example_v1$Type

也就是说,您正在做的是非常繁重的多重测试。我会非常谨慎地使用相应的 p 值,您至少需要使用多重测试校正,例如建议的 here.