使用circumplex包分析环形数据时purrr::map_dbl报错的解决咨询
问题:使用circumplex包的ssm_analyze函数分析环形数据时触发缺失值报错
报错信息
Error in **purrr::map_dbl()**: ℹ In index: 1. ℹ With name: t1. Caused by error in **quantile.default()**: ! missing values and NaN's not allowed if 'na.rm' is FALSE
执行代码
res.a1 <- ssm_analyze(.data = df.area1, scales = c(a.pos1, v.neg1, a.neg1, v.pos1), angles = c(90, 180, 270, 360), measures = c(safe1, inter1), measures_labels = c('Safety', 'Interaction')) summary(res.a1)
回溯信息
Backtrace: ▆ 1. ├─circumplex::ssm_analyze(...) 2. │ └─circumplex:::ssm_analyze_corrs(...) 3. │ └─circumplex:::ssm_bootstrap(...) 4. │ └─... %>% dplyr::select(-fit_lci) 5. ├─dplyr::select(., -fit_lci) 6. ├─circumplex (local) reshape_params(., suffix = "lci") 7. │ └─... %>% tibble::as_tibble(nrow = nrow(.)) 8. ├─tibble::as_tibble(., nrow = nrow(.)) 9. ├─base::`colnames<-`(...) 10. │ └─base::is.data.frame(x) 11. ├─base::matrix(., ncol = 6, byrow = TRUE) 12. └─purrr::map_dbl(., .f = quantile, probs = ((1 - interval)/2)) 13. └─purrr:::map_("double", .x, .f, ..., .progress = .progress) 14. ├─purrr:::with_indexed_errors(...) 15. │ └─base::withCallingHandlers(...) 16. ├─purrr:::call_with_cleanup(...) 17. ├─stats (local) .f(.x[[i]], ...) 18. └─stats:::quantile.default(.x[[i]], ...) 19. └─base::stop("missing values and NaN's not allowed if 'na.rm' is FALSE") Run rlang::last_trace(drop = FALSE) to see 4 hidden frames.
数据情况
已确认所有变量观测数均为94,数据样本如下:
id v.neg1 v.pos1 a.neg1 a.pos1 inter1 safe1 1 -0.1031421 -1.7056656 -1.6342515 -0.2935107 6 5 2 -0.1031421 -0.7625329 0.9694712 -0.2935107 5 7 3 -0.1031421 0.1805999 0.3185405 -0.2935107 7 8 4 9.5922176 1.1237326 0.3185405 -0.2935107 3 8 5 -0.1031421 0.1805999 0.3185405 -0.2935107 5 8 6 -0.1031421 1.1237326 0.9694712 -0.2935107 8 8 7 -0.1031421 -0.7625329 -1.6342515 -0.2935107 5 5 8 -0.1031421 -1.7056656 -1.6342515 -0.2935107 4 5 9 -0.1031421 0.1805999 -1.6342515 -0.2935107 6 7 10 -0.1031421 2.0668654 0.9694712 -0.2935107 9 9
解决方案
从回溯信息看,报错出现在bootstrap抽样后的分位数计算环节。原数据无显性缺失,但抽样过程或内部计算可能产生NaN/缺失值,可尝试以下方法解决:
清理隐形缺失值:先排查并替换数据中的
Inf或NaN,再删除含缺失值的行:df.area1 <- df.area1 %>% mutate(across(everything(), ~ifelse(is.infinite(.) | is.nan(.), NA, .))) %>% drop_na()调整bootstrap参数:关闭bootstrap或减少抽样次数,避免异常样本引发错误:
# 关闭bootstrap res.a1 <- ssm_analyze(.data = df.area1, scales = c(a.pos1, v.neg1, a.neg1, v.pos1), angles = c(90, 180, 270, 360), measures = c(safe1, inter1), measures_labels = c('Safety', 'Interaction'), bootstrap = FALSE) # 或减少抽样次数 res.a1 <- ssm_analyze(.data = df.area1, scales = c(a.pos1, v.neg1, a.neg1, v.pos1), angles = c(90, 180, 270, 360), measures = c(safe1, inter1), measures_labels = c('Safety', 'Interaction'), n_boot = 100)修正环形角度设置:环形数据通常用
0代替360,避免计算冲突:angles = c(90, 180, 270, 0)检查变量方差:样本中
a.pos1全部为同一值,方差为0会导致相关系数计算产生NaN,进而触发报错。运行以下代码确认:sapply(df.area1[, c("a.pos1", "v.neg1", "v.pos1", "a.neg1", "inter1", "safe1")], var)若存在方差为0的变量,需重新检查数据预处理流程,或根据研究设计调整变量选择。
内容的提问来源于stack exchange,提问作者Ola
相关产品推荐
相关产品推荐

