[R] nls question
Hi: Using nls how can I increase the numbers of iterations to go beyond 50. I just want to be able to predict for the last two weeks of the year. This is what I have: weight_random - runif(50,1,24) weight - sort(weight_random);weight weightData - data.frame(weight,week=1:50) weightData plot(weight ~ week, weightData) M_model - nls(weight ~ alpha + beta*exp(gamma*week), weightData, start = c(alpha = 0.0, beta = 1, gamma = 0.2), trace = TRUE) ### I get the error below: Error in nls(weight ~ alpha + beta * exp(gamma * week), weightData, start = c(alpha = 0, : number of iterations exceeded maximum of 50 M_model ### predict for another 2 weeks newD - data.frame(week = 1:52);newD newD$pred_wt - predict(M_model, newD) newD plot(pred_wt ~ week, newD, pch = 4, col = red, ylab = Weight, xlab = Week) with(weightData, points(week, weight,col='blue')) Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish Wildlife Service California, USA http://www.fws.gov/redbluff/rbdd_jsmp.aspx [[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.
Re: [R] nls question
See ?nls.control (referenced from ?nls). On 12/07/2012 18:47, Felipe Carrillo wrote: Hi: Using nls how can I increase the numbers of iterations to go beyond 50. I just want to be able to predict for the last two weeks of the year. This is what I have: weight_random - runif(50,1,24) weight - sort(weight_random);weight weightData - data.frame(weight,week=1:50) weightData plot(weight ~ week, weightData) M_model - nls(weight ~ alpha + beta*exp(gamma*week), weightData, start = c(alpha = 0.0, beta = 1, gamma = 0.2), trace = TRUE) ### I get the error below: Error in nls(weight ~ alpha + beta * exp(gamma * week), weightData, start = c(alpha = 0, : number of iterations exceeded maximum of 50 M_model ### predict for another 2 weeks newD - data.frame(week = 1:52);newD newD$pred_wt - predict(M_model, newD) newD plot(pred_wt ~ week, newD, pch = 4, col = red, ylab = Weight, xlab = Week) with(weightData, points(week, weight,col='blue')) Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish Wildlife Service California, USA http://www.fws.gov/redbluff/rbdd_jsmp.aspx [[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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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.
Re: [R] nls question
Read the Help file! ?nls ## Note the control argument ?nls.control -- Bert On Thu, Jul 12, 2012 at 10:47 AM, Felipe Carrillo mazatlanmex...@yahoo.comwrote: Hi: Using nls how can I increase the numbers of iterations to go beyond 50. I just want to be able to predict for the last two weeks of the year. This is what I have: weight_random - runif(50,1,24) weight - sort(weight_random);weight weightData - data.frame(weight,week=1:50) weightData plot(weight ~ week, weightData) M_model - nls(weight ~ alpha + beta*exp(gamma*week), weightData, start = c(alpha = 0.0, beta = 1, gamma = 0.2), trace = TRUE) ### I get the error below: Error in nls(weight ~ alpha + beta * exp(gamma * week), weightData, start = c(alpha = 0, : number of iterations exceeded maximum of 50 M_model ### predict for another 2 weeks newD - data.frame(week = 1:52);newD newD$pred_wt - predict(M_model, newD) newD plot(pred_wt ~ week, newD, pch = 4, col = red, ylab = Weight, xlab = Week) with(weightData, points(week, weight,col='blue')) Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish Wildlife Service California, USA http://www.fws.gov/redbluff/rbdd_jsmp.aspx [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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.
Re: [R] nls question
Thanks Bert, I increased the number of iterations: M_model - nls(weight ~ alpha + beta*exp(gamma*week),control=nls.control(maxiter=200), weightData, start = c(alpha = 0.0, beta = 1, gamma = 0.2), trace = TRUE) But now the 'start' argument seems to be the problem. Looking at the model coefficients: alpha beta gamma -49.108008320 52.394172340 0.005269907 If I change the alpha and beta values inside start..I still get the error... Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish Wildlife Service California, USA http://www.fws.gov/redbluff/rbdd_jsmp.aspx From: Bert Gunter gunter.ber...@gene.com To: Felipe Carrillo mazatlanmex...@yahoo.com Cc: r-help@r-project.org r-help@r-project.org Sent: Thursday, July 12, 2012 10:56 AM Subject: Re: [R] nls question Read the Help file! ?nls ## Note the control argument ?nls.control -- Bert On Thu, Jul 12, 2012 at 10:47 AM, Felipe Carrillo mazatlanmex...@yahoo.com wrote: Hi: Using nls how can I increase the numbers of iterations to go beyond 50. I just want to be able to predict for the last two weeks of the year. This is what I have: weight_random - runif(50,1,24) weight - sort(weight_random);weight weightData - data.frame(weight,week=1:50) weightData plot(weight ~ week, weightData) M_model - nls(weight ~ alpha + beta*exp(gamma*week), weightData, start = c(alpha = 0.0, beta = 1, gamma = 0.2), trace = TRUE) ### I get the error below: Error in nls(weight ~ alpha + beta * exp(gamma * week), weightData, start = c(alpha = 0, : number of iterations exceeded maximum of 50 M_model ### predict for another 2 weeks newD - data.frame(week = 1:52);newD newD$pred_wt - predict(M_model, newD) newD plot(pred_wt ~ week, newD, pch = 4, col = red, ylab = Weight, xlab = Week) with(weightData, points(week, weight,col='blue')) Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish Wildlife Service California, USA http://www.fws.gov/redbluff/rbdd_jsmp.aspx [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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.
Re: [R] nls question
On Thu, Jul 12, 2012 at 3:02 PM, Felipe Carrillo mazatlanmex...@yahoo.com wrote: Thanks Bert, I increased the number of iterations: M_model - nls(weight ~ alpha + beta*exp(gamma*week),control=nls.control(maxiter=200), weightData, start = c(alpha = 0.0, beta = 1, gamma = 0.2), trace = TRUE) But now the 'start' argument seems to be the problem. Looking at the model coefficients: alpha beta gamma -49.108008320 52.394172340 0.005269907 If I change the alpha and beta values inside start..I still get the error... Try using the plinear algorithm: nls(weight ~ cbind(1, exp(gamma*week)), weightData, start = list(gamma = 0.2), alg = plinear) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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.
Re: [R] nls question
I get a different error now: nls(weight ~ cbind(1, exp(gamma*week)), weightData, start = list(gamma= 0.2), alg = plinear) Error in nls(weight ~ cbind(1, exp(gamma * week)), weightData, start = list(gamma = 0.2), : step factor 0.000488281 reduced below 'minFactor' of 0.000976562 The help file says: When start is missing, a very cheap guess for start is tried (if algorithm != plinear). So I removed 'plinear' from the call and got the following: nls(weight ~ cbind(1, exp(gamma*week)), weightData,start = list(gamma=0.2),trace=TRUE) 1466475181 : 0.2 Error in qr.qty(QR, resid) : 'qr' and 'y' must have the same number of rows Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish Wildlife Service California, USA http://www.fws.gov/redbluff/rbdd_jsmp.aspx From: Gabor Grothendieck ggrothendi...@gmail.com To: Felipe Carrillo mazatlanmex...@yahoo.com Cc: Bert Gunter gunter.ber...@gene.com; r-help@r-project.org r-help@r-project.org Sent: Thursday, July 12, 2012 12:14 PM Subject: Re: [R] nls question On Thu, Jul 12, 2012 at 3:02 PM, Felipe Carrillo mazatlanmex...@yahoo.com wrote: Thanks Bert, I increased the number of iterations: M_model - nls(weight ~ alpha + beta*exp(gamma*week),control=nls.control(maxiter=200), weightData, start = c(alpha = 0.0, beta = 1, gamma = 0.2), trace = TRUE) But now the 'start' argument seems to be the problem. Looking at the model coefficients: alpha beta gamma -49.108008320 52.394172340 0.005269907 If I change the alpha and beta values inside start..I still get the error... Try using the plinear algorithm: nls(weight ~ cbind(1, exp(gamma*week)), weightData, start = list(gamma = 0.2), alg = plinear) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com [[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.
Re: [R] nls question
On Thu, Jul 12, 2012 at 3:40 PM, Felipe Carrillo mazatlanmex...@yahoo.com wrote: I get a different error now: nls(weight ~ cbind(1, exp(gamma*week)), weightData, start = list(gamma= 0.2), alg = plinear) Error in nls(weight ~ cbind(1, exp(gamma * week)), weightData, start = list(gamma = 0.2), : step factor 0.000488281 reduced below 'minFactor' of 0.000976562 The help file says: When start is missing, a very cheap guess for start is tried (if algorithm != plinear). Please give a reproducible example by setting the seed. This reproducible example converges: set.seed(123) weight_random - runif(50,1,24) weight - sort(weight_random) weightData - data.frame(weight,week=1:50) nls(weight ~ cbind(1, exp(gamma*week)), weightData, start = list(gamma = 0.2), alg = plinear) Nonlinear regression model model: weight ~ cbind(1, exp(gamma * week)) data: weightData gamma .lin1 .lin2 1.136e-03 -3.949e+02 3.962e+02 residual sum-of-squares: 9.17 Number of iterations to convergence: 8 Achieved convergence tolerance: 9.581e-06 as does nls with Gauss Newton: nls(weight ~ alpha + beta*exp(gamma*week), weightData, start = + c(alpha = 0.0, beta = 1, gamma = 0.2) + ) Nonlinear regression model model: weight ~ alpha + beta * exp(gamma * week) data: weightData alpha beta gamma -3.949e+02 3.961e+02 1.136e-03 residual sum-of-squares: 9.17 Number of iterations to convergence: 48 Achieved convergence tolerance: 2.906e-06 So I removed 'plinear' from the call and got the following: nls(weight ~ cbind(1, exp(gamma*week)), weightData,start = list(gamma=0.2),trace=TRUE) It cannot be specified as if it were a plinear model but then use Gauss-Newton. See ?nls -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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.
Re: [R] nls question
To add to Gabor's remarks: This has nothing to do per se with R -- it is your insufficient understanding of the underlying mathematical issues. It is pretty trivial to do the plinear algorithm by hand. Fit linear regressions weight ~ lm(weight ~z) where z is exp(gamma*week) for a suitable sequence of gamma values and just choose that which gives the best fit. Or use this to get a starting value for nls. -- Bert On Thu, Jul 12, 2012 at 1:03 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: On Thu, Jul 12, 2012 at 3:40 PM, Felipe Carrillo mazatlanmex...@yahoo.com wrote: I get a different error now: nls(weight ~ cbind(1, exp(gamma*week)), weightData, start = list(gamma= 0.2), alg = plinear) Error in nls(weight ~ cbind(1, exp(gamma * week)), weightData, start = list(gamma = 0.2), : step factor 0.000488281 reduced below 'minFactor' of 0.000976562 The help file says: When start is missing, a very cheap guess for start is tried (if algorithm != plinear). Please give a reproducible example by setting the seed. This reproducible example converges: set.seed(123) weight_random - runif(50,1,24) weight - sort(weight_random) weightData - data.frame(weight,week=1:50) nls(weight ~ cbind(1, exp(gamma*week)), weightData, start = list(gamma = 0.2), alg = plinear) Nonlinear regression model model: weight ~ cbind(1, exp(gamma * week)) data: weightData gamma .lin1 .lin2 1.136e-03 -3.949e+02 3.962e+02 residual sum-of-squares: 9.17 Number of iterations to convergence: 8 Achieved convergence tolerance: 9.581e-06 as does nls with Gauss Newton: nls(weight ~ alpha + beta*exp(gamma*week), weightData, start = + c(alpha = 0.0, beta = 1, gamma = 0.2) + ) Nonlinear regression model model: weight ~ alpha + beta * exp(gamma * week) data: weightData alpha beta gamma -3.949e+02 3.961e+02 1.136e-03 residual sum-of-squares: 9.17 Number of iterations to convergence: 48 Achieved convergence tolerance: 2.906e-06 So I removed 'plinear' from the call and got the following: nls(weight ~ cbind(1, exp(gamma*week)), weightData,start = list(gamma=0.2),trace=TRUE) It cannot be specified as if it were a plinear model but then use Gauss-Newton. See ?nls -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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.
Re: [R] nls question
Thank you all for your help. Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish Wildlife Service California, USA http://www.fws.gov/redbluff/rbdd_jsmp.aspx From: Gabor Grothendieck ggrothendi...@gmail.com To: Felipe Carrillo mazatlanmex...@yahoo.com Cc: r-help@r-project.org r-help@r-project.org Sent: Thursday, July 12, 2012 1:03 PM Subject: Re: [R] nls question On Thu, Jul 12, 2012 at 3:40 PM, Felipe Carrillo mazatlanmex...@yahoo.com wrote: I get a different error now: nls(weight ~ cbind(1, exp(gamma*week)), weightData, start = list(gamma= 0.2), alg = plinear) Error in nls(weight ~ cbind(1, exp(gamma * week)), weightData, start = list(gamma = 0.2), : step factor 0.000488281 reduced below 'minFactor' of 0.000976562 The help file says: When start is missing, a very cheap guess for start is tried (if algorithm != plinear). Please give a reproducible example by setting the seed. This reproducible example converges: set.seed(123) weight_random - runif(50,1,24) weight - sort(weight_random) weightData - data.frame(weight,week=1:50) nls(weight ~ cbind(1, exp(gamma*week)), weightData, start = list(gamma = 0.2), alg = plinear) Nonlinear regression model model: weight ~ cbind(1, exp(gamma * week)) data: weightData gamma .lin1 .lin2 1.136e-03 -3.949e+02 3.962e+02 residual sum-of-squares: 9.17 Number of iterations to convergence: 8 Achieved convergence tolerance: 9.581e-06 as does nls with Gauss Newton: nls(weight ~ alpha + beta*exp(gamma*week), weightData, start = + c(alpha = 0.0, beta = 1, gamma = 0.2) + ) Nonlinear regression model model: weight ~ alpha + beta * exp(gamma * week) data: weightData alpha beta gamma -3.949e+02 3.961e+02 1.136e-03 residual sum-of-squares: 9.17 Number of iterations to convergence: 48 Achieved convergence tolerance: 2.906e-06 So I removed 'plinear' from the call and got the following: nls(weight ~ cbind(1, exp(gamma*week)), weightData,start = list(gamma=0.2),trace=TRUE) It cannot be specified as if it were a plinear model but then use Gauss-Newton. See ?nls -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com [[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.