【问题标题】:What function should I use in tbl_regression to tidy (argument tidy_fun) lm output performed on grouped data?我应该在 tbl_regression 中使用什么函数来整理(参数 tidy_fun)对分组数据执行的 lm 输出?
【发布时间】:2021-11-08 16:41:08
【问题描述】:

我刚刚发现了 gtsummary 包,我想哇,我想为我的线性回归获得这种类型的快速表,但我遇到了一些麻烦,无法让它为我工作。 我有一个包含生长和荧光数据的数据表。数据按实验条件荧光类型分组。我对此数据进行了线性回归,请参见下面的示例

#make a dummy data table 

d <- data.frame(
      condition = rep(c('B', 'U'), 20),
      growth = rep(1:20, 2),
type=rep(c(F1,F2,F3,F4),5)
      response= rnorm(20)
    )
#run lm on grouped data by condition and fluorescence type 
linear_models <- d%>%
  group_by(type,condition)%>%
  do(model = lm(growth ~fluo, data = .))
#make table with tbl_regression 
tbl_regression(linear_models)

这里我收到一条错误消息,告诉我 tidy 函数未能整理模型

! `broom::tidy()` failed to tidy the model.
x Calling var(x) on a factor x is defunct.
Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm)
! `tidy_parameters()` also failed.
! `broom::tidy()` failed to tidy the model.
x Calling var(x) on a factor x is defunct.
Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm)
! `tidy_parameters()` also failed.
x There was an error calling `tidy_fun()`. Most likely, this is because the
function supplied in `tidy_fun=` was misspelled, does not exist, is not
compatible with your object, or was missing necessary arguments (e.g. `conf.level=` or `conf.int=`). See error message below.
Error: Error in eval(expr, envir = list(`?` = function(...) stop()), enclos = envir): You need to install 'parameters' to use 'tidy_parameters'.
In addition: There were 16 warnings (use warnings() to see them)

我尝试直接使用 broom::tidy 获得一张桌子,它可以工作,但它不是我想要获得的桌子。

linear_models <- d%>%
  group_by(type,condition)%>%
  do(broom::tidy(lm(growth ~fluo, .)))

谁能提示我在 tbl_regression 函数中遗漏了什么以及如何在我的模型上工作?

对不起,太长了!谢谢

【问题讨论】:

  • 输入示例中没有fluo
  • 对不起,我在创建虚拟数据时弄错了列名,fluo 应该是响应

标签: r gtsummary


【解决方案1】:

考虑使用map 循环list 中的“模型”对象并应用`tbl_regression

library(purrr)
library(dplyr)
library(gtsummary)
out <- d%>%
  group_by(type,condition)%>%
  do(model = lm(growth ~response, data = .)) %>%
  ungroup %>%
  mutate(model = map(model, tbl_regression))

-检查

out$model[[2]]


或者另一种选择是直接在summarise 内的lm 上应用tbl_regression 并作为list 对象返回

out <- d %>%
  group_by(type, condition) %>%
  summarise(model = list(tbl_regression(lm(growth ~ response))), .groups = 'drop')

-输出

out
# A tibble: 4 × 3
  type  condition model     
  <chr> <chr>     <list>    
1 F1    B         <tbl_rgrs>
2 F2    U         <tbl_rgrs>
3 F3    B         <tbl_rgrs>
4 F4    U         <tbl_rgrs>

数据

d <- structure(list(condition = c("B", "U", "B", "U", "B", "U", "B", 
"U", "B", "U", "B", "U", "B", "U", "B", "U", "B", "U", "B", "U", 
"B", "U", "B", "U", "B", "U", "B", "U", "B", "U", "B", "U", "B", 
"U", "B", "U", "B", "U", "B", "U"), growth = c(1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), type = c("F1", "F2", 
"F3", "F4", "F1", "F2", "F3", "F4", "F1", "F2", "F3", "F4", "F1", 
"F2", "F3", "F4", "F1", "F2", "F3", "F4", "F1", "F2", "F3", "F4", 
"F1", "F2", "F3", "F4", "F1", "F2", "F3", "F4", "F1", "F2", "F3", 
"F4", "F1", "F2", "F3", "F4"), response = c(0.513847456201564, 
-0.820367660407753, 0.440731787146136, 0.203848762708769, -0.529724800374613, 
-1.43570130262853, 0.0732862795002955, -0.740805987248482, -0.603335474342407, 
-0.41537077313497, 0.457044030941835, -2.16121669238704, 0.423012730497808, 
-0.302949652630124, -0.0691136349178037, 0.0709372889906787, 
1.20684795477784, 0.182525811801302, 0.195440636740125, -0.348940594898786, 
0.513847456201564, -0.820367660407753, 0.440731787146136, 0.203848762708769, 
-0.529724800374613, -1.43570130262853, 0.0732862795002955, -0.740805987248482, 
-0.603335474342407, -0.41537077313497, 0.457044030941835, -2.16121669238704, 
0.423012730497808, -0.302949652630124, -0.0691136349178037, 0.0709372889906787, 
1.20684795477784, 0.182525811801302, 0.195440636740125, -0.348940594898786
)), class = "data.frame", row.names = c(NA, -40L))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2019-01-14
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    相关资源
    最近更新 更多