rstatix

 library(rstatix) 

library(ggpubr)  # For easy data-visualizatio

library(gtsummary)

library(tidyverse)


iris %>% 

  group_by(Species) %>% 

  get_summary_stats(Sepal.Length, Sepal.Width, type = "common")


iris %>% get_summary_stats(type = "common")


iris %>%

  group_by(Species) %>% 

  get_summary_stats(Sepal.Length, show=c('n'))

  

df <- ToothGrowth

df$dose <- as.factor(df$dose)


# 比较2个独立组别

df %>% 

  t_test(len ~ supp, paired = FALSE)  -> stat.test


# 分组数据:在按照「dose」分组后比较 supp 水平:

stat.test <- df %>%

  group_by(dose) %>%

  t_test(len ~ supp) %>%

  adjust_pvalue() %>%

  add_significance("p.adj")


# 比较配对样本

stat.test <- df %>% 

  t_test(len ~ supp, paired = TRUE)


stat.test <- df %>% 


# 成对比较:如果分组变量包含多于2个分类,会自动执行成对比较

pairwise.test <- df %>% t_test(len ~ dose)


# 基于参考组的成对比较

stat.test <- df %>% t_test(len ~ dose, ref.group = "0.5")


# 基于总体水平的成对比较

# T-test

stat.test <- df %>% t_test(len ~ dose, ref.group = "all")


# One-way ANOVA test

df %>% anova_test(len ~ dose)


# Two-way ANOVA test

df %>% anova_test(len ~ supp*dose)


# Two-way repeated measures ANOVA

df$id <- rep(1:10, 6) # Add individuals id

df %>% anova_test(len ~ supp*dose + Error(id/(supp*dose)))


# Use model as arguments

.my.model <- lm(yield ~ block + N*P*K, npk)

anova_test(.my.model)


mydata <- mtcars %>% 

  select(mpg, disp, hp, drat, wt, qsec)


mydata %>% cor_test(wt, mpg, method = "pearson")


#先选择部分变量进行展示

trial2 <- trial %>% select(age, grade, death, trt)

# 绘制

table1 <- tbl_summary(trial2)


table2 <- 

  tbl_summary(

    trial2,

    by = trt, # 分组

    statistic = list(all_continuous() ~ "{mean} ({sd})"),  

    missing = "no", # 

  ) %>%

  add_n() %>% # 添加非NA观测值个数

  add_p() %>% # 添加P值

  add_overall() %>%

  modify_header(label = "**Variable**") %>% # 标签列header

  bold_labels()  #label 粗体


table3 <- 

  tbl_summary(

    trial2,

    by = trt, # 分组

    statistic = list(all_continuous() ~ "{mean} ({sd})")

  ) %>%

  add_p(test = list(all_continuous() ~ "t.test"))  # 添加P值


#构建逻辑回归

mod1 <- glm(response ~ trt + age + grade, trial, family = binomial)

t1 <- tbl_regression(mod1, exponentiate = TRUE)

t1


library(survival)

t2 <-

  coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%

  tbl_regression(exponentiate = TRUE)

t2


# merge tables 

tbl_merge_ex1 <-

  tbl_merge(

    tbls = list(t1, t2),    tab_spanner = c("**Tumor Response**", "**Time to Death**") #防止混淆,定义名字

  )

评论

此博客中的热门博文

V2ray websocket(ws)+tls+nginx分流

Rstudio 使用代理