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)) ? In case it's helpful here is all my code before this point: str(LMB) MMi=as.integer(MM) Yearf=as.factor(Year) Agei=as.integer(Age) ( svCom <- vbStarts(MMi~Agei,data=LMB)) ( svGen <- lapply(svCom,rep,2) ) vbGen <- MMi~Linf[Yearf]*(1-exp(-K[Yearf]*(Age-t0[Yearf]))+error) fitGen <- nls(vbGen,data=LMB,start=svGen) ? Thank you, April
April Lindeman <aprillindeman <at> yahoo.com> writes:> 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 howto 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))> In case it's helpful here is all my code before this point: > str(LMB) > MMi=as.integer(MM) > Yearf=as.factor(Year) > Agei=as.integer(Age) > ( svCom <- vbStarts(MMi~Agei,data=LMB)) > ( svGen <- lapply(svCom,rep,2) ) > vbGen <- MMi~Linf[Yearf]*(1-exp(-K[Yearf]*(Age-t0[Yearf]))+error) > fitGen <- nls(vbGen,data=LMB,start=svGen)I believe that the reason you're running into trouble is that you have a very small number of data points per year. You're trying to fit a three-parameter model to at *most* 5 data points per year, and at least 4 (2005-2007 have only 4 ages, 2008-2012 have 5 ages). In principle this should be possible, but nls is a little bit fragile and so it gets in trouble. I wasn't able to run all your code because some of the packages you use seem to be unavailable for the development version of R I'm using ... The following solution works for me. You may be better off fitting a nonlinear mixed model, but I don't have time to work out that example at the moment ... 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)) library(ggplot2) LMB$Yearf <- factor(LMB$Year) (g0 <- ggplot(LMB,aes(Age,MM))+geom_point(aes(colour=factor(Year)))) (g2 <- g0 + geom_smooth(aes(colour=factor(Year)),se=FALSE,method="auto",span=2)) n1 <- nls(MM~SSasympOff(Age,Asym,lrc,c0),data=LMB) pframe <- data.frame(Age=seq(1,5,length.out=50)) pframe$MM <- predict(n1,newdata=pframe) (g1 <- g0+geom_line(data=pframe)) library(nlme) nyear <- length(unique(LMB$Year)) svec <- c(rbind(coef(n1), matrix(0,nrow=nyear-1,ncol=length(coef(n1))))) fit2 <- gnls(MM~SSasympOff(Age,Asym,lrc,c0),data=LMB, params=Asym+lrc+c0~Yearf, start=svec) printCoefmat(summary(fit2)$tTable) pframe2 <- expand.grid(Age=seq(1,5,length.out=50), Year=unique(LMB$Year)) pframe2$Yearf <- factor(pframe2$Year) pframe2$MM <- predict(fit2,newdata=pframe2) g0 + geom_line(data=pframe2, aes(colour=factor(Year)))
Pretty hard to say since you did not give us all of your code:> str(LMB)'data.frame': 37 obs. of 3 variables: $ Age : int 1 2 3 5 1 2 3 4 1 2 ... $ MM : num 156 296 326 375 181 ... $ Year: int 2005 2005 2005 2005 2006 2006 2006 2006 2007 2007 ...> MMi=as.integer(MM)Error: object 'MM' not found> Yearf=as.factor(Year)Error in is.factor(x) : object 'Year' not found> Agei=as.integer(Age)Error: object 'Age' not found So you left out an attach(LMB) at least. Then> attach(LMB) > MMi=as.integer(MM) > Yearf=as.factor(Year) > Agei=as.integer(Age) > ( svCom <- vbStarts(MMi~Agei,data=LMB))Error: could not find function "vbStarts" Now you've left out loading one or more packages with library() commands. I'll stop here. If you want help, provide "minimal, self-contained, reproducible code." ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of April Lindeman > Sent: Tuesday, September 04, 2012 1:43 PM > To: r-help at r-project.org > Subject: [R] Comparing Von Bertalanffy Growth Curves > > > > 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/VonBertalanf > fy.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)) > > > In case it's helpful here is all my code before this point: > str(LMB) > MMi=as.integer(MM) > Yearf=as.factor(Year) > Agei=as.integer(Age) > ( svCom <- vbStarts(MMi~Agei,data=LMB)) > ( svGen <- lapply(svCom,rep,2) ) > vbGen <- MMi~Linf[Yearf]*(1-exp(-K[Yearf]*(Age-t0[Yearf]))+error) > fitGen <- nls(vbGen,data=LMB,start=svGen) > > > Thank you, April > > > ______________________________________________ > 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.
April Lindeman <aprillindeman@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, AprilHere'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]]