0

I'm working with an nls model using this formula (b1 * ((b2 * x)^b4)) / (1 + ((b2 * x)^b4))^(b3 / b4)

I use nls2 package with a random algorithm to find the initial values. This my code for the function of formula and curve:

eqn <- function(x){(b1 * ((b2 * x)^b4)) / (1 + ((b2 * x)^b4))^(b3 / b4)}


curve(eqn, data$x, data$y, col = "green", n = 6)

I don't know, if I use n = "" properly. But, I use n = 6 as the length of the equation because my data have 6 values on x and y variables.

However, when I try to make a curve for the model, it ended up on an error: object 'b1' not found.

How can I avoid this error? I avoided this error by setting up an estimated values of the variables I get using nls2 outside the structure of nls2.

For example:

b1 = 1
b2 = 5
b3 = 0.7
b4 = 9.5

**On the other hand, how to set up the values of the variables inside the curve() for the equation without getting an error: object 'b1' not found., should I use start = list()?

curve(eqn, data$x, data$y, col = "green", n = 6, start = list (b1 = 1, b2 = 5, b3 = 0.7, b = 9.5))

I found various examples about making a curve for a polynomial equation, but those examples already have defined values for the variables of the equation. Is there a way to set up the variables using the estimated values calculated from the nls2?

Dave2e
  • 22,192
  • 18
  • 42
  • 50
HYDR0GEN
  • 91
  • 1
  • 10
  • 1
    Well you need a way to pass the parameters to the function, i.e. `eqn <- function (x, b1, b2, b3, b4){...}`. The correct way to define your function depends on how `nls` is designed to work. – mikeck Jan 02 '20 at 03:13
  • 1
    Just use predict. Like this: fit <- nls(..., data = data); curve(predict(fit, newdata = data.frame(x = x)), from = 0, to = 2) – Roland Jan 02 '20 at 08:05

1 Answers1

1

You will need to define your equation to accept the coefficients b1 to b4 as inputs like this:

eqn <- function(x, b1, b2, b3, b4){
         (b1 * ((b2 * x)^b4)) / (1 + ((b2 * x)^b4))^(b3 / b4)
       }

The pass the coefficients to the calling function like this:

curve(eqn(x, b1 = 1, b2 = 5, b3 = 0.7, b4 = 9.5), from=0, to=2, col = "green", n = 60)

You need to define the starting and stopping values for x. Also select a value for "n" a large enough value for a smooth curve, n at 6 will not be smooth.

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • It works properly. Thank You. Anyway, is it possible to set up the values of the variables from the estimated values calculated using nls rather than using defined values, for instance, `b1 = 1, b2 = 5, b3 = 0.7, b4 = 9`? Because if I used defined values, then the estimation of nls will be nonsense? I try to remove the nls then the everything works properly. Therefore, I can remove the nls? – HYDR0GEN Jan 02 '20 at 03:58
  • 1
    @HYDR0GEN, I suggest you need to the perform the process in a multiple steps. First run the `nls` and obtain the results, once you verify a successful solution and obtain the coefficients then pass the coefficients to the curve function. Instead of individually defining the coefficients as I did above, you should be able to pass them as a vector. – Dave2e Jan 02 '20 at 14:18
  • I will try it immediately. Thank You for the suggestion. – HYDR0GEN Jan 02 '20 at 14:35