nlsic | R Documentation |
Solve non linear least squares problem min_par ||r(par,...)$res||
with optional inequality constraints u%*%par >= co
and optional equality constraints e%*%par = eco
nlsic(
par,
r,
u = NULL,
co = NULL,
control = list(),
e = NULL,
eco = NULL,
flsi = lsi,
...
)
par |
initial values for parameter vector. It can be in non feasible domain,
i.e. for which |
r |
a function calculating residual vector
by a call to |
u |
constraint matrix in |
co |
constraint right hand side vector |
control |
a list of control parameters ('=' indicates default values):
|
e |
linear equality constraint matrix in |
eco |
right hand side vector in equality constraints |
flsi |
function solving linear least squares problem. For a custom function,
see interfaces in |
... |
parameters passed through to r() |
Solving method consist in sequential LSI problems globalized by backtracking technique.
If e, eco are not NULL, reduce jacobian to basis of e's kernel before lsi()
call.
NB. If the function r()
returns a list having a field "jacobian" it is
supposed to be equal to the jacobian dr/dpar.
If not, numerical derivation numDeriv::jacobian()
is automatically used for its calculation.
NB2. nlsic() does not call stop() on possible errors. Instead, 'error' field is set to 1
in the returned result. This is done to allow a user to examine the current state
of data and possibly take another path then to merely stop the program. So, a
user must allways check this field at return from nlsic().
NB3. User should test that field 'mes' is not NULL even when error is 0. It may
contain a warning message.
a list with following components (some components can be absent depending on 'control' parameter)
'par' estimated values of par
'lastp' the last LSI solution during non linear iterations
'hci' vector of half-width confidence intervals for par
'ci_p' p-value for which CI was calculated
'ci_fdeg' freedom degree used for CI calculation
'sd_res' standard deviation of residuals
'covpar' covariance matrix for par
'laststep' the last increment after possible back-tracking iterations
'normp' the norm of lastp
'res' the last residual vector
'prevres' residual vector from previous non linear iteration
'jacobian' the last used jacobian
'retres' last returned result of r()
call
'it' non linear iteration number where solution was obtained
'btit' back-tracking iteration number done during the last non linear iteration
'history' list with convergence history information
'error' error code: 0 - normal end, 1 - some error occurred, see message in 'mes'
'mes' textual message explaining what problem was in case of error
lsi, lsi_ln, uplo2uco
# solve min_{a,b} ||exp(a*x+b)-meas||, a,b>=1
a_true=1; b_true=2; x=0:5
# simulation function
sim=function(par, x) exp(par[["a"]]*x+par[["b"]])
# residual function to be passed to nlsic()
resid=function(par, cjac, ...) {
dots=list(...)
s=sim(par, dots$x)
result=list(res=s-dots$meas)
if (cjac) {
result$jacobian=cbind(a=s*dots$x, b=s)
}
result
}
# simulated noised measurements for true parameters
set.seed(7) # for reproducible results
meas=sim(c(a=a_true, b=b_true), x)+rnorm(x)
# starting values for par
par=c(a=0, b=0)
# prepare constraints
uco=uplo2uco(par, lower=c(a=1, b=1))
# main call: solve the problem
fit=nlsic(par, resid, uco$u, uco$co, control=list(trace=1), x=x, meas=meas)
if (fit$error == 1) {
stop(fit$mes)
} else {
print(fit$par) # a=1.001590, b=1.991194
if (! is.null(fit$mes)) {
warning(fit$mes)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.