You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在tbl_svysummary中仅查看因子变量特定水平并获取分层P值?

解决方案

要在tbl_strata结合tbl_svysummary的分析中,仅展示因子变量standing_chair的difficulty水平并计算组间对比的P值,可通过以下步骤实现:

核心思路

利用gtsummary的表格修改函数,在生成加权统计表格后过滤出目标水平的行,同时保留调查设计下的组间对比逻辑。

代码示例

假设你的处理组/对照组变量名为treatment(请替换为实际变量名),具体代码如下:

library(gtsummary)
library(survey)

# 按处理组/对照组分层,生成目标统计表格
tbl_strata(
  data = obese_adults,
  strata = treatment,  # 替换为你的分层变量名
  .tbl_fun = ~ .x %>%
    # 指定仅分析standing_chair变量,按treatment组做对比
    tbl_svysummary(
      include = standing_chair,
      by = treatment,
      # 可选:自定义分类变量的统计量格式(如百分比)
      statistic = all_categorical() ~ "{p}%"
    ) %>%
    # 过滤出仅展示difficulty水平的行
    modify_table_body(
      filter,
      variable == "standing_chair" & level == "difficulty"
    ) %>%
    # 添加基于调查设计的组间对比P值
    add_p(test = all_categorical() ~ svychisq.test)
)

关键说明

  • include = standing_chair:限制仅分析目标变量,避免冗余输出。
  • modify_table_body(filter, ...):精准筛选出standing_chair变量下difficulty水平对应的行,剔除其他水平的统计结果。
  • add_p(test = svychisq.test):针对调查设计数据,指定使用加权卡方检验计算组间差异的P值;若standing_chair是有序因子,可替换为svyrank.test适配秩和检验。

内容的提问来源于stack exchange,提问作者happytree12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 21:52:29