如何从 glmulti 中排除交互项?

问题描述 投票:0回答:2

我正在安装以下内容:

offspring.survival_STAB <-glmulti(ST~ STAB + Age + BS+ BSP + Sex,
                                     data=ST, 
                                     exclude="BS:BSP",
                                     level=2,
                                     fitfunc=glm,
                                     crit="aicc",
                                     family = Gamma(link = "inverse"),
                                     method = "h")

此代码的目的是获取除交互“BS:BSP”之外的所有可能的交互。

但是,当我在公式中添加 except=c() 时,出现此错误:

Error in glmulti(ST ~ STAB + Age + BS + BSP + Sex, data = ST, exclude = "BS:BSP",  : 
  Improper call of glmulti.

我是否遗漏了一些关于 except=c() 的内容? 是在“glmulti”中指定交互项的另一种方法吗?

r glmulti
2个回答
0
投票

glmulti
不接受公式作为输入。您可以通过文本参数指定回归模型:

glmulti("ST", 
        c("STAB", "Age", "BS", "BSP", "Sex"),
        data=ST, 
        exclude="BS:BSP", etc.)

来自文档https://www.rdocumentation.org/packages/glmulti/versions/0.5-1/topics/glmulti

从评论来看,

glmulti
似乎不起作用。然后就是自己做啦!

该函数采用一个数据框并构建一个包含所有主效应和所有成对相互作用的方程,除了作为参数给出的那些。它假设左侧位于第 1 列中,其余列构成右侧预测变量。根据您的意愿容纳代码:

make_eq = function(df, exclude) {
  Y = colnames(df)[1]
  X = colnames(df)[-1]
  terms = c(X, apply(combn(X, 2), 2, paste, collapse=":"))
  terms = setdiff(terms, exclude)
  terms = paste(terms, collapse=" + ")
  eq = paste0(Y, " ~ ", terms)
  eval(parse(text=eq))
}

obs = data.frame(
  Y = rnorm(10),
  A = rnorm(10),
  B = rnorm(10),
  C = rnorm(10),
  D = rnorm(10)
)

lm(make_eq(obs, c("A:C", "C:D")), data = obs)

0
投票

我阅读了 JSS glmulti 论文,其中包作者陈述了许多排除交互的方法。

我也遇到过这个错误。 我认为变量名称越简单,我的工作假设就越好。 我会持续更新这篇文章。

library(glmulti)
set.seed(1)
myDataFrame <- data.frame( c=rnorm(100),
                           a=as.factor(rbinom(100,5,0.9)),
                           z=rbinom(100,1,0.05))
summary(myDataFrame)
myDataFrame$y <- 3*myDataFrame$c + 
                 2*(myDataFrame$a==4)*myDataFrame$z +
                 2*(myDataFrame$a==5)*myDataFrame$z +
                10* myDataFrame$c    *myDataFrame$z

summary(myDataFrame)
head(myDataFrame)
output <- glmulti(y ~ -1 + c*a*z - c:z - a:c, 
                  data = myDataFrame, 
                  maxit = 30)
summary(output)

## check all the formulas to make sure they
## excluded `c:z` and `a:c`
output@formulas

## and the best formula:
summary(output)$bestmodel


## If we had not excluded `c:z` and `a:c`:
output2 <- glmulti(y ~ -1 + c*a*z, 
                  data = myDataFrame, 
                  maxit = 30)
summary(output2)

## check all the formulas to make sure they
## include `c:z` and `a:c`
output2@formulas

## ...and the best formula:
summary(output2)$bestmodel
© www.soinside.com 2019 - 2024. All rights reserved.