Home > Software engineering >  How to do non-linear regression in R
How to do non-linear regression in R

Time:06-02

I have a bunch of data and I need to find the parameter values for enter image description here

CodePudding user response:

Your function is over defined. Having both B & C as adjustable parameters is redundant and thus preventing the solver from finding an optimal solution. If you can remove one of these variables or define a fixed value then nls() can then find a solution.

Your equation becomes A/cos(BC Bx)^2 D. If we want to optimize just variables B & C, it simplifies down to (BC Bx).
Let us assume your dependent variable is K which is equal to (BC Bx).
We can now attempt to solve K = (B1C1 B1x). We can now pick a random value for B say B1 and find for C minimizing error of K= (B1C1 B1x).
But say we chose a different B value say B2 there exist a different C value to minimize K = (B2C2 B2x).
Because there is an infinite number of solutions for B & C,nls() is generating the error.

So try:

values <- read.csv("testdata.csv")

C <- 1
nls(Y~A/cos(B*(C   X))^2   D, 
    data =values, start = c(A = 1, B=1, D=1))


Nonlinear regression model
  model: Y ~ A/cos(B * (C   X))^2   D
   data: values
         A          B          D 
   0.03245    1.00005 -771.33115 
 residual sum-of-squares: 87192

Number of iterations to convergence: 23 
Achieved convergence tolerance: 8.887e-06

One can try several different values for C and the convergence tolerance will be very similar.

  •  Tags:  
  • r nls
  • Related