[R] question about formulating a nls optimization
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Fri Jul 18 21:04:02 CEST 2003
Adi Humbert <adrian_humbert at yahoo.com> writes:
> Dear list,
>
> I'm migrating a project from Matlab to R, and I'm
> facing a relatively complicated problem for nls. My
> objective function is below:
>
> >> objFun <- function(yEx,xEx,tEx,gamma,theta,kappa){
> yTh <- pdfDY(xEx,tEx,gamma,theta,kappa)
> sum(log(yEx/yTh)^2)
> }
>
> The equation is yTh=P(xEx,tEx) + noise.
> I collect my data in:
>
> >> data <- data.frame(xEx,tEx,yEx)
>
> And I do the nls:
> aux <- nls( ~ objFun(yEx,xEx,tEx,gamma,theta,kappa),
> data=data,
> start=list(gamma=0.085, theta=8.62*10^(-5),
> kappa=4.45*10^(-3)), trace=TRUE)
>
> I get the following error:
> 179.8911 : 8.50e-02 8.62e-05 4.45e-03
> Error in sum(rr[-(1:npar)]^2) : subscript out of
> bounds
>
> One problem that I identified is that the algorithm
> does not change the values of the parameters at all.
> It calculates the objective function OK, but the
> parameters are not updated.
>
> The function pdfDY is a Fourier integral with
> parameters xEx, tEx, gamma, theta, kappa, that I
> calculate with a Filon algorithm. The calculation of
> pdfDY is correct.
>
> Can anybody help me with this? Thank you very much,
Your objFun() is returning a scalar where nls is expecting a vector of
residuals. Try making it return just log(yEx/yTh), or rewrite as
log(yEx) ~ log(pdfDY(xEx,tEx,gamma,theta,kappa))
or use optim() to minimize general functions.
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list