[R] multiple nls - next fit even after convergence problem
GOUACHE David
D.GOUACHE at arvalisinstitutduvegetal.fr
Sat Aug 4 15:25:06 CEST 2007
Hello R-gurus,
I'm trying to adjust different growth curves to a rather extensive dataset.
I wrote up a function to go through all of them, but am encountering a problem :
among the more than 1000 curves I have, obviously for some of them I encounter conversion problems.
I'd like for my function to keep going to the next curve and store the fact that for curve number X I had a convergence problem.
This is my original function :
comp.fit.2<-function(tab)
{
fit.log<-nls(surf.vert.tot ~ 100/(1+exp(((log(81))/a)*(sum.T.levee-b))), start=list( a=ifelse(sum(tab$surf.vert.tot>76)<1 | sum(tab$surf.vert.tot<15)<1,400,-max(tab$sum.T.levee[tab$surf.vert.tot>76],na.rm=T)+min(tab$sum.T.levee[tab$surf.vert.tot<15],na.rm=T)), b=tab$sum.T.levee[abs(tab$surf.vert.tot-50)==min(abs(tab$surf.vert.tot-50),na.rm=T)]),data=tab,control=list(maxiter=100))
fit.exp<-nls(surf.vert.tot ~ ifelse(100-10*exp((log(9)/a)*(sum.T.levee-b))<0,0,100-10*exp((log(9)/a)*(sum.T.levee-b))), start=list( a=ifelse(sum(tab$surf.vert.tot>76)<1 | sum(tab$surf.vert.tot<15)<1,400,-max(tab$sum.T.levee[tab$surf.vert.tot>76],na.rm=T)+min(tab$sum.T.levee[tab$surf.vert.tot<15],na.rm=T)), b=tab$sum.T.levee[abs(tab$surf.vert.tot-90)==min(abs(tab$surf.vert.tot-90),na.rm=T)]),data=tab,control=list(maxiter=100))
fit.gomp<-nls(surf.vert.tot ~ 100*exp(-(log(10/9))*exp(-( ( log(log(10/9))-log(log(10)) )/a )*(sum.T.levee-b))), start=list( a=ifelse(sum(tab$surf.vert.tot>76)<1 | sum(tab$surf.vert.tot<15)<1,400,-max(tab$sum.T.levee[tab$surf.vert.tot>76],na.rm=T)+min(tab$sum.T.levee[tab$surf.vert.tot<15],na.rm=T)), b=tab$sum.T.levee[abs(tab$surf.vert.tot-90)==min(abs(tab$surf.vert.tot-90),na.rm=T)]),data=tab,control=list(maxiter=100))
rmse.log<-sqrt(mean(residuals(fit.log)^2))
rmse.exp<-sqrt(mean(residuals(fit.exp)^2))
rmse.gomp<-sqrt(mean(residuals(fit.gomp)^2))
data.frame(rmse.log=rmse.log,rmse.gomp=rmse.gomp,rmse.exp=rmse.exp,semis=unique(tab$semis),densite=unique(tab$densite),traitement=unique(tab$traitement),bloc=unique(tab$bloc),num.feuille.def=unique(tab$num.feuille.def))
}
I've thought of just storing the 3 model results in a list, and then going through each model object,
but even then, when I have a convergence problem, the function breaks off.
What I'd like is to find a way for the function to keep running despite the convergence problem, and store in the model object NA or something like that...
Does anybody have an idea for this ?
Below is a sample of my data set, called tab.ex, with the points for 3 curves. The 1st and 3rd converge, ans the second fails and breaks off the function.
Thanks in advance for your suggestions !!!
tab.ex$semis<-as.factor(tab.ex$semis)
tab.ex$densite<-as.factor(tab.ex$densite)
tab.ex$bloc<-as.factor(tab.ex$bloc)
tab.ex$num.feuille.def<-as.factor(tab.ex$num.feuille.def)
tab.ex$num.not<-as.factor(tab.ex$num.not)
tab.ex$sum.T.levee<-tab.ex$sum.T.semis-117
semis densite traitement bloc num.feuille.def surf.vert.tot sum.T.semis
1 1 NT 1 1 100 1764.95
1 1 NT 1 1 100 1867.3
1 1 NT 1 1 98.50833333 1983.25
1 1 NT 1 1 37.91416667 2200
1 1 NT 1 1 0 2308.45
1 1 NT 1 1 0 2436.05
1 1 NT 1 1 0 2549.95
1 1 NT 1 1 0 2678.05
1 2 NT 1 1 100 1641.05
1 2 NT 1 1 100 1764.95
1 2 NT 1 1 100 1867.3
1 2 NT 1 1 99.58333333 1983.25
1 2 NT 1 1 0 2200
1 2 NT 1 1 0 2308.45
1 2 NT 1 1 0 2436.05
1 2 NT 1 1 0 2549.95
1 2 NT 1 1 0 2678.05
1 1 T 1 3 100 1440.1
1 1 T 1 3 99.45545455 1525.7
1 1 T 1 3 98.11727273 1641.05
1 1 T 1 3 97.65545455 1764.95
1 1 T 1 3 95.113 1867.3
1 1 T 1 3 93.04545455 1983.25
1 1 T 1 3 87.00666667 2200
1 1 T 1 3 50.895 2308.45
1 1 T 1 3 21.67416667 2436.05
1 1 T 1 3 4.333333333 2549.95
1 1 T 1 3 0 2678.05
More information about the R-help
mailing list