循环遍历函数参数(与 multcomp::glht 的一系列对比)

Looping through function arguments (series of contrasts with multcomp::glht)

我希望编写一个函数,运行s 对比回归模型并引导这些结果以获得置信区间,在对比列表上循环该函数。

我已经尝试了嵌套在函数中的循环,lapply,map ... none 似乎得到了我想要的(returns 结果只是第一个对比列表或最后一个)。

对于对比列表中的单个对比,代码如下所示:

df <- data.frame(

  H0013301_new_data = c(0,2,3,6,0,4,2,4,8,1),
  drink_stat94_KEYES_2 = c("Heavy","Abstainer","Occasional","Moderate","Abstainer","Occasional","Heavy","Moderate","Moderate","Abstainer"),
  drink_stat02_KEYES_2 = c("Heavy","Abstainer","Occasional","Abstainer","Abstainer","Heavy","Heavy","Moderate","Moderate","Abstainer"),
  drink_stat06_KEYES_2 = c("Occasional","Abstainer","Occasional","Abstainer","Occasional","Heavy","Heavy","Moderate","Moderate","Heavy"),
FIN_weight_survPS_trimmed=
c(.5,2.4,.6,4.8,1.2,.08,.34,.56,1.6,.27)
)

#reordering factors
df$drink_stat94_KEYES_2<-fct_relevel(df$drink_stat94_KEYES_2, "Abstainer", "Occasional", "Moderate", "Heavy")
contrasts(df$drink_stat94_KEYES_2)<-contr.treatment(4,base=1)
df$drink_stat02_KEYES_2<-fct_relevel(df$drink_stat02_KEYES_2, "Abstainer", "Occasional", "Moderate", "Heavy")
contrasts(df$drink_stat02_KEYES_2)<-contr.treatment(4,base=1)
df$drink_stat06_KEYES_2<-fct_relevel(df$drink_stat06_KEYES_2, "Abstainer", "Occasional", "Moderate", "Heavy")
contrasts(df$drink_stat06_KEYES_2)<-contr.treatment(4,base=1)



#defining contrast
c1 <- rbind("A,A,A"=c(1,0,0,0,0,0,0,0,0,0)
                ) 

#defining function to feed to boostrap
fc_2<-function(d,i){
 TrialOutcomeModel_M<-lm(H0013301_new_data ~ drink_stat94_KEYES_2 + drink_stat02_KEYES_2 + drink_stat06_KEYES_2, weights=FIN_weight_survPS_trimmed, data = d[i,]) 
 test <- multcomp::glht(TrialOutcomeModel_M, linfct=c1) 
 return(coef(test))
}
boot_out<-boot(data=df, fc_2, R=500) 
boot.ci(boot_out, type="perc")

但让我们假设我想 运行 我的函数(并增强结果)在以下对比列表中,而不只是 c1:

c1 <- rbind("A,A,A"=c(1,0,0,0,0,0,0,0,0,0)
                ) 
c2 <- rbind("A,A,O"=c(1,0,0,0,0,0,0,1,0,0)
                ) 
c3 <- rbind("A,A,M"=c(1,0,0,0,0,0,0,0,1,0)
                ) 

c_vector<-list(c1,c2,c3)

关于我将如何处理这个问题的任何建议代码? (P.S。我知道 linfct 参数可以采用对比矩阵,但我专门寻找 loop/lapply 解决方案)。

(下面我将引用您在示例代码中创建的对象)

计划有两个步骤:

  1. 准备一个函数fun_boot(),它接受一个对比对象(如c1),returns一个基于它的boot对象和df数据;

  2. 将该函数应用于对比列表 c_vector

因此,实现 有 2 个元素:

# [!] Assume all required libraries loaded
# [!] Assume all necessary data exists

# Step 1
fun_boot <- function(contrast)
{
    # Make statistic function
    fun_statistic <- function(d, i)
    {
        TrialOutcomeModel_M <- lm(
           formula = H0013301_new_data ~ drink_stat94_KEYES_2 + drink_stat02_KEYES_2 + drink_stat06_KEYES_2,
           data    = d[i,],
           weights = FIN_weight_survPS_trimmed
        ) 
        test <- multcomp::glht(
           TrialOutcomeModel_M,
           linfct = contrast
        )
        return(coef(test))
    }

    # Make boot call (hehe)
    return (boot(
       data      = df,
       statistic = fun_statistic,
       R         = 500
    ))
}

# Step 2
boot_out_vector <- lapply(
   X   = c_vector,
   FUN = fun_boot
)