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.
Asked: 2022-06-11 11:00:00 +0000
Seen: 7 times
Last updated: Jul 02 '22