I am currently working with 30 datasets with the same column names, but different numeric data. I need to apply a linear mixed model and a generalised linear model to every instance of the dataset and plot the resulting fixed effect coefficients on a forest plot.
The data is currently structured as follows (using the same dataset for every list element for simplicity):
library(lme4)
data_list <- list()
# There's definitely a better way of doing this through lapply(), I just can't figure out how
for (i in 1:30){
data_list[[i]] <- tibble::as_tibble(mtcars) # this would originally load different data at every instance
}
compute_model_lmm <- function(data){
lmer("mpg ~ hp + disp + drat + (1|cyl)", data = data)
}
result_list_lmm <- lapply(data_list, compute_model_lmm)
What I am currently doing is
library(modelsummary)
modelplot(result_list_lmm)+
facet_wrap(~model) #modelplot() takes arguments/functions from ggplot2
which takes an awful amount of time, but it works.
Now, I would like to compare another model on the same plot, as in
compute_model_glm <- function(data){
glm("mpg ~ hp + disp + drat + cyl", data = data)
}
result_list_glm <- lapply(data_list, compute_model_glm)
modelplot(list(result_list_lmm[[1]], result_list_glm[[1]]))
but for every instance of the plot.
How do I specify it to modelplot()
?
Thanks in advance!