在R函数中产生最小数量的变量组合

我有兴趣找出下面模型中哪种变量组合(binge followup sreport age)产生最小的I2统计量顺序(最小到最大)。每个模型的I2如下获得:

I2 <- function(x)as.double(x$mod_info$I.2)

有没有一种方法可以通过循环公式自动在R中实现这一目标?

示例::首先拟合effectsize ~ binge,然后拟合effectsize ~ binge + followup,然后...

注意: :假设我已存储了所有变量的名称,例如:var.names = c("binge","followup","sreport","age")

library(robumeta)

fit <- robu(effectsize ~ binge + followup + sreport + age,data = get(data(hierdat)),study = studyid,var = var)

# Get the `I2` for the above model:

I2(fit) # gives 63.993

# Note: I think `lapply(seq_along(var.names),function(i)combn(var.names,i))` can 
                        # give us each combination that should be used in the formula.
ccwysxbf 回答:在R函数中产生最小数量的变量组合

您可以按照建议创建解释变量的所有组合,进行一些小的更改(以非递归方式列出),然后转换为公式:

combos <- unlist(lapply(seq_along(var.names),function(i) combn(var.names,i,simplify = FALSE)),recursive = FALSE)

formulae <- lapply(combos,function(x) paste('effectsize ~',paste(x,collapse = '+')))

然后将每个公式应用于您的数据:

fit <- lapply(formulae,function(f) 
                         robu(as.formula(f),data = get(data(hierdat))))

然后您可以在拟合的每个成员上获取I2,然后通过which.min()将获得最小I2的那个。

本文链接:https://www.f2er.com/3141823.html

大家都在问