Re: [R] solve nonlinear equation using BBsolve
After my reply you sent me the following privately: Thank you for your respond. Now I adjust the parameters which are close to the value that I'm expected and it gives me the following message: I tried a few combination. My question is why it said Unsuccessful convergence but still give some answers? Can I use the answers? p0 - c(2.5,25,25,0.8) mgf_gammasum(p0) [1] -17.360 -3410.690 0.3508769-1.1028287 BBsolve(par = p0, fn = mgf_gammasum)[[1]] Unsuccessful convergence. [1] 2.066909 44.068739 24.809270 0.972542 source(.trPaths[5], echo=TRUE, max.deparse.length=1) p0 - c(1.7,36,50,0.9) mgf_gammasum(p0) [1]3.840 2601.0300.72320210.2866732 BBsolve(par = p0, fn = mgf_gammasum)[[1]] Unsuccessful convergence. [1] 2.0823407 18.3757502 49.9935914 0.945 p0 - c(2,40,40,0.8) mgf_gammasum(p0) [1] 17.640 2798.7100.4883676 -0.5653881 BBsolve(par = p0, fn = mgf_gammasum)[[1]] Unsuccessful convergence. [1] 2.059853 29.215478 39.882727 0.914894 It is only giving you the values it stopped at. You are only printing [[1]] of BBsolve's result. BBsolve provides more information. You can easily check if the result is usable. Do something like this. p0 - c(2.5,25,25,0.8) bb.result - BBsolve(par = p0, fn = mgf_gammasum) bb.result mgf_gammasum(bb.result$par) You will see that BBsolve has NOT found a solution. If you use nleqslv as follows you will see that the jacobian matrix in your starting point is very ill-conditioned. nleqslv(p0,mgf_gammasum) All your other starting points have similar problems. You really need to rethink your system of equations. In future please also reply to the list and not only to me privately. best Berend -- View this message in context: http://r.789695.n4.nabble.com/solve-nonlinear-equation-using-BBsolve-tp3052167p3053902.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] solve nonlinear equation using BBsolve
Hi r-users, I would like to solve system of nonlinear equation using BBsolve function and below is my code. I have 4 parameters and I have 4 eqns. mgf_gammasum - function(p) { t - rep(NA, length(p)) mn - 142.36 vr - 9335.69 sk - 0.8139635 kur - 3.252591 rh - 0.896 # cumulants k1 - p[1]*(p[2]+p[3]) k2 - p[1]*(2*p[2]*p[3]*p[4] +p[2]^2+p[3]^2) k3 - 2*p[1]*(p[2]+p[3])*(p[2]^2 + p[3]^2 - p[2]*p[3] + 3*p[2]*p[3]*p[4]) k4 - 6*p[1]*((p[2]+p[3])^2*(p[2]^2 + p[3]^2 - 2*p[2]*p[3] + 4*p[2]*p[3]*p[4])+ 2*p[2]^2*p[3]^2*(1-p[4])^2) t[1] - k1 - mn t[2] - k2 - vr t[3] - k3/(k2^1.5) - sk t[4] - k4/(k2^2) - kur t } I tried this p0 - rep(0, 4) BBsolve(par = p0, fn = mgf_gammasum) dfsane(par = p0, fn = mgf_gammasum, control = list(trace = FALSE)) sane(par = p0, fn = mgf_gammasum, control = list(trace = FALSE)) and got the error message: BBsolve(par = p0, fn = mgf_gammasum) Error in optim(par = par, fn = U, method = Nelder-Mead, control = list(maxit = 100), : function cannot be evaluated at initial parameters Error in optim(par = par, fn = U, method = Nelder-Mead, control = list(maxit = 100), : function cannot be evaluated at initial parameters Error in optim(par = par, fn = U, method = Nelder-Mead, control = list(maxit = 100), : function cannot be evaluated at initial parameters Error in optim(par = par, fn = U, method = Nelder-Mead, control = list(maxit = 100), : function cannot be evaluated at initial parameters Error in optim(par = par, fn = U, method = Nelder-Mead, control = list(maxit = 100), : function cannot be evaluated at initial parameters Error in optim(par = par, fn = U, method = Nelder-Mead, control = list(maxit = 100), : function cannot be evaluated at initial parameters Error in BBsolve(par = p0, fn = mgf_gammasum) : object ans.best not found dfsane(par = p0, fn = mgf_gammasum, control = list(trace = FALSE)) Error in dfsane(par = p0, fn = mgf_gammasum, control = list(trace = FALSE)) : Failure in initial functional evaluation. sane(par = p0, fn = mgf_gammasum, control = list(trace = FALSE)) Error in sane(par = p0, fn = mgf_gammasum, control = list(trace = FALSE)) : Failure in initial functional evaluation Does anybody has any idea what is wrong? Any suggetsion is very much appreciated. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] solve nonlinear equation using BBsolve
Roslina Zakaria wrote: Hi r-users, I would like to solve system of nonlinear equation using BBsolve function and below is my code. I have 4 parameters and I have 4 eqns. mgf_gammasum - function(p) { t - rep(NA, length(p)) mn - 142.36 vr - 9335.69 sk - 0.8139635 kur - 3.252591 rh - 0.896 # cumulants k1 - p[1]*(p[2]+p[3]) k2 - p[1]*(2*p[2]*p[3]*p[4] +p[2]^2+p[3]^2) k3 - 2*p[1]*(p[2]+p[3])*(p[2]^2 + p[3]^2 - p[2]*p[3] + 3*p[2]*p[3]*p[4]) k4 - 6*p[1]*((p[2]+p[3])^2*(p[2]^2 + p[3]^2 - 2*p[2]*p[3] + 4*p[2]*p[3]*p[4])+ 2*p[2]^2*p[3]^2*(1-p[4])^2) t[1] - k1 - mn t[2] - k2 - vr t[3] - k3/(k2^1.5) - sk t[4] - k4/(k2^2) - kur t } I tried this p0 - rep(0, 4) BBsolve(par = p0, fn = mgf_gammasum) dfsane(par = p0, fn = mgf_gammasum, control = list(trace = FALSE)) sane(par = p0, fn = mgf_gammasum, control = list(trace = FALSE)) and got the error message: BBsolve(par = p0, fn = mgf_gammasum) Error in optim(par = par, fn = U, method = Nelder-Mead, control = list(maxit = 100), : I did this: p0 - rep(0, 4) mgf_gammasum(p0) [1] -142.36 -9335.69 NaN NaN Your starting values are invalid. We've had a discussion about this function before (somewhere in May of this year). I tried this p0 - c(1,1,1,1) mgf_gammasum(p0) BBsolve(par = p0, fn = mgf_gammasum) == BBsolve can't find a solution So let's try something else library(nleqslv) nleqslv(p0,mgf_gammasum) == Oops, singular jacobian # Try to calculate the jacobian in the starting point n - length(p0) J - matrix(data=rep(0,n*n),nrow=n) eps - 1e-8 f0 - mgf_gammasum(p0) J[,1] - (mgf_gammasum(p0+c(eps+eps*p0[1],0 ,0 ,0 )) - f0)/(eps+eps*p0[1]) J[,2] - (mgf_gammasum(p0+c(0 ,eps+eps*p0[2],0 ,0 )) - f0)/(eps+eps*p0[2]) J[,3] - (mgf_gammasum(p0+c(0 ,0 ,eps+eps*p0[3],0 )) - f0)/(eps+eps*p0[3]) J[,4] - (mgf_gammasum(p0+c(0 ,0 ,0 ,eps+eps*p0[4])) - f0)/(eps+eps*p0[4]) J svd(J) Your system of equations is almost singular (at least in this starting point). Solution is to find a better starting point assuming there is one. Berend -- View this message in context: http://r.789695.n4.nabble.com/solve-nonlinear-equation-using-BBsolve-tp3052167p3052178.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.