我在使用segmented包中的pscore.test函数时遇到了问题(我相信),因为它在内部要求在最初制作模型的环境之外的环境中对我的模型框架进行判断.以下是代码和错误回溯:

ESs <- c("comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp138286_c0_seq1:1233")

models <- lapply(ESs, function(i){
  lm(EL ~ temp, 数据=subset(数据, 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.数据.frame(数据)
 10. │     ├─dplyr::filter(数据, es_code == i)
 11. │     └─dplyr:::filter.数据.frame(数据, es_code == i)
 12. │       └─dplyr:::filter_rows(.数据, ..., 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))

即使我在Apply语句中创建了lm并将其输入到pscore.test中,我也得到了相同的问题.我不知道该如何处理这个问题.

数据

数据 <- 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 = "数据.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     数据sets  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

推荐答案

lmsubset=论点可以帮助你做到这一点.

ESs <- c("comp137978_c1_seq3:415", "comp137978_c1_seq3:475", "comp138286_c0_seq1:1233")

models <- lapply(ESs, function(i) {
  lm(EL ~ temp, data=data, subset=es_code == i)
})

lapply(models, function(i) segmented::pscore.test(i, seg.Z=~temp))
# [[1]]
# 
#   Score test for one/two changes in the slope
# 
# data:  formula = EL ~ temp 
# breakpoint for variable = temp 
# model = gaussian , link = identity , method = lm
# observed value = -1.2351, n.points = 10, p-value = 0.2327
# alternative hypothesis: two.sided   (1 breakpoint) 
# 
# 
# [[2]]
# 
#   Score test for one/two changes in the slope
# 
# data:  formula = EL ~ temp 
# breakpoint for variable = temp 
# model = gaussian , link = identity , method = lm
# observed value = 0.26025, n.points = 10, p-value = 0.7976
# alternative hypothesis: two.sided   (1 breakpoint) 
# 
# 
# [[3]]
# 
#   Score test for one/two changes in the slope
# 
# data:  formula = EL ~ temp 
# breakpoint for variable = temp 
# model = gaussian , link = identity , method = lm
# observed value = -2.2314, n.points = 10, p-value = 0.03669
# alternative hypothesis: two.sided   (1 breakpoint) 

R相关问答推荐

根据shiny 应用程序中的数字输入更改图标 colored颜色

如何使用R Shiny中的条件面板仅隐藏和显示用户输入,同时仍允许运行基础计算?

在垂直轴中包含多个ggplot2图中的平均值

使用R中相同值创建分组观测指标

如何在R中合并和合并多个rabrame?

在R中将特定列的值向右移动

如何在geom_col中反转条

如何在ggplot中标记qqplot上的点?

提取一个列表中单个列的重复观察结果R

计算数据帧中指定值之前的行数,仅基于每行之后的future 行,单位为r

如何在ggplot2中绘制具有特定 colored颜色 的连续色轮

在R中,我如何使用滑动窗口计算位置,然后进行过滤?

列名具有特殊字符时的循环回归

我如何go 掉盒子图底部的数字?

R -如何分配夜间GPS数据(即跨越午夜的数据)相同的开始日期?

是否从列中删除★符号?

将数据从一列转换为按组累计计数的单个虚拟变量

将美学添加到ggploy中的文本标签

根据排名的顶点属性调整曲线图布局(&Q)

如何准确地指出Read_delim所面临的问题?