Re: [R] try/tryCatch
I would assume that if the code were as follows that the error could the caught and the loop continued: 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-try(lmer(outcome~0+d1+d0+(0+d1+d0 | persons), family=binomial, data=tmp1, nAGQ=3)) if (!inherits(lm1, 'try-error')){ a[ii,1]=lm1@fixef[1] a[ii,2]=lm1@fixef[2] a[ii,3]=vcov(lm1)[1,2]/prod(sqrt(diag(vcov(lm1 a[ii,4:5]=sqrt(diag(vcov(lm1))) } } return(a) } On Mon, Mar 18, 2013 at 11:38 AM, lisa wang.li...@gmail.com wrote: 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@fixef[1] a[ii,2]=lm1@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 jholt...@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.li...@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@fixef[1] a[ii,2]=lm1@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@fixef[1] a[ii,2]=lm1@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.li...@gmail.com cell: +49 -0176-87786557 Tübingen, Germany, 72070 [[alternative HTML version deleted]] __ R-help@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.li...@gmail.com cell: +49 -0176-87786557 Tübingen, Germany, 72070 -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how
[R] try/tryCatch
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@fixef[1] a[ii,2]=lm1@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@fixef[1] a[ii,2]=lm1@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.li...@gmail.com cell: +49 -0176-87786557 Tübingen, Germany, 72070 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] try/tryCatch
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.li...@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@fixef[1] a[ii,2]=lm1@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@fixef[1] a[ii,2]=lm1@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.li...@gmail.com cell: +49 -0176-87786557 Tübingen, Germany, 72070 [[alternative HTML version deleted]] __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] try/tryCatch
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@fixef[1] a[ii,2]=lm1@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 jholt...@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.li...@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@fixef[1] a[ii,2]=lm1@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@fixef[1] a[ii,2]=lm1@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.li...@gmail.com cell: +49 -0176-87786557 Tübingen, Germany, 72070 [[alternative HTML version deleted]] __ R-help@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.li...@gmail.com cell: +49 -0176-87786557 Tübingen, Germany, 72070 __ R-help@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.
[R] try / tryCatch for download.file( ) within a for loop when URL does not exist
I am trying to download a bunch of files from a server, for which I am using download.file( ) within a for loop. The script is working fine except until download.file hits a URL which has no file, at which point it exits. I want to change this behavior to simple log the failure and maintain state within the for loop and iterate to next. I read about try / tryCatch but am having trouble understanding what/how it does. Thanks. #begin script date - as.POSIXlt(as.Date(2007-10-15, %Y-%m-%d)) for (i in 1:difftime(as.POSIXlt(as.Date(2007-10-15, %Y-%m-%d)),2007-10-01)) { if (date$wday != 0 date$wday != 6) { datestr - as.character(date, %d%m%y) #make date character string ddmmyy url - paste(http://www.bseindia.com/bhavcopy/eq,datestr,_csv.zip,sep=;) file - paste(C:/,datestr,_csv.zip,sep=) #options(show.error.messages = FALSE) #options(warn = -1) try(download.file(url, destfile = file, quiet = TRUE, mode = wb), finally = cat(OK:,url,\n)) date - as.POSIXlt(as.Date(date - 86400)) } else {date - as.POSIXlt(as.Date(date - 86400))} } # end script Thanks, Vishal Belsare __ R-help@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.
Re: [R] try / tryCatch for download.file( ) within a for loop when URL does not exist
Jim, Thanks. Actually I just got it working a few minutes ago with: tryCatch({download.file(url, destfile = file, quiet = FALSE, mode = wb)}, silent = FALSE, condition = function(err) { } ) but I like your suggestion better. I'll attempt to log the list of url's downloaded ok and those which encountered an error. Thanks! Vishal On 10/17/07, jim holtman [EMAIL PROTECTED] wrote: I think this is closer to what you want. You can determine what you want to do; this one just goes to the next iteration: date - as.POSIXlt(as.Date(2007-10-15, %Y-%m-%d)) for (i in 1:difftime(as.POSIXlt(as.Date(2007-10-15, %Y-%m-%d)),2007-10-01)) { if (date$wday != 0 date$wday != 6) { datestr - as.character(date, %d%m%y) #make date character string ddmmyy url - paste(http://www.bseindia.com/bhavcopy/eq,datestr,_csv.zip,sep=;) file - paste(C:/,datestr,_csv.zip,sep=) #options(show.error.messages = FALSE) #options(warn = -1) err - try(download.file(url, destfile = file, quiet = TRUE, mode = wb)) if (class(err) == try-error) next # skip this iteration on error date - as.POSIXlt(as.Date(date - 86400)) } else {date - as.POSIXlt(as.Date(date - 86400))} } On 10/16/07, Vishal Belsare [EMAIL PROTECTED] wrote: I am trying to download a bunch of files from a server, for which I am using download.file( ) within a for loop. The script is working fine except until download.file hits a URL which has no file, at which point it exits. I want to change this behavior to simple log the failure and maintain state within the for loop and iterate to next. I read about try / tryCatch but am having trouble understanding what/how it does. Thanks. #begin script date - as.POSIXlt(as.Date(2007-10-15, %Y-%m-%d)) for (i in 1:difftime(as.POSIXlt(as.Date(2007-10-15, %Y-%m-%d)),2007-10-01)) { if (date$wday != 0 date$wday != 6) { datestr - as.character(date, %d%m%y) #make date character string ddmmyy url - paste(http://www.bseindia.com/bhavcopy/eq,datestr,_csv.zip,sep=;) file - paste(C:/,datestr,_csv.zip,sep=) #options(show.error.messages = FALSE) #options(warn = -1) try(download.file(url, destfile = file, quiet = TRUE, mode = wb), finally = cat(OK:,url,\n)) date - as.POSIXlt(as.Date(date - 86400)) } else {date - as.POSIXlt(as.Date(date - 86400))} } # end script Thanks, Vishal Belsare __ R-help@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 Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@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.