In my work, I use l-moments for estimation and obtain a system of nonlinear equations. I am using the 'nleqslv' package in the R- program to solve these equations but am struggling to choose initial values. Is there any criteria to choose initial values in this package or is there any other method to solve these equations? My system of equations are given below. simeqn=function(x){ y=numeric(4) y[1]=x[1]*(((gamma(1+x[2])*gamma(x[3]-x[2]))/gamma(x[3]))+((gamma(1- x[2])*gamma(x[4]+x[2]))/gamma(x[4])))- 38353 y[2]=x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(gamma(2*x[3]-x[2])/gamma(2*x[3]))-(gamma(x[4]+x[2])/gamma(x[4]))+(gamma(2*x[4]+x[2])/gamma(2*x[4])))- 3759.473 y[3]=x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(3*gamma(2*x[3]-x[2])/gamma(2*x[3]))+(2*gamma(3*x[3]-x[2])/gamma(3*x[3]))+(gamma(x[4]+x[2])/gamma(x[4]))-(3*gamma(2*x[4]+x[2])/gamma(2*x[4]))+(2*gamma(3*x[4]+x[2])/gamma(3*x[4])))- 966.3958 y[4]x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(6*gamma(2*x[3]-x[2])/gamma(2*x[3]))+(10*gamma(3*x[3]-x[2])/gamma(3*x[3]))-(5*gamma(4*x[3]-x[2])/gamma(4*x[3]))-(gamma(x[4]+x[2])/gamma(x[4]))+(6*gamma(2*x[4]+x[2])/gamma(2*x[4]))-(10*gamma(3*x[4]+x[2])/gamma(3*x[4]))+(5*gamma(4*x[4]+x[2])/gamma(4*x[4])))- 500.952 y } [[alternative HTML version deleted]]
I would suggest treating initial values as hyperparameters. Try a range of values to understand how your choice influences your outcome. Plot the result. Eventually (I hope) you will get a feel for the right answer for your specific type of data and you will be able to reduce the time needed for this activity. If you want to be safe I would use the microbenchmark package to determine program run time. If runtimes are short then I might try a range of 0 to 10 in steps of 0.01 and see what happens. If there is an interesting region I might then focus on that in finer steps. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of ASHLIN VARKEY Sent: Tuesday, November 15, 2022 2:49 AM To: r-help at r-project.org Subject: [R] Initial value choosing in nleqslv package [External Email] In my work, I use l-moments for estimation and obtain a system of nonlinear equations. I am using the 'nleqslv' package in the R- program to solve these equations but am struggling to choose initial values. Is there any criteria to choose initial values in this package or is there any other method to solve these equations? My system of equations are given below. simeqn=function(x){ y=numeric(4) y[1]=x[1]*(((gamma(1+x[2])*gamma(x[3]-x[2]))/gamma(x[3]))+((gamma(1- x[2])*gamma(x[4]+x[2]))/gamma(x[4])))- 38353 y[2]=x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(gamma(2*x[3]-x[2])/gamma(2*x[3]))-(gamma(x[4]+x[2])/gamma(x[4]))+(gamma(2*x[4]+x[2])/gamma(2*x[4])))- 3759.473 y[3]=x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(3*gamma(2*x[3]-x[2])/gamma(2*x[3]))+(2*gamma(3*x[3]-x[2])/gamma(3*x[3]))+(gamma(x[4]+x[2])/gamma(x[4]))-(3*gamma(2*x[4]+x[2])/gamma(2*x[4]))+(2*gamma(3*x[4]+x[2])/gamma(3*x[4])))- 966.3958 y[4]x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(6*gamma(2*x[3]-x[2])/gamma(2*x[3]))+(10*gamma(3*x[3]-x[2])/gamma(3*x[3]))-(5*gamma(4*x[3]-x[2])/gamma(4*x[3]))-(gamma(x[4]+x[2])/gamma(x[4]))+(6*gamma(2*x[4]+x[2])/gamma(2*x[4]))-(10*gamma(3*x[4]+x[2])/gamma(3*x[4]))+(5*gamma(4*x[4]+x[2])/gamma(4*x[4])))- 500.952 y } [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7C7119f0c53d984aed0be908dac7156d81%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638041192349108654%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=E%2FVs%2FPCFnXV9LVsgg24SqBmb7LXB890OaIdqfoHjuFM%3D&reserved=0 PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C7119f0c53d984aed0be908dac7156d81%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638041192349108654%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=A9pn7JDIrGgJJO0g2bdHmrYRo2UCO9yxMiGiOORO8HY%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code.
There is a reason linear algebra techniques are so popular... they are much more deterministic than nonlinear techniques. Deriving a method of starting a nonlinear optimization is highly specific to the equations, not to the optimization algorithm. In some cases you can create an approximation using linear algebra to find a reasonable starting point. In others, people develop other heuristics by studying asymptotes of the equations. In orhers, there is just always some chance that the user will have to try and fail until they get something that works. On November 14, 2022 11:49:02 PM PST, ASHLIN VARKEY <ashlinvarkey at gmail.com> wrote:>In my work, I use l-moments for estimation and obtain a system of >nonlinear equations. I am using the 'nleqslv' package in the R- program to >solve these equations but am struggling to choose initial values. Is there >any criteria to choose initial values in this package or is there any other >method to solve these equations? My system of equations are given below. > > simeqn=function(x){ > > y=numeric(4) > > y[1]=x[1]*(((gamma(1+x[2])*gamma(x[3]-x[2]))/gamma(x[3]))+((gamma(1- >x[2])*gamma(x[4]+x[2]))/gamma(x[4])))- 38353 > > >y[2]=x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(gamma(2*x[3]-x[2])/gamma(2*x[3]))-(gamma(x[4]+x[2])/gamma(x[4]))+(gamma(2*x[4]+x[2])/gamma(2*x[4])))- >3759.473 > > >y[3]=x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(3*gamma(2*x[3]-x[2])/gamma(2*x[3]))+(2*gamma(3*x[3]-x[2])/gamma(3*x[3]))+(gamma(x[4]+x[2])/gamma(x[4]))-(3*gamma(2*x[4]+x[2])/gamma(2*x[4]))+(2*gamma(3*x[4]+x[2])/gamma(3*x[4])))- >966.3958 > > y[4]>x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(6*gamma(2*x[3]-x[2])/gamma(2*x[3]))+(10*gamma(3*x[3]-x[2])/gamma(3*x[3]))-(5*gamma(4*x[3]-x[2])/gamma(4*x[3]))-(gamma(x[4]+x[2])/gamma(x[4]))+(6*gamma(2*x[4]+x[2])/gamma(2*x[4]))-(10*gamma(3*x[4]+x[2])/gamma(3*x[4]))+(5*gamma(4*x[4]+x[2])/gamma(4*x[4])))- >500.952 > > y > >} > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.
A rather crude approach to solving nonlinear equations is to rewrite the equations as residuals and minimize the sum of squares. A zero sumsquares gives a solution. It is NOT guaranteed to work, of course. I recommend a Marquardt approach minpack.lm::nlslm or my nlsr::nlfb. You will need to specify a function for the jacobian (derivatives of the residuals w.r.t. the parameters) for the latter package, but it does handle bounds correctly. Bounds in minpack.lm can be specified but I've a couple of failed cases, though I'll caution that in this area the scaling is such that all packages can be made to fail in some cases. I can send the latest developmental version of the nlsr package with some updates to make use of jacobians easier if you take that route. JN On 2022-11-15 02:49, ASHLIN VARKEY wrote:> In my work, I use l-moments for estimation and obtain a system of > nonlinear equations. I am using the 'nleqslv' package in the R- program to > solve these equations but am struggling to choose initial values. Is there > any criteria to choose initial values in this package or is there any other > method to solve these equations? My system of equations are given below. > > simeqn=function(x){ > > y=numeric(4) > > y[1]=x[1]*(((gamma(1+x[2])*gamma(x[3]-x[2]))/gamma(x[3]))+((gamma(1- > x[2])*gamma(x[4]+x[2]))/gamma(x[4])))- 38353 > > > y[2]=x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(gamma(2*x[3]-x[2])/gamma(2*x[3]))-(gamma(x[4]+x[2])/gamma(x[4]))+(gamma(2*x[4]+x[2])/gamma(2*x[4])))- > 3759.473 > > > y[3]=x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(3*gamma(2*x[3]-x[2])/gamma(2*x[3]))+(2*gamma(3*x[3]-x[2])/gamma(3*x[3]))+(gamma(x[4]+x[2])/gamma(x[4]))-(3*gamma(2*x[4]+x[2])/gamma(2*x[4]))+(2*gamma(3*x[4]+x[2])/gamma(3*x[4])))- > 966.3958 > > y[4]> x[1]*gamma(1+x[2])*((gamma(x[3]-x[2])/gamma(x[3]))-(6*gamma(2*x[3]-x[2])/gamma(2*x[3]))+(10*gamma(3*x[3]-x[2])/gamma(3*x[3]))-(5*gamma(4*x[3]-x[2])/gamma(4*x[3]))-(gamma(x[4]+x[2])/gamma(x[4]))+(6*gamma(2*x[4]+x[2])/gamma(2*x[4]))-(10*gamma(3*x[4]+x[2])/gamma(3*x[4]))+(5*gamma(4*x[4]+x[2])/gamma(4*x[4])))- > 500.952 > > y > > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.