I'm quite new in R and in StackOverflow. I'm trying to make a large bootstrap using a nlsLM inside a for loop. The nlsLM must to run about 1000 or 1500 times, each loop use about 70% of my data ordered randomly. If I run my code about 100 times it works fine, but with 1000 or 1500 loops it give me an error like " Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates ". I've already tried different start values, but always get the same error. Here is my code:
fitpoly2.btsp<-function(rep,s.rep) {
nsample<-round(nrow(dados)*(s.rep/100))
coefpoly2<-matrix()
for (i in 1:rep) {
rand<-dados[sample(nrow(dados),nsample),]
poly2fitted<-tryCatch({poly2fitted<-tryCatch({
nlsLM(Weight ~ (a1 * Length^b1) * (1/(1 + exp(rate *(Length - scp)))) + (a2 * Length^b2) *(1 - (1/(1 + exp(rate * (Length - scp))))),data=rand,start=list(a1=0.01,b1=2.8,a2=0.02,b2=3,scp=36,rate=10),control=nls.lm.control(maxiter=1024),lower=c(0,0,0,0,0,1),upper=c(1,6,1,6,74,20),trace=TRUE)})
if (i==1){ coefpoly2<-coef(poly2fitted)
} else {
coefpoly2<-rbind(coefpoly2,coef(poly2fitted))}
}}
Well, my friends, I know this problem is simple to solve and maybe my code is wrong or primitive, but I really don't know how can I fix it.
Thank you for you help and suggestions!!