0

I have an issue about nls function. I am trying to make plots with von Bertalanffy growth (Length and age) for each species (I have 13 species in total).

First I made a list of dataframe, each dataframe contain the data for a specific Species

dfss <- split(df, list(df$Species.Name),drop=TRUE,sep="/")

I then have a function to calculate the models


VGBF<-function(xi){
  
 #VGBF.calc
    agesum<-xi %>% group_by(Species.Name) %>% summarise(minage=min(age),maxage=max(age))
    measures<-as.data.frame(cbind(Length_1=xi$Length_1,age=xi$age))
   vb<-vbFuns(param="Typical")
   f.starts<-vbStarts(Length_1~age,data=measures)
   f.fit<-  nls(Length_1~vb(age,Linf,K,t0),data=measures,start=f.starts,nls.control(maxiter = 1000))
  #  coef(f.fit)
  # f.boot1<- Boot(f.fit)
  # confint(f.boot1)
  # predict(f.fit,data.frame(age=unique(xi$minimum.age):unique(xi$maximum.age)))
 

  predict2<- function(x) {predict(x,data.frame(age=ages))}
  ages<-unique(xi$minimum.age):unique(xi$maximum.age)
  predict2(f.fit)
  
  ages<-seq(unique(xi$minimum.age)-2,unique(xi$maximum.age)+2,by=0.2)
  f.boot2<-Boot(f.fit,f=predict2)
  preds1<-data.frame(ages,
                     predict(f.fit,data.frame(age=ages)),
                     confint(f.boot2))
  names(preds1)<-c("age","fit","LCI","UCI")
  preds2<-filter(preds1,age>=agesum$minage,age<=agesum$maxage)}

I then apply that function to my list that contains the information for each species

lapply(dfss,VGBF)

I have the following error message

Error in nls(Length_1 ~ cvb(age, Linf, K, t0), data = measures, astart=f.starts, : 
 step factor 0.000488281 reduced below 'minFactor' of 0.000976562

The script works well when it is not nested in a lapply function (using the original dataset filtered for a specific species as the dataframe used for the content of the VGBF function).

It also seems not to be working when I use a for loop instead of a lapply function.

So, my question is how can I overcome this issue.

I hope my question is clear enough.

Thank you in advance

Sebastien
  • 25
  • 3
  • 1
    I think the error that you are getting is coming for a specific Species and not all of them. How many species do you have in your data? Can you try this with `drop=FALSE` in `split`? – Ronak Shah Jun 29 '21 at 03:39
  • Sorry for the late answer, you're right I only have this problem for 2 species. However I don't know why this error occur on those specific species thus I can't fix it. If you have any suggestions I would be more than pleased to hear – Sebastien Jul 07 '21 at 05:49

0 Answers0