在R中的lme循环中跳过错误警告

时间:2012-09-04 19:55:22

标签: r

我正在尝试自动化我的R脚本来进行大量分析的循环,目前我已经暂停让它在失败的CI测试期间给我一个警告并继续下一个响应变量。我已经尝试了“tryCatch”和“尝试”分开。有人能告诉我我做错了什么以及如何修改它?这可能是愚蠢的事情,但我花了几周时间试图解决它。如果我对下面的代码进行概括,那将会让人感到困惑,所以我将它保留为原始形式。

这是我所拥有的,这里是相关的错误 -

int <- try(intervals(tmpLme))
if (inherits(int, "try-error"))
    lme(tmp ~ I(log10(Body.mass..kg.)), random = ~1 | Species / CatNumber,
        data=felids, na.action=na.omit )

lower <- dim(int$fixed)[1] 
upper <- dim(int$fixed)[1] 
felidCIlower <- append(felidCIlower, int$fixed[,1],lower) 
felidCIupper <- append(felidCIupper, int$fixed[,3],upper)  

这是我想跳过的错误,但请注意:

Error in intervals.lme(tmpLme) : 
  Cannot get confidence intervals on var-cov components: Non-positive
  definite approximate variance-covariance 

这是我不想要的错误 - 它显示我跳过上述错误的尝试无效:

Error in int$fixed : $ operator is invalid for atomic vectors

然后是这次尝试

int.model <- function(tmpLme)
int <- tryCatch(intervals(tmpLme), error=function(e) NULL )

- 或 -

int <- tryCatch(intervals(tmpLme),
                error=function(e) lme(tmp ~ I(log10(Body.mass..kg.)),
                                      random = ~1 | Species / CatNumber,
                                      data=felids,na.action=na.omit ))
lower <- dim(int$fixed)[1] 
upper <- dim(int$fixed)[1] 
felidCIlower <- append(felidCIlower, int$fixed[,1], lower) 
felidCIupper <- append(felidCIupper, int$fixed[,3], upper)  

Error in !after : invalid argument type

提前致谢!

2 个答案:

答案 0 :(得分:0)

第一块代码中的错误是,如果int确实从类"try-error"继承,则符合会丢弃lme()。您是否打算分配该内容,然后在其上调用intervals()并将其分配给int,以便int无论如何都包含模型字词区间?

如果是这样,第一个代码块应为:

int <- try(intervals(tmpLme))
if (inherits(int, "try-error")) {
    tmpLme2 <- lme(tmp ~ log10(Body.mass..kg.), random = ~1 | Species/CatNumber,
                   data=felids, na.action=na.omit)
    int <- intervals(tmpLme2)
}

另请注意,公式中的I(....)来电不需要log10()

答案 1 :(得分:0)

failwith()包中使用plyr怎么样?

library(plyr)
# Change the NULL to whatever you want to return in case of an error.
clean_interval <- failwith(NULL, intervals)
int <- clean_interval(tmpLme)