April Lindeman <aprillinde...@yahoo.com> wrote on 09/04/2012 01:43:06 PM:
> 
> I am trying to compare Vbert growth curves from several years of 
> fish data. I am following the code provided by: http://
> www.ncfaculty.net/dogle/fishR/gnrlex/VonBertalanffy/VonBertalanffy.pdf
> . Specifically the section on VBGM Comparisons between groups. 
>  
> This code is pretty cut and dry. I am able to run it perfectly with 
> the "fake" data that is provided. But when I run it with my own data
> I get stuck with this line: 
> fitGen <- nls(vbGen,data=LMB,start=svGen)
>  
> I get this error code: Error in numericDeriv(form[[3L]], names(ind),
> env) : Missing value or an infinity produced when evaluating the model.
> 
>  
> Does anyone know how to fix it? I have no missing values and do not 
> know how to fix the infinity produced. 
> 
> Here is my data set: structure(list(Age = c(1L, 2L, 3L, 5L, 1L, 2L, 
> 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 
> 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), MM = c(155.
> 79, 296.37, 325.77, 374.65, 181.46, 258.31, 321.88, 355.6, 139.75, 
> 230.72, 319.61, 344.84, 130.92, 236.34, 290.53, 360.33, 400.61, 155.
> 33, 240.87, 315.46, 345.05, 378.2, 134.71, 256.66, 333.71, 362.99, 
> 381.46, 153.91, 217.21, 287.8, 357.28, 385.62, 222.25, 288.93, 294.
> 05, 332.79, 367.39), Year = c(2005L, 2005L, 2005L, 2005L, 2006L, 
> 2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 2007L, 2008L, 2008L, 
> 2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2009L, 2010L, 
> 2010L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 2011L, 2011L, 
> 2012L, 2012L, 2012L, 2012L, 2012L)), .Names = c("Age", "MM", 
> "Year"), class = "data.frame", row.names = c(NA, -37L)) 


As another person commented, you skipped the step where you attached the 
data
attach(LMB)


> In case it's helpful here is all my code before this point: 
> str(LMB) 


Why are you carrying out these as.integer() conversions?  It's not clear 
to me that they are necessary.


> MMi=as.integer(MM) 
> Yearf=as.factor(Year) 
> Agei=as.integer(Age) 


As another person commented, you skipped the step where you attached the 
needed packages.  I found I had to install and attach 7 packages to get 
your code to run!  But this could have been avoided by simply supplying us 
with the starting values:
svCom <- list(Linf=430, K=0.5, t0=0)


> ( svCom <- vbStarts(MMi~Agei,data=LMB)) 


You copied this code from the example in the pdf, but in that example they 
had only 2 groups.  You have 8 years.  So, this should be:
svGen <- lapply(svCom, rep, length(unique(Year)))


> ( svGen <- lapply(svCom,rep,2) ) 


Wow.  I didn't know you use the extraction function, [], in a formula. 
This is very good to know!  Thanks for educating me!!


> vbGen <- MMi~Linf[Yearf]*(1-exp(-K[Yearf]*(Age-t0[Yearf]))+error) 
> fitGen <- nls(vbGen,data=LMB,start=svGen) 
> 
>  
> Thank you, April


Here's the whole reproducible snippet of code ...

# the data
LMB <- structure(list(Age = c(1L, 2L, 3L, 5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), MM = c(155.79, 296.37, 325.77, 374.65, 
181.46, 258.31, 321.88, 355.6, 139.75, 230.72, 319.61, 344.84, 130.92, 
236.34, 290.53, 360.33, 400.61, 155.33, 240.87, 315.46, 345.05, 378.2, 
134.71, 256.66, 333.71, 362.99, 381.46, 153.91, 217.21, 287.8, 357.28, 
385.62, 222.25, 288.93, 294.05, 332.79, 367.39), Year = c(2005L, 2005L, 
2005L, 2005L, 2006L, 2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 2007L, 
2008L, 2008L, 2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2012L, 2012L, 2012L, 2012L, 2012L)), .Names = c("Age", "MM", "Year"), 
class = "data.frame", row.names = c(NA, -37L)) 

# attach the data
attach(LMB)

# provide starting values
svCom <- list(Linf=430, K=0.5, t0=0)
# repeat the starting values for each year
svGen <- lapply(svCom, rep, length(unique(Year)))
# define a new variable, uniquely numbered for each year
Yearf <- as.factor(Year)
# fit a single model with different parameter estimates for each year
fitGen <- nls(MM ~ Linf[Yearf]*(1-exp(-K[Yearf]*(Age-t0[Yearf]))), 
data=LMB, start=svGen) 
fitGen

Jean
        [[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.

Reply via email to