Re: [R] Nonlinear Regression
Have you worked through the examples in the 'nls' help file, especially the following: DNase1 - subset(DNase, Run == 1) fm3DNase1 - nls(density ~ Asym/(1 + exp((xmid - log(conc))/scal)), data = DNase1, start = list(Asym = 3, xmid = 0, scal = 1), trace = TRUE) Treated - Puromycin[Puromycin$state == treated, ] weighted.MM - function(resp, conc, Vm, K) { ## Purpose: exactly as white book p. 451 -- RHS for nls() ## Weighted version of Michaelis-Menten model ## -- ## Arguments: 'y', 'x' and the two parameters (see book) ## -- ## Author: Martin Maechler, Date: 23 Mar 2001 pred - (Vm * conc)/(K + conc) (resp - pred) / sqrt(pred) } Pur.wt - nls( ~ weighted.MM(rate, conc, Vm, K), data = Treated, start = list(Vm = 200, K = 0.1), trace = TRUE) 112.5978 : 200.0 0.1 17.33824 : 205.67588840 0.04692873 14.6097 : 206.33087396 0.05387279 14.59694 : 206.79883508 0.05457132 14.59690 : 206.83291286 0.05460917 14.59690 : 206.83468191 0.05461109 # In the call to 'nls' here, 'Vm' and 'K' are in 'start' and must therefore be parameters to be estimated. # The other names passed to the global 'weighted.MM' must be columns of 'data = Treated'. # To get the residual sum of squares, first note that it is printed as the first column in the trace output. # To get that from Pur.wt, I first tried 'class(Pur.wt)'. # This told me it was of class 'nls'. # I then tried method(class='nls'). # One of the functions listed was 'residuals.nls'. That gave me the residuals. # I then tried 'sum(residuals(Pur.wt)^2)', which returned 14.59690. Hope this helps. Spencer Graves p.s. Did this answer your question? Your example did not seem to me to be self contained, which makes it more difficult for me to know if I'm misinterpreting your question. If the example had been self contained, I might have replied a couple of days ago. tronter wrote: Hello I followed the example in page 59, chapter 11 of the 'Introduction to R' manual. I entered my own x,y data. I used the least squares. My function has 5 parameters: p[1], p[2], p[3], p[4], p[5]. I plotted the x-y data. Then I used lines(spline(xfit,yfit)) to overlay best curves on the data while changing the parameters. My question is how do I calculate the residual sum of squares. In the example they have the following: df - data.frame( x=x, y=y) fit - nls(y ~SSmicmen(s, Vm, K), df) fit In the second line how would I input my function? Would it be: fit - nls(y ~ myfunction(p[1], p[2], p[3], p[4], p[5]), df) where myfunction is the actual function? My function doesnt have a name, so should I just enter it? Thanks __ R-help@stat.math.ethz.ch 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] nonlinear regression: nls, gnls, gnm, other?
Hi Johann, The current version of gnm is unable to fit this type of model, though a new version with more flexibility is soon to be released. In any case, you probably want to use nls or gnls, depending on the assumptions that can be made about the model errors. For nls it is usual to assume that the errors are normally distributed with mean zero and constant variance, though the normal assumption is not strictly necessary. If you have reason to think the errors are correlated and/or have unequal variances, then gnls would be appropriate. The examples on ?nls may be enough to get you started, Heather -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Johann Hibschman Sent: 16 January 2007 04:05 To: Turner, Heather; r-help Subject: [R] nonlinear regression: nls, gnls, gnm, other? Hi all, I'm trying to fit a nonlinear (logistic-like) regression, and I'd like to get some recommendations for which package to use. The expression I want to fit is something like: y ~ A * exp(X * Beta1) / (1 + exp(-(x + X * Beta2 - xmid)/scal)) Basically, it's a logistic function, but I want to be able to modify the saturation amplitude by a few parameters (Beta1) and shift the inflection point around with a few other parameters (Beta2). I have a ton of data, but I often have trouble getting the routine to fit. (I've been using nlin in SAS, which seems sloppier in terms of accepted convergence.) Now, from what I can tell, I can use nls, gnls, or gnm to fit something like this, but I can't tell which would be better, or if there's something else I should be trying. To do this right, though, I have to do a lot more reading, but I'd like to know where to start. (I have more of a physics/computer background, so I immediately jump to thinking of regression as minimizing some cost function across a multidimensional space and then start mumbling about simulated annealing or some such, but this isn't helping me much in interpreting the available literature.) So, does anyone have any suggestions? I imagine I'm going to have to pick up a book, but should it be Pinheiro Bates on nlme, Bates Watts, the pdf manual to gnm, or what? Thanks for any suggestions, Johann __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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] nonlinear regression: nls, gnls, gnm, other?
On Tue, 16 Jan 2007, Turner, Heather wrote: Hi Johann, The current version of gnm is unable to fit this type of model, though a new version with more flexibility is soon to be released. In any case, you probably want to use nls or gnls, depending on the assumptions that can be made about the model errors. For nls it is usual to assume that the errors are normally distributed with mean zero and constant variance, though the normal assumption is not strictly necessary. If you have reason to think the errors are correlated and/or have unequal variances, then gnls would be appropriate. nls is able to handle unequal variances since 2.3.0: from the help weights: an optional numeric vector of (fixed) weights. When present, the objective function is weighted least squares. The examples on ?nls may be enough to get you started, Heather -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Johann Hibschman Sent: 16 January 2007 04:05 To: Turner, Heather; r-help Subject: [R] nonlinear regression: nls, gnls, gnm, other? Hi all, I'm trying to fit a nonlinear (logistic-like) regression, and I'd like to get some recommendations for which package to use. The expression I want to fit is something like: y ~ A * exp(X * Beta1) / (1 + exp(-(x + X * Beta2 - xmid)/scal)) Basically, it's a logistic function, but I want to be able to modify the saturation amplitude by a few parameters (Beta1) and shift the inflection point around with a few other parameters (Beta2). I have a ton of data, but I often have trouble getting the routine to fit. (I've been using nlin in SAS, which seems sloppier in terms of accepted convergence.) Now, from what I can tell, I can use nls, gnls, or gnm to fit something like this, but I can't tell which would be better, or if there's something else I should be trying. To do this right, though, I have to do a lot more reading, but I'd like to know where to start. (I have more of a physics/computer background, so I immediately jump to thinking of regression as minimizing some cost function across a multidimensional space and then start mumbling about simulated annealing or some such, but this isn't helping me much in interpreting the available literature.) So, does anyone have any suggestions? I imagine I'm going to have to pick up a book, but should it be Pinheiro Bates on nlme, Bates Watts, the pdf manual to gnm, or what? Thanks for any suggestions, Johann __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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, [EMAIL PROTECTED] 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@stat.math.ethz.ch 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] nonlinear regression-getting the explained variation
On 11/23/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I'm trying to teach myself R, and by the way, re-learning statistics using Crawley's Statistics: an introduction using R. I've reached the regression chapter, and when it deals with non-linear regresion using the nls library I face the following problem: I follow the steps--- deer-read.table(c:\\temp\\jaws.txt,header=T) ---data available at http://www.bio.ic.ac.uk/research/crawley/statistics/data/ attach(deer) names(deer) library(nls) model2-nls(bone~a*(1-exp(-c*age)),start=list(a=120,c=0.064)) summary(model2) I've taken away those steps that regard plotting. But I hope you did actually plot the data when you tried this exercise. Also, you can, if you wish, read the data from the URL without copying the data to a local file. deer - read.table(http://www.bio.ic.ac.uk/research/crawley/statistics/data/jaws.txt;, header = TRUE) model2-nls(bone~a*(1-exp(-c*age)),deer, start=list(a=120,c=0.064)) summary(model2) Formula: bone ~ a * (1 - exp(-c * age)) Parameters: Estimate Std. Error t value Pr(|t|) a 115.580562.84365 40.645 2e-16 c 0.118820.01233 9.635 3.69e-13 Residual standard error: 13.1 on 52 degrees of freedom If you want to make things even easier you could use the self-starting model SSasympOrig as summary(fm1 - nls(bone ~ SSasympOrig(age, Asym, lrc), deer)) Formula: bone ~ SSasympOrig(age, Asym, lrc) Parameters: Estimate Std. Error t value Pr(|t|) Asym 115.5805 2.8436 40.65 2e-16 lrc -2.1302 0.1038 -20.52 2e-16 Residual standard error: 13.1 on 52 degrees of freedom The parameter lrc for that model is the natural logarithm of the rate constant 'c'. And everything goes fine, I understand the steps taken and so, but on the text Crawley says the model... and explained 84.6% of the total variation in bone lenght. I guess this is linked to the adjusted squared R for linear models, but I just can't find how to get it in this case... Actually it's just an R-squared, not an adjusted R-squared. It is being calculated as 1 - RSS/AdjSS where RSS is the residual sum of squares from the fitted model and AdjSS is the adjusted sum of squares or the sum of squares of the deviations of the observed responses from their mean. In this case the values are sum(resid(model2)^2) # RSS [1] 8929.143 with(deer, sum((bone - mean(bone))^2)) # AdjSS [1] 59007.99 so the value of R^2 from the formula is with(deer, 1 - sum(resid(model2)^2)/sum((bone - mean(bone))^2)) [1] 0.848679 I've tried in Statgraphics, and it plots the anova table and r^2 right the way, how could I do so in R? Yes, many software packages that fit nonlinear regression models do produce an anova table and an R^2 value for any model, even when that table and the R^2 value do not apply. (I'm assuming that the anova table is based on dividing the adjusted sum of squares into model and residual components so it is essentially the same calculation as above.) It would not be difficult to include these values in the summary output from an nls model in R but we don't because they could be nonsense. To see why we must examine what the R^2 should represent. First you should read what Bill Venables has to say on the general subject of analysis of variance for linear models. Use install.packages(fortunes); library(fortunes); fortune(curious) All the summaries like an anova table or an R^2 value are based on the comparison of two model fits - the model we just fit to the data and the corresponding trivial model. The trivial model is either an arbitrary constant or zero, depending on whether the model consisting of a constant only can be embedded in the model we have fit. If we can select values of the parameters that turn our model into a one-parameter model consisting of a constant then the comparison sum of squares is AdjSS as above because the parameter estimate in the trivial model is mean(response). If we can't embed the constant model in our model then the appropriate comparison sum of squares is the unadjusted sum of squares sum(response^2) Technically we can't embed the model for which the predictions are an arbitrary constant in this model because of that point at age == 0. No matter how we change the parameters 'a' and 'c' in a*(1-exp(-c*age)) we always predict zero at age == 0. Thus the only model with constant predictions that is embedded in this model is the model that predicts 0 for all ages so we should use the unadjusted sum of squares. However, if we ignore the point at age == 0 (which doesn't contribute any information to the model fit) then we can get a constant model by letting c go to +Inf. As c goes to +Inf the conditional estimate of a goes to mean(bone) so the constant model is embedded in the model we have fit. We don't include the R^2 or the anova table in the summary output for a nonlinear regression model because we can't tell if these are the appropriate formulas. Look at the model in
Re: [R] Nonlinear Regression model: Diagnostics
I don't know how to get the error message you reported. The following modification of the first 'nls' example worked for me: DNase1 - subset(DNase, Run == 1) fm1DNase1 - nls( density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1) profile(fm1DNase1) fm1DNase1.2 - nls( density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1, alg=default, trace=TRUE) profile(fm1DNase1.2) Have you made scatterplots indicating that the model you are trying to fit seems plausible? If yes, I suggest you try to produce an extremely simple, self-contained example that generates your error message, then send that to this list. Before you submit another post, however, please read the posting guide! www.R-project.org/posting-guide.html. Some people have reported that the posting guide helped them solve their own problem. Failing that, I know that at least one of the R Project's leading contributors has a policy of not responding to posts that seem inconsistent with the suggestions in that guide. Even without that, I believe that posts more consistent with that guide tend to be clearer and easier to understand. This tends to increase chances of getting (quickly) the information you most need to proceed. hope this helps, spencer graves Sachin J wrote: Hi, I am trying to run the following nonlinear regression model. nreg - nls(y ~ exp(-b*x), data = mydf, start = list(b = 0), alg = default, trace = TRUE) OUTPUT: 24619327 : 0 24593178 : 0.0001166910 24555219 : 0.0005019005 24521810 : 0.001341571 24500774 : 0.002705402 24490713 : 0.004401078 24486658 : 0.00607728 24485115 : 0.007484372 24484526 : 0.008552635 24484298 : 0.009314779 24484208 : 0.009837009 24484172 : 0.01018542 24484158 : 0.01041381 24484152 : 0.01056181 24484150 : 0.01065700 24484149 : 0.01071794 24484148 : 0.01075683 24484148 : 0.01078161 24484148 : 0.01079736 24484148 : 0.01080738 24484148 : 0.01081374 Nonlinear regression model model: y ~ exp(-b * x) data: mydf b 0.01081374 residual sum-of-squares: 24484148 My question is how do I interpret the results of this model. profile(nreg) 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : 24484156 : Error in prof$getProfile() : number of iterations exceeded maximum of 50 I am unable to understand the error cause. Any pointers would be of great help. Regards, Sachin - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] nonlinear regression with M estimation
the package nlrq does median nonlinear regression... among other things. url:www.econ.uiuc.edu/~rogerRoger Koenker email [EMAIL PROTECTED] Department of Economics vox:217-333-4558University of Illinois fax:217-244-6678Champaign, IL 61820 On Jul 5, 2004, at 11:08 AM, Ruei-Che Liu wrote: Hi All, Could any one tells me if R or S has the capacity to fit nonlinear regression with Huber's M estimation? Any suggestion is appreciated. I was aware of 'rlm' in MASS library for robust linear regression and 'nls' for nonlinear least squares regression, but did not seem to be able to find robust non-linear regression function. Thanks and regards, Ray Liu __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] nonlinear regression with M estimation
I don't think there is one. One problem is that both nls and robust procedures need a starting point and so you would need a good non-linear resistant method to start. (For certain Huber-type linear regressions you can show there is a unique solution and so any starting point will do. But that is rather unusual.) The nearest equivalent I can think of is package nlrq, which also needs suitable starting values. Once you have those, you could just call optim to minimize the log-likelihood under the Huber long-tailed model. On Mon, 5 Jul 2004, Ruei-Che Liu wrote: Could any one tells me if R or S has the capacity to fit nonlinear regression with Huber's M estimation? Any suggestion is appreciated. I was aware of 'rlm' in MASS library for robust linear regression and 'nls' for nonlinear least squares regression, but did not seem to be able to find robust non-linear regression function. -- Brian D. Ripley, [EMAIL PROTECTED] 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 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] nonlinear regression and Excel solver
On Wednesday 14 January 2004 19:57, Kristian Omland wrote: [snip] Is Excel's Solver an adequate tool for numerical approximation in general and nonlinear regression in particular? Or should I push on writing S-Plus code? From what I've heard (and I know some expert users), Excel's solver is pretty good. It may need to be restarted several times to come to its final resting place. However, despite their considerable visual appeal and ease of learning, there are significant drawbacks, in my opinion, to using spreadsheets for this type of work: (1) You can't easily review your source code to see what is happening. The operations are hidden in cell formulas, or even worse, in macros. These must be examined one at a time. (2) There are no inherent loop structures. (3) It's easy to change a formula when you just mean to change a data value. (4) Expansion to a different dimensionality can be more bug-prone than with a programming language like R (or C or Fortran). Of course, opinions vary. Is anyone out there interested in assisting me with S-Plus code with the potential payoff of collaboration on a publication in the ecological literature? I would be interested if not already overcommitted (and by a large factor). Obviously, I would be equally enthused if an R user was interested in a collaboration. I do hope you will find someone. Good luck with your research! (We all need it.) -- Michael Prager NOAA Center for Coastal Fisheries and Habitat Research Beaufort, North Carolina 28516 USA http://shrimp.ccfhrb.noaa.gov/~mprager/ NOTE: Opinions expressed are personal, not official. No government endorsement of any product is made or implied. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] nonlinear regression and Excel solver
Hi, I don't know S-Plus and its functions nlminb() and ms(). However, in R I would use optim(), optimize or nlm(). I used these functions quiet often and had only very few problems. I think that R is better, easier and more flexible than Excel (at least in the long run), but since I don't anything about the Lefkovitch matrix framework I might be wrong in this case. Best wishes, Arne On Wednesday 14 January 2004 19:57, Kristian Omland wrote: Hi all, Earlier today I posted this question on s-news, so apologies to some for the duplication. Please put aside your snobbery about Microsoft products for a moment. I am fitting population models to annual survey data for trout. For those of you familiar with ecological models, I am working in the Lefkovitch matrix framework; for those unfamiliar with that shorthand, the modeled variable is a vector of abundances of fish in five size classes, with a system of linear equations (represented by a matrix) governing survival, advancement from smaller to larger stages, and reproduction. So far, I have been using a likelihood approach in an Excel spreadsheet. The spreadsheet includes the annual survey data, the Lefkovitch matrix, and projections of the model, i.e., realizations to be compared to the data. It computes the negative log-likelihood of each realization assuming log-normally distributed noise and the sum of those likelihood components. I use the Solver add-in to minimize the negative log-likelihood over the parameters in the Lefkovitch matrix. I have made a tentative stab at using nlminb() [minor success] and ms() [no success] to fit the model in S-Plus, but my proficiency is such that I still have greater flexibility fitting the models with Excel. Thus my question for you all is ... Is Excels Solver an adequate tool for numerical approximation in general and nonlinear regression in particular? Or should I push on writing S-Plus code? Is anyone out there interested in assisting me with S-Plus code with the potential payoff of collaboration on a publication in the ecological literature? Obviously, I would be equally enthused if an R user was interested in a collaboration. Thanks in advance, Kristian -- Arne Henningsen Department of Agricultural Economics University of Kiel Olshausenstr. 40 D-24098 Kiel (Germany) Tel: +49-431-880 4445 Fax: +49-431-880 1397 [EMAIL PROTECTED] http://www.uni-kiel.de/agrarpol/ahenningsen/ __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html