英文:
Efficiently and iteratively fit, diagnose, modify, and organize linear models in R (into one place)
问题
以下是您要翻译的内容:
"oftentimes I need to fit several linear models (dozens) in R. These models may have the same predictor but varying response variables, data distribution families, and more.
What I've been doing: fitting models one by one, looking at diagnostic plots one by one, and altering the model as needed (see below).
My goal: fit models with functions that fit initial models using a specified set of predictors and response variables and families and datasets. Put those models into one place, like a dataframe, where I can access the model object itself as well as characteristics like the model's AIC or formula. Provide diagnostic plots with DHARMa. If necessary, modify the model and then update the dataframe to reflect that model's changes.
How can I achieve this ideal workflow? I've tried to make a reprex, below. Thanks for any advice!
library(tidyverse)
library(DHARMa)
library(magrittr)
library(glmmTMB)
cars <- datasets::mtcars
# 3 models, each with different response variables. Some (e.g. glm2)
# have non-Gaussian families; some have filtered datasets
lm1 <- glmmTMB(mpg ~ hp, data = cars)
glm2 <- glmmTMB(cyl ~ hp,
data = cars,
family = "poisson")
lm3 <- glmmTMB(drat ~ hp,
data = cars %>%
dplyr::filter(carb != 1))
# next, look at diagnostic plots
plot(DHARMa::simulateResiduals(lm1))
plot(DHARMa::simulateResiduals(glm2))
plot(DHARMa::simulateResiduals(lm3))
# transform one model's response variable to satisfy assumptions
glm2 <- glmmTMB(cyl^2 ~ hp,
data = cars,
family = "poisson")
# check diagnostics again
plot(DHARMa::simulateResiduals(lm2))
# organize models into one place. A dataframe? A list? Somewhere where
# I can access the model itself and the model formula so I can extract
# those things to be used as arguments in other functions
?
英文:
oftentimes I need to fit several linear models (dozens) in R. These models may have the same predictor but varying response variables, data distribution families, and more.
What I've been doing: fitting models one by one, looking at diagnostic plots one by one, and altering the model as needed (see below).
My goal: fit models with functions that fit initial models using a specified set of predictors and response variables and families and datasets. Put those models into one place, like a dataframe, where I can access the model object itself as well as characteristics like the model's AIC or formula. Provide diagnostic plots with DHARMa. If necessary, modify the model and then update the dataframe to reflect that model's changes.
How can I achieve this ideal workflow? I've tried to make a reprex, below. Thanks for any advice!
library(tidyverse)
library(DHARMa)
library(magrittr)
library(glmmTMB)
cars <- datasets::mtcars
# 3 models, each with different response variables. Some (e.g. glm2)
# have non-Gaussian families; some have filtered datasets
lm1 <- glmmTMB(mpg ~ hp, data = cars)
glm2 <- glmmTMB(cyl ~ hp,
data = cars,
family = "poisson")
lm3 <- glmmTMB(drat ~ hp,
data = cars %>%
dplyr::filter(carb != 1))
# next, look at diagnostic plots
plot(DHARMa::simulateResiduals(lm1))
plot(DHARMa::simulateResiduals(glm2))
plot(DHARMa::simulateResiduals(lm3))
# transform one model's response variable to satisfy assumptions
glm2 <- glmmTMB(cyl^2 ~ hp,
data = cars,
family = "poisson")
# check diagnostics again
plot(DHARMa::simulateResiduals(lm2))
# organize models into one place. A dataframe? A list? Somewhere where
# I can access the model itself and the model formula so I can extract
# those things to be used as arguments in other functions
?
答案1
得分: 1
你可以将"everything"放入数据框中,只要接收列是列表列(意味着该列的单元格内容是列表)。这是一种方便的方法,可以对数据进行切片和切块,并将它们和/或模型公式、配方、图表等保留在表格结构中。只需记住在必要时将项目封装到列表中。
对于你的示例,你可以:
- 创建所有所需模型函数、依赖变量、预测变量等组合的数据框:
the_grid <-
expand.grid(dependent = c('mpg', 'cyl', 'drat'),
type = 'glmmTMB',
predictors = c('hp'),
predicate = c('TRUE', 'TRUE', 'carb != 1'),
stringsAsFactors = FALSE
)
- 使用此数据框添加包含模型和每个参数组合的绘图的列表列:
library(DHARMa)
library(glmmTMB)
library(dplyr)
library(ggeffects) ## to plot model effects
the_models <-
the_grid %>%
rowwise() %>%
mutate(
m = do.call(type,
args = list(formula = reformulate(response = dependent,
termlabels = predictors,
),
data = mtcars
)
) |> list(),
p = list(ggpredict(m) |> plot())
)
- 示例:第一行的模型摘要:
the_models$m[[1]] |> summary()
- 示例:第5行的效应图:
the_models$p[[5]]
英文:
You can put "everything" into a dataframe, as long as the receiving column is a list-column (meaning cell contents of this columns are lists). That's a convenient way to slice & dice your data and keep them and/or model formulae, recipes, plots, you name it ... in a tabular structure. Just remember to encapsulate items in a list, where necessary.
In case of your example, you could:
- create a dataframe of all desired combinations of model function, dependent, predictors etc.:
the_grid <-
expand.grid(dependent = c('mpg', 'cyl', 'drat'),
type = 'glmmTMB',
predictors = c('hp'),
predicate = c('TRUE', 'TRUE', 'carb != 1'),
stringsAsFactors = FALSE
)
- use this dataframe to add list-columns containing, e.g. a model and a plot per parameter combination:
library(DHARMa)
library(glmmTMB)
library(dplyr)
library(ggeffects) ## to plot model effects
the_models <-
the_grid |>
rowwise() |> ## !important
mutate(
m = do.call(type,
args = list(formula = reformulate(response = dependent,
termlabels = predictors,
),
data = mtcars
)
) |> list(), ## don't forget to wrap the result in a list
## add a ggeffect plot object for fun:
p = list(ggpredict(m) |> plot()) ## again, use a list
)
## > the_models
## # A tibble: 9 x 6
## # Rowwise:
## dependent type predictors predicate m p
## <fct> <fct> <fct> <fct> <list> <list>
## 1 mpg glmmTMB hp TRUE <glmmTMB> <named list [1]>
## 2 cyl glmmTMB hp TRUE <glmmTMB> <named list [1]>
## 3 drat glmmTMB hp TRUE <glmmTMB> <named list [1]>
## 4 mpg glmmTMB hp TRUE <glmmTMB> <named list [1]>
## 5 cyl glmmTMB hp TRUE <glmmTMB> <named list [1]>
## 6 drat glmmTMB hp TRUE <glmmTMB> <named list [1]>
## 7 mpg glmmTMB hp carb != 1 <glmmTMB> <named list [1]>
## 8 cyl glmmTMB hp carb != 1 <glmmTMB> <named list [1]>
- example: model summary for row 1:
## > the_models$m[[1]] |> summary()
## Family: gaussian ( identity )
## Formula: mpg ~ hp
## Data: filter(mtcars, TRUE)
##
## AIC BIC logLik deviance df.resid
## 181.2 185.6 -87.6 175.2 29
##
##
## Dispersion estimate for gaussian family (sigma^2): 14
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 30.098862 1.582037 19.025 < 2e-16 ***
## hp -0.068228 0.009798 -6.964 3.32e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
- example: effect plot for row 5:
the_models$p[[5]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论