高效迭代地在R中拟合、诊断、修改和组织线性模型(汇总到一个地方)。

huangapple go评论75阅读模式
英文:

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 &lt;- datasets::mtcars

# 3 models, each with different response variables. Some (e.g. glm2)
# have non-Gaussian families; some have filtered datasets
lm1 &lt;- glmmTMB(mpg ~ hp, data = cars)
glm2 &lt;- glmmTMB(cyl ~ hp, 
                data = cars,
               family = &quot;poisson&quot;)
lm3 &lt;- glmmTMB(drat ~ hp,
               data = cars %&gt;%
                 dplyr::filter(carb != 1))

# next, look at diagnostic plots
plot(DHARMa::simulateResiduals(lm1))
plot(DHARMa::simulateResiduals(glm2))
plot(DHARMa::simulateResiduals(lm3))

# transform one model&#39;s response variable to satisfy assumptions
glm2 &lt;- glmmTMB(cyl^2 ~ hp, 
                data = cars,
                  family = &quot;poisson&quot;)

# 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 &lt;- 
  expand.grid(dependent = c(&#39;mpg&#39;, &#39;cyl&#39;, &#39;drat&#39;),
              type = &#39;glmmTMB&#39;,
              predictors = c(&#39;hp&#39;),
              predicate = c(&#39;TRUE&#39;, &#39;TRUE&#39;, &#39;carb != 1&#39;),
              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 &lt;- 
  the_grid |&gt;
  rowwise() |&gt; ## !important
  mutate(
    m = do.call(type,
                args = list(formula = reformulate(response = dependent,
                                                  termlabels = predictors,
                                                  ),
                            data = mtcars
                            )
                ) |&gt; list(), ## don&#39;t forget to wrap the result in a list
    ## add a ggeffect plot object for fun:
    p = list(ggpredict(m) |&gt; plot()) ## again, use a list
  )
## &gt; the_models
## # A tibble: 9 x 6
## # Rowwise: 
##   dependent type    predictors predicate m         p               
##   &lt;fct&gt;     &lt;fct&gt;   &lt;fct&gt;      &lt;fct&gt;     &lt;list&gt;    &lt;list&gt;          
## 1 mpg       glmmTMB hp         TRUE      &lt;glmmTMB&gt; &lt;named list [1]&gt;
## 2 cyl       glmmTMB hp         TRUE      &lt;glmmTMB&gt; &lt;named list [1]&gt;
## 3 drat      glmmTMB hp         TRUE      &lt;glmmTMB&gt; &lt;named list [1]&gt;
## 4 mpg       glmmTMB hp         TRUE      &lt;glmmTMB&gt; &lt;named list [1]&gt;
## 5 cyl       glmmTMB hp         TRUE      &lt;glmmTMB&gt; &lt;named list [1]&gt;
## 6 drat      glmmTMB hp         TRUE      &lt;glmmTMB&gt; &lt;named list [1]&gt;
## 7 mpg       glmmTMB hp         carb != 1 &lt;glmmTMB&gt; &lt;named list [1]&gt;
## 8 cyl       glmmTMB hp         carb != 1 &lt;glmmTMB&gt; &lt;named list [1]&gt;
  • example: model summary for row 1:
## &gt; the_models$m[[1]] |&gt; 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(&gt;|z|)    
## (Intercept) 30.098862   1.582037  19.025  &lt; 2e-16 ***
## hp          -0.068228   0.009798  -6.964 3.32e-12 ***
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
  • example: effect plot for row 5:
the_models$p[[5]]

高效迭代地在R中拟合、诊断、修改和组织线性模型(汇总到一个地方)。

huangapple
  • 本文由 发表于 2023年6月8日 03:07:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定