Jstable 包进行亚组分析-R语言

jstable 包是一个强大的工具,可以方便地实现多重亚组分析并生成出版标准的结果表格和森林图。

用法:
TableSubgroupMultiGLM(
  formula,  # 模型公式,通常用于生存分析
  var_subgroups = NULL,  # 要进行分析的多个亚组变量,默认为 NULL
  var_cov = NULL,  # 额外调整的协变量,默认为 NULL
  data,  # 数据集或 survey 包中的 svydesign 设计对象
  family = "binomial",  # 模型家族,可选 "gaussian"(正态分布)、"binomial"(二项分布)、"poisson"(泊松分布)或 "quasipoisson"(准泊松分布)
  decimal.estimate = 2,  # 估计值的小数位数,默认为 2
  decimal.percent = 1,  # 百分比的小数位数,默认为 1
  decimal.pvalue = 3,  # P 值的小数位数,默认为 3
  line = FALSE  # 是否在亚组变量之间包含换行符,默认为 FALSE
)

主要功能:

  • 广义线性模型分析(GLM):通过 TableSubgroupMultiGLM() 函数进行亚组分析。
  • Cox 模型分析:使用 TableSubgroupMultiCox() 进行 Cox 模型下的亚组分析。
  • 其他:其他功能需要我们进一步去探索。

1. 安装和加载 jstable 包

首先,我们需要安装并加载 jstable 包及其他依赖包:

# 安装 jstable 包(如未安装)
# install.packages("jstable")

# 加载必要的包
library(readr)
library(forestploter)
library(dplyr)
library(jstable)

2. 读取数据并处理

使用 readr 读取数据,并将相关变量转换为因子类型,以便后续进行亚组分析。

# 读取 CSV 数据
dt <- read.csv("depression_survey.csv", sep = ',', header = TRUE)

# 将多个变量转换为因子
dt$Gender <- factor(dt$Gender)
dt$Education_Level <- factor(dt$Education_Level)
dt$Marital_Status <- factor(dt$Marital_Status)
dt$Depression_Status <- factor(dt$Depression_Status)
dt$Chronic_Illness <- factor(dt$Chronic_Illness)
dt$Family_History_Depression <- factor(dt$Family_History_Depression)

# 创建新的年龄分组 Age.degree
dt <- dt %>%
  mutate(Age.degree = case_when(
    Age < 40 ~ 1,
    Age >= 40 & Age < 65 ~ 2,
    Age >= 65 ~ 3
  ))

# 将 Age.degree 转换为因子
dt$Age.degree <- factor(dt$Age.degree)

3. 进行亚组分析

使用 TableSubgroupMultiGLM() 函数进行多重亚组分析,分析每周活动时间对抑郁状态的影响。

# 使用 TableSubgroupMultiGLM 进行亚组分析
res <- TableSubgroupMultiGLM(
  formula = Depression_Status ~ Weekly_Activity_Hours,  
  var_subgroups = c("Age.degree", "Gender", "Education_Level", "Marital_Status",
                    "Chronic_Illness", "Family_History_Depression"),
  data = dt,  # 指定数据集
  family = "binomial",  # 使用逻辑回归
  decimal.estimate = 2,  # 估计值保留两位小数
  decimal.pvalue = 3  # P 值保留三位小数
)

# 查看分析结果
print(res)

4. 对结果进行后处理

有时,结果表中可能包含 NA 或复杂结构的 P value,需要进行处理并生成可用的表格。

# 处理 P 值,将列表中的 P 值提取为数值
res$`P value` <- sapply(res$`P value`, function(x) {
  if (is.numeric(x)) {
    return(x)
  } else if (is.character(x)) {
    return(as.numeric(x))
  } else if (is.list(x)) {
    return(as.numeric(x[[1]]))
  } else {
    return(NA)
  }
})

# 保存结果为 CSV 文件
write.csv(res, 'subgroup_analysis_results.csv', row.names = FALSE)

5. 绘制森林图

最后,使用 forestploter 包绘制森林图,以便直观展示各个亚组中的分析结果。

# 定义森林图的主题
tm <- forest_theme(
  base_size = 10,       
  refline_col = "grey",
  refline_lwd = 2,
  ci_col = "#1E9C76",
  refline_lty = "solid",
  arrow_type = "closed",    
  ci_lty = 1,
  ci_Theight = 0.2,
  ci_lwd = 2.3
)

# 选择需要绘制的列
res1 <- res[, c(1:3, 9, 10, 7, 8)]

# 绘制森林图
p <- forest(
  res1,
  est = res$OR,
  lower = res$Lower,
  upper = res$Upper,
  ci_column = 5,
  xlim = c(0.9, 1.1),
  ticks_at = c(0.90, 1.00, 1.10),
  ref_line = 1,
  sizes = 0.8,
  arrow_lab = c("Low Risk", "High Risk"),
  theme = tm
)

# 保存森林图为 PNG 文件
png("subgroup_forest_plot.png", width = 3000, height = 5000, res = 300)

# 打印森林图到文件中
print(p)

# 关闭 PNG 设备,完成保存
dev.off()

    特别申明:本文为转载文章,转载自峰林 敲冰煮茗录,不代表贪吃的夜猫子立场,如若转载,请注明出处:https://mp.weixin.qq.com/s?__biz=MzU2MTc1MTU1NQ==&mid=2247484517&idx=1&sn=789a1029f5929247ea5f5dcde34a3c20&chksm=fc72b709cb053e1f7f13fa85ee59d5840f52cfd227702844b3207de034f757cb7c171bde36fc&scene=178&cur_album_id=3656632259353591809#rd

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    xujunzju管理者
    上一篇 2024年10月16日 03:12
    下一篇 2024年10月21日 18:16

    相关推荐

    发表回复

    登录后才能评论
    联系我们
    邮箱:
    xujunzju@gmail.com
    公众号:
    xujunzju6174
    捐赠本站
    捐赠本站
    分享本页
    返回顶部
    我们最大的光荣,不在于一次也不失败,而在于每次倒下都能够站起来。