在lapply中调用segmented包pscore.test的环境错误解决求助
问题:segmented包pscore.test函数因环境评估报错
使用segmented包的pscore.test()函数时,出现object 'i' not found错误,原因是该函数会在模型创建的外部环境中重新评估模型框架,而原模型通过subset()创建时保留了对变量i的引用,后续调用时i已不存在。
原代码
ESs <- c("comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp138286_c0_seq1:1233") models <- lapply(ESs, function(i){ lm(EL ~ temp, data=subset(data, es_code == i)) }) library(segmented) lapply(models, function(i) pscore.test(i, seg.Z=~temp))
错误信息
Error in `filter()`: ! Problem while computing `..1 = es_code == i`. Caused by error in `mask$eval_all_filter()`: ! object 'i' not found --- Backtrace: ▆ 1. ├─BiocGenerics::lapply(models, function(i) pscore.test(i, seg.Z = ~temp)) 2. ├─base::lapply(models, function(i) pscore.test(i, seg.Z = ~temp)) 3. │ └─global FUN(X[[i]], ...) 4. │ └─segmented::pscore.test(i, seg.Z = ~temp) 5. │ ├─base::eval(mf) 6. │ │ └─base::eval(mf) 7. │ ├─stats::model.frame(...) 8. │ ├─stats::model.frame.default(...) 9. │ │ └─base::is.data.frame(data) 10. │ ├─dplyr::filter(data, es_code == i) 11. │ └─dplyr:::filter.data.frame(data, es_code == i) 12. │ └─dplyr:::filter_rows(.data, ..., caller_env = caller_env()) 13. │ └─dplyr:::filter_eval(dots, mask = mask, error_call = error_call) 14. │ ├─base::withCallingHandlers(...) 15. │ └─mask$eval_all_filter(dots, env_filter) 16. └─base::.handleSimpleError(...) 17. └─dplyr (local) h(simpleError(msg, call)) 18. └─rlang::abort(bullets, call = error_call, parent = skip_internal_condition(e))
数据
data <- structure(list(EL = c(0.105914718019257, 0.00572519083969466, 0.875763747454175, 0.709941520467836, 0.0230607966457023, 0.00131061598951507, 0.88255033557047, 0.627272727272727, 0.127433628318584, 0.0772676371780515, 0.794117647058823, 0.619552414605418, 0.610021786492375, 0.284644194756554, 0.0939490445859873, 0.0137420718816068, 0.0749128919860627, 0.01, 0.868312757201646, 0.643356643356643, 0.543909348441926, 0.258706467661692, 0.822709163346614, 0.568459657701711, 0.27190332326284, 0.104795737122558, 0.579497907949791, 0.464713715046605, 0.563583815028902, 0.374376039933444, 0.460405156537753, 0.160046728971963, 0.531353135313531, 0.349532710280374, 0.319230769230769, 0.141025641025641, 0.589473684210526, 0.42258064516129, 0.602040816326531, 0.254752851711027, 0.965699208443272, 0.788273615635179, 0.96280276816609, 0.842271293375394, 0.987025023169602, 0.894894894894895, 0.966329966329966, 0.748914616497829, 0.946175637393768, 0.989655172413793, 0.825665859564165, 0.942332896461337, 0.928571428571429, 0.957163958641064, 0.934981684981685, 0.96353591160221, 0.955985915492958, 0.948051948051948, 0.9140625, 0.920071047957371, 0.959531416400426, 0.947741364038973, 0.965025906735751), temp = c(23L, 23L, 10L, 10L, 25L, 25L, 10L, 10L, 25L, 25L, 10L, 10L, 12L, 12L, 23L, 23L, 23L, 23L, 14L, 14L, 18L, 18L, 14L, 14L, 18L, 18L, 14L, 14L, 18L, 18L, 16L, 16L, 20L, 20L, 20L, 20L, 16L, 16L, 20L, 20L, 12L, 23L, 10L, 25L, 10L, 25L, 10L, 25L, 12L, 12L, 23L, 14L, 18L, 14L, 18L, 14L, 18L, 16L, 20L, 16L, 20L, 16L, 20L), es_code = c("comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233", "comp138286_c0_seq1:1233")), class = "data.frame", row.names = c(NA, -63L))
会话信息
R version 4.2.1 (2022-06-23) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Ventura 13.3.1 Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] segmented_1.6-4 nlme_3.1-157 MASS_7.3-57 loaded via a namespace (and not attached): [1] compiler_4.2.1 tools_4.2.1 rstudioapi_0.13 splines_4.2.1 grid_4.2.1 lattice_0.20-45
解决方案
问题根源在于subset()函数会保留筛选表达式的环境引用,当pscore.test()尝试重新构建模型框架时,会去解析该表达式,但此时原环境中的变量i已经不存在。解决方法是提前创建独立的子集数据框,避免依赖外部变量:
ESs <- c("comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp138286_c0_seq1:1233") models <- lapply(ESs, function(i){ # 直接生成子集数据框,而非使用subset保留表达式 sub_data <- data[data$es_code == i, ] lm(EL ~ temp, data = sub_data) }) library(segmented) lapply(models, function(m) pscore.test(m, seg.Z=~temp))
这样每个模型的data参数指向独立的子集数据框,没有外部变量依赖,pscore.test()可以正常评估模型框架。
内容的提问来源于stack exchange,提问作者CephBirk
相关产品推荐
相关产品推荐

