[R] try/tryCatch

lisa wang.lisay at gmail.com
Mon Mar 18 16:38:02 CET 2013


here is the error:

> aa<-metatrialstry(beta_5_50)

Error in asMethod(object) : matrix is not symmetric [1,2]



metatrials, the function that i am attempting to convert with try/tryCatch,
gives me back a matrix with as many rows are there are simulations  (z) in
the aray with dim(x,y,z). with the data i attached, x is 500(number of
patients), y is 9 (these are covariates), and z is 500.


metatrials<-function(mydata){

a<-matrix(data=NA, nrow=dim(mydata)[3], ncol=5)
colnames(a)=c("sens", "spec", "corr", "sens_se", "spec_se")

for(ii in 1:dim(mydata)[3]){
    tmp<-mydata[,,ii]
    tmp1<-as.data.frame(tmp)
    names(tmp1)=c("persons", "d1", "tp", "fn", "fp", "fn", "detect", "d0",
"outcome")
    lm1<-lmer(outcome~0+d1+d0+(0+d1+d0 | persons), family=binomial,
data=tmp1, nAGQ=3)
    a[ii,1]=lm1 at fixef[1]
    a[ii,2]=lm1 at fixef[2]
    a[ii,3]=vcov(lm1)[1,2]/prod(sqrt(diag(vcov(lm1))))
    a[ii,4:5]=sqrt(diag(vcov(lm1)))

    }
    return(a)
}


######################


what i want is for the function to go on to the next data set in the array
and simply return an NA for that line in the metatrials results. so
basically, just keep going.


thanks so much for your help!


-Lisa


On Mon, Mar 18, 2013 at 4:24 PM, jim holtman <jholtman at gmail.com> wrote:

> It would help if you told us what type of error you are getting and to
> also provide sample data so that we could run it to see what happens.  I
> use 'try' a lot to catch errors and have not had any problems with it.
>
> On Mon, Mar 18, 2013 at 6:11 AM, lisa <wang.lisay at gmail.com> wrote:
>
>> Hi All,
>>
>> I have tried every fix on my try or tryCatch that I have found on the
>> internet, but so far have not been able to get my R code to continue with
>> the "for loop" after the lmer model results in an error.
>>
>> Here is two attemps of my code, the input is a 3D array file, but really
>> any function would do....
>>
>> metatrialstry<-function(mydata){
>>
>> a<-matrix(data=NA, nrow=dim(mydata)[3], ncol=5)
>> #colnames(a)=c("sens", "spec", "corr", "sens_se", "spec_se",
>> "counter")#colnames(a)=c("sens", "spec", "corr", "sens_se", "spec_se")
>> k=1
>> for(ii in 1:dim(mydata)[3]){
>>     tmp<-mydata[,,ii]
>>     tmp1<-as.data.frame(tmp)
>>     names(tmp1)=c("persons", "d1", "tp", "fn", "fp", "fn", "detect", "d0",
>> "outcome")
>>     lm1<-try(lmer(outcome~0+d1+d0+(0+d1+d0 | persons), family=binomial,
>> data=tmp1, nAGQ=3), silent=T)
>>     if(class(lm1)[1]!='try-error'){
>>         a[ii,1]=lm1 at fixef[1]
>>         a[ii,2]=lm1 at fixef[2]
>>         a[ii,3]=vcov(lm1)[1,2]/prod(sqrt(diag(vcov(lm1))))
>>         a[ii,4:5]=sqrt(diag(vcov(lm1)))
>>         }
>>     }
>>     #k=k+1
>>     #a[ii,6]=k
>>
>>     return(a)
>> }
>>
>> #########################################
>> ######### try / try catch ###############
>> #########################################
>>
>>
>>
>> metatrialstry<-function(mydata){
>>
>> a<-matrix(data=NA, nrow=dim(mydata)[3], ncol=5)
>> #colnames(a)=c("sens", "spec", "corr", "sens_se", "spec_se", "counter")
>> colnames(a)=c("sens", "spec", "corr", "sens_se", "spec_se")
>> #a[,6]=rep(0, length(a[,6]))
>> for(ii in 1:dim(mydata)[3]){
>>     tmp<-mydata[,,ii]
>>     tmp1<-as.data.frame(tmp)
>>     names(tmp1)=c("persons", "d1", "tp", "fn", "fp", "fn", "detect", "d0",
>> "outcome")
>>     lm1<-tryCatch(lmer(outcome~0+d1+d0+(0+d1+d0 | persons),
>> family=binomial, data=tmp1, nAGQ=3), error=function(e) e)
>>     a[ii,1]=lm1 at fixef[1]
>>     a[ii,2]=lm1 at fixef[2]
>>     a[ii,3]=vcov(lm1)[1,2]/prod(sqrt(diag(vcov(lm1))))
>>     a[ii,4:5]=sqrt(diag(vcov(lm1)))
>>     }
>>     return(a)
>> }
>>
>>
>> Any guidance would be greatly appreciated...
>>
>> thanks!
>> Lisa
>>
>> --
>> Lisa Wang
>> email: wang.lisay at gmail.com
>> cell: +49 -0176-87786557
>> Tübingen, Germany, 72070
>>
>>         [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.




-- 
Lisa Wang
email: wang.lisay at gmail.com
cell: +49 -0176-87786557
Tübingen, Germany, 72070


More information about the R-help mailing list