Ruggero
Ruggero

Reputation: 291

Use nls with fixed parameters?

I've been trying to use the nls function to fit experimental data to a model that I have, expressed by a function of 3 parameters, let's say a, b and c. However, I would like to keep b and c fixed, since I know their true value, and fit only the parameter a:

nls(formula=pattern~myfunction(a, b, c), start=list(a=estimate_a), control=list(maxiter=50, tol=5e-8, warnOnly=T), algorithm="port", weights=sqrt(pattern), na.action=na.exclude, lower=0, upper=1)

But apparently this does not work... How can I tell R that b and c are fixed?

Upvotes: 2

Views: 2740

Answers (3)

J C Nash
J C Nash

Reputation: 56

The issue of fixed (or MASKED) parameters has been around a long time. Ron Duggleby of U. of Queensland introduced me to the term "masked" when I was on sabbatical there in 1987, and I have had masks in my own software for nonlinear optimization and nonlinear least squares since. In particular, the CRAN package "nlsr" or the developmental "nlsr2" (https://gitlab.com/nashjc/improvenls/-/tree/master/nlsr-rox) handle fixed parameters reliably.

Another approach is to use "nls()" with the "port" algorithm and set upper and lower bounds equal for the fixed parameters. I'm not sure if this is pushing the envelope, and have only tried a couple of examples. For those examples, "minpack.lm::nlsLM()" using the same equal bounds approach seems to give incorrect results sometimes.

John Nash

Upvotes: 1

G. Grothendieck
G. Grothendieck

Reputation: 269586

To fix a parameter (1) set it before running nls and (2) do not include it in start. Here is a self contained example showing the fixing of a to 0 :

a <- 0
nls(demand ~ a + b * Time, BOD, start = list(b = 1))

Upvotes: 7

Lars Lau Raket
Lars Lau Raket

Reputation: 1974

A quick solution:

my_new_function <- function(a) myfunction(a, b = b_true, c = c_true)
nls(formula = pattern ~ my_new_function(a), start = list(a = estimate_a), 
    control = list(maxiter = 50, tol = 5e-8, warnOnly = TRUE), algorithm = "port", 
    weights = sqrt(pattern), na.action = na.exclude, lower = 0, upper = 1)

Upvotes: 0

Related Questions