可以使用 group by 和 tidy 将多级模型 (lme) 拟合到重复的 measures/longitudinal 数据吗?

Can group by and tidy be used to fit multilevel models (lme) to repeated measures/longitudinal data?

考虑以下示例longitudinal/repeated 测量数据集

library(tidyverse)
library(broom)
library(nlme)

data <- read.csv("https://stats.idre.ucla.edu/stat/data/study2.csv")
data <- data %>% mutate(dbp =rnorm(120, 30:150), sbp = rnorm(120, 50:200),bmi 
= rnorm(120,15:40), chol = rnorm(120,50:350), insulin = rnorm(120,2:40), educ = rnorm(120,5:10))

我可以使用 group_by %>% do(tidy(*)) 到 运行 几个未调整和调整的 单级 回归模型(循环遍历结果和风险列表)和提取模型结果到数据框如下

out <-c("pulse","insulin","chol")
exp <- c("factor(exertype)","sbp","dbp")
conf <- c("bmi","factor(diet)")

#Unadjusted models - single level regression (lm)
#################################################
Unadjusted <- expand.grid(out, exp) %>%
group_by(Var1) %>% rowwise() %>%
summarise(frm = paste0(Var1, "~", Var2)) %>%
group_by(model_id = row_number(),frm) %>%
do(tidy(lm(.$frm, data = data))) %>%
mutate(lci = estimate-(1.96*std.error)) %>%
mutate(uci = estimate+(1.96*std.error))

#Adjusted models - single level regression (lm)
###############################################
Adjusted <- expand.grid(out, exp, conf) %>%
group_by(Var1, Var2) %>%
summarise(Var3 = paste0(Var3, collapse = "+")) %>%
rowwise() %>%
summarise(frm = paste0(Var1, "~", Var2, "+", Var3)) %>%
group_by(model_id = row_number(), frm) %>%
do(tidy(lm(.$frm, data = data))) %>%
mutate(lci = estimate-(1.96*std.error)) %>%
mutate(uci = estimate+(1.96*std.error))

我想使用相同的过程来拟合 多级 模型来解释重复数据。使用示例代码:

lme(sbp ~ pulse+factor(diet)+time, data=data, random= ~time|id, method ="ML")

但是,当我尝试这样做时,例如使用:

#Unadjusted models - multi-level regression (lme)
#################################################
Unadjusted <- expand.grid(out, exp) %>%
group_by(Var1) %>% rowwise() %>%
summarise(frm = paste0(Var1, "~", Var2)) %>%
group_by(model_id = row_number(),frm) %>%
do(tidy(lme(.$frm, data = data, random= ~time|id, method = "ML"))) %>%
mutate(lci = estimate-(1.96*std.error)) %>%
mutate(uci = estimate+(1.96*std.error))

我收到以下错误消息:

Error in UseMethod("lme") : no applicable method for 'lme' applied to an object of class "character"

关于如何使它适用于 lme 类型模型的任何想法?

lm不同,lme不接受公式作为字符值。您需要将其显式转换为公式。尝试只添加 as.formula()

do(tidy(lme(as.formula(.$frm), ...))) %>%