在使用
的摘要/比较表中lme_stat_function <- function(data, variable, by, group, ...){
nlme::lme(variable ~ by, random = ~1|group, na.action = na.omit, data = data) %>%
broom.mixed::tidy()
}
lme_stat_render <- list(
all_continuous() ~ "lme_stat_function"
)
study_dat %>%
select(ID, Study_Group, Variable1) %>%
tbl_summary(
by = Study_Group,
type = list(
where(is.numeric) ~ "continuous2"
)
) %>%
bold_labels() %>%
italicize_levels() %>%
add_p(
test = lme_stat_render
)
几天来,我一直在为这个看似简单的问题搏斗,除了有效地读取For variable Variable1 (Study_Group) and "p.value" statistic: variable lengths differ (found for 'group')
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 84, 84,
85, 86, 87, 87, 88, 88, 89, 89, 90, 90, 91, 92, 93, 93, 94, 95,
95, 96, 97, 97), Variable1 = c(6, 29, 16, 15, 19, 15,
4, 8, 26, 9, 7, 27, 3, 31, 16, 19, 8, 12, 5, 7, 7, 12, 9, 54,
5, 14, 4, 3, 11, 17, 7), Study_Group = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), levels = c("Group A",
"Group B"), class = "factor")), row.names = c(NA, -31L
), class = c("tbl_df", "tbl", "data.frame"))
i我认识到,这里的重复测量仅针对一组重复,但我不是在此处辩论统计方法。我只是想让这个工作原样。有指针吗?
您在正确的轨道上。看起来这个问题可能是在模型的构建中。在下面的示例中,我使用了reformulat()
,这是从字符串输入中创建公式的绝佳助手。
library(gtsummary)
study_dat <-
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 84, 84,
85, 86, 87, 87, 88, 88, 89, 89, 90, 90, 91, 92, 93, 93, 94, 95,
95, 96, 97, 97),
Variable1 = c(6, 29, 16, 15, 19, 15,
4, 8, 26, 9, 7, 27, 3, 31, 16, 19, 8, 12, 5, 7, 7, 12, 9, 54,
5, 14, 4, 3, 11, 17, 7),
Study_Group = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), levels = c("Group A",
"Group B"), class = "factor")), row.names = c(NA, -31L
),
class = c("tbl_df", "tbl", "data.frame"))
# you can construct a function that returns a single
# row data frame with the p-value in it
# details for writing this function: https://www.danieldsjoberg.com/gtsummary/reference/tests.html#custom-functions
my_lme_test <- function(data, by, variable, group, ...) {
nlme::lme(
data = data,
fixed = reformulate(response = variable, termlabels = by),
random = reformulate(glue::glue("1 | {group}"))
) |>
broom.mixed::tidy() |>
dplyr::filter(startsWith(term, by)) # keep the row associated with treatment
}
# test the function
my_lme_test(study_dat, by = 'Study_Group', variable = "Variable1", group = "ID")
#> # A tibble: 1 × 8
#> effect group term estimate std.error df statistic p.value
#> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 fixed <NA> Study_GroupGroup B -2.48 3.94 21 -0.631 0.535
study_dat |>
select(ID, Study_Group, Variable1) |>
tbl_summary(
by = Study_Group,
include = -ID,
type = where(is.numeric) ~ "continuous2"
) |>
add_p(test = everything() ~ my_lme_test, group = ID) |>
as_kable() # convert to kable to display on SO
集群an =14
P-值 | 值1 | ||
---|---|---|---|