[R] Non-linear Least Square Optimization -- Function of two variables.

Berend Hasselman bhh at xs4all.nl
Wed Jan 18 16:11:45 CET 2012


On 18-01-2012, at 14:25, Lorenzo Isella wrote:

> Dear All,
> In the past I have often used minpack (http://bit.ly/zXVls3) relying
> on the Levenberg-Marquardt algorithm to perform non-linear fittings.
> However, I have always dealt with a function of a single variable.
> Is there any difference if the function depends on two variables?
> To fix the ideas, please consider the function
> 
> f(R,N)=(a/(log(2*N))+b)*R+c*N^d,
> 
> where a,b,c,d are fit parameters.
> For a set of values {N_i, R_i}, i=1,2,.....etc... can I use minpack as
> for the case of function of a single variable?

Yes.

> Should I instead resort to another package?

No necessarily.
Here is an example


fRN <- function(par,R,N,Y) {
    a <- par[1]
    b <- par[2]
    c <- par[3]
    d <- par[4]
    res <- Y - ((a/(log(2*N))+b)*R+c*N^d)
}

set.seed(1)
T <- 50
R <- runif(T)
N <- 4+runif(T)

a <- 1
b <- 2
c <- .5
d <- .7

u <- rnorm(T)/5 
sum(u^2)
Y <- (a/(log(2*N))+b)*R+c*N^d  + u

plot(Y)

library(minpack.lm)
  
par.start <- c(1,1,.2,.3)

nls.lm.control(maxiter=100)
nls.lm(par.start, fRN, R=R, N=N, Y=Y)


Berend



More information about the R-help mailing list