将 kendall tau 添加到 gtsummary

问题描述 投票:0回答:0

Iv 使用 tbl_summary 函数创建了一个带有 gysummary 的表。

按三分位数分组:“低三分位数”、“中三分位数”、“高三分位数”

使用 add_p,Kruskal Wallis 检验比较这 3 组的不同特征。

我希望从 Kendall tau 相关性中添加一列 p 值(使用 1、2、3 作为数字而不是“低三分位数”、“中三分位数”、“高三分位数”。

这可能吗?

我尝试计算相关性并将 p 值保存为向量,但无法将其添加为列。

编辑: 这是代表:

library(reprex)
#> Warning: package 'reprex' was built under R version 4.2.3

library(quantreg)
#> Warning: package 'quantreg' was built under R version 4.2.3
#> Loading required package: SparseM
#> 
#> Attaching package: 'SparseM'
#> The following object is masked from 'package:base':
#> 
#>     backsolve
library(readxl)
library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.2.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(SciViews)
#> Warning: package 'SciViews' was built under R version 4.2.1
library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 4.2.3
library(labelled)
#> Warning: package 'labelled' was built under R version 4.2.3

set.seed(123)

# Create sample data
n <- 20

data <- data.frame(
  VAT_Quantile_3 = factor(sample(c("Low tertile", "Medium tertile", "High tertile"), n, replace = TRUE)),
  Gender = factor(sample(c("Male", "Female"), n, replace = TRUE)),
  Age = c(50, 49, 48, 51, 52, 50, 49, 48, 51, 52, 50, 49, 48, 51, 52,50, 49, 48, 51, 52),
  WC_0 = c(100, 99, 98, 101, 102, 100, 99, 98, 101, 102, 100, 99, 98, 101, 102, 100, 99, 98, 101, 102),
  HOMAIR_0 = c(1, 2, 3, 4, 5,1, 2, 3, 4, 5,1, 2, 3, 4, 5,1, 2, 3, 4, 5)
)

head(data)
#>   VAT_Quantile_3 Gender Age WC_0 HOMAIR_0
#> 1   High tertile   Male  50  100        1
#> 2   High tertile   Male  49   99        2
#> 3   High tertile   Male  48   98        3
#> 4 Medium tertile Female  51  101        4
#> 5   High tertile Female  52  102        5
#> 6 Medium tertile   Male  50  100        1
summary(data)
#>         VAT_Quantile_3    Gender        Age          WC_0        HOMAIR_0
#>  High tertile  :8      Female: 9   Min.   :48   Min.   : 98   Min.   :1  
#>  Low tertile   :5      Male  :11   1st Qu.:49   1st Qu.: 99   1st Qu.:2  
#>  Medium tertile:7                  Median :50   Median :100   Median :3  
#>                                    Mean   :50   Mean   :100   Mean   :3  
#>                                    3rd Qu.:51   3rd Qu.:101   3rd Qu.:4  
#>                                    Max.   :52   Max.   :102   Max.   :5

tab_1<- data %>%
  select(VAT_Quantile_3, Gender , Age , WC_0, HOMAIR_0) %>%
  set_variable_labels(WC_0 = "WC", HOMAIR_0 = "HOMA-IR") %>%
  tbl_summary(by = VAT_Quantile_3,
              type = list(all_continuous() ~ 'continuous2'),
              missing = "no",
              digits = list(all_continuous() ~ c(1, 1),
                            all_categorical() ~ c(0,1)))  %>%
  add_overall() %>%
  add_p(,pvalue_fun = ~style_pvalue(., digits = 3)) %>%
  add_q() %>%
  add_n() %>%
  modify_caption("**Table 1. Baseline characteristics across VAT area sex-specific tertials**") %>% 
  bold_p(t = 0.05, q = TRUE) %>%
  bold_labels() %>% 
  italicize_levels() %>%  
  modify_footnote(all_stat_cols() ~ "Values are presented as median (p25, p75) for continuous variables, and as number (%) for categorical variables.")
#> add_q: Adjusting p-values with
#> `stats::p.adjust(x$table_body$p.value, method = "fdr")`



#code for Kendell's tau:
cor.test(data$Age, as.numeric(data$VAT_Quantile_3), method = "kendall")
#> Warning in cor.test.default(data$Age, as.numeric(data$VAT_Quantile_3), method =
#> "kendall"): Cannot compute exact p-value with ties
#> 
#>  Kendall's rank correlation tau
#> 
#> data:  data$Age and as.numeric(data$VAT_Quantile_3)
#> z = -0.21336, p-value = 0.831
#> alternative hypothesis: true tau is not equal to 0
#> sample estimates:
#>         tau 
#> -0.04144342
cor.test(data$WC_0, as.numeric(data$VAT_Quantile_3), method = "kendall")
#> Warning in cor.test.default(data$WC_0, as.numeric(data$VAT_Quantile_3), :
#> Cannot compute exact p-value with ties
#> 
#>  Kendall's rank correlation tau
#> 
#> data:  data$WC_0 and as.numeric(data$VAT_Quantile_3)
#> z = -0.21336, p-value = 0.831
#> alternative hypothesis: true tau is not equal to 0
#> sample estimates:
#>         tau 
#> -0.04144342
cor.test(data$HOMAIR_0, as.numeric(data$VAT_Quantile_3), method = "kendall")
#> Warning in cor.test.default(data$HOMAIR_0, as.numeric(data$VAT_Quantile_3), :
#> Cannot compute exact p-value with ties
#> 
#>  Kendall's rank correlation tau
#> 
#> data:  data$HOMAIR_0 and as.numeric(data$VAT_Quantile_3)
#> z = -0.85343, p-value = 0.3934
#> alternative hypothesis: true tau is not equal to 0
#> sample estimates:
#>        tau 
#> -0.1657737



library(flextable)
#> Warning: package 'flextable' was built under R version 4.2.3
#> 
#> Attaching package: 'flextable'
#> The following objects are masked from 'package:gtsummary':
#> 
#>     as_flextable, continuous_summary
tab_1 %>%
  as_flex_table() %>% 
  save_as_image(path = "C:/דוקטורט בכונן/מאמר עם הילה/Area proportion paper/AREA.png")
#> [1] "C:/דוקטורט בכונן/מאמר עם הילה/Area proportion paper/AREA.png"

创建于 2023-04-11 与 reprex v2.0.2

correlation gtsummary
© www.soinside.com 2019 - 2024. All rights reserved.