本文介绍了在R函数中产生最小量的变量组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有兴趣找出下面模型中哪种变量组合(binge followup sreport age)产生最小的I2统计量(从最小到最大).每个模型的I2都是这样获得的:

I'm interested in finding out which combination of variables (binge followup sreport age) in my model below produce smallest I2 statistic in rank order (smallest to largest). The I2 from each model is obtained like so:

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

有没有一种方法可以通过遍历公式在R中自动执行此操作?

Is there a way to automate this in R by looping over formulas?

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

Ex: First fitting effectsize ~ binge, then effectsize ~ binge + followup then ...

注意: :假设我已存储了所有变量的名称,如下所示: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.

推荐答案

您可以按照您的建议创建解释变量的所有组合,只需稍作更改即可(以非递归方式取消列出),然后转换为公式:

You can create all combinations of your explanatory variables as you suggest, with a small change (to unlist non-recursively), then convert to formulas:

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 = '+')))

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

Then apply each formula to your data:

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

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

You can then obtain your I2 on each member of the fit, and then a which.min() will give you the one with the smallest I2.

这篇关于在R函数中产生最小量的变量组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 17:25