Ask Your Question
1

How can constraints for model fitting be applied to a specific set of parameters using FME::modFit or optim?

asked 2022-06-11 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-07-02 15:00:00 +0000

david gravatar image

To apply constraints for model fitting to a specific set of parameters using FME::modFit or optim, you can use the "lower" and "upper" arguments in the function. These arguments define the lower and upper limits of the parameter values, ensuring that the optimization process only searches for an optimal solution within these limits.

For example, to implement a lower bound constraint on a parameter "x" with a minimum value of 0 in the modFit function, you can write:

result <- modFit(formula = y ~ a*x + b, data = mydata,
                 start = list(a = 1, b = 1, x = 0),
                 lower = c(a = -Inf, b = -Inf, x = 0))

In this example, the "lower" argument specifies that the "a" and "b" parameters have no lower bound, while the "x" parameter has a minimum value of 0.

Similarly, to implement an upper bound constraint on a parameter "x" with a maximum value of 10 in the optim function, you can write:

fn <- function(par) {
  a <- par[1]
  b <- par[2]
  x <- par[3]
  yhat <- a*x + b
  resid <- y - yhat
  return(sum(resid^2))
}
result <- optim(par = c(1, 1, 5), fn = fn,
                lower = c(-Inf, -Inf, -Inf),
                upper = c(Inf, Inf, 10))

In this example, the "upper" argument specifies that the "x" parameter has a maximum value of 10, while the "a" and "b" parameters have no upper bound. Note that the "lower" argument is also included, although it is not strictly necessary in this case as the function already defaults to "-Inf" for parameters with no lower bound.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-11 11:00:00 +0000

Seen: 7 times

Last updated: Jul 02 '22