2

I'm trying to automate my R script to do a loop with loads of analyses and currently I've run aground on getting it to give me a warning during a failed CI test and proceed to the next response variable. I've tried "tryCatch" and "try" separately. Can someone tell me what I'm doing wrong and how to amend it? It's probably something dumb but I've spent weeks trying to sort it out. If I generalize the code below it will get confusing so I've left it in its original form.

Here's what I've got and here are the associated errors-

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)  

This is the error I want to skip over but note:

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

This is the error I don't want - it shows my attempt to skip over the above error isn't working:

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

Then there's this attempt

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

-or-

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

Thanks in advance!

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • See some closely related questions [here](http://stackoverflow.com/q/2589275/210673) and [here](http://stackoverflow.com/q/1395622/210673); also, [this](http://stackoverflow.com/q/4948361/210673) is the way I handle this kind of issue. – Aaron left Stack Overflow Sep 06 '12 at 01:35

2 Answers2

0

The mistake in the first chunk of code is that if int does inherit from class "try-error", you fit but throw away the lme() fit. Did you intend to assign that and then call intervals() on it and assign that to int so that int contained model term intervals no matter what?

If so the first code chunk should be:

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)
}

Note also that you don't need I(....) around the log10() call in the formula.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
0

what about using failwith() in package 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)
Maiasaura
  • 32,226
  • 27
  • 104
  • 108