remove all fixed effects from mixed model
我正在尝试使用
首先,如果固定因子不连续,则
第二,如果存在交互作用,则部分匹配会分解并仅识别第一项(例如,
这是一些示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #Create example data set.seed(9) data=data.frame(y=rnorm(100,5,10),y.binom=rbinom(100,1,0.5), y.poisson=rpois(100,5),fixed1=rnorm(100,20,100), fixed2=c("Treatment1","Treatment2"),covar=rnorm(100,20,100), rand1=LETTERS[1:2], rand2=c(rep("W",25),rep("X",25),rep("Y",25),rep("Z",25))) library(lme4) #Fit generalized linear mixed effects model mod=glmer(y.poisson~fixed1*fixed2+covar+(1|rand2/rand1),family="poisson",data) #Pull out names of fixed effects fixef.names=do.call(rbind,lapply(1:length(names(fixef(mod))[-1]),function(j) { d=colnames(mod@frame)[pmatch(colnames(mod@frame),names(fixef(mod))[-1][j])>0] d[!is.na(d)] } ) )[,1] # Generate null model (intercept and random effects only, no fixed effects) null.mod=update(mod,paste(".~.-",paste(fixef.names,collapse="-"),sep="")) |
感谢您的帮助!
1 2 3 4 5 6 7 | parens <- function(x) paste0("(",x,")") onlyBars <- function(form) reformulate(sapply(findbars(form), function(x) parens(deparse(x))), response=".") onlyBars(formula(mod)) ## . ~ (1 | rand1:rand2) + (1 | rand2) update(mod,onlyBars(formula(mod))) |
1)仅针对单一效果:是否会衍生出"变量"名称,然后将处理方式与用于拟合的数据框中的内容进行匹配,以使您获得正确的名称以提取/添加到更新语句中?铅>
2)对于交互,也许首先尝试使用:进行拆分。然后检查输出的长度。如果> 1,则匹配两个变量,并根据需要删除/添加到更新。它不会像函数那样优雅,但应该可以。
3)为什么不只使用glmulti库?它已经以自动化的方式做到这一点。如果您不想要所有拟合模型,只需提取想要的模型并继续。但是它将所有适合的对象存储在其对象结构中。