6

I'm trying to run a nonlinear multiple regression in R with a dataset, it has thousands of rows so I'll just put the first few here:

      Header.1 Header.2   Header.3  Header.4 Header.5 Header.6 Header.7
1          -60      -45 615 720        1.8318          0.428    -11.614
2          -59      -45 616 720        1.8322          0.429    -11.498
3          -58      -45 617 720        1.8326          0.430    -11.383
4          -57      -45 618 720        1.8330          0.430    -11.267
5          -56      -45 619 720        1.8334          0.431    -11.152
6          -55      -45 620 720        1.8338          0.432    -11.036
7          -54      -45 621 720        1.8342          0.433    -10.921
8          -53      -45 622 720        1.8346          0.433    -10.806
9          -52      -45 623 720        1.8350          0.434    -10.691
10         -51      -45 624 720        1.8354          0.435    -10.576
11         -50      -45 625 720        1.8357          0.435    -10.461
12         -49      -45 626 720        1.8361          0.436    -10.347
13         -48      -45 627 720        1.8365          0.437    -10.232
14         -47      -45 628 720        1.8369          0.438    -10.118
15         -46      -45 629 720        1.8373          0.438    -10.003
16         -45      -45 630 720        1.8377          0.439     -9.889
17         -44      -45 631 720        1.8381          0.440     -9.775
18         -43      -45 632 720        1.8385          0.440     -9.660
19         -42      -45 633 720        1.8389          0.441     -9.546
20         -41      -45 634 720        1.8393          0.442     -9.432
21         -40      -45 635 720        1.8397          0.442     -9.318
22         -39      -45 636 720        1.8400          0.443     -9.205
23         -38      -45 637 720        1.8404          0.444     -9.091
24         -37      -45 638 720        1.8408          0.445     -8.977
25         -36      -45 639 720        1.8412          0.445     -8.864
26         -35      -45 640 720        1.8416          0.446     -8.751
27         -34      -45 641 720        1.8420          0.447     -8.637
28         -33      -45 642 720        1.8424          0.447     -8.524

Can someone please explain to me in very simple terms how to run a nonliner multiple regression using Header.1 and Header.2 as independent variables and Header.7 as the dependent variable? I successfully ran a linear multiple regression using lm(), but when I tried to use nls(), I got the folloiwng error message:

Error in getInitial.default(func, data, mCall = as.list(match.call(func, : no 'getInitial' method found for "function" objects

If you need more information in order to be able to run the regression let me know. Thanks.

LyzandeR
  • 34,139
  • 12
  • 63
  • 78
japem
  • 869
  • 3
  • 13
  • 27
  • 3
    Can you show the actual code you tried to run? – mgamba Dec 21 '13 at 15:45
  • Sounds like you need to set initial values - see R docs for NLS and look at examples for the "start" argument to NLS. – alex keil Dec 21 '13 at 15:55
  • nls(Header.7 ~ Header.1 + Header.2, data = mydataset) I have a suspicion that that is totally wrong but I'm not sure how to do it right. I did it like I would do with lm() but I'm not sure if that's how it's supposed to be done. – japem Dec 21 '13 at 15:58
  • what nonlinear model do you want to fit?? I can see that `Header.1` and `Header.2` are the predictor variables, but what is the nonlinear function that relates them to the response? – Ben Bolker Dec 21 '13 at 16:49

2 Answers2

9

In order to use nls, you need to specify both a formula and start values for the variables. So the first thing to do is decide what kind of nonlinear formula you want to try and fit.

For example, if you do this:

m2<-nls(Header.7 ~ Header.1*a + Header.2*b + c,data=data,start=c(a=0,b=0,c=0))

then you will get (approximately) the same result as an ordinary linear regression, because the model you are fitting is linear. There is no "default" non-linear regression, so you need to figure out what kind of non-linear model you want to fit. See ?nls for details.

mrip
  • 13,932
  • 4
  • 34
  • 52
  • 1
    Ah... that's kind of annoying. Thanks for clearing it up, though. I feel like it should be possible for R to figure it out by itself without me having to set some coefficients, but whatever. – japem Dec 23 '13 at 01:46
  • How do you figure out where to start.. in this case where there are 4 predictors, there should a better way to start than just going with any model and trying out all possibilities? – Kira Nov 06 '16 at 18:34
  • `SSasymp()` helps pick these coefficients – CrunchyTopping Apr 29 '20 at 15:32
5

So in a linear model, the parameters are specified implicitly:

fit <- lm(Header.7 ~ Header.1 + Header.2, data=...)

will fit the model:

Header.7 = a * Header.1 + b * Header.2 + c

In a non-linear model, you have to specify the parameters explicitly, as @mrip demonstrates. Of course, in a non-linear model, the model formula can be arbitrarily complex:

fit <- nls(Header.7 ~ exp(a*Header.1 + b/Header.2), data=..., start=c(...))

Finally, start is optional: nls(...) will make a guess. But there is no guarantee that the model will converge to meaningful parameter values, or even converge at all.

jlhoward
  • 52,898
  • 6
  • 81
  • 125