Hello, I am using maximum likelihood to find the best parameters for a model. This involves sometimes tweaking the starting values to find a solution that converges. I would like to automate the process so that when the optimizer runs into an error it tweaks one of the parameters slightly, tries the fit again, and then continues this until a solution if found. I have been using try() to test if a fit will work (see below), but how do I run a loop that says continue until class(m1) is not "try error"? m1<-mlefun(startvals, data=data) if(class(m1)=="try-error"){startvals<-list(alpha=10,beta=1,loggamma=log(5),logk=log(exp(unlist(startvals[4]))+0.2)) mlefun(starvals, data)} This seems like it should be easy... but I am stymied. Thanks for your help John
John Poulsen wrote:> Hello, > > I am using maximum likelihood to find the best parameters for a model. > This involves sometimes tweaking the starting values to find a solution > that converges. > > I would like to automate the process so that when the optimizer runs > into an error it tweaks one of the parameters slightly, tries the fit > again, and then continues this until a solution if found. > > I have been using try() to test if a fit will work (see below), but how > do I run a loop that says continue until class(m1) is not "try error"? > > m1<-mlefun(startvals, data=data) > > if(class(m1)=="try-error"){startvals<-list(alpha=10,beta=1,loggamma=log(5),logk=log(exp(unlist(startvals[4]))+0.2)) > > mlefun(starvals, data)} >m1 <- mlefun(starvals, data=data) while(class(m1) == "try-error"){ startvals <- list(alpha=10, beta=1, loggamma=log(5), logk=log(exp(unlist(startvals[4]))+0.2)) m1 <- mlefun(starvals, data=data) } Uwe Ligges> This seems like it should be easy... but I am stymied. Thanks for your > help > > John > > ______________________________________________ > 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.
On May 30, 2009, at 9:36 AM, Uwe Ligges wrote:> > John Poulsen wrote: >> Hello, >> I am using maximum likelihood to find the best parameters for a >> model. This involves sometimes tweaking the starting values to >> find a solution that converges. >> I would like to automate the process so that when the optimizer >> runs into an error it tweaks one of the parameters slightly, tries >> the fit again, and then continues this until a solution if found. >> I have been using try() to test if a fit will work (see below), but >> how do I run a loop that says continue until class(m1) is not "try >> error"? >> m1<-mlefun(startvals, data=data) >> >> if(class(m1)=="try-error"){startvals<- >> list >> (alpha=10,beta=1,loggamma=log(5),logk=log(exp(unlist(startvals[4])) >> +0.2)) mlefun(starvals, data)} >> > > m1 <- mlefun(starvals, data=data) > while(class(m1) == "try-error"){ > startvals <- list(alpha=10, beta=1, loggamma=log(5), > logk=log(exp(unlist(startvals[4]))+0.2)) > m1 <- mlefun(starvals, data=data) > } >So this implicitly assumes that try() is wrapped around the code inside mlefun? David Winsemius, MD Heritage Laboratories West Hartford, CT
David Winsemius wrote:> > On May 30, 2009, at 9:36 AM, Uwe Ligges wrote: >> >> John Poulsen wrote: >>> Hello, >>> I am using maximum likelihood to find the best parameters for a >>> model. This involves sometimes tweaking the starting values to find >>> a solution that converges. >>> I would like to automate the process so that when the optimizer runs >>> into an error it tweaks one of the parameters slightly, tries the fit >>> again, and then continues this until a solution if found. >>> I have been using try() to test if a fit will work (see below), but >>> how do I run a loop that says continue until class(m1) is not "try >>> error"? >>> m1<-mlefun(startvals, data=data) >>> >>> if(class(m1)=="try-error"){startvals<-list(alpha=10,beta=1,loggamma=log(5),logk=log(exp(unlist(startvals[4]))+0.2)) >>> mlefun(starvals, data)} >>> >> >> m1 <- mlefun(starvals, data=data) >> while(class(m1) == "try-error"){ >> startvals <- list(alpha=10, beta=1, loggamma=log(5), >> logk=log(exp(unlist(startvals[4]))+0.2)) >> m1 <- mlefun(starvals, data=data) >> } >> > > So this implicitly assumes that try() is wrapped around the code inside > mlefun?Argh, thanks, I actually meant try(mlefun(starvals, data=data)) each time. Best, Uwe> > > David Winsemius, MD > Heritage Laboratories > West Hartford, CT >
Yes, it is wrapped around the code in mlefun, but could be changed... Thanks again, John. David Winsemius wrote:> > On May 30, 2009, at 9:36 AM, Uwe Ligges wrote: >> >> John Poulsen wrote: >>> Hello, >>> I am using maximum likelihood to find the best parameters for a >>> model. This involves sometimes tweaking the starting values to find >>> a solution that converges. >>> I would like to automate the process so that when the optimizer runs >>> into an error it tweaks one of the parameters slightly, tries the >>> fit again, and then continues this until a solution if found. >>> I have been using try() to test if a fit will work (see below), but >>> how do I run a loop that says continue until class(m1) is not "try >>> error"? >>> m1<-mlefun(startvals, data=data) >>> >>> if(class(m1)=="try-error"){startvals<-list(alpha=10,beta=1,loggamma=log(5),logk=log(exp(unlist(startvals[4]))+0.2)) >>> mlefun(starvals, data)} >>> >> >> m1 <- mlefun(starvals, data=data) >> while(class(m1) == "try-error"){ >> startvals <- list(alpha=10, beta=1, loggamma=log(5), >> logk=log(exp(unlist(startvals[4]))+0.2)) >> m1 <- mlefun(starvals, data=data) >> } >> > > So this implicitly assumes that try() is wrapped around the code > inside mlefun? > > > David Winsemius, MD > Heritage Laboratories > West Hartford, CT > >