Search code examples
gtsummary

Error: attempt to apply non-function in tbl_uvregression from gtsummary


when I attempt to use tbl_uvregression from the gtsummary package I keep getting an error message about attempting to apply a non-function.

Here is an example:

library(gtsummary)
library(tidyverse)
packageVersion("gtsummary")
# package 1.7.0

data("mtcars")

cars <- mtcars %>% 
    dplyr::select(mpg, disp, cyl, hp) %>% 
tbl_uvregression(
    method = "lm",
    y = hp,
    exponentiate = TRUE,
)

# Performing individual univariate linear regression works just fine
lm(hp ~ mpg, data = cars)
lm(hp ~ disp, data = cars)
lm(hp ~ cyl, data = cars)

I get the following error message: There was an error constructing model "lm"(formula = hp ~ mpg, data = .) See error below. Error in mutate(): ℹ In argument: model = map(...). Caused by error in value[[3L]](): ! Error in eval(.): attempt to apply non-function Run rlang::last_error() to see where the error occurred.

Can someone tell me what I am doing wrong?

PS: I really enjoy using gtsummary, many thanks for creating this excellent package!


Solution

  • I made two small changes.

    1. Unquoted method = lm.
    2. Change exponentiate = FALSE because TRUE is not valid for lm() models.
    library(gtsummary)
    packageVersion("gtsummary")
    #> [1] '1.7.0'
    
    mtcars %>% 
      select(mpg, disp, cyl, hp) %>% 
      tbl_uvregression(
        method = lm,
        y = hp,
        exponentiate = FALSE, # TRUE is not valid for lm() models
      ) %>%
      as_kable() # convert to kable to display on stackoverflow
    
    Characteristic N Beta 95% CI p-value
    mpg 32 -8.8 -12, -6.2 <0.001
    disp 32 0.44 0.31, 0.56 <0.001
    cyl 32 32 24, 40 <0.001

    Created on 2023-02-26 with reprex v2.0.2