我想使用
lme4
中的 R
包和 lmer()
函数形成尽可能多的模型。
library(tidyverse); library(lme4);
gate = read_csv('gate_data.csv')
m1=lmer(CHG ~ YEAR*BLAREA + YEAR:LOC + YEAR:FOC + YEAR:PSD + YEAR:LAT + YEAR:FAFPAT + (0+YEAR|EYEID), data = gate)
m2 = lmer(CHG ~ YEAR*BLAREA + YEAR:LOC + YEAR:FOC + YEAR:PSD + YEAR:LAT + YEAR:FAFPAT + (1|EYEID), data = gate)
BIC(m1, m2)
使用模型
m1
和m2
,我想要使用m1
的m2
包中的lmer()
函数从lme4
和R
中使用的变量获得更多可能的模型。
#library(tidyverse)
#library(lme4)
#gate <- read_csv('gate_data.CSV')
# Create a vector of independent variables excluding the dependent variable
independent_vars <- setdiff(names(gate), "CHG")
# Create an empty list to store the models
models <- list()
# Loop through all possible combinations of independent variables
for (i in 1:length(independent_vars)) {
model_formula <- paste("CHG ~", paste(c(independent_vars[i], "YEAR"), collapse = "*"))
# Add fixed-effects model to the list
models[[i]] <- lmer(as.formula(model_formula), data = gate)
# Add random-intercept model with random intercept for EYEID to the list
models[[i + length(independent_vars)]] <- lmer(as.formula(model_formula) + (1 | EYEID), data = gate)
# Add mixed-effects model with random intercept for EYEID and random slope for YEAR to the list
models[[i + 2 * length(independent_vars)]] <- lmer(as.formula(model_formula) + (1 + YEAR | EYEID), data = gate)
}
# Print the models
models
我尝试了上述方法,但收到了一条“错误消息:”
公式中没有指定随机效应项请帮帮我。
请注意,有专用方法(
update
、
add1
、
drop1
、
step
)来改变模型项;步骤有助于实现自动化;对于自动
lmer
比较,{lmerTest} 可能会派上用场。