最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - New error using multcomp::glht(), missRanger(), and mice::pool() - Stack Overflow

programmeradmin12浏览0评论

I'm revisiting old code that involves multiple imputation using missRanger::missRanger(), multcomp::glht() (to derive the linear combination of effects), mice::pool(), and broom.mixed::tidy(). While it previously worked, it now fails when I try to create a tidy table or otherwise extract results. I've reproduced the error using mtcars below.

# 0. PACKAGES

library(tidyverse)
library(mice)
library(broom.mixed)
library(missRanger)
# 1. DATA
# set 20% of the mtcars dataset missing
mtcars_miss <-
  generateNA(mtcars, 
             p = 0.2, 
             seed = 2024)

# generate 5 imputations
mtcars_i <- 
  replicate(9, 
            missRanger(mtcars_miss, 
                       num.tree = 500, 
                       pmm.k = 3, 
                       verbose = 0, 
                       seed = 2024), 
            simplify = FALSE)
# 2. ANALYSIS WITHOUT LINEAR COMBINATIONS
mtcars_mod <- 
  lapply(mtcars_i, 
         function(x)
           glm(mpg ~ hp * wt + am, 
                              data = x))

pool(mtcars_mod)

pool(mtcars_mod) |> 
  tidy(conf.int = TRUE)
# 3. ANALYSIS WITH LINEAR COMBINATIONS
mtcars_lin <- 
  lapply(mtcars_i, 
         function(x)
           multcomp::glht(glm(mpg ~ hp * wt + am, 
                              data = x), 
                          linfct = c("hp = 0", 
                                     "`hp:wt` = 0", 
                                     "hp + `hp:wt` = 0")) |> 
           summary())

pool(mtcars_lin)

pool(mtcars_lin) |> 
  tidy(conf.int = TRUE)

This last chunk generates the following error message. As I note above, this is a new error that did not appear when I ran the code in October 2024. I don't know what the cause is because the output of the pool() argument creates mipo for both the standard model and the one with the linear combination of effects.

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 0, 3

Any ideas as to which package is causing the issue? Or how I can work around this? It is occurring both with glm() and glmmTMB() model functions.

I'm revisiting old code that involves multiple imputation using missRanger::missRanger(), multcomp::glht() (to derive the linear combination of effects), mice::pool(), and broom.mixed::tidy(). While it previously worked, it now fails when I try to create a tidy table or otherwise extract results. I've reproduced the error using mtcars below.

# 0. PACKAGES

library(tidyverse)
library(mice)
library(broom.mixed)
library(missRanger)
# 1. DATA
# set 20% of the mtcars dataset missing
mtcars_miss <-
  generateNA(mtcars, 
             p = 0.2, 
             seed = 2024)

# generate 5 imputations
mtcars_i <- 
  replicate(9, 
            missRanger(mtcars_miss, 
                       num.tree = 500, 
                       pmm.k = 3, 
                       verbose = 0, 
                       seed = 2024), 
            simplify = FALSE)
# 2. ANALYSIS WITHOUT LINEAR COMBINATIONS
mtcars_mod <- 
  lapply(mtcars_i, 
         function(x)
           glm(mpg ~ hp * wt + am, 
                              data = x))

pool(mtcars_mod)

pool(mtcars_mod) |> 
  tidy(conf.int = TRUE)
# 3. ANALYSIS WITH LINEAR COMBINATIONS
mtcars_lin <- 
  lapply(mtcars_i, 
         function(x)
           multcomp::glht(glm(mpg ~ hp * wt + am, 
                              data = x), 
                          linfct = c("hp = 0", 
                                     "`hp:wt` = 0", 
                                     "hp + `hp:wt` = 0")) |> 
           summary())

pool(mtcars_lin)

pool(mtcars_lin) |> 
  tidy(conf.int = TRUE)

This last chunk generates the following error message. As I note above, this is a new error that did not appear when I ran the code in October 2024. I don't know what the cause is because the output of the pool() argument creates mipo for both the standard model and the one with the linear combination of effects.

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 0, 3

Any ideas as to which package is causing the issue? Or how I can work around this? It is occurring both with glm() and glmmTMB() model functions.

Share Improve this question edited Feb 15 at 0:31 pppery 3,81425 gold badges37 silver badges50 bronze badges asked Jan 14 at 2:55 Tyler LaneTyler Lane 891 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Partial answer to my own question. As suspect, it was a version control issue. I posted my frustration to Bluesky and someone provided a workaround, which is the Groundhog package. Code below

# 0. PACKAGES
knitr::opts_chunk$set(echo = TRUE)

# load ground first
library(groundhog)

# load other packages
groundhog.library(
  c("mice", "broom.mixed", 
    "multcomp", "missRanger"), 
  "2024-10-15")

library(tidyverse)
# 1. SETUP DATA
# set 20% of the mtcars dataset missing
mtcars_miss <-
  generateNA(mtcars, 
             p = 0.2, 
             seed = 2024)

# generate 5 imputations
mtcars_i <- 
  replicate(9, 
            missRanger(mtcars_miss, 
                       num.tree = 500, 
                       pmm.k = 3, 
                       verbose = 0, 
                       seed = 2024), 
            simplify = FALSE)
# 2. ANALYSIS WITHOUT LINEAR COMBINATIONS
mtcars_mod <- 
  lapply(mtcars_i, 
         function(x)
           glm(mpg ~ hp * wt + am, 
                              data = x))

pool(mtcars_mod)

pool(mtcars_mod) |> 
  tidy()
# 3. ANALYSIS WITHOUT LINEAR COMBINATIONS
mtcars_lin <- 
  lapply(mtcars_i, 
         function(x)
           multcomp::glht(glm(mpg ~ hp * wt + am, 
                              data = x), 
                          linfct = c("hp = 0", 
                                     "`hp:wt` = 0", 
                                     "hp + `hp:wt` = 0")))

pool(mtcars_lin)

pool(mtcars_lin) |> 
  tidy()

The model results aren't great, but the error is gone and all of the packages work well together again!

发布评论

评论列表(0)

  1. 暂无评论