Adding new column to a gtsummary tbl_regression output, calculated using existing columns

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

Adding new column to a gtsummary tbl_regression output, calculated using existing columns

问题

I would like to add a column to my gtsummary::tbl_regression output object called '发病率' which is calculated using the 'N' and '事件N' columns already produced using the gtsummary::add_n and gtsummary::add_nevent functions.

I have attempted this in a few ways including trying to use the gtsummary::add_stat() function, but this does not seem to work with tbl_regression objects. Having tried to calculate the column manually with dplyr::mutate() and the tbl_regression object's '_data' element, I've been unable to get this to appear in the final output. add_stat example, which doesn't work:

add_nevent_ex <-
  glm(response ~ trt, trial, family = binomial) %>%
  tbl_regression() %>%
  add_n() %>%
  add_nevent() %>%
  add_stat(fns = list(发病率 = 事件N / N))
英文:

I would like to add a column to my gtsummary::tbl_regression output object called 'Incidence Rate' which is calculated using the 'N' and 'Event N' columns already produced using the gtsummary::add_n and gtsummary::add_nevent functions.

I have attempted this in a few ways including trying to use the gtsummary::add_stat() function, but this does not seem to work with tbl_regression objects. Having tried to calculate the column manually with dplyr::mutate() and the tbl_regression object's '_data' element, I've been unable to get this to appear in the final output. add_stat example, which doesn't work:

add_nevent_ex &lt;-
  glm(response ~ trt, trial, family = binomial) %&gt;%
  tbl_regression() %&gt;%
  add_n() %&gt;%
  add_nevent() %&gt;%
  add_stat(fns = list(IncidenceRate = Event N / N))

答案1

得分: 1

我最终通过以下方式完成了这个任务:

add_nevent_ex <-
  glm(response ~ trt, trial, family = binomial) %>%
  tbl_regression() %>%
  add_n(location = "level") %>%
  add_nevent(location = "level") %>%
  modify_table_body(
      ~ .x %>%
        dplyr::mutate(incidence_rate = (n_event / n_obs)*1000)) 
    %>%
  # assigning header labels
  modify_header(incidence_rate = "**每1,000人的比率**")

这将应用于模型的“水平”而不是“标签”。如果您希望修改为应用于“标签”,只需将 dplyr::mutate(incidence_rate = (n_event / n_obs)*1000)) 替换为 dplyr::mutate(incidence_rate = (N_event / N_obs)*1000))

非常感谢该软件包的作者Daniel D. Sjoberg提供的帮助。

英文:

I managed to accomplish this in the end by doing the following:

add_nevent_ex &lt;-
  glm(response ~ trt, trial, family = binomial) %&gt;%
  tbl_regression() %&gt;%
  add_n(location = &quot;level&quot;) %&gt;%
  add_nevent(location = &quot;level&quot;) %&gt;%
  modify_table_body(
      ~ .x %&gt;%        
        dplyr::mutate(incidence_rate = (n_event / n_obs)*1000)) 
    %&gt;%
  # assigning header labels
  modify_header(incidence_rate = &quot;**Rate Per 1,000**&quot;)

This applies the rate on the 'levels' of the model as opposed to the 'labels'. If you wished to modify this to apply to the 'labels', just replace dplyr::mutate(incidence_rate = (n_event / n_obs)*1000)) with dplyr::mutate(incidence_rate = (N_event / N_obs)*1000)).

Many thanks to the package's author, Daniel D. Sjoberg, for the help.

huangapple
  • 本文由 发表于 2023年4月13日 22:42:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006781.html
匿名

发表评论

匿名网友

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

确定