英文:
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 <-
glm(response ~ trt, trial, family = binomial) %>%
tbl_regression() %>%
add_n() %>%
add_nevent() %>%
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 <-
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 = "**Rate Per 1,000**")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论