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

嵌套数据使用rstatix包t_test函数报错问题

问题:rstatix::t_test处理嵌套数据时触发"formula缺失"错误

使用stats::t.test()处理嵌套数据时正常运行,但调用rstatix::t_test()时抛出错误:! argument "formula" is missing, with no default。

错误原因

rstatix::t_test()的参数逻辑和基础包的t.test()存在差异:

  • 基础包t.test()允许在公式中直接用.x$列名的方式引用嵌套数据框中的列
  • rstatix::t_test()必须显式传入data参数指定数据框,公式中仅使用列名即可,不支持.x$的写法。

修正代码

将嵌套数据框传入rstatix::t_test()的data参数,公式简化为列名形式:

library(tidyverse)
library(rstatix)

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

nested_data <- df %>% 
  group_by(dose) %>% 
  nest()

# 修正后的rstatix t_test调用
nested_models2 <- nested_data %>%
  mutate(t_test2 = map(data, ~rstatix::t_test(len ~ supp, data = .x, paired = TRUE, detailed = TRUE)))

# 查看结果
nested_models2

验证结果

运行修正后的代码后,会生成包含每组t检验结果的嵌套数据框:

# A tibble: 3 × 3
# Groups:   dose [3]
  dose  data              t_test2         
  <fct> <list>            <list>          
1 0.5   <tibble [20 × 2]> <tibble [1 × 16]>
2 1     <tibble [20 × 2]> <tibble [1 × 16]>
3 2     <tibble [20 × 2]> <tibble [1 × 16]>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 02:15:09