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

无NA数据集调用rstatix的cohens_d()报逻辑值缺失错误排查

问题分析:rstatix包计算效应量报错原因及解决办法

问题现象

  • 清理完数据框NA后,用rstatix::cohens_d(y~x)计算效应量,报错:missing value where TRUE/FALSE needed
  • 换成wilcox_effsize()函数,又报错:can't deal with factors containing only one level
  • 换另一结构相似但带NA的数据集,却能正常运行
  • 运行环境:R 4.2.2(x86_64-w64-mingw32/x64),Windows 10 x64(build 19044)

出错的数据集及代码

# 出错的数据集结构
structure(list(y = c(7.18497519069826, 7.3003780648707, 7.17955179116519, 
8.36921585741014, 8.15836249209525, 7.09061070782841, 7.49108141342319, 
7.1846914308176, 6.67089495352021, 6.69143515214406, 6.42357351973274, 
7.52608069180203, 7.24501887073775, 6.85901814388889, 7.57170883180869, 
7.33425264233423, 8.04921802267018, 7.03181227133037, 7.59494473669508, 
7.19479175772192, 7.50365451924296, 7.98766626492627, 7.69670578093392, 
7.60357736815147, 6.96018527660461, 6.87390159786446, 7.06818586174616, 
7.73303668293358, 7.00902574208691, 7.43980621139333, 7.21563756343506, 
7.28869626059026, 7.16435285578444, 8.40397796366936, 8.11092624226642, 
6.87139778148748, 7.28510702956681, 7.28533222764388, 7.09131515969722, 
6.75541746281094, 7.48515334990365, 7.04727486738418, 7.05153839051533, 
6.94610823043691, 6.88677264305444, 7.17522180034305, 8.01535975540921, 
6.97657921864011, 7.44994098877334, 7.24328614608345, 6.94987770403687, 
7.0265332645233, 7.03662889536216, 6.7070589406276, 7.44075170047919, 
6.58972625625424, 6.75913881628117, 7.41597441137657, 7.57460994134019
), x = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), levels = c("untreated", "VRZ", "AMB", "untreated_107"), class = "factor")), row.names = c(NA, 
-59L), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(c(`58` = 58L), class = "omit"))

# 报错的运行代码
r_test %>%
  cohens_d(y~ x) %>%  
  as.data.frame()

可正常运行的对比数据集

structure(list(y = c(9.91e+08, 8.17e+08, 461200000, 15330000, 
175100000, 50320000, 13590000, 22970000, 2778000, 3453000, 12890000, 
375900000, 44590000, 1.611e+09, 1e+09, 889900000, 373200000, 
NA, NA, NA, NA, NA, 5010000, 6549000, 23160000, 32520000, 7707000, 
556900000, 634600000, 820900000, 391400000, 498300000, 147900000, 
646900000, 22060000, 1e+07, 306800000, 319400000, 41290000, 94100000, 
127200000, 117200000, 618300000, 570700000, 617100000, 284900000, 
449600000, 3866000, 6918000, 4177000, 14870000, 29380000, 2815000, 
1619000, 3126000, 1710000, 2191000), x = structure(c(1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("untreated", "VRZ", "AMB"
), class = "factor")), row.names = c(NA, -57L), class = c("tbl_df", 
"tbl", "data.frame"))

问题根源

仔细看出错数据集的因子x:它的水平包含"untreated_107",但整个数据里没有任何观测属于这个水平——相当于这个因子水平是空的。

  • wilcox_effsize()的报错直接点出了这个问题:无法处理只有一个水平的情况(这里是空水平,等同于该水平没有样本)
  • cohens_d()的报错是因为内部计算时,遇到空水平导致逻辑判断出现NA,触发了missing value where TRUE/FALSE needed的错误

而对比数据集的因子x所有水平都有对应的观测值,所以能正常运行。

解决办法

用droplevels()函数移除因子中的空水平,再计算效应量:

# 清理空因子水平
r_test_cleaned <- droplevels(r_test)

# 重新计算Cohen's d
r_test_cleaned %>%
  cohens_d(y ~ x) %>%
  as.data.frame()

# 重新计算Wilcoxon效应量
r_test_cleaned %>%
  wilcox_effsize(y ~ x)

内容的提问来源于stack exchange,提问作者mr.raccoon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 08:48:18