使用glmer
,我可以运行逻辑回归混合模型。但是,当我尝试使用glmulti
进行相同操作时,会出现错误(如下所述)。我认为问题在于我指定要在glmulti
中使用的函数。我想要一个函数,该函数使用logit链接为包含连续固定协变量和分类随机效应的数据指定逻辑回归模型。响应变量是二进制0/1。
样本数据:
library(lme4)
library(rJava)
library(glmulti)
set.seed(666)
x1 = rnorm(1000) # some continuous variables
x2 = rnorm(1000)
x3 = rnorm(1000)
r1 = rep(c("red", "blue"), times = 500) #categorical random effects
r2 = rep(c("big", "small"), times = 500)
z = 1 + 2*x1 + 3*x2 +2*x3
pr = 1/(1+exp(-z))
y = rbinom(1000,1,pr) # bernoulli response variable
df = data.frame(y=y,x1=x1,x2=x2, x3=x3, r1=r1, r2=r2)
一个glmer logistic回归就可以了:
model1<-glmer(y~x1+x2+x3+(1|r1)+(1|r2),data=df,family="binomial")
但是当我尝试通过glmulti使用相同的模型结构时会发生错误:
# create a function - I think this is where my problem is
glmer.glmulti<-function(formula, data, family=binomial(link ="logit"), random="", ...){
glmer(paste(deparse(formula),random),data=data,...)
}
# run glmulti models
glmulti.logregmixed <-
glmulti(formula(glmer(y~x1+x2+x3+(1|r1)+(1|r2), data=df), fixed.only=TRUE), #error w/o fixed.only=TRUE
data=df,
level = 2,
method = "g",
crit = "aicc",
confsetsize = 128,
plotty = F, report = F,
fitfunc = glmer.glmulti,
family = binomial(link ="logit"),
random="+(1|r1)","+(1|r2)", # possibly this line is incorrect?
intercept=TRUE)
#Errors returned:
singular fit
Error in glmulti(formula(glmer(y ~ x1 + x2 + x3 + (1 | r1) + (1 | r2), :
Improper call of glmulti.
In addition: Warning message:
In glmer(y ~ x1 + x2 + x3 + (1 | r1) + (1 | r2), data = df) :
calling glmer() with family=gaussian (identity link) as a shortcut to lmer() is deprecated; please call lmer() directly
我在glmulti代码的formula
和fitfunc
部分中尝试了对该函数的各种更改。我尝试用lmer
代替glmer
,但我想我不明白该错误。我还担心调用lmer
可能会更改模型结构,因为在我的尝试之一中,模型的summary()
表示"Linear mixed model fit by REML ['lmerMod']."
,我需要glmulti模型与之相同我使用model1
和glmer
来获取(即summary(model1)
给出了"Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']"
许多类似的问题仍未得到解答。预先感谢!
信用:
在以下帮助下创建的样本数据集: https://stats.stackexchange.com/questions/46523/how-to-simulate-artificial-data-for-logistic-regression
glmulti代码从此处改编而成: Model selection using glmulti