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, Adrian
Peter Dalgaard BSA
2003-Jul-18 19:04 UTC
[R] question about formulating a nls optimization
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
Adi Humbert <adrian_humbert at yahoo.com> writes:> 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 > boundsCould you run traceback() at this point so we can see where the error message is originating? I would hope that npar is 3 and my guess is that the error message is originating in a call to conv = function() { rr <- qr.qty( QR, resid ) # rotated residual vector sqrt( sum( rr[1:npar]^2 ) / sum( rr[ -(1:npar) ]^2 ) ) }, How many rows are there in your data frame?> 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.