繁体   English   中英

如何在 gtsummary 中进行重复测量 Anova 和 Friedman 测试?

[英]How to do repeated measures Anova and Friedman test in gtsummary?

我重复了临床试验期间第 0、3 和 7 天测量的少数患者空腹血糖的测量数据。 我想做friedman.test (并重复测量另一个数据的方差分析)。 是否有可能在gtsummary中进行 Friedman 测试和重复测量 Anova?

这是我的代码

library(gtsummary)
library(dplyr)
smpl %>% select(c(Participant.ID, value, Day)) %>% 
             tbl_summary(by = Day, include = -Participant.ID) %>% add_p()

默认情况下, gtsummary对我数据中的三个组(第 0、3 和 7 天)进行Kruskal-Wallis rank sum test 因为这是一个重复的措施,所以我想对这些群体进行friedman.test 所以我尝试了以下代码

my_friedman <- function(data, variable, by, random, ...) {
     data <- data[c(variable, by)] %>% dplyr::filter(complete.cases(.))
     friedman.test(variable ~ data[[by]] | random, data = data)
     
}

smpl %>% select(c(Participant.ID, value, Day)) %>% 
          tbl_summary(by = Day) %>% 
           add_p(test = value ~ "my_friedman", random = Participant.ID)

但这给出了一个错误..

在此处输入图像描述

这是我的示例数据dputsmpl

structure(list(Participant.ID = c(1002, 1007, 1010, 1017, 1018, 
1022, 1044, 1050, 1051, 1052, 1002, 1007, 1010, 1017, 1018, 1022, 
1044, 1050, 1051, 1052, 1002, 1007, 1010, 1017, 1018, 1022, 1044, 
1050, 1051, 1052), Randomization = c("Pioglitazone", "Pioglitazone", 
"Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
"Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
"Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
"Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
"Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
"Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
"Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone"
), Day = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L), .Label = c("0", "3", "7"), class = "factor"), 
    value = c(31.92, 53.25, 34.69, 33.32, 25.94, 34.69, 22.65, 
    33.32, 24.33, 25.94, 31.92, 46.54, 44.74, 145.12, 37.34, 
    39.89, 22.65, 27.5, 25.94, 25.94, 37.34, 53.25, 29.02, 37.34, 
    25.94, 34.69, 22.65, 27.5, 29.02, 31.92)), row.names = c(NA, 
-30L), class = c("tbl_df", "tbl", "data.frame"))

有人可以帮忙吗?

这是对friedman.test的直接调用

> friedman.test(value ~ Day | Participant.ID, data = aa)

    Friedman rank sum test

data:  value and Day and Participant.ID
Friedman chi-squared = 1.2667, df = 2, p-value = 0.5308

你的代码很接近。 我做了一些小的调整。 见下文!

library(gtsummary)
#> #BlackLivesMatter

smpl <-
  structure(list(Participant.ID = c(1002, 1007, 1010, 1017, 1018, 
                                    1022, 1044, 1050, 1051, 1052, 1002, 1007, 1010, 1017, 1018, 1022, 
                                    1044, 1050, 1051, 1052, 1002, 1007, 1010, 1017, 1018, 1022, 1044, 
                                    1050, 1051, 1052), Randomization = c("Pioglitazone", "Pioglitazone", 
                                                                         "Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
                                                                         "Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
                                                                         "Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
                                                                         "Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
                                                                         "Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
                                                                         "Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone", 
                                                                         "Pioglitazone", "Pioglitazone", "Pioglitazone", "Pioglitazone"
                                    ), Day = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                         2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                                         3L, 3L, 3L, 3L), .Label = c("0", "3", "7"), class = "factor"), 
                 value = c(31.92, 53.25, 34.69, 33.32, 25.94, 34.69, 22.65, 
                           33.32, 24.33, 25.94, 31.92, 46.54, 44.74, 145.12, 37.34, 
                           39.89, 22.65, 27.5, 25.94, 25.94, 37.34, 53.25, 29.02, 37.34, 
                           25.94, 34.69, 22.65, 27.5, 29.02, 31.92)), row.names = c(NA, 
                                                                                    -30L), class = c("tbl_df", "tbl", "data.frame"))

my_friedman <- function(data, variable, by, group, ...) {
  # construct the formula
  formula <- stringr::str_glue("{variable} ~ {by} | {group}") |> as.formula()
  
  # perform Friedman test
  friedman.test(
    formula = formula, 
    data = data
  ) |> 
    broom::tidy()
}

my_friedman(data = smpl, variable = "value", by = "Day", group = "Participant.ID")
#> # A tibble: 1 × 4
#>   statistic p.value parameter method                
#>       <dbl>   <dbl>     <dbl> <chr>                 
#> 1      1.27   0.531         2 Friedman rank sum test

tbl <-
  smpl %>% 
  select(Participant.ID, value, Day) %>% 
  tbl_summary(
    by = Day,
    include = -Participant.ID
  ) %>% 
  add_p(test = value ~ "my_friedman", group = Participant.ID)

在此处输入图像描述 创建于 2023-01-08,使用reprex v2.0.2

①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM